MDL-50490 cron: Prevent tasks from leaving uncommitted transactions
[moodle.git] / mod / workshop / assessment.php
bloba75b44e8c01726b5a80d4e1dd9b388b7030b6f7f
1 <?php
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.
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/>.
18 /**
19 * Assess a submission or view the single assessment
21 * Assessment id parameter must be passed. The script displays the submission and
22 * the assessment form. If the current user is the reviewer and the assessing is
23 * allowed, new assessment can be saved.
24 * If the assessing is not allowed (for example, the assessment period is over
25 * or the current user is eg a teacher), the assessment form is opened
26 * in a non-editable mode.
27 * The capability 'mod/workshop:peerassess' is intentionally not checked here.
28 * The user is considered as a reviewer if the corresponding assessment record
29 * has been prepared for him/her (during the allocation). So even a user without the
30 * peerassess capability (like a 'teacher', for example) can become a reviewer.
32 * @package mod_workshop
33 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
38 require_once(dirname(__FILE__).'/locallib.php');
40 $asid = required_param('asid', PARAM_INT); // assessment id
41 $assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
42 $submission = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 0), '*', MUST_EXIST);
43 $workshop = $DB->get_record('workshop', array('id' => $submission->workshopid), '*', MUST_EXIST);
44 $course = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
45 $cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
47 require_login($course, false, $cm);
48 if (isguestuser()) {
49 print_error('guestsarenotallowed');
51 $workshop = new workshop($workshop, $cm, $course);
53 $PAGE->set_url($workshop->assess_url($assessment->id));
54 $PAGE->set_title($workshop->name);
55 $PAGE->set_heading($course->fullname);
56 $PAGE->navbar->add(get_string('assessingsubmission', 'workshop'));
58 $canviewallassessments = has_capability('mod/workshop:viewallassessments', $workshop->context);
59 $canviewallsubmissions = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
60 $cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
61 $canoverridegrades = has_capability('mod/workshop:overridegrades', $workshop->context);
62 $isreviewer = ($USER->id == $assessment->reviewerid);
63 $isauthor = ($USER->id == $submission->authorid);
65 if ($canviewallsubmissions) {
66 // check this flag against the group membership yet
67 if (groups_get_activity_groupmode($workshop->cm) == SEPARATEGROUPS) {
68 // user must have accessallgroups or share at least one group with the submission author
69 if (!has_capability('moodle/site:accessallgroups', $workshop->context)) {
70 $usersgroups = groups_get_activity_allowed_groups($workshop->cm);
71 $authorsgroups = groups_get_all_groups($workshop->course->id, $submission->authorid, $workshop->cm->groupingid, 'g.id');
72 $sharedgroups = array_intersect_key($usersgroups, $authorsgroups);
73 if (empty($sharedgroups)) {
74 $canviewallsubmissions = false;
80 if ($isreviewer or $isauthor or ($canviewallassessments and $canviewallsubmissions)) {
81 // such a user can continue
82 } else {
83 print_error('nopermissions', 'error', $workshop->view_url(), 'view this assessment');
86 if ($isauthor and !$isreviewer and !$canviewallassessments and $workshop->phase != workshop::PHASE_CLOSED) {
87 // authors can see assessments of their work at the end of workshop only
88 print_error('nopermissions', 'error', $workshop->view_url(), 'view assessment of own work before workshop is closed');
91 // only the reviewer is allowed to modify the assessment
92 if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
93 $assessmenteditable = true;
94 } else {
95 $assessmenteditable = false;
98 // check that all required examples have been assessed by the user
99 if ($assessmenteditable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT
100 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
101 // the reviewer must have submitted their own submission
102 $reviewersubmission = $workshop->get_submission_by_author($assessment->reviewerid);
103 $output = $PAGE->get_renderer('mod_workshop');
104 if (!$reviewersubmission) {
105 // no money, no love
106 $assessmenteditable = false;
107 echo $output->header();
108 echo $output->heading(format_string($workshop->name));
109 notice(get_string('exampleneedsubmission', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
110 echo $output->footer();
111 exit;
112 } else {
113 $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
114 foreach ($examples as $exampleid => $example) {
115 if (is_null($example->grade)) {
116 $assessmenteditable = false;
117 echo $output->header();
118 echo $output->heading(format_string($workshop->name));
119 notice(get_string('exampleneedassessed', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
120 echo $output->footer();
121 exit;
127 // load the grading strategy logic
128 $strategy = $workshop->grading_strategy_instance();
130 if (is_null($assessment->grade) and !$assessmenteditable) {
131 $mform = null;
132 } else {
133 // Are there any other pending assessments to do but this one?
134 if ($assessmenteditable) {
135 $pending = $workshop->get_pending_assessments_by_reviewer($assessment->reviewerid, $assessment->id);
136 } else {
137 $pending = array();
139 // load the assessment form and process the submitted data eventually
140 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
141 array('editableweight' => $cansetassessmentweight, 'pending' => !empty($pending)));
143 // Set data managed by the workshop core, subplugins set their own data themselves.
144 $currentdata = (object)array(
145 'weight' => $assessment->weight,
146 'feedbackauthor' => $assessment->feedbackauthor,
147 'feedbackauthorformat' => $assessment->feedbackauthorformat,
149 if ($assessmenteditable and $workshop->overallfeedbackmode) {
150 $currentdata = file_prepare_standard_editor($currentdata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
151 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
152 if ($workshop->overallfeedbackfiles) {
153 $currentdata = file_prepare_standard_filemanager($currentdata, 'feedbackauthorattachment',
154 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
155 $assessment->id);
158 $mform->set_data($currentdata);
160 if ($mform->is_cancelled()) {
161 redirect($workshop->view_url());
162 } elseif ($assessmenteditable and ($data = $mform->get_data())) {
164 // Let the grading strategy subplugin save its data.
165 $rawgrade = $strategy->save_assessment($assessment, $data);
167 // Store the data managed by the workshop core.
168 $coredata = (object)array('id' => $assessment->id);
169 if (isset($data->feedbackauthor_editor)) {
170 $coredata->feedbackauthor_editor = $data->feedbackauthor_editor;
171 $coredata = file_postupdate_standard_editor($coredata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
172 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
173 unset($coredata->feedbackauthor_editor);
175 if (isset($data->feedbackauthorattachment_filemanager)) {
176 $coredata->feedbackauthorattachment_filemanager = $data->feedbackauthorattachment_filemanager;
177 $coredata = file_postupdate_standard_filemanager($coredata, 'feedbackauthorattachment',
178 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
179 $assessment->id);
180 unset($coredata->feedbackauthorattachment_filemanager);
181 if (empty($coredata->feedbackauthorattachment)) {
182 $coredata->feedbackauthorattachment = 0;
185 if (isset($data->weight) and $cansetassessmentweight) {
186 $coredata->weight = $data->weight;
188 // Update the assessment data if there is something other than just the 'id'.
189 if (count((array)$coredata) > 1 ) {
190 $DB->update_record('workshop_assessments', $coredata);
191 $params = array(
192 'relateduserid' => $submission->authorid,
193 'objectid' => $assessment->id,
194 'context' => $workshop->context,
195 'other' => array(
196 'workshopid' => $workshop->id,
197 'submissionid' => $assessment->submissionid
201 if (is_null($assessment->grade)) {
202 // All workshop_assessments are created when allocations are made. The create event is of more use located here.
203 $event = \mod_workshop\event\submission_assessed::create($params);
204 $event->trigger();
205 } else {
206 $params['other']['grade'] = $assessment->grade;
207 $event = \mod_workshop\event\submission_reassessed::create($params);
208 $event->trigger();
212 // And finally redirect the user's browser.
213 if (!is_null($rawgrade) and isset($data->saveandclose)) {
214 redirect($workshop->view_url());
215 } else if (!is_null($rawgrade) and isset($data->saveandshownext)) {
216 $next = reset($pending);
217 if (!empty($next)) {
218 redirect($workshop->assess_url($next->id));
219 } else {
220 redirect($PAGE->url); // This should never happen but just in case...
222 } else {
223 // either it is not possible to calculate the $rawgrade
224 // or the reviewer has chosen "Save and continue"
225 redirect($PAGE->url);
230 // load the form to override gradinggrade and/or set weight and process the submitted data eventually
231 if ($canoverridegrades or $cansetassessmentweight) {
232 $options = array(
233 'editable' => true,
234 'editableweight' => $cansetassessmentweight,
235 'overridablegradinggrade' => $canoverridegrades);
236 $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
237 if ($data = $feedbackform->get_data()) {
238 $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
239 $record = new stdclass();
240 $record->id = $assessment->id;
241 if ($cansetassessmentweight) {
242 $record->weight = $data->weight;
244 if ($canoverridegrades) {
245 $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
246 $record->gradinggradeoverby = $USER->id;
247 $record->feedbackreviewer = $data->feedbackreviewer;
248 $record->feedbackreviewerformat = $data->feedbackreviewerformat;
250 $DB->update_record('workshop_assessments', $record);
251 redirect($workshop->view_url());
255 // output starts here
256 $output = $PAGE->get_renderer('mod_workshop'); // workshop renderer
257 echo $output->header();
258 echo $output->heading(format_string($workshop->name));
259 echo $output->heading(get_string('assessedsubmission', 'workshop'), 3);
261 $submission = $workshop->get_submission_by_id($submission->id); // reload so can be passed to the renderer
262 echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
264 // show instructions for assessing as they may contain important information
265 // for evaluating the assessment
266 if (trim($workshop->instructreviewers)) {
267 $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
268 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
269 print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
270 echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
271 print_collapsible_region_end();
274 // extend the current assessment record with user details
275 $assessment = $workshop->get_assessment_by_id($assessment->id);
277 if ($isreviewer) {
278 $options = array(
279 'showreviewer' => true,
280 'showauthor' => has_capability('mod/workshop:viewauthornames', $workshop->context),
281 'showform' => $assessmenteditable or !is_null($assessment->grade),
282 'showweight' => true,
284 $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
285 $assessment->title = get_string('assessmentbyyourself', 'workshop');
286 echo $output->render($assessment);
288 } else {
289 $options = array(
290 'showreviewer' => has_capability('mod/workshop:viewreviewernames', $workshop->context),
291 'showauthor' => has_capability('mod/workshop:viewauthornames', $workshop->context),
292 'showform' => $assessmenteditable or !is_null($assessment->grade),
293 'showweight' => true,
295 $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
296 echo $output->render($assessment);
299 if (!$assessmenteditable and $canoverridegrades) {
300 $feedbackform->display();
303 echo $output->footer();