weekly release 2.6dev
[moodle.git] / admin / roles / define.php
CommitLineData
9e492db0 1<?php
f4acee5d 2
01a2ce80
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
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/>.
f4acee5d 17
18/**
19 * Lets the user edit role definitions.
20 *
21 * Responds to actions:
bbdb7070 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
f4acee5d 26 *
5d354ded 27 * @package core
01a2ce80
PS
28 * @subpackage role
29 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
f4acee5d 32
33 require_once(dirname(__FILE__) . '/../../config.php');
34 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
35
36 $action = required_param('action', PARAM_ALPHA);
bbdb7070 37 if (!in_array($action, array('add', 'duplicate', 'edit', 'view'))) {
38 throw new moodle_exception('invalidaccess');
39 }
f4acee5d 40 if ($action != 'add') {
1e12c120 41 $roleid = required_param('roleid', PARAM_INT);
f4acee5d 42 } else {
43 $roleid = 0;
44 }
45
46/// Get the base URL for this and related pages into a convenient variable.
47 $manageurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
bbdb7070 48 $defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
49 if ($action == 'duplicate') {
50 $baseurl = $defineurl . '?action=add';
51 } else {
52 $baseurl = $defineurl . '?action=' . $action;
53 if ($roleid) {
54 $baseurl .= '&amp;roleid=' . $roleid;
55 }
56 }
f4acee5d 57
58/// Check access permissions.
bf006d2c 59 $systemcontext = context_system::instance();
f4acee5d 60 require_login();
61 require_capability('moodle/role:manage', $systemcontext);
7a82b2af 62 admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), $defineurl);
f4acee5d 63
64/// Handle the cancel button.
65 if (optional_param('cancel', false, PARAM_BOOL)) {
66 redirect($manageurl);
67 }
68
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);
74 }
75
76/// Get some basic data we are going to need.
77 $roles = get_all_roles();
bbdb7070 78 $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
f4acee5d 79 $rolescount = count($roles);
80
f4acee5d 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);
86 } else {
87 $definitiontable = new define_role_table_basic($systemcontext, $roleid);
88 }
89 $definitiontable->read_submitted_permissions();
bbdb7070 90 if ($action == 'duplicate') {
91 $definitiontable->make_copy();
f4acee5d 92 }
93
bbdb7070 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);
99 redirect($manageurl);
100 }
f4acee5d 101
102/// Print the page header and tabs.
61ef8f9f 103 echo $OUTPUT->header();
f4acee5d 104
105 $currenttab = 'manage';
4f0c2d00 106 include('managetabs.php');
f4acee5d 107
108 if ($action == 'add') {
109 $title = get_string('addinganewrole', 'role');
bbdb7070 110 } else if ($action == 'duplicate') {
111 $title = get_string('addingrolebycopying', 'role', $rolenames[$roleid]->localname);
f4acee5d 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);
116 }
5435c9dc 117 echo $OUTPUT->heading_with_help($title, 'roles', 'role');
f4acee5d 118
bbdb7070 119/// Work out some button labels.
bed9cec8 120 if ($action == 'add' || $action == 'duplicate') {
121 $submitlabel = get_string('createthisrole', 'role');
f4acee5d 122 } else {
bbdb7070 123 $submitlabel = get_string('savechanges');
124 }
f4acee5d 125
bbdb7070 126/// On the view page, show some extra controls at the top.
f4acee5d 127 if ($action == 'view') {
414a4a91 128 echo $OUTPUT->container_start('buttons');
f4acee5d 129 $options = array();
130 $options['roleid'] = $roleid;
131 $options['action'] = 'edit';
5c2ed7e2 132 echo $OUTPUT->single_button(new moodle_url($defineurl, $options), get_string('edit'));
f4acee5d 133 $options['action'] = 'reset';
4f0c2d00 134 if ($definitiontable->get_archetype()) {
5c2ed7e2 135 echo $OUTPUT->single_button(new moodle_url($manageurl, $options), get_string('resetrole', 'role'));
f4acee5d 136 } else {
5c2ed7e2 137 echo $OUTPUT->single_button(new moodle_url($manageurl, $options), get_string('resetrolenolegacy', 'role'));
f4acee5d 138 }
139 $options['action'] = 'duplicate';
5c2ed7e2
PS
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'));
414a4a91 142 echo $OUTPUT->container_end();
f4acee5d 143 }
144
bbdb7070 145 // Start the form.
20486a5a 146 echo $OUTPUT->box_start('generalbox');
bbdb7070 147 if ($action == 'view') {
148 echo '<div class="mform">';
149 } else {
150 ?>
151<form id="rolesform" class="mform" action="<?php echo $baseurl; ?>" method="post"><div>
152<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
bbdb7070 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'); ?>" />
156</div>
157 <?php
158 }
159
160 // Print the form controls.
f4acee5d 161 $definitiontable->display();
bbdb7070 162
163/// Close the stuff we left open above.
164 if ($action == 'view') {
165 echo '</div>';
166 } else {
167 ?>
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'); ?>" />
171</div>
172</div></form>
173 <?php
174 }
20486a5a 175 echo $OUTPUT->box_end();
f4acee5d 176
bbdb7070 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>';
180 echo '</div>';
181
73d6f52f 182 echo $OUTPUT->footer();