MDL-75318 core: Capability to share course to MoodleNet

This commit is contained in:
Safat
2023-08-22 11:00:06 +10:00
parent e998f14061
commit a0238f277d
5 changed files with 89 additions and 5 deletions
+1
View File
@@ -313,6 +313,7 @@ $string['managerdescription'] = 'Managers can access courses and modify them, bu
$string['manageroles'] = 'Manage roles';
$string['maybeassignedin'] = 'Context types where this role may be assigned';
$string['moodlenet:shareactivity'] = 'Share activities to MoodleNet';
$string['moodlenet:sharecourse'] = 'Share course to MoodleNet';
$string['morethan'] = 'More than {$a}';
$string['multipleroles'] = 'Multiple roles';
$string['my:manageblocks'] = 'Manage Dashboard page blocks';
+34 -5
View File
@@ -43,14 +43,43 @@ class utilities {
}
/**
* Check whether a user has the capabilities required to share activities from a given course to MoodleNet.
* Check whether a user has the capabilities required to share activities or courses to MoodleNet.
*
* @param \core\context\course $coursecontext Course context where the activity would be shared from.
* @param \core\context\course $coursecontext Course context where the activity or course would be shared from.
* @param int $userid The user ID being checked.
* @param string $type The type of resource being checked (either 'activity' or 'course').
* @return boolean
* @throws \coding_exception If an invalid resource type is provided.
*/
public static function can_user_share(\core\context\course $coursecontext, int $userid): bool {
return (has_capability('moodle/moodlenet:shareactivity', $coursecontext, $userid) &&
has_capability('moodle/backup:backupactivity', $coursecontext, $userid));
public static function can_user_share(\core\context\course $coursecontext, int $userid, string $type = 'activity'): bool {
if ($type === 'course') {
return (has_capability('moodle/moodlenet:sharecourse', $coursecontext, $userid) &&
has_capability('moodle/backup:backupcourse', $coursecontext, $userid));
} else if ($type === 'activity') {
return (has_capability('moodle/moodlenet:shareactivity', $coursecontext, $userid) &&
has_capability('moodle/backup:backupactivity', $coursecontext, $userid));
}
throw new \coding_exception('Invalid resource type');
}
/**
* Get the support url.
*
* @return string
*/
public static function get_support_url(): string {
global $CFG;
$supporturl = '';
if ($CFG->supportavailability && $CFG->supportavailability !== CONTACT_SUPPORT_DISABLED) {
if (!empty($CFG->supportpage)) {
$supporturl = $CFG->supportpage;
} else {
$supporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
}
}
return $supporturl;
}
}
+10
View File
@@ -2724,4 +2724,14 @@ $capabilities = array(
'manager' => CAP_ALLOW,
]
],
// Allow users to share courses to MoodleNet.
'moodle/moodlenet:sharecourse' => [
'captype' => 'read',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => [
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
]
],
);
+42
View File
@@ -133,4 +133,46 @@ class utilities_test extends \advanced_testcase {
assign_capability('moodle/moodlenet:shareactivity', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
$this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id));
}
/**
* Test can_user_share_course_to_moodlenet method.
*
* @covers ::can_user_share_course_to_moodlenet
*/
public function test_can_user_share_course_to_moodlenet(): void {
global $DB;
// Generate data.
$student1 = $this->generator->create_user();
$teacher1 = $this->generator->create_user();
$teacher2 = $this->generator->create_user();
$manager1 = $this->generator->create_user();
// Enrol users.
$this->generator->enrol_user($student1->id, $this->course->id, 'student');
$this->generator->enrol_user($teacher1->id, $this->course->id, 'teacher');
$this->generator->enrol_user($teacher2->id, $this->course->id, 'editingteacher');
$this->generator->enrol_user($manager1->id, $this->course->id, 'manager');
// Get roles.
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
$editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher'], 'id', MUST_EXIST);
// Test with default settings.
// Student and Teacher cannot share the course.
$this->assertFalse(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $student1->id));
$this->assertFalse(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $teacher1->id));
// Editing-teacher and Manager can share the course.
$this->assertTrue(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $teacher2->id));
$this->assertTrue(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $manager1->id));
// Teacher who has the capabilities can share the course.
assign_capability('moodle/moodlenet:sharecourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
$this->assertTrue(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $teacher1->id));
// Editing-teacher who does not have the capabilities can not share the course.
assign_capability('moodle/moodlenet:sharecourse', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
$this->assertFalse(utilities::can_user_share_course_to_moodlenet($this->coursecontext, $teacher2->id));
}
}
+2
View File
@@ -86,6 +86,8 @@ information provided here is intended especially for developers.
* New behat behat_navigation::i_close_block_drawer_if_open() and behat_navigation::i_keep_block_drawer_closed()
to ensure in some test that the block drawer is closed. This helps with random failures due to the block drawer
being forced open in all behat tests.
* The signature of the static method `can_user_share` in the `core\moodlenet\local\can_share_manager` class has been updated to include an additional parameter `$type`.
This parameter specifies the type of resource being checked for sharing capabilities, which can either be 'activity' or 'course'.
=== 4.2 ===