MDL-56303 quiz: lack of quiz filtering
authorDan Poltawski <dan@moodle.com>
Thu, 6 Oct 2016 15:51:00 +0000 (16:51 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 6 Oct 2016 20:24:57 +0000 (22:24 +0200)
Also, correctly feed completiongradeitemnumber with grade item numbers
and not with grade item ids.

mod/quiz/db/upgrade.php

index fc6ff3e..c7faa3e 100644 (file)
@@ -222,21 +222,24 @@ function xmldb_quiz_upgrade($oldversion) {
 
     if ($oldversion < 2016100300) {
         // Find quizzes with the combination of require passing grade and grade to pass 0.
-        $quizzes = $DB->get_records_sql("
-            SELECT gi.id, gi.iteminstance
+        $gradeitems = $DB->get_records_sql("
+            SELECT gi.id, gi.itemnumber, cm.id AS cmid
               FROM {quiz} q
         INNER JOIN {course_modules} cm ON q.id = cm.instance
         INNER JOIN {grade_items} gi ON q.id = gi.iteminstance
+        INNER JOIN {modules} m ON m.id = cm.module
              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));
-            }
+               AND cm.completiongradeitemnumber IS NULL
+               AND gi.itemmodule = m.name
+               AND gi.itemtype = ?
+               AND m.name = ?", array('mod', 'quiz'));
+
+        foreach ($gradeitems as $gradeitem) {
+            $DB->execute("UPDATE {course_modules}
+                             SET completiongradeitemnumber = :itemnumber
+                           WHERE id = :cmid",
+                array('itemnumber' => $gradeitem->itemnumber, 'cmid' => $gradeitem->cmid));
         }
         // Quiz savepoint reached.
         upgrade_mod_savepoint(true, 2016100300, 'quiz');