]> git.mjollnir.org Git - moodle.git/commitdiff
user selector: MDL-16966 Nicer messages when there are too many users to show.
authortjhunt <tjhunt>
Tue, 11 Nov 2008 02:26:03 +0000 (02:26 +0000)
committertjhunt <tjhunt>
Tue, 11 Nov 2008 02:26:03 +0000 (02:26 +0000)
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
user/selector/lib.php
user/selector/script.js

index 125fbd868a1789fb28f7af5e4ca0f073858c43fd..c6a9dc42c479178b53e1250713b29a5bf702a5fd 100644 (file)
@@ -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 <b>must</b> 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';
index 08982573db80dcec47d83c265959209569faabc1..ecf4d36e0846254bb05b8947eaf8d3f02e234508 100644 (file)
@@ -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 <optgroup>s and <options>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);
index b83fde31c3ba23ac0b9d0080a203ef707b77ae08..0e8a292741062613059e29518bfc408c7c4b7313 100644 (file)
@@ -428,6 +428,7 @@ user_selector.prototype.output_options = function(data) {
         if (!this.listbox.multiple) {
             this.selected = {};
         }
+        this.handle_selection_change();
     }
     this.onlyoption = null;