diff --git a/public/mod/bigbluebuttonbn/classes/local/helpers/roles.php b/public/mod/bigbluebuttonbn/classes/local/helpers/roles.php index 5811324805c..c4ea0b0b640 100644 --- a/public/mod/bigbluebuttonbn/classes/local/helpers/roles.php +++ b/public/mod/bigbluebuttonbn/classes/local/helpers/roles.php @@ -26,8 +26,8 @@ namespace mod_bigbluebuttonbn\local\helpers; use cache; use cache_store; -use context; -use context_course; +use core\context; +use core\context\{course, system}; use mod_bigbluebuttonbn\instance; use mod_bigbluebuttonbn\local\proxy\bigbluebutton_proxy; use stdClass; @@ -81,11 +81,11 @@ class roles { /** * Returns an array containing all the users in a context wrapped for html select element. * - * @param context_course $context + * @param course $context * @param null $bbactivity * @return array $users */ - public static function get_users_array(context_course $context, $bbactivity = null) { + public static function get_users_array(course $context, $bbactivity = null) { // CONTRIB-7972, check the group of current user and course group mode. $groups = null; $users = (array) get_enrolled_users($context, '', 0, 'u.*', null, 0, 0, true); @@ -120,11 +120,10 @@ class roles { public static function has_capability_in_course(int $courseid, string $capability) { global $DB; if (empty($courseid) || !$DB->record_exists('course', ['id' => $courseid])) { - return has_capability('moodle/site:config', \context_system::instance()); + return has_capability('moodle/site:config', system::instance()); } - $coursecontext = context_course::instance($courseid); - return has_capability($capability, $coursecontext); + return has_capability($capability, course::instance($courseid)); } /** @@ -440,7 +439,7 @@ class roles { */ public static function import_get_courses_for_select(instance $instance): array { if ($instance->is_admin()) { - $courses = get_courses('all', 'c.fullname ASC'); + $courses = get_courses('all', 'c.fullname ASC', 'c.id, c.shortname, c.fullname'); // It includes the name of the site as a course (category 0), so remove the first one. unset($courses['1']); } else { @@ -452,7 +451,10 @@ class roles { }); $coursesforselect = []; foreach ($courses as $course) { - $coursesforselect[$course->id] = $course->fullname . " (" . $course->shortname . ")"; + $coursesforselect[$course->id] = format_string( + get_course_display_name_for_list($course), + options: ['context' => course::instance($course->id)], + ); } return $coursesforselect; } diff --git a/public/mod/bigbluebuttonbn/tests/behat/recordings_import.feature b/public/mod/bigbluebuttonbn/tests/behat/recordings_import.feature index 851c9d68d73..e92dba2ae73 100644 --- a/public/mod/bigbluebuttonbn/tests/behat/recordings_import.feature +++ b/public/mod/bigbluebuttonbn/tests/behat/recordings_import.feature @@ -39,7 +39,7 @@ Feature: Manage and list recordings Scenario Outline: I check that I can import recordings into the Recording activity from other activities When I am on the "" "bigbluebuttonbn activity" page logged in as "admin" And I click on "Import recording links" "button" - And I select "Test Course 1 (C1)" from the "sourcecourseid" singleselect + And I select "Test Course 1" from the "sourcecourseid" singleselect And I select "RoomRecordings" from the "sourcebn" singleselect # add the first recording And I click on "a.action-icon" "css_element" in the "Recording 1" "table_row" @@ -59,7 +59,7 @@ Feature: Manage and list recordings When I am on the "RoomRecordings1" "bigbluebuttonbn activity" page logged in as "admin" And I change window size to "large" And I click on "Import recording links" "button" - And I select "Test Course 1 (C1)" from the "sourcecourseid" singleselect + And I select "Test Course 1" from the "sourcecourseid" singleselect And I select "RoomRecordings" from the "sourcebn" singleselect # add the first recording And I click on "a.action-icon" "css_element" in the "Recording 1" "table_row" @@ -76,7 +76,7 @@ Feature: Manage and list recordings And I wait until the page is ready But I should not see "Recording 1" And I click on "Import recording links" "button" - And I select "Test Course 1 (C1)" from the "sourcecourseid" singleselect + And I select "Test Course 1" from the "sourcecourseid" singleselect And I select "RoomRecordings" from the "sourcebn" singleselect And I should see "Recording 1" diff --git a/public/mod/bigbluebuttonbn/tests/local/helpers/roles_test.php b/public/mod/bigbluebuttonbn/tests/local/helpers/roles_test.php index ea2fc16b6e8..9c8f8a33222 100644 --- a/public/mod/bigbluebuttonbn/tests/local/helpers/roles_test.php +++ b/public/mod/bigbluebuttonbn/tests/local/helpers/roles_test.php @@ -17,6 +17,7 @@ namespace mod_bigbluebuttonbn\local\helpers; use context_course; +use mod_bigbluebuttonbn\instance; use mod_bigbluebuttonbn\test\testcase_helper_trait; /** @@ -27,7 +28,6 @@ use mod_bigbluebuttonbn\test\testcase_helper_trait; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @author Laurent David (laurent@call-learning.fr) * @covers \mod_bigbluebuttonbn\local\helpers\roles - * @coversDefaultClass \mod_bigbluebuttonbn\local\helpers\roles */ final class roles_test extends \advanced_testcase { use testcase_helper_trait; @@ -91,4 +91,43 @@ final class roles_test extends \advanced_testcase { $users = roles::get_users_array($context, $bbactivity); $this->assertCount($numstudents + $numteachers, $users); } + + /** + * Test getting courses from which we can import + */ + public function test_import_get_courses_for_select(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', true); + + $courseone = $this->getDataGenerator()->create_course(); + [$context, $cm, $activity] = $this->create_instance($courseone); + $instance = instance::get_from_instanceid($activity->id); + + // Course two should be returned, because it has an activity instance. + $coursetwo = $this->getDataGenerator()->create_course([ + 'fullname' => 'EnglishSpanish', + ]); + $this->create_instance($coursetwo); + + // Course three should not be returned, because it has no activity instance. + $coursethree = $this->getDataGenerator()->create_course(); + + $coursesforselect = roles::import_get_courses_for_select($instance); + $this->assertEquals([ + $courseone->id => $courseone->fullname, + $coursetwo->id => 'English', + ], $coursesforselect); + + // Display extended course names. + set_config('courselistshortnames', 1); + + $coursesforselect = roles::import_get_courses_for_select($instance); + $this->assertEquals([ + $courseone->id => "{$courseone->shortname} {$courseone->fullname}", + $coursetwo->id => "{$coursetwo->shortname} English", + ], $coursesforselect); + } }