From 10d480aefc28fd4e93af30cc5ed5a503aa3a1c61 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 21 Sep 2016 16:09:25 +0800 Subject: [PATCH] MDL-54802 mod_quiz: Add validation to require passing grade greater than 0 This commit adds a form validation to prevent the creation of quizzes using zero or null grade to pass when using require passing grade. --- mod/quiz/db/upgrade.php | 22 ++++++++++++++++++++++ mod/quiz/lang/en/quiz.php | 1 + mod/quiz/mod_form.php | 7 +++++++ mod/quiz/version.php | 2 +- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mod/quiz/db/upgrade.php b/mod/quiz/db/upgrade.php index f42c0072f83..3483107af61 100644 --- a/mod/quiz/db/upgrade.php +++ b/mod/quiz/db/upgrade.php @@ -193,5 +193,27 @@ function xmldb_quiz_upgrade($oldversion) { // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2016052301) { + // Find quizzes with the combination of require passing grade and grade to pass 0. + $quizzes = $DB->get_records_sql(" + SELECT gi.id, gi.iteminstance + FROM {quiz} q + INNER JOIN {course_modules} cm ON q.id = cm.instance + INNER JOIN {grade_items} gi ON q.id = gi.iteminstance + WHERE q.completionpass = 1 + AND gi.gradepass = 0 + AND cm.completiongradeitemnumber IS NULL"); + if ($quizzes) { + foreach ($quizzes as $quiz) { + $DB->execute("UPDATE {course_modules} + SET completiongradeitemnumber = :gradeitemid + WHERE instance = :quizid", + array('gradeitemid' => $quiz->id, 'quizid' => $quiz->iteminstance)); + } + } + // Quiz savepoint reached. + upgrade_mod_savepoint(true, 2016052301, 'quiz'); + } + return true; } diff --git a/mod/quiz/lang/en/quiz.php b/mod/quiz/lang/en/quiz.php index 5ce7771fe60..6cdd6a25ab1 100644 --- a/mod/quiz/lang/en/quiz.php +++ b/mod/quiz/lang/en/quiz.php @@ -396,6 +396,7 @@ $string['grademethod_help'] = 'When multiple attempts are allowed, the following * Last attempt (all other attempts are ignored)'; $string['gradesdeleted'] = 'Quiz grades deleted'; $string['gradesofar'] = '{$a->method}: {$a->mygrade} / {$a->quizgrade}.'; +$string['gradetopassnotset'] = 'This quiz does not have a grade to pass set so you cannot use this option. Please use the require grade setting instead.'; $string['gradingdetails'] = 'Marks for this submission: {$a->raw}/{$a->max}.'; $string['gradingdetailsadjustment'] = 'With previous penalties this gives {$a->cur}/{$a->max}.'; $string['gradingdetailspenalty'] = 'This submission attracted a penalty of {$a}.'; diff --git a/mod/quiz/mod_form.php b/mod/quiz/mod_form.php index a768e975149..d2938baf8f8 100644 --- a/mod/quiz/mod_form.php +++ b/mod/quiz/mod_form.php @@ -533,6 +533,13 @@ class mod_quiz_mod_form extends moodleform_mod { } } + if (array_key_exists('completion', $data) && $data['completion'] == COMPLETION_TRACKING_AUTOMATIC) { + // Show an error if require passing grade was selected and the grade to pass was setted to 0. + if ($data['completionpass'] == 1 && (empty($data['gradepass']) || grade_floatval($data['gradepass']) == 0)) { + $errors['gradepass'] = get_string('gradetopassnotset', 'quiz'); + } + } + // Check the boundary value is a number or a percentage, and in range. $i = 0; while (!empty($data['feedbackboundaries'][$i] )) { diff --git a/mod/quiz/version.php b/mod/quiz/version.php index a2ce9496e4c..4bb9aee6dec 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; +$plugin->version = 2016052301; $plugin->requires = 2016051900; $plugin->component = 'mod_quiz'; $plugin->cron = 60;