Skip to content

Commit

Permalink
refactor to use DB module format instead of cm_info as this is not al…
Browse files Browse the repository at this point in the history
…lowed during module upgrade (uses get_fast_modinfo internally) and use rebuild_course_cache to clear cache for the same reason. Replace logging component with a fake logger for similar reason
  • Loading branch information
Glutamat42 committed Sep 20, 2024
1 parent ab09fdd commit e30a16c
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php
Original file line number Diff line number Diff line change
@@ -9,13 +9,30 @@
use local_adler\local\exceptions\not_an_adler_course_exception;
use local_logging\logger;
use moodle_exception;
use stdClass;

class FakeLogger {
public function __call($name, $arguments) {
// Do nothing
}
}

class upgrade_3_2_0_to_4_0_0_completionlib {
private int $course_id;
private logger $logger;
private logger|FakeLogger $logger;
private moodle_core_repository $moodle_core_repository;
private bool $called_during_upgrade;

public function __construct(int $course_id) {
$this->logger = new logger('local_adler', self::class);
global $CFG;
if (property_exists($CFG, 'upgraderunning') && $CFG->upgraderunning) {
// not allowed to depend on other plugins during upgrade
$this->logger = new FakeLogger();
$this->called_during_upgrade = true;
} else {
$this->logger = new logger('local_adler', self::class);
$this->called_during_upgrade = false;
}
$this->course_id = $course_id;
$this->moodle_core_repository = new moodle_core_repository();
}
@@ -35,11 +52,11 @@ public function execute(): void {
}

/**
* @param cm_info $cm_info
* @param stdClass $cm_info
* @return void
* @throws dml_exception
*/
private function upgrade_module(cm_info $cm_info): void {
private function upgrade_module(stdClass $cm_info): void {
if ($cm_info->completion == COMPLETION_TRACKING_MANUAL) {
if ($cm_info->modname == "h5pactivity") {
$this->upgrade_h5p_module($cm_info);
@@ -57,7 +74,7 @@ private function upgrade_module(cm_info $cm_info): void {
*/
private function resetCourseCache(): void {
purge_caches(['courses' => $this->course_id]);
get_fast_modinfo($this->course_id, 0, true);
rebuild_course_cache($this->course_id, true);
}

/**
@@ -66,7 +83,16 @@ private function resetCourseCache(): void {
*/
public function upgrade_modules(): void {
global $DB;
$cm_infos = get_fast_modinfo($this->course_id)->get_cms();
// get_fast_modinfo is not allowed during upgrade and there is no get_modinfo
// TODO: probably always follow this approach
// TODO: repo pattern
$cm_infos = $DB->get_records_sql(
'SELECT cm.*, m.name AS modname
FROM {course_modules} cm
JOIN {modules} m ON m.id = cm.module
WHERE cm.course = ?',
[$this->course_id]
);

$transaction = $DB->start_delegated_transaction();
foreach ($cm_infos as $cm_info) {
@@ -76,11 +102,11 @@ public function upgrade_modules(): void {
}

/**
* @param cm_info $cm_info
* @param stdClass $cm_info
* @return void
* @throws dml_exception
*/
public function upgrade_h5p_module(cm_info $cm_info): void {
public function upgrade_h5p_module(stdClass $cm_info): void {
$grade_item = $this->moodle_core_repository->get_grade_item('h5pactivity', $cm_info->instance);
if ($grade_item) {
$this->logger->info('Setting completion for h5p cm ' . $cm_info->id . ' to "passing grade"');
@@ -94,22 +120,22 @@ public function upgrade_h5p_module(cm_info $cm_info): void {
}

/**
* @param cm_info $cm_info
* @param stdClass $cm_info
* @return void
* @throws dml_exception
*/
public function upgrade_normal_module(cm_info $cm_info): void {
public function upgrade_normal_module(stdClass $cm_info): void {
$this->logger->info('Setting completion for cm ' . $cm_info->id . ' to view tracking');
$this->set_completion_to_auto($cm_info, true);
}

/**
* @param cm_info $cm_info
* @param stdClass $cm_info
* @param bool $view if true: complete on view, if false: complete when achieving a passing grade
* @return void
* @throws dml_exception
*/
public function set_completion_to_auto(cm_info $cm_info, bool $view): void {
public function set_completion_to_auto(stdClass $cm_info, bool $view): void {
$this->moodle_core_repository->update_course_module_record($cm_info->id, [
'completion' => 2,
'completionpassgrade' => $view ? 0 : 1,

0 comments on commit e30a16c

Please sign in to comment.