Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/roles/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00033 require_once($CFG->libdir.'/adminlib.php');
00034 require_once($CFG->dirroot.'/user/selector/lib.php');
00035 
00036 // Classes for producing tables with one row per capability ====================
00037 
00046 abstract class capability_table_base {
00048     protected $context;
00049 
00051     protected $capabilities = array();
00052 
00054     protected $id;
00055 
00057     protected $classes = array('rolecap');
00058 
00060     const NUM_CAPS_FOR_SEARCH = 12;
00061 
00067     public function __construct($context, $id) {
00068         $this->context = $context;
00069         $this->capabilities = fetch_context_capabilities($context);
00070         $this->id = $id;
00071     }
00072 
00078     public function add_classes($classnames) {
00079         $this->classes = array_unique(array_merge($this->classes, $classnames));
00080     }
00081 
00085     public function display() {
00086         if (count($this->capabilities) > capability_table_base::NUM_CAPS_FOR_SEARCH) {
00087             global $PAGE;
00088             $PAGE->requires->strings_for_js(array('filter','clear'),'moodle');
00089             $PAGE->requires->js_init_call('M.core_role.init_cap_table_filter', array($this->id, $this->context->id));
00090         }
00091         echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n";
00092         echo '<tr><th class="name" align="left" scope="col">' . get_string('capability','role') . '</th>';
00093         $this->add_header_cells();
00094         echo "</tr>\n</thead>\n<tbody>\n";
00095 
00097         $contextlevel = 0;
00098         $component = '';
00099         foreach ($this->capabilities as $capability) {
00100             if ($this->skip_row($capability)) {
00101                 continue;
00102             }
00103 
00105             if (component_level_changed($capability, $component, $contextlevel)) {
00106                 $this->print_heading_row($capability);
00107             }
00108             $contextlevel = $capability->contextlevel;
00109             $component = $capability->component;
00110 
00112             echo '<tr class="' . implode(' ', array_unique(array_merge(array('rolecap'),
00113                     $this->get_row_classes($capability)))) . '">';
00114 
00116             echo '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
00117                     '<span class="cap-name">' . $capability->name . '</span></span></th>';
00118 
00120             $this->add_row_cells($capability);
00121 
00123             echo "</tr>\n";
00124         }
00125 
00127         echo "</tbody>\n</table>\n";
00128     }
00129 
00134     protected function print_heading_row($capability) {
00135         echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
00136                 get_component_string($capability->component, $capability->contextlevel) .
00137                 '</strong></td></tr>';
00138 
00139     }
00140 
00142     protected abstract function add_header_cells();
00143 
00145     protected abstract function num_extra_columns();
00146 
00154     protected function skip_row($capability) {
00155         return false;
00156     }
00157 
00165     protected function get_row_classes($capability) {
00166         return array();
00167     }
00168 
00177     protected abstract function add_row_cells($capability);
00178 }
00179 
00185 class check_capability_table extends capability_table_base {
00186     protected $user;
00187     protected $fullname;
00188     protected $contextname;
00189     protected $stryes;
00190     protected $strno;
00191     private $hascap;
00192 
00199     public function __construct($context, $user, $contextname) {
00200         global $CFG;
00201         parent::__construct($context, 'explaincaps');
00202         $this->user = $user;
00203         $this->fullname = fullname($user);
00204         $this->contextname = $contextname;
00205         $this->stryes = get_string('yes');
00206         $this->strno = get_string('no');
00207     }
00208 
00209     protected function add_header_cells() {
00210         echo '<th>' . get_string('allowed', 'role') . '</th>';
00211     }
00212 
00213     protected function num_extra_columns() {
00214         return 1;
00215     }
00216 
00217     protected function get_row_classes($capability) {
00218         $this->hascap = has_capability($capability->name, $this->context, $this->user->id);
00219         if ($this->hascap) {
00220             return array('yes');
00221         } else {
00222             return array('no');
00223         }
00224     }
00225 
00226     protected function add_row_cells($capability) {
00227         global $OUTPUT;
00228         if ($this->hascap) {
00229             $result = $this->stryes;
00230         } else {
00231             $result = $this->strno;
00232         }
00233         $a = new stdClass;
00234         $a->fullname = $this->fullname;
00235         $a->capability = $capability->name;
00236         $a->context = $this->contextname;
00237         echo '<td>' . $result . '</td>';
00238     }
00239 }
00240 
00241 
00245 class permissions_table extends capability_table_base {
00246     protected $contextname;
00247     protected $allowoverrides;
00248     protected $allowsafeoverrides;
00249     protected $overridableroles;
00250     protected $roles;
00251     protected $icons = array();
00252 
00258     public function __construct($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles) {
00259         global $DB;
00260 
00261         parent::__construct($context, 'permissions');
00262         $this->contextname = $contextname;
00263         $this->allowoverrides = $allowoverrides;
00264         $this->allowsafeoverrides = $allowsafeoverrides;
00265         $this->overridableroles = $overridableroles;
00266 
00267         $roles = $DB->get_records('role', null, 'sortorder DESC');
00268         foreach ($roles as $roleid=>$role) {
00269             $roles[$roleid] = $role->name;
00270         }
00271         $this->roles = role_fix_names($roles, $context);
00272 
00273     }
00274 
00275     protected function add_header_cells() {
00276         echo '<th>' . get_string('risks', 'role') . '</th>';
00277         echo '<th>' . get_string('neededroles', 'role') . '</th>';
00278         echo '<th>' . get_string('prohibitedroles', 'role') . '</th>';
00279     }
00280 
00281     protected function num_extra_columns() {
00282         return 3;
00283     }
00284 
00285     protected function add_row_cells($capability) {
00286         global $OUTPUT, $PAGE;
00287 
00288         $context = $this->context;
00289         $contextid = $this->context->id;
00290         $allowoverrides = $this->allowoverrides;
00291         $allowsafeoverrides = $this->allowsafeoverrides;
00292         $overridableroles = $this->overridableroles;
00293         $roles = $this->roles;
00294 
00295 
00296         list($needed, $forbidden) = get_roles_with_cap_in_context($context, $capability->name);
00297         $neededroles    = array();
00298         $forbiddenroles = array();
00299         $allowable      = $overridableroles;
00300         $forbitable     = $overridableroles;
00301         foreach ($neededroles as $id=>$unused) {
00302             unset($allowable[$id]);
00303         }
00304         foreach ($forbidden as $id=>$unused) {
00305             unset($allowable[$id]);
00306             unset($forbitable[$id]);
00307         }
00308 
00309         foreach ($roles as $id=>$name) {
00310             if (isset($needed[$id])) {
00311                 $neededroles[$id] = $roles[$id];
00312                 if (isset($overridableroles[$id]) and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
00313                     $preventurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'prevent'=>1));
00314                     $neededroles[$id] .= $OUTPUT->action_icon($preventurl, new pix_icon('t/delete', get_string('prevent', 'role')));
00315                 }
00316             }
00317         }
00318         $neededroles = implode(', ', $neededroles);
00319         foreach ($roles as $id=>$name) {
00320             if (isset($forbidden[$id])  and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
00321                 $forbiddenroles[$id] = $roles[$id];
00322                 if (isset($overridableroles[$id]) and prohibit_is_removable($id, $context, $capability->name)) {
00323                     $unprohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'roleid'=>$id, 'capability'=>$capability->name, 'unprohibit'=>1));
00324                     $forbiddenroles[$id] .= $OUTPUT->action_icon($unprohibiturl, new pix_icon('t/delete', get_string('delete')));
00325                 }
00326             }
00327         }
00328         $forbiddenroles = implode(', ', $forbiddenroles);
00329 
00330         if ($allowable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
00331             $allowurl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'allow'=>1));
00332             $neededroles .= '<div class="allowmore">'.$OUTPUT->action_icon($allowurl, new pix_icon('t/add', get_string('allow', 'role'))).'</div>';
00333         }
00334 
00335         if ($forbitable and ($allowoverrides or ($allowsafeoverrides and is_safe_capability($capability)))) {
00336             $prohibiturl = new moodle_url($PAGE->url, array('contextid'=>$contextid, 'capability'=>$capability->name, 'prohibit'=>1));
00337             $forbiddenroles .= '<div class="prohibitmore">'.$OUTPUT->action_icon($prohibiturl, new pix_icon('t/add', get_string('prohibit', 'role'))).'</div>';
00338         }
00339 
00340         $risks = $this->get_risks($capability);
00341 
00342         echo '<td>' . $risks . '</td>';
00343         echo '<td>' . $neededroles . '</td>';
00344         echo '<td>' . $forbiddenroles . '</td>';
00345     }
00346 
00347     protected function get_risks($capability) {
00348         global $OUTPUT;
00349 
00350         $allrisks = get_all_risks();
00351         $risksurl = new moodle_url(get_docs_url(s(get_string('risks', 'role'))));
00352 
00353         $return = '';
00354 
00355         foreach ($allrisks as $type=>$risk) {
00356             if ($risk & (int)$capability->riskbitmask) {
00357                 if (!isset($this->icons[$type])) {
00358                     $pixicon = new pix_icon('/i/' . str_replace('risk', 'risk_', $type), get_string($type . 'short', 'admin'));
00359                     $this->icons[$type] = $OUTPUT->action_icon($risksurl, $pixicon, new popup_action('click', $risksurl));
00360                 }
00361                 $return .= $this->icons[$type];
00362             }
00363         }
00364 
00365         return $return;
00366     }
00367 }
00368 
00369 
00376 abstract class capability_table_with_risks extends capability_table_base {
00377     protected $allrisks;
00378     protected $allpermissions; // We don't need perms ourselves, but all our subclasses do.
00379     protected $strperms; // Language string cache.
00380     protected $risksurl; // URL in moodledocs about risks.
00381     protected $riskicons = array(); // Cache to avoid regenerating the HTML for each risk icon.
00383     protected $parentpermissions;
00384     protected $displaypermissions;
00385     protected $permissions;
00386     protected $changed;
00387     protected $roleid;
00388 
00389     public function __construct($context, $id, $roleid) {
00390         parent::__construct($context, $id);
00391 
00392         $this->allrisks = get_all_risks();
00393         $this->risksurl = get_docs_url(s(get_string('risks', 'role')));
00394 
00395         $this->allpermissions = array(
00396             CAP_INHERIT => 'inherit',
00397             CAP_ALLOW => 'allow',
00398             CAP_PREVENT => 'prevent' ,
00399             CAP_PROHIBIT => 'prohibit',
00400         );
00401 
00402         $this->strperms = array();
00403         foreach ($this->allpermissions as $permname) {
00404             $this->strperms[$permname] =  get_string($permname, 'role');
00405         }
00406 
00407         $this->roleid = $roleid;
00408         $this->load_current_permissions();
00409 
00411         foreach ($this->capabilities as $capid => $cap) {
00412             if (!isset($this->permissions[$cap->name])) {
00413                 $this->permissions[$cap->name] = CAP_INHERIT;
00414             }
00415             $this->capabilities[$capid]->locked = false;
00416         }
00417     }
00418 
00419     protected function load_current_permissions() {
00420         global $DB;
00421 
00423         if ($this->roleid) {
00424             $this->permissions = $DB->get_records_menu('role_capabilities', array('roleid' => $this->roleid,
00425                     'contextid' => $this->context->id), '', 'capability,permission');
00426         } else {
00427             $this->permissions = array();
00428         }
00429     }
00430 
00431     protected abstract function load_parent_permissions();
00432 
00437     public function read_submitted_permissions() {
00438         $this->changed = array();
00439 
00440         foreach ($this->capabilities as $cap) {
00441             if ($cap->locked || $this->skip_row($cap)) {
00443                 continue;
00444             }
00445 
00446             $permission = optional_param($cap->name, null, PARAM_PERMISSION);
00447             if (is_null($permission)) {
00449                 continue;
00450             }
00451 
00454             if ($this->permissions[$cap->name] != $permission) {
00455                 $this->permissions[$cap->name] = $permission;
00456                 $this->changed[] = $cap->name;
00457             }
00458         }
00459     }
00460 
00464     public function save_changes() {
00466         foreach ($this->changed as $changedcap) {
00467             assign_capability($changedcap, $this->permissions[$changedcap],
00468                     $this->roleid, $this->context->id, true);
00469         }
00470 
00472         mark_context_dirty($this->context->path);
00473     }
00474 
00475     public function display() {
00476         $this->load_parent_permissions();
00477         foreach ($this->capabilities as $cap) {
00478             if (!isset($this->parentpermissions[$cap->name])) {
00479                 $this->parentpermissions[$cap->name] = CAP_INHERIT;
00480             }
00481         }
00482         parent::display();
00483     }
00484 
00485     protected function add_header_cells() {
00486         global $OUTPUT;
00487         echo '<th colspan="' . count($this->displaypermissions) . '" scope="col">' .
00488                 get_string('permission', 'role') . ' ' . $OUTPUT->help_icon('permission', 'role') . '</th>';
00489         echo '<th class="risk" colspan="' . count($this->allrisks) . '" scope="col">' . get_string('risks','role') . '</th>';
00490     }
00491 
00492     protected function num_extra_columns() {
00493         return count($this->displaypermissions) + count($this->allrisks);
00494     }
00495 
00496     protected function get_row_classes($capability) {
00497         $rowclasses = array();
00498         foreach ($this->allrisks as $riskname => $risk) {
00499             if ($risk & (int)$capability->riskbitmask) {
00500                 $rowclasses[] = $riskname;
00501             }
00502         }
00503         return $rowclasses;
00504     }
00505 
00506     protected abstract function add_permission_cells($capability);
00507 
00508     protected function add_row_cells($capability) {
00509         $this->add_permission_cells($capability);
00511         foreach ($this->allrisks as $riskname => $risk) {
00512             echo '<td class="risk ' . str_replace('risk', '', $riskname) . '">';
00513             if ($risk & (int)$capability->riskbitmask) {
00514                 echo $this->get_risk_icon($riskname);
00515             }
00516             echo '</td>';
00517         }
00518     }
00519 
00526     function get_risk_icon($type) {
00527         global $OUTPUT;
00528         if (!isset($this->riskicons[$type])) {
00529             $iconurl = $OUTPUT->pix_url('i/' . str_replace('risk', 'risk_', $type));
00530             $text = '<img src="' . $iconurl . '" alt="' . get_string($type . 'short', 'admin') . '" />';
00531             $action = new popup_action('click', $this->risksurl, 'docspopup');
00532             $this->riskicons[$type] = $OUTPUT->action_link($this->risksurl, $text, $action, array('title'=>get_string($type, 'admin')));
00533         }
00534         return $this->riskicons[$type];
00535     }
00536 }
00537 
00543 class define_role_table_advanced extends capability_table_with_risks {
00545     protected $role;
00547     protected $errors;
00548     protected $contextlevels;
00549     protected $allcontextlevels;
00550     protected $disabled = '';
00551 
00552     public function __construct($context, $roleid) {
00553         $this->roleid = $roleid;
00554         parent::__construct($context, 'defineroletable', $roleid);
00555         $this->displaypermissions = $this->allpermissions;
00556         $this->strperms[$this->allpermissions[CAP_INHERIT]] = get_string('notset', 'role');
00557 
00558         $this->allcontextlevels = array(
00559             CONTEXT_SYSTEM => get_string('coresystem'),
00560             CONTEXT_USER => get_string('user'),
00561             CONTEXT_COURSECAT => get_string('category'),
00562             CONTEXT_COURSE => get_string('course'),
00563             CONTEXT_MODULE => get_string('activitymodule'),
00564             CONTEXT_BLOCK => get_string('block')
00565         );
00566     }
00567 
00568     protected function load_current_permissions() {
00569         global $DB;
00570         if ($this->roleid) {
00571             if (!$this->role = $DB->get_record('role', array('id' => $this->roleid))) {
00572                 throw new moodle_exception('invalidroleid');
00573             }
00574             $contextlevels = get_role_contextlevels($this->roleid);
00575             // Put the contextlevels in the array keys, as well as the values.
00576             if (!empty($contextlevels)) {
00577                 $this->contextlevels = array_combine($contextlevels, $contextlevels);
00578             } else {
00579                 $this->contextlevels = array();
00580             }
00581         } else {
00582             $this->role = new stdClass;
00583             $this->role->name = '';
00584             $this->role->shortname = '';
00585             $this->role->description = '';
00586             $this->role->archetype = '';
00587             $this->contextlevels = array();
00588         }
00589         parent::load_current_permissions();
00590     }
00591 
00592     public function read_submitted_permissions() {
00593         global $DB;
00594         $this->errors = array();
00595 
00596         // Role name.
00597         $name = optional_param('name', null, PARAM_MULTILANG);
00598         if (!is_null($name)) {
00599             $this->role->name = $name;
00600             if (html_is_blank($this->role->name)) {
00601                 $this->errors['name'] = get_string('errorbadrolename', 'role');
00602             }
00603         }
00604         if ($DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) {
00605             $this->errors['name'] = get_string('errorexistsrolename', 'role');
00606         }
00607 
00608         // Role short name. We clean this in a special way. We want to end up
00609         // with only lowercase safe ASCII characters.
00610         $shortname = optional_param('shortname', null, PARAM_RAW);
00611         if (!is_null($shortname)) {
00612             $this->role->shortname = $shortname;
00613             $this->role->shortname = textlib_get_instance()->specialtoascii($this->role->shortname);
00614             $this->role->shortname = moodle_strtolower(clean_param($this->role->shortname, PARAM_ALPHANUMEXT));
00615             if (empty($this->role->shortname)) {
00616                 $this->errors['shortname'] = get_string('errorbadroleshortname', 'role');
00617             }
00618         }
00619         if ($DB->record_exists_select('role', 'shortname = ? and id <> ?', array($this->role->shortname, $this->roleid))) {
00620             $this->errors['shortname'] = get_string('errorexistsroleshortname', 'role');
00621         }
00622 
00623         // Description.
00624         $description = optional_param('description', null, PARAM_RAW);
00625         if (!is_null($description)) {
00626             $this->role->description = $description;
00627         }
00628 
00629         // Legacy type.
00630         $archetype = optional_param('archetype', null, PARAM_RAW);
00631         if (isset($archetype)) {
00632             $archetypes = get_role_archetypes();
00633             if (isset($archetypes[$archetype])){
00634                 $this->role->archetype = $archetype;
00635             } else {
00636                 $this->role->archetype = '';
00637             }
00638         }
00639 
00640         // Assignable context levels.
00641         foreach ($this->allcontextlevels as $cl => $notused) {
00642             $assignable = optional_param('contextlevel' . $cl, null, PARAM_BOOL);
00643             if (!is_null($assignable)) {
00644                 if ($assignable) {
00645                     $this->contextlevels[$cl] = $cl;
00646                 } else {
00647                     unset($this->contextlevels[$cl]);
00648                 }
00649             }
00650         }
00651 
00652         // Now read the permissions for each capability.
00653         parent::read_submitted_permissions();
00654     }
00655 
00656     public function is_submission_valid() {
00657         return empty($this->errors);
00658     }
00659 
00664     public function make_copy() {
00665         $this->roleid = 0;
00666         unset($this->role->id);
00667         $this->role->name .= ' ' . get_string('copyasnoun');
00668         $this->role->shortname .= 'copy';
00669     }
00670 
00671     public function get_role_name() {
00672         return $this->role->name;
00673     }
00674 
00675     public function get_role_id() {
00676         return $this->role->id;
00677     }
00678 
00679     public function get_archetype() {
00680         return $this->role->archetype;
00681     }
00682 
00683     protected function load_parent_permissions() {
00684         $this->parentpermissions = get_default_capabilities($this->role->archetype);
00685     }
00686 
00687     public function save_changes() {
00688         global $DB;
00689 
00690         if (!$this->roleid) {
00691             // Creating role
00692             $this->role->id = create_role($this->role->name, $this->role->shortname, $this->role->description, $this->role->archetype);
00693             $this->roleid = $this->role->id; // Needed to make the parent::save_changes(); call work.
00694         } else {
00695             // Updating role
00696             $DB->update_record('role', $this->role);
00697         }
00698 
00699         // Assignable contexts.
00700         set_role_contextlevels($this->role->id, $this->contextlevels);
00701 
00702         // Permissions.
00703         parent::save_changes();
00704     }
00705 
00706     protected function get_name_field($id) {
00707         return '<input type="text" id="' . $id . '" name="' . $id . '" maxlength="254" value="' . s($this->role->name) . '" />';
00708     }
00709 
00710     protected function get_shortname_field($id) {
00711         return '<input type="text" id="' . $id . '" name="' . $id . '" maxlength="254" value="' . s($this->role->shortname) . '" />';
00712     }
00713 
00714     protected function get_description_field($id) {
00715         return print_textarea(true, 10, 50, 50, 10, 'description', $this->role->description, 0, true);
00716     }
00717 
00718     protected function get_archetype_field($id) {
00719         $options = array();
00720         $options[''] = get_string('none');
00721         foreach(get_role_archetypes() as $type) {
00722             $options[$type] = get_string('archetype'.$type, 'role');
00723         }
00724         return html_writer::select($options, 'archetype', $this->role->archetype, false);
00725     }
00726 
00727     protected function get_assignable_levels_control() {
00728         $output = '';
00729         foreach ($this->allcontextlevels as $cl => $clname) {
00730             $extraarguments = $this->disabled;
00731             if (in_array($cl, $this->contextlevels)) {
00732                 $extraarguments .= 'checked="checked" ';
00733             }
00734             if (!$this->disabled) {
00735                 $output .= '<input type="hidden" name="contextlevel' . $cl . '" value="0" />';
00736             }
00737             $output .= '<input type="checkbox" id="cl' . $cl . '" name="contextlevel' . $cl .
00738                     '" value="1" ' . $extraarguments . '/> ';
00739             $output .= '<label for="cl' . $cl . '">' . $clname . "</label><br />\n";
00740         }
00741         return $output;
00742     }
00743 
00744     protected function print_field($name, $caption, $field) {
00745         global $OUTPUT;
00746         // Attempt to generate HTML like formslib.
00747         echo '<div class="fitem">';
00748         echo '<div class="fitemtitle">';
00749         if ($name) {
00750             echo '<label for="' . $name . '">';
00751         }
00752         echo $caption;
00753         if ($name) {
00754             echo "</label>\n";
00755         }
00756         echo '</div>';
00757         if (isset($this->errors[$name])) {
00758             $extraclass = ' error';
00759         } else {
00760             $extraclass = '';
00761         }
00762         echo '<div class="felement' . $extraclass . '">';
00763         if (isset($this->errors[$name])) {
00764             echo $OUTPUT->error_text($this->errors[$name]);
00765         }
00766         echo $field;
00767         echo '</div>';
00768         echo '</div>';
00769     }
00770 
00771     protected function print_show_hide_advanced_button() {
00772         echo '<p class="definenotice">' . get_string('highlightedcellsshowdefault', 'role') . ' </p>';
00773         echo '<div class="advancedbutton">';
00774         echo '<input type="submit" name="toggleadvanced" value="' . get_string('hideadvanced', 'form') . '" />';
00775         echo '</div>';
00776     }
00777 
00778     public function display() {
00779         global $OUTPUT;
00780         // Extra fields at the top of the page.
00781         echo '<div class="topfields clearfix">';
00782         $this->print_field('name', get_string('rolefullname', 'role'), $this->get_name_field('name'));
00783         $this->print_field('shortname', get_string('roleshortname', 'role'), $this->get_shortname_field('shortname'));
00784         $this->print_field('edit-description', get_string('description'), $this->get_description_field('description'));
00785         $this->print_field('menuarchetype', get_string('archetype', 'role').'&nbsp;'.$OUTPUT->help_icon('archetype', 'role'), $this->get_archetype_field('archetype'));
00786         $this->print_field('', get_string('maybeassignedin', 'role'), $this->get_assignable_levels_control());
00787         echo "</div>";
00788 
00789         $this->print_show_hide_advanced_button();
00790 
00791         // Now the permissions table.
00792         parent::display();
00793     }
00794 
00795     protected function add_permission_cells($capability) {
00797         foreach ($this->displaypermissions as $perm => $permname) {
00798             $strperm = $this->strperms[$permname];
00799             $extraclass = '';
00800             if ($perm == $this->parentpermissions[$capability->name]) {
00801                 $extraclass = ' capdefault';
00802             }
00803             $checked = '';
00804             if ($this->permissions[$capability->name] == $perm) {
00805                 $checked = 'checked="checked" ';
00806             }
00807             echo '<td class="' . $permname . $extraclass . '">';
00808             echo '<label><input type="radio" name="' . $capability->name .
00809                     '" value="' . $perm . '" ' . $checked . '/> ';
00810             echo '<span class="note">' . $strperm . '</span>';
00811             echo '</label></td>';
00812         }
00813     }
00814 }
00815 
00816 class define_role_table_basic extends define_role_table_advanced {
00817     protected $stradvmessage;
00818     protected $strallow;
00819 
00820     public function __construct($context, $roleid) {
00821         parent::__construct($context, $roleid);
00822         $this->displaypermissions = array(CAP_ALLOW => $this->allpermissions[CAP_ALLOW]);
00823         $this->stradvmessage = get_string('useshowadvancedtochange', 'role');
00824         $this->strallow = $this->strperms[$this->allpermissions[CAP_ALLOW]];
00825     }
00826 
00827     protected function print_show_hide_advanced_button() {
00828         echo '<div class="advancedbutton">';
00829         echo '<input type="submit" name="toggleadvanced" value="' . get_string('showadvanced', 'form') . '" />';
00830         echo '</div>';
00831     }
00832 
00833     protected function add_permission_cells($capability) {
00834         $perm = $this->permissions[$capability->name];
00835         $permname = $this->allpermissions[$perm];
00836         $defaultperm = $this->allpermissions[$this->parentpermissions[$capability->name]];
00837         echo '<td class="' . $permname . '">';
00838         if ($perm == CAP_ALLOW || $perm == CAP_INHERIT) {
00839             $checked = '';
00840             if ($perm == CAP_ALLOW) {
00841                 $checked = 'checked="checked" ';
00842             }
00843             echo '<input type="hidden" name="' . $capability->name . '" value="' . CAP_INHERIT . '" />';
00844             echo '<label><input type="checkbox" name="' . $capability->name .
00845                     '" value="' . CAP_ALLOW . '" ' . $checked . '/> ' . $this->strallow . '</label>';
00846         } else {
00847             echo '<input type="hidden" name="' . $capability->name . '" value="' . $perm . '" />';
00848             echo $this->strperms[$permname] . '<span class="note">' . $this->stradvmessage . '</span>';
00849         }
00850         echo '</td>';
00851     }
00852 }
00853 class view_role_definition_table extends define_role_table_advanced {
00854     public function __construct($context, $roleid) {
00855         parent::__construct($context, $roleid);
00856         $this->displaypermissions = array(CAP_ALLOW => $this->allpermissions[CAP_ALLOW]);
00857         $this->disabled = 'disabled="disabled" ';
00858     }
00859 
00860     public function save_changes() {
00861         throw new moodle_exception('invalidaccess');
00862     }
00863 
00864     protected function get_name_field($id) {
00865         return strip_tags(format_string($this->role->name));
00866     }
00867 
00868     protected function get_shortname_field($id) {
00869         return $this->role->shortname;
00870     }
00871 
00872     protected function get_description_field($id) {
00873         return format_text($this->role->description, FORMAT_HTML);
00874     }
00875 
00876     protected function get_archetype_field($id) {
00877         if (empty($this->role->archetype)) {
00878             return get_string('none');
00879         } else {
00880             return get_string('archetype'.$this->role->archetype, 'role');
00881         }
00882     }
00883 
00884     protected function print_show_hide_advanced_button() {
00885         // Do nothing.
00886     }
00887 
00888     protected function add_permission_cells($capability) {
00889         $perm = $this->permissions[$capability->name];
00890         $permname = $this->allpermissions[$perm];
00891         $defaultperm = $this->allpermissions[$this->parentpermissions[$capability->name]];
00892         if ($permname != $defaultperm) {
00893             $default = get_string('defaultx', 'role', $this->strperms[$defaultperm]);
00894         } else {
00895             $default = "&#xa0;";
00896         }
00897         echo '<td class="' . $permname . '">' . $this->strperms[$permname] . '<span class="note">' .
00898                 $default . '</span></td>';
00899 
00900     }
00901 }
00902 
00903 class override_permissions_table_advanced extends capability_table_with_risks {
00904     protected $strnotset;
00905     protected $haslockedcapabilities = false;
00906 
00919     public function __construct($context, $roleid, $safeoverridesonly) {
00920         parent::__construct($context, 'overriderolestable', $roleid);
00921         $this->displaypermissions = $this->allpermissions;
00922         $this->strnotset = get_string('notset', 'role');
00923 
00925         if ($safeoverridesonly) {
00926             foreach ($this->capabilities as $capid => $cap) {
00927                 if (!is_safe_capability($cap)) {
00928                     $this->capabilities[$capid]->locked = true;
00929                     $this->haslockedcapabilities = true;
00930                 }
00931             }
00932         }
00933     }
00934 
00935     protected function load_parent_permissions() {
00936         global $DB;
00937 
00939         $parentcontext = get_context_instance_by_id(get_parent_contextid($this->context));
00940         $this->parentpermissions = role_context_capabilities($this->roleid, $parentcontext);
00941     }
00942 
00943     public function has_locked_capabilities() {
00944         return $this->haslockedcapabilities;
00945     }
00946 
00947     protected function add_permission_cells($capability) {
00948         $disabled = '';
00949         if ($capability->locked || $this->parentpermissions[$capability->name] == CAP_PROHIBIT) {
00950             $disabled = ' disabled="disabled"';
00951         }
00952 
00954         foreach ($this->displaypermissions as $perm => $permname) {
00955             $strperm = $this->strperms[$permname];
00956             $extraclass = '';
00957             if ($perm != CAP_INHERIT && $perm == $this->parentpermissions[$capability->name]) {
00958                 $extraclass = ' capcurrent';
00959             }
00960             $checked = '';
00961             if ($this->permissions[$capability->name] == $perm) {
00962                 $checked = 'checked="checked" ';
00963             }
00964             echo '<td class="' . $permname . $extraclass . '">';
00965             echo '<label><input type="radio" name="' . $capability->name .
00966                     '" value="' . $perm . '" ' . $checked . $disabled . '/> ';
00967             if ($perm == CAP_INHERIT) {
00968                 $inherited = $this->parentpermissions[$capability->name];
00969                 if ($inherited == CAP_INHERIT) {
00970                     $inherited = $this->strnotset;
00971                 } else {
00972                     $inherited = $this->strperms[$this->allpermissions[$inherited]];
00973                 }
00974                 $strperm .= ' (' . $inherited . ')';
00975             }
00976             echo '<span class="note">' . $strperm . '</span>';
00977             echo '</label></td>';
00978         }
00979     }
00980 }
00981 
00982 // User selectors for managing role assignments ================================
00983 
00987 abstract class role_assign_user_selector_base extends user_selector_base {
00988     const MAX_USERS_PER_PAGE = 100;
00989 
00990     protected $roleid;
00991     protected $context;
00992 
00997     public function __construct($name, $options) {
00998         global $CFG;
00999         if (isset($options['context'])) {
01000             $this->context = $options['context'];
01001         } else {
01002             $this->context = get_context_instance_by_id($options['contextid']);
01003         }
01004         $options['accesscontext'] = $this->context;
01005         parent::__construct($name, $options);
01006         $this->roleid = $options['roleid'];
01007         require_once($CFG->dirroot . '/group/lib.php');
01008     }
01009 
01010     protected function get_options() {
01011         global $CFG;
01012         $options = parent::get_options();
01013         $options['file'] = $CFG->admin . '/roles/lib.php';
01014         $options['roleid'] = $this->roleid;
01015         $options['contextid'] = $this->context->id;
01016         return $options;
01017     }
01018 }
01019 
01027 class potential_assignees_below_course extends role_assign_user_selector_base {
01028     public function find_users($search) {
01029         global $DB;
01030 
01031         list($enrolsql, $eparams) = get_enrolled_sql($this->context);
01032 
01033         // Now we have to go to the database.
01034         list($wherecondition, $params) = $this->search_sql($search, 'u');
01035         $params = array_merge($params, $eparams);
01036 
01037         if ($wherecondition) {
01038             $wherecondition = ' AND ' . $wherecondition;
01039         }
01040 
01041         $fields      = 'SELECT ' . $this->required_fields_sql('u');
01042         $countfields = 'SELECT COUNT(u.id)';
01043 
01044         $sql   = " FROM {user} u
01045                   WHERE u.id IN ($enrolsql) $wherecondition
01046                         AND u.id NOT IN (
01047                            SELECT r.userid
01048                              FROM {role_assignments} r
01049                             WHERE r.contextid = :contextid
01050                                   AND r.roleid = :roleid)";
01051         $order = ' ORDER BY lastname ASC, firstname ASC';
01052 
01053         $params['contextid'] = $this->context->id;
01054         $params['roleid'] = $this->roleid;
01055 
01056         // Check to see if there are too many to show sensibly.
01057         if (!$this->is_validating()) {
01058             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
01059             if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) {
01060                 return $this->too_many_results($search, $potentialmemberscount);
01061             }
01062         }
01063 
01064         // If not, show them.
01065         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
01066 
01067         if (empty($availableusers)) {
01068             return array();
01069         }
01070 
01071         if ($search) {
01072             $groupname = get_string('potusersmatching', 'role', $search);
01073         } else {
01074             $groupname = get_string('potusers', 'role');
01075         }
01076 
01077         return array($groupname => $availableusers);
01078     }
01079 }
01080 
01086 class potential_assignees_course_and_above extends role_assign_user_selector_base {
01087     public function find_users($search) {
01088         global $DB;
01089 
01090         list($wherecondition, $params) = $this->search_sql($search, '');
01091 
01092         $fields      = 'SELECT ' . $this->required_fields_sql('');
01093         $countfields = 'SELECT COUNT(1)';
01094 
01095         $sql = " FROM {user}
01096                 WHERE $wherecondition
01097                       AND id NOT IN (
01098                          SELECT r.userid
01099                            FROM {role_assignments} r
01100                           WHERE r.contextid = :contextid
01101                                 AND r.roleid = :roleid)";
01102         $order = ' ORDER BY lastname ASC, firstname ASC';
01103 
01104         $params['contextid'] = $this->context->id;
01105         $params['roleid'] = $this->roleid;
01106 
01107         if (!$this->is_validating()) {
01108             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
01109             if ($potentialmemberscount > role_assign_user_selector_base::MAX_USERS_PER_PAGE) {
01110                 return $this->too_many_results($search, $potentialmemberscount);
01111             }
01112         }
01113 
01114         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
01115 
01116         if (empty($availableusers)) {
01117             return array();
01118         }
01119 
01120         if ($search) {
01121             $groupname = get_string('potusersmatching', 'role', $search);
01122         } else {
01123             $groupname = get_string('potusers', 'role');
01124         }
01125 
01126         return array($groupname => $availableusers);
01127     }
01128 }
01129 
01134 class existing_role_holders extends role_assign_user_selector_base {
01135 
01136     public function __construct($name, $options) {
01137         parent::__construct($name, $options);
01138     }
01139 
01140     public function find_users($search) {
01141         global $DB;
01142 
01143         list($wherecondition, $params) = $this->search_sql($search, 'u');
01144         list($ctxcondition, $ctxparams) = $DB->get_in_or_equal(get_parent_contexts($this->context, true), SQL_PARAMS_NAMED, 'ctx');
01145         $params = array_merge($params, $ctxparams);
01146         $params['roleid'] = $this->roleid;
01147 
01148         $sql = "SELECT ra.id as raid," . $this->required_fields_sql('u') . ",ra.contextid,ra.component
01149                 FROM {role_assignments} ra
01150                 JOIN {user} u ON u.id = ra.userid
01151                 JOIN {context} ctx ON ra.contextid = ctx.id
01152                 WHERE
01153                     $wherecondition AND
01154                     ctx.id $ctxcondition AND
01155                     ra.roleid = :roleid
01156                 ORDER BY ctx.depth DESC, ra.component, u.lastname, u.firstname";
01157         $contextusers = $DB->get_records_sql($sql, $params);
01158 
01159         // No users at all.
01160         if (empty($contextusers)) {
01161             return array();
01162         }
01163 
01164         // We have users. Out put them in groups by context depth.
01165         // To help the loop below, tack a dummy user on the end of the results
01166         // array, to trigger output of the last group.
01167         $dummyuser = new stdClass;
01168         $dummyuser->contextid = 0;
01169         $dummyuser->id = 0;
01170         $dummyuser->component = '';
01171         $contextusers[] = $dummyuser;
01172         $results = array(); // The results array we are building up.
01173         $doneusers = array(); // Ensures we only list each user at most once.
01174         $currentcontextid = $this->context->id;
01175         $currentgroup = array();
01176         foreach ($contextusers as $user) {
01177             if (isset($doneusers[$user->id])) {
01178                 continue;
01179             }
01180             $doneusers[$user->id] = 1;
01181             if ($user->contextid != $currentcontextid) {
01182                 // We have got to the end of the previous group. Add it to the results array.
01183                 if ($currentcontextid == $this->context->id) {
01184                     $groupname = $this->this_con_group_name($search, count($currentgroup));
01185                 } else {
01186                     $groupname = $this->parent_con_group_name($search, $currentcontextid);
01187                 }
01188                 $results[$groupname] = $currentgroup;
01189                 // Get ready for the next group.
01190                 $currentcontextid = $user->contextid;
01191                 $currentgroup = array();
01192             }
01193             // Add this user to the group we are building up.
01194             unset($user->contextid);
01195             if ($currentcontextid != $this->context->id) {
01196                 $user->disabled = true;
01197             }
01198             if ($user->component !== '') {
01199                 // bad luck, you can tweak only manual role assignments
01200                 $user->disabled = true;
01201             }
01202             unset($user->component);
01203             $currentgroup[$user->id] = $user;
01204         }
01205 
01206         return $results;
01207     }
01208 
01209     protected function this_con_group_name($search, $numusers) {
01210         if ($this->context->contextlevel == CONTEXT_SYSTEM) {
01211             // Special case in the System context.
01212             if ($search) {
01213                 return get_string('extusersmatching', 'role', $search);
01214             } else {
01215                 return get_string('extusers', 'role');
01216             }
01217         }
01218         $contexttype = get_contextlevel_name($this->context->contextlevel);
01219         if ($search) {
01220             $a = new stdClass;
01221             $a->search = $search;
01222             $a->contexttype = $contexttype;
01223             if ($numusers) {
01224                 return get_string('usersinthisxmatching', 'role', $a);
01225             } else {
01226                 return get_string('noneinthisxmatching', 'role', $a);
01227             }
01228         } else {
01229             if ($numusers) {
01230                 return get_string('usersinthisx', 'role', $contexttype);
01231             } else {
01232                 return get_string('noneinthisx', 'role', $contexttype);
01233             }
01234         }
01235     }
01236 
01237     protected function parent_con_group_name($search, $contextid) {
01238         $context = get_context_instance_by_id($contextid);
01239         $contextname = print_context_name($context, true, true);
01240         if ($search) {
01241             $a = new stdClass;
01242             $a->contextname = $contextname;
01243             $a->search = $search;
01244             return get_string('usersfrommatching', 'role', $a);
01245         } else {
01246             return get_string('usersfrom', 'role', $contextname);
01247         }
01248     }
01249 }
01250 
01255 abstract class role_allow_role_page {
01256     protected $tablename;
01257     protected $targetcolname;
01258     protected $roles;
01259     protected $allowed = null;
01260 
01265     public function __construct($tablename, $targetcolname) {
01266         $this->tablename = $tablename;
01267         $this->targetcolname = $targetcolname;
01268         $this->load_required_roles();
01269     }
01270 
01274     protected function load_required_roles() {
01276         $this->roles = get_all_roles();
01277         role_fix_names($this->roles, get_context_instance(CONTEXT_SYSTEM), ROLENAME_ORIGINAL);
01278     }
01279 
01283     public function process_submission() {
01284         global $DB;
01286         $DB->delete_records($this->tablename);
01287         foreach ($this->roles as $fromroleid => $notused) {
01288             foreach ($this->roles as $targetroleid => $alsonotused) {
01289                 if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
01290                     $this->set_allow($fromroleid, $targetroleid);
01291                 }
01292             }
01293         }
01294     }
01295 
01301     protected abstract function set_allow($fromroleid, $targetroleid);
01302 
01306     public function load_current_settings() {
01307         global $DB;
01309         $this->allowed = array();
01310         foreach ($this->roles as $role) {
01311             // Make an array $role->id => false. This is probably too clever for its own good.
01312             $this->allowed[$role->id] = array_combine(array_keys($this->roles), array_fill(0, count($this->roles), false));
01313         }
01314         $rs = $DB->get_recordset($this->tablename);
01315         foreach ($rs as $allow) {
01316             $this->allowed[$allow->roleid][$allow->{$this->targetcolname}] = true;
01317         }
01318         $rs->close();
01319     }
01320 
01326     protected function is_allowed_target($targetroleid) {
01327         return true;
01328     }
01329 
01334     public function get_table() {
01335         $table = new html_table();
01336         $table->tablealign = 'center';
01337         $table->cellpadding = 5;
01338         $table->cellspacing = 0;
01339         $table->width = '90%';
01340         $table->align = array('left');
01341         $table->rotateheaders = true;
01342         $table->head = array('&#xa0;');
01343         $table->colclasses = array('');
01344 
01346         foreach ($this->roles as $targetrole) {
01347             $table->head[] = $targetrole->localname;
01348             $table->align[] = 'left';
01349             if ($this->is_allowed_target($targetrole->id)) {
01350                 $table->colclasses[] = '';
01351             } else {
01352                 $table->colclasses[] = 'dimmed_text';
01353             }
01354         }
01355 
01357         foreach ($this->roles as $fromrole) {
01358             $row = array($fromrole->localname);
01359             foreach ($this->roles as $targetrole) {
01360                 $checked = '';
01361                 $disabled = '';
01362                 if ($this->allowed[$fromrole->id][$targetrole->id]) {
01363                     $checked = 'checked="checked" ';
01364                 }
01365                 if (!$this->is_allowed_target($targetrole->id)) {
01366                     $disabled = 'disabled="disabled" ';
01367                 }
01368                 $name = 's_' . $fromrole->id . '_' . $targetrole->id;
01369                 $tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
01370                 $row[] = '<input type="checkbox" name="' . $name . '" id="' . $name .
01371                         '" title="' . $tooltip . '" value="1" ' . $checked . $disabled . '/>' .
01372                         '<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
01373             }
01374             $table->data[] = $row;
01375         }
01376 
01377         return $table;
01378     }
01379 
01384     public abstract function get_intro_text();
01385 }
01386 
01390 class role_allow_assign_page extends role_allow_role_page {
01391     public function __construct() {
01392         parent::__construct('role_allow_assign', 'allowassign');
01393     }
01394 
01395     protected function set_allow($fromroleid, $targetroleid) {
01396         allow_assign($fromroleid, $targetroleid);
01397     }
01398 
01399     protected function get_cell_tooltip($fromrole, $targetrole) {
01400         $a = new stdClass;
01401         $a->fromrole = $fromrole->localname;
01402         $a->targetrole = $targetrole->localname;
01403         return get_string('allowroletoassign', 'role', $a);
01404     }
01405 
01406     public function get_intro_text() {
01407         return get_string('configallowassign', 'admin');
01408     }
01409 }
01410 
01414 class role_allow_override_page extends role_allow_role_page {
01415     public function __construct() {
01416         parent::__construct('role_allow_override', 'allowoverride');
01417     }
01418 
01419     protected function set_allow($fromroleid, $targetroleid) {
01420         allow_override($fromroleid, $targetroleid);
01421     }
01422 
01423     protected function get_cell_tooltip($fromrole, $targetrole) {
01424         $a = new stdClass;
01425         $a->fromrole = $fromrole->localname;
01426         $a->targetrole = $targetrole->localname;
01427         return get_string('allowroletooverride', 'role', $a);
01428     }
01429 
01430     public function get_intro_text() {
01431         return get_string('configallowoverride2', 'admin');
01432     }
01433 }
01434 
01438 class role_allow_switch_page extends role_allow_role_page {
01439     protected $allowedtargetroles;
01440 
01441     public function __construct() {
01442         parent::__construct('role_allow_switch', 'allowswitch');
01443     }
01444 
01445     protected function load_required_roles() {
01446         global $DB;
01447         parent::load_required_roles();
01448         $this->allowedtargetroles = $DB->get_records_menu('role', NULL, 'id');
01449     }
01450 
01451     protected function set_allow($fromroleid, $targetroleid) {
01452         allow_switch($fromroleid, $targetroleid);
01453     }
01454 
01455     protected function is_allowed_target($targetroleid) {
01456         return isset($this->allowedtargetroles[$targetroleid]);
01457     }
01458 
01459     protected function get_cell_tooltip($fromrole, $targetrole) {
01460         $a = new stdClass;
01461         $a->fromrole = $fromrole->localname;
01462         $a->targetrole = $targetrole->localname;
01463         return get_string('allowroletoswitch', 'role', $a);
01464     }
01465 
01466     public function get_intro_text() {
01467         return get_string('configallowswitch', 'admin');
01468     }
01469 }
01470 
01483 function roles_get_potential_user_selector($context, $name, $options) {
01484         $blockinsidecourse = false;
01485         if ($context->contextlevel == CONTEXT_BLOCK) {
01486             $parentcontext = get_context_instance_by_id(get_parent_contextid($context));
01487             $blockinsidecourse = in_array($parentcontext->contextlevel, array(CONTEXT_MODULE, CONTEXT_COURSE));
01488         }
01489 
01490         if (($context->contextlevel == CONTEXT_MODULE || $blockinsidecourse) &&
01491                 !is_inside_frontpage($context)) {
01492             $potentialuserselector = new potential_assignees_below_course('addselect', $options);
01493         } else {
01494             $potentialuserselector = new potential_assignees_course_and_above('addselect', $options);
01495         }
01496     return $potentialuserselector;
01497 }
01498 
01499 class admins_potential_selector extends user_selector_base {
01504     public function __construct() {
01505         global $CFG, $USER;
01506         $admins = explode(',', $CFG->siteadmins);
01507         parent::__construct('addselect', array('multiselect'=>false, 'exclude'=>$admins));
01508     }
01509 
01510     public function find_users($search) {
01511         global $CFG, $DB;
01512         list($wherecondition, $params) = $this->search_sql($search, '');
01513 
01514         $fields      = 'SELECT ' . $this->required_fields_sql('');
01515         $countfields = 'SELECT COUNT(1)';
01516 
01517         $sql = " FROM {user}
01518                 WHERE $wherecondition AND mnethostid = :localmnet";
01519         $order = ' ORDER BY lastname ASC, firstname ASC';
01520         $params['localmnet'] = $CFG->mnet_localhost_id; // it could be dangerous to make remote users admins and also this could lead to other problems
01521 
01522         // Check to see if there are too many to show sensibly.
01523         if (!$this->is_validating()) {
01524             $potentialcount = $DB->count_records_sql($countfields . $sql, $params);
01525             if ($potentialcount > 100) {
01526                 return $this->too_many_results($search, $potentialcount);
01527             }
01528         }
01529 
01530         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
01531 
01532         if (empty($availableusers)) {
01533             return array();
01534         }
01535 
01536         if ($search) {
01537             $groupname = get_string('potusersmatching', 'role', $search);
01538         } else {
01539             $groupname = get_string('potusers', 'role');
01540         }
01541 
01542         return array($groupname => $availableusers);
01543     }
01544 
01545     protected function get_options() {
01546         global $CFG;
01547         $options = parent::get_options();
01548         $options['file'] = $CFG->admin . '/roles/lib.php';
01549         return $options;
01550     }
01551 }
01552 
01553 class admins_existing_selector extends user_selector_base {
01558     public function __construct() {
01559         global $CFG, $USER;
01560         parent::__construct('removeselect', array('multiselect'=>false));
01561     }
01562 
01563     public function find_users($search) {
01564         global $DB, $CFG;
01565         list($wherecondition, $params) = $this->search_sql($search, '');
01566 
01567         $fields      = 'SELECT ' . $this->required_fields_sql('');
01568         $countfields = 'SELECT COUNT(1)';
01569 
01570         if ($wherecondition) {
01571             $wherecondition = "$wherecondition AND id IN ($CFG->siteadmins)";
01572         } else {
01573             $wherecondition = "id IN ($CFG->siteadmins)";
01574         }
01575         $sql = " FROM {user}
01576                 WHERE $wherecondition";
01577         $order = ' ORDER BY lastname ASC, firstname ASC';
01578 
01579         $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
01580 
01581         if (empty($availableusers)) {
01582             return array();
01583         }
01584 
01585         $mainadmin = array();
01586         $adminids = explode(',', $CFG->siteadmins);
01587         foreach ($adminids as $id) {
01588             if (isset($availableusers[$id])) {
01589                 $mainadmin = array($id=>$availableusers[$id]);
01590                 unset($availableusers[$id]);
01591                 break;
01592             }
01593         }
01594 
01595         $result = array();
01596         if ($mainadmin) {
01597             $result[get_string('mainadmin', 'role')] = $mainadmin;
01598         }
01599 
01600         if ($availableusers) {
01601             if ($search) {
01602                 $groupname = get_string('extusersmatching', 'role', $search);
01603             } else {
01604                 $groupname = get_string('extusers', 'role');
01605             }
01606             $result[$groupname] = $availableusers;
01607         }
01608 
01609         return $result;
01610     }
01611 
01612     protected function get_options() {
01613         global $CFG;
01614         $options = parent::get_options();
01615         $options['file'] = $CFG->admin . '/roles/lib.php';
01616         return $options;
01617     }
01618 }
 All Data Structures Namespaces Files Functions Variables Enumerations