From ab09fdd6a0c826aad58b063b8fe24a2e68a3667c Mon Sep 17 00:00:00 2001 From: Markus Date: Thu, 19 Sep 2024 14:32:34 +0200 Subject: [PATCH] upgrade existing courses to new completionlib settings on plugin upgrade --- db/upgrade.php | 32 ++++++++++++++++++++++++++++++ tests/upgrade_test.php | 44 ++++++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 db/upgrade.php create mode 100644 tests/upgrade_test.php diff --git a/db/upgrade.php b/db/upgrade.php new file mode 100644 index 0000000..e9f6edc --- /dev/null +++ b/db/upgrade.php @@ -0,0 +1,32 @@ +get_manager(); // Loads ddl manager and xmldb classes. + + if ($oldversion < 2024090900) { + $courses = get_courses('all'); + foreach ($courses as $course) { + if ($course->id == 1) { + // moodle special course for startpage + continue; + } + try { + $upgrader = new upgrade_3_2_0_to_4_0_0_completionlib($course->id); + $upgrader->execute(); + } catch (not_an_adler_course_exception $e) { + // Do nothing + } + } + } + + // Everything has succeeded to here. Return true. + return true; +} \ No newline at end of file diff --git a/tests/upgrade_test.php b/tests/upgrade_test.php new file mode 100644 index 0000000..a33eb16 --- /dev/null +++ b/tests/upgrade_test.php @@ -0,0 +1,44 @@ +dirroot . '/local/adler/tests/lib/adler_testcase.php'); +require_once($CFG->dirroot . '/local/adler/db/upgrade.php'); + +class upgrade_test extends adler_testcase { + + private function create_legacy_module(int $course_id): stdClass { + return $this->getDataGenerator()->get_plugin_generator('mod_url')->create_instance([ + 'course' => $course_id, + 'completion' => COMPLETION_TRACKING_MANUAL, + 'completeionview' => 0, + 'completionpassgrade' => 0 + ]); + } + public function test_upgrade_2024090900_completion_changes() { + // Create 3 courses + $course1 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $course2 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $course3 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + + $this->getDataGenerator()->get_plugin_generator('local_adler')->create_adler_course_object($course1->id); + $this->getDataGenerator()->get_plugin_generator('local_adler')->create_adler_course_object($course3->id); + + $course1_mod1 = $this->create_legacy_module($course1->id); + $course2_mod1 = $this->create_legacy_module($course2->id); + $course_3_mod1 = $this->create_legacy_module($course3->id); + + // call cud + xmldb_local_adler_upgrade(2024090800); + + // verify + $cm1 = get_fast_modinfo($course1->id)->get_cm($course1_mod1->cmid); + $this->assertEquals(COMPLETION_TRACKING_AUTOMATIC, $cm1->completion); + $cm2 = get_fast_modinfo($course2->id)->get_cm($course2_mod1->cmid); + $this->assertEquals(COMPLETION_TRACKING_MANUAL, $cm2->completion); + $cm3 = get_fast_modinfo($course3->id)->get_cm($course_3_mod1->cmid); + $this->assertEquals(COMPLETION_TRACKING_AUTOMATIC, $cm3->completion); + } +} \ No newline at end of file diff --git a/version.php b/version.php index 305d49e..dede197 100644 --- a/version.php +++ b/version.php @@ -21,7 +21,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024060300; +$plugin->version = 2024090900; $plugin->requires = 2022112800; // Moodle version $plugin->component = 'local_adler'; $plugin->release = '3.2.0-dev';