From ff2ec2cb95b2cd673dcb199c0e7c45dc58516bef Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 27 Jul 2012 11:42:03 +0100 Subject: [PATCH] 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. --- mod/quiz/attempt.php | 1 + mod/quiz/attemptlib.php | 12 +++++++++++- mod/quiz/processattempt.php | 12 ++++++------ mod/quiz/review.php | 5 +++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 162e67262a7..a24245dd215 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -41,6 +41,7 @@ $attemptid = required_param('attempt', PARAM_INT); $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. diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index 78a74a61307..7d6b7965df1 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -555,6 +555,16 @@ class quiz_attempt { } } + /** + * 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(); @@ -1101,7 +1111,7 @@ class quiz_attempt { } /** - * 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) { diff --git a/mod/quiz/processattempt.php b/mod/quiz/processattempt.php index 1757eb5db41..9f391f04ecf 100644 --- a/mod/quiz/processattempt.php +++ b/mod/quiz/processattempt.php @@ -37,13 +37,13 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php'); $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); +$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); +$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); diff --git a/mod/quiz/review.php b/mod/quiz/review.php index 7bb6335d8fe..c19afaa8970 100644 --- a/mod/quiz/review.php +++ b/mod/quiz/review.php @@ -31,8 +31,8 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php'); 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) { @@ -44,6 +44,7 @@ if ($showall !== 0) { $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());