<?php 
include("parent/callMe.php");
include('Sessioncheck.php');

if(($_REQUEST['action']=='EditSubject'))
{

	
		// echo "<pre>"; print_r($_REQUEST); echo "</pre>";


		    $m->where ("term_id", $_REQUEST['termid']);
			$m->where ("class_id", $_REQUEST['classid']);
			$m->where ("section_id", $_REQUEST['sectionid']);
			$m->where ("session_id", $_REQUEST['sessionid']);
			$m->where ("student_id", $_REQUEST['studentid']);
			$m->where ("subject_id", $_REQUEST['subjectid']);
			$m->where("branch_id", $_SESSION['branch']);
			$old_Marks =  $m->get ('eduportal_exam_marksheet');
	
if(count($old_Marks)>0){
  
		$data = array(
		'obtained_marks' => $_REQUEST['sbmrks']
		);

		$m->where ("term_id", $_REQUEST['termid']);
		$m->where ("class_id", $_REQUEST['classid']);
		$m->where ("section_id", $_REQUEST['sectionid']);
		$m->where ("session_id", $_REQUEST['sessionid']);
		$m->where ("student_id", $_REQUEST['studentid']);
		$m->where ("subject_id", $_REQUEST['subjectid']);
		$m->where("branch_id", $_SESSION['branch']);
		$result = $m->update('eduportal_exam_marksheet',$data);
		//echo "umar";


		// now adding user audit detail



	  				$m->where("subject_id", $_REQUEST['subjectid']);

					$sec = $m->get("eduportal_subject_config");


					$m->where("section_id", $_REQUEST['sectionid']);

					$sectionName = $m->get("edu_adm_section_detail");


					$m->where("term_id", $_REQUEST['termid']);

					$termName = $m->get("eduportal_exam_terms");


					$m->where("student_id", $_REQUEST['studentid']);

					$Stdname = $m->get("edu_student_details");



					$m->where("class_id", $_REQUEST['classid']);

					$rec = $m->get("edu_class_config");



					$Class_Name=$rec[0]['Class_Name'];

					$Section_Name=$sectionName[0]['section_name'];

					$Term_Name=$termName[0]['term_title'];

					$Student_Name=$Stdname[0]['first_name'];

					$subject_name=$sec[0]['subject'];

					$username=$_SESSION['username'];

					$old_Sub_Marks=$old_Marks[0]['obtained_marks'];
					$updated_Marks=$_REQUEST['sbmrks'];


					$action="Update Single Student Marks";
	  				$details="User: ".$username." , Sutdent: ".$Student_Name." , Class=".$Class_Name.", Section=".$Section_Name.", Subject=".$subject_name.", Term=".$Term_Name.", Old Marks=".$old_Sub_Marks.", Updated Marks=".$updated_Marks ;

	  				

	  				date_default_timezone_set("Asia/Karachi");



					$data = array(

					'user_id' => $_SESSION['user_id'],

					'class_id' => $_REQUEST['classid'],

					'section_id' => $_REQUEST['sectionid'],

					'session_id' => $_REQUEST['sessionid'],

					'term_id' => $_REQUEST['termid'],

					'module_id' => 15,

					'action' => $action,

					'details' => $details,

					'date' => date("Y-m-d"),

					'month_id' => date('m'),

					'time' => date("h:i:sa"),

					'branch_id' => $_SESSION['branch'],

					);



					//print_r($data);



					$result = $m->insert('eduportal_users_exam_auditlog_detail',$data);

	} else {

//echo "ali";
		$m->where("term_id", $_REQUEST['termid']);
	    $m->where("session_id", $_REQUEST['sessionid']);
	    $m->where("subject_id", $_REQUEST['subjectid']);
	    $m->where("branch_id", $_SESSION['branch']);	  
	    $m->where("class_id", $_REQUEST['classid']);
	    $m->where("section_id", $_REQUEST['sectionid']);
	    $Tmarks = $m->get("eduportal_exam_marksheet");
	    $avg=($_REQUEST['sbmrks']/$Tmarks[0]['total_marks'])*100;




    $query="SELECT grade_id,grade_title FROM eduportal_exam_school_grades WHERE percantage_start_range <=".round($avg)." and percantage_end_range >=".round($avg)." and branch_id=".$_SESSION['branch'];
     


		 $average =  $m->query($query);


		$data1 = array(

		'branch_id' => $_SESSION['branch'],

		'subject_id' => $_REQUEST['subjectid'],

		'student_id' => $_REQUEST['studentid'],

		'session_id' => $_REQUEST['sessionid'],

		'section_id' => $_REQUEST['sectionid'],

		'class_id' => $_REQUEST['classid'],

		'total_marks' => $Tmarks[0]['total_marks'],

		'obtained_marks' => $_REQUEST['sbmrks'],

		'grade' => $average[0]['grade_title'],

		'term_id' => $_REQUEST['termid']

		);

		 //echo "<pre>"; print_r($data1); echo "</pre>";

	$result =  $m->insert ('eduportal_exam_marksheet', $data1);	




	// now adding user audit detail



	  				$m->where("subject_id", $_REQUEST['subjectid']);

					$sec = $m->get("eduportal_subject_config");


					$m->where("section_id", $_REQUEST['sectionid']);

					$sectionName = $m->get("edu_adm_section_detail");


					$m->where("term_id", $_REQUEST['termid']);

					$termName = $m->get("eduportal_exam_terms");


					$m->where("student_id", $_REQUEST['studentid']);

					$Stdname = $m->get("edu_student_details");



					$m->where("class_id", $_REQUEST['classid']);

					$rec = $m->get("edu_class_config");



					$Class_Name=$rec[0]['Class_Name'];

					$Section_Name=$sectionName[0]['section_name'];

					$Term_Name=$termName[0]['term_title'];

					$Student_Name=$Stdname[0]['first_name'];

					$subject_name=$sec[0]['subject'];

					$username=$_SESSION['username'];

				
					$Add_Marks=$_REQUEST['sbmrks'];


					$action="Add Single Student Marks";
	  				$details="User: ".$username." , Sutdent: ".$Student_Name." , Class=".$Class_Name.", Section=".$Section_Name.", Subject=".$subject_name.", Term=".$Term_Name.", Add Marks=".$Add_Marks ;

	  				

	  				date_default_timezone_set("Asia/Karachi");



					$data = array(

					'user_id' => $_SESSION['user_id'],

					'class_id' => $_REQUEST['classid'],

					'section_id' => $_REQUEST['sectionid'],

					'session_id' => $_REQUEST['sessionid'],

					'term_id' => $_REQUEST['termid'],

					'module_id' => 15,

					'action' => $action,

					'details' => $details,

					'date' => date("Y-m-d"),

					'month_id' => date('m'),

					'time' => date("h:i:sa"),

					'branch_id' => $_SESSION['branch'],

					);



					//print_r($data);



					$result = $m->insert('eduportal_users_exam_auditlog_detail',$data);


	}


} // ending main if



?>