diff --git a/classes/external/search_users.php b/classes/external/search_users.php index 35d619f..651d060 100644 --- a/classes/external/search_users.php +++ b/classes/external/search_users.php @@ -37,7 +37,6 @@ use core_user_external; use context_module; use moodle_exception; -use course_enrolment_manager; use core\session\exception; /** @@ -120,7 +119,7 @@ public static function execute(int $cmid, string $search, bool $searchanywhere, $groups); } else { - $manager = new course_enrolment_manager($PAGE, $course); + $manager = new \mod_dialogue\local\course_enrolment_manager($PAGE, $course); $users = $manager->search_users($params['search'], $params['searchanywhere'], $params['page'], @@ -139,10 +138,6 @@ public static function execute(int $cmid, string $search, bool $searchanywhere, if ($user->id == $USER->id) { continue; } - if (!has_capability('mod/dialogue:receive', $context, $user)) { - // User does not have the ability to receive so remove them. - continue; - } if ($userdetails = user_get_user_details($user, $course, $requiredfields)) { $results[] = $userdetails; } diff --git a/classes/local/course_enrolment_manager.php b/classes/local/course_enrolment_manager.php index 0b0898b..8e95f1a 100644 --- a/classes/local/course_enrolment_manager.php +++ b/classes/local/course_enrolment_manager.php @@ -60,4 +60,59 @@ public function search_users_with_groups(string $search = '', bool $searchanywhe $params = array_merge($params, $inparams); return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, false); } + + /** + * Searches through the enrolled users in this course based on search_users but with mod_dialogue permission. + * + * @param string $search The search term. + * @param bool $searchanywhere Can the search term be anywhere, or must it be at the start. + * @param int $page Starting at 0. + * @param int $perpage Number of users returned per page. + * @param bool $returnexactcount Return the exact total users using count_record or not. + * @param ?int $contextid Context ID we are in - we might use search on activity level and its group mode can be different from course group mode. + * @return array with two or three elements: + * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true) + * array users List of user objects returned by the query. + * boolean moreusers True if there are still more users, otherwise is False. + */ + public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25, + bool $returnexactcount = false, ?int $contextid = null) { + global $USER; + + [$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere); + + if (isset($contextid)) { + // If contextid is set, we need to determine the group mode that should be used (module or course). + [$context, $course, $cm] = get_context_info_array($contextid); + // If cm instance is returned, then use the group mode from the module, otherwise get the course group mode. + $groupmode = $cm ? groups_get_activity_groupmode($cm, $course) : groups_get_course_groupmode($this->course); + } else { + // Otherwise, default to the group mode of the course. + $context = $this->context; + $groupmode = groups_get_course_groupmode($this->course); + } + + if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) { + $groups = groups_get_all_groups($this->course->id, $USER->id, 0, 'g.id'); + $groupids = array_column($groups, 'id'); + if (!$groupids) { + return ['totalusers' => 0, 'users' => [], 'moreusers' => false]; + } + } else { + $groupids = []; + } + + [$enrolledsql, $enrolledparams] = get_enrolled_sql($context, 'mod/dialogue:receive', $groupids); + + $fields = 'SELECT ' . $ufields; + $countfields = 'SELECT COUNT(u.id)'; + $sql = " FROM {user} u + $joins + JOIN ($enrolledsql) je ON je.id = u.id + WHERE $wherecondition"; + + $params = array_merge($params, $enrolledparams); + + return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount); + } }