Skip to content

Commit

Permalink
MDL-57688 mod_lesson: Move duplicated code to methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jleyva committed Mar 21, 2017
1 parent f4d0909 commit dbba944
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 58 deletions.
13 changes: 3 additions & 10 deletions mod/lesson/continue.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,9 @@
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;
Expand Down
71 changes: 71 additions & 0 deletions mod/lesson/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,77 @@ public function get_user_timers($userid = null, $sort = '', $fields = '*', $limi
$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));
}
}
}
}


Expand Down
57 changes: 9 additions & 48 deletions mod/lesson/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@

$lesson->set_module_viewed();

$timer = null;

// This is where several messages (usually warnings) are displayed
// all of this is displayed above the actual page

Expand All @@ -204,57 +206,16 @@
$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;
Expand Down

0 comments on commit dbba944

Please sign in to comment.