MDL-42167 Update workshop assessment record only when really changed
[moodle.git] / mod / workshop / exassessment.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 an example submission
20  *
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
25  */
27 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
28 require_once(dirname(__FILE__).'/locallib.php');
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);
34 $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
35 $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
37 require_login($course, false, $cm);
38 if (isguestuser()) {
39     print_error('guestsarenotallowed');
40 }
41 $workshop = new workshop($workshop, $cm, $course);
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';
49 $canmanage  = has_capability('mod/workshop:manageexamples', $workshop->context);
50 $isreviewer = ($USER->id == $assessment->reviewerid);
52 if ($isreviewer or $canmanage) {
53     // such a user can continue
54 } else {
55     print_error('nopermissions', 'error', $workshop->view_url(), 'assess example submission');
56 }
58 // only the reviewer is allowed to modify the assessment
59 if (($canmanage and $assessment->weight == 1) or ($isreviewer and $workshop->assessing_examples_allowed())) {
60     $assessmenteditable = true;
61 } else {
62     $assessmenteditable = false;
63 }
65 // load the grading strategy logic
66 $strategy = $workshop->grading_strategy_instance();
68 // load the assessment form and process the submitted data eventually
69 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable);
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 );
76 if ($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);
87 if ($mform->is_cancelled()) {
88     redirect($workshop->view_url());
89 } elseif ($assessmenteditable and ($data = $mform->get_data())) {
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     }
104     // Let the grading strategy subplugin save its data.
105     $rawgrade = $strategy->save_assessment($assessment, $data);
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     }
125     if ($canmanage) {
126         // Remember the last one who edited the reference assessment.
127         $coredata->reviewerid = $USER->id;
128     }
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     }
134     if (!is_null($rawgrade) and isset($data->saveandclose)) {
135         if ($canmanage) {
136             redirect($workshop->view_url());
137         } else {
138             redirect($workshop->excompare_url($example->id, $assessment->id));
139         }
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     }
147 // output starts here
148 $output = $PAGE->get_renderer('mod_workshop');      // workshop renderer
149 echo $output->header();
150 echo $output->heading(format_string($workshop->name));
151 echo $output->heading(get_string('assessedexample', 'workshop'), 3);
153 $example = $workshop->get_example_by_id($example->id);     // reload so can be passed to the renderer
154 echo $output->render($workshop->prepare_example_submission(($example)));
156 // show instructions for assessing as thay may contain important information
157 // for evaluating the assessment
158 if (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'));
162     echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
163     print_collapsible_region_end();
166 // extend the current assessment record with user details
167 $assessment = $workshop->get_assessment_by_id($assessment->id);
169 if ($canmanage and $assessment->weight == 1) {
170     $options = array(
171         'showreviewer'  => false,
172         'showauthor'    => false,
173         'showform'      => true,
174     );
175     $assessment = $workshop->prepare_example_reference_assessment($assessment, $mform, $options);
176     $assessment->title = get_string('assessmentreference', 'workshop');
177     echo $output->render($assessment);
179 } else if ($isreviewer) {
180     $options = array(
181         'showreviewer'  => true,
182         'showauthor'    => false,
183         'showform'      => true,
184     );
185     $assessment = $workshop->prepare_example_assessment($assessment, $mform, $options);
186     $assessment->title = get_string('assessmentbyyourself', 'workshop');
187     echo $output->render($assessment);
189 } else if ($canmanage) {
190     $options = array(
191         'showreviewer'  => true,
192         'showauthor'    => false,
193         'showform'      => true,
194         'showweight'    => false,
195     );
196     $assessment = $workshop->prepare_example_assessment($assessment, $mform, $options);
197     echo $output->render($assessment);
200 echo $output->footer();