Skip to content

Commit

Permalink
MDL-50516 mod_lesson: prevented user from repeating question
Browse files Browse the repository at this point in the history
It was possible in the lesson module for a user to
answer a question, get the result, then click back
in the browser and re-answer it, even if they exceeded
the number of attempts allowed per question.
  • Loading branch information
mdjnelson authored and andrewnicols committed Sep 9, 2015
1 parent b8b323d commit 0dea8ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 1 addition & 5 deletions mod/lesson/continue.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@
if ($result->attemptsremaining != 0 && $lesson->review && !$reviewmode) {
$lesson->add_message(get_string('attemptsremaining', 'lesson', $result->attemptsremaining));
}
// Report if max attempts reached
if ($result->maxattemptsreached != 0 && $lesson->review && !$reviewmode) {
$lesson->add_message('('.get_string("maximumnumberofattemptsreached", "lesson").')');
}

$PAGE->set_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $page->id));
$PAGE->set_subpage($page->id);
Expand All @@ -187,7 +183,7 @@
if ($lesson->ongoing && !$reviewmode) {
echo $lessonoutput->ongoing_score($lesson);
}
if (!$result->maxattemptsreached && !$reviewmode) {
if (!$reviewmode) {
echo $result->feedback;
}

Expand Down
18 changes: 15 additions & 3 deletions mod/lesson/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,19 @@ final public function record_attempt($context) {
} else {
if (!has_capability('mod/lesson:manage', $context)) {
$nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$this->lesson->id, "userid"=>$USER->id));

// Get the number of attempts that have been made on this question for this student and retake,
$nattempts = $DB->count_records('lesson_attempts', array('lessonid' => $this->lesson->id,
'userid' => $USER->id, 'pageid' => $this->properties->id, 'retry' => $nretakes));

// Check if they have reached (or exceeded) the maximum number of attempts allowed.
if ($nattempts >= $this->lesson->maxattempts) {
$result->maxattemptsreached = true;
$result->feedback = get_string('maximumnumberofattemptsreached', 'lesson');
$result->newpageid = $this->lesson->get_next_page($this->properties->nextpageid);
return $result;
}

// record student's attempt
$attempt = new stdClass;
$attempt->lessonid = $this->lesson->id;
Expand Down Expand Up @@ -2462,14 +2475,13 @@ final public function record_attempt($context) {
$event->add_record_snapshot('lesson_attempts', $attempt);
$event->trigger();

// Increase the number of attempts made.
$nattempts++;
}
}
// "number of attempts remaining" message if $this->lesson->maxattempts > 1
// displaying of message(s) is at the end of page for more ergonomic display
if (!$result->correctanswer && ($result->newpageid == 0)) {
// wrong answer and student is stuck on this page - check how many attempts
// the student has had at this page/question
$nattempts = $DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry" => $attempt->retry));
// retreive the number of attempts left counter for displaying at bottom of feedback page
if ($nattempts >= $this->lesson->maxattempts) {
if ($this->lesson->maxattempts > 1) { // don't bother with message if only one attempt
Expand Down

0 comments on commit 0dea8ed

Please sign in to comment.