MDL-21198 towards origianl single_button() syntax + other improvements
[moodle.git] / admin / roles / define.php
CommitLineData
9e492db0 1<?php
f4acee5d 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * Lets the user edit role definitions.
28 *
29 * Responds to actions:
bbdb7070 30 * add - add a new role
31 * duplicate - like add, only initialise the new role by using an existing one.
32 * edit - edit the definition of a role
33 * view - view the definition of a role
f4acee5d 34 *
35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36 * @package roles
37 *//** */
38
39 require_once(dirname(__FILE__) . '/../../config.php');
40 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
41
42 $action = required_param('action', PARAM_ALPHA);
bbdb7070 43 if (!in_array($action, array('add', 'duplicate', 'edit', 'view'))) {
44 throw new moodle_exception('invalidaccess');
45 }
f4acee5d 46 if ($action != 'add') {
47 $roleid = required_param('roleid', PARAM_INTEGER);
48 } else {
49 $roleid = 0;
50 }
51
52/// Get the base URL for this and related pages into a convenient variable.
53 $manageurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
bbdb7070 54 $defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
55 if ($action == 'duplicate') {
56 $baseurl = $defineurl . '?action=add';
57 } else {
58 $baseurl = $defineurl . '?action=' . $action;
59 if ($roleid) {
60 $baseurl .= '&amp;roleid=' . $roleid;
61 }
62 }
f4acee5d 63
64/// Check access permissions.
65 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
66 require_login();
67 require_capability('moodle/role:manage', $systemcontext);
7a82b2af 68 admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), $defineurl);
f4acee5d 69
70/// Handle the cancel button.
71 if (optional_param('cancel', false, PARAM_BOOL)) {
72 redirect($manageurl);
73 }
74
75/// Handle the toggle advanced mode button.
76 $showadvanced = get_user_preferences('definerole_showadvanced', false);
77 if (optional_param('toggleadvanced', false, PARAM_BOOL)) {
78 $showadvanced = !$showadvanced;
79 set_user_preference('definerole_showadvanced', $showadvanced);
80 }
81
82/// Get some basic data we are going to need.
83 $roles = get_all_roles();
bbdb7070 84 $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
f4acee5d 85 $rolescount = count($roles);
86
f4acee5d 87/// Create the table object.
88 if ($action == 'view') {
89 $definitiontable = new view_role_definition_table($systemcontext, $roleid);
90 } else if ($showadvanced) {
91 $definitiontable = new define_role_table_advanced($systemcontext, $roleid);
92 } else {
93 $definitiontable = new define_role_table_basic($systemcontext, $roleid);
94 }
95 $definitiontable->read_submitted_permissions();
bbdb7070 96 if ($action == 'duplicate') {
97 $definitiontable->make_copy();
f4acee5d 98 }
99
bbdb7070 100/// Process submission in necessary.
101 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
102 $definitiontable->save_changes();
103 add_to_log(SITEID, 'role', $action, 'admin/roles/define.php?action=view&roleid=' .
104 $definitiontable->get_role_id(), $definitiontable->get_role_name(), '', $USER->id);
105 redirect($manageurl);
106 }
f4acee5d 107
108/// Print the page header and tabs.
109 admin_externalpage_print_header();
110
111 $currenttab = 'manage';
112 include_once('managetabs.php');
113
114 if ($action == 'add') {
115 $title = get_string('addinganewrole', 'role');
bbdb7070 116 } else if ($action == 'duplicate') {
117 $title = get_string('addingrolebycopying', 'role', $rolenames[$roleid]->localname);
f4acee5d 118 } else if ($action == 'view') {
119 $title = get_string('viewingdefinitionofrolex', 'role', $rolenames[$roleid]->localname);
120 } else if ($action == 'edit') {
121 $title = get_string('editingrolex', 'role', $rolenames[$roleid]->localname);
122 }
4bcc5118 123 echo $OUTPUT->heading_with_help($title, 'roles');
f4acee5d 124
bbdb7070 125/// Work out some button labels.
bed9cec8 126 if ($action == 'add' || $action == 'duplicate') {
127 $submitlabel = get_string('createthisrole', 'role');
f4acee5d 128 } else {
bbdb7070 129 $submitlabel = get_string('savechanges');
130 }
f4acee5d 131
bbdb7070 132/// On the view page, show some extra controls at the top.
f4acee5d 133 if ($action == 'view') {
414a4a91 134 echo $OUTPUT->container_start('buttons');
f4acee5d 135 $options = array();
136 $options['roleid'] = $roleid;
137 $options['action'] = 'edit';
414a4a91 138 echo $OUTPUT->button(html_form::make_button($defineurl, $options, get_string('edit')));
f4acee5d 139 $options['action'] = 'reset';
bbdb7070 140 if ($definitiontable->get_legacy_type()) {
414a4a91 141 echo $OUTPUT->button(html_form::make_button($manageurl, $options, get_string('resetrole', 'role')));
f4acee5d 142 } else {
414a4a91 143 echo $OUTPUT->button(html_form::make_button($manageurl, $options, get_string('resetrolenolegacy', 'role')));
f4acee5d 144 }
145 $options['action'] = 'duplicate';
414a4a91 146 echo $OUTPUT->button(html_form::make_button($defineurl, $options, get_string('duplicaterole', 'role')));
147 echo $OUTPUT->button(html_form::make_button($manageurl, null, get_string('listallroles', 'role')));
148 echo $OUTPUT->container_end();
f4acee5d 149 }
150
bbdb7070 151 // Start the form.
20486a5a 152 echo $OUTPUT->box_start('generalbox');
bbdb7070 153 if ($action == 'view') {
154 echo '<div class="mform">';
155 } else {
156 ?>
157<form id="rolesform" class="mform" action="<?php echo $baseurl; ?>" method="post"><div>
158<input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
bbdb7070 159<div class="submit buttons">
160 <input type="submit" name="savechanges" value="<?php echo $submitlabel; ?>" />
161 <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
162</div>
163 <?php
164 }
165
166 // Print the form controls.
f4acee5d 167 $definitiontable->display();
bbdb7070 168
169/// Close the stuff we left open above.
170 if ($action == 'view') {
171 echo '</div>';
172 } else {
173 ?>
174<div class="submit buttons">
175 <input type="submit" name="savechanges" value="<?php echo $submitlabel; ?>" />
176 <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
177</div>
178</div></form>
179 <?php
180 }
20486a5a 181 echo $OUTPUT->box_end();
f4acee5d 182
bbdb7070 183/// Print a link back to the all roles list.
184 echo '<div class="backlink">';
185 echo '<p><a href="' . $manageurl . '">' . get_string('backtoallroles', 'role') . '</a></p>';
186 echo '</div>';
187
73d6f52f 188 echo $OUTPUT->footer();