<?php
include("include.php");
include("phpfunctions.php");
include("getfunctions.php");


function instructor_login()
{
	$sel=executework("select * from lms_instructor_register where email_id='".$_POST['email_id']."' and password='".$_POST['password']."' and status=1");
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		$row=@mysqli_fetch_array($sel);
		$_SESSION['instructor_email']=$row['email_id'];
		$_SESSION['login_type']="Instructor";
		$_SESSION['login_id']=$row['id'];
		redirect("dashboard.php");
	}
	else
	{
		redirect("index.php?fail=1");
	}	
}





function recovery_email_check(){
 
   $sel = executework("SELECT * FROM lms_instructor_register WHERE email_id ='".$_POST['loginEmail']."'");
   
   if(mysqli_num_rows($sel) > 0){
   		redirect("recoveryAccount.php?succ=1&loginEmail=".$_POST['loginEmail']);
   }else{
   	redirect("recoveryAccount.php?fail=1");
   }

}



function change_password()
{
	
	$sel=executework("select * from lms_instructor_register where email_id ='".$_POST['email_id']."'");
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		 $row= mysqli_fetch_assoc($sel);
		 
		 if($_POST['newpassword'] === $_POST['confirmpassword']){
		 //$hashpassword = password_hash($_POST['newpassword'], PASSWORD_DEFAULT);
		
			$update=executework("update lms_instructor_register set password='".$_POST['newpassword']."' where email_id ='".$_POST['email_id']."' ");
			redirect("index.php?newpass=1");
		}else{
		 redirect("forgotpassword.php?fail=1");
		}
		
	}
	else
	{
		redirect("forgotpassword.php?fail=1");
	}
}


// change Password
function instructor_change_password()
{
	$sel=executework("select * from lms_instructor_register where id='".$_SESSION['login_id']."' and password='".$_POST['oldpassword']."'");
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		$update=executework("update lms_instructor_register set password='".$_POST['newpassword']."' where id='".$_SESSION['login_id']."' ");
		redirect("change_password.php?succ=1");
	}
	else
	{
		redirect("change_password.php?fail=1");
	}
}

function instructor_signup()
{	
	$sel=executework("select * from lms_instructor_register where email_id='".$_POST['email_id']."'");
	$cnt=@mysqli_num_rows($sel);
	
	if(!empty($_FILES['instructor_resume']['name']))
	{
		$image_name = $_FILES['instructor_resume']['name'];
		$tmp_name   = $_FILES['instructor_resume']['tmp_name'];
		$filename = $_FILES['instructor_resume']['name'];
		$len=strlen($filename);
		$pos=strrpos($filename,'.');
		$ext1=substr($filename,$pos,$len);
		$fname=substr($filename,0,$pos);
		$file_name='';
		$target_dir = "Instructor_Resume/";
		$file_name= preg_replace("/[^a-zA-Z0-9]/", "", $fname)."_".time().$ext1;
		$target_file = $target_dir.$file_name;
		move_uploaded_file($_FILES['instructor_resume']['tmp_name'],$target_file);
		$qry=",instructor_resume='".$file_name."'";

	}
	if(!empty($_POST['mid']))
	{
		
		$upd=executework("update lms_instructor_register set instructor_name='".$_POST['instructor_name']."', email_id='".$_POST['email_id']."',instructor_mobile='".$_POST['instructor_mobile']."',instructor_education='".$_POST['instructor_education']."', instructor_linkedIn='".$_POST['instructor_linkedIn']."',instructor_address='".$_POST['instructor_address']."',university_name='".$_POST['university_name']."', university_major='".$_POST['university_major']."',teaching_exp='".$_POST['teaching_exp']."'".$qry." where id='".$_POST['mid']."'");
		
		redirect("dashboard.php");
	}
	if($cnt>0)
	{
			redirect("register.php?exist=1");
	}
	else
	{
		$int=executeworkins("insert into lms_instructor_register (instructor_name,email_id,password,instructor_mobile,instructor_education,instructor_linkedIn,instructor_address,university_name,university_major,teaching_exp,instructor_resume,created_by,created_on,status) values('".$_POST['instructor_name']."','".$_POST['email_id']."','123','".$_POST['instructor_mobile']."','".$_POST['instructor_education']."','".$_POST['instructor_linkedIn']."','".$_POST['instructor_address']."','".$_POST['university_name']."','".$_POST['university_major']."','".$_POST['teaching_exp']."','".$file_name."','".$_SESSION['user_name']."','".date('Y-m-d h:i:s')."','1')");
		
//		$_SESSION['instructor_email']=$_POST['email_id'];
//		$_SESSION['login_type']="Instructor";
//		$_SESSION['login_id']=$int;
		
		redirect("index.php?succ=1");
	}
}



	function uploadFile($fileKey, $uploadDir = "") {
		if (empty($_FILES[$fileKey]['name'])) {
			return '';
		}
	
		$filename = $_FILES[$fileKey]['name'];
		$tmp_name = $_FILES[$fileKey]['tmp_name'];
	
		$pos   = strrpos($filename, '.');
		$fname = substr($filename, 0, $pos);
		$ext   = substr($filename, $pos);
	
		$newName = preg_replace("/[^a-zA-Z0-9]/", "", $fname) . "_" . time() . $ext;
	
		if (!is_dir($uploadDir)) {
			mkdir($uploadDir, 0777, true);
		}
	
		move_uploaded_file($tmp_name, $uploadDir . $newName);
	
		return $newName;
	}
	


