MDL-55289 workshop: Include repository/lib.php to use its constants
[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 *
5cdcfcb9 21 * @package mod_workshop
65601f04
DM
22 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33e4dea6
DM
24 */
25
1fcf0ca8
RS
26require(__DIR__.'/../../config.php');
27require_once(__DIR__.'/locallib.php');
33e4dea6 28
3fa22899
DM
29$cmid = required_param('cmid', PARAM_INT); // Course module id.
30$id = optional_param('id', 0, PARAM_INT); // Submission id.
31$edit = optional_param('edit', false, PARAM_BOOL); // Open the page for editing?
32$assess = optional_param('assess', false, PARAM_BOOL); // Instant assessment required.
33$delete = optional_param('delete', false, PARAM_BOOL); // Submission removal requested.
34$confirm = optional_param('confirm', false, PARAM_BOOL); // Submission removal request confirmed.
33e4dea6 35
3fa22899
DM
36$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
37$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
33e4dea6
DM
38
39require_login($course, false, $cm);
33e4dea6 40if (isguestuser()) {
b8ead2e6 41 print_error('guestsarenotallowed');
33e4dea6
DM
42}
43
fef8ee91
RT
44$workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
45$workshop = new workshop($workshoprecord, $cm, $course);
51508f25 46
2c75324e
DM
47$PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
48
49if ($edit) {
50 $PAGE->url->param('edit', $edit);
51}
33e4dea6 52
127032fe 53if ($id) { // submission is specified
51508f25 54 $submission = $workshop->get_submission_by_id($id);
1f013271
AG
55
56 $params = array(
57 'objectid' => $submission->id,
58 'context' => $workshop->context,
59 'courseid' => $workshop->course->id,
60 'relateduserid' => $submission->authorid,
61 'other' => array(
62 'workshopid' => $workshop->id
63 )
64 );
65
66 $event = \mod_workshop\event\submission_viewed::create($params);
67 $event->trigger();
5450f7b6 68
127032fe 69} else { // no submission specified
0dc47fb9 70 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
7a789aa8 71 $submission = new stdclass();
127032fe 72 $submission->id = null;
00aca3c1 73 $submission->authorid = $USER->id;
2e7bd6c2 74 $submission->example = 0;
c6b784f0
DM
75 $submission->grade = null;
76 $submission->gradeover = null;
60719642 77 $submission->published = null;
c6b784f0 78 $submission->feedbackauthor = null;
884482fb 79 $submission->feedbackauthorformat = editors_get_preferred_format();
53fad4b9 80 }
33e4dea6 81}
c1e883bb 82
00aca3c1 83$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
84$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
85$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 86$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 87$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 88$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
deabe48b 89$candeleteall = has_capability('mod/workshop:deletesubmissions', $workshop->context);
5a372494
DM
90$userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
91$isreviewer = !empty($userassessment);
2f289d36 92$editable = ($cansubmit and $ownsubmission);
deabe48b 93$deletable = $candeleteall;
d67c20b8
DM
94$ispublished = ($workshop->phase == workshop::PHASE_CLOSED
95 and $submission->published == 1
96 and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
2f289d36 97
9ddff589 98if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
2f289d36
DM
99 $editable = false;
100}
9ddff589 101if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
2f289d36
DM
102 $editable = false;
103}
104
f65bc55f
DM
105if ($canviewall) {
106 // check this flag against the group membership yet
107 if (groups_get_activity_groupmode($workshop->cm) == SEPARATEGROUPS) {
108 // user must have accessallgroups or share at least one group with the submission author
109 if (!has_capability('moodle/site:accessallgroups', $workshop->context)) {
110 $usersgroups = groups_get_activity_allowed_groups($workshop->cm);
111 $authorsgroups = groups_get_all_groups($workshop->course->id, $submission->authorid, $workshop->cm->groupingid, 'g.id');
112 $sharedgroups = array_intersect_key($usersgroups, $authorsgroups);
113 if (empty($sharedgroups)) {
114 $canviewall = false;
115 }
116 }
117 }
118}
119
514d8c22
DM
120if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
121 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
122 // check that all required examples have been assessed by the user
123 $examples = $workshop->get_examples_for_reviewer($USER->id);
124 foreach ($examples as $exampleid => $example) {
125 if (is_null($example->grade)) {
126 $editable = false;
127 break;
128 }
129 }
130}
131$edit = ($editable and $edit);
51508f25 132
deabe48b
PN
133if (!$candeleteall and $ownsubmission and $editable) {
134 // Only allow the student to delete their own submission if it's still editable and hasn't been assessed.
135 if (count($workshop->get_assessments_of_submission($submission->id)) > 0) {
136 $deletable = false;
137 } else {
138 $deletable = true;
139 }
140}
141
142if ($submission->id and $delete and $confirm and $deletable) {
143 require_sesskey();
144 $workshop->delete_submission($submission);
30c52da9
PN
145
146 // Event information.
147 $params = array(
148 'context' => $workshop->context,
149 'courseid' => $workshop->course->id,
150 'relateduserid' => $submission->authorid,
151 'other' => array(
152 'submissiontitle' => $submission->title
153 )
154 );
155 $params['objectid'] = $submission->id;
156 $event = \mod_workshop\event\submission_deleted::create($params);
157 $event->add_record_snapshot('workshop', $workshoprecord);
158 $event->trigger();
159
deabe48b
PN
160 redirect($workshop->view_url());
161}
162
d67c20b8
DM
163$seenaspublished = false; // is the submission seen as a published submission?
164
3dc78e5b
DM
165if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
166 // ok you can go
d67c20b8
DM
167} elseif ($submission->id and $ispublished) {
168 // ok you can go
169 $seenaspublished = true;
3dc78e5b
DM
170} elseif (is_null($submission->id) and $cansubmit) {
171 // ok you can go
172} else {
60719642 173 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
174}
175
9ddff589 176if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
ac239eba
DM
177 require_sesskey();
178 $assessmentid = $workshop->add_allocation($submission, $USER->id);
179 redirect($workshop->assess_url($assessmentid));
180}
181
514d8c22 182if ($edit) {
1fcf0ca8 183 require_once(__DIR__.'/submission_form.php');
67cd00ba 184
996f7e82
DM
185 $submission = file_prepare_standard_editor($submission, 'content', $workshop->submission_content_options(),
186 $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
187
188 $submission = file_prepare_standard_filemanager($submission, 'attachment', $workshop->submission_attachment_options(),
189 $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
190
191 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
192 'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
67cd00ba
DM
193
194 if ($mform->is_cancelled()) {
195 redirect($workshop->view_url());
196
197 } elseif ($cansubmit and $formdata = $mform->get_data()) {
2e7bd6c2
DM
198 if ($formdata->example == 0) {
199 // this was used just for validation, it must be set to zero when dealing with normal submissions
200 unset($formdata->example);
201 } else {
202 throw new coding_exception('Invalid submission form data value: example');
203 }
67cd00ba 204 $timenow = time();
2e7bd6c2 205 if (is_null($submission->id)) {
67cd00ba 206 $formdata->workshopid = $workshop->id;
81eccf0a 207 $formdata->example = 0;
67cd00ba
DM
208 $formdata->authorid = $USER->id;
209 $formdata->timecreated = $timenow;
884482fb 210 $formdata->feedbackauthorformat = editors_get_preferred_format();
67cd00ba
DM
211 }
212 $formdata->timemodified = $timenow;
213 $formdata->title = trim($formdata->title);
214 $formdata->content = ''; // updated later
215 $formdata->contentformat = FORMAT_HTML; // updated later
216 $formdata->contenttrust = 0; // updated later
2f289d36
DM
217 $formdata->late = 0x0; // bit mask
218 if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
219 $formdata->late = $formdata->late | 0x1;
220 }
221 if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
222 $formdata->late = $formdata->late | 0x2;
223 }
1f013271
AG
224
225 // Event information.
226 $params = array(
227 'context' => $workshop->context,
228 'courseid' => $workshop->course->id,
229 'other' => array(
230 'submissiontitle' => $formdata->title
231 )
232 );
cbf4b0e6 233 $logdata = null;
2e7bd6c2
DM
234 if (is_null($submission->id)) {
235 $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
1f013271
AG
236 $params['objectid'] = $submission->id;
237 $event = \mod_workshop\event\submission_created::create($params);
238 $event->trigger();
2e7bd6c2
DM
239 } else {
240 if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
241 throw new moodle_exception('err_submissionid', 'workshop');
242 }
67cd00ba 243 }
1f013271 244 $params['objectid'] = $submission->id;
996f7e82
DM
245
246 // Save and relink embedded images and save attachments.
247 $formdata = file_postupdate_standard_editor($formdata, 'content', $workshop->submission_content_options(),
248 $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
249
250 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
251 $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
252
f1b4b387 253 if (empty($formdata->attachment)) {
ac239eba 254 // explicit cast to zero integer
f1b4b387
DM
255 $formdata->attachment = 0;
256 }
67cd00ba
DM
257 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
258 $DB->update_record('workshop_submissions', $formdata);
1f013271 259 $event = \mod_workshop\event\submission_updated::create($params);
fef8ee91 260 $event->add_record_snapshot('workshop', $workshoprecord);
1f013271 261 $event->trigger();
67ba9007
KG
262
263 // send submitted content for plagiarism detection
264 $fs = get_file_storage();
265 $files = $fs->get_area_files($workshop->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
cbf4b0e6 266
1f013271
AG
267 $params['other']['content'] = $formdata->content;
268 $params['other']['pathnamehashes'] = array_keys($files);
269
cbf4b0e6
FM
270 $event = \mod_workshop\event\assessable_uploaded::create($params);
271 $event->set_legacy_logdata($logdata);
272 $event->trigger();
67ba9007 273
67cd00ba 274 redirect($workshop->submission_url($formdata->id));
33e4dea6 275 }
33e4dea6
DM
276}
277
232175e4
DM
278// load the form to override grade and/or publish the submission and process the submitted data eventually
279if (!$edit and ($canoverride or $canpublish)) {
280 $options = array(
281 'editable' => true,
282 'editablepublished' => $canpublish,
283 'overridablegrade' => $canoverride);
284 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
285 if ($data = $feedbackform->get_data()) {
286 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 287 $record = new stdclass();
557a1100 288 $record->id = $submission->id;
232175e4
DM
289 if ($canoverride) {
290 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
291 $record->gradeoverby = $USER->id;
292 $record->feedbackauthor = $data->feedbackauthor;
293 $record->feedbackauthorformat = $data->feedbackauthorformat;
294 }
295 if ($canpublish) {
296 $record->published = !empty($data->published);
297 }
557a1100
DM
298 $DB->update_record('workshop_submissions', $record);
299 redirect($workshop->view_url());
300 }
301}
302
0dc47fb9
DM
303$PAGE->set_title($workshop->name);
304$PAGE->set_heading($course->fullname);
39861053 305if ($edit) {
b761e6d9 306 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 307 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 308} elseif ($ownsubmission) {
b761e6d9 309 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
310} else {
311 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 312}
33e4dea6 313
c1e883bb 314// Output starts here
81b22887
DM
315$output = $PAGE->get_renderer('mod_workshop');
316echo $output->header();
317echo $output->heading(format_string($workshop->name), 2);
ddc9cff6 318echo $output->heading(get_string('mysubmission', 'workshop'), 3);
c1e883bb 319
e0142f7e
DM
320// show instructions for submitting as thay may contain some list of questions and we need to know them
321// while reading the submitted answer
322if (trim($workshop->instructauthors)) {
323 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
324 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
325 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
367a75fa 326 echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
e0142f7e
DM
327 print_collapsible_region_end();
328}
329
3dc78e5b
DM
330// if in edit mode, display the form to edit the submission
331
514d8c22 332if ($edit) {
67ba9007
KG
333 if (!empty($CFG->enableplagiarism)) {
334 require_once($CFG->libdir.'/plagiarismlib.php');
335 echo plagiarism_print_disclosure($cm->id);
336 }
c1e883bb 337 $mform->display();
81b22887 338 echo $output->footer();
c1e883bb
DM
339 die();
340}
341
deabe48b
PN
342// Confirm deletion (if requested).
343if ($deletable and $delete) {
344 $prompt = get_string('submissiondeleteconfirm', 'workshop');
345 if ($candeleteall) {
58489e11
DM
346 $count = count($workshop->get_assessments_of_submission($submission->id));
347 if ($count > 0) {
348 $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
deabe48b
PN
349 }
350 }
58489e11 351 echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
deabe48b
PN
352}
353
3dc78e5b
DM
354// else display the submission
355
356if ($submission->id) {
d67c20b8
DM
357 if ($seenaspublished) {
358 $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
359 } else {
360 $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
361 }
362 echo $output->render($workshop->prepare_submission($submission, $showauthor));
3dc78e5b 363} else {
81b22887 364 echo $output->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
365}
366
58489e11
DM
367// If not at removal confirmation screen, some action buttons can be displayed.
368if (!$delete) {
369 if ($editable) {
370 if ($submission->id) {
371 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
372 $btntxt = get_string('editsubmission', 'workshop');
373 } else {
374 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
375 $btntxt = get_string('createsubmission', 'workshop');
376 }
377 echo $output->single_button($btnurl, $btntxt, 'get');
2f289d36 378 }
c1e883bb 379
58489e11
DM
380 if ($submission->id and $deletable) {
381 $url = new moodle_url($PAGE->url, array('delete' => 1));
382 echo $output->single_button($url, get_string('deletesubmission', 'workshop'), 'get');
383 }
deabe48b 384
58489e11
DM
385 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
386 $url = new moodle_url($PAGE->url, array('assess' => 1));
387 echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
388 }
ac239eba
DM
389}
390
0dfb4bad
DM
391if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
392 if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
393 echo $output->render(new workshop_feedback_author($submission));
394 }
395}
396
3dc78e5b
DM
397// and possibly display the submission's review(s)
398
3dc78e5b 399if ($isreviewer) {
38504a44
DM
400 // user's own assessment
401 $strategy = $workshop->grading_strategy_instance();
402 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
403 $options = array(
404 'showreviewer' => true,
405 'showauthor' => $showauthor,
406 'showform' => !is_null($userassessment->grade),
407 'showweight' => true,
408 );
409 $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
410 $assessment->title = get_string('assessmentbyyourself', 'workshop');
411
412 if ($workshop->assessing_allowed($USER->id)) {
413 if (is_null($userassessment->grade)) {
414 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
415 } else {
416 $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
5a372494 417 }
5a372494 418 }
38504a44
DM
419 if ($canoverride) {
420 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
421 }
422
423 echo $output->render($assessment);
f68648e9
DM
424
425 if ($workshop->phase == workshop::PHASE_CLOSED) {
426 if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
427 echo $output->render(new workshop_feedback_reviewer($userassessment));
428 }
429 }
3dc78e5b
DM
430}
431
5a372494 432if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
38504a44
DM
433 // other assessments
434 $strategy = $workshop->grading_strategy_instance();
435 $assessments = $workshop->get_assessments_of_submission($submission->id);
436 $showreviewer = has_capability('mod/workshop:viewreviewernames', $workshop->context);
5a372494
DM
437 foreach ($assessments as $assessment) {
438 if ($assessment->reviewerid == $USER->id) {
439 // own assessment has been displayed already
440 continue;
441 }
3779dcae
DM
442 if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
443 // students do not see peer-assessment that are not graded yet
444 continue;
445 }
38504a44
DM
446 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
447 $options = array(
448 'showreviewer' => $showreviewer,
449 'showauthor' => $showauthor,
450 'showform' => !is_null($assessment->grade),
451 'showweight' => true,
452 );
f68648e9 453 $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
38504a44 454 if ($canoverride) {
f68648e9
DM
455 $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
456 }
457 echo $output->render($displayassessment);
458
459 if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
460 if (strlen(trim($assessment->feedbackreviewer)) > 0) {
461 echo $output->render(new workshop_feedback_reviewer($assessment));
462 }
7a5f4be0 463 }
5a372494 464 }
3dc78e5b
DM
465}
466
557a1100
DM
467if (!$edit and $canoverride) {
468 // display a form to override the submission grade
469 $feedbackform->display();
470}
471
81b22887 472echo $output->footer();