MDL-65555 mod_quiz: fix group override backup and restore
authorNathan Nguyen <nathannguyen@catalyst-au.net>
Mon, 24 Jun 2019 05:49:44 +0000 (15:49 +1000)
committerNathan Nguyen <nathannguyen@catalyst-au.net>
Tue, 16 Jul 2019 01:16:03 +0000 (11:16 +1000)
mod/quiz/backup/moodle2/backup_quiz_stepslib.php
mod/quiz/backup/moodle2/restore_quiz_stepslib.php
mod/quiz/db/upgrade.php
mod/quiz/tests/restore_override_test.php [new file with mode: 0644]
mod/quiz/version.php

index 1e72487..ae65681 100644 (file)
@@ -140,6 +140,13 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru
             $overrideparams['userid'] = backup_helper::is_sqlparam(null);
 
         }
+
+        // Skip group overrides if not including groups.
+        $groupinfo = $this->get_setting_value('groups');
+        if (!$groupinfo) {
+            $overrideparams['groupid'] = backup_helper::is_sqlparam(null);
+        }
+
         $override->set_source_table('quiz_overrides', $overrideparams);
 
         // All the rest of elements only happen if we are including user info.
index 3c31ba2..aafaa3a 100644 (file)
@@ -377,6 +377,11 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
             $data->groupid = $this->get_mappingid('group', $data->groupid);
         }
 
+        // Skip if there is no user and no group data.
+        if (empty($data->userid) && empty($data->groupid)) {
+            return;
+        }
+
         $data->timeopen = $this->apply_date_offset($data->timeopen);
         $data->timeclose = $this->apply_date_offset($data->timeclose);
 
index 7c0a426..0435aed 100644 (file)
@@ -141,6 +141,13 @@ function xmldb_quiz_upgrade($oldversion) {
         upgrade_mod_savepoint(true, 2018040800, 'quiz');
     }
 
+    if ($oldversion < 2019062400) {
+        // Delete orphaned group overrides.
+        $DB->delete_records_select('quiz_overrides', 'groupid = 0 AND userid IS NULL');
+
+        upgrade_mod_savepoint(true, 2019062400, 'quiz');
+    }
+
     // Automatically generated Moodle v3.5.0 release upgrade line.
     // Put any upgrade step following this.
 
diff --git a/mod/quiz/tests/restore_override_test.php b/mod/quiz/tests/restore_override_test.php
new file mode 100644 (file)
index 0000000..7836a99
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Restore override tests.
+ *
+ * @package    mod_quiz
+ * @author   2019 Nathan Nguyen <nathannguyen@catalyst-au.net>
+ * @copyright Catalyst IT
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
+/**
+ * Restore override tests.
+ *
+ * @package    mod_quiz
+ * @author   2019 Nathan Nguyen <nathannguyen@catalyst-au.net>
+ * @copyright Catalyst IT
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_quiz_restore_override_testcase extends restore_date_testcase {
+
+    /**
+     * Test restore overrides.
+     */
+    public function test_restore_overrides() {
+        global $DB, $USER;
+        $this->resetAfterTest();
+
+        $course = $this->getDataGenerator()->create_course();
+        $quizgen = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
+        $quiz = $quizgen->create_instance(['course' => $course->id]);
+
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+
+        $now = 100;
+
+        // Group overrides.
+        $groupoverride1 = (object)[
+            'quiz' => $quiz->id,
+            'groupid' => $group1->id,
+            'timeopen' => $now,
+            'timeclose' => $now + 20
+        ];
+        $DB->insert_record('quiz_overrides', $groupoverride1);
+
+        $groupoverride2 = (object)[
+            'quiz' => $quiz->id,
+            'groupid' => $group2->id,
+            'timeopen' => $now,
+            'timeclose' => $now + 40
+        ];
+        $DB->insert_record('quiz_overrides', $groupoverride2);
+
+        // Current quiz overrides.
+        $overrides = $DB->get_records('quiz_overrides', ['quiz' => $quiz->id]);
+        $this->assertEquals(2, count($overrides));
+
+        // User Override.
+        $useroverride = (object)[
+            'quiz' => $quiz->id,
+            'userid' => $USER->id,
+            'sortorder' => 1,
+            'timeopen' => 100,
+            'timeclose' => 200
+        ];
+        $DB->insert_record('quiz_overrides', $useroverride);
+
+        // Current quiz overrides.
+        $overrides = $DB->get_records('quiz_overrides', ['quiz' => $quiz->id]);
+        $this->assertEquals(3, count($overrides));
+
+        // Back up and restore including group info and user info.
+        set_config('backup_general_groups', 1, 'backup');
+        $newcourseid = $this->backup_and_restore($course);
+        $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
+        $overrides = $DB->get_records('quiz_overrides', ['quiz' => $newquiz->id]);
+        // 2 groups overrides and 1 user override.
+        $this->assertEquals(3, count($overrides));
+
+        // Back up and restore with user info and without group info.
+        set_config('backup_general_groups', 0, 'backup');
+        $newcourseid = $this->backup_and_restore($course);
+        $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
+        $overrides = $DB->get_records('quiz_overrides', ['quiz' => $newquiz->id]);
+        // 1 user override.
+        $this->assertEquals(1, count($overrides));
+    }
+}
index 472a6ed..0783945 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2019052000;
+$plugin->version   = 2019062400;
 $plugin->requires  = 2019051100;
 $plugin->component = 'mod_quiz';