MDL-59401 mod_workshop: rename method remanining fix
[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_workshop
33  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
37 require(__DIR__.'/../../config.php');
38 require_once(__DIR__.'/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');
50 }
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 $cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
59 $canoverridegrades      = has_capability('mod/workshop:overridegrades', $workshop->context);
60 $isreviewer             = ($USER->id == $assessment->reviewerid);
62 $workshop->check_view_assessment($assessment, $submission);
64 // only the reviewer is allowed to modify the assessment
65 if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
66     $assessmenteditable = true;
67 } else {
68     $assessmenteditable = false;
69 }
71 // check that all required examples have been assessed by the user
72 if ($assessmenteditable) {
74     list($assessed, $notice) = $workshop->check_examples_assessed_before_assessment($assessment->reviewerid);
75     if (!$assessed) {
76         echo $output->header();
77         echo $output->heading(format_string($workshop->name));
78         notice(get_string($notice, 'workshop'), new moodle_url('/mod/workshop/view.php', array('id' => $cm->id)));
79         echo $output->footer();
80         exit;
81     }
82 }
84 // load the grading strategy logic
85 $strategy = $workshop->grading_strategy_instance();
87 if (is_null($assessment->grade) and !$assessmenteditable) {
88     $mform = null;
89 } else {
90     // Are there any other pending assessments to do but this one?
91     if ($assessmenteditable) {
92         $pending = $workshop->get_pending_assessments_by_reviewer($assessment->reviewerid, $assessment->id);
93     } else {
94         $pending = array();
95     }
96     // load the assessment form and process the submitted data eventually
97     $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
98                                         array('editableweight' => $cansetassessmentweight, 'pending' => !empty($pending)));
100     // Set data managed by the workshop core, subplugins set their own data themselves.
101     $currentdata = (object)array(
102         'weight' => $assessment->weight,
103         'feedbackauthor' => $assessment->feedbackauthor,
104         'feedbackauthorformat' => $assessment->feedbackauthorformat,
105     );
106     if ($assessmenteditable and $workshop->overallfeedbackmode) {
107         $currentdata = file_prepare_standard_editor($currentdata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
108             $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
109         if ($workshop->overallfeedbackfiles) {
110             $currentdata = file_prepare_standard_filemanager($currentdata, 'feedbackauthorattachment',
111                 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
112                 $assessment->id);
113         }
114     }
115     $mform->set_data($currentdata);
117     if ($mform->is_cancelled()) {
118         redirect($workshop->view_url());
119     } elseif ($assessmenteditable and ($data = $mform->get_data())) {
121         // Let the grading strategy subplugin save its data.
122         $rawgrade = $strategy->save_assessment($assessment, $data);
124         // Store the data managed by the workshop core.
125         $coredata = (object)array('id' => $assessment->id);
126         if (isset($data->feedbackauthor_editor)) {
127             $coredata->feedbackauthor_editor = $data->feedbackauthor_editor;
128             $coredata = file_postupdate_standard_editor($coredata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
129                 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
130             unset($coredata->feedbackauthor_editor);
131         }
132         if (isset($data->feedbackauthorattachment_filemanager)) {
133             $coredata->feedbackauthorattachment_filemanager = $data->feedbackauthorattachment_filemanager;
134             $coredata = file_postupdate_standard_filemanager($coredata, 'feedbackauthorattachment',
135                 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
136                 $assessment->id);
137             unset($coredata->feedbackauthorattachment_filemanager);
138             if (empty($coredata->feedbackauthorattachment)) {
139                 $coredata->feedbackauthorattachment = 0;
140             }
141         }
142         if (isset($data->weight) and $cansetassessmentweight) {
143             $coredata->weight = $data->weight;
144         }
145         // Update the assessment data if there is something other than just the 'id'.
146         if (count((array)$coredata) > 1 ) {
147             $DB->update_record('workshop_assessments', $coredata);
148             $params = array(
149                 'relateduserid' => $submission->authorid,
150                 'objectid' => $assessment->id,
151                 'context' => $workshop->context,
152                 'other' => array(
153                     'workshopid' => $workshop->id,
154                     'submissionid' => $assessment->submissionid
155                 )
156             );
158             if (is_null($assessment->grade)) {
159                 // All workshop_assessments are created when allocations are made. The create event is of more use located here.
160                 $event = \mod_workshop\event\submission_assessed::create($params);
161                 $event->trigger();
162             } else {
163                 $params['other']['grade'] = $assessment->grade;
164                 $event = \mod_workshop\event\submission_reassessed::create($params);
165                 $event->trigger();
166             }
167         }
169         // And finally redirect the user's browser.
170         if (!is_null($rawgrade) and isset($data->saveandclose)) {
171             redirect($workshop->view_url());
172         } else if (!is_null($rawgrade) and isset($data->saveandshownext)) {
173             $next = reset($pending);
174             if (!empty($next)) {
175                 redirect($workshop->assess_url($next->id));
176             } else {
177                 redirect($PAGE->url); // This should never happen but just in case...
178             }
179         } else {
180             // either it is not possible to calculate the $rawgrade
181             // or the reviewer has chosen "Save and continue"
182             redirect($PAGE->url);
183         }
184     }
187 // load the form to override gradinggrade and/or set weight and process the submitted data eventually
188 if ($canoverridegrades or $cansetassessmentweight) {
189     $options = array(
190         'editable' => true,
191         'editableweight' => $cansetassessmentweight,
192         'overridablegradinggrade' => $canoverridegrades);
193     $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
194     if ($data = $feedbackform->get_data()) {
195         $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
196         $record = new stdclass();
197         $record->id = $assessment->id;
198         if ($cansetassessmentweight) {
199             $record->weight = $data->weight;
200         }
201         if ($canoverridegrades) {
202             $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
203             $record->gradinggradeoverby = $USER->id;
204             $record->feedbackreviewer = $data->feedbackreviewer;
205             $record->feedbackreviewerformat = $data->feedbackreviewerformat;
206         }
207         $DB->update_record('workshop_assessments', $record);
208         redirect($workshop->view_url());
209     }
212 // output starts here
213 $output = $PAGE->get_renderer('mod_workshop');      // workshop renderer
214 echo $output->header();
215 echo $output->heading(format_string($workshop->name));
216 echo $output->heading(get_string('assessedsubmission', 'workshop'), 3);
218 $submission = $workshop->get_submission_by_id($submission->id);     // reload so can be passed to the renderer
219 echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
221 // show instructions for assessing as they may contain important information
222 // for evaluating the assessment
223 if (trim($workshop->instructreviewers)) {
224     $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
225         'mod_workshop', 'instructreviewers', null, workshop::instruction_editors_options($PAGE->context));
226     print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
227     echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
228     print_collapsible_region_end();
231 // extend the current assessment record with user details
232 $assessment = $workshop->get_assessment_by_id($assessment->id);
234 if ($isreviewer) {
235     $options    = array(
236         'showreviewer'  => true,
237         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
238         'showform'      => $assessmenteditable or !is_null($assessment->grade),
239         'showweight'    => true,
240     );
241     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
242     $assessment->title = get_string('assessmentbyyourself', 'workshop');
243     echo $output->render($assessment);
245 } else {
246     $options    = array(
247         'showreviewer'  => has_capability('mod/workshop:viewreviewernames', $workshop->context),
248         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
249         'showform'      => $assessmenteditable or !is_null($assessment->grade),
250         'showweight'    => true,
251     );
252     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
253     echo $output->render($assessment);
256 if (!$assessmenteditable and $canoverridegrades) {
257     $feedbackform->display();
260 echo $output->footer();