Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/roles/manage.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 
00037     require_once(dirname(__FILE__) . '/../../config.php');
00038     require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
00039 
00040     $action = optional_param('action', '', PARAM_ALPHA);
00041     if ($action) {
00042         $roleid = required_param('roleid', PARAM_INT);
00043     }
00044 
00046     $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
00047     $defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
00048 
00050     $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00051     require_login();
00052     require_capability('moodle/role:manage', $systemcontext);
00053     admin_externalpage_setup('defineroles');
00054 
00056     $roles = get_all_roles();
00057     role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
00058 
00059     $undeletableroles = array();
00060     $undeletableroles[$CFG->notloggedinroleid] = 1;
00061     $undeletableroles[$CFG->guestroleid] = 1;
00062     $undeletableroles[$CFG->defaultuserroleid] = 1;
00063 
00065     $confirmed = optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey();
00066     switch ($action) {
00067         case 'delete':
00068             if (isset($undeletableroles[$roleid])) {
00069                 print_error('cannotdeletethisrole', '', $baseurl);
00070             }
00071             if (!$confirmed) {
00072                 // show confirmation
00073                 echo $OUTPUT->header();
00074                 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
00075                 $a = new stdClass();
00076                 $a->id = $roleid;
00077                 $a->name = $roles[$roleid]->name;
00078                 $a->shortname = $roles[$roleid]->shortname;
00079                 $a->count = $DB->count_records('role_assignments', array('roleid'=>$roleid));
00080 
00081                 $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
00082                 $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
00083                 echo $OUTPUT->confirm(get_string('deleterolesure', 'role', $a), $formcontinue, $formcancel);
00084                 echo $OUTPUT->footer();
00085                 die;
00086             }
00087             if (!delete_role($roleid)) {
00088                 // The delete failed, but mark the context dirty in case.
00089                 mark_context_dirty($systemcontext->path);
00090                 print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
00091             }
00092             // Deleted a role sitewide...
00093             mark_context_dirty($systemcontext->path);
00094             add_to_log(SITEID, 'role', 'delete', 'admin/roles/manage.php', $roles[$roleid]->localname, '', $USER->id);
00095             redirect($baseurl);
00096             break;
00097 
00098         case 'moveup':
00099             if (confirm_sesskey()) {
00100                 $prevrole = null;
00101                 $thisrole = null;
00102                 foreach ($roles as $role) {
00103                     if ($role->id == $roleid) {
00104                         $thisrole = $role;
00105                         break;
00106                     } else {
00107                         $prevrole = $role;
00108                     }
00109                 }
00110                 if (is_null($thisrole) || is_null($prevrole)) {
00111                     print_error('cannotmoverolewithid', 'error', '', $roleid);
00112                 }
00113                 if (!switch_roles($thisrole, $prevrole)) {
00114                     print_error('cannotmoverolewithid', 'error', '', $roleid);
00115                 }
00116             }
00117 
00118             redirect($baseurl);
00119             break;
00120 
00121         case 'movedown':
00122             if (confirm_sesskey()) {
00123                 $thisrole = null;
00124                 $nextrole = null;
00125                 foreach ($roles as $role) {
00126                     if ($role->id == $roleid) {
00127                         $thisrole = $role;
00128                     } else if (!is_null($thisrole)) {
00129                         $nextrole = $role;
00130                         break;
00131                     }
00132                 }
00133                 if (is_null($nextrole)) {
00134                     print_error('cannotmoverolewithid', 'error', '', $roleid);
00135                 }
00136                 if (!switch_roles($thisrole, $nextrole)) {
00137                     print_error('cannotmoverolewithid', 'error', '', $roleid);
00138                 }
00139             }
00140 
00141             redirect($baseurl);
00142             break;
00143 
00144         case 'reset':
00145             if (!$confirmed) {
00146                 // show confirmation
00147                 echo $OUTPUT->header();
00148                 $optionsyes = array('action'=>'reset', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
00149                 $optionsno  = array('action'=>'view', 'roleid'=>$roleid);
00150                 $a = new stdClass();
00151                 $a->id = $roleid;
00152                 $a->name = $roles[$roleid]->name;
00153                 $a->shortname = $roles[$roleid]->shortname;
00154                 $a->legacytype = $roles[$roleid]->archetype;
00155                 if (empty($a->legacytype)) {
00156                     $warning = get_string('resetrolesurenolegacy', 'role', $a);
00157                 } else {
00158                     $warning = get_string('resetrolesure', 'role', $a);
00159                 }
00160                 $formcontinue = new single_button(new moodle_url('manage.php', $optionsyes), get_string('yes'));
00161                 $formcancel = new single_button(new moodle_url('manage.php', $optionsno), get_string('no'), 'get');
00162                 echo $OUTPUT->confirm($warning, $formcontinue, $formcancel);
00163                 echo $OUTPUT->footer();
00164                 die;
00165             }
00166 
00167             // Reset context levels for standard archetypes
00168             if ($roles[$roleid]->archetype) {
00169                 set_role_contextlevels($roleid, get_default_contextlevels($roles[$roleid]->archetype));
00170             }
00171 
00172             //reset or delete the capabilities
00173             reset_role_capabilities($roleid);
00174 
00175             // Mark context dirty, log and redirect.
00176             mark_context_dirty($systemcontext->path);
00177             add_to_log(SITEID, 'role', 'reset', 'admin/roles/manage.php?action=reset&roleid=' . $roleid, $roles[$roleid]->localname, '', $USER->id);
00178             redirect($defineurl . '?action=view&roleid=' . $roleid);
00179             break;
00180     }
00181 
00183     echo $OUTPUT->header();
00184 
00185     $currenttab = 'manage';
00186     include_once('managetabs.php');
00187 
00189     $table = new html_table();
00190     $table->tablealign = 'center';
00191     $table->align = array('left', 'left', 'left', 'left');
00192     $table->wrap = array('nowrap', '', 'nowrap','nowrap');
00193     $table->width = '90%';
00194     $table->head = array(
00195         get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'role'),
00196         get_string('description'),
00197         get_string('roleshortname', 'role'),
00198         get_string('edit')
00199     );
00200 
00202     $stredit = get_string('edit');
00203     $strduplicate = get_string('duplicate');
00204     $strdelete = get_string('delete');
00205     $strmoveup = get_string('moveup');
00206     $strmovedown = get_string('movedown');
00207 
00209     $table->data = array();
00210     $firstrole = reset($roles);
00211     $lastrole = end($roles);
00212     foreach ($roles as $role) {
00213 
00215         $row = array(
00216             '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
00217             format_text($role->description, FORMAT_HTML),
00218             s($role->shortname),
00219             '',
00220         );
00221 
00223         // move up
00224         if ($role->sortorder != $firstrole->sortorder) {
00225             $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
00226         } else {
00227             $row[3] .= get_spacer();
00228         }
00229         // move down
00230         if ($role->sortorder != $lastrole->sortorder) {
00231             $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
00232         } else {
00233             $row[3] .= get_spacer();
00234         }
00235         // edit
00236         $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
00237                 'edit', $stredit, get_string('editxrole', 'role', $role->localname));
00238         // duplicate
00239         $row[3] .= get_action_icon($defineurl . '?action=duplicate&amp;roleid=' . $role->id,
00240                 'copy', $strduplicate, get_string('createrolebycopying', 'role', $role->localname));
00241         // delete
00242         if (isset($undeletableroles[$role->id])) {
00243             $row[3] .= get_spacer();
00244         } else {
00245             $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
00246                   'delete', $strdelete, get_string('deletexrole', 'role', $role->localname));
00247         }
00248 
00249         $table->data[] = $row;
00250     }
00251     echo html_writer::table($table);
00252 
00253     echo $OUTPUT->container_start('buttons');
00254     echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'role'), 'get');
00255     echo $OUTPUT->container_end();
00256 
00257     echo $OUTPUT->footer();
00258     die;
00259 
00260 function get_action_icon($url, $icon, $alt, $tooltip) {
00261     global $OUTPUT;
00262     return '<a title="' . $tooltip . '" href="'. $url . '">' .
00263             '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
00264 }
00265 function get_spacer() {
00266     global $OUTPUT;
00267     return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
00268 }
 All Data Structures Namespaces Files Functions Variables Enumerations