<?php
ob_start();
session_start();
include("include.php");
include("phpfunctions.php");

function student_login()
{	
	$sel=executework("select * from lms_stud_register where stud_email='".addslashes($_POST['email_id'])."' and password='".addslashes($_POST['password'])."' and status=1");
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		$row=@mysqli_fetch_array($sel);
		$_SESSION['log_email']=$row['stud_email'];
		$_SESSION['name_log']=$row['stud_name'];
		$_SESSION['login_type']="Student";
		$_SESSION['login_id']=$row['id'];
		redirect("dashboard.php");
	}
	else
	{
		redirect("login.php?fail=1");
	}	
}

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

	}
	
	if(!empty($_POST['mid']))
	{
			$upd=executework("update lms_course_master set cat_id='".$_POST['cat_id']."',title='".$_POST['title']."',course_type='".$_POST['course_type']."'".$qry.",modified_by='".$_SESSION['login_type']."',modified_on='".date('Y-m-d h:i:s')."' where id='".$_POST['mid']."'");
	}
	
	if($cnt>0)
	{
		 redirect("register.php?exist=1");
	}
	
	else
	{
		 	$ins = executeworkins("insert into lms_stud_register(stud_name,stud_email, stud_mobile, stud_education,stud_resume,stud_linkedin,stud_address, university_name,university_major,stud_gpa,password,created_on,status) VALUES('".addslashes($_POST['stud_name'])."','".addslashes($_POST['stud_email'])."','".addslashes($_POST['stud_mobile'])."','".addslashes($_POST['stud_education'])."','".$file_name."','".addslashes($_POST['stud_linkedin'])."','".addslashes($_POST['stud_address'])."','".addslashes($_POST['university_name'])."','".addslashes($_POST['university_major'])."','".addslashes($_POST['stud_gpa'])."','".addslashes($_POST['password'])."','".date('Y-m-d H:i:s')."',1)");
		
	}
	 redirect("register.php?succ=1");
} 

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

//---------------- Courses Display
function get_courses($id,$course_id,$to_date,$status,$lmt)
{
	$qr='';$limit='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($course_id!='')
	$qr.=" and course_id='".$course_id."'";

	if($status!='')
	$qr.=" and status='".$status."'";
	if($lmt!='')
	$limit.=" limit ".$lmt;
	$sel=executework("select * from lms_instructor_courses where id<>''".$qr."order by id desc".$limit);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}  

function get_courses_count($id,$course_id,$to_date,$limit)
{
	$qr='';$lmt=''; $n=0;
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($course_id!='')
	$qr.=" and course_id='".$course_id."'";
	
	if($limit!='')
	$lmt.=" limit ".$limit;
	
	$seln=executework("select count(*) as cnt from lms_instructor_courses where id<>''".$qr." order by id"); 
	$rown=@mysqli_fetch_array($seln);
	if(!empty($rown['cnt']))
	$n=$rown['cnt'];
	$sel=executework("select * from lms_instructor_courses where id<>''".$qr." order by id".$lmt);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	if($cnt>0)
	{
		while($row=@mysqli_fetch_array($sel))
		{
			$row['cnt']=$n;
			$arr[]=$row;
		}
	}
	return $arr;
}

function get_same_courses($id,$course,$to_date,$status,$lmt)
{
	$qr='';$limit='';
	if($id!='')
	$qr.=" and id!='".$id."'";
	
	if($course!='')
	$qr.=" and course_id='".$course."'";

	if($status!='')
	$qr.=" and status='".$status."'";
	
	if($lmt!='')
	$limit.=" limit ".$lmt;
	$sel=executework("select * from lms_instructor_courses where id<>''".$qr."order by id desc".$limit);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
} 

function get_curriculum($id,$course,$course_title,$status,$lmt)
{
	$qr='';$limit='';
	if($id!='')
	$qr.=" and id!='".$id."'"; 
	
	if($course_title!='')
	$qr.=" and course_title='".$course_title."'";
	
	if($course!='')
	$qr.=" and course_id='".$course."'";

	if($status!='')
	$qr.=" and status='".$status."'";
	
	if($lmt!='')
	$limit.=" limit ".$lmt;

	$sel=executework("select * from lms_instructor_modules where id<>''".$qr."order by id desc".$limit);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
} 

