Skip to content

Commit

Permalink
add database functions for upgrade code to moodle_core_repository.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Sep 16, 2024
1 parent 22cf619 commit 21e59be
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
28 changes: 28 additions & 0 deletions classes/local/db/moodle_core_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
65 changes: 65 additions & 0 deletions tests/local/db/moodle_core_repository_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

global $CFG;

use dml_exception;
use local_adler\lib\adler_testcase;
use local_adler\local\db\moodle_core_repository;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 21e59be

Please sign in to comment.