diff --git a/lang/en/role.php b/lang/en/role.php index 27ab5477096..d91056c62a9 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -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'; diff --git a/lib/classes/moodlenet/utilities.php b/lib/classes/moodlenet/utilities.php index f47d2f8b0aa..334da949947 100644 --- a/lib/classes/moodlenet/utilities.php +++ b/lib/classes/moodlenet/utilities.php @@ -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; } } diff --git a/lib/db/access.php b/lib/db/access.php index 0ec36384501..bdb791f7e80 100644 --- a/lib/db/access.php +++ b/lib/db/access.php @@ -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, + ] + ], ); diff --git a/lib/tests/moodlenet/utilities_test.php b/lib/tests/moodlenet/utilities_test.php index b5e15e5c08c..cb24be9ef65 100644 --- a/lib/tests/moodlenet/utilities_test.php +++ b/lib/tests/moodlenet/utilities_test.php @@ -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)); + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index d99706fa030..adb6c051087 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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 ===