function courses_details($mid){
 
 	$course_img = uploadFile('course_img','courses_upload/');
	
$course_overview = str_replace("'", "\'", $_POST['course_overview']);
$details = str_replace("'", "\'", $_POST['details']);

$additional_info = str_replace("'", "\'", $_POST['additional_info']);
 
 
	if($mid !=""){
			$sql = executework("SELECT * FROM lms_instructor_courses WHERE id = '".$mid."'");
			$row = mysqli_fetch_assoc($sql);
			if(empty($_FILES['course_img'])){
			  $course_img = $row['course_img'];
			}
	
	
		executework("update lms_instructor_courses set  category_id = '".$_POST['category_id']."',category_title = '".$_POST['category_title']."', course_id = '".$_POST['course_id']."', course_title ='".$_POST['course_title']."' , course_type = '".$_POST['course_type']."',course_img = '".$course_img."',course_video = '".$_POST['course_video']."',no_of_class ='".$_POST['no_of_class']."',duration = '".$_POST['duration']."',course_price = '".$_POST['course_price']."',course_discount = '".$_POST['course_discount']."',course_level = '".$_POST['course_level']."',start_date ='".$_POST['start_date']."',language = '".$_POST['language']."',course_overview = '".$course_overview."',details = '".$details."',additional_info = '".$additional_info."',grade = '".$_POST['grade']."', modified_on = '".date('Y-m-d H:i:s')."',modified_by='".$_SESSION['login_id']."' where id='".$mid."'");
		redirect("my_courses.php?succ=1");
	
	}else{
		$ins = executeworkins("insert into lms_instructor_courses (category_id,category_title,course_id, course_title,course_type,course_img, course_video,no_of_class, duration,course_price,course_discount, course_level,start_date,language,course_overview,details,additional_info,grade,created_on,created_by) values('".$_POST['category_id']."','".$_POST['category_title']."','".$_POST['course_id']."','".$_POST['course_title']."','".$_POST['course_type']."','".$course_img."','".$_POST['course_video']."','".$_POST['no_of_class']."','".$_POST['duration']."','".$_POST['course_price']."','".$_POST['course_discount']."','".$_POST['course_level']."','".$_POST['start_date']."','".$_POST['language']."','".$course_overview."','".$details."','".$additional_info."','".$_POST['grade']."','".date('Y-m-d H:i:s')."','".$_SESSION['login_id']."')");
	 redirect("my_courses.php?succ=1");	
	}
}



