Skip to content
Browse files

MDL-34599 quiz attempts: more robust if page number out of range.

Rather than throwing an exception, we should just show the first/last
page of the quiz if the page number is out-of-range.
  • Loading branch information...
1 parent dd9274c commit dcfdf468fc37fde8057dd63099681e455d182d9d @timhunt timhunt committed Jul 27, 2012
Showing with 23 additions and 11 deletions.
  1. +1 −0 mod/quiz/attempt.php
  2. +12 −2 mod/quiz/attemptlib.php
  3. +7 −7 mod/quiz/processattempt.php
  4. +3 −2 mod/quiz/review.php
View
1 mod/quiz/attempt.php
@@ -42,6 +42,7 @@
$page = optional_param('page', 0, PARAM_INT);
$attemptobj = quiz_attempt::create($attemptid);
+$page = $attemptobj->force_page_number_into_range($page);
$PAGE->set_url($attemptobj->attempt_url(null, $page));
// Check login.
View
14 mod/quiz/attemptlib.php
@@ -516,7 +516,17 @@ private function number_questions() {
}
}
- // Simple getters ======================================================================
+ /**
+ * If the given page number is out of range (before the first page, or after
+ * the last page, chnage it to be within range).
+ * @param int $page the requested page number.
+ * @return int a safe page number to use.
+ */
+ public function force_page_number_into_range($page) {
+ return min(max($page, 0), count($this->pagelayout) - 1);
+ }
+
+ // Simple getters ==========================================================
public function get_quiz() {
return $this->quizobj->get_quiz();
}
@@ -974,7 +984,7 @@ public function cannot_review_message($short = false) {
}
/**
- * Initialise the JS etc. required all the questions on a page..
+ * Initialise the JS etc. required all the questions on a page.
* @param mixed $page a page number, or 'all'.
*/
public function get_html_head_contributions($page = 'all', $showall = false) {
View
14 mod/quiz/processattempt.php
@@ -37,13 +37,13 @@
$timenow = time();
// Get submitted parameters.
-$attemptid = required_param('attempt', PARAM_INT);
-$next = optional_param('next', false, PARAM_BOOL);
-$thispage = optional_param('thispage', 0, PARAM_INT);
-$nextpage = optional_param('nextpage', 0, PARAM_INT);
-$finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
-$timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
-$scrollpos = optional_param('scrollpos', '', PARAM_RAW);
+$attemptid = required_param('attempt', PARAM_INT);
+$thispage = required_param('thispage', PARAM_INT);
+$nextpage = required_param('nextpage', PARAM_INT);
+$next = optional_param('next', false, PARAM_BOOL);
+$finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
+$timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
+$scrollpos = optional_param('scrollpos', '', PARAM_RAW);
$transaction = $DB->start_delegated_transaction();
$attemptobj = quiz_attempt::create($attemptid);
View
5 mod/quiz/review.php
@@ -32,8 +32,8 @@
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
$attemptid = required_param('attempt', PARAM_INT);
-$page = optional_param('page', 0, PARAM_INT);
-$showall = optional_param('showall', 0, PARAM_BOOL);
+$page = optional_param('page', 0, PARAM_INT);
+$showall = optional_param('showall', 0, PARAM_BOOL);
$url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
if ($page !== 0) {
@@ -45,6 +45,7 @@
$PAGE->set_url($url);
$attemptobj = quiz_attempt::create($attemptid);
+$page = $attemptobj->force_page_number_into_range($page);
// Check login.
require_login($attemptobj->get_course(), false, $attemptobj->get_cm());

0 comments on commit dcfdf46

Please sign in to comment.
Something went wrong with that request. Please try again.