//------------------ Main Funtion  (Cart)
function add_to_cart($sid,$child_id,$action)
{
	$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Student' and order_status NOT IN('completed','cancelled')");
	$cnt=@mysqli_num_rows($sel);
	
	if($cnt==0)
		{
			$_SESSION['cart_id']=time();
			$int=executeworkins("insert into lms_cart_course (cart_id,user_id,user_type,created_by,created_on) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','Student','".$_SESSION['login_id']."','".date('Y-m-d')."')");
		}
		else
		{
			$row=@mysqli_fetch_array($sel);
			$_SESSION['cart_id']=$row['cart_id'];
		}
	
	$selcart=executework("select * from lms_cart_list where cart_id='".$_SESSION['cart_id']."' and child_id='".$child_id."' and purchage_course_id='".$sid."'");
	$cntcart=@mysqli_num_rows($selcart);
	$rowcart=@mysqli_fetch_array($selcart);
	
	if($cntcart==0 && $action=='add')
	{
			$cart_courses=executework("select * from lms_instructor_courses where id='".$sid."'");
			$cntcart_courses=@mysqli_num_rows($cart_courses);
			$rowcart_courses=@mysqli_fetch_array($cart_courses);
	
			$cart=executework("insert into lms_cart_list (cart_id,parent_id,child_id,purchage_course_id,amount,p_date,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','".$child_id."','".$sid."','".$rowcart_courses['course_price']."','".date('Y-m-d')."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
		
	}
	
	if($cntcart>0 && $action=='remove')
	{
		$sel=executework("DELETE FROM lms_cart_list WHERE id='".$rowcart['id']."'");
	}
	
		$cart_items=executework("select COUNT(*) AS cnt,SUM(amount) AS total_amount from lms_cart_list where cart_id='".$_SESSION['cart_id']."'");
		$cntcart_items=@mysqli_num_rows($cart_items);
		$rowcart_items=@mysqli_fetch_array($cart_items);
		
		$update=executework("update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ");
} 

//----------- Checkout 
function checkout_status($sid)
{
	$update=executework("update lms_cart_course set payment_status='success',order_status='completed' where id='".$sid."'");
	$_SESSION['cart_id']='';
	redirect("cart.php?dsucc=1");
}

//---------------- View Courses
function get_view_courses($child_id)
{
	$sel=executework("SELECT * FROM lms_cart_list cl JOIN lms_cart_course cc ON cc.cart_id = cl.cart_id JOIN lms_instructor_courses ic ON ic.id = cl.purchage_course_id
WHERE cl.child_id = '".$child_id."' AND cl.parent_id = '".$_SESSION['login_id']."' AND cc.order_status = 'completed'");
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}

//---------------- View Courses
function get_modules($main_id)
{
	$sel=executework("SELECT * FROM lms_instructor_modules where course_id='".$main_id."' and status=1");
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}

//------------ Lessons

function get_lessons($course_id,$module_id)
{
	$sel=executework("SELECT * FROM lms_instructor_lessons where course_id='".$course_id."' and module_id='".$module_id."' and status=1");
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}

//-------------------- Course Details
function get_course_master($lmt)
{
	$limit='';
	
	if($lmt!='')
	$limit.=" limit ".$lmt;

	$sel=executework("SELECT * FROM lms_course_master where status=1".$limit);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}

//--------------------- Home Page Display Courses Display
function get_courses_display($id,$course_id,$to_date,$status,$lmt)
{
	$qr='';$limit='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($course_id!='')
	$qr.=" and course_id='".$course_id."'";

	if($status!='')
	$qr.=" and status='".$status."'";
	if($lmt!='')
	$limit.=" limit ".$lmt;
	$sel=executework("select * from lms_instructor_courses where id<>''".$qr.$limit);
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		if($row['status']==1)
		{
			$row['st']='Active';
			$row['st1']='Deactivate';
		}
		else
		{
			$row['st']='Disabled';
			$row['st1']='Activate';
		}
		$arr[]=$row;
	}
	return $arr;
}  

?>