From ee326d5e7db3dc372779059169cd4e696beabc36 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 6 Jun 2012 20:21:33 +0100 Subject: [PATCH] MDL-33548 quiz manual grading: check the grade is in range. To do this nicely, I refactored some code out of the quiz manual grading report. --- mod/quiz/comment.php | 2 +- mod/quiz/report/grading/report.php | 6 +----- question/behaviour/behaviourbase.php | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/mod/quiz/comment.php b/mod/quiz/comment.php index 8ac1f10a14d..85e6d455eca 100644 --- a/mod/quiz/comment.php +++ b/mod/quiz/comment.php @@ -55,7 +55,7 @@ echo $OUTPUT->heading(format_string($attemptobj->get_question_name($slot))); // Process any data that was submitted. if (data_submitted() && confirm_sesskey()) { - if (optional_param('submit', false, PARAM_BOOL)) { + if (optional_param('submit', false, PARAM_BOOL) && question_behaviour::is_manual_grade_in_range($attemptobj->get_uniqueid(), $slot)) { $transaction = $DB->start_delegated_transaction(); $attemptobj->process_submitted_actions(time()); $transaction->allow_commit(); diff --git a/mod/quiz/report/grading/report.php b/mod/quiz/report/grading/report.php index 1e7e7c0d769..37679f759c4 100644 --- a/mod/quiz/report/grading/report.php +++ b/mod/quiz/report/grading/report.php @@ -461,11 +461,7 @@ class quiz_grading_report extends quiz_default_report { foreach ($qubaids as $qubaid) { foreach ($slots as $slot) { - $prefix = 'q' . $qubaid . ':' . $slot . '_'; - $mark = optional_param($prefix . '-mark', null, PARAM_NUMBER); - $maxmark = optional_param($prefix . '-maxmark', null, PARAM_NUMBER); - $minfraction = optional_param($prefix . ':minfraction', null, PARAM_NUMBER); - if (!is_null($mark) && ($mark < $minfraction * $maxmark || $mark > $maxmark)) { + if (!question_behaviour::is_manual_grade_in_range($qubaid, $slot)) { return false; } } diff --git a/question/behaviour/behaviourbase.php b/question/behaviour/behaviourbase.php index ca09117def6..cd527ef3a74 100644 --- a/question/behaviour/behaviourbase.php +++ b/question/behaviour/behaviourbase.php @@ -458,7 +458,8 @@ abstract class question_behaviour { $fraction = null; } else if ($fraction > 1 || $fraction < $this->qa->get_min_fraction()) { throw new coding_exception('Score out of range when processing ' . - 'a manual grading action.', $pendingstep); + 'a manual grading action.', 'Question ' . $this->qa->get_question()->id . + ', slot ' . $this->qa->get_slot() . ', fraction ' . $fraction); } $pendingstep->set_fraction($fraction); } @@ -468,6 +469,20 @@ abstract class question_behaviour { return question_attempt::KEEP; } + /** + * Validate that the manual grade submitted for a particular question is in range. + * @param int $qubaid the question_usage id. + * @param int $slot the slot number within the usage. + * @return bool whether the submitted data is in range. + */ + public static function is_manual_grade_in_range($qubaid, $slot) { + $prefix = 'q' . $qubaid . ':' . $slot . '_'; + $mark = optional_param($prefix . '-mark', null, PARAM_NUMBER); + $maxmark = optional_param($prefix . '-maxmark', null, PARAM_NUMBER); + $minfraction = optional_param($prefix . ':minfraction', null, PARAM_NUMBER); + return is_null($mark) || ($mark >= $minfraction * $maxmark && $mark <= $maxmark); + } + /** * @param $comment the comment text to format. If omitted, * $this->qa->get_manual_comment() is used.