MDL-26535 workshop: new capability to control the anonymity of published submissions
[moodle.git] / mod / workshop / submission.php
CommitLineData
33e4dea6
DM
1<?php
2
53fad4b9
DM
3// This file is part of Moodle - http://moodle.org/
4//
33e4dea6
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//
33e4dea6
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/>.
17
33e4dea6 18/**
51508f25 19 * View a single (usually the own) submission, submit own work.
33e4dea6 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
33e4dea6
DM
25 */
26
27require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
127032fe 28require_once(dirname(__FILE__).'/locallib.php');
33e4dea6 29
c1e883bb
DM
30$cmid = required_param('cmid', PARAM_INT); // course module id
31$id = optional_param('id', 0, PARAM_INT); // submission id
32$edit = optional_param('edit', false, PARAM_BOOL); // open for editing?
ac239eba 33$assess = optional_param('assess', false, PARAM_BOOL); // instant assessment required
33e4dea6 34
c1e883bb
DM
35$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
33e4dea6
DM
37
38require_login($course, false, $cm);
33e4dea6 39if (isguestuser()) {
b8ead2e6 40 print_error('guestsarenotallowed');
33e4dea6
DM
41}
42
51508f25
DM
43$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44$workshop = new workshop($workshop, $cm, $course);
45
a6855934 46$PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id, 'edit' => $edit));
33e4dea6 47
127032fe 48if ($id) { // submission is specified
51508f25 49 $submission = $workshop->get_submission_by_id($id);
5450f7b6
DM
50 $workshop->log('view submission', $workshop->submission_url($submission->id), $submission->id);
51
127032fe 52} else { // no submission specified
0dc47fb9 53 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
7a789aa8 54 $submission = new stdclass();
127032fe 55 $submission->id = null;
00aca3c1 56 $submission->authorid = $USER->id;
2e7bd6c2 57 $submission->example = 0;
c6b784f0
DM
58 $submission->grade = null;
59 $submission->gradeover = null;
60719642 60 $submission->published = null;
c6b784f0 61 $submission->feedbackauthor = null;
884482fb 62 $submission->feedbackauthorformat = editors_get_preferred_format();
53fad4b9 63 }
33e4dea6 64}
c1e883bb 65
00aca3c1 66$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
67$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
68$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 69$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 70$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 71$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
5a372494
DM
72$userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
73$isreviewer = !empty($userassessment);
2f289d36
DM
74$editable = ($cansubmit and $ownsubmission);
75
76if (empty($submission->id) and !$workshop->creating_submission_allowed()) {
77 $editable = false;
78}
79if ($submission->id and !$workshop->modifying_submission_allowed()) {
80 $editable = false;
81}
82
514d8c22
DM
83if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
84 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
85 // check that all required examples have been assessed by the user
86 $examples = $workshop->get_examples_for_reviewer($USER->id);
87 foreach ($examples as $exampleid => $example) {
88 if (is_null($example->grade)) {
89 $editable = false;
90 break;
91 }
92 }
93}
94$edit = ($editable and $edit);
51508f25 95
3dc78e5b
DM
96if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
97 // ok you can go
98} elseif (is_null($submission->id) and $cansubmit) {
99 // ok you can go
100} else {
60719642 101 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
102}
103
ac239eba
DM
104if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
105 require_sesskey();
106 $assessmentid = $workshop->add_allocation($submission, $USER->id);
107 redirect($workshop->assess_url($assessmentid));
108}
109
514d8c22 110if ($edit) {
67cd00ba
DM
111 require_once(dirname(__FILE__).'/submission_form.php');
112
113 $maxfiles = $workshop->nattachments;
114 $maxbytes = $workshop->maxbytes;
115 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
116 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
117 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
64f93798 118 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 119 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
64f93798 120 'mod_workshop', 'submission_attachment', $submission->id);
67cd00ba
DM
121
122 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
123 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
124
125 if ($mform->is_cancelled()) {
126 redirect($workshop->view_url());
127
128 } elseif ($cansubmit and $formdata = $mform->get_data()) {
2e7bd6c2
DM
129 if ($formdata->example == 0) {
130 // this was used just for validation, it must be set to zero when dealing with normal submissions
131 unset($formdata->example);
132 } else {
133 throw new coding_exception('Invalid submission form data value: example');
134 }
67cd00ba 135 $timenow = time();
2e7bd6c2 136 if (is_null($submission->id)) {
67cd00ba 137 $formdata->workshopid = $workshop->id;
81eccf0a 138 $formdata->example = 0;
67cd00ba
DM
139 $formdata->authorid = $USER->id;
140 $formdata->timecreated = $timenow;
884482fb 141 $formdata->feedbackauthorformat = editors_get_preferred_format();
67cd00ba
DM
142 }
143 $formdata->timemodified = $timenow;
144 $formdata->title = trim($formdata->title);
145 $formdata->content = ''; // updated later
146 $formdata->contentformat = FORMAT_HTML; // updated later
147 $formdata->contenttrust = 0; // updated later
2f289d36
DM
148 $formdata->late = 0x0; // bit mask
149 if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
150 $formdata->late = $formdata->late | 0x1;
151 }
152 if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
153 $formdata->late = $formdata->late | 0x2;
154 }
2e7bd6c2
DM
155 if (is_null($submission->id)) {
156 $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
5450f7b6 157 $workshop->log('add submission', $workshop->submission_url($submission->id), $submission->id);
2e7bd6c2 158 } else {
5450f7b6 159 $workshop->log('update submission', $workshop->submission_url($submission->id), $submission->id);
2e7bd6c2
DM
160 if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
161 throw new moodle_exception('err_submissionid', 'workshop');
162 }
67cd00ba
DM
163 }
164 // save and relink embedded images and save attachments
165 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
2e7bd6c2 166 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 167 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
2e7bd6c2 168 'mod_workshop', 'submission_attachment', $submission->id);
f1b4b387 169 if (empty($formdata->attachment)) {
ac239eba 170 // explicit cast to zero integer
f1b4b387
DM
171 $formdata->attachment = 0;
172 }
67cd00ba
DM
173 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
174 $DB->update_record('workshop_submissions', $formdata);
175 redirect($workshop->submission_url($formdata->id));
33e4dea6 176 }
33e4dea6
DM
177}
178
232175e4
DM
179// load the form to override grade and/or publish the submission and process the submitted data eventually
180if (!$edit and ($canoverride or $canpublish)) {
181 $options = array(
182 'editable' => true,
183 'editablepublished' => $canpublish,
184 'overridablegrade' => $canoverride);
185 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
186 if ($data = $feedbackform->get_data()) {
187 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 188 $record = new stdclass();
557a1100 189 $record->id = $submission->id;
232175e4
DM
190 if ($canoverride) {
191 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
192 $record->gradeoverby = $USER->id;
193 $record->feedbackauthor = $data->feedbackauthor;
194 $record->feedbackauthorformat = $data->feedbackauthorformat;
195 }
196 if ($canpublish) {
197 $record->published = !empty($data->published);
198 }
557a1100
DM
199 $DB->update_record('workshop_submissions', $record);
200 redirect($workshop->view_url());
201 }
202}
203
0dc47fb9
DM
204$PAGE->set_title($workshop->name);
205$PAGE->set_heading($course->fullname);
39861053 206if ($edit) {
b761e6d9 207 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 208 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 209} elseif ($ownsubmission) {
b761e6d9 210 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
211} else {
212 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 213}
33e4dea6 214
c1e883bb 215// Output starts here
81b22887
DM
216$output = $PAGE->get_renderer('mod_workshop');
217echo $output->header();
218echo $output->heading(format_string($workshop->name), 2);
c1e883bb 219
e0142f7e
DM
220// show instructions for submitting as thay may contain some list of questions and we need to know them
221// while reading the submitted answer
222if (trim($workshop->instructauthors)) {
223 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
224 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
225 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
367a75fa 226 echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
e0142f7e
DM
227 print_collapsible_region_end();
228}
229
3dc78e5b
DM
230// if in edit mode, display the form to edit the submission
231
514d8c22 232if ($edit) {
c1e883bb 233 $mform->display();
81b22887 234 echo $output->footer();
c1e883bb
DM
235 die();
236}
237
3dc78e5b
DM
238// else display the submission
239
240if ($submission->id) {
81b22887 241 echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
3dc78e5b 242} else {
81b22887 243 echo $output->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
244}
245
514d8c22 246if ($editable) {
2f289d36
DM
247 if ($submission->id) {
248 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
249 $btntxt = get_string('editsubmission', 'workshop');
250 } else {
251 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
252 $btntxt = get_string('createsubmission', 'workshop');
253 }
81b22887 254 echo $output->single_button($btnurl, $btntxt, 'get');
c1e883bb
DM
255}
256
ac239eba
DM
257if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
258 $url = new moodle_url($PAGE->url, array('assess' => 1));
81b22887 259 echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
ac239eba
DM
260}
261
3dc78e5b
DM
262// and possibly display the submission's review(s)
263
3dc78e5b 264if ($isreviewer) {
3dc78e5b 265 $strategy = $workshop->grading_strategy_instance();
5a372494 266 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
81b22887 267 echo $output->heading(get_string('assessmentbyyourself', 'workshop'), 2);
5a372494
DM
268 // reviewers can always see the grades they gave even they are not available yet
269 if (is_null($userassessment->grade)) {
81b22887 270 echo $output->heading(get_string('notassessed', 'workshop'), 3);
5a372494 271 if ($workshop->assessing_allowed()) {
81b22887 272 echo $output->container($output->single_button($workshop->assess_url($userassessment->id), get_string('assess', 'workshop'), 'get'),
7a5f4be0 273 array('class' => 'buttonsbar'));
5a372494
DM
274 }
275 } else {
276 $a = new stdclass();
277 $a->max = $workshop->real_grade(100);
278 $a->received = $workshop->real_grade($userassessment->grade);
81b22887 279 echo $output->heading(get_string('gradeinfo', 'workshop', $a), 3);
5a372494 280 if ($userassessment->weight != 1) {
81b22887 281 echo $output->heading(get_string('weightinfo', 'workshop', $userassessment->weight), 3);
5a372494
DM
282 }
283 if ($workshop->assessing_allowed()) {
81b22887 284 echo $output->container($output->single_button($workshop->assess_url($userassessment->id), get_string('reassess', 'workshop'), 'get'),
7a5f4be0 285 array('class' => 'buttonsbar'));
5a372494
DM
286 }
287 $mform->display();
288 }
3dc78e5b
DM
289}
290
5a372494 291if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
3dc78e5b 292 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
293 $assessments = $workshop->get_assessments_of_submission($submission->id);
294 $canviewreviewernames = has_capability('mod/workshop:viewreviewernames', $workshop->context);
295 foreach ($assessments as $assessment) {
296 if ($assessment->reviewerid == $USER->id) {
297 // own assessment has been displayed already
298 continue;
299 }
300 if (is_null($assessment->grade)) {
301 // not graded assessment are not displayed
302 continue;
303 }
304 if ($canviewreviewernames) {
305 $reviewer = new stdclass();
306 $reviewer->firstname = $assessment->reviewerfirstname;
307 $reviewer->lastname = $assessment->reviewerlastname;
81b22887 308 echo $output->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
5a372494 309 } else {
81b22887 310 echo $output->heading(get_string('assessmentbyunknown', 'workshop'), 2);
5a372494
DM
311 }
312 $a = new stdclass();
313 $a->max = $workshop->real_grade(100);
314 $a->received = $workshop->real_grade($assessment->grade);
81b22887 315 echo $output->heading(get_string('gradeinfo', 'workshop', $a), 3);
5a372494 316 if ($assessment->weight != 1) {
81b22887 317 echo $output->heading(get_string('weightinfo', 'workshop', $assessment->weight), 3);
5a372494 318 }
7a5f4be0 319 if (has_capability('mod/workshop:overridegrades', $workshop->context)) {
81b22887 320 echo $output->container($output->single_button($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'), 'get'),
7a5f4be0
DM
321 array('class' => 'buttonsbar'));
322 }
5a372494
DM
323 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
324 $mform->display();
325 }
3dc78e5b
DM
326}
327
557a1100
DM
328if (!$edit and $canoverride) {
329 // display a form to override the submission grade
330 $feedbackform->display();
331}
332
81b22887 333echo $output->footer();