MDL-42190 workshop: Improve the confirmation of submission removal
[moodle.git] / mod / workshop / submission.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  * View a single (usually the own) submission, submit own work.
20  *
21  * @package    mod_workshop
22  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27 require_once(dirname(__FILE__).'/locallib.php');
28 require_once($CFG->dirroot . '/repository/lib.php');
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.
37 $cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
38 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
40 require_login($course, false, $cm);
41 if (isguestuser()) {
42     print_error('guestsarenotallowed');
43 }
45 $workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
46 $workshop = new workshop($workshoprecord, $cm, $course);
48 $PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
50 if ($edit) {
51     $PAGE->url->param('edit', $edit);
52 }
54 if ($id) { // submission is specified
55     $submission = $workshop->get_submission_by_id($id);
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     );
67     $event = \mod_workshop\event\submission_viewed::create($params);
68     $event->trigger();
70 } else { // no submission specified
71     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
72         $submission = new stdclass();
73         $submission->id = null;
74         $submission->authorid = $USER->id;
75         $submission->example = 0;
76         $submission->grade = null;
77         $submission->gradeover = null;
78         $submission->published = null;
79         $submission->feedbackauthor = null;
80         $submission->feedbackauthorformat = editors_get_preferred_format();
81     }
82 }
84 $ownsubmission  = $submission->authorid == $USER->id;
85 $canviewall     = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
86 $cansubmit      = has_capability('mod/workshop:submit', $workshop->context);
87 $canallocate    = has_capability('mod/workshop:allocate', $workshop->context);
88 $canpublish     = has_capability('mod/workshop:publishsubmissions', $workshop->context);
89 $canoverride    = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
90 $candeleteall   = has_capability('mod/workshop:deletesubmissions', $workshop->context);
91 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
92 $isreviewer     = !empty($userassessment);
93 $editable       = ($cansubmit and $ownsubmission);
94 $deletable      = $candeleteall;
95 $ispublished    = ($workshop->phase == workshop::PHASE_CLOSED
96                     and $submission->published == 1
97                     and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
99 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
100     $editable = false;
102 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
103     $editable = false;
106 if ($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     }
121 if ($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     }
132 $edit = ($editable and $edit);
134 if (!$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     }
143 if ($submission->id and $delete and $confirm and $deletable) {
144     require_sesskey();
145     $workshop->delete_submission($submission);
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();
161     redirect($workshop->view_url());
164 $seenaspublished = false; // is the submission seen as a published submission?
166 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
167     // ok you can go
168 } elseif ($submission->id and $ispublished) {
169     // ok you can go
170     $seenaspublished = true;
171 } elseif (is_null($submission->id) and $cansubmit) {
172     // ok you can go
173 } else {
174     print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
177 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
178     require_sesskey();
179     $assessmentid = $workshop->add_allocation($submission, $USER->id);
180     redirect($workshop->assess_url($assessmentid));
183 if ($edit) {
184     require_once(dirname(__FILE__).'/submission_form.php');
186     $maxfiles       = $workshop->nattachments;
187     $maxbytes       = $workshop->maxbytes;
188     $contentopts    = array(
189                         'trusttext' => true,
190                         'subdirs'   => false,
191                         'maxfiles'  => $maxfiles,
192                         'maxbytes'  => $maxbytes,
193                         'context'   => $workshop->context,
194                         'return_types' => FILE_INTERNAL | FILE_EXTERNAL
195                       );
197     $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes, 'return_types' => FILE_INTERNAL);
198     $submission     = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
199                                         'mod_workshop', 'submission_content', $submission->id);
200     $submission     = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
201                                         'mod_workshop', 'submission_attachment', $submission->id);
203     $mform          = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
204                                                     'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
206     if ($mform->is_cancelled()) {
207         redirect($workshop->view_url());
209     } elseif ($cansubmit and $formdata = $mform->get_data()) {
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         }
216         $timenow = time();
217         if (is_null($submission->id)) {
218             $formdata->workshopid     = $workshop->id;
219             $formdata->example        = 0;
220             $formdata->authorid       = $USER->id;
221             $formdata->timecreated    = $timenow;
222             $formdata->feedbackauthorformat = editors_get_preferred_format();
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
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         }
237         // Event information.
238         $params = array(
239             'context' => $workshop->context,
240             'courseid' => $workshop->course->id,
241             'other' => array(
242                 'submissiontitle' => $formdata->title
243             )
244         );
245         $logdata = null;
246         if (is_null($submission->id)) {
247             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
248             $params['objectid'] = $submission->id;
249             $event = \mod_workshop\event\submission_created::create($params);
250             $event->trigger();
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             }
255         }
256         $params['objectid'] = $submission->id;
257         // save and relink embedded images and save attachments
258         $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
259                                                       'mod_workshop', 'submission_content', $submission->id);
260         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
261                                                            'mod_workshop', 'submission_attachment', $submission->id);
262         if (empty($formdata->attachment)) {
263             // explicit cast to zero integer
264             $formdata->attachment = 0;
265         }
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);
268         $event = \mod_workshop\event\submission_updated::create($params);
269         $event->add_record_snapshot('workshop', $workshoprecord);
270         $event->trigger();
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);
276         $params['other']['content'] = $formdata->content;
277         $params['other']['pathnamehashes'] = array_keys($files);
279         $event = \mod_workshop\event\assessable_uploaded::create($params);
280         $event->set_legacy_logdata($logdata);
281         $event->trigger();
283         redirect($workshop->submission_url($formdata->id));
284     }
287 // load the form to override grade and/or publish the submission and process the submitted data eventually
288 if (!$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);
294     if ($data = $feedbackform->get_data()) {
295         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
296         $record = new stdclass();
297         $record->id = $submission->id;
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         }
307         $DB->update_record('workshop_submissions', $record);
308         redirect($workshop->view_url());
309     }
312 $PAGE->set_title($workshop->name);
313 $PAGE->set_heading($course->fullname);
314 if ($edit) {
315     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
316     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
317 } elseif ($ownsubmission) {
318     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
319 } else {
320     $PAGE->navbar->add(get_string('submission', 'workshop'));
323 // Output starts here
324 $output = $PAGE->get_renderer('mod_workshop');
325 echo $output->header();
326 echo $output->heading(format_string($workshop->name), 2);
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
330 if (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'));
334     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
335     print_collapsible_region_end();
338 // if in edit mode, display the form to edit the submission
340 if ($edit) {
341     if (!empty($CFG->enableplagiarism)) {
342         require_once($CFG->libdir.'/plagiarismlib.php');
343         echo plagiarism_print_disclosure($cm->id);
344     }
345     $mform->display();
346     echo $output->footer();
347     die();
350 // Confirm deletion (if requested).
351 if ($deletable and $delete) {
352     $prompt = get_string('submissiondeleteconfirm', 'workshop');
353     if ($candeleteall) {
354         $count = count($workshop->get_assessments_of_submission($submission->id));
355         if ($count > 0) {
356             $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
357         }
358     }
359     echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
362 // else display the submission
364 if ($submission->id) {
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));
371 } else {
372     echo $output->box(get_string('noyoursubmission', 'workshop'));
375 // If not at removal confirmation screen, some action buttons can be displayed.
376 if (!$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');
386     }
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     }
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     }
399 if (($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     }
405 // and possibly display the submission's review(s)
407 if ($isreviewer) {
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');
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'));
425         }
426     }
427     if ($canoverride) {
428         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
429     }
431     echo $output->render($assessment);
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     }
440 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
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);
445     foreach ($assessments as $assessment) {
446         if ($assessment->reviewerid == $USER->id) {
447             // own assessment has been displayed already
448             continue;
449         }
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         }
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         );
461         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
462         if ($canoverride) {
463             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
464         }
465         echo $output->render($displayassessment);
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             }
471         }
472     }
475 if (!$edit and $canoverride) {
476     // display a form to override the submission grade
477     $feedbackform->display();
480 echo $output->footer();