MDL-29794 Shared grading form templates can be deleted now
[moodle.git] / grade / grading / manage.php
CommitLineData
6832a102
DM
1<?php
2
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/>.
17
18/**
9c9cee4e
DM
19 * A single gradable area management page
20 *
21 * This page alows the user to set the current active method in the given
22 * area, provides access to the plugin editor and allows user to save the
23 * current form as a template or re-use some existing form.
24 *
6832a102
DM
25 * @package core
26 * @subpackage grading
27 * @copyright 2011 David Mudrak <david@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30
31require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
32require_once($CFG->dirroot.'/grade/grading/lib.php');
33
fde33804 34// identify gradable area by its id
71bd9d20 35$areaid = optional_param('areaid', null, PARAM_INT);
fde33804 36// alternatively the context, component and areaname must be provided
71bd9d20
DM
37$contextid = optional_param('contextid', null, PARAM_INT);
38$component = optional_param('component', null, PARAM_COMPONENT);
39$area = optional_param('area', null, PARAM_AREA);
fde33804 40// keep the caller's URL so that we know where to send the user finally
71bd9d20 41$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
fde33804 42// active method selector
71bd9d20 43$setmethod = optional_param('setmethod', null, PARAM_PLUGIN);
fde33804
DM
44// publish the given form definition as a new template in the forms bank
45$shareform = optional_param('shareform', null, PARAM_INT);
671ec8f5
DM
46// delete the given form definition
47$deleteform = optional_param('deleteform', null, PARAM_INT);
fde33804
DM
48// consider the required action as confirmed
49$confirmed = optional_param('confirmed', false, PARAM_BOOL);
50// a message to display, typically a previous action's result
51$message = optional_param('message', null, PARAM_NOTAGS);
6832a102
DM
52
53if (!is_null($areaid)) {
54 // get manager by id
55 $manager = get_grading_manager($areaid);
56} else {
57 // get manager by context and component
98a73bb7 58 if (is_null($contextid) or is_null($component) or is_null($area)) {
6832a102
DM
59 throw new coding_exception('The caller script must identify the gradable area.');
60 }
61 $context = get_context_instance_by_id($contextid, MUST_EXIST);
62 $manager = get_grading_manager($context, $component, $area);
63}
64
9c9cee4e
DM
65if ($manager->get_context()->contextlevel < CONTEXT_COURSE) {
66 throw new coding_exception('Unsupported gradable area context level');
67}
fde33804 68
9c9cee4e
DM
69// get the currently active method
70$method = $manager->get_active_method();
fde33804 71
9c9cee4e 72list($context, $course, $cm) = get_context_info_array($manager->get_context()->id);
fde33804 73
9c9cee4e
DM
74require_login($course, true, $cm);
75require_capability('moodle/grade:managegradingforms', $context);
6832a102 76
9c9cee4e
DM
77if (!empty($returnurl)) {
78 $returnurl = new moodle_url($returnurl);
6832a102 79} else {
9c9cee4e 80 $returnurl = null;
6832a102
DM
81}
82
98a73bb7 83$PAGE->set_url($manager->get_management_url($returnurl));
6832a102
DM
84navigation_node::override_active_url($manager->get_management_url());
85$PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
86$PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
87$output = $PAGE->get_renderer('core_grading');
88
6832a102 89// process the eventual change of the active grading method
71bd9d20 90if (!empty($setmethod)) {
6832a102 91 require_sesskey();
71bd9d20 92 if ($setmethod == 'none') {
6832a102 93 // here we expect that noone would actually want to call their plugin as 'none'
71bd9d20 94 $setmethod = null;
6832a102 95 }
71bd9d20 96 $manager->set_active_method($setmethod);
6832a102
DM
97 redirect($PAGE->url);
98}
99
fde33804
DM
100// publish the form as a template
101if (!empty($shareform)) {
102 require_capability('moodle/grade:sharegradingforms', get_system_context());
103 $controller = $manager->get_controller($method);
104 $definition = $controller->get_definition();
105 if (!$confirmed) {
106 // let the user confirm they understand what they are doing (haha ;-)
107 echo $output->header();
108 echo $output->confirm(get_string('manageactionshareconfirm', 'core_grading', s($definition->name)),
109 new moodle_url($PAGE->url, array('shareform' => $shareform, 'confirmed' => 1)),
110 $PAGE->url);
111 echo $output->footer();
112 die();
113 } else {
114 require_sesskey();
115 $newareaid = $manager->create_shared_area($method);
116 $targetarea = get_grading_manager($newareaid);
117 $targetcontroller = $targetarea->get_controller($method);
7622ae95
DM
118 $targetcontroller->update_definition($controller->get_definition_copy($targetcontroller),
119 gradingform_controller::DEFINITION_STATUS_READY);
fde33804
DM
120 redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactionsharedone', 'core_grading'))));
121 }
122}
123
671ec8f5
DM
124// delete the form definition
125if (!empty($deleteform)) {
126 $controller = $manager->get_controller($method);
127 $definition = $controller->get_definition();
128 if (!$confirmed) {
129 // let the user confirm they understand the consequences (also known as WTF-effect)
130 echo $output->header();
131 echo $output->confirm(markdown_to_html(get_string('manageactiondeleteconfirm', 'core_grading', array(
132 'formname' => s($definition->name),
133 'component' => $manager->get_component_title(),
134 'area' => $manager->get_area_title()))),
135 new moodle_url($PAGE->url, array('deleteform' => $deleteform, 'confirmed' => 1)), $PAGE->url);
136 echo $output->footer();
137 die();
138 } else {
139 require_sesskey();
140 $controller->delete_definition();
141 redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactiondeletedone', 'core_grading'))));
142 }
143}
144
6832a102 145echo $output->header();
fde33804
DM
146
147if (!empty($message)) {
148 echo $output->management_message($message);
149}
150
6832a102
DM
151echo $output->heading(get_string('gradingmanagementtitle', 'core_grading', array(
152 'component' => $manager->get_component_title(), 'area' => $manager->get_area_title())));
153
154// display the active grading method information and selector
155echo $output->management_method_selector($manager, $PAGE->url);
156
157// get the currently active method's controller
6832a102
DM
158if (!empty($method)) {
159 $controller = $manager->get_controller($method);
160 // display relevant actions
161 echo $output->container_start('actions');
162 if ($controller->is_form_defined()) {
fde33804 163 $definition = $controller->get_definition();
6832a102 164 echo $output->management_action_icon($controller->get_editor_url($returnurl),
fde33804 165 get_string('manageactionedit', 'core_grading'), 'b/document-edit');
671ec8f5 166 echo $output->management_action_icon(new moodle_url($PAGE->url, array('deleteform' => $definition->id)),
6832a102 167 get_string('manageactiondelete', 'core_grading'), 'b/edit-delete');
fde33804
DM
168 if (has_capability('moodle/grade:sharegradingforms', get_system_context())) {
169 echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)),
170 get_string('manageactionshare', 'core_grading'), 'b/bookmark-new');
171 }
6832a102
DM
172 } else {
173 echo $output->management_action_icon($controller->get_editor_url($returnurl),
174 get_string('manageactionnew', 'core_grading'), 'b/document-new');
20836db9 175 $pickurl = new moodle_url('/grade/grading/templates.php', array('targetid' => $controller->get_areaid()));
9c9cee4e
DM
176 if (!is_null($returnurl)) {
177 $pickurl->param('returnurl', $returnurl->out(false));
178 }
179 echo $output->management_action_icon($pickurl,
6832a102
DM
180 get_string('manageactionclone', 'core_grading'), 'b/edit-copy');
181 }
182 echo $output->container_end();
183
184 // display the grading form preview
185 if ($controller->is_form_defined()) {
186 echo $output->box($controller->render_preview($PAGE), 'preview');
187 }
188}
189
190
191echo $output->footer();