diff --git a/mod/lesson/continue.php b/mod/lesson/continue.php index 7db68d95c2d..06396041eab 100644 --- a/mod/lesson/continue.php +++ b/mod/lesson/continue.php @@ -53,16 +53,9 @@ $PAGE->navbar->add(get_string('continue', 'lesson')); if (!$canmanage) { $lesson->displayleft = lesson_displayleftif($lesson); $timer = $lesson->update_timer(); - if ($lesson->timelimit) { - $timeleft = ($timer->starttime + $lesson->timelimit) - time(); - if ($timeleft <= 0) { - // Out of time - $lesson->add_message(get_string('eolstudentoutoftime', 'lesson')); - redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal'))); - } else if ($timeleft < 60) { - // One minute warning - $lesson->add_message(get_string("studentoneminwarning", "lesson")); - } + if (!$lesson->check_time($timer)) { + redirect(new moodle_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => LESSON_EOL, 'outoftime' => 'normal'))); + die; // Shouldn't be reached, but make sure. } } else { $timer = new stdClass; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 4cf21f63415..8a77bcc6415 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -2454,6 +2454,77 @@ class lesson extends lesson_base { $params = array('lessonid' => $this->properties->id, 'userid' => $userid); return $DB->get_records('lesson_timer', $params, $sort, $fields, $limitfrom, $limitnum); } + + /** + * Check if the user is out of time in a timed lesson. + * + * @param stdClass $timer timer object + * @return bool True if the user is on time, false is the user ran out of time + * @since Moodle 3.3 + */ + public function check_time($timer) { + if ($this->properties->timelimit) { + $timeleft = $timer->starttime + $this->properties->timelimit - time(); + if ($timeleft <= 0) { + // Out of time. + $this->add_message(get_string('eolstudentoutoftime', 'lesson')); + return false; + } else if ($timeleft < 60) { + // One minute warning. + $this->add_message(get_string('studentoneminwarning', 'lesson')); + } + } + return true; + } + + /** + * Add different informative messages to the given page. + * + * @param lesson_page $page page object + * @param reviewmode $bool whether we are in review mode or not + * @since Moodle 3.3 + */ + public function add_messages_on_page_view(lesson_page $page, $reviewmode) { + global $DB, $USER; + + if (!$this->can_manage()) { + if ($page->qtype == LESSON_PAGE_BRANCHTABLE && $this->properties->minquestions) { + // Tell student how many questions they have seen, how many are required and their grade. + $ntries = $DB->count_records("lesson_grades", array("lessonid" => $this->properties->id, "userid" => $USER->id)); + $gradeinfo = lesson_grade($this, $ntries); + if ($gradeinfo->attempts) { + if ($gradeinfo->nquestions < $this->properties->minquestions) { + $a = new stdClass; + $a->nquestions = $gradeinfo->nquestions; + $a->minquestions = $this->properties->minquestions; + $this->add_message(get_string('numberofpagesviewednotice', 'lesson', $a)); + } + + if (!$reviewmode && !$this->properties->retake) { + $this->add_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify'); + if ($this->properties->grade != GRADE_TYPE_NONE) { + $a = new stdClass; + $a->grade = number_format($gradeinfo->grade * $this->properties->grade / 100, 1); + $a->total = $this->properties->grade; + $this->add_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify'); + } + } + } + } + } else { + if ($this->properties->timelimit) { + $this->add_message(get_string('teachertimerwarning', 'lesson')); + } + if (lesson_display_teacher_warning($this)) { + // This is the warning msg for teachers to inform them that cluster + // and unseen does not work while logged in as a teacher. + $warningvars = new stdClass(); + $warningvars->cluster = get_string('clusterjump', 'lesson'); + $warningvars->unseen = get_string('unseenpageinbranch', 'lesson'); + $this->add_message(get_string('teacherjumpwarning', 'lesson', $warningvars)); + } + } + } } diff --git a/mod/lesson/view.php b/mod/lesson/view.php index 74a2ae45595..af41050c958 100644 --- a/mod/lesson/view.php +++ b/mod/lesson/view.php @@ -193,6 +193,8 @@ if ($pageid != LESSON_EOL) { $lesson->set_module_viewed(); + $timer = null; + // This is where several messages (usually warnings) are displayed // all of this is displayed above the actual page @@ -204,57 +206,16 @@ if ($pageid != LESSON_EOL) { $restart = ($continue && $startlastseen == 'yes'); $timer = $lesson->update_timer($continue, $restart); - if ($lesson->timelimit) { - $timeleft = $timer->starttime + $lesson->timelimit - time(); - if ($timeleft <= 0) { - // Out of time - $lesson->add_message(get_string('eolstudentoutoftime', 'lesson')); - redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal'))); - die; // Shouldn't be reached, but make sure - } else if ($timeleft < 60) { - // One minute warning - $lesson->add_message(get_string('studentoneminwarning', 'lesson')); - } - } - - if ($page->qtype == LESSON_PAGE_BRANCHTABLE && $lesson->minquestions) { - // tell student how many questions they have seen, how many are required and their grade - $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id)); - $gradeinfo = lesson_grade($lesson, $ntries); - if ($gradeinfo->attempts) { - if ($gradeinfo->nquestions < $lesson->minquestions) { - $a = new stdClass; - $a->nquestions = $gradeinfo->nquestions; - $a->minquestions = $lesson->minquestions; - $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a)); - } - - if (!$reviewmode && !$lesson->retake){ - $lesson->add_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify'); - if ($lesson->grade != GRADE_TYPE_NONE) { - $a = new stdClass; - $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1); - $a->total = $lesson->grade; - $lesson->add_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify'); - } - } - } - } - } else { - $timer = null; - if ($lesson->timelimit) { - $lesson->add_message(get_string('teachertimerwarning', 'lesson')); - } - if (lesson_display_teacher_warning($lesson)) { - // This is the warning msg for teachers to inform them that cluster - // and unseen does not work while logged in as a teacher - $warningvars = new stdClass(); - $warningvars->cluster = get_string('clusterjump', 'lesson'); - $warningvars->unseen = get_string('unseenpageinbranch', 'lesson'); - $lesson->add_message(get_string('teacherjumpwarning', 'lesson', $warningvars)); + // Check time limit. + if (!$lesson->check_time($timer)) { + redirect(new moodle_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => LESSON_EOL, 'outoftime' => 'normal'))); + die; // Shouldn't be reached, but make sure. } } + // Add different informative messages to the given page. + $lesson->add_messages_on_page_view($page, $reviewmode); + $PAGE->set_subpage($page->id); $currenttab = 'view'; $extraeditbuttons = true;