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