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