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;
$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";
*/
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),
// 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;
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');
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();
* 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);
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"';
$this->output_user($user) . "</option>\n";
}
} else {
+ $output = '<optgroup label="' . s($groupname) . '">' . "\n";
$output .= '<option disabled="disabled"> </option>' . "\n";
}
$output .= "</optgroup>\n";
}
/**
- * Enter description here...
+ *
*
+ * @return any HTML needed here.
*/
protected function initialise_javascript() {
global $USER;
}
}
+/**
+ * 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
// 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;
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;
}
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);
}
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