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

function kid_login()
{
	$sel=executework("select * from lms_kid_register where parent_email='".$_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['parent_email'];
		$_SESSION['login_type']="Kid";
		$_SESSION['login_id']=$row['id'];
		redirect("dashboard.php");
	}
	else
	{
		redirect("index.php?fail=1");
	}	
}

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

function kids_signup()
{	
	$sel=executework("select * from lms_kid_register where parent_email='".$_POST['parent_email']."'");
	$cnt=@mysqli_num_rows($sel);
	
	if(!empty($_POST['mid']))
	{
		
		$upd=executework("update lms_kid_register set parent_name='".$_POST['parent_name']."',parent_email='".$_POST['parent_email']."', parent_mobile='".$_POST['parent_mobile']."' where id='".$_POST['mid']."'");
		
		redirect("dashboard.php");
	}
	if($cnt>0)
	{
			redirect("register.php?exist=1");
	}
	else
	{
		$int=executeworkins("insert into lms_kid_register (parent_name,parent_email,password,parent_mobile,created_on,status) values('".$_POST['parent_name']."','".$_POST['parent_email']."','123','".$_POST['parent_mobile']."','".date('Y-m-d h:i:s')."','1')");
		
		redirect("index.php?succ=1");
	}
}

//---------- Adding Child
function add_child()
{	
	
	if(!empty($_POST['mid']))
	{
		
		$upd=executework("update lms_child_details set child_dob='".$_POST['child_dob']."',child_name='".$_POST['child_name']."',child_age='".$_POST['child_age']."',grade='".$_POST['grade']."', modified_by='".$_SESSION['login_id']."',modified_on='".date('Y-m-d h:i:s')."' where id='".$_POST['mid']."'");
		
		redirect("childs_details.php?succ=1");
	}
	
	else
	{
		$int=executeworkins("insert into lms_child_details (parent_id,child_name,child_dob,child_age,grade,school,created_by,created_on,status) values('".$_SESSION['login_id']."','".$_POST['child_name']."','".$_POST['child_dob']."','".$_POST['child_age']."','".$_POST['grade']."','".$_POST['school']."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
		
		redirect("childs_details.php?succ=1");
	}
}

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



function purchage_product_bk($sid,$child_id)
{
	if(empty($_SESSION['cart_id']))
	{	
		$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Parent'");
		$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']."','Parent','".$_SESSION['login_id']."','".date('Y-m-d')."')");
			$cart_item=1;
			add_to_card($sid,$child_id,$cart_item);
		}
		else
		{
			$row=@mysqli_fetch_array($sel);
			$_SESSION['cart_id']=$row['cart_id'];
			$cart_item=$row['cart_item']+1;
			add_to_card($sid,$child_id,$cart_item);
		}
	}
	else
	{	
		$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Parent'");
		$cnt=@mysqli_num_rows($sel);
		$row=@mysqli_fetch_array($sel);
		$cart_item=$row['cart_item']+1;
		add_to_card($sid,$child_id,$cart_item);
	}
} 

function add_to_card($sid,$child_id,$cart_item)
{
	$update=executework("update lms_cart_course set cart_item='".$cart_item."' where cart_id='".$_SESSION['cart_id']."' ");
	
	$cart=executework("insert into lms_cart_list (cart_id,parent_id,child_id,purchage_course_id,p_date,created_by,created_on,status) values('".$_SESSION['cart_id']."','".$_SESSION['login_id']."','".$child_id."','".$sid."','".date('Y-m-d')."','".$_SESSION['login_id']."','".date('Y-m-d h:i:s')."','1')");
	
}

function purchage_product_bk_bk($sid,$child_id)
{
	$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Parent' and order_status NOT IN('completed','cancelled')");
	$cnt=@mysqli_num_rows($sel);
	$row=@mysqli_fetch_array($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']."','Parent','".$_SESSION['login_id']."','".date('Y-m-d')."')");
	}
	else
	{
			
			$_SESSION['cart_id']=$row['cart_id'];
	}
	
	//$update=executework("update lms_cart_course set cart_item='".$cart_item."' where cart_id='".$_SESSION['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);
	if($cntcart==0)
	{
			$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')");
	
		$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);
		//echo $cntcart_items['cnt'];
		
		$update=executework("update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ");
		
	}
}

