diff --git a/classes/local/db/moodle_core_repository.php b/classes/local/db/moodle_core_repository.php index 63e4d1a..331e149 100644 --- a/classes/local/db/moodle_core_repository.php +++ b/classes/local/db/moodle_core_repository.php @@ -19,4 +19,32 @@ public function get_role_id_by_shortname(string $shortname): int|false { public function get_user_id_by_username(string $username): int|false { return (int)$this->db->get_field('user', 'id', array('username' => $username)); } + + + /** + * @param string $module name of the module + * @param int $instance_id instance id of the module + * @throws dml_exception if the record does not exist + */ + public function get_grade_item(string $module, int $instance_id): object { + return $this->db->get_record('grade_items', array('iteminstance' => $instance_id, 'itemmodule' => $module), '*', MUST_EXIST); + } + + /** + * @param int $grade_item_id + * @param array $data + * @throws dml_exception if the record does not exist + */ + public function update_grade_item_record(int $grade_item_id, array $data): void { + $this->db->update_record('grade_items', (object)array_merge($data, ['id' => $grade_item_id])); + } + + /** + * @param int $cmid + * @param array $data + * @throws dml_exception if the record does not exist + */ + public function update_course_module_record(int $cmid, array $data): void { + $this->db->update_record('course_modules', (object)array_merge($data, ['id' => $cmid])); + } } \ No newline at end of file diff --git a/tests/local/db/moodle_core_repository_test.php b/tests/local/db/moodle_core_repository_test.php index 17e07c4..495a78c 100644 --- a/tests/local/db/moodle_core_repository_test.php +++ b/tests/local/db/moodle_core_repository_test.php @@ -4,6 +4,7 @@ global $CFG; +use dml_exception; use local_adler\lib\adler_testcase; use local_adler\local\db\moodle_core_repository; @@ -41,4 +42,68 @@ public function test_get_user_id_by_username() { $this->assertEquals(false, $result); } + + public function provide_true_false_data() { + return [ + 'true' => [true], + 'false' => [false] + ]; + } + + /** + * @dataProvider provide_true_false_data + */ + public function test_get_grade_item($exists) { + $moodle_core_repository = new moodle_core_repository(); + + if ($exists) { + // create grade_item + $course = $this->getDataGenerator()->create_course(); + $grade_item = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id, 'itemmodule' => 'url', 'iteminstance' => 1]); + + // call function + $result = $moodle_core_repository->get_grade_item($grade_item->itemmodule, $grade_item->iteminstance); + + // check result + $this->assertEquals($grade_item->id, $result->id); + } else { + // error case + $this->expectException(dml_exception::class); + + // call function + $moodle_core_repository->get_grade_item('url', 1); + } + } + + public function test_update_grade_item_record() { + global $DB; + $moodle_core_repository = new moodle_core_repository(); + + // create grade_item + $course = $this->getDataGenerator()->create_course(); + $grade_item = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id, 'itemmodule' => 'url', 'iteminstance' => 1]); + + // call function + $moodle_core_repository->update_grade_item_record($grade_item->id, ['gradepass' => 100]); + + // check result + $result = $DB->get_record('grade_items', ['id' => $grade_item->id]); + $this->assertEquals(100, $result->gradepass); + } + + public function test_update_course_module_record() { + global $DB; + $moodle_core_repository = new moodle_core_repository(); + + // create course_module + $course = $this->getDataGenerator()->create_course(); + $course_module = $this->getDataGenerator()->create_module('url', ['course' => $course->id]); + + // call function + $moodle_core_repository->update_course_module_record($course_module->cmid, ['completion' => 2]); + + // check result + $result = $DB->get_record('course_modules', ['id' => $course_module->cmid]); + $this->assertEquals(2, $result->completion); + } } \ No newline at end of file