MDL-30998: updated docblocks for advanced grading
[moodle.git] / grade / grading / manage.php
CommitLineData
6832a102 1<?php
6832a102
DM
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
9c9cee4e
DM
18 * A single gradable area management page
19 *
20 * This page alows the user to set the current active method in the given
21 * area, provides access to the plugin editor and allows user to save the
22 * current form as a template or re-use some existing form.
23 *
d22e9e32 24 * @package core_grading
6832a102
DM
25 * @copyright 2011 David Mudrak <david@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
30require_once($CFG->dirroot.'/grade/grading/lib.php');
31
fde33804 32// identify gradable area by its id
71bd9d20 33$areaid = optional_param('areaid', null, PARAM_INT);
fde33804 34// alternatively the context, component and areaname must be provided
71bd9d20
DM
35$contextid = optional_param('contextid', null, PARAM_INT);
36$component = optional_param('component', null, PARAM_COMPONENT);
37$area = optional_param('area', null, PARAM_AREA);
fde33804 38// keep the caller's URL so that we know where to send the user finally
71bd9d20 39$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
fde33804 40// active method selector
71bd9d20 41$setmethod = optional_param('setmethod', null, PARAM_PLUGIN);
fde33804
DM
42// publish the given form definition as a new template in the forms bank
43$shareform = optional_param('shareform', null, PARAM_INT);
671ec8f5
DM
44// delete the given form definition
45$deleteform = optional_param('deleteform', null, PARAM_INT);
fde33804
DM
46// consider the required action as confirmed
47$confirmed = optional_param('confirmed', false, PARAM_BOOL);
48// a message to display, typically a previous action's result
49$message = optional_param('message', null, PARAM_NOTAGS);
6832a102
DM
50
51if (!is_null($areaid)) {
52 // get manager by id
53 $manager = get_grading_manager($areaid);
54} else {
55 // get manager by context and component
98a73bb7 56 if (is_null($contextid) or is_null($component) or is_null($area)) {
6832a102
DM
57 throw new coding_exception('The caller script must identify the gradable area.');
58 }
d197ea43 59 $context = context::instance_by_id($contextid, MUST_EXIST);
6832a102
DM
60 $manager = get_grading_manager($context, $component, $area);
61}
62
9c9cee4e
DM
63if ($manager->get_context()->contextlevel < CONTEXT_COURSE) {
64 throw new coding_exception('Unsupported gradable area context level');
65}
fde33804 66
9c9cee4e
DM
67// get the currently active method
68$method = $manager->get_active_method();
fde33804 69
9c9cee4e 70list($context, $course, $cm) = get_context_info_array($manager->get_context()->id);
fde33804 71
9c9cee4e
DM
72require_login($course, true, $cm);
73require_capability('moodle/grade:managegradingforms', $context);
6832a102 74
9c9cee4e
DM
75if (!empty($returnurl)) {
76 $returnurl = new moodle_url($returnurl);
6832a102 77} else {
9c9cee4e 78 $returnurl = null;
6832a102
DM
79}
80
98a73bb7 81$PAGE->set_url($manager->get_management_url($returnurl));
6832a102
DM
82navigation_node::override_active_url($manager->get_management_url());
83$PAGE->set_title(get_string('gradingmanagement', 'core_grading'));
84$PAGE->set_heading(get_string('gradingmanagement', 'core_grading'));
85$output = $PAGE->get_renderer('core_grading');
86
6832a102 87// process the eventual change of the active grading method
71bd9d20 88if (!empty($setmethod)) {
6832a102 89 require_sesskey();
71bd9d20 90 if ($setmethod == 'none') {
6832a102 91 // here we expect that noone would actually want to call their plugin as 'none'
71bd9d20 92 $setmethod = null;
6832a102 93 }
71bd9d20 94 $manager->set_active_method($setmethod);
6832a102
DM
95 redirect($PAGE->url);
96}
97
fde33804
DM
98// publish the form as a template
99if (!empty($shareform)) {
dd736a87 100 require_capability('moodle/grade:sharegradingforms', context_system::instance());
fde33804
DM
101 $controller = $manager->get_controller($method);
102 $definition = $controller->get_definition();
103 if (!$confirmed) {
104 // let the user confirm they understand what they are doing (haha ;-)
105 echo $output->header();
106 echo $output->confirm(get_string('manageactionshareconfirm', 'core_grading', s($definition->name)),
107 new moodle_url($PAGE->url, array('shareform' => $shareform, 'confirmed' => 1)),
108 $PAGE->url);
109 echo $output->footer();
110 die();
111 } else {
112 require_sesskey();
113 $newareaid = $manager->create_shared_area($method);
114 $targetarea = get_grading_manager($newareaid);
115 $targetcontroller = $targetarea->get_controller($method);
3599b113 116 $targetcontroller->update_definition($controller->get_definition_copy($targetcontroller));
3f3ee711 117 $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id));
fde33804
DM
118 redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactionsharedone', 'core_grading'))));
119 }
120}
121
671ec8f5
DM
122// delete the form definition
123if (!empty($deleteform)) {
124 $controller = $manager->get_controller($method);
125 $definition = $controller->get_definition();
126 if (!$confirmed) {
127 // let the user confirm they understand the consequences (also known as WTF-effect)
128 echo $output->header();
129 echo $output->confirm(markdown_to_html(get_string('manageactiondeleteconfirm', 'core_grading', array(
130 'formname' => s($definition->name),
131 'component' => $manager->get_component_title(),
132 'area' => $manager->get_area_title()))),
133 new moodle_url($PAGE->url, array('deleteform' => $deleteform, 'confirmed' => 1)), $PAGE->url);
134 echo $output->footer();
135 die();
136 } else {
137 require_sesskey();
138 $controller->delete_definition();
139 redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactiondeletedone', 'core_grading'))));
140 }
141}
142
6832a102 143echo $output->header();
fde33804
DM
144
145if (!empty($message)) {
146 echo $output->management_message($message);
147}
148
6832a102
DM
149echo $output->heading(get_string('gradingmanagementtitle', 'core_grading', array(
150 'component' => $manager->get_component_title(), 'area' => $manager->get_area_title())));
151
152// display the active grading method information and selector
153echo $output->management_method_selector($manager, $PAGE->url);
154
155// get the currently active method's controller
6832a102
DM
156if (!empty($method)) {
157 $controller = $manager->get_controller($method);
158 // display relevant actions
159 echo $output->container_start('actions');
160 if ($controller->is_form_defined()) {
fde33804 161 $definition = $controller->get_definition();
3f3ee711 162 // icon to edit the form definition
6832a102 163 echo $output->management_action_icon($controller->get_editor_url($returnurl),
fde33804 164 get_string('manageactionedit', 'core_grading'), 'b/document-edit');
3f3ee711 165 // icon to delete the current form definition
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');
3f3ee711 168 // icon to save the form as a new template
dd736a87 169 if (has_capability('moodle/grade:sharegradingforms', context_system::instance())) {
3f3ee711
DM
170 if (empty($definition->copiedfromid)) {
171 $hasoriginal = false;
172 } else {
173 $hasoriginal = $DB->record_exists('grading_definitions', array('id' => $definition->copiedfromid));
174 }
3599b113
MG
175 if (!$controller->is_form_available()) {
176 // drafts can not be shared
177 $allowshare = false;
178 } else if (!$hasoriginal) {
3f3ee711
DM
179 // was created from scratch or is orphaned
180 if (empty($definition->timecopied)) {
181 // was never shared before
182 $allowshare = true;
183 } else if ($definition->timemodified > $definition->timecopied) {
184 // was modified since last time shared
185 $allowshare = true;
186 } else {
187 // was not modified since last time shared
188 $allowshare = false;
189 }
190 } else {
191 // was created from a template and the template still exists
192 if ($definition->timecreated == $definition->timemodified) {
193 // was not modified since created
194 $allowshare = false;
195 } else if (empty($definition->timecopied)) {
196 // was modified but was not re-shared yet
197 $allowshare = true;
198 } else if ($definition->timemodified > $definition->timecopied) {
199 // was modified since last time re-shared
200 $allowshare = true;
201 } else {
202 // was not modified since last time re-shared
203 $allowshare = false;
204 }
205 }
206 if ($allowshare) {
207 echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)),
208 get_string('manageactionshare', 'core_grading'), 'b/bookmark-new');
209 }
fde33804 210 }
6832a102
DM
211 } else {
212 echo $output->management_action_icon($controller->get_editor_url($returnurl),
213 get_string('manageactionnew', 'core_grading'), 'b/document-new');
4d064218 214 $pickurl = new moodle_url('/grade/grading/pick.php', array('targetid' => $controller->get_areaid()));
9c9cee4e
DM
215 if (!is_null($returnurl)) {
216 $pickurl->param('returnurl', $returnurl->out(false));
217 }
218 echo $output->management_action_icon($pickurl,
6832a102
DM
219 get_string('manageactionclone', 'core_grading'), 'b/edit-copy');
220 }
221 echo $output->container_end();
222
2ae7faf1
MG
223 // display the message if the form is currently not available (if applicable)
224 if ($message = $controller->form_unavailable_notification()) {
225 echo $output->notification($message);
226 }
6832a102
DM
227 // display the grading form preview
228 if ($controller->is_form_defined()) {
49a537b3 229 if ($definition->status == gradingform_controller::DEFINITION_STATUS_READY) {
967d346f 230 $tag = html_writer::tag('span', get_string('statusready', 'core_grading'), array('class' => 'status ready'));
49a537b3 231 } else {
967d346f 232 $tag = html_writer::tag('span', get_string('statusdraft', 'core_grading'), array('class' => 'status draft'));
49a537b3
DM
233 }
234 echo $output->heading(s($definition->name) . ' ' . $tag, 3, 'definition-name');
49a537b3 235 echo $output->box($controller->render_preview($PAGE), 'definition-preview');
6832a102
DM
236 }
237}
238
239
240echo $output->footer();