<?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 status=0");
	$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,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','Student','".$_SESSION['login_id']."','".date('Y-m-d')."',0)");
		}
		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=='remove')
	{
		$sel=executeworkins("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);
		if(empty($rowcart_items['cnt']))
		{
		    $cnt=0;
		    $amt=0;
		}
		else
		{
		    $cnt=$rowcart_items['cnt'];
		    $amt=$rowcart_items['total_amount'];
		}
		
		$update=executework("update lms_cart_course set cart_item='".$cnt."',amount='".$amt."' where cart_id='".$_SESSION['cart_id']."' ");
		redirect("cart.php?suss=1");
} 

//----------- Checkout 
function checkout_status($sid)
{
	$update=executework("update lms_cart_course set order_status='check_out' where id='".$sid."'");
	redirect("check_out.php");
}

//----------- Payment  
function payment_status($sid)
{
	$update=executework("update lms_cart_course set order_status='completed',payment_status='success', status=1 where id='".$sid."'");
	$_SESSION['cart_id']='';
	redirect("check_out.php?succ=1");
}
//-------------- Back To Cart

function back_cart($sid)
{
	$update=executework("update lms_cart_course set order_status='' where id='".$sid."'");
	redirect("cart.php?succ=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)
{
	$qr='';
	if(!empty($course_id))
		$qr.=(" and course_id='".$course_id."'");
	if(!empty($module_id))
		$qr.=(" and module_id='".$module_id."'");
		
	$sel=executework("SELECT * FROM lms_instructor_lessons where status=1".$qr);
	$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;
}  
//------------
function get_cart_details()
{	
	
	$sel=executework("SELECT group_concat(lms_cart_list.purchage_course_id) as crs FROM lms_cart_course JOIN lms_cart_list ON lms_cart_course.cart_id = lms_cart_list.cart_id WHERE lms_cart_course.user_id = '".$_SESSION['login_id']."' AND lms_cart_course.user_type = 'Student' AND lms_cart_course.status = 0");
	$cnt=@mysqli_num_rows($sel);
	$arr=array();
	while($row=@mysqli_fetch_array($sel))
	{
		$arr=explode(',',$row['crs']);
	}
	return $arr;
}

//---------- course filter 
function get_courses_filter($id,$course_id,$to_date,$status,$lmt)
{
    $qr=''; 
    $limit='';

    if($id!='')
        $qr.=" and id='".$id."'";

    if($course_id!='')
        $qr.=" and (course_id='".$course_id."' || course_level='".$course_id."' || category_title like '%".$course_id."%' || course_title like '%".$course_id."%' || course_type like '%".$course_id."%' || course_price like '%".$course_id."%' || course_discount like '%".$course_id."%' || course_level like '%".$course_id."%' || language like '%".$course_id."%' || no_of_class like '%".$course_id."%' || duration like '%".$course_id."%' || course_overview like '%".$course_id."%' || details like '%".$course_id."%' || additional_info like '%".$course_id."%')";

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

    if($lmt!='')
        $limit.=" limit ".$lmt;
	echo "select * from lms_instructor_courses where 1 ".$qr.$limit;
	
    $sel=executework("select * from lms_instructor_courses where 1 ".$qr.$limit);

    $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 Status
function get_cours_status($child_id,$course_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' and cl.purchage_course_id='".$course_id."'");
	$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;
}

?>