Skip to content

Commit

Permalink
MDL-78388 course: Copy locally assigned roles to new course module wh…
Browse files Browse the repository at this point in the history
…en duplicating
  • Loading branch information
djarran committed May 22, 2024
1 parent 7766f1c commit bfc44fb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,14 @@ function duplicate_module($course, $cm, int $sectionid = null, bool $changename
$DB->insert_record('role_capabilities', $override);
}

// Copy locally assigned roles to new course module.
$overrides = $DB->get_records('role_assignments', ['contextid' => $cmcontext->id]);
foreach ($overrides as $override) {
$override->contextid = $newcmcontext->id;
unset($override->id);
$DB->insert_record('role_assignments', $override);
}

// Trigger course module created event. We can trigger the event only if we know the newcmid.
$newcm = get_fast_modinfo($cm->course)->get_cm($newcmid);
$event = \core\event\course_module_created::create_from_cm($newcm);
Expand Down
38 changes: 38 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3083,6 +3083,44 @@ public function test_duplicate_module_permissions(): void {
$this->assertEquals(count($overrides), count($newoverrides));
}

/**
* Test that locally assigned roles are duplicated correctly after duplicate_module().
* @covers ::duplicate_module
* @return void
*/
public function test_duplicate_module_role_assignments(): void {
global $DB;
$this->setAdminUser();
$this->resetAfterTest();

// Create course and course module.
$course = self::getDataGenerator()->create_course();
$res = self::getDataGenerator()->create_module('assign', ['course' => $course]);
$cm = get_coursemodule_from_id('assign', $res->cmid, 0, false, MUST_EXIST);
$cmcontext = \context_module::instance($cm->id);

// Enrol student user.
$user = self::getDataGenerator()->create_user();
$roleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
self::getDataGenerator()->enrol_user($user->id, $course->id, $roleid);

// Assign user a new local role.
$newroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST);
role_assign($newroleid, $user->id, $cmcontext->id);

// Duplicate module.
$newcm = duplicate_module($course, $cm);
$newcmcontext = \context_module::instance($newcm->id);

// Assert that user still has role assigned.
$this->assertTrue(user_has_role_assignment($user->id, $newroleid, $newcmcontext->id));

// Assert that both modules contain the same count of overrides.
$overrides = $DB->get_records('role_assignments', ['contextid' => $cmcontext->id]);
$newoverrides = $DB->get_records('role_assignments', ['contextid' => $newcmcontext->id]);
$this->assertEquals(count($overrides), count($newoverrides));
}

/**
* Tests that when creating or updating a module, if the availability settings
* are present but set to an empty tree, availability is set to null in
Expand Down

0 comments on commit bfc44fb

Please sign in to comment.