function add_to_cart($sid,$child_id,$action)
{
	$sel=executework("select * from lms_cart_course where user_id='".$_SESSION['login_id']."' and user_type='Parent' 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']."','Parent','".$_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']."' ");
} 






function delete_child($sid)
{
	$news=get_child($sid,'','');
	if(!empty($news[0]['image']) && file_exists("activitys/".$news[0]['image']))
	unlink("activitys/".$news[0]['image']);
	$del=delete_record("lms_child_details","id",$sid,'');
	if($del=='deleted')
	redirect("category_master.php?dsucc=1");
	else
	redirect("category_master.php?invalid=1");
}

function get_child($id,$status,$limit)
{
	$qr='';$lmt='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($status!='')
	$qr.=" and status='".$status."'";
	if($limit!='')
	$lmt.=" limit ".$limit;
	
	$sel=executework("select * from lms_child_details where parent_id='".$_SESSION['login_id']."'".$qr."order by id desc".$lmt);
	$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_child_count($id,$limit)
{
	$qr='';$lmt=''; $n=0;
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($limit!='')
	$lmt.=" limit ".$limit;
	
	$seln=executework("select count(*) as cnt from lms_child_details where parent_id='".$_SESSION['login_id']."'".$qr." order by id"); 
	$rown=@mysqli_fetch_array($seln);
	if(!empty($rown['cnt']))
	$n=$rown['cnt'];
	$sel=executework("select * from lms_child_details where parent_id='".$_SESSION['login_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_course_details($id,$course_id,$status,$limit)
{
	$qr='';$lmt='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($course_id!='')
	$qr.=" and grade='".$course_id."'";
	
	if($status!='')
	$qr.=" and status='".$status."'";
	if($limit!='')
	$lmt.=" limit ".$limit;
	$sel=executework("select * from lms_instructor_courses where status=1 ".$qr."order by id desc".$lmt);
	$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_course_details_count($id,$course_id,$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 status=1 ".$qr." order by id"); 
	$rown=@mysqli_fetch_array($seln);
	if(!empty($rown['cnt']))
	$n=$rown['cnt'];
	$sel=executework("select * from lms_instructor_courses where status=1 ".$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 remove_item($sid)
{
	$sel=executework("DELETE FROM lms_cart_list WHERE id='".$sid."'");
	
	
	
	$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);
	
	echo "update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ";
		
		$update=executework("update lms_cart_course set cart_item='".$rowcart_items['cnt']."',amount='".$rowcart_items['total_amount']."' where cart_id='".$_SESSION['cart_id']."' ");
	
	//redirect("cart.php?dsucc=1");
}

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");
}


function get_child_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;
}
 
//----- Course Topics Details

function get_titles($course_id,$module_id)
{
	$sel=executework("SELECT * FROM lms_instructor_lessons where course_id='".$course_id."' and status=1 and module_id='".$module_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;
} 

function get_videos_count($course_id,$lession)
{
	$sel=executework("SELECT * FROM lms_instructor_lessons where course_id='".$course_id."' and status=1 and lesson_type ='Video' and lesson_title='".$lession."'");
	$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_course_name($id)
{
	$sel=executework("select * from lms_course_master where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['title']))
	$val=$row['title'];
	return $val;
}

function get_course_type($id)
{
	$sel=executework("select * from lms_course_master where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['course_type']))
	$val=$row['course_type'];
	return $val;
}

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

function get_inst_course($id,$status,$ordby,$lmt)//4
{
	$qr='';$limit='';$ord ='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	
	if($status!='')
	$qr.=" and status='".$status."'";
	if($ordby !='')
	$ord = " and order by '".$ordby."'";
	
	
	
	if($lmt!='')
	$limit.=" limit ".$lmt;
	
	$sel=executework("select * from lms_instructor_courses where id<>''".$qr.$ord.$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_module($id,$course_id,$status,$ordby,$lmt)//4
{
	$qr='';$limit='';$ord ='';
	if($id!='')
	$qr.=" and id='".$id."'";
		if($course_id!='')
	$qr.=" and course_id='".$course_id."'";
	
	
	if($status!='')
	$qr.=" and status='".$status."'";
	if($ordby !='')
	$ord = " and order by '".$ordby."'";
	
	
	
	if($lmt!='')
	$limit.=" limit ".$lmt;
	
	$sel=executework("select * from  lms_instructor_modules where id<>''".$qr.$ord.$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_lesson($id,$course_id,$module_id,$status,$ordby,$lmt)//4
{
	$qr='';$limit='';$ord ='';
	if($id!='')
	$qr.=" and id='".$id."'";
	
	if($course_id!='')
	$qr.=" and course_id='".$course_id."'";
	
	if($module_id!='')
	$qr.=" and module_id='".$module_id."'";
	
	
	if($status!='')
	$qr.=" and status='".$status."'";
	if($ordby !='')
	$ord = " and order by '".$ordby."'";
	
	
	
	if($lmt!='')
	$limit.=" limit ".$lmt;
	
	$sel=executework("select * from  lms_instructor_lessons where id<>''".$qr.$ord.$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;
}

?>