Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
|
Base class for user selectors. More...
Public Member Functions | |
__construct ($name, $options=array()) | |
Constructor. | |
clear_exclusions () | |
Clear the list of excluded user ids. | |
display ($return=false) | |
Output this user_selector as HTML. | |
exclude ($arrayofuserids) | |
All to the list of user ids that this control will not select. | |
find_users ($search) | |
Search the database for users matching the $search string, and any other conditions that apply. | |
get_exclusions () | |
Returns the list of user ids that this control will not select. | |
get_name () | |
Returns the id/name of this control. | |
get_rows () | |
Returns the number of rows to display in this control. | |
get_selected_user () | |
Convenience method for when multiselect is false (throws an exception if not). | |
get_selected_users () | |
The users that were selected. | |
invalidate_selected_users () | |
Invalidates the list of selected users. | |
is_multiselect () | |
Returns true is multiselect should be allowed. | |
output_user ($user) | |
Convert a user object to a string suitable for displaying as an option in the list box. | |
set_extra_fields ($fields) | |
Set the user fields that are displayed in the selector in addition to the user's name. | |
set_multiselect ($multiselect) | |
Whether this control will allow selection of many, or just one user. | |
set_rows ($numrows) | |
The height this control will be displayed, in rows. | |
Public Attributes | |
int | $maxusersperpage = 100 |
this is used to define maximum number of users visible in list | |
boolean | $viewfullnames = false |
Whether to override fullname() | |
Protected Member Functions | |
get_options () | |
Note: this function must be implemented if you use the search ajax field (e.g. | |
initialise_javascript ($search) | |
Initialises JS for this control. | |
is_validating () | |
Returns true if this control is validating a list of users. | |
load_selected_users () | |
Get the list of users that were selected by doing optional_param then validating the result. | |
output_optgroup ($groupname, $users, $select) | |
Output one particular optgroup. | |
output_options ($groupedusers, $search) | |
Output the list of <optgroup>s and <options>s that go inside the select. | |
required_fields_sql (string $u) | |
Returns SQL to select required fields. | |
search_button_caption () | |
Returns the string to use for the search button caption. | |
search_sql (string $search, string $u) | |
Returns an array with SQL to perform a search and the params that go into it. | |
too_many_results ($search, $count) | |
Used to generate a nice message when there are too many users to show. | |
Protected Attributes | |
object | $accesscontext |
Context used for capability checks regarding this selector (does not necessarily restrict user list) | |
boolean | $autoselectunique = false |
If only one user matches the search, should we select them automatically. | |
array | $exclude = array() |
A list of userids that should not be returned by this control. | |
array | $extrafields |
Extra fields to search on and return in addition to firstname and lastname. | |
boolean | $includecustomfields = false |
Whether to include custom user profile fields. | |
boolean | $multiselect = true |
Whether the conrol should allow selection of many users, or just one. | |
string | $name |
The control name (and id) in the HTML. | |
boolean | $preserveselected = false |
When the search changes, do we keep previously selected options that do not match the new search term? | |
int | $rows = USER_SELECTOR_DEFAULT_ROWS |
The height this control should have, in rows. | |
int | $searchtype = USER_SEARCH_STARTS_WITH |
When searching, do we only match the starts of fields (better performance) or do we match occurrences anywhere or do we match exact the fields. | |
array null | $selected = null |
A list of the users who are selected. | |
string | $userfieldsjoin = '' |
User fields join for custom fields. | |
array | $userfieldsmappings = [] |
User fields mappings for custom fields. | |
array | $userfieldsparams = [] |
User fields params for custom fields. | |
string | $userfieldsselects = '' |
User fields selects for custom fields. | |
mixed | $validatinguserids = null |
This is used by get selected users. | |
Static Protected Attributes | |
static array | $jsmodule |
JavaScript YUI3 Module definition. | |
Base class for user selectors.
In your theme, you must give each user-selector a defined width. If the user selector has name="myid", then the div myid_wrapper must have a width specified.
user_selector_base::__construct | ( | $name, | |
$options = array() ) |
Constructor.
Each subclass must have a constructor with this signature.
string | $name | the control name/id for use in the HTML. |
array | $options | other options needed to construct this selector. You must be able to clone a userselector by doing new get_class($us)($us->get_name(), $us->get_options()); |
Reimplemented in cohort_candidate_selector, cohort_existing_selector, core_role_admins_existing_selector, core_role_admins_potential_selector, core_role_assign_user_selector_base, core_role_check_users_selector, enrol_manual_current_participant, enrol_manual_potential_participant, groups_user_selector_base, mnetservice_enrol_existing_users_selector, mnetservice_enrol_potential_users_selector, mod_forum_potential_subscriber_selector, mod_forum_subscriber_selector_base, and service_user_selector.
user_selector_base::display | ( | $return = false | ) |
Output this user_selector as HTML.
boolean | $return | if true, return the HTML as a string instead of outputting it. |
mixed | if $return is true, returns the HTML as a string, otherwise returns nothing. |
user_selector_base::exclude | ( | $arrayofuserids | ) |
All to the list of user ids that this control will not select.
For example, on the role assign page, we do not list the users who already have the role in question.
array | $arrayofuserids | the user ids to exclude. |
|
abstract |
Search the database for users matching the $search string, and any other 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 and in admin/roles/lib.php. They should help you see exactly what you have to do.
string | $search | the search string. |
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 arrays should be userids, and the values should be user objects containing at least the list of fields returned by the method required_fields_sql(). If a user object has a ->disabled property that is true, then that option will be displayed greyed out, and will not be returned by get_selected_users. |
Reimplemented in badge_existing_users_selector, badge_potential_users_selector, cohort_candidate_selector, cohort_existing_selector, core_role_admins_existing_selector, core_role_admins_potential_selector, core_role_check_users_selector, core_role_existing_role_holders, core_role_potential_assignees_below_course, core_role_potential_assignees_course_and_above, enrol_manual_current_participant, enrol_manual_potential_participant, group_members_selector, group_non_members_selector, mnetservice_enrol_existing_users_selector, mnetservice_enrol_potential_users_selector, mod_forum_existing_subscriber_selector, mod_forum_potential_subscriber_selector, and service_user_selector.
user_selector_base::get_exclusions | ( | ) |
Returns the list of user ids that this control will not select.
array | the list of user ids that this control will not select. |
user_selector_base::get_name | ( | ) |
Returns the id/name of this control.
string | the id/name that this control will have in the HTML. |
|
protected |
Note: this function must be implemented if you use the search ajax field (e.g.
set $options['file'] = '/admin/filecontainingyourclass.php';)
array | the options needed to recreate this user_selector. |
Reimplemented in badge_award_selector_base, cohort_candidate_selector, cohort_existing_selector, core_role_admins_existing_selector, core_role_admins_potential_selector, core_role_assign_user_selector_base, core_role_check_users_selector, enrol_manual_current_participant, enrol_manual_potential_participant, groups_user_selector_base, mnetservice_enrol_existing_users_selector, mnetservice_enrol_potential_users_selector, mod_forum_potential_subscriber_selector, mod_forum_subscriber_selector_base, and service_user_selector.
user_selector_base::get_rows | ( | ) |
Returns the number of rows to display in this control.
integer | the height this control will be displayed, in rows. |
user_selector_base::get_selected_user | ( | ) |
Convenience method for when multiselect is false (throws an exception if not).
moodle_exception |
object | the selected user object, or null if none. |
user_selector_base::get_selected_users | ( | ) |
The users that were selected.
This is a more sophisticated version of optional_param($this->name, array(), PARAM_INT) that validates the returned list of ids against the rules for this user selector.
array | of user objects. |
|
protected |
Initialises JS for this control.
string | $search |
string | any HTML needed here. |
user_selector_base::invalidate_selected_users | ( | ) |
Invalidates the list of selected users.
If you update the database in such a way that it is likely to change the list of users that this component is allowed to select from, then you must call this method. For example, on the role assign page, after you have assigned some roles to some users, you should call this.
user_selector_base::is_multiselect | ( | ) |
Returns true is multiselect should be allowed.
boolean | whether this control will allow selection of more than one user. |
|
protected |
Returns true if this control is validating a list of users.
boolean | if true, we are validating a list of selected users, rather than preparing a list of uesrs to choose from. |
|
protected |
Get the list of users that were selected by doing optional_param then validating the result.
array | of user objects. |
|
protected |
Output one particular optgroup.
Used by the preceding function output_options.
string | $groupname | the label for this optgroup. |
array | $users | the users to put in this optgroup. |
boolean | $select | if true, select the users in this group. |
string | HTML code. |
|
protected |
Output the list of <optgroup>s and <options>s that go inside the select.
This method should do the same as the JavaScript method user_selector.prototype.handle_response.
array | $groupedusers | an array, as returned by find_users. |
string | $search |
string | HTML code. |
user_selector_base::output_user | ( | $user | ) |
Convert a user object to a string suitable for displaying as an option in the list box.
object | $user | the user to display. |
string | a string representation of the user. |
Reimplemented in group_non_members_selector.
|
protected |
Returns SQL to select required fields.
string | $u | the table alias for the user table in the query being built. May be ''. |
string | fragment of SQL to go in the select list of the query. |
coding_exception | if used when includecustomfields is true |
|
protected |
Returns the string to use for the search button caption.
string | the caption for the search button. |
|
protected |
Returns an array with SQL to perform a search and the params that go into it.
string | $search | the text to search for. |
string | $u | the table alias for the user table in the query being built. May be ''. |
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. |
user_selector_base::set_extra_fields | ( | $fields | ) |
Set the user fields that are displayed in the selector in addition to the user's name.
array | $fields | a list of field names that exist in the user table. |
user_selector_base::set_multiselect | ( | $multiselect | ) |
Whether this control will allow selection of many, or just one user.
boolean | $multiselect | true = allow multiple selection. |
user_selector_base::set_rows | ( | $numrows | ) |
The height this control will be displayed, in rows.
integer | $numrows | the desired height. |
|
protected |
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.
string | $search | the search term, as passed in to the find users method. |
int | $count | the number of users that currently match. |
array | in the right format to return from the find_users method. |
|
staticprotected |
JavaScript YUI3 Module definition.