MDL-8249 localise standard role names and descriptions if empty
[moodle.git] / admin / roles / lib.php
blob212cd211c6fda27e1f8bde15de53b76e8638a392
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Library code used by the roles administration interfaces.
21 * Responds to actions:
22 * add - add a new role
23 * duplicate - like add, only initialise the new role by using an existing one.
24 * edit - edit the definition of a role
25 * view - view the definition of a role
27 * @package core
28 * @subpackage role
29 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 require_once($CFG->libdir.'/adminlib.php');
34 require_once($CFG->dirroot.'/user/selector/lib.php');
36 // Classes for producing tables with one row per capability ====================
38 /**
39 * This class represents a table with one row for each of a list of capabilities
40 * where the first cell in the row contains the capability name, and there is
41 * arbitrary stuff in the rest of the row. This class is used by
42 * admin/roles/manage.php, override.php and check.php.
44 * An ajaxy search UI shown at the top, if JavaScript is on.
46 abstract class capability_table_base {
47 /** The context this table relates to. */
48 protected $context;
50 /** The capabilities to display. Initialised as fetch_context_capabilities($context). */
51 protected $capabilities = array();
53 /** Added as an id="" attribute to the table on output. */
54 protected $id;
56 /** Added to the class="" attribute on output. */
57 protected $classes = array('rolecap');
59 /** Default number of capabilities in the table for the search UI to be shown. */
60 const NUM_CAPS_FOR_SEARCH = 12;
62 /**
63 * Constructor
64 * @param object $context the context this table relates to.
65 * @param string $id what to put in the id="" attribute.
67 public function __construct($context, $id) {
68 $this->context = $context;
69 $this->capabilities = fetch_context_capabilities($context);
70 $this->id = $id;
73 /**
74 * Use this to add class="" attributes to the table. You get the rolecap by
75 * default.
76 * @param array $classnames of class names.
78 public function add_classes($classnames) {
79 $this->classes = array_unique(array_merge($this->classes, $classnames));
82 /**
83 * Display the table.
85 public function display() {
86 if (count($this->capabilities) > capability_table_base::NUM_CAPS_FOR_SEARCH) {
87 global $PAGE;
88 $PAGE->requires->strings_for_js(array('filter','clear'),'moodle');
89 $PAGE->requires->js_init_call('M.core_role.init_cap_table_filter', array($this->id, $this->context->id));
91 echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n";
92 echo '<tr><th class="name" align="left" scope="col">' . get_string('capability','role') . '</th>';
93 $this->add_header_cells();
94 echo "</tr>\n</thead>\n<tbody>\n";
96 /// Loop over capabilities.
97 $contextlevel = 0;
98 $component = '';
99 foreach ($this->capabilities as $capability) {
100 if ($this->skip_row($capability)) {
101 continue;
104 /// Prints a breaker if component or name or context level has changed
105 if (component_level_changed($capability, $component, $contextlevel)) {
106 $this->print_heading_row($capability);
108 $contextlevel = $capability->contextlevel;
109 $component = $capability->component;
111 /// Start the row.
112 echo '<tr class="' . implode(' ', array_unique(array_merge(array('rolecap'),
113 $this->get_row_classes($capability)))) . '">';
115 /// Table cell for the capability name.
116 echo '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
117 '<span class="cap-name">' . $capability->name . '</span></span></th>';
119 /// Add the cells specific to this table.
120 $this->add_row_cells($capability);
122 /// End the row.
123 echo "</tr>\n";
126 /// End of the table.
127 echo "</tbody>\n</table>\n";
131 * Used to output a heading rows when the context level or component changes.
132 * @param object $capability gives the new component and contextlevel.
134 protected function print_heading_row($capability) {
135 echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
136 get_component_string($capability->component, $capability->contextlevel) .
137 '</strong></td></tr>';
141 /** For subclasses to override, output header cells, after the initial capability one. */
142 protected abstract function add_header_cells();
144 /** For subclasses to override, return the number of cells that add_header_cells/add_row_cells output. */
145 protected abstract function num_extra_columns();
148 * For subclasses to override. Allows certain capabilties
149 * to be left out of the table.
151 * @param object $capability the capability this row relates to.
152 * @return boolean. If true, this row is omitted from the table.
154 protected function skip_row($capability) {
155 return false;
159 * For subclasses to override. A change to reaturn class names that are added
160 * to the class="" attribute on the &lt;tr> for this capability.
162 * @param object $capability the capability this row relates to.
163 * @return array of class name strings.
165 protected function get_row_classes($capability) {
166 return array();
170 * For subclasses to override. Output the data cells for this capability. The
171 * capability name cell will already have been output.
173 * You can rely on get_row_classes always being called before add_row_cells.
175 * @param object $capability the capability this row relates to.
177 protected abstract function add_row_cells($capability);
181 * Subclass of capability_table_base for use on the Check permissions page.
183 * We have one additional column, Allowed, which contains yes/no.
185 class check_capability_table extends capability_table_base {
186 protected $user;
187 protected $fullname;
188 protected $contextname;
189 protected $stryes;
190 protected $strno;
191 private $hascap;
194 * Constructor
195 * @param object $context the context this table relates to.
196 * @param object $user the user we are generating the results for.
197 * @param string $contextname print_context_name($context) - to save recomputing.
199 public function __construct($context, $user, $contextname) {
200 global $CFG;
201 parent::__construct($context, 'explaincaps');
202 $this->user = $user;
203 $this->fullname = fullname($user);
204 $this->contextname = $contextname;
205 $this->stryes = get_string('yes');
206 $this->strno = get_string('no');
209 protected function add_header_cells() {
210 echo '<th>' . get_string('allowed', 'role') . '</th>';
213 protected function num_extra_columns() {
214 return 1;
217 protected function get_row_classes($capability) {
218 $this->hascap = has_capability($capability->name, $this->context, $this->user->id);
219 if ($this->hascap) {
220 return array('yes');
221 } else {
222 return array('no');
226 protected function add_row_cells($capability) {
227 global $OUTPUT;
228 if ($this->hascap) {
229 $result = $this->stryes;
230 } else {
231 $result = $this->strno;
233 $a = new stdClass;
234 $a->fullname = $this->fullname;
235 $a->capability = $capability->name;
236 $a->context = $this->contextname;
237 echo '<td>' . $result . '</td>';
243 * Subclass of capability_table_base for use on the Permissions page.
245 class permissions_table extends capability_table_base {
246 protected $contextname;
247 protected $allowoverrides;
248 protected $allowsafeoverrides;
249 protected $overridableroles;
250 protected $roles;
251 protected $icons = array();
254 * Constructor
255 * @param object $context the context this table relates to.
256 * @param string $contextname print_context_name($context) - to save recomputing.
258 public function __construct($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles) {
259 parent::__construct($context, 'permissions');
260 $this->contextname = $contextname;
261 $this->allowoverrides = $allowoverrides;
262 $this->allowsafeoverrides = $allowsafeoverrides;
263 $this->overridableroles = $overridableroles;
265 $roles = get_all_roles($context);
266 $this->roles = role_fix_names(array_reverse($roles, true), $context, ROLENAME_ALIAS, true);
270 protected function add_header_cells() {
271 echo '<th>' . get_string('risks', 'role') . '</th>';
272 echo '<th>' . get_string('neededroles', 'role') . '</th>';
273 echo '<th>' . get_string('prohibitedroles', 'role') . '</th>';
276 protected function num_extra_columns() {
277 return 3;
280 protected function add_row_cells($capability) {
281 global $OUTPUT, $PAGE;
283 $context = $this->context;
284 $contextid = $this->context->id;
285 $allowoverrides = $this->allowoverrides;
286 $allowsafeoverrides = $this->allowsafeoverrides;
287 $overridableroles = $this->overridableroles;
288 $roles = $this->roles;
291 list($needed, $forbidden) = get_roles_with_cap_in_context($context, $capability->name);
292 $neededroles = array();
293 $forbiddenroles = array();
294 $allowable = $overridableroles;
295 $forbitable = $overridableroles;
296 foreach ($neededroles as $id=>$unused) {
297 unset($allowable[$id]);
299 foreach ($forbidden as $id=>$unused) {
300 unset($allowable[$id]);
301 unset($forbitable[$id]);
304 foreach ($roles as $id=>$name) {
305 if (isset($needed[$id])) {
306 $neededroles[$id] = $roles[$id];
307 if (isset($overridableroles[$id]) and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
308 $preventurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'prevent'=>1));
309 $neededroles[$id] .= $OUTPUT->action_icon($preventurl, new pix_icon('t/delete', get_string('prevent', 'role')));
313 $neededroles = implode(', ', $neededroles);
314 foreach ($roles as $id=>$name) {
315 if (isset($forbidden[$id]) and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
316 $forbiddenroles[$id] = $roles[$id];
317 if (isset($overridableroles[$id]) and prohibit_is_removable($id, $context, $capability->name)) {
318 $unprohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'unprohibit'=>1));
319 $forbiddenroles[$id] .= $OUTPUT->action_icon($unprohibiturl, new pix_icon('t/delete', get_string('delete')));
323 $forbiddenroles = implode(', ', $forbiddenroles);
325 if ($allowable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
326 $allowurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'allow'=>1));
327 $neededroles .= '<div class="allowmore">'.$OUTPUT->action_icon($allowurl, new pix_icon('t/add', get_string('allow', 'role'))).'</div>';
330 if ($forbitable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
331 $prohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'prohibit'=>1));
332 $forbiddenroles .= '<div class="prohibitmore">'.$OUTPUT->action_icon($prohibiturl, new pix_icon('t/add', get_string('prohibit', 'role'))).'</div>';
335 $risks = $this->get_risks($capability);
337 echo '<td>' . $risks . '</td>';
338 echo '<td>' . $neededroles . '</td>';
339 echo '<td>' . $forbiddenroles . '</td>';
342 protected function get_risks($capability) {
343 global $OUTPUT;
345 $allrisks = get_all_risks();
346 $risksurl = new moodle_url(get_docs_url(s(get_string('risks', 'role'))));
348 $return = '';
350 foreach ($allrisks as $type=>$risk) {
351 if ($risk & (int)$capability->riskbitmask) {
352 if (!isset($this->icons[$type])) {
353 $pixicon = new pix_icon('/i/' . str_replace('risk', 'risk_', $type), get_string($type . 'short', 'admin'));
354 $this->icons[$type] = $OUTPUT->action_icon($risksurl, $pixicon, new popup_action('click', $risksurl));
356 $return .= $this->icons[$type];
360 return $return;
366 * This subclass is the bases for both the define roles and override roles
367 * pages. As well as adding the risks columns, this also provides generic
368 * facilities for showing a certain number of permissions columns, and
369 * recording the current and submitted permissions for each capability.
371 abstract class capability_table_with_risks extends capability_table_base {
372 protected $allrisks;
373 protected $allpermissions; // We don't need perms ourselves, but all our subclasses do.
374 protected $strperms; // Language string cache.
375 protected $risksurl; // URL in moodledocs about risks.
376 protected $riskicons = array(); // Cache to avoid regenerating the HTML for each risk icon.
377 /** The capabilities to highlight as default/inherited. */
378 protected $parentpermissions;
379 protected $displaypermissions;
380 protected $permissions;
381 protected $changed;
382 protected $roleid;
384 public function __construct($context, $id, $roleid) {
385 parent::__construct($context, $id);
387 $this->allrisks = get_all_risks();
388 $this->risksurl = get_docs_url(s(get_string('risks', 'role')));
390 $this->allpermissions = array(
391 CAP_INHERIT => 'inherit',
392 CAP_ALLOW => 'allow',
393 CAP_PREVENT => 'prevent' ,
394 CAP_PROHIBIT => 'prohibit',
397 $this->strperms = array();
398 foreach ($this->allpermissions as $permname) {
399 $this->strperms[$permname] = get_string($permname, 'role');
402 $this->roleid = $roleid;
403 $this->load_current_permissions();
405 /// Fill in any blank permissions with an explicit CAP_INHERIT, and init a locked field.
406 foreach ($this->capabilities as $capid => $cap) {
407 if (!isset($this->permissions[$cap->name])) {
408 $this->permissions[$cap->name] = CAP_INHERIT;
410 $this->capabilities[$capid]->locked = false;
414 protected function load_current_permissions() {
415 global $DB;
417 /// Load the overrides/definition in this context.
418 if ($this->roleid) {
419 $this->permissions = $DB->get_records_menu('role_capabilities', array('roleid' => $this->roleid,
420 'contextid' => $this->context->id), '', 'capability,permission');
421 } else {
422 $this->permissions = array();
426 protected abstract function load_parent_permissions();
429 * Update $this->permissions based on submitted data, while making a list of
430 * changed capabilities in $this->changed.
432 public function read_submitted_permissions() {
433 $this->changed = array();
435 foreach ($this->capabilities as $cap) {
436 if ($cap->locked || $this->skip_row($cap)) {
437 /// The user is not allowed to change the permission for this capability
438 continue;
441 $permission = optional_param($cap->name, null, PARAM_PERMISSION);
442 if (is_null($permission)) {
443 /// A permission was not specified in submitted data.
444 continue;
447 /// If the permission has changed, update $this->permissions and
448 /// Record the fact there is data to save.
449 if ($this->permissions[$cap->name] != $permission) {
450 $this->permissions[$cap->name] = $permission;
451 $this->changed[] = $cap->name;
457 * Save the new values of any permissions that have been changed.
459 public function save_changes() {
460 /// Set the permissions.
461 foreach ($this->changed as $changedcap) {
462 assign_capability($changedcap, $this->permissions[$changedcap],
463 $this->roleid, $this->context->id, true);
466 /// Force accessinfo refresh for users visiting this context.
467 mark_context_dirty($this->context->path);
470 public function display() {
471 $this->load_parent_permissions();
472 foreach ($this->capabilities as $cap) {
473 if (!isset($this->parentpermissions[$cap->name])) {
474 $this->parentpermissions[$cap->name] = CAP_INHERIT;
477 parent::display();
480 protected function add_header_cells() {
481 global $OUTPUT;
482 echo '<th colspan="' . count($this->displaypermissions) . '" scope="col">' .
483 get_string('permission', 'role') . ' ' . $OUTPUT->help_icon('permission', 'role') . '</th>';
484 echo '<th class="risk" colspan="' . count($this->allrisks) . '" scope="col">' . get_string('risks','role') . '</th>';
487 protected function num_extra_columns() {
488 return count($this->displaypermissions) + count($this->allrisks);
491 protected function get_row_classes($capability) {
492 $rowclasses = array();
493 foreach ($this->allrisks as $riskname => $risk) {
494 if ($risk & (int)$capability->riskbitmask) {
495 $rowclasses[] = $riskname;
498 return $rowclasses;
501 protected abstract function add_permission_cells($capability);
503 protected function add_row_cells($capability) {
504 $this->add_permission_cells($capability);
505 /// One cell for each possible risk.
506 foreach ($this->allrisks as $riskname => $risk) {
507 echo '<td class="risk ' . str_replace('risk', '', $riskname) . '">';
508 if ($risk & (int)$capability->riskbitmask) {
509 echo $this->get_risk_icon($riskname);
511 echo '</td>';
516 * Print a risk icon, as a link to the Risks page on Moodle Docs.
518 * @param string $type the type of risk, will be one of the keys from the
519 * get_all_risks array. Must start with 'risk'.
521 function get_risk_icon($type) {
522 global $OUTPUT;
523 if (!isset($this->riskicons[$type])) {
524 $iconurl = $OUTPUT->pix_url('i/' . str_replace('risk', 'risk_', $type));
525 $text = '<img src="' . $iconurl . '" alt="' . get_string($type . 'short', 'admin') . '" />';
526 $action = new popup_action('click', $this->risksurl, 'docspopup');
527 $this->riskicons[$type] = $OUTPUT->action_link($this->risksurl, $text, $action, array('title'=>get_string($type, 'admin')));
529 return $this->riskicons[$type];
534 * As well as tracking the permissions information about the role we are creating
535 * or editing, we also track the other information about the role. (This class is
536 * starting to be more and more like a formslib form in some respects.)
538 class define_role_table_advanced extends capability_table_with_risks {
539 /** Used to store other information (besides permissions) about the role we are creating/editing. */
540 protected $role;
541 /** Used to store errors found when validating the data. */
542 protected $errors;
543 protected $contextlevels;
544 protected $allcontextlevels;
545 protected $disabled = '';
547 public function __construct($context, $roleid) {
548 $this->roleid = $roleid;
549 parent::__construct($context, 'defineroletable', $roleid);
550 $this->displaypermissions = $this->allpermissions;
551 $this->strperms[$this->allpermissions[CAP_INHERIT]] = get_string('notset', 'role');
553 $this->allcontextlevels = array(
554 CONTEXT_SYSTEM => get_string('coresystem'),
555 CONTEXT_USER => get_string('user'),
556 CONTEXT_COURSECAT => get_string('category'),
557 CONTEXT_COURSE => get_string('course'),
558 CONTEXT_MODULE => get_string('activitymodule'),
559 CONTEXT_BLOCK => get_string('block')
563 protected function load_current_permissions() {
564 global $DB;
565 if ($this->roleid) {
566 if (!$this->role = $DB->get_record('role', array('id' => $this->roleid))) {
567 throw new moodle_exception('invalidroleid');
569 $contextlevels = get_role_contextlevels($this->roleid);
570 // Put the contextlevels in the array keys, as well as the values.
571 if (!empty($contextlevels)) {
572 $this->contextlevels = array_combine($contextlevels, $contextlevels);
573 } else {
574 $this->contextlevels = array();
576 } else {
577 $this->role = new stdClass;
578 $this->role->name = '';
579 $this->role->shortname = '';
580 $this->role->description = '';
581 $this->role->archetype = '';
582 $this->contextlevels = array();
584 parent::load_current_permissions();
587 public function read_submitted_permissions() {
588 global $DB;
589 $this->errors = array();
591 // Role short name. We clean this in a special way. We want to end up
592 // with only lowercase safe ASCII characters.
593 $shortname = optional_param('shortname', null, PARAM_RAW);
594 if (!is_null($shortname)) {
595 $this->role->shortname = $shortname;
596 $this->role->shortname = textlib::specialtoascii($this->role->shortname);
597 $this->role->shortname = textlib::strtolower(clean_param($this->role->shortname, PARAM_ALPHANUMEXT));
598 if (empty($this->role->shortname)) {
599 $this->errors['shortname'] = get_string('errorbadroleshortname', 'role');
602 if ($DB->record_exists_select('role', 'shortname = ? and id <> ?', array($this->role->shortname, $this->roleid))) {
603 $this->errors['shortname'] = get_string('errorexistsroleshortname', 'role');
606 // Role name.
607 $name = optional_param('name', null, PARAM_MULTILANG);
608 if (!is_null($name)) {
609 $this->role->name = $name;
610 // Hack: short names of standard roles are equal to archetypes, empty name means localised via lang packs.
611 $archetypes = get_role_archetypes();
612 if (!isset($archetypes[$shortname]) and html_is_blank($this->role->name)) {
613 $this->errors['name'] = get_string('errorbadrolename', 'role');
616 if ($this->role->name !== '' and $DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) {
617 $this->errors['name'] = get_string('errorexistsrolename', 'role');
620 // Description.
621 $description = optional_param('description', null, PARAM_RAW);
622 if (!is_null($description)) {
623 $this->role->description = $description;
626 // Legacy type.
627 $archetype = optional_param('archetype', null, PARAM_RAW);
628 if (isset($archetype)) {
629 $archetypes = get_role_archetypes();
630 if (isset($archetypes[$archetype])){
631 $this->role->archetype = $archetype;
632 } else {
633 $this->role->archetype = '';
637 // Assignable context levels.
638 foreach ($this->allcontextlevels as $cl => $notused) {
639 $assignable = optional_param('contextlevel' . $cl, null, PARAM_BOOL);
640 if (!is_null($assignable)) {
641 if ($assignable) {
642 $this->contextlevels[$cl] = $cl;
643 } else {
644 unset($this->contextlevels[$cl]);
649 // Now read the permissions for each capability.
650 parent::read_submitted_permissions();
653 public function is_submission_valid() {
654 return empty($this->errors);
658 * Call this after the table has been initialised, so to indicate that
659 * when save is called, we want to make a duplicate role.
661 public function make_copy() {
662 $this->roleid = 0;
663 unset($this->role->id);
664 $this->role->name .= ' ' . get_string('copyasnoun');
665 $this->role->shortname .= 'copy';
668 public function get_role_name() {
669 return $this->role->name;
672 public function get_role_id() {
673 return $this->role->id;
676 public function get_archetype() {
677 return $this->role->archetype;
680 protected function load_parent_permissions() {
681 $this->parentpermissions = get_default_capabilities($this->role->archetype);
684 public function save_changes() {
685 global $DB;
687 if (!$this->roleid) {
688 // Creating role
689 $this->role->id = create_role($this->role->name, $this->role->shortname, $this->role->description, $this->role->archetype);
690 $this->roleid = $this->role->id; // Needed to make the parent::save_changes(); call work.
691 } else {
692 // Updating role
693 $DB->update_record('role', $this->role);
696 // Assignable contexts.
697 set_role_contextlevels($this->role->id, $this->contextlevels);
699 // Permissions.
700 parent::save_changes();
703 protected function get_name_field($id) {
704 return '<input type="text" id="' . $id . '" name="' . $id . '" maxlength="254" value="' . s($this->role->name) . '" />';
707 protected function get_shortname_field($id) {
708 return '<input type="text" id="' . $id . '" name="' . $id . '" maxlength="254" value="' . s($this->role->shortname) . '" />';
711 protected function get_description_field($id) {
712 return print_textarea(true, 10, 50, 50, 10, 'description', $this->role->description, 0, true);
715 protected function get_archetype_field($id) {
716 $options = array();
717 $options[''] = get_string('none');
718 foreach(get_role_archetypes() as $type) {
719 $options[$type] = get_string('archetype'.$type, 'role');
721 return html_writer::select($options, 'archetype', $this->role->archetype, false);
724 protected function get_assignable_levels_control() {
725 $output = '';
726 foreach ($this->allcontextlevels as $cl => $clname) {
727 $extraarguments = $this->disabled;
728 if (in_array($cl, $this->contextlevels)) {
729 $extraarguments .= 'checked="checked" ';
731 if (!$this->disabled) {
732 $output .= '<input type="hidden" name="contextlevel' . $cl . '" value="0" />';
734 $output .= '<input type="checkbox" id="cl' . $cl . '" name="contextlevel' . $cl .
735 '" value="1" ' . $extraarguments . '/> ';
736 $output .= '<label for="cl' . $cl . '">' . $clname . "</label><br />\n";
738 return $output;
741 protected function print_field($name, $caption, $field) {
742 global $OUTPUT;
743 // Attempt to generate HTML like formslib.
744 echo '<div class="fitem">';
745 echo '<div class="fitemtitle">';
746 if ($name) {
747 echo '<label for="' . $name . '">';
749 echo $caption;
750 if ($name) {
751 echo "</label>\n";
753 echo '</div>';
754 if (isset($this->errors[$name])) {
755 $extraclass = ' error';
756 } else {
757 $extraclass = '';
759 echo '<div class="felement' . $extraclass . '">';
760 if (isset($this->errors[$name])) {
761 echo $OUTPUT->error_text($this->errors[$name]);
763 echo $field;
764 echo '</div>';
765 echo '</div>';
768 protected function print_show_hide_advanced_button() {
769 echo '<p class="definenotice">' . get_string('highlightedcellsshowdefault', 'role') . ' </p>';
770 echo '<div class="advancedbutton">';
771 echo '<input type="submit" name="toggleadvanced" value="' . get_string('hideadvanced', 'form') . '" />';
772 echo '</div>';
775 public function display() {
776 global $OUTPUT;
777 // Extra fields at the top of the page.
778 echo '<div class="topfields clearfix">';
779 $this->print_field('shortname', get_string('roleshortname', 'role').'&nbsp;'.$OUTPUT->help_icon('roleshortname', 'role'), $this->get_shortname_field('shortname'));
780 $this->print_field('name', get_string('customrolename', 'role').'&nbsp;'.$OUTPUT->help_icon('customrolename', 'role'), $this->get_name_field('name'));
781 $this->print_field('edit-description', get_string('customroledescription', 'role').'&nbsp;'.$OUTPUT->help_icon('customroledescription', 'role'), $this->get_description_field('description'));
782 $this->print_field('menuarchetype', get_string('archetype', 'role').'&nbsp;'.$OUTPUT->help_icon('archetype', 'role'), $this->get_archetype_field('archetype'));
783 $this->print_field('', get_string('maybeassignedin', 'role'), $this->get_assignable_levels_control());
784 echo "</div>";
786 $this->print_show_hide_advanced_button();
788 // Now the permissions table.
789 parent::display();
792 protected function add_permission_cells($capability) {
793 /// One cell for each possible permission.
794 foreach ($this->displaypermissions as $perm => $permname) {
795 $strperm = $this->strperms[$permname];
796 $extraclass = '';
797 if ($perm == $this->parentpermissions[$capability->name]) {
798 $extraclass = ' capdefault';
800 $checked = '';
801 if ($this->permissions[$capability->name] == $perm) {
802 $checked = 'checked="checked" ';
804 echo '<td class="' . $permname . $extraclass . '">';
805 echo '<label><input type="radio" name="' . $capability->name .
806 '" value="' . $perm . '" ' . $checked . '/> ';
807 echo '<span class="note">' . $strperm . '</span>';
808 echo '</label></td>';
813 class define_role_table_basic extends define_role_table_advanced {
814 protected $stradvmessage;
815 protected $strallow;
817 public function __construct($context, $roleid) {
818 parent::__construct($context, $roleid);
819 $this->displaypermissions = array(CAP_ALLOW => $this->allpermissions[CAP_ALLOW]);
820 $this->stradvmessage = get_string('useshowadvancedtochange', 'role');
821 $this->strallow = $this->strperms[$this->allpermissions[CAP_ALLOW]];
824 protected function print_show_hide_advanced_button() {
825 echo '<div class="advancedbutton">';
826 echo '<input type="submit" name="toggleadvanced" value="' . get_string('showadvanced', 'form') . '" />';
827 echo '</div>';
830 protected function add_permission_cells($capability) {
831 $perm = $this->permissions[$capability->name];
832 $permname = $this->allpermissions[$perm];
833 $defaultperm = $this->allpermissions[$this->parentpermissions[$capability->name]];
834 echo '<td class="' . $permname . '">';
835 if ($perm == CAP_ALLOW || $perm == CAP_INHERIT) {
836 $checked = '';
837 if ($perm == CAP_ALLOW) {
838 $checked = 'checked="checked" ';
840 echo '<input type="hidden" name="' . $capability->name . '" value="' . CAP_INHERIT . '" />';
841 echo '<label><input type="checkbox" name="' . $capability->name .
842 '" value="' . CAP_ALLOW . '" ' . $checked . '/> ' . $this->strallow . '</label>';
843 } else {
844 echo '<input type="hidden" name="' . $capability->name . '" value="' . $perm . '" />';
845 echo $this->strperms[$permname] . '<span class="note">' . $this->stradvmessage . '</span>';
847 echo '</td>';
850 class view_role_definition_table extends define_role_table_advanced {
851 public function __construct($context, $roleid) {
852 parent::__construct($context, $roleid);
853 $this->displaypermissions = array(CAP_ALLOW => $this->allpermissions[CAP_ALLOW]);
854 $this->disabled = 'disabled="disabled" ';
857 public function save_changes() {
858 throw new moodle_exception('invalidaccess');
861 protected function get_name_field($id) {
862 return role_get_name($this->role);
865 protected function get_shortname_field($id) {
866 return $this->role->shortname;
869 protected function get_description_field($id) {
870 return role_get_description($this->role);
873 protected function get_archetype_field($id) {
874 if (empty($this->role->archetype)) {
875 return get_string('none');
876 } else {
877 return get_string('archetype'.$this->role->archetype, 'role');
881 protected function print_show_hide_advanced_button() {
882 // Do nothing.
885 protected function add_permission_cells($capability) {
886 $perm = $this->permissions[$capability->name];
887 $permname = $this->allpermissions[$perm];
888 $defaultperm = $this->allpermissions[$this->parentpermissions[$capability->name]];
889 if ($permname != $defaultperm) {
890 $default = get_string('defaultx', 'role', $this->strperms[$defaultperm]);
891 } else {
892 $default = "&#xa0;";
894 echo '<td class="' . $permname . '">' . $this->strperms[$permname] . '<span class="note">' .
895 $default . '</span></td>';
900 class override_permissions_table_advanced extends capability_table_with_risks {
901 protected $strnotset;
902 protected $haslockedcapabilities = false;
905 * Constructor
907 * This method loads loads all the information about the current state of
908 * the overrides, then updates that based on any submitted data. It also
909 * works out which capabilities should be locked for this user.
911 * @param object $context the context this table relates to.
912 * @param integer $roleid the role being overridden.
913 * @param boolean $safeoverridesonly If true, the user is only allowed to override
914 * capabilities with no risks.
916 public function __construct($context, $roleid, $safeoverridesonly) {
917 parent::__construct($context, 'overriderolestable', $roleid);
918 $this->displaypermissions = $this->allpermissions;
919 $this->strnotset = get_string('notset', 'role');
921 /// Determine which capabilities should be locked.
922 if ($safeoverridesonly) {
923 foreach ($this->capabilities as $capid => $cap) {
924 if (!is_safe_capability($cap)) {
925 $this->capabilities[$capid]->locked = true;
926 $this->haslockedcapabilities = true;
932 protected function load_parent_permissions() {
933 global $DB;
935 /// Get the capabilities from the parent context, so that can be shown in the interface.
936 $parentcontext = get_context_instance_by_id(get_parent_contextid($this->context));
937 $this->parentpermissions = role_context_capabilities($this->roleid, $parentcontext);
940 public function has_locked_capabilities() {
941 return $this->haslockedcapabilities;
944 protected function add_permission_cells($capability) {
945 $disabled = '';
946 if ($capability->locked || $this->parentpermissions[$capability->name] == CAP_PROHIBIT) {
947 $disabled = ' disabled="disabled"';
950 /// One cell for each possible permission.
951 foreach ($this->displaypermissions as $perm => $permname) {
952 $strperm = $this->strperms[$permname];
953 $extraclass = '';
954 if ($perm != CAP_INHERIT && $perm == $this->parentpermissions[$capability->name]) {
955 $extraclass = ' capcurrent';
957 $checked = '';
958 if ($this->permissions[$capability->name] == $perm) {
959 $checked = 'checked="checked" ';
961 echo '<td class="' . $permname . $extraclass . '">';
962 echo '<label><input type="radio" name="' . $capability->name .
963 '" value="' . $perm . '" ' . $checked . $disabled . '/> ';
964 if ($perm == CAP_INHERIT) {
965 $inherited = $this->parentpermissions[$capability->name];
966 if ($inherited == CAP_INHERIT) {
967 $inherited = $this->strnotset;
968 } else {
969 $inherited = $this->strperms[$this->allpermissions[$inherited]];
971 $strperm .= ' (' . $inherited . ')';
973 echo '<span class="note">' . $strperm . '</span>';
974 echo '</label></td>';
979 // User selectors for managing role assignments ================================
982 * Base class to avoid duplicating code.
984 abstract class role_assign_user_selector_base extends user_selector_base {
985 const MAX_USERS_PER_PAGE = 100;
987 protected $roleid;
988 protected $context;
991 * @param string $name control name
992 * @param array $options should have two elements with keys groupid and courseid.
994 public function __construct($name, $options) {
995 global $CFG;
996 if (isset($options['context'])) {
997 $this->context = $options['context'];
998 } else {
999 $this->context = get_context_instance_by_id($options['contextid']);
1001 $options['accesscontext'] = $this->context;
1002 parent::__construct($name, $options);
1003 $this->roleid = $options['roleid'];
1004 require_once($CFG->dirroot . '/group/lib.php');
1007 protected function get_options() {
1008 global $CFG;
1009 $options = parent::get_options();
1010 $options['file'] = $CFG->admin . '/roles/lib.php';
1011 $options['roleid'] = $this->roleid;
1012 $options['contextid'] = $this->context->id;
1013 return $options;
1018 * User selector subclass for the list of potential users on the assign roles page,
1019 * when we are assigning in a context below the course level. (CONTEXT_MODULE and
1020 * some CONTEXT_BLOCK).
1022 * This returns only enrolled users in this context.
1024 class potential_assignees_below_course extends role_assign_user_selector_base {
1025 public function find_users($search) {
1026 global $DB;
1028 list($enrolsql, $eparams) = get_enrolled_sql($this->context);
1030 // Now we have to go to the database.
1031 list($wherecondition, $params) = $this->search_sql($search, 'u');
1032 $params = array_merge($params, $eparams);
1034 if ($wherecondition) {
1035 $wherecondition = ' AND ' . $wherecondition;
1038 $fields = 'SELECT ' . $this->required_fields_sql('u');
1039 $countfields = 'SELECT COUNT(u.id)';
1041 $sql = " FROM {user} u
1042 WHERE u.id IN ($enrolsql) $wherecondition
1043 AND u.id NOT IN (
1044 SELECT r.userid
1045 FROM {role_assignments} r
1046 WHERE r.contextid = :contextid
1047 AND r.roleid = :roleid)";
1048 $order = ' ORDER BY lastname ASC, firstname ASC';
1050 $params['contextid'] = $this->context->id;
1051 $params['roleid'] = $this->roleid;
1053 // Check to see if there are too many to show sensibly.
1054 if (!$this->is_validating()) {
1055 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
1056 if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) {
1057 return $this->too_many_results($search, $potentialmemberscount);
1061 // If not, show them.
1062 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
1064 if (empty($availableusers)) {
1065 return array();
1068 if ($search) {
1069 $groupname = get_string('potusersmatching', 'role', $search);
1070 } else {
1071 $groupname = get_string('potusers', 'role');
1074 return array($groupname => $availableusers);
1079 * User selector subclass for the list of potential users on the assign roles page,
1080 * when we are assigning in a context at or above the course level. In this case we
1081 * show all the users in the system who do not already have the role.
1083 class potential_assignees_course_and_above extends role_assign_user_selector_base {
1084 public function find_users($search) {
1085 global $DB;
1087 list($wherecondition, $params) = $this->search_sql($search, '');
1089 $fields = 'SELECT ' . $this->required_fields_sql('');
1090 $countfields = 'SELECT COUNT(1)';
1092 $sql = " FROM {user}
1093 WHERE $wherecondition
1094 AND id NOT IN (
1095 SELECT r.userid
1096 FROM {role_assignments} r
1097 WHERE r.contextid = :contextid
1098 AND r.roleid = :roleid)";
1099 $order = ' ORDER BY lastname ASC, firstname ASC';
1101 $params['contextid'] = $this->context->id;
1102 $params['roleid'] = $this->roleid;
1104 if (!$this->is_validating()) {
1105 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
1106 if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) {
1107 return $this->too_many_results($search, $potentialmemberscount);
1111 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
1113 if (empty($availableusers)) {
1114 return array();
1117 if ($search) {
1118 $groupname = get_string('potusersmatching', 'role', $search);
1119 } else {
1120 $groupname = get_string('potusers', 'role');
1123 return array($groupname => $availableusers);
1128 * User selector subclass for the list of users who already have the role in
1129 * question on the assign roles page.
1131 class existing_role_holders extends role_assign_user_selector_base {
1133 public function __construct($name, $options) {
1134 parent::__construct($name, $options);
1137 public function find_users($search) {
1138 global $DB;
1140 list($wherecondition, $params) = $this->search_sql($search, 'u');
1141 list($ctxcondition, $ctxparams) = $DB->get_in_or_equal(get_parent_contexts($this->context, true), SQL_PARAMS_NAMED, 'ctx');
1142 $params = array_merge($params, $ctxparams);
1143 $params['roleid'] = $this->roleid;
1145 $sql = "SELECT ra.id as raid," . $this->required_fields_sql('u') . ",ra.contextid,ra.component
1146 FROM {role_assignments} ra
1147 JOIN {user} u ON u.id = ra.userid
1148 JOIN {context} ctx ON ra.contextid = ctx.id
1149 WHERE
1150 $wherecondition AND
1151 ctx.id $ctxcondition AND
1152 ra.roleid = :roleid
1153 ORDER BY ctx.depth DESC, ra.component, u.lastname, u.firstname";
1154 $contextusers = $DB->get_records_sql($sql, $params);
1156 // No users at all.
1157 if (empty($contextusers)) {
1158 return array();
1161 // We have users. Out put them in groups by context depth.
1162 // To help the loop below, tack a dummy user on the end of the results
1163 // array, to trigger output of the last group.
1164 $dummyuser = new stdClass;
1165 $dummyuser->contextid = 0;
1166 $dummyuser->id = 0;
1167 $dummyuser->component = '';
1168 $contextusers[] = $dummyuser;
1169 $results = array(); // The results array we are building up.
1170 $doneusers = array(); // Ensures we only list each user at most once.
1171 $currentcontextid = $this->context->id;
1172 $currentgroup = array();
1173 foreach ($contextusers as $user) {
1174 if (isset($doneusers[$user->id])) {
1175 continue;
1177 $doneusers[$user->id] = 1;
1178 if ($user->contextid != $currentcontextid) {
1179 // We have got to the end of the previous group. Add it to the results array.
1180 if ($currentcontextid == $this->context->id) {
1181 $groupname = $this->this_con_group_name($search, count($currentgroup));
1182 } else {
1183 $groupname = $this->parent_con_group_name($search, $currentcontextid);
1185 $results[$groupname] = $currentgroup;
1186 // Get ready for the next group.
1187 $currentcontextid = $user->contextid;
1188 $currentgroup = array();
1190 // Add this user to the group we are building up.
1191 unset($user->contextid);
1192 if ($currentcontextid != $this->context->id) {
1193 $user->disabled = true;
1195 if ($user->component !== '') {
1196 // bad luck, you can tweak only manual role assignments
1197 $user->disabled = true;
1199 unset($user->component);
1200 $currentgroup[$user->id] = $user;
1203 return $results;
1206 protected function this_con_group_name($search, $numusers) {
1207 if ($this->context->contextlevel == CONTEXT_SYSTEM) {
1208 // Special case in the System context.
1209 if ($search) {
1210 return get_string('extusersmatching', 'role', $search);
1211 } else {
1212 return get_string('extusers', 'role');
1215 $contexttype = get_contextlevel_name($this->context->contextlevel);
1216 if ($search) {
1217 $a = new stdClass;
1218 $a->search = $search;
1219 $a->contexttype = $contexttype;
1220 if ($numusers) {
1221 return get_string('usersinthisxmatching', 'role', $a);
1222 } else {
1223 return get_string('noneinthisxmatching', 'role', $a);
1225 } else {
1226 if ($numusers) {
1227 return get_string('usersinthisx', 'role', $contexttype);
1228 } else {
1229 return get_string('noneinthisx', 'role', $contexttype);
1234 protected function parent_con_group_name($search, $contextid) {
1235 $context = get_context_instance_by_id($contextid);
1236 $contextname = print_context_name($context, true, true);
1237 if ($search) {
1238 $a = new stdClass;
1239 $a->contextname = $contextname;
1240 $a->search = $search;
1241 return get_string('usersfrommatching', 'role', $a);
1242 } else {
1243 return get_string('usersfrom', 'role', $contextname);
1249 * Base class for managing the data in the grid of checkboxes on the role allow
1250 * allow/overrides/switch editing pages (allow.php).
1252 abstract class role_allow_role_page {
1253 protected $tablename;
1254 protected $targetcolname;
1255 protected $roles;
1256 protected $allowed = null;
1259 * @param string $tablename the table where our data is stored.
1260 * @param string $targetcolname the name of the target role id column.
1262 public function __construct($tablename, $targetcolname) {
1263 $this->tablename = $tablename;
1264 $this->targetcolname = $targetcolname;
1265 $this->load_required_roles();
1269 * Load information about all the roles we will need information about.
1271 protected function load_required_roles() {
1272 /// Get all roles
1273 $this->roles = role_fix_names(get_all_roles(), get_context_instance(CONTEXT_SYSTEM), ROLENAME_ORIGINAL);
1277 * Update the data with the new settings submitted by the user.
1279 public function process_submission() {
1280 global $DB;
1281 /// Delete all records, then add back the ones that should be allowed.
1282 $DB->delete_records($this->tablename);
1283 foreach ($this->roles as $fromroleid => $notused) {
1284 foreach ($this->roles as $targetroleid => $alsonotused) {
1285 if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
1286 $this->set_allow($fromroleid, $targetroleid);
1293 * Set one allow in the database.
1294 * @param integer $fromroleid
1295 * @param integer $targetroleid
1297 protected abstract function set_allow($fromroleid, $targetroleid);
1300 * Load the current allows from the database.
1302 public function load_current_settings() {
1303 global $DB;
1304 /// Load the current settings
1305 $this->allowed = array();
1306 foreach ($this->roles as $role) {
1307 // Make an array $role->id => false. This is probably too clever for its own good.
1308 $this->allowed[$role->id] = array_combine(array_keys($this->roles), array_fill(0, count($this->roles), false));
1310 $rs = $DB->get_recordset($this->tablename);
1311 foreach ($rs as $allow) {
1312 $this->allowed[$allow->roleid][$allow->{$this->targetcolname}] = true;
1314 $rs->close();
1318 * @param integer $targetroleid a role id.
1319 * @return boolean whether the user should be allowed to select this role as a
1320 * target role.
1322 protected function is_allowed_target($targetroleid) {
1323 return true;
1327 * @return object a $table structure that can be passed to print_table, containing
1328 * one cell for each checkbox.
1330 public function get_table() {
1331 $table = new html_table();
1332 $table->tablealign = 'center';
1333 $table->cellpadding = 5;
1334 $table->cellspacing = 0;
1335 $table->width = '90%';
1336 $table->align = array('left');
1337 $table->rotateheaders = true;
1338 $table->head = array('&#xa0;');
1339 $table->colclasses = array('');
1341 /// Add role name headers.
1342 foreach ($this->roles as $targetrole) {
1343 $table->head[] = $targetrole->localname;
1344 $table->align[] = 'left';
1345 if ($this->is_allowed_target($targetrole->id)) {
1346 $table->colclasses[] = '';
1347 } else {
1348 $table->colclasses[] = 'dimmed_text';
1352 /// Now the rest of the table.
1353 foreach ($this->roles as $fromrole) {
1354 $row = array($fromrole->localname);
1355 foreach ($this->roles as $targetrole) {
1356 $checked = '';
1357 $disabled = '';
1358 if ($this->allowed[$fromrole->id][$targetrole->id]) {
1359 $checked = 'checked="checked" ';
1361 if (!$this->is_allowed_target($targetrole->id)) {
1362 $disabled = 'disabled="disabled" ';
1364 $name = 's_' . $fromrole->id . '_' . $targetrole->id;
1365 $tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
1366 $row[] = '<input type="checkbox" name="' . $name . '" id="' . $name .
1367 '" title="' . $tooltip . '" value="1" ' . $checked . $disabled . '/>' .
1368 '<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
1370 $table->data[] = $row;
1373 return $table;
1377 * Snippet of text displayed above the table, telling the admin what to do.
1378 * @return unknown_type
1380 public abstract function get_intro_text();
1384 * Subclass of role_allow_role_page for the Allow assigns tab.
1386 class role_allow_assign_page extends role_allow_role_page {
1387 public function __construct() {
1388 parent::__construct('role_allow_assign', 'allowassign');
1391 protected function set_allow($fromroleid, $targetroleid) {
1392 allow_assign($fromroleid, $targetroleid);
1395 protected function get_cell_tooltip($fromrole, $targetrole) {
1396 $a = new stdClass;
1397 $a->fromrole = $fromrole->localname;
1398 $a->targetrole = $targetrole->localname;
1399 return get_string('allowroletoassign', 'role', $a);
1402 public function get_intro_text() {
1403 return get_string('configallowassign', 'admin');
1408 * Subclass of role_allow_role_page for the Allow overrides tab.
1410 class role_allow_override_page extends role_allow_role_page {
1411 public function __construct() {
1412 parent::__construct('role_allow_override', 'allowoverride');
1415 protected function set_allow($fromroleid, $targetroleid) {
1416 allow_override($fromroleid, $targetroleid);
1419 protected function get_cell_tooltip($fromrole, $targetrole) {
1420 $a = new stdClass;
1421 $a->fromrole = $fromrole->localname;
1422 $a->targetrole = $targetrole->localname;
1423 return get_string('allowroletooverride', 'role', $a);
1426 public function get_intro_text() {
1427 return get_string('configallowoverride2', 'admin');
1432 * Subclass of role_allow_role_page for the Allow switches tab.
1434 class role_allow_switch_page extends role_allow_role_page {
1435 protected $allowedtargetroles;
1437 public function __construct() {
1438 parent::__construct('role_allow_switch', 'allowswitch');
1441 protected function load_required_roles() {
1442 global $DB;
1443 parent::load_required_roles();
1444 $this->allowedtargetroles = $DB->get_records_menu('role', NULL, 'id');
1447 protected function set_allow($fromroleid, $targetroleid) {
1448 allow_switch($fromroleid, $targetroleid);
1451 protected function is_allowed_target($targetroleid) {
1452 return isset($this->allowedtargetroles[$targetroleid]);
1455 protected function get_cell_tooltip($fromrole, $targetrole) {
1456 $a = new stdClass;
1457 $a->fromrole = $fromrole->localname;
1458 $a->targetrole = $targetrole->localname;
1459 return get_string('allowroletoswitch', 'role', $a);
1462 public function get_intro_text() {
1463 return get_string('configallowswitch', 'admin');
1468 * Get the potential assignees selector for a given context.
1470 * If this context is a course context, or inside a course context (module or
1471 * some blocks) then return a potential_assignees_below_course object. Otherwise
1472 * return a potential_assignees_course_and_above.
1474 * @param stdClass $context a context.
1475 * @param string $name passed to user selector constructor.
1476 * @param array $options to user selector constructor.
1477 * @return user_selector_base an appropriate user selector.
1479 function roles_get_potential_user_selector($context, $name, $options) {
1480 $blockinsidecourse = false;
1481 if ($context->contextlevel == CONTEXT_BLOCK) {
1482 $parentcontext = get_context_instance_by_id(get_parent_contextid($context));
1483 $blockinsidecourse = in_array($parentcontext->contextlevel, array(CONTEXT_MODULE, CONTEXT_COURSE));
1486 if (($context->contextlevel == CONTEXT_MODULE || $blockinsidecourse) &&
1487 !is_inside_frontpage($context)) {
1488 $potentialuserselector = new potential_assignees_below_course('addselect', $options);
1489 } else {
1490 $potentialuserselector = new potential_assignees_course_and_above('addselect', $options);
1492 return $potentialuserselector;
1495 class admins_potential_selector extends user_selector_base {
1497 * @param string $name control name
1498 * @param array $options should have two elements with keys groupid and courseid.
1500 public function __construct() {
1501 global $CFG, $USER;
1502 $admins = explode(',', $CFG->siteadmins);
1503 parent::__construct('addselect', array('multiselect'=>false, 'exclude'=>$admins));
1506 public function find_users($search) {
1507 global $CFG, $DB;
1508 list($wherecondition, $params) = $this->search_sql($search, '');
1510 $fields = 'SELECT ' . $this->required_fields_sql('');
1511 $countfields = 'SELECT COUNT(1)';
1513 $sql = " FROM {user}
1514 WHERE $wherecondition AND mnethostid = :localmnet";
1515 $order = ' ORDER BY lastname ASC, firstname ASC';
1516 $params['localmnet'] = $CFG->mnet_localhost_id; // it could be dangerous to make remote users admins and also this could lead to other problems
1518 // Check to see if there are too many to show sensibly.
1519 if (!$this->is_validating()) {
1520 $potentialcount = $DB->count_records_sql($countfields . $sql, $params);
1521 if ($potentialcount > 100) {
1522 return $this->too_many_results($search, $potentialcount);
1526 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
1528 if (empty($availableusers)) {
1529 return array();
1532 if ($search) {
1533 $groupname = get_string('potusersmatching', 'role', $search);
1534 } else {
1535 $groupname = get_string('potusers', 'role');
1538 return array($groupname => $availableusers);
1541 protected function get_options() {
1542 global $CFG;
1543 $options = parent::get_options();
1544 $options['file'] = $CFG->admin . '/roles/lib.php';
1545 return $options;
1549 class admins_existing_selector extends user_selector_base {
1551 * @param string $name control name
1552 * @param array $options should have two elements with keys groupid and courseid.
1554 public function __construct() {
1555 global $CFG, $USER;
1556 parent::__construct('removeselect', array('multiselect'=>false));
1559 public function find_users($search) {
1560 global $DB, $CFG;
1561 list($wherecondition, $params) = $this->search_sql($search, '');
1563 $fields = 'SELECT ' . $this->required_fields_sql('');
1564 $countfields = 'SELECT COUNT(1)';
1566 if ($wherecondition) {
1567 $wherecondition = "$wherecondition AND id IN ($CFG->siteadmins)";
1568 } else {
1569 $wherecondition = "id IN ($CFG->siteadmins)";
1571 $sql = " FROM {user}
1572 WHERE $wherecondition";
1573 $order = ' ORDER BY lastname ASC, firstname ASC';
1575 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
1577 if (empty($availableusers)) {
1578 return array();
1581 $mainadmin = array();
1582 $adminids = explode(',', $CFG->siteadmins);
1583 foreach ($adminids as $id) {
1584 if (isset($availableusers[$id])) {
1585 $mainadmin = array($id=>$availableusers[$id]);
1586 unset($availableusers[$id]);
1587 break;
1591 $result = array();
1592 if ($mainadmin) {
1593 $result[get_string('mainadmin', 'role')] = $mainadmin;
1596 if ($availableusers) {
1597 if ($search) {
1598 $groupname = get_string('extusersmatching', 'role', $search);
1599 } else {
1600 $groupname = get_string('extusers', 'role');
1602 $result[$groupname] = $availableusers;
1605 return $result;
1608 protected function get_options() {
1609 global $CFG;
1610 $options = parent::get_options();
1611 $options['file'] = $CFG->admin . '/roles/lib.php';
1612 return $options;