diff --git a/README.md b/README.md index 549fe85..f6e3a2d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,6 @@ Damit der mbz api endpunkt auch mit größeren Dateien funktioniert sind folgend - `max_execution_time` auf mindestens 60 setzen - `output_buffering` auf 8192 setzen ``` -todo: some variable might not be required anymore after upload_course rework ## Setup diff --git a/classes/adler_score.php b/classes/adler_score.php index a7b2d20..fb65956 100644 --- a/classes/adler_score.php +++ b/classes/adler_score.php @@ -52,7 +52,7 @@ public function __construct(cm_info $course_module, int $user_id = null) { // get adler score metadata object try { - $this->score_item = $this->adler_course_module_repository->get_adler_score_record_by_cmid($this->course_module->id); + $this->score_item = $this->adler_course_module_repository->get_adler_course_module_by_cmid($this->course_module->id); } catch (dml_exception $e) { $this->logger->error('Could not get adler score record for cmid ' . $this->course_module->id . ': ' . $e->getMessage()); throw new not_an_adler_cm_exception(); diff --git a/classes/external/trigger_event_cm_viewed.php b/classes/external/trigger_event_cm_viewed.php index 552aecf..e4487c7 100644 --- a/classes/external/trigger_event_cm_viewed.php +++ b/classes/external/trigger_event_cm_viewed.php @@ -43,8 +43,6 @@ public static function execute_returns(): external_function_parameters { * @throws invalid_parameter_exception */ public static function execute($module_id): array { - // TODO: a lot of unused code -> refactor - $logger = new logger('local_adler', 'trigger_event_cm_viewed'); $adler_course_module_repository = new adler_course_module_repository(); // Parameter validation @@ -56,8 +54,7 @@ public static function execute($module_id): array { try { $course_module = get_coursemodule_from_id(null, $params['module_id'], 0, false, MUST_EXIST); } catch (dml_exception $e) { - // PHPStorm says this exception is never thrown, but this is wrong, - // see test test_score_primitive_learning_element_course_module_not_exist + // dml_exception is thrown, but not documented in get_coursemodule_from_id function signature throw new invalid_parameter_exception('failed_to_get_course_module'); } $course_module_cm_info = get_fast_modinfo($course_module->course)->get_cm($course_module->id); @@ -69,10 +66,7 @@ public static function execute($module_id): array { throw new not_an_adler_course_exception(); } // validate course module is adler course module - try { - $adler_course_module_repository->get_adler_score_record_by_cmid($course_module->id); - // todo: improve this -> separate function to test if cm is adler cm - } catch (dml_exception $e) { + if (!$adler_course_module_repository->record_for_cmid_exists($course_module->id)) { throw new not_an_adler_cm_exception(); } @@ -81,7 +75,6 @@ public static function execute($module_id): array { self::validate_context($context); - // trigger event self::trigger_module_specific_view_event($course_module, $course); @@ -95,7 +88,7 @@ public static function execute($module_id): array { /** * @throws coding_exception */ - private static function trigger_module_specific_view_event($course_module, $course) { + private static function trigger_module_specific_view_event($course_module, $course): void { $module_context = context_module::instance($course_module->id); // Determine the specific event class for the module @@ -111,11 +104,8 @@ private static function trigger_module_specific_view_event($course_module, $cour ]); $event->trigger(); - // completion $completion = new completion_info($course); $completion->set_module_viewed($course_module); - - // todo: maybe prefer _view() function of each module if it exists } } diff --git a/classes/local/db/adler_course_module_repository.php b/classes/local/db/adler_course_module_repository.php index 3edb41e..0fa4da4 100644 --- a/classes/local/db/adler_course_module_repository.php +++ b/classes/local/db/adler_course_module_repository.php @@ -6,12 +6,24 @@ use stdClass; class adler_course_module_repository extends base_repository { + /** + * Check if a record for the given cmid exists. This is equivalent to + * checking if a course module is an adler course module. + * + * @param int $cmid + * @return bool + * @throws dml_exception + */ + public function record_for_cmid_exists(int $cmid): bool { + return $this->db->record_exists('local_adler_course_modules', array('cmid' => $cmid)); + } + /** * @param int $cmid * @return stdClass course_module record * @throws dml_exception */ - public function get_adler_score_record_by_cmid(int $cmid): stdClass { + public function get_adler_course_module_by_cmid(int $cmid): stdClass { return $this->db->get_record('local_adler_course_modules', array('cmid' => $cmid), '*', MUST_EXIST); } diff --git a/classes/observer.php b/classes/observer.php index a74b6e8..b044809 100644 --- a/classes/observer.php +++ b/classes/observer.php @@ -35,7 +35,7 @@ public static function course_module_deleted(course_module_deleted $event): void } // check if is adler cm try { - $adler_course_module_repository->get_adler_score_record_by_cmid($cmid); + $adler_course_module_repository->get_adler_course_module_by_cmid($cmid); } catch (dml_exception $e) { return; } diff --git a/dev_utils/seed/section.php b/dev_utils/seed/section.php index 80ceb2b..d8f301a 100644 --- a/dev_utils/seed/section.php +++ b/dev_utils/seed/section.php @@ -60,7 +60,7 @@ function seed(int $course_id) { $max_possible_score = 0; foreach ($cms_in_section as $cm) { try { - $adler_cm = $adler_course_module_repository->get_adler_score_record_by_cmid($cm->id); + $adler_cm = $adler_course_module_repository->get_adler_course_module_by_cmid($cm->id); } catch (dml_exception $e) { continue; } diff --git a/tests/external/deprecated_mocks.php b/tests/external/deprecated_mocks.php index afcbc70..2b03549 100644 --- a/tests/external/deprecated_mocks.php +++ b/tests/external/deprecated_mocks.php @@ -6,7 +6,6 @@ namespace local_adler\external; -use local_adler\adler_score; use local_adler\adler_score_helpers_mock; require_once(__DIR__ . '/generic_mocks.php'); diff --git a/tests/helpers_test.php b/tests/helpers_test.php index 58bc681..8b2652c 100644 --- a/tests/helpers_test.php +++ b/tests/helpers_test.php @@ -4,7 +4,6 @@ use local_adler\lib\adler_testcase; -use local_adler\lib\static_mock_utilities_trait; # import for other classes importing this class use moodle_exception; use Throwable; diff --git a/tests/local/db/adler_course_module_repository_test.php b/tests/local/db/adler_course_module_repository_test.php index 3b7bccd..5bf28a8 100644 --- a/tests/local/db/adler_course_module_repository_test.php +++ b/tests/local/db/adler_course_module_repository_test.php @@ -27,7 +27,7 @@ public function test_get_adler_score_record() { $adler_score_item = $this->getDataGenerator()->get_plugin_generator('local_adler')->create_adler_course_module($cm->cmid); // call function - $result = $adler_course_module_repository->get_adler_score_record_by_cmid($cm->cmid); + $result = $adler_course_module_repository->get_adler_course_module_by_cmid($cm->cmid); // check result $this->assertEquals($adler_score_item->id, $result->id); @@ -42,7 +42,7 @@ public function test_get_adler_score_record() { $cm = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); // call function - $adler_course_module_repository->get_adler_score_record_by_cmid($cm->cmid); + $adler_course_module_repository->get_adler_course_module_by_cmid($cm->cmid); } public function test_delete_adler_course_module_record() {