3 require_once('../../config.php');
5 require_once($CFG->libdir
.'/adminlib.php');
7 admin_externalpage_setup('defineroles');
9 $roleid = optional_param('roleid', 0, PARAM_INT
); // if set, we are editing a role
10 $name = optional_param('name', '', PARAM_MULTILANG
); // new role name
11 $shortname = optional_param('shortname', '', PARAM_RAW
); // new role shortname, special cleaning before storage
12 $description = optional_param('description', '', PARAM_CLEAN
); // new role desc
13 $action = optional_param('action', '', PARAM_ALPHA
);
14 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
15 $cancel = optional_param('cancel', 0, PARAM_BOOL
);
17 $sitecontext = get_context_instance(CONTEXT_SYSTEM
);
19 require_capability('moodle/role:manage', $sitecontext);
22 redirect('manage.php');
28 $roles = get_all_roles();
29 $rolescount = count($roles);
31 /// fix sort order if needed
34 foreach ($roles as $rolex) {
35 $rolesort[$i] = $rolex->id
;
36 if ($rolex->sortorder
!= $i) {
40 update_record('role', $r);
41 $roles[$rolex->id
]->sortorder
= $i;
46 // do not delete these default system roles
47 $defaultroles = array();
48 $defaultroles[] = $CFG->notloggedinroleid
;
49 $defaultroles[] = $CFG->guestroleid
;
50 $defaultroles[] = $CFG->defaultuserroleid
;
51 $defaultroles[] = $CFG->defaultcourseroleid
;
53 /// form processing, editing a role, adding a role, deleting a role etc.
56 if ($data = data_submitted() and confirm_sesskey()) {
58 $shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR
)); // only lowercase safe ASCII characters
59 $legacytype = required_param('legacytype', PARAM_RAW
);
61 $legacyroles = get_legacy_roles();
62 if (!array_key_exists($legacytype, $legacyroles)) {
67 $errors['name'] = get_string('errorbadrolename', 'role');
68 } else if (count_records('role', 'name', $name)) {
69 $errors['name'] = get_string('errorexistsrolename', 'role');
72 if (empty($shortname)) {
73 $errors['shortname'] = get_string('errorbadroleshortname', 'role');
74 } else if (count_records('role', 'shortname', $shortname)) {
75 $errors['shortname'] = get_string('errorexistsroleshortname', 'role');
79 $newroleid = create_role($name, $shortname, $description);
81 // set proper legacy type
82 if (!empty($legacytype)) {
83 assign_capability($legacyroles[$legacytype], CAP_ALLOW
, $newroleid, $sitecontext->id
);
87 $newrole = new object();
88 $newrole->name
= $name;
89 $newrole->shortname
= $shortname;
90 $newrole->description
= $description;
91 $newrole->legacytype
= $legacytype;
94 $allowed_values = array(CAP_INHERIT
, CAP_ALLOW
, CAP_PREVENT
, CAP_PROHIBIT
);
95 $capabilities = fetch_context_capabilities($sitecontext); // capabilities applicable in this context
97 foreach ($capabilities as $cap) {
98 if (!isset($data->{$cap->name
})) {
102 // legacy caps have their own selector
103 if (islegacy($data->{$cap->name
})) {
107 $capname = $cap->name
;
108 $value = clean_param($data->{$cap->name
}, PARAM_INT
);
109 if (!in_array($value, $allowed_values)) {
113 if (empty($errors)) {
114 assign_capability($capname, $value, $newroleid, $sitecontext->id
);
116 $newrole->$capname = $value;
120 if (empty($errors)) {
121 redirect('manage.php');
127 if ($data = data_submitted() and confirm_sesskey()) {
129 $shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR
)); // only lowercase safe ASCII characters
130 $legacytype = required_param('legacytype', PARAM_RAW
);
132 $legacyroles = get_legacy_roles();
133 if (!array_key_exists($legacytype, $legacyroles)) {
138 $errors['name'] = get_string('errorbadrolename', 'role');
139 } else if ($rs = get_records('role', 'name', $name)) {
142 $errors['name'] = get_string('errorexistsrolename', 'role');
146 if (empty($shortname)) {
147 $errors['shortname'] = get_string('errorbadroleshortname', 'role');
148 } else if ($rs = get_records('role', 'shortname', $shortname)) {
151 $errors['shortname'] = get_string('errorexistsroleshortname', 'role');
154 if (!empty($errors)) {
155 $newrole = new object();
156 $newrole->name
= $name;
157 $newrole->shortname
= $shortname;
158 $newrole->description
= $description;
159 $newrole->legacytype
= $legacytype;
162 $allowed_values = array(CAP_INHERIT
, CAP_ALLOW
, CAP_PREVENT
, CAP_PROHIBIT
);
163 $capabilities = fetch_context_capabilities($sitecontext); // capabilities applicable in this context
165 foreach ($capabilities as $cap) {
166 if (!isset($data->{$cap->name
})) {
170 // legacy caps have their own selector
171 if (islegacy($data->{$cap->name
}) === 0 ) {
175 $capname = $cap->name
;
176 $value = clean_param($data->{$cap->name
}, PARAM_INT
);
177 if (!in_array($value, $allowed_values)) {
181 if (!empty($errors)) {
182 $newrole->$capname = $value;
187 $SQL = "SELECT * FROM {$CFG->prefix}role_capabilities
188 WHERE roleid = $roleid AND capability = '$capname'
189 AND contextid = $sitecontext->id";
191 $localoverride = get_record_sql($SQL);
193 if ($localoverride) { // update current overrides
194 if ($value == CAP_INHERIT
) { // inherit = delete
195 unassign_capability($capname, $roleid, $sitecontext->id
);
198 $localoverride->permission
= $value;
199 $localoverride->timemodified
= time();
200 $localoverride->modifierid
= $USER->id
;
201 update_record('role_capabilities', $localoverride);
203 } else { // insert a record
204 if ($value != CAP_INHERIT
) {
205 assign_capability($capname, $value, $roleid, $sitecontext->id
);
211 if (empty($errors)) {
212 // update normal role settings
215 $role->shortname
= $shortname;
216 $role->description
= $description;
218 if (!update_record('role', $role)) {
219 error('Could not update role!');
222 // set proper legacy type
223 foreach($legacyroles as $ltype=>$lcap) {
224 if ($ltype == $legacytype) {
225 assign_capability($lcap, CAP_ALLOW
, $roleid, $sitecontext->id
);
227 unassign_capability($lcap, $roleid);
231 redirect('manage.php');
237 if (in_array($roleid, $defaultroles)) {
238 error('This role is used as one of the default system roles, it can not be deleted');
240 if ($confirm and data_submitted() and confirm_sesskey()) {
241 if (!delete_role($roleid)) {
242 error('Could not delete role with ID '.$roleid);
245 } else if (confirm_sesskey()){
247 admin_externalpage_print_header();
248 $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
251 $a->name
= $roles[$roleid]->name
;
252 $a->shortname
= $roles[$roleid]->shortname
;
253 $a->count
= (int)count_records('role_assignments', 'roleid', $roleid);
254 notice_yesno(get_string('deleterolesure', 'role', $a), 'manage.php', 'manage.php', $optionsyes, NULL, 'post', 'get');
255 admin_externalpage_print_footer();
259 redirect('manage.php');
263 if (array_key_exists($roleid, $roles) and confirm_sesskey()) {
264 $role = $roles[$roleid];
265 if ($role->sortorder
> 0) {
266 $above = $roles[$rolesort[$role->sortorder
- 1]];
268 if (!switch_roles($role, $above)) {
269 error("Cannot move role with ID $roleid");
274 redirect('manage.php');
278 if (array_key_exists($roleid, $roles) and confirm_sesskey()) {
279 $role = $roles[$roleid];
280 if ($role->sortorder +
1 < $rolescount) {
281 $below = $roles[$rolesort[$role->sortorder +
1]];
283 if (!switch_roles($role, $below)) {
284 error("Cannot move role with ID $roleid");
289 redirect('manage.php');
293 if (!array_key_exists($roleid, $roles)) {
294 redirect('manage.php');
297 if ($confirm and data_submitted() and confirm_sesskey()) {
298 //ok - lets duplicate!
301 admin_externalpage_print_header();
302 $optionsyes = array('action'=>'duplicate', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
303 $optionsno = array('action'=>'view', 'roleid'=>$roleid);
306 $a->name
= $roles[$roleid]->name
;
307 $a->shortname
= $roles[$roleid]->shortname
;
308 notice_yesno(get_string('duplicaterolesure', 'role', $a), 'manage.php', 'manage.php', $optionsyes, $optionsno, 'post', 'get');
309 admin_externalpage_print_footer();
313 // duplicate current role
314 $sourcerole = get_record('role','id',$roleid);
316 $fullname = $sourcerole->name
;
317 $shortname = $sourcerole->shortname
;
318 $currentfullname = "";
319 $currentshortname = "";
322 // find a name for the duplicated role
325 $suffixfull = " ".get_string("copyasnoun")." ".$counter;
326 $suffixshort = "_".$counter;
331 $currentfullname = $fullname.$suffixfull;
332 // Limit the size of shortname - database column accepts <= 15 chars
333 $currentshortname = substr($shortname, 0, 15 - strlen($suffixshort)).$suffixshort;
334 $coursefull = get_record("role","name",addslashes($currentfullname));
335 $courseshort = get_record("role","shortname",addslashes($currentshortname));
337 } while ($coursefull ||
$courseshort);
339 $description = 'duplicate of '.$fullname;
340 if ($newrole = create_role($currentfullname, $currentshortname, $description)) {
341 // dupilcate all the capabilities
342 role_cap_duplicate($sourcerole, $newrole);
344 redirect('manage.php');
348 if (!array_key_exists($roleid, $roles)) {
349 redirect('manage.php');
352 if ($confirm and data_submitted() and confirm_sesskey()) {
353 reset_role_capabilities($roleid);
354 redirect('manage.php?action=view&roleid='.$roleid);
358 admin_externalpage_print_header();
359 $optionsyes = array('action'=>'reset', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
360 $optionsno = array('action'=>'view', 'roleid'=>$roleid);
363 $a->name
= $roles[$roleid]->name
;
364 $a->shortname
= $roles[$roleid]->shortname
;
365 $a->legacytype
= get_legacy_type($roleid);
366 if (empty($a->legacytype
)) {
367 $warning = get_string('resetrolesurenolegacy', 'role', $a);
369 $warning = get_string('resetrolesure', 'role', $a);
371 notice_yesno($warning, 'manage.php', 'manage.php', $optionsyes, $optionsno, 'post', 'get');
372 admin_externalpage_print_footer();
384 admin_externalpage_print_header();
386 $currenttab = 'manage';
387 include_once('managetabs.php');
389 if (($roleid and ($action == 'view' or $action == 'edit')) or $action == 'add') { // view or edit role details
391 if ($action == 'add') {
393 if (empty($errors) or empty($newrole)) {
394 $role = new object();
396 $role->shortname
= '';
397 $role->description
= '';
398 $role->legacytype
= '';
400 $role = stripslashes_safe($newrole);
402 } else if ($action == 'edit' and !empty($errors) and !empty($newrole)) {
403 $role = stripslashes_safe($newrole);
405 if(!$role = get_record('role', 'id', $roleid)) {
406 error('Incorrect role ID!');
408 $role->legacytype
= get_legacy_type($role->id
);
411 foreach ($roles as $rolex) {
412 $roleoptions[$rolex->id
] = strip_tags(format_string($rolex->name
));
415 // this is the array holding capabilities of this role sorted till this context
416 $r_caps = role_context_capabilities($roleid, $sitecontext);
418 // this is the available capabilities assignable in this context
419 $capabilities = fetch_context_capabilities($sitecontext);
421 $usehtmleditor = can_use_html_editor();
425 print_heading_with_help(get_string('addrole', 'role'), 'roles');
428 print_heading_with_help(get_string('viewrole', 'role'), 'roles');
431 print_heading_with_help(get_string('editrole', 'role'), 'roles');
435 echo '<div class="selector">';
436 if ($action == 'view') {
437 popup_form('manage.php?action=view&roleid=', $roleoptions, 'switchrole', $roleid, '', '', '',
438 false, 'self', get_string('selectrole', 'role'));
440 echo '<div class="buttons">';
442 $legacytype = get_legacy_type($roleid);
444 $options['roleid'] = $roleid;
445 $options['action'] = 'edit';
446 print_single_button('manage.php', $options, get_string('edit'));
447 $options['action'] = 'reset';
448 if (empty($legacytype)) {
449 print_single_button('manage.php', $options, get_string('resetrolenolegacy', 'role'));
451 print_single_button('manage.php', $options, get_string('resetrole', 'role'));
453 $options['action'] = 'duplicate';
454 print_single_button('manage.php', $options, get_string('duplicaterole', 'role'));
455 print_single_button('manage.php', null, get_string('listallroles', 'role'));
460 $lang = str_replace('_utf8', '', current_language());
462 print_simple_box_start('center');
463 include_once('manage.html');
464 print_simple_box_end();
466 if ($usehtmleditor) {
467 use_html_editor('description');
472 print_heading_with_help(get_string('roles', 'role'), 'roles');
476 $table->tablealign
= 'center';
477 $table->align
= array('right', 'left', 'left', 'left');
478 $table->wrap
= array('nowrap', '', 'nowrap','nowrap');
479 $table->cellpadding
= 5;
480 $table->cellspacing
= 0;
481 $table->width
= '90%';
482 $table->data
= array();
484 $table->head
= array(get_string('name'),
485 get_string('description'),
486 get_string('shortname'),
489 /*************************
490 * List all current roles *
491 **************************/
493 foreach ($roles as $role) {
495 $stredit = get_string('edit');
496 $strdelete = get_string('delete');
497 $strmoveup = get_string('moveup');
498 $strmovedown = get_string('movedown');
501 $row[0] = '<a href="manage.php?roleid='.$role->id
.'&action=view">'.format_string($role->name
).'</a>';
502 $row[1] = format_text($role->description
, FORMAT_HTML
);
503 $row[2] = s($role->shortname
);
504 $row[3] = '<a title="'.$stredit.'" href="manage.php?action=edit&roleid='.$role->id
.'">'.
505 '<img src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> ';
506 if (in_array($role->id
, $defaultroles)) {
507 $row[3] .= '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
509 $row[3] .= '<a title="'.$strdelete.'" href="manage.php?action=delete&roleid='.$role->id
.'&sesskey='.sesskey().'">'.
510 '<img src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
512 if ($role->sortorder
!= 0) {
513 $row[3] .= '<a title="'.$strmoveup.'" href="manage.php?action=moveup&roleid='.$role->id
.'&sesskey='.sesskey().'">'.
514 '<img src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
516 $row[3] .= '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
518 if ($role->sortorder+
1 < $rolescount) {
519 $row[3] .= '<a title="'.$strmovedown.'" href="manage.php?action=movedown&roleid='.$role->id
.'&sesskey='.sesskey().'">'.
520 '<img src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
522 $row[3] .= '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
525 $table->data
[] = $row;
530 $options = new object();
531 $options->action
= 'add';
532 echo '<div class="buttons">';
533 print_single_button('manage.php', $options, get_string('addrole', 'role'), 'get');
537 admin_externalpage_print_footer();
541 /// ================ some internal functions ====================////
543 function switch_roles($first, $second) {
545 //first find temorary sortorder number
546 $tempsort = count_records('role') +
3;
547 while (get_record('role','sortorder', $tempsort)) {
552 $r1->id
= $first->id
;
553 $r1->sortorder
= $tempsort;
555 $r2->id
= $second->id
;
556 $r2->sortorder
= $first->sortorder
;
558 if (!update_record('role', $r1)) {
559 debugging("Can not update role with ID $r1->id!");
563 if (!update_record('role', $r2)) {
564 debugging("Can not update role with ID $r2->id!");
568 $r1->sortorder
= $second->sortorder
;
569 if (!update_record('role', $r1)) {
570 debugging("Can not update role with ID $r1->id!");
577 // duplicates all the base definitions of a role
578 function role_cap_duplicate($sourcerole, $targetrole) {
580 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
581 $caps = get_records_sql("SELECT * FROM {$CFG->prefix}role_capabilities
582 WHERE roleid = $sourcerole->id
583 AND contextid = $systemcontext->id");
584 // adding capabilities
585 foreach ($caps as $cap) {
587 $cap->roleid
= $targetrole;
588 insert_record('role_capabilities', $cap);