MDL-57688 mod_lesson: Move duplicated code to methods

Some code that will be used by Web Services was moved, we created new
methods to avoid code duplication:
- To generate the lesson messages on page view
- To check the time restrictions in a current attempt
This commit is contained in:
Juan Leyva 2017-01-18 13:16:23 +01:00
parent f4d0909fc0
commit dbba944e32
3 changed files with 83 additions and 58 deletions

View File

@ -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;

View File

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

View File

@ -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;