function course_status($sid,$stat)
{
	if($stat=='Activate')
	$status=1;
	else
	$status=0;
	$upd=update_status("lms_instructor_courses",'id',$sid,'status',$status,'');
	if($upd=='success')
	redirect("course_report.php?ssucc=1");
	else
	redirect("course_report.php?invalid=1");
}

function delete_course($sid)
{
	$del=delete_record("lms_instructor_courses","id",$sid,'');
	if($del=='deleted')
	redirect("course_report.php?dsucc=1");
	else
	redirect("course_report.php?invalid=1");
}



function get_latest_items($condition)
{
	$count=get_reccount('lms_instructor_courses','id',$condition);
	return $count;
}




function documents_upload($mid){

	$image        = uploadFile('image','doc_uploads/');
	$id_proof     = uploadFile('id_proof','doc_uploads/');
	$grad_proof    = uploadFile('grad_proof','doc_uploads/');
	$exp_cert  = uploadFile('exp_cert','doc_uploads/');
	
	if($mid!=""){
				$sql = executework("SELECT * FROM lms_instructor_documents WHERE id = '".$mid."'");
				$row = mysqli_fetch_assoc($sql);
				if(empty($_FILES['image'])){
				  $image  = $row['image'];
				}
				if(empty($_FILES['id_proof'])){
				  $id_proof = $row['id_proof'];
				}
				if(empty($_FILES['grad_proof'])){
				   $grad_proof = $row['grad_proof'];
				}
				if(empty($_FILES['exp_cert'])){
				   $exp_cert = $row['exp_cert'];
				}
	
	
			executework("UPDATE lms_instructor_documents SET instructor_id =  '".$_SESSION['login_id']."',image = '".$image."',id_proof = '".$id_proof."',grad_proof = '".$grad_proof."',exp_cert = '".$exp_cert."',modified_on = '".date('Y-m-d H:i:s')."',created_by = '".$_SESSION['login_id']."'  WHERE id = '".$mid."'");
	redirect("documentation.php?succ=1");
	
	}else{
		  executeworkins("insert into lms_instructor_documents (instructor_id,image,id_proof,grad_proof,exp_cert,created_on,created_by) values ('".$_SESSION['login_id']."','".$image."','".$id_proof."','".$grad_proof."','".$exp_cert."','".date('Y-m-d H:i:s')."','".$_SESSION['login_id']."')");
	
	redirect("documentation.php?succ=1");
	}

}



function documents_status($sid,$stat)
{
	if($stat=='Activate')
	$status=1;
	else
	$status=0;
	$upd=update_status("lms_instructor_documents",'id',$sid,'status',$status,'');
	if($upd=='success')
	redirect("documentation.php?ssucc=1");
	else
	redirect("documentation.php?invalid=1");
}

function delete_documents($sid)
{
	$del=delete_record("lms_instructor_documents","id",$sid,'');
	if($del=='deleted')
	redirect("documentation.php?dsucc=1");
	else
	redirect("documentation.php?invalid=1");
}




function modules($view_id){

    $course_id    = $_POST['course_id'];
    $modules      = $_POST['module_title']; 

    if($_POST['mid'] != ""){
   
                executework(" UPDATE lms_instructor_modules  SET  course_id    = '".$course_id."',  module_title = '".  $modules ."',   modified_on  = '".date('Y-m-d H:i:s')."',   modified_by  = '".$_SESSION['login_id']."'  WHERE id = '".$_POST['mid']."' ");
			redirect("curriculum.php?view_id=$view_id&upd=1");
    } else {

                executeworkins("INSERT INTO lms_instructor_modules  (course_id, course_title, module_title, created_on, created_by)   VALUES  ('".$course_id."','".$_POST['course_title']."', '".$modules."', '".date('Y-m-d H:i:s')."', '".$_SESSION['login_id']."') ");
				
				redirect("curriculum.php?view_id=$view_id&succ=1");

    }
}

function delete_module($did, $view_id){
	
	executework("DELETE FROM lms_instructor_modules WHERE id = '".$did."'");
	 redirect("curriculum.php?view_id=$view_id&dscuu=1");
}



