MDL-55289 workshop: Include repository/lib.php to use its constants
[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(__DIR__.'/../../config.php');
27 require_once(__DIR__.'/locallib.php');
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.
36 $cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
39 require_login($course, false, $cm);
40 if (isguestuser()) {
41     print_error('guestsarenotallowed');
42 }
44 $workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
45 $workshop = new workshop($workshoprecord, $cm, $course);
47 $PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
49 if ($edit) {
50     $PAGE->url->param('edit', $edit);
51 }
53 if ($id) { // submission is specified
54     $submission = $workshop->get_submission_by_id($id);
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     );
66     $event = \mod_workshop\event\submission_viewed::create($params);
67     $event->trigger();
69 } else { // no submission specified
70     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
71         $submission = new stdclass();
72         $submission->id = null;
73         $submission->authorid = $USER->id;
74         $submission->example = 0;
75         $submission->grade = null;
76         $submission->gradeover = null;
77         $submission->published = null;
78         $submission->feedbackauthor = null;
79         $submission->feedbackauthorformat = editors_get_preferred_format();
80     }
81 }
83 $ownsubmission  = $submission->authorid == $USER->id;
84 $canviewall     = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
85 $cansubmit      = has_capability('mod/workshop:submit', $workshop->context);
86 $canallocate    = has_capability('mod/workshop:allocate', $workshop->context);
87 $canpublish     = has_capability('mod/workshop:publishsubmissions', $workshop->context);
88 $canoverride    = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
89 $candeleteall   = has_capability('mod/workshop:deletesubmissions', $workshop->context);
90 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
91 $isreviewer     = !empty($userassessment);
92 $editable       = ($cansubmit and $ownsubmission);
93 $deletable      = $candeleteall;
94 $ispublished    = ($workshop->phase == workshop::PHASE_CLOSED
95                     and $submission->published == 1
96                     and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
98 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
99     $editable = false;
101 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
102     $editable = false;
105 if ($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     }
120 if ($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     }
131 $edit = ($editable and $edit);
133 if (!$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     }
142 if ($submission->id and $delete and $confirm and $deletable) {
143     require_sesskey();
144     $workshop->delete_submission($submission);
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();
160     redirect($workshop->view_url());
163 $seenaspublished = false; // is the submission seen as a published submission?
165 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
166     // ok you can go
167 } elseif ($submission->id and $ispublished) {
168     // ok you can go
169     $seenaspublished = true;
170 } elseif (is_null($submission->id) and $cansubmit) {
171     // ok you can go
172 } else {
173     print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
176 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
177     require_sesskey();
178     $assessmentid = $workshop->add_allocation($submission, $USER->id);
179     redirect($workshop->assess_url($assessmentid));
182 if ($edit) {
183     require_once(__DIR__.'/submission_form.php');
185     $submission = file_prepare_standard_editor($submission, 'content', $workshop->submission_content_options(),
186         $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
188     $submission = file_prepare_standard_filemanager($submission, 'attachment', $workshop->submission_attachment_options(),
189         $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
191     $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
192         'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
194     if ($mform->is_cancelled()) {
195         redirect($workshop->view_url());
197     } elseif ($cansubmit and $formdata = $mform->get_data()) {
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         }
204         $timenow = time();
205         if (is_null($submission->id)) {
206             $formdata->workshopid     = $workshop->id;
207             $formdata->example        = 0;
208             $formdata->authorid       = $USER->id;
209             $formdata->timecreated    = $timenow;
210             $formdata->feedbackauthorformat = editors_get_preferred_format();
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
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         }
225         // Event information.
226         $params = array(
227             'context' => $workshop->context,
228             'courseid' => $workshop->course->id,
229             'other' => array(
230                 'submissiontitle' => $formdata->title
231             )
232         );
233         $logdata = null;
234         if (is_null($submission->id)) {
235             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
236             $params['objectid'] = $submission->id;
237             $event = \mod_workshop\event\submission_created::create($params);
238             $event->trigger();
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             }
243         }
244         $params['objectid'] = $submission->id;
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);
250         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
251             $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
253         if (empty($formdata->attachment)) {
254             // explicit cast to zero integer
255             $formdata->attachment = 0;
256         }
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);
259         $event = \mod_workshop\event\submission_updated::create($params);
260         $event->add_record_snapshot('workshop', $workshoprecord);
261         $event->trigger();
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);
267         $params['other']['content'] = $formdata->content;
268         $params['other']['pathnamehashes'] = array_keys($files);
270         $event = \mod_workshop\event\assessable_uploaded::create($params);
271         $event->set_legacy_logdata($logdata);
272         $event->trigger();
274         redirect($workshop->submission_url($formdata->id));
275     }
278 // load the form to override grade and/or publish the submission and process the submitted data eventually
279 if (!$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);
285     if ($data = $feedbackform->get_data()) {
286         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
287         $record = new stdclass();
288         $record->id = $submission->id;
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         }
298         $DB->update_record('workshop_submissions', $record);
299         redirect($workshop->view_url());
300     }
303 $PAGE->set_title($workshop->name);
304 $PAGE->set_heading($course->fullname);
305 if ($edit) {
306     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
307     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
308 } elseif ($ownsubmission) {
309     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
310 } else {
311     $PAGE->navbar->add(get_string('submission', 'workshop'));
314 // Output starts here
315 $output = $PAGE->get_renderer('mod_workshop');
316 echo $output->header();
317 echo $output->heading(format_string($workshop->name), 2);
318 echo $output->heading(get_string('mysubmission', 'workshop'), 3);
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
322 if (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'));
326     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
327     print_collapsible_region_end();
330 // if in edit mode, display the form to edit the submission
332 if ($edit) {
333     if (!empty($CFG->enableplagiarism)) {
334         require_once($CFG->libdir.'/plagiarismlib.php');
335         echo plagiarism_print_disclosure($cm->id);
336     }
337     $mform->display();
338     echo $output->footer();
339     die();
342 // Confirm deletion (if requested).
343 if ($deletable and $delete) {
344     $prompt = get_string('submissiondeleteconfirm', 'workshop');
345     if ($candeleteall) {
346         $count = count($workshop->get_assessments_of_submission($submission->id));
347         if ($count > 0) {
348             $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
349         }
350     }
351     echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
354 // else display the submission
356 if ($submission->id) {
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));
363 } else {
364     echo $output->box(get_string('noyoursubmission', 'workshop'));
367 // If not at removal confirmation screen, some action buttons can be displayed.
368 if (!$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');
378     }
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     }
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     }
391 if (($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     }
397 // and possibly display the submission's review(s)
399 if ($isreviewer) {
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');
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'));
417         }
418     }
419     if ($canoverride) {
420         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
421     }
423     echo $output->render($assessment);
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     }
432 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
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);
437     foreach ($assessments as $assessment) {
438         if ($assessment->reviewerid == $USER->id) {
439             // own assessment has been displayed already
440             continue;
441         }
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         }
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         );
453         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
454         if ($canoverride) {
455             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
456         }
457         echo $output->render($displayassessment);
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             }
463         }
464     }
467 if (!$edit and $canoverride) {
468     // display a form to override the submission grade
469     $feedbackform->display();
472 echo $output->footer();