Skip to content

Commit

Permalink
handling of invalid h5p elements, replace Dummy logger with a primiti…
Browse files Browse the repository at this point in the history
…ve cli logger
  • Loading branch information
Glutamat42 committed Oct 10, 2024
1 parent c0c867c commit 37960de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
14 changes: 9 additions & 5 deletions classes/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
use moodle_exception;
use stdClass;

class FakeLogger {
class SimpleCLILogger {
public function __call($name, $arguments) {
// Do nothing
$logLevels = ['info', 'warning', 'error'];
if (in_array($name, $logLevels) && isset($arguments[0])) {
cli_writeln(strtoupper($name) . ': ' . $arguments[0]);
}
}
}

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

public function __construct(int $course_id) {
global $CFG;
if (property_exists($CFG, 'upgraderunning') && $CFG->upgraderunning) {
// not allowed to depend on other plugins during upgrade
$this->logger = new FakeLogger();
$this->logger = new SimpleCLILogger();
$this->called_during_upgrade = true;
} else {
$this->logger = new logger('local_adler', self::class);
Expand Down Expand Up @@ -105,7 +108,8 @@ public function upgrade_h5p_module(stdClass $cm_info): void {
'gradepass' => $grade_item->grademax
]);
} catch (dml_exception $e) {
$this->logger->error('No grade item found for h5p cm ' . $cm_info->id);
$this->logger->warning('No grade item found for h5p cm ' . $cm_info->id . ', setting to "view tracking"');
$this->upgrade_normal_module($cm_info);
}
}

Expand Down
39 changes: 35 additions & 4 deletions tests/local/upgrade/upgrade_3_2_0_to_4_0_0_completionlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ public function test_completion_already_auto() {
$this->assertEquals(0, $cm->completionview);
}

public function test_h5p_element_without_grade() {
// create course
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
// make course an adler course
$this->getDataGenerator()->get_plugin_generator('local_adler')->create_adler_course_object($course->id);

// create module
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$module = $this->create_legacy_module('h5pactivity', $course->id, true);


$cud = new upgrade_3_2_0_to_4_0_0_completionlib($course->id);
$cud->execute();

$cm = get_fast_modinfo($course->id)->get_cm($module->cmid);
$this->assertEquals(COMPLETION_TRACKING_AUTOMATIC, $cm->completion);
$this->assertEquals(0, $cm->completionpassgrade);
$this->assertEquals(1, $cm->completionview);
}

public function test_execute_not_adler_course() {
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);

Expand All @@ -90,14 +111,24 @@ public function test_execute_not_adler_course() {
}



private function create_legacy_module(string $module_type, int $course_id) {
return $this->getDataGenerator()->get_plugin_generator('mod_' . $module_type)->create_instance([
/**
* @param string $module_type
* @param int $course_id
* @param bool $grade_none This disables the creation of an entry in the grade_items table
* @return stdClass
* @throws coding_exception
*/
private function create_legacy_module(string $module_type, int $course_id, bool $grade_none = false): stdClass {
$module_data = [
'course' => $course_id,
'completion' => COMPLETION_TRACKING_MANUAL,
'completeionview' => 0,
'completionpassgrade' => 0
]);
];
if ($grade_none) {
$module_data['grade'] = 0;
}
return $this->getDataGenerator()->get_plugin_generator('mod_' . $module_type)->create_instance($module_data);
}

public function test_with_user_attempt() {
Expand Down

0 comments on commit 37960de

Please sign in to comment.