function lessons_upload($mid){

 $video_upload =  uploadFile('video_upload','lessons_uploads/');
 $material_upload = uploadFile('material_upload','lessons_uploads/');
 $assignment_upload =  uploadFile('assignment_upload','lessons_uploads/');
  $text_upload =  uploadFile('text_upload','lessons_uploads/');
   
$lesson_content = str_replace("'", "\'", $_POST['lesson_content']);
  $assign_instruction= str_replace("'","\'",$_POST['assign_instruction']);
 
	

	if($mid!=""){
	
				$sql= executework("SELECT * FROM lms_instructor_lessons WHERE id = '".$mid."'");
				$row =mysqli_fetch_assoc($sql);
				if(empty($_FILES['video_upload'])){
					 $video_upload = $row['video_upload'];
				}
				if(empty($_FILES['material_upload'])){
				 $material_upload = $row['material_upload'];
				}
				if(empty($_FILES['assignment_upload'])){
				   $assignment_upload = $row['assignment_upload'];
				}
				
				if(empty($_FILES['text_upload'])){
						$text_upload =  $row['text_upload'];
				}
	
	
	
	
		executework("UPDATE lms_instructor_lessons SET  course_id = '".$_POST['course_id']."', module_id = '".$_POST['module_id']."', module_title = '".$_POST['module_title']."', lesson_title = '".$_POST['lesson_title']."',lesson_type = '".$_POST['lesson_type']."',video_url = '".$_POST['video_url']."',video_upload = '".$video_upload."',material_url = '".$_POST['material_url']."', material_upload = '".$material_upload."',assignment_upload = '".$assignment_upload."', lesson_url = '".$_POST['lesson_url']."',text_upload = '".$text_upload."',lesson_content = '".$lesson_content."',assign_instruction = '".$assign_instruction."',modified_on = '".date('Y-m-d H:i:s')."',modified_by = '".$_SESSION['login_id']."'  WHERE id = '".$mid."'");
		redirect("curriculum.php?view_id=".$_POST['course_id']."&upd=1");
		
	}else{
		executeworkins("INSERT INTO  lms_instructor_lessons (course_id,course_title,module_id,module_title, lesson_title,lesson_type,video_url,video_upload,material_url, material_upload,assignment_upload, lesson_url,text_upload,lesson_content,assign_instruction,created_on,created_by) VALUES ('".$_POST['course_id']."','".$_POST['course_title']."','".$_POST['module_id']."','".$_POST['module_title']."','".$_POST['lesson_title']."','".$_POST['lesson_type']."','".$_POST['video_url']."','".$video_upload."','".$_POST['material_url']."','".$material_upload."','".$assignment_upload."','".$_POST['lesson_url']."','".$text_upload."','".$lesson_content."','".$assign_instruction."','".date('Y-m-d H:i:s')."','".$_SESSION['login_id']."')");
		
		 $sec = executework("SELECT * FROM lms_instructor_modules WHERE id = '".$_POST['module_id']."'");
		 $row2 = mysqli_fetch_assoc($sec);
		 $count = intval($row2['no_of_lessons'] ?? 0)+1;
		 $up = executework("UPDATE lms_instructor_modules SET no_of_lessons	='".$count."'  WHERE id = '".$_POST['module_id']."'");
		
	redirect("curriculum.php?view_id=".$_POST['course_id']."&succ=1");
	}

}



function delete_lesson_data($did, $view_id){
     $sec = executework("SELECT * FROM lms_instructor_modules WHERE id = '".$did."'");
		 $row2 = mysqli_fetch_assoc($sec);
		 $count = intval($row2['no_of_lessons'] ?? 0)-1;
		 $up = executework("UPDATE lms_instructor_modules SET no_of_lessons	='".$count."'  WHERE id = '".$did."'");		
 
	executework("DELETE FROM lms_instructor_lessons WHERE id = '".$did."'");
	 redirect("curriculum.php?view_id=$view_id&dscuu=1");

}


































?>