weekly release 2.6dev
[moodle.git] / mod / workshop / assessment.php
CommitLineData
0968b1a3 1<?php
53fad4b9
DM
2
3// This file is part of Moodle - http://moodle.org/
4//
0968b1a3
DM
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.
53fad4b9 14//
0968b1a3
DM
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/>.
53fad4b9 17
0968b1a3 18/**
407b1e91 19 * Assess a submission or view the single assessment
0968b1a3 20 *
407b1e91
DM
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.
0968b1a3 31 *
65601f04
DM
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
0968b1a3
DM
36 */
37
38require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
0968b1a3
DM
39require_once(dirname(__FILE__).'/locallib.php');
40
407b1e91
DM
41$asid = required_param('asid', PARAM_INT); // assessment id
42$assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
becec954 43$submission = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 0), '*', MUST_EXIST);
407b1e91 44$workshop = $DB->get_record('workshop', array('id' => $submission->workshopid), '*', MUST_EXIST);
74df2951 45$course = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
407b1e91 46$cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
0968b1a3
DM
47
48require_login($course, false, $cm);
a39d7d87
DM
49if (isguestuser()) {
50 print_error('guestsarenotallowed');
51}
52$workshop = new workshop($workshop, $cm, $course);
6e309973 53
407b1e91
DM
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'));
407b1e91
DM
58
59$canviewallassessments = has_capability('mod/workshop:viewallassessments', $workshop->context);
60$canviewallsubmissions = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
c6b784f0
DM
61$cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
62$canoverridegrades = has_capability('mod/workshop:overridegrades', $workshop->context);
407b1e91
DM
63$isreviewer = ($USER->id == $assessment->reviewerid);
64$isauthor = ($USER->id == $submission->authorid);
65
3f5e9915
DM
66if ($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}
80
407b1e91
DM
81if ($isreviewer or $isauthor or ($canviewallassessments and $canviewallsubmissions)) {
82 // such a user can continue
83} else {
60719642
DM
84 print_error('nopermissions', 'error', $workshop->view_url(), 'view this assessment');
85}
86
87if ($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');
407b1e91
DM
90}
91
92// only the reviewer is allowed to modify the assessment
9ddff589 93if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
77f43e7d 94 $assessmenteditable = true;
407b1e91 95} else {
77f43e7d 96 $assessmenteditable = false;
0968b1a3 97}
0968b1a3 98
cff28ef0
DM
99// check that all required examples have been assessed by the user
100if ($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 if (!$reviewersubmission) {
105 // no money, no love
106 $assessmenteditable = false;
81b22887
DM
107 echo $output->header();
108 echo $output->heading(get_string('exampleneedsubmission', 'workshop'), 2);
109 echo $output->footer();
cff28ef0
DM
110 exit;
111 } else {
112 $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
113 foreach ($examples as $exampleid => $example) {
114 if (is_null($example->grade)) {
115 $assessmenteditable = false;
81b22887
DM
116 echo $output->header();
117 echo $output->heading(get_string('exampleneedassessed', 'workshop'), 2);
118 echo $output->footer();
cff28ef0
DM
119 exit;
120 }
121 }
122 }
123}
124
05e69e56
DM
125// load the grading strategy logic
126$strategy = $workshop->grading_strategy_instance();
127
c6b784f0
DM
128if (is_null($assessment->grade) and !$assessmenteditable) {
129 $mform = null;
130} else {
1df3bc14
DM
131 // Are there any other pending assessments to do but this one?
132 if ($assessmenteditable) {
133 $pending = $workshop->get_pending_assessments_by_reviewer($assessment->reviewerid, $assessment->id);
134 } else {
135 $pending = array();
136 }
c6b784f0 137 // load the assessment form and process the submitted data eventually
77d746a2
DM
138 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
139 array('editableweight' => $cansetassessmentweight, 'pending' => !empty($pending)));
c6a793d5
DM
140
141 // Set data managed by the workshop core, subplugins set their own data themselves.
142 $currentdata = (object)array(
143 'weight' => $assessment->weight,
144 'feedbackauthor' => $assessment->feedbackauthor,
145 'feedbackauthorformat' => $assessment->feedbackauthorformat,
146 );
77d746a2
DM
147 if ($assessmenteditable and $workshop->overallfeedbackmode) {
148 $currentdata = file_prepare_standard_editor($currentdata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
149 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
150 if ($workshop->overallfeedbackfiles) {
151 $currentdata = file_prepare_standard_filemanager($currentdata, 'feedbackauthorattachment',
152 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
153 $assessment->id);
c6a793d5 154 }
c6a793d5
DM
155 }
156 $mform->set_data($currentdata);
157
c6b784f0 158 if ($mform->is_cancelled()) {
407b1e91 159 redirect($workshop->view_url());
c6b784f0 160 } elseif ($assessmenteditable and ($data = $mform->get_data())) {
5450f7b6
DM
161 if (is_null($assessment->grade)) {
162 $workshop->log('add assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
163 } else {
164 $workshop->log('update assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
165 }
c6a793d5
DM
166
167 // Let the grading strategy subplugin save its data.
c6b784f0 168 $rawgrade = $strategy->save_assessment($assessment, $data);
c6a793d5
DM
169
170 // Store the data managed by the workshop core.
171 $coredata = (object)array('id' => $assessment->id);
172 if (isset($data->feedbackauthor_editor)) {
173 $coredata->feedbackauthor_editor = $data->feedbackauthor_editor;
77d746a2 174 $coredata = file_postupdate_standard_editor($coredata, 'feedbackauthor', $workshop->overall_feedback_content_options(),
c6a793d5
DM
175 $workshop->context, 'mod_workshop', 'overallfeedback_content', $assessment->id);
176 unset($coredata->feedbackauthor_editor);
177 }
178 if (isset($data->feedbackauthorattachment_filemanager)) {
179 $coredata->feedbackauthorattachment_filemanager = $data->feedbackauthorattachment_filemanager;
180 $coredata = file_postupdate_standard_filemanager($coredata, 'feedbackauthorattachment',
77d746a2 181 $workshop->overall_feedback_attachment_options(), $workshop->context, 'mod_workshop', 'overallfeedback_attachment',
c6a793d5
DM
182 $assessment->id);
183 unset($coredata->feedbackauthorattachment_filemanager);
184 if (empty($coredata->feedbackauthorattachment)) {
185 $coredata->feedbackauthorattachment = 0;
186 }
187 }
c6b784f0 188 if (isset($data->weight) and $cansetassessmentweight) {
c6a793d5 189 $coredata->weight = $data->weight;
c6b784f0 190 }
c6a793d5
DM
191 $DB->update_record('workshop_assessments', $coredata);
192
193 // And finally redirect the user's browser.
c6b784f0
DM
194 if (!is_null($rawgrade) and isset($data->saveandclose)) {
195 redirect($workshop->view_url());
1df3bc14
DM
196 } else if (!is_null($rawgrade) and isset($data->saveandshownext)) {
197 $next = reset($pending);
198 if (!empty($next)) {
199 redirect($workshop->assess_url($next->id));
200 } else {
201 redirect($PAGE->url); // This should never happen but just in case...
202 }
c6b784f0
DM
203 } else {
204 // either it is not possible to calculate the $rawgrade
205 // or the reviewer has chosen "Save and continue"
206 redirect($PAGE->url);
207 }
05e69e56
DM
208 }
209}
210
c6b784f0
DM
211// load the form to override gradinggrade and/or set weight and process the submitted data eventually
212if ($canoverridegrades or $cansetassessmentweight) {
213 $options = array(
214 'editable' => true,
215 'editableweight' => $cansetassessmentweight,
216 'overridablegradinggrade' => $canoverridegrades);
217 $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
77f43e7d 218 if ($data = $feedbackform->get_data()) {
e554671d 219 $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
7a789aa8 220 $record = new stdclass();
e554671d 221 $record->id = $assessment->id;
c6b784f0
DM
222 if ($cansetassessmentweight) {
223 $record->weight = $data->weight;
224 }
225 if ($canoverridegrades) {
226 $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
227 $record->gradinggradeoverby = $USER->id;
228 $record->feedbackreviewer = $data->feedbackreviewer;
229 $record->feedbackreviewerformat = $data->feedbackreviewerformat;
230 }
e554671d
DM
231 $DB->update_record('workshop_assessments', $record);
232 redirect($workshop->view_url());
77f43e7d
DM
233 }
234}
235
407b1e91 236// output starts here
81b22887
DM
237$output = $PAGE->get_renderer('mod_workshop'); // workshop renderer
238echo $output->header();
239echo $output->heading(get_string('assessedsubmission', 'workshop'), 2);
a39d7d87 240
407b1e91 241$submission = $workshop->get_submission_by_id($submission->id); // reload so can be passed to the renderer
81b22887 242echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
a39d7d87 243
5924db72 244// show instructions for assessing as they may contain important information
e0142f7e
DM
245// for evaluating the assessment
246if (trim($workshop->instructreviewers)) {
247 $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
248 'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
249 print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
367a75fa 250 echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
e0142f7e
DM
251 print_collapsible_region_end();
252}
253
38504a44
DM
254// extend the current assessment record with user details
255$assessment = $workshop->get_assessment_by_id($assessment->id);
256
77f43e7d 257if ($isreviewer) {
38504a44
DM
258 $options = array(
259 'showreviewer' => true,
260 'showauthor' => has_capability('mod/workshop:viewauthornames', $workshop->context),
261 'showform' => $assessmenteditable or !is_null($assessment->grade),
262 'showweight' => true,
263 );
264 $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
265 $assessment->title = get_string('assessmentbyyourself', 'workshop');
266 echo $output->render($assessment);
77f43e7d 267
c6b784f0 268} else {
38504a44
DM
269 $options = array(
270 'showreviewer' => has_capability('mod/workshop:viewreviewernames', $workshop->context),
271 'showauthor' => has_capability('mod/workshop:viewauthornames', $workshop->context),
272 'showform' => $assessmenteditable or !is_null($assessment->grade),
273 'showweight' => true,
274 );
275 $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
276 echo $output->render($assessment);
c6b784f0 277}
38504a44
DM
278
279if (!$assessmenteditable and $canoverridegrades) {
77f43e7d 280 $feedbackform->display();
77f43e7d 281}
38504a44 282
81b22887 283echo $output->footer();