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