MDL-42167 Update workshop assessment record only when really changed
[moodle.git] / mod / workshop / exassessment.php
CommitLineData
becec954
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 * Assess an example submission
20 *
65601f04
DM
21 * @package mod
22 * @subpackage workshop
23 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
becec954
DM
25 */
26
27require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
28require_once(dirname(__FILE__).'/locallib.php');
29
30$asid = required_param('asid', PARAM_INT); // assessment id
31$assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
32$example = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 1), '*', MUST_EXIST);
33$workshop = $DB->get_record('workshop', array('id' => $example->workshopid), '*', MUST_EXIST);
74df2951 34$course = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
becec954
DM
35$cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
36
37require_login($course, false, $cm);
38if (isguestuser()) {
39 print_error('guestsarenotallowed');
40}
41$workshop = new workshop($workshop, $cm, $course);
42
43$PAGE->set_url($workshop->exassess_url($assessment->id));
44$PAGE->set_title($workshop->name);
45$PAGE->set_heading($course->fullname);
46$PAGE->navbar->add(get_string('assessingexample', 'workshop'));
47$currenttab = 'assessment';
48
49$canmanage = has_capability('mod/workshop:manageexamples', $workshop->context);
50$isreviewer = ($USER->id == $assessment->reviewerid);
51
52if ($isreviewer or $canmanage) {
53 // such a user can continue
54} else {
60719642 55 print_error('nopermissions', 'error', $workshop->view_url(), 'assess example submission');
becec954
DM
56}
57
58// only the reviewer is allowed to modify the assessment
38504a44 59if (($canmanage and $assessment->weight == 1) or ($isreviewer and $workshop->assessing_examples_allowed())) {
becec954
DM
60 $assessmenteditable = true;
61} else {
62 $assessmenteditable = false;
63}
64
65// load the grading strategy logic
66$strategy = $workshop->grading_strategy_instance();
67
68// load the assessment form and process the submitted data eventually
69$mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable);
77d746a2
DM
70
71// Set data managed by the workshop core, subplugins set their own data themselves.
72$currentdata = (object)array(
73 'feedbackauthor' => $assessment->feedbackauthor,
74 'feedbackauthorformat' => $assessment->feedbackauthorformat,
75);
76if ($assessmenteditable and $workshop->overallfeedbackmode) {
77 $currentdata = file_prepare_standard_editor($currentdata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
78 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
79 if ($workshop->overallfeedbackfiles) {
80 $currentdata = file_prepare_standard_filemanager($currentdata, 'feedbackauthorattachment',
81 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
82 $assessment->id);
83 }
84}
85$mform->set_data($currentdata);
86
becec954
DM
87if ($mform->is_cancelled()) {
88 redirect($workshop->view_url());
89} elseif ($assessmenteditable and ($data = $mform->get_data())) {
5450f7b6
DM
90 if ($canmanage) {
91 if (is_null($assessment->grade)) {
92 $workshop->log('add reference assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
93 } else {
94 $workshop->log('update reference assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
95 }
96 } else {
97 if (is_null($assessment->grade)) {
98 $workshop->log('add example assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
99 } else {
100 $workshop->log('update example assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
101 }
102 }
77d746a2
DM
103
104 // Let the grading strategy subplugin save its data.
becec954 105 $rawgrade = $strategy->save_assessment($assessment, $data);
77d746a2
DM
106
107 // Store the data managed by the workshop core.
108 $coredata = (object)array('id' => $assessment->id);
109 if (isset($data->feedbackauthor_editor)) {
110 $coredata->feedbackauthor_editor = $data->feedbackauthor_editor;
111 $coredata = file_postupdate_standard_editor($coredata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
112 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
113 unset($coredata->feedbackauthor_editor);
114 }
115 if (isset($data->feedbackauthorattachment_filemanager)) {
116 $coredata->feedbackauthorattachment_filemanager = $data->feedbackauthorattachment_filemanager;
117 $coredata = file_postupdate_standard_filemanager($coredata, 'feedbackauthorattachment',
118 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
119 $assessment->id);
120 unset($coredata->feedbackauthorattachment_filemanager);
121 if (empty($coredata->feedbackauthorattachment)) {
122 $coredata->feedbackauthorattachment = 0;
123 }
124 }
cbf87967 125 if ($canmanage) {
77d746a2
DM
126 // Remember the last one who edited the reference assessment.
127 $coredata->reviewerid = $USER->id;
cbf87967 128 }
996531e7
MS
129 // Update the assessment data if there is something other than just the 'id'.
130 if (count((array)$coredata) > 1 ) {
131 $DB->update_record('workshop_assessments', $coredata);
132 }
77d746a2 133
becec954 134 if (!is_null($rawgrade) and isset($data->saveandclose)) {
cbf87967
DM
135 if ($canmanage) {
136 redirect($workshop->view_url());
137 } else {
138 redirect($workshop->excompare_url($example->id, $assessment->id));
139 }
becec954
DM
140 } else {
141 // either it is not possible to calculate the $rawgrade
142 // or the reviewer has chosen "Save and continue"
143 redirect($PAGE->url);
144 }
145}
146
147// output starts here
81b22887
DM
148$output = $PAGE->get_renderer('mod_workshop'); // workshop renderer
149echo $output->header();
764d7ba9
RW
150echo $output->heading(format_string($workshop->name));
151echo $output->heading(get_string('assessedexample', 'workshop'), 3);
becec954 152
becec954 153$example = $workshop->get_example_by_id($example->id); // reload so can be passed to the renderer
81b22887 154echo $output->render($workshop->prepare_example_submission(($example)));
becec954 155
e0142f7e
DM
156// show instructions for assessing as thay may contain important information
157// for evaluating the assessment
158if (trim($workshop->instructreviewers)) {
159 $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
160 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
161 print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
367a75fa 162 echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
e0142f7e
DM
163 print_collapsible_region_end();
164}
165
38504a44
DM
166// extend the current assessment record with user details
167$assessment = $workshop->get_assessment_by_id($assessment->id);
168
169if ($canmanage and $assessment->weight == 1) {
170 $options = array(
171 'showreviewer' => false,
172 'showauthor' => false,
173 'showform' => true,
38504a44 174 );
cdc85777 175 $assessment = $workshop->prepare_example_reference_assessment($assessment, $mform, $options);
38504a44
DM
176 $assessment->title = get_string('assessmentreference', 'workshop');
177 echo $output->render($assessment);
178
179} else if ($isreviewer) {
180 $options = array(
181 'showreviewer' => true,
182 'showauthor' => false,
183 'showform' => true,
38504a44 184 );
cdc85777 185 $assessment = $workshop->prepare_example_assessment($assessment, $mform, $options);
38504a44
DM
186 $assessment->title = get_string('assessmentbyyourself', 'workshop');
187 echo $output->render($assessment);
188
189} else if ($canmanage) {
190 $options = array(
191 'showreviewer' => true,
192 'showauthor' => false,
193 'showform' => true,
194 'showweight' => false,
195 );
cdc85777 196 $assessment = $workshop->prepare_example_assessment($assessment, $mform, $options);
38504a44 197 echo $output->render($assessment);
becec954
DM
198}
199
81b22887 200echo $output->footer();