3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Code for ajax user selectors.
22 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * The default size of a user selector.
29 define('USER_SELECTOR_DEFAULT_ROWS', 20);
32 * Base class for user selectors.
34 * In your theme, you must give each user-selector a defined width. If the
35 * user selector has name="myid", then the div myid_wrapper must have a width
38 abstract class user_selector_base {
39 /** @var string The control name (and id) in the HTML. */
41 /** @var array Extra fields to search on and return in addition to firstname and lastname. */
42 protected $extrafields;
43 /** @var object Context used for capability checks regarding this selector (does
44 * not necessarily restrict user list) */
45 protected $accesscontext;
46 /** @var boolean Whether the conrol should allow selection of many users, or just one. */
47 protected $multiselect = true;
48 /** @var int The height this control should have, in rows. */
49 protected $rows = USER_SELECTOR_DEFAULT_ROWS;
50 /** @var array A list of userids that should not be returned by this control. */
51 protected $exclude = array();
52 /** @var array|null A list of the users who are selected. */
53 protected $selected = null;
54 /** @var boolean When the search changes, do we keep previously selected options that do
55 * not match the new search term? */
56 protected $preserveselected = false;
57 /** @var boolean If only one user matches the search, should we select them automatically. */
58 protected $autoselectunique = false;
59 /** @var boolean When searching, do we only match the starts of fields (better performance)
60 * or do we match occurrences anywhere? */
61 protected $searchanywhere = false;
62 /** @var mixed This is used by get selected users */
63 protected $validatinguserids = null;
65 /** @var boolean Used to ensure we only output the search options for one user selector on
67 private static $searchoptionsoutput = false;
69 /** @var array JavaScript YUI3 Module definition */
70 protected static $jsmodule = array(
71 'name' => 'user_selector',
72 'fullpath' => '/user/selector/module.js',
73 'requires' => array('node', 'event-custom', 'datasource', 'json'),
75 array('previouslyselectedusers', 'moodle', '%%SEARCHTERM%%'),
76 array('nomatchingusers', 'moodle', '%%SEARCHTERM%%'),
77 array('none', 'moodle')
81 // Public API ==============================================================
84 * Constructor. Each subclass must have a constructor with this signature.
86 * @param string $name the control name/id for use in the HTML.
87 * @param array $options other options needed to construct this selector.
88 * You must be able to clone a userselector by doing new get_class($us)($us->get_name(), $us->get_options());
90 public function __construct($name, $options = array()) {
93 // Initialise member variables from constructor arguments.
96 // Use specified context for permission checks, system context if not
98 if (isset($options['accesscontext'])) {
99 $this->accesscontext = $options['accesscontext'];
101 $this->accesscontext = get_context_instance(CONTEXT_SYSTEM);
104 if (isset($options['extrafields'])) {
105 $this->extrafields = $options['extrafields'];
106 } else if (!empty($CFG->showuseridentity) &&
107 has_capability('moodle/site:viewuseridentity', $this->accesscontext)) {
108 $this->extrafields = explode(',', $CFG->showuseridentity);
110 $this->extrafields = array();
112 if (isset($options['exclude']) && is_array($options['exclude'])) {
113 $this->exclude = $options['exclude'];
115 if (isset($options['multiselect'])) {
116 $this->multiselect = $options['multiselect'];
119 // Read the user prefs / optional_params that we use.
120 $this->preserveselected = $this->initialise_option('userselector_preserveselected', $this->preserveselected);
121 $this->autoselectunique = $this->initialise_option('userselector_autoselectunique', $this->autoselectunique);
122 $this->searchanywhere = $this->initialise_option('userselector_searchanywhere', $this->searchanywhere);
126 * All to the list of user ids that this control will not select. For example,
127 * on the role assign page, we do not list the users who already have the role
130 * @param array $arrayofuserids the user ids to exclude.
132 public function exclude($arrayofuserids) {
133 $this->exclude = array_unique(array_merge($this->exclude, $arrayofuserids));
137 * Clear the list of excluded user ids.
139 public function clear_exclusions() {
144 * @return array the list of user ids that this control will not select.
146 public function get_exclusions() {
147 return clone($this->exclude);
151 * @return array of user objects. The users that were selected. This is a more sophisticated version
152 * of optional_param($this->name, array(), PARAM_INTEGER) that validates the
153 * returned list of ids against the rules for this user selector.
155 public function get_selected_users() {
157 if (is_null($this->selected)) {
158 $this->selected = $this->load_selected_users();
160 return $this->selected;
164 * Convenience method for when multiselect is false (throws an exception if not).
165 * @return object the selected user object, or null if none.
167 public function get_selected_user() {
168 if ($this->multiselect) {
169 throw new moodle_exception('cannotcallusgetselecteduser');
171 $users = $this->get_selected_users();
172 if (count($users) == 1) {
173 return reset($users);
174 } else if (count($users) == 0) {
177 throw new moodle_exception('userselectortoomany');
182 * If you update the database in such a way that it is likely to change the
183 * list of users that this component is allowed to select from, then you
184 * must call this method. For example, on the role assign page, after you have
185 * assigned some roles to some users, you should call this.
187 public function invalidate_selected_users() {
188 $this->selected = null;
192 * Output this user_selector as HTML.
193 * @param boolean $return if true, return the HTML as a string instead of outputting it.
194 * @return mixed if $return is true, returns the HTML as a string, otherwise returns nothing.
196 public function display($return = false) {
199 // Get the list of requested users.
200 $search = optional_param($this->name . '_searchtext', '', PARAM_RAW);
201 if (optional_param($this->name . '_clearbutton', false, PARAM_BOOL)) {
204 $groupedusers = $this->find_users($search);
206 // Output the select.
209 if ($this->multiselect) {
211 $multiselect = 'multiple="multiple" ';
213 $output = '<div class="userselector" id="' . $this->name . '_wrapper">' . "\n" .
214 '<select name="' . $name . '" id="' . $this->name . '" ' .
215 $multiselect . 'size="' . $this->rows . '">' . "\n";
217 // Populate the select.
218 $output .= $this->output_options($groupedusers, $search);
220 // Output the search controls.
221 $output .= "</select>\n<div>\n";
222 $output .= '<input type="text" name="' . $this->name . '_searchtext" id="' .
223 $this->name . '_searchtext" size="15" value="' . s($search) . '" />';
224 $output .= '<input type="submit" name="' . $this->name . '_searchbutton" id="' .
225 $this->name . '_searchbutton" value="' . $this->search_button_caption() . '" />';
226 $output .= '<input type="submit" name="' . $this->name . '_clearbutton" id="' .
227 $this->name . '_clearbutton" value="' . get_string('clear') . '" />';
229 // And the search options.
230 $optionsoutput = false;
231 if (!user_selector_base::$searchoptionsoutput) {
232 $output .= print_collapsible_region_start('', 'userselector_options',
233 get_string('searchoptions'), 'userselector_optionscollapsed', true, true);
234 $output .= $this->option_checkbox('preserveselected', $this->preserveselected, get_string('userselectorpreserveselected'));
235 $output .= $this->option_checkbox('autoselectunique', $this->autoselectunique, get_string('userselectorautoselectunique'));
236 $output .= $this->option_checkbox('searchanywhere', $this->searchanywhere, get_string('userselectorsearchanywhere'));
237 $output .= print_collapsible_region_end(true);
239 $PAGE->requires->js_init_call('M.core_user.init_user_selector_options_tracker', array(), false, self::$jsmodule);
240 user_selector_base::$searchoptionsoutput = true;
242 $output .= "</div>\n</div>\n\n";
244 // Initialise the ajax functionality.
245 $output .= $this->initialise_javascript($search);
247 // Return or output it.
256 * The height this control will be displayed, in rows.
258 * @param integer $numrows the desired height.
260 public function set_rows($numrows) {
261 $this->rows = $numrows;
265 * @return integer the height this control will be displayed, in rows.
267 public function get_rows() {
272 * Whether this control will allow selection of many, or just one user.
274 * @param boolean $multiselect true = allow multiple selection.
276 public function set_multiselect($multiselect) {
277 $this->multiselect = $multiselect;
281 * @return boolean whether this control will allow selection of more than one user.
283 public function is_multiselect() {
284 return $this->multiselect;
288 * @return string the id/name that this control will have in the HTML.
290 public function get_name() {
295 * Set the user fields that are displayed in the selector in addition to the
298 * @param array $fields a list of field names that exist in the user table.
300 public function set_extra_fields($fields) {
301 $this->extrafields = $fields;
304 // API for sublasses =======================================================
307 * Search the database for users matching the $search string, and any other
308 * conditions that apply. The SQL for testing whether a user matches the
309 * search string should be obtained by calling the search_sql method.
311 * This method is used both when getting the list of choices to display to
312 * the user, and also when validating a list of users that was selected.
314 * When preparing a list of users to choose from ($this->is_validating()
315 * return false) you should probably have an maximum number of users you will
316 * return, and if more users than this match your search, you should instead
317 * return a message generated by the too_many_results() method. However, you
318 * should not do this when validating.
320 * If you are writing a new user_selector subclass, I strongly recommend you
321 * look at some of the subclasses later in this file and in admin/roles/lib.php.
322 * They should help you see exactly what you have to do.
324 * @param string $search the search string.
325 * @return array An array of arrays of users. The array keys of the outer
326 * array should be the string names of optgroups. The keys of the inner
327 * arrays should be userids, and the values should be user objects
328 * containing at least the list of fields returned by the method
329 * required_fields_sql(). If a user object has a ->disabled property
330 * that is true, then that option will be displayed greyed out, and
331 * will not be returned by get_selected_users.
333 public abstract function find_users($search);
337 * Note: this function must be implemented if you use the search ajax field
338 * (e.g. set $options['file'] = '/admin/filecontainingyourclass.php';)
339 * @return array the options needed to recreate this user_selector.
341 protected function get_options() {
343 'class' => get_class($this),
344 'name' => $this->name,
345 'exclude' => $this->exclude,
346 'extrafields' => $this->extrafields,
347 'multiselect' => $this->multiselect,
348 'accesscontext' => $this->accesscontext,
352 // Inner workings ==========================================================
355 * @return boolean if true, we are validating a list of selected users,
356 * rather than preparing a list of uesrs to choose from.
358 protected function is_validating() {
359 return !is_null($this->validatinguserids);
363 * Get the list of users that were selected by doing optional_param then
364 * validating the result.
366 * @return array of user objects.
368 protected function load_selected_users() {
369 // See if we got anything.
370 if ($this->multiselect) {
371 $userids = optional_param_array($this->name, array(), PARAM_INTEGER);
373 $userid = optional_param($this->name, 0, PARAM_INTEGER);
374 if (empty($userid)) {
377 $userids = array($userid);
380 // If we did, use the find_users method to validate the ids.
381 $this->validatinguserids = $userids;
382 $groupedusers = $this->find_users('');
383 $this->validatinguserids = null;
385 // Aggregate the resulting list back into a single one.
387 foreach ($groupedusers as $group) {
388 foreach ($group as $user) {
389 if (!isset($users[$user->id]) && empty($user->disabled) && in_array($user->id, $userids)) {
390 $users[$user->id] = $user;
395 // If we are only supposed to be selecting a single user, make sure we do.
396 if (!$this->multiselect && count($users) > 1) {
397 $users = array_slice($users, 0, 1);
404 * @param string $u the table alias for the user table in the query being
406 * @return string fragment of SQL to go in the select list of the query.
408 protected function required_fields_sql($u) {
409 // Raw list of fields.
410 $fields = array('id', 'firstname', 'lastname');
411 $fields = array_merge($fields, $this->extrafields);
413 // Prepend the table alias.
415 foreach ($fields as &$field) {
416 $field = $u . '.' . $field;
419 return implode(',', $fields);
423 * @param string $search the text to search for.
424 * @param string $u the table alias for the user table in the query being
426 * @return array an array with two elements, a fragment of SQL to go in the
427 * where clause the query, and an array containing any required parameters.
428 * this uses ? style placeholders.
430 protected function search_sql($search, $u) {
439 // If we have a $search string, put a field LIKE '$search%' condition on each field.
442 $DB->sql_fullname($u . 'firstname', $u . 'lastname'),
443 $conditions[] = $u . 'lastname'
445 foreach ($this->extrafields as $field) {
446 $conditions[] = $u . $field;
448 if ($this->searchanywhere) {
449 $searchparam = '%' . $search . '%';
451 $searchparam = $search . '%';
454 foreach ($conditions as $key=>$condition) {
455 $conditions[$key] = $DB->sql_like($condition, ":con{$i}00", false, false);
456 $params["con{$i}00"] = $searchparam;
459 $tests[] = '(' . implode(' OR ', $conditions) . ')';
462 // Add some additional sensible conditions
463 $tests[] = $u . "id <> :guestid";
464 $params['guestid'] = $CFG->siteguest;
465 $tests[] = $u . 'deleted = 0';
466 $tests[] = $u . 'confirmed = 1';
468 // If we are being asked to exclude any users, do that.
469 if (!empty($this->exclude)) {
470 list($usertest, $userparams) = $DB->get_in_or_equal($this->exclude, SQL_PARAMS_NAMED, 'ex', false);
471 $tests[] = $u . 'id ' . $usertest;
472 $params = array_merge($params, $userparams);
475 // If we are validating a set list of userids, add an id IN (...) test.
476 if (!empty($this->validatinguserids)) {
477 list($usertest, $userparams) = $DB->get_in_or_equal($this->validatinguserids, SQL_PARAMS_NAMED, 'val');
478 $tests[] = $u . 'id ' . $usertest;
479 $params = array_merge($params, $userparams);
486 // Combing the conditions and return.
487 return array(implode(' AND ', $tests), $params);
491 * Used to generate a nice message when there are too many users to show.
492 * The message includes the number of users that currently match, and the
493 * text of the message depends on whether the search term is non-blank.
495 * @param string $search the search term, as passed in to the find users method.
496 * @param int $count the number of users that currently match.
497 * @return array in the right format to return from the find_users method.
499 protected function too_many_results($search, $count) {
503 $a->search = $search;
504 return array(get_string('toomanyusersmatchsearch', '', $a) => array(),
505 get_string('pleasesearchmore') => array());
507 return array(get_string('toomanyuserstoshow', '', $count) => array(),
508 get_string('pleaseusesearch') => array());
513 * Output the list of <optgroup>s and <options>s that go inside the select.
514 * This method should do the same as the JavaScript method
515 * user_selector.prototype.handle_response.
517 * @param array $groupedusers an array, as returned by find_users.
518 * @return string HTML code.
520 protected function output_options($groupedusers, $search) {
523 // Ensure that the list of previously selected users is up to date.
524 $this->get_selected_users();
526 // If $groupedusers is empty, make a 'no matching users' group. If there is
527 // only one selected user, set a flag to select them if that option is turned on.
529 if (empty($groupedusers)) {
530 if (!empty($search)) {
531 $groupedusers = array(get_string('nomatchingusers', '', $search) => array());
533 $groupedusers = array(get_string('none') => array());
535 } else if ($this->autoselectunique && count($groupedusers) == 1 &&
536 count(reset($groupedusers)) == 1) {
538 if (!$this->multiselect) {
539 $this->selected = array();
543 // Output each optgroup.
544 foreach ($groupedusers as $groupname => $users) {
545 $output .= $this->output_optgroup($groupname, $users, $select);
548 // If there were previously selected users who do not match the search, show them too.
549 if ($this->preserveselected && !empty($this->selected)) {
550 $output .= $this->output_optgroup(get_string('previouslyselectedusers', '', $search), $this->selected, true);
553 // This method trashes $this->selected, so clear the cache so it is
554 // rebuilt before anyone tried to use it again.
555 $this->selected = null;
561 * Output one particular optgroup. Used by the preceding function output_options.
563 * @param string $groupname the label for this optgroup.
564 * @param array $users the users to put in this optgroup.
565 * @param boolean $select if true, select the users in this group.
566 * @return string HTML code.
568 protected function output_optgroup($groupname, $users, $select) {
569 if (!empty($users)) {
570 $output = ' <optgroup label="' . htmlspecialchars($groupname) . ' (' . count($users) . ')">' . "\n";
571 foreach ($users as $user) {
573 if (!empty($user->disabled)) {
574 $attributes .= ' disabled="disabled"';
575 } else if ($select || isset($this->selected[$user->id])) {
576 $attributes .= ' selected="selected"';
578 unset($this->selected[$user->id]);
579 $output .= ' <option' . $attributes . ' value="' . $user->id . '">' .
580 $this->output_user($user) . "</option>\n";
583 $output = ' <optgroup label="' . htmlspecialchars($groupname) . '">' . "\n";
584 $output .= ' <option disabled="disabled"> </option>' . "\n";
586 $output .= " </optgroup>\n";
591 * Convert a user object to a string suitable for displaying as an option in the list box.
593 * @param object $user the user to display.
594 * @return string a string representation of the user.
596 public function output_user($user) {
597 $out = fullname($user);
598 if ($this->extrafields) {
599 $displayfields = array();
600 foreach ($this->extrafields as $field) {
601 $displayfields[] = $user->{$field};
603 $out .= ' (' . implode(', ', $displayfields) . ')';
609 * @return string the caption for the search button.
611 protected function search_button_caption() {
612 return get_string('search');
615 // Initialise one of the option checkboxes, either from
616 // the request, or failing that from the user_preferences table, or
617 // finally from the given default.
618 private function initialise_option($name, $default) {
619 $param = optional_param($name, null, PARAM_BOOL);
620 if (is_null($param)) {
621 return get_user_preferences($name, $default);
623 set_user_preference($name, $param);
628 // Output one of the options checkboxes.
629 private function option_checkbox($name, $on, $label) {
631 $checked = ' checked="checked"';
635 $name = 'userselector_' . $name;
636 $output = '<p><input type="hidden" name="' . $name . '" value="0" />' .
637 // For the benefit of brain-dead IE, the id must be different from the name of the hidden form field above.
638 // It seems that document.getElementById('frog') in IE will return and element with name="frog".
639 '<input type="checkbox" id="' . $name . 'id" name="' . $name . '" value="1"' . $checked . ' /> ' .
640 '<label for="' . $name . 'id">' . $label . "</label></p>\n";
641 user_preference_allow_ajax_update($name, PARAM_BOOL);
646 * @param boolean $optiontracker if true, initialise JavaScript for updating the user prefs.
647 * @return any HTML needed here.
649 protected function initialise_javascript($search) {
650 global $USER, $PAGE, $OUTPUT;
653 // Put the options into the session, to allow search.php to respond to the ajax requests.
654 $options = $this->get_options();
655 $hash = md5(serialize($options));
656 $USER->userselectors[$hash] = $options;
658 // Initialise the selector.
659 $PAGE->requires->js_init_call('M.core_user.init_user_selector', array($this->name, $hash, $this->extrafields, $search), false, self::$jsmodule);
664 // User selectors for managing group members ==================================
667 * Base class to avoid duplicating code.
669 abstract class groups_user_selector_base extends user_selector_base {
674 * @param string $name control name
675 * @param array $options should have two elements with keys groupid and courseid.
677 public function __construct($name, $options) {
679 $options['accesscontext'] = get_context_instance(CONTEXT_COURSE, $options['courseid']);
680 parent::__construct($name, $options);
681 $this->groupid = $options['groupid'];
682 $this->courseid = $options['courseid'];
683 require_once($CFG->dirroot . '/group/lib.php');
686 protected function get_options() {
687 $options = parent::get_options();
688 $options['groupid'] = $this->groupid;
689 $options['courseid'] = $this->courseid;
694 * @param array $roles array in the format returned by groups_calculate_role_people.
695 * @return array array in the format find_users is supposed to return.
697 protected function convert_array_format($roles, $search) {
701 $groupedusers = array();
702 foreach ($roles as $role) {
705 $a->role = $role->name;
706 $a->search = $search;
707 $groupname = get_string('matchingsearchandrole', '', $a);
709 $groupname = $role->name;
711 $groupedusers[$groupname] = $role->users;
712 foreach ($groupedusers[$groupname] as &$user) {
714 $user->fullname = fullname($user);
717 return $groupedusers;
722 * User selector subclass for the list of users who are in a certain group.
723 * Used on the add group memebers page.
725 class group_members_selector extends groups_user_selector_base {
726 public function find_users($search) {
727 list($wherecondition, $params) = $this->search_sql($search, 'u');
728 $roles = groups_get_members_by_role($this->groupid, $this->courseid,
729 $this->required_fields_sql('u'), 'u.lastname, u.firstname',
730 $wherecondition, $params);
731 return $this->convert_array_format($roles, $search);
736 * User selector subclass for the list of users who are not in a certain group.
737 * Used on the add group members page.
739 class group_non_members_selector extends groups_user_selector_base {
740 const MAX_USERS_PER_PAGE = 100;
743 * An array of user ids populated by find_users() used in print_user_summaries()
745 private $potentialmembersids = array();
747 public function output_user($user) {
748 return parent::output_user($user) . ' (' . $user->numgroups . ')';
752 * Returns the user selector JavaScript module
755 public function get_js_module() {
756 return self::$jsmodule;
760 * Creates a global JS variable (userSummaries) that is used by the group selector
761 * to print related information when the user clicks on a user in the groups UI.
763 * Used by /group/clientlib.js
765 * @global moodle_database $DB
766 * @global moodle_page $PAGE
767 * @param int $courseid
769 public function print_user_summaries($courseid) {
772 $usersummaries = array();
774 // Get other groups user already belongs to
775 $usergroups = array();
776 $potentialmembersids = $this->potentialmembersids;
777 if( empty($potentialmembersids)==false ) {
778 list($membersidsclause, $params) = $DB->get_in_or_equal($potentialmembersids, SQL_PARAMS_NAMED, 'pm');
779 $sql = "SELECT u.id AS userid, g.*
781 JOIN {groups_members} gm ON u.id = gm.userid
782 JOIN {groups} g ON gm.groupid = g.id
783 WHERE u.id $membersidsclause AND g.courseid = :courseid ";
784 $params['courseid'] = $courseid;
785 $rs = $DB->get_recordset_sql($sql, $params);
786 foreach ($rs as $usergroup) {
787 $usergroups[$usergroup->userid][$usergroup->id] = $usergroup;
791 foreach ($potentialmembersids as $userid) {
792 if (isset($usergroups[$userid])) {
793 $usergrouplist = html_writer::start_tag('ul');
794 foreach ($usergroups[$userid] as $groupitem) {
795 $usergrouplist .= html_writer::tag('li', format_string($groupitem->name));
797 $usergrouplist .= html_writer::end_tag('ul');
801 $usersummaries[] = $usergrouplist;
805 $PAGE->requires->data_for_js('userSummaries', $usersummaries);
808 public function find_users($search) {
811 // Get list of allowed roles.
812 $context = get_context_instance(CONTEXT_COURSE, $this->courseid);
813 if ($validroleids = groups_get_possible_roles($context)) {
814 list($roleids, $roleparams) = $DB->get_in_or_equal($validroleids, SQL_PARAMS_NAMED, 'r');
817 $roleparams = array();
820 // Get the search condition.
821 list($searchcondition, $searchparams) = $this->search_sql($search, 'u');
824 list($enrolsql, $enrolparams) = get_enrolled_sql($context);
825 $fields = "SELECT r.id AS roleid, r.shortname AS roleshortname, r.name AS rolename, u.id AS userid,
826 " . $this->required_fields_sql('u') . ",
827 (SELECT count(igm.groupid)
828 FROM {groups_members} igm
829 JOIN {groups} ig ON igm.groupid = ig.id
830 WHERE igm.userid = u.id AND ig.courseid = :courseid) AS numgroups";
831 $sql = " FROM {user} u
832 JOIN ($enrolsql) e ON e.id = u.id
833 LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid " . get_related_contexts_string($context) . " AND ra.roleid $roleids)
834 LEFT JOIN {role} r ON r.id = ra.roleid
836 AND u.id NOT IN (SELECT userid
837 FROM {groups_members}
838 WHERE groupid = :groupid)
839 AND $searchcondition";
840 $orderby = "ORDER BY u.lastname, u.firstname";
842 $params = array_merge($searchparams, $roleparams, $enrolparams);
843 $params['courseid'] = $this->courseid;
844 $params['groupid'] = $this->groupid;
846 if (!$this->is_validating()) {
847 $potentialmemberscount = $DB->count_records_sql("SELECT COUNT(DISTINCT u.id) $sql", $params);
848 if ($potentialmemberscount > group_non_members_selector::MAX_USERS_PER_PAGE) {
849 return $this->too_many_results($search, $potentialmemberscount);
853 $rs = $DB->get_recordset_sql("$fields $sql $orderby", $params);
854 $roles = groups_calculate_role_people($rs, $context);
856 //don't hold onto user IDs if we're doing validation
857 if (empty($this->validatinguserids) ) {
859 foreach($roles as $k=>$v) {
861 foreach($v->users as $uid=>$userobject) {
862 $this->potentialmembersids[] = $uid;
869 return $this->convert_array_format($roles, $search);