From deb39f43824e5a3b118dcc8f75220f533ed09c65 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Tue, 11 Nov 2008 02:26:03 +0000 Subject: [PATCH] user selector: MDL-16966 Nicer messages when there are too many users to show. Also, improved the comment that explains to subclassers what they have to do, and fixed get_selected_users, so if the number of selected users is greater than the max number of users to show, it still works. --- lang/en_utf8/moodle.php | 4 +++ user/selector/lib.php | 70 ++++++++++++++++++++++++++++++++++------- user/selector/script.js | 1 + 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index 125fbd868a..c6a9dc42c4 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -1191,6 +1191,8 @@ $string['phone'] = 'Phone'; $string['phone2'] = 'Mobile Phone'; $string['phpinfo'] = 'PHP info'; $string['pleaseclose'] = 'Please close this window now.'; +$string['pleasesearchmore'] = 'Please search some more'; +$string['pleaseusesearch'] = 'Please use the search'; $string['plugincheck'] = 'Plugins check'; $string['pluginchecknotice'] = 'The following tables show the modules, blocks and filters that have been detected in your current Moodle installation; They indicate which plugins are standard, and which are not. All non-standard plugins should be checked and upgraded to their most recent versions @@ -1512,6 +1514,8 @@ $string['todaylogs'] = 'Today\'s logs'; $string['toeveryone'] = 'to everyone'; $string['toomanybounces'] = 'That email address has had too many bounces. You must change it to continue.'; $string['toomanytoshow'] = 'There are too many users to show.'; +$string['toomanyuserstoshow'] = 'Too many users ($a) to show'; +$string['toomanyusersmatchsearch'] = 'Too many users ($a->count) match \'$a->search\''; $string['toonly'] = 'to $a only'; $string['top'] = 'Top'; $string['topic'] = 'Topic'; diff --git a/user/selector/lib.php b/user/selector/lib.php index 08982573db..ecf4d36e08 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -271,6 +271,19 @@ abstract class user_selector_base { * conditions that apply. The SQL for testing whether a user matches the * search string should be obtained by calling the search_sql method. * + * This method is used both when getting the list of choices to display to + * the user, and also when validating a list of users that was selected. + * + * When preparing a list of users to choose from ($this->is_validating() + * return false) you should probably have an maximum number of users you will + * return, and if more users than this match your search, you should instead + * return a message generated by the too_many_results() method. However, you + * should not do this when validating. + * + * If you are writing a new user_selector subclass, I strongly recommend you + * look at some of the subclasses later in this file. They should help you + * see exactly what you have to do. + * * @param string $search the search string. * @return array An array of arrays of users. The array keys of the outer * array should be the string names of optgroups. The keys of the inner @@ -296,6 +309,14 @@ abstract class user_selector_base { // Inner workings ========================================================== + /** + * @return boolean if true, we are validating a list of selected users, + * rather than preparing a list of uesrs to choose from. + */ + protected function is_validating() { + return !is_null($this->validatinguserids); + } + /** * Get the list of users that were selected by doing optional_param then * validating the result. @@ -420,6 +441,28 @@ abstract class user_selector_base { return array(implode(' AND ', $tests), $params); } + /** + * Used to generate a nice message when there are too many users to show. + * The message includes the number of users that currently match, and the + * text of the message depends on whether the search term is non-blank. + * + * @param string $search the search term, as passed in to the find users method. + * @param unknown_type $count the number of users that currently match. + * @return array in the right format to return from the find_users method. + */ + protected function too_many_results($search, $count) { + if ($search) { + $a = new stdClass; + $a->count = $count; + $a->search = $search; + return array(get_string('toomanyusersmatchsearch', '', $a) => array(), + get_string('pleasesearchmore') => array()); + } else { + return array(get_string('toomanyuserstoshow', '', $count) => array(), + get_string('pleaseusesearch') => array()); + } + } + /** * Output the list of s and s that go inside the select. * This method should do the same as the JavaScript method @@ -681,10 +724,11 @@ class potential_assignees_below_course extends role_assign_user_selector_base { $params[] = $this->roleid; // Check to see if there are too many to show sensibly. - $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); - if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) { - return array(get_string('toomanytoshow') => array(), - get_string('trysearching') => array()); + if (!$this->is_validating()) { + $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); + if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) { + return $this->too_many_results($search, $potentialmemberscount); + } } // If not, show them. @@ -731,10 +775,11 @@ class potential_assignees_course_and_above extends role_assign_user_selector_bas $params[] = $this->context->id; $params[] = $this->roleid; - $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); - if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) { - return array(get_string('toomanytoshow') => array(), - get_string('trysearching') => array()); + if (!$this->is_validating()) { + $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); + if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) { + return $this->too_many_results($search, $potentialmemberscount); + } } $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); @@ -925,11 +970,12 @@ class group_non_members_selector extends groups_user_selector_base { $orderby = " ORDER BY u.lastname, u.firstname"; $params = array_merge($roleparams, array($this->groupid), $searchparams); - $potentialmemberscount = $DB->count_records_sql('SELECT count(DISTINCT u.id) ' . $sql, $params); - if ($potentialmemberscount > group_non_members_selector::MAX_USERS_PER_PAGE) { - return array(get_string('toomanytoshow') => array(), - get_string('trysearching') => array()); + if (!$this->is_validating()) { + $potentialmemberscount = $DB->count_records_sql('SELECT count(DISTINCT u.id) ' . $sql, $params); + if ($potentialmemberscount > group_non_members_selector::MAX_USERS_PER_PAGE) { + return $this->too_many_results($search, $potentialmemberscount); + } } array_unshift($params, $this->courseid); diff --git a/user/selector/script.js b/user/selector/script.js index b83fde31c3..0e8a292741 100644 --- a/user/selector/script.js +++ b/user/selector/script.js @@ -428,6 +428,7 @@ user_selector.prototype.output_options = function(data) { if (!this.listbox.multiple) { this.selected = {}; } + this.handle_selection_change(); } this.onlyoption = null; -- 2.39.5