Skip to content

Commit

Permalink
cleanup & refactor a method to repo pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Sep 4, 2024
1 parent 7941d7e commit a2ad29e
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 22 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion classes/adler_score.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 3 additions & 13 deletions classes/external/trigger_event_cm_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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();
}

Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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 <modname>_view() function of each module if it exists
}
}
14 changes: 13 additions & 1 deletion classes/local/db/adler_course_module_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion dev_utils/seed/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion tests/external/deprecated_mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion tests/helpers_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions tests/local/db/adler_course_module_repository_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down

0 comments on commit a2ad29e

Please sign in to comment.