From f2384c458f2d3413e95c8188db9244cf1dc9fc72 Mon Sep 17 00:00:00 2001 From: Salem Saidi Date: Tue, 27 Feb 2024 19:00:36 -0500 Subject: [PATCH] MDL-81070 course: Display contact names in the search result by admin --- course/classes/management/helper.php | 54 ++++++++++++++++++++++++ course/tests/behat/course_search.feature | 29 +++++++++++++ lang/en/moodle.php | 1 + 3 files changed, 84 insertions(+) diff --git a/course/classes/management/helper.php b/course/classes/management/helper.php index a278032ea4c..1b253f79013 100644 --- a/course/classes/management/helper.php +++ b/course/classes/management/helper.php @@ -129,6 +129,12 @@ class helper { 'value' => join('
', $roledetails) ); } + + $contactsdetails = self::get_contacts_by_role_details($course); + if ($contactsdetails) { + $details['coursecontact'] = $contactsdetails; + } + if ($course->can_review_enrolments()) { $enrolmentlines = array(); $instances = \enrol_get_instances($course->id, true); @@ -163,6 +169,54 @@ class helper { return $details; } + /** + * Returns the course contact details, if any. + * + * @param \core_course_list_element $course + * @return array|null Returns null if there are no contacts, otherwise an array of contact details. + */ + private static function get_contacts_by_role_details(\core_course_list_element $course): ?array { + if (!$course->can_access()) { + return null; + } + + $contactsbyrole = []; + foreach ($course->get_course_contacts() as $contact) { + $rolenames = array_map( + fn($role) => $role->displayname, + $contact['roles'] + ); + $contacturl = new \moodle_url('/user/view.php', ['id' => $contact['user']->id]); + $coursecontact = \html_writer::link($contacturl, $contact['username']); + + foreach ($rolenames as $rolename) { + if (!array_key_exists($rolename, $contactsbyrole)) { + $contactsbyrole[$rolename] = []; + } + $contactsbyrole[$rolename][] = $coursecontact; + } + } + $contactsbyrolelist = []; + foreach ($contactsbyrole as $rolename => $contacts) { + $contactsbyrolelist[] = get_string( + 'contactsbyrolelist', + 'moodle', + (object) [ + 'role' => $rolename, + 'contacts' => implode(', ', $contacts), + ] + ); + } + + if (empty($contactsbyrolelist)) { + return null; + } + return [ + 'key' => \get_string('coursecontact', 'admin'), + 'value' => join('
', $contactsbyrolelist), + ]; + } + /** * Returns an array of actions that can be performed upon a category being shown in a list. * diff --git a/course/tests/behat/course_search.feature b/course/tests/behat/course_search.feature index bd5c99c5f67..6125955b7f5 100644 --- a/course/tests/behat/course_search.feature +++ b/course/tests/behat/course_search.feature @@ -27,6 +27,35 @@ Feature: Courses can be searched for and moved in bulk. And I should not see "English Y1" And I should not see "English Y2" + Scenario: Search courses displays contact names + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | t1@example.com | + | teacher2 | Teacher | 2 | t2@example.com | + | teacher3 | Teacher | 3 | t3@example.com | + | teacher4 | Teacher | 4 | t3@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | BIO1 | editingteacher | + | teacher2 | ENG2 | editingteacher | + | teacher3 | BIO1 | teacher | + | teacher4 | BIO1 | editingteacher | + And I log in as "admin" + And I navigate to "Appearance > Courses" in site administration + And I set the following fields to these values: + | Teacher | 1 | + | Non-editing teacher | 1 | + And I press "Save changes" + When I go to the courses management page + And I set the field "Search" to "BIO1" + And I press "Search" + Then I should see "Biology Y1" + When I follow "Biology Y1" + Then I should see "Course contacts" + And I should see "Teacher: Teacher 1, Teacher 4" + And I should see "Non-editing teacher: Teacher 3" + And I should not see "Teacher: Teacher 2" + @javascript Scenario: Search courses and move results in bulk Given I log in as "admin" diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 86251f5e3b8..d803a183969 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -297,6 +297,7 @@ $string['confirmednot'] = 'Your registration has not yet been confirmed. Please $string['confirmcheckfull'] = 'Are you absolutely sure you want to confirm {$a} ?'; $string['confirmcoursemove'] = 'Are you sure you want to move this course ({$a->course}) into this category ({$a->category})?'; $string['considereddigitalminor'] = 'You are too young to create an account on this site.'; +$string['contactsbyrolelist'] = '{$a->role}: {$a->contacts}'; $string['content'] = 'Content'; $string['contentexport_aboutthiscourse'] = 'Course summary'; $string['contentexport_coursesummary'] = 'This file is part of the content downloaded from {$a->coursename}.';