Skip to content

Commit a9cb77e

Browse files
authored
Merge pull request #137 from sumaiyamannan/teacherdropdown
Load teacher names in dialogue creation with large number of users and groups in a course
2 parents 85eb852 + a780f1a commit a9cb77e

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

classes/external/search_users.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use core_user_external;
3838
use context_module;
3939
use moodle_exception;
40-
use course_enrolment_manager;
4140
use core\session\exception;
4241

4342
/**
@@ -120,7 +119,7 @@ public static function execute(int $cmid, string $search, bool $searchanywhere,
120119
$groups);
121120

122121
} else {
123-
$manager = new course_enrolment_manager($PAGE, $course);
122+
$manager = new \mod_dialogue\local\course_enrolment_manager($PAGE, $course);
124123
$users = $manager->search_users($params['search'],
125124
$params['searchanywhere'],
126125
$params['page'],
@@ -139,10 +138,6 @@ public static function execute(int $cmid, string $search, bool $searchanywhere,
139138
if ($user->id == $USER->id) {
140139
continue;
141140
}
142-
if (!has_capability('mod/dialogue:receive', $context, $user)) {
143-
// User does not have the ability to receive so remove them.
144-
continue;
145-
}
146141
if ($userdetails = user_get_user_details($user, $course, $requiredfields)) {
147142
$results[] = $userdetails;
148143
}

classes/local/course_enrolment_manager.php

+55
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,59 @@ public function search_users_with_groups(string $search = '', bool $searchanywhe
6060
$params = array_merge($params, $inparams);
6161
return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, false);
6262
}
63+
64+
/**
65+
* Searches through the enrolled users in this course based on search_users but with mod_dialogue permission.
66+
*
67+
* @param string $search The search term.
68+
* @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
69+
* @param int $page Starting at 0.
70+
* @param int $perpage Number of users returned per page.
71+
* @param bool $returnexactcount Return the exact total users using count_record or not.
72+
* @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.
73+
* @return array with two or three elements:
74+
* int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
75+
* array users List of user objects returned by the query.
76+
* boolean moreusers True if there are still more users, otherwise is False.
77+
*/
78+
public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,
79+
bool $returnexactcount = false, ?int $contextid = null) {
80+
global $USER;
81+
82+
[$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere);
83+
84+
if (isset($contextid)) {
85+
// If contextid is set, we need to determine the group mode that should be used (module or course).
86+
[$context, $course, $cm] = get_context_info_array($contextid);
87+
// If cm instance is returned, then use the group mode from the module, otherwise get the course group mode.
88+
$groupmode = $cm ? groups_get_activity_groupmode($cm, $course) : groups_get_course_groupmode($this->course);
89+
} else {
90+
// Otherwise, default to the group mode of the course.
91+
$context = $this->context;
92+
$groupmode = groups_get_course_groupmode($this->course);
93+
}
94+
95+
if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
96+
$groups = groups_get_all_groups($this->course->id, $USER->id, 0, 'g.id');
97+
$groupids = array_column($groups, 'id');
98+
if (!$groupids) {
99+
return ['totalusers' => 0, 'users' => [], 'moreusers' => false];
100+
}
101+
} else {
102+
$groupids = [];
103+
}
104+
105+
[$enrolledsql, $enrolledparams] = get_enrolled_sql($context, 'mod/dialogue:receive', $groupids);
106+
107+
$fields = 'SELECT ' . $ufields;
108+
$countfields = 'SELECT COUNT(u.id)';
109+
$sql = " FROM {user} u
110+
$joins
111+
JOIN ($enrolledsql) je ON je.id = u.id
112+
WHERE $wherecondition";
113+
114+
$params = array_merge($params, $enrolledparams);
115+
116+
return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
117+
}
63118
}

0 commit comments

Comments
 (0)