mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
MDL-67585 core_course: add user param to course_allowed_module
We want to be able to call this for a user, not rely on a global.
This commit is contained in:
parent
eec2d09f84
commit
8210186372
@ -2155,9 +2155,12 @@ function course_format_name ($course,$max=100) {
|
||||
* Is the user allowed to add this type of module to this course?
|
||||
* @param object $course the course settings. Only $course->id is used.
|
||||
* @param string $modname the module name. E.g. 'forum' or 'quiz'.
|
||||
* @param \stdClass $user the user to check, defaults to the global user if not provided.
|
||||
* @return bool whether the current user is allowed to add this type of module to this course.
|
||||
*/
|
||||
function course_allowed_module($course, $modname) {
|
||||
function course_allowed_module($course, $modname, \stdClass $user = null) {
|
||||
global $USER;
|
||||
$user = $user ?? $USER;
|
||||
if (is_numeric($modname)) {
|
||||
throw new coding_exception('Function course_allowed_module no longer
|
||||
supports numeric module ids. Please update your code to pass the module name.');
|
||||
@ -2179,7 +2182,7 @@ function course_allowed_module($course, $modname) {
|
||||
}
|
||||
|
||||
$coursecontext = context_course::instance($course->id);
|
||||
return has_capability($capability, $coursecontext);
|
||||
return has_capability($capability, $coursecontext, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6923,4 +6923,26 @@ class core_course_courselib_testcase extends advanced_testcase {
|
||||
$this->setAdminUser();
|
||||
$this->assertTrue($request2->can_approve());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the course allowed module method.
|
||||
*/
|
||||
public function test_course_allowed_module() {
|
||||
$this->resetAfterTest();
|
||||
global $DB;
|
||||
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
|
||||
$manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
|
||||
|
||||
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
||||
assign_capability('mod/assign:addinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
|
||||
|
||||
// Global user (teacher) has no permissions in this course.
|
||||
$this->setUser($teacher);
|
||||
$this->assertFalse(course_allowed_module($course, 'assign'));
|
||||
|
||||
// Manager has permissions.
|
||||
$this->assertTrue(course_allowed_module($course, 'assign', $manager));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user