<?php
include ('../../config.php');
require_once($CFG->libdir . '/completionlib.php');
require_once($CFG->libdir . '/modinfolib.php');
require_once('locallib.php');
global $DB;
$lessonid = $_POST['lessonid'];
$userid = $_POST['userid'];
$pauseResume = $_POST['pauseResume'];
//echo $pauseResume;print_r($_POST);
   
if($pauseResume=='resume'){
	$timenow = time();
	$timer = new stdClass;
	$timer->lessonid = $lessonid;
	$timer->userid = $userid;
	$timer->starttime = $timenow;
	$timer->lessontime = $timenow;
	$DB->insert_record('lesson_timer', $timer);
}else if($pauseResume=='pause'){
	$params = array('lessonid' => $lessonid, 'userid' => $userid);
	$timer= $DB->get_records('lesson_timer', $params, 'starttime DESC', '*', 0,1);
	//print_r($timer);
	$timer = current($timer); // This will get the latest start time record.
	$timenow = time();
	$timer->lessontime = $timenow;
	//$timer->completed = $endreached;
	$DB->update_record('lesson_timer', $timer); 
}else if($pauseResume=='stop'){
    
	$params = array('lessonid' => $lessonid, 'userid' => $userid);
	$timer= $DB->get_records('lesson_timer', $params, 'starttime DESC', '*', 0,1);
	//print_r($timer);
	$timer = current($timer); // This will get the latest start time record.
	$timenow = time();
	$timer->lessontime = $timenow;
	$timer->completed = true;
	$DB->update_record('lesson_timer', $timer); 
	
	$cm = get_coursemodule_from_id('lesson', $lessonid, 0, false, MUST_EXIST);
    $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
    $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST), $cm, $course);
    $data = lesson_grade($lesson,0,$userid);
    //echo "grade";
    //print_r($data->grade);
    
    if($data->grade == 100) {
	$courseid = $_POST['courseid'];
	// Update completion state.
	/*$cm = get_coursemodule_from_instance('lesson', $lessonid, $courseid,
		false, MUST_EXIST);
	$course = get_course($cm->course);*/
	//print_r($cm);//die();
	$completion = new completion_info($course);
	
	//if ($completion->is_enabled($cm)) {
	$completion->update_state($cm, COMPLETION_COMPLETE,$userid);
	//}
	//Just check if completion is updated otherwise forcefully insert the record
	$cmRec= $DB->get_record('course_modules_completion', array("coursemoduleid"=>$cm->id,"userid"=>$userid));
	if(!$cmRec){
	$timenow = time();
	$cc = new stdClass;
	$cc->coursemoduleid = $cm->id;
	$cc->userid = $userid;
	$cc->completionstate = 1;
	
	$cc->timemodified = $timenow;
	$ins = $DB->insert_record('course_modules_completion', $cc);
	//echo $ins;
	}
	//End
	//Have to clear completion cache
	 $completioncache = cache::make('core', 'completion');
	 //$completioncache->delete($userid . '_' . $cm->course);
	 //get_fast_modinfo($cm->course, $userid, true);
	 //print_r($completioncache);
        if ($userid == $USER->id) {
            // Update module completion in user's cache.
            if (!($cachedata = $completioncache->get($userid . '_' . $cm->course))
                    || $cachedata['cacherev'] != $course->cacherev) {
                $cachedata = array('cacherev' => $course->cacherev);
            }
            
			$cachedata[$cm->id]['completionstate'] = 1;
			$cachedata[$cm->id]['timemodified'] = time();
            $completioncache->set($userid . '_' . $cm->course, $cachedata);
			
            // reset modinfo for user (no need to call rebuild_course_cache())
            get_fast_modinfo($cm->course, $userid, true);
			// print_r($completioncache);
        } else {
            // Remove another user's completion cache for this course.
            $completioncache->delete($userid . '_' . $cm->course);
        }
	}
	//print_r($timer);
	//print_r($completion);
	die();
}

//print_r($timer);
die();
?>