Skip to content

Commit

Permalink
upgrade existing courses to new completionlib settings on plugin upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Sep 19, 2024
1 parent 9a03ad8 commit ab09fdd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
32 changes: 32 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use local_adler\local\exceptions\not_an_adler_course_exception;
use local_adler\local\upgrade\upgrade_3_2_0_to_4_0_0_completionlib;

/**
* @throws moodle_exception
* @throws not_an_adler_course_exception
*/
function xmldb_local_adler_upgrade($oldversion): bool {
// global $CFG, $DB;
// $dbman = $DB->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;
}
44 changes: 44 additions & 0 deletions tests/upgrade_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php /** @noinspection PhpIllegalPsrClassPathInspection */

global $CFG;

use local_adler\lib\adler_testcase;

require_once($CFG->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);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ab09fdd

Please sign in to comment.