]> git.mjollnir.org Git - moodle.git/commitdiff
user selection: MDL-17053 Improve the user selector used on the assign roles and...
authortjhunt <tjhunt>
Wed, 29 Oct 2008 05:09:18 +0000 (05:09 +0000)
committertjhunt <tjhunt>
Wed, 29 Oct 2008 05:09:18 +0000 (05:09 +0000)
user/selector/lib.php
user/selector/script.js
user/selector/test.php

index c202f971746b18b7ff05a9a702d12b38053af101..9160e636f2dbe20a1d84b6e1edb0f8c43a7fbbb0 100644 (file)
@@ -123,15 +123,9 @@ abstract class user_selector_base {
     public function display($return = false) {
         global $USER, $CFG;
 
-        // Get the list of requested users, and if there is only one, set a flag to autoselect it.
+        // Get the list of requested users.
         $search = optional_param($this->name . '_searchtext', '', PARAM_RAW);
         $groupedusers = $this->find_users($search);
-        $select = false;
-        if (empty($groupedusers) && empty($this->selected)) {
-            $groupedusers = array(get_string('nomatchingusers') => array());
-        } else if (count($groupedusers) == 1 && count(reset($groupedusers)) == 1) {
-            $select = true;
-        }
 
         // Output the select.
         $name = $this->name;
@@ -145,7 +139,7 @@ abstract class user_selector_base {
                 $multiselect . 'size="' . $this->rows . '">' . "\n";
 
         // Populate the select.
-        $output .= $this->output_options($groupedusers, $select);
+        $output .= $this->output_options($groupedusers);
 
         // Output the search controls.
         $output .= "</select>\n<div>\n";
@@ -221,6 +215,9 @@ abstract class user_selector_base {
      */
     public abstract function find_users($search);
 
+    /**
+     * @return array the options needed to recreate this user_selector.
+     */
     protected function get_options() {
         return array(
             'class' => get_class($this),
@@ -231,12 +228,21 @@ abstract class user_selector_base {
 
     // Inner workings ==========================================================
 
+    /**
+     * Get the list of users that were selected by doing optional_param then
+     * validating the result.
+     *
+     * @return array of user objects.
+     */
     protected function load_selected_users() {
         // See if we got anything.
         $userids = optional_param($this->name, array(), PARAM_INTEGER);
         if (empty($userids)) {
             return array();
         }
+        if (!$this->multiselect) {
+            $userids = array($userids);
+        }
 
         // If we did, use the find_users method to validate the ids.
         $this->validatinguserids = $userids;
@@ -248,9 +254,20 @@ abstract class user_selector_base {
         foreach ($groupedusers as $group) {
             $users += $group;
         }
+
+        // If we are only supposed to be selecting a single user, make sure we do.
+        if (!$this->multiselect && count($users) > 1) {
+            $users = array_slice($users, 0, 1);
+        }
+
         return $users;
     }
 
+    /**
+     * @param string $u the table alias for the user table in the query being
+     *      built. May be ''.
+     * @return string fragment of SQL to go in the select list of the query.
+     */
     protected function required_fields_sql($u) {
         // Raw list of fields.
         $fields = array('id', 'firstname', 'lastname');
@@ -265,6 +282,14 @@ abstract class user_selector_base {
         return implode(',', $fields);
     }
 
+    /**
+     * @param string $search the text to search for.
+     * @param string $u the table alias for the user table in the query being
+     *      built. May be ''.
+     * @return array an array with two elements, a fragment of SQL to go in the
+     *      where clause the query, and an array containing any required parameters.
+     *      this uses ? style placeholders.
+     */
     protected function search_sql($search, $u) {
         global $DB;
         $params = array();
@@ -314,16 +339,27 @@ abstract class user_selector_base {
      * This method should do the same as the JavaScript method
      * user_selector.prototype.handle_response.
      *
-     * @param unknown_type $groupedusers
-     * @param unknown_type $select
-     * @return unknown
+     * @param array $groupedusers an array, as returned by find_users.
+     * @return string HTML code.
      */
-    protected function output_options($groupedusers, $select) {
+    protected function output_options($groupedusers) {
         $output = '';
 
         // Ensure that the list of previously selected users is up to date.
         $this->get_selected_users();
 
+        // If $groupedusers is empty, make a 'no matching users' group. If there
+        // is only one selected user, set a flag to select them.
+        $select = false;
+        if (empty($groupedusers) && empty($this->selected)) {
+            $groupedusers = array(get_string('nomatchingusers') => array());
+        } else if (count($groupedusers) == 1 && count(reset($groupedusers)) == 1) {
+            $select = true;
+            if (!$this->multiselect) {
+                $this->selected = array();
+            }
+        }
+
         // Output each optgroup.
         foreach ($groupedusers as $groupname => $users) {
             $output .= $this->output_optgroup($groupname, $users, $select);
@@ -341,9 +377,17 @@ abstract class user_selector_base {
         return $output;
     }
 
+    /**
+     * Output one particular optgroup. Used by the preceding function output_options.
+     *
+     * @param string $groupname the label for this optgroup.
+     * @param array $users the users to put in this optgroup.
+     * @param boolean $select if true, select the users in this group.
+     * @return string HTML code.
+     */
     protected function output_optgroup($groupname, $users, $select) {
-        $output = '<optgroup label="' . s($groupname) . ' (' . count($users) . ')">' . "\n";
         if (!empty($users)) {
+            $output = '<optgroup label="' . s($groupname) . ' (' . count($users) . ')">' . "\n";
             foreach ($users as $user) {
                 if ($select || isset($this->selected[$user->id])) {
                     $selectattr = ' selected="selected"';
@@ -355,6 +399,7 @@ abstract class user_selector_base {
                         $this->output_user($user) . "</option>\n";
             }
         } else {
+            $output = '<optgroup label="' . s($groupname) . '">' . "\n";
             $output .= '<option disabled="disabled">&nbsp;</option>' . "\n";
         }
         $output .= "</optgroup>\n";
@@ -385,8 +430,9 @@ abstract class user_selector_base {
     }
 
     /**
-     * Enter description here...
+     * 
      *
+     * @return any HTML needed here.
      */
     protected function initialise_javascript() {
         global $USER;
@@ -408,12 +454,20 @@ abstract class user_selector_base {
     }
 }
 
+/**
+ * User selector subclass  for the list of potential users on the assign roles page.
+ *
+ */
 class role_assign_potential_user_selector extends user_selector_base {
     public function find_users($search) {
         return array(); // TODO
     }
 }
 
+/**
+ * User selector subclass for the list of users who already have the role in
+ * question on the assign roles page.
+ */
 class role_assign_current_user_selector extends user_selector_base {
     public function find_users($search) {
         return array(); // TODO
index 8fbeddde2c0a9f36e3c20390a2c2f5dfbd8f7933..be8822f7566482fc0535ad13542d9ef79c9ea33d 100644 (file)
@@ -290,6 +290,9 @@ user_selector.prototype.output_options = function(data) {
     // If there was only one option matching the search results, select it.
     if (this.onlyoption) {
         this.onlyoption.selected = true;
+        if (!this.listbox.multiple) {
+            this.selected = {};
+        }
     }
     this.onlyoption = null;
 
@@ -303,9 +306,16 @@ user_selector.prototype.output_options = function(data) {
         this.output_group(this.strprevselected, this.selected, true);
     }
     this.selected = null;
-
 }
 
+/**
+ * This method should do the same sort of thing as the PHP method
+ * user_selector_base::output_optgroup.
+ *
+ * @param String groupname the label for this optgroup.v
+ * @param Object users the users to put in this optgroup.
+ * @param Boolean select if true, select the users in this group.
+ */
 user_selector.prototype.output_group = function(groupname, users, select) {
     var optgroup = document.createElement('optgroup');
     optgroup.label = groupname;
@@ -327,13 +337,14 @@ user_selector.prototype.output_group = function(groupname, users, select) {
         }
         count++;
     }
-    if (count == 0) {
+    if (count > 0) {
+        optgroup.label += ' (' + count + ')';
+    } else {
         var option = document.createElement('option');
         option.disabled = 'disabled';
         option.appendChild(document.createTextNode('\u00A0'));
         optgroup.appendchild(option);
     }
-    optgroup.label += ' (' + count + ')';
     this.listbox.appendChild(optgroup);
 }
 
@@ -354,4 +365,5 @@ user_selector.prototype.output_user = function(user) {
     return output;
 }
 
+// Say that we want to be a source of custom events.
 YAHOO.lang.augmentProto(user_selector, YAHOO.util.EventProvider);
\ No newline at end of file
index 78e6fc1b57ab85ee5e72345e6265133687b8c720..c1e334136ba25be78138d870e13a4cccf78d152f 100644 (file)
@@ -46,6 +46,7 @@ require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
 print_header();
 
 $userselector = new test_user_selector('myuserselector');
+$userselector->set_multiselect(false);
 
 $users = $userselector->get_selected_users();
 if (!empty($users)) {