Skip to content

Commit

Permalink
MDL-75318 core: Capability to share course to MoodleNet
Browse files Browse the repository at this point in the history
  • Loading branch information
safatshahin committed Aug 22, 2023
1 parent e998f14 commit a0238f2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
1 change: 1 addition & 0 deletions lang/en/role.php
Expand Up @@ -313,6 +313,7 @@
$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';
Expand Down
39 changes: 34 additions & 5 deletions lib/classes/moodlenet/utilities.php
Expand Up @@ -43,14 +43,43 @@ public static function is_valid_instance(issuer $issuer): bool {
}

/**
* 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 changes: 10 additions & 0 deletions lib/db/access.php
Expand Up @@ -2724,4 +2724,14 @@
'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 changes: 42 additions & 0 deletions lib/tests/moodlenet/utilities_test.php
Expand Up @@ -133,4 +133,46 @@ public function test_can_user_share() {
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 changes: 2 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -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 ===

Expand Down

0 comments on commit a0238f2

Please sign in to comment.