MDL-50516 mod_lesson: prevented user from repeating question

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.
This commit is contained in:
Mark Nelson 2015-07-29 23:37:01 -07:00 committed by Andrew Nicols
parent b8b323d6a9
commit 0dea8ed76a
2 changed files with 16 additions and 8 deletions

View File

@ -168,10 +168,6 @@ if ($canmanage) {
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);
@ -187,7 +183,7 @@ if ($lesson->displayleft) {
if ($lesson->ongoing && !$reviewmode) {
echo $lessonoutput->ongoing_score($lesson);
}
if (!$result->maxattemptsreached && !$reviewmode) {
if (!$reviewmode) {
echo $result->feedback;
}

View File

@ -2426,6 +2426,19 @@ abstract class lesson_page extends lesson_base {
} 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;
@ -2462,14 +2475,13 @@ abstract class lesson_page extends lesson_base {
$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