3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Lets the user edit role definitions.
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
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(dirname(__FILE__
) . '/../../config.php');
34 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/roles/lib.php');
36 $action = required_param('action', PARAM_ALPHA
);
37 if (!in_array($action, array('add', 'duplicate', 'edit', 'view'))) {
38 throw new moodle_exception('invalidaccess');
40 if ($action != 'add') {
41 $roleid = required_param('roleid', PARAM_INTEGER
);
46 /// Get the base URL for this and related pages into a convenient variable.
47 $manageurl = $CFG->wwwroot
. '/' . $CFG->admin
. '/roles/manage.php';
48 $defineurl = $CFG->wwwroot
. '/' . $CFG->admin
. '/roles/define.php';
49 if ($action == 'duplicate') {
50 $baseurl = $defineurl . '?action=add';
52 $baseurl = $defineurl . '?action=' . $action;
54 $baseurl .= '&roleid=' . $roleid;
58 /// Check access permissions.
59 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
61 require_capability('moodle/role:manage', $systemcontext);
62 admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), $defineurl);
64 /// Handle the cancel button.
65 if (optional_param('cancel', false, PARAM_BOOL
)) {
69 /// Handle the toggle advanced mode button.
70 $showadvanced = get_user_preferences('definerole_showadvanced', false);
71 if (optional_param('toggleadvanced', false, PARAM_BOOL
)) {
72 $showadvanced = !$showadvanced;
73 set_user_preference('definerole_showadvanced', $showadvanced);
76 /// Get some basic data we are going to need.
77 $roles = get_all_roles();
78 $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL
);
79 $rolescount = count($roles);
81 /// Create the table object.
82 if ($action == 'view') {
83 $definitiontable = new view_role_definition_table($systemcontext, $roleid);
84 } else if ($showadvanced) {
85 $definitiontable = new define_role_table_advanced($systemcontext, $roleid);
87 $definitiontable = new define_role_table_basic($systemcontext, $roleid);
89 $definitiontable->read_submitted_permissions();
90 if ($action == 'duplicate') {
91 $definitiontable->make_copy();
94 /// Process submission in necessary.
95 if (optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
96 $definitiontable->save_changes();
97 add_to_log(SITEID
, 'role', $action, 'admin/roles/define.php?action=view&roleid=' .
98 $definitiontable->get_role_id(), $definitiontable->get_role_name(), '', $USER->id
);
102 /// Print the page header and tabs.
103 echo $OUTPUT->header();
105 $currenttab = 'manage';
106 include('managetabs.php');
108 if ($action == 'add') {
109 $title = get_string('addinganewrole', 'role');
110 } else if ($action == 'duplicate') {
111 $title = get_string('addingrolebycopying', 'role', $rolenames[$roleid]->localname
);
112 } else if ($action == 'view') {
113 $title = get_string('viewingdefinitionofrolex', 'role', $rolenames[$roleid]->localname
);
114 } else if ($action == 'edit') {
115 $title = get_string('editingrolex', 'role', $rolenames[$roleid]->localname
);
117 echo $OUTPUT->heading_with_help($title, 'roles', 'role');
119 /// Work out some button labels.
120 if ($action == 'add' ||
$action == 'duplicate') {
121 $submitlabel = get_string('createthisrole', 'role');
123 $submitlabel = get_string('savechanges');
126 /// On the view page, show some extra controls at the top.
127 if ($action == 'view') {
128 echo $OUTPUT->container_start('buttons');
130 $options['roleid'] = $roleid;
131 $options['action'] = 'edit';
132 echo $OUTPUT->single_button(new moodle_url($defineurl, $options), get_string('edit'));
133 $options['action'] = 'reset';
134 if ($definitiontable->get_archetype()) {
135 echo $OUTPUT->single_button(new moodle_url($manageurl, $options), get_string('resetrole', 'role'));
137 echo $OUTPUT->single_button(new moodle_url($manageurl, $options), get_string('resetrolenolegacy', 'role'));
139 $options['action'] = 'duplicate';
140 echo $OUTPUT->single_button(new moodle_url($defineurl, $options), get_string('duplicaterole', 'role'));
141 echo $OUTPUT->single_button(new moodle_url($manageurl), get_string('listallroles', 'role'));
142 echo $OUTPUT->container_end();
146 echo $OUTPUT->box_start('generalbox');
147 if ($action == 'view') {
148 echo '<div class="mform">';
151 <form id
="rolesform" class="mform" action
="<?php echo $baseurl; ?>" method
="post"><div
>
152 <input type
="hidden" name
="sesskey" value
="<?php p(sesskey()) ?>" />
153 <div
class="submit buttons">
154 <input type
="submit" name
="savechanges" value
="<?php echo $submitlabel; ?>" />
155 <input type
="submit" name
="cancel" value
="<?php print_string('cancel'); ?>" />
160 // Print the form controls.
161 $definitiontable->display();
163 /// Close the stuff we left open above.
164 if ($action == 'view') {
168 <div
class="submit buttons">
169 <input type
="submit" name
="savechanges" value
="<?php echo $submitlabel; ?>" />
170 <input type
="submit" name
="cancel" value
="<?php print_string('cancel'); ?>" />
175 echo $OUTPUT->box_end();
177 /// Print a link back to the all roles list.
178 echo '<div class="backlink">';
179 echo '<p><a href="' . $manageurl . '">' . get_string('backtoallroles', 'role') . '</a></p>';
182 echo $OUTPUT->footer();