MDL-42167 Update workshop assessment record only when really changed
[moodle.git] / mod / workshop / assessment.php
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.
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/>.
18 /**
19  * Assess a submission or view the single assessment
20  *
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.
31  *
32  * @package    mod
33  * @subpackage workshop
34  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
38 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
39 require_once(dirname(__FILE__).'/locallib.php');
41 $asid       = required_param('asid', PARAM_INT);  // assessment id
42 $assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
43 $submission = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 0), '*', MUST_EXIST);
44 $workshop   = $DB->get_record('workshop', array('id' => $submission->workshopid), '*', MUST_EXIST);
45 $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
46 $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
48 require_login($course, false, $cm);
49 if (isguestuser()) {
50     print_error('guestsarenotallowed');
51 }
52 $workshop = new workshop($workshop, $cm, $course);
54 $PAGE->set_url($workshop->assess_url($assessment->id));
55 $PAGE->set_title($workshop->name);
56 $PAGE->set_heading($course->fullname);
57 $PAGE->navbar->add(get_string('assessingsubmission', 'workshop'));
59 $canviewallassessments  = has_capability('mod/workshop:viewallassessments', $workshop->context);
60 $canviewallsubmissions  = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
61 $cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
62 $canoverridegrades      = has_capability('mod/workshop:overridegrades', $workshop->context);
63 $isreviewer             = ($USER->id == $assessment->reviewerid);
64 $isauthor               = ($USER->id == $submission->authorid);
66 if ($canviewallsubmissions) {
67     // check this flag against the group membership yet
68     if (groups_get_activity_groupmode($workshop->cm) == SEPARATEGROUPS) {
69         // user must have accessallgroups or share at least one group with the submission author
70         if (!has_capability('moodle/site:accessallgroups', $workshop->context)) {
71             $usersgroups = groups_get_activity_allowed_groups($workshop->cm);
72             $authorsgroups = groups_get_all_groups($workshop->course->id, $submission->authorid, $workshop->cm->groupingid, 'g.id');
73             $sharedgroups = array_intersect_key($usersgroups, $authorsgroups);
74             if (empty($sharedgroups)) {
75                 $canviewallsubmissions = false;
76             }
77         }
78     }
79 }
81 if ($isreviewer or $isauthor or ($canviewallassessments and $canviewallsubmissions)) {
82     // such a user can continue
83 } else {
84     print_error('nopermissions', 'error', $workshop->view_url(), 'view this assessment');
85 }
87 if ($isauthor and !$isreviewer and !$canviewallassessments and $workshop->phase != workshop::PHASE_CLOSED) {
88     // authors can see assessments of their work at the end of workshop only
89     print_error('nopermissions', 'error', $workshop->view_url(), 'view assessment of own work before workshop is closed');
90 }
92 // only the reviewer is allowed to modify the assessment
93 if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
94     $assessmenteditable = true;
95 } else {
96     $assessmenteditable = false;
97 }
99 // check that all required examples have been assessed by the user
100 if ($assessmenteditable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT
101         and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
102     // the reviewer must have submitted their own submission
103     $reviewersubmission = $workshop->get_submission_by_author($assessment->reviewerid);
104     $output = $PAGE->get_renderer('mod_workshop');
105     if (!$reviewersubmission) {
106         // no money, no love
107         $assessmenteditable = false;
108         echo $output->header();
109         echo $output->heading(format_string($workshop->name));
110         notice(get_string('exampleneedsubmission', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
111         echo $output->footer();
112         exit;
113     } else {
114         $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
115         foreach ($examples as $exampleid => $example) {
116             if (is_null($example->grade)) {
117                 $assessmenteditable = false;
118                 echo $output->header();
119                 echo $output->heading(format_string($workshop->name));
120                 notice(get_string('exampleneedassessed', 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
121                 echo $output->footer();
122                 exit;
123             }
124         }
125     }
128 // load the grading strategy logic
129 $strategy = $workshop->grading_strategy_instance();
131 if (is_null($assessment->grade) and !$assessmenteditable) {
132     $mform = null;
133 } else {
134     // Are there any other pending assessments to do but this one?
135     if ($assessmenteditable) {
136         $pending = $workshop->get_pending_assessments_by_reviewer($assessment->reviewerid, $assessment->id);
137     } else {
138         $pending = array();
139     }
140     // load the assessment form and process the submitted data eventually
141     $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
142                                         array('editableweight' => $cansetassessmentweight, 'pending' => !empty($pending)));
144     // Set data managed by the workshop core, subplugins set their own data themselves.
145     $currentdata = (object)array(
146         'weight' => $assessment->weight,
147         'feedbackauthor' => $assessment->feedbackauthor,
148         'feedbackauthorformat' => $assessment->feedbackauthorformat,
149     );
150     if ($assessmenteditable and $workshop->overallfeedbackmode) {
151         $currentdata = file_prepare_standard_editor($currentdata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
152             $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
153         if ($workshop->overallfeedbackfiles) {
154             $currentdata = file_prepare_standard_filemanager($currentdata, 'feedbackauthorattachment',
155                 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
156                 $assessment->id);
157         }
158     }
159     $mform->set_data($currentdata);
161     if ($mform->is_cancelled()) {
162         redirect($workshop->view_url());
163     } elseif ($assessmenteditable and ($data = $mform->get_data())) {
164         if (is_null($assessment->grade)) {
165             $workshop->log('add assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
166         } else {
167             $workshop->log('update assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
168         }
170         // Let the grading strategy subplugin save its data.
171         $rawgrade = $strategy->save_assessment($assessment, $data);
173         // Store the data managed by the workshop core.
174         $coredata = (object)array('id' => $assessment->id);
175         if (isset($data->feedbackauthor_editor)) {
176             $coredata->feedbackauthor_editor = $data->feedbackauthor_editor;
177             $coredata = file_postupdate_standard_editor($coredata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
178                 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
179             unset($coredata->feedbackauthor_editor);
180         }
181         if (isset($data->feedbackauthorattachment_filemanager)) {
182             $coredata->feedbackauthorattachment_filemanager = $data->feedbackauthorattachment_filemanager;
183             $coredata = file_postupdate_standard_filemanager($coredata, 'feedbackauthorattachment',
184                 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
185                 $assessment->id);
186             unset($coredata->feedbackauthorattachment_filemanager);
187             if (empty($coredata->feedbackauthorattachment)) {
188                 $coredata->feedbackauthorattachment = 0;
189             }
190         }
191         if (isset($data->weight) and $cansetassessmentweight) {
192             $coredata->weight = $data->weight;
193         }
194         // Update the assessment data if there is something other than just the 'id'.
195         if (count((array)$coredata) > 1 ) {
196             $DB->update_record('workshop_assessments', $coredata);
197         }
199         // And finally redirect the user's browser.
200         if (!is_null($rawgrade) and isset($data->saveandclose)) {
201             redirect($workshop->view_url());
202         } else if (!is_null($rawgrade) and isset($data->saveandshownext)) {
203             $next = reset($pending);
204             if (!empty($next)) {
205                 redirect($workshop->assess_url($next->id));
206             } else {
207                 redirect($PAGE->url); // This should never happen but just in case...
208             }
209         } else {
210             // either it is not possible to calculate the $rawgrade
211             // or the reviewer has chosen "Save and continue"
212             redirect($PAGE->url);
213         }
214     }
217 // load the form to override gradinggrade and/or set weight and process the submitted data eventually
218 if ($canoverridegrades or $cansetassessmentweight) {
219     $options = array(
220         'editable' => true,
221         'editableweight' => $cansetassessmentweight,
222         'overridablegradinggrade' => $canoverridegrades);
223     $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
224     if ($data = $feedbackform->get_data()) {
225         $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
226         $record = new stdclass();
227         $record->id = $assessment->id;
228         if ($cansetassessmentweight) {
229             $record->weight = $data->weight;
230         }
231         if ($canoverridegrades) {
232             $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
233             $record->gradinggradeoverby = $USER->id;
234             $record->feedbackreviewer = $data->feedbackreviewer;
235             $record->feedbackreviewerformat = $data->feedbackreviewerformat;
236         }
237         $DB->update_record('workshop_assessments', $record);
238         redirect($workshop->view_url());
239     }
242 // output starts here
243 $output = $PAGE->get_renderer('mod_workshop');      // workshop renderer
244 echo $output->header();
245 echo $output->heading(format_string($workshop->name));
246 echo $output->heading(get_string('assessedsubmission', 'workshop'), 3);
248 $submission = $workshop->get_submission_by_id($submission->id);     // reload so can be passed to the renderer
249 echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
251 // show instructions for assessing as they may contain important information
252 // for evaluating the assessment
253 if (trim($workshop->instructreviewers)) {
254     $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
255         'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
256     print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
257     echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
258     print_collapsible_region_end();
261 // extend the current assessment record with user details
262 $assessment = $workshop->get_assessment_by_id($assessment->id);
264 if ($isreviewer) {
265     $options    = array(
266         'showreviewer'  => true,
267         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
268         'showform'      => $assessmenteditable or !is_null($assessment->grade),
269         'showweight'    => true,
270     );
271     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
272     $assessment->title = get_string('assessmentbyyourself', 'workshop');
273     echo $output->render($assessment);
275 } else {
276     $options    = array(
277         'showreviewer'  => has_capability('mod/workshop:viewreviewernames', $workshop->context),
278         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
279         'showform'      => $assessmenteditable or !is_null($assessment->grade),
280         'showweight'    => true,
281     );
282     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
283     echo $output->render($assessment);
286 if (!$assessmenteditable and $canoverridegrades) {
287     $feedbackform->display();
290 echo $output->footer();