3d7e1fb42e9e3b397b54a67a22ce1cf8639af581
[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');
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(__DIR__.'/submission_form.php');
186     $submission = file_prepare_standard_editor($submission, 'content', $workshop->submission_content_options(),
187         $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
189     $submission = file_prepare_standard_filemanager($submission, 'attachment', $workshop->submission_attachment_options(),
190         $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
192     $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
193         'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
195     if ($mform->is_cancelled()) {
196         redirect($workshop->view_url());
198     } elseif ($cansubmit and $formdata = $mform->get_data()) {
199         if ($formdata->example == 0) {
200             // this was used just for validation, it must be set to zero when dealing with normal submissions
201             unset($formdata->example);
202         } else {
203             throw new coding_exception('Invalid submission form data value: example');
204         }
205         $timenow = time();
206         if (is_null($submission->id)) {
207             $formdata->workshopid     = $workshop->id;
208             $formdata->example        = 0;
209             $formdata->authorid       = $USER->id;
210             $formdata->timecreated    = $timenow;
211             $formdata->feedbackauthorformat = editors_get_preferred_format();
212         }
213         $formdata->timemodified       = $timenow;
214         $formdata->title              = trim($formdata->title);
215         $formdata->content            = '';          // updated later
216         $formdata->contentformat      = FORMAT_HTML; // updated later
217         $formdata->contenttrust       = 0;           // updated later
218         $formdata->late               = 0x0;         // bit mask
219         if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
220             $formdata->late = $formdata->late | 0x1;
221         }
222         if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
223             $formdata->late = $formdata->late | 0x2;
224         }
226         // Event information.
227         $params = array(
228             'context' => $workshop->context,
229             'courseid' => $workshop->course->id,
230             'other' => array(
231                 'submissiontitle' => $formdata->title
232             )
233         );
234         $logdata = null;
235         if (is_null($submission->id)) {
236             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
237             $params['objectid'] = $submission->id;
238             $event = \mod_workshop\event\submission_created::create($params);
239             $event->trigger();
240         } else {
241             if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
242                 throw new moodle_exception('err_submissionid', 'workshop');
243             }
244         }
245         $params['objectid'] = $submission->id;
247         // Save and relink embedded images and save attachments.
248         $formdata = file_postupdate_standard_editor($formdata, 'content', $workshop->submission_content_options(),
249             $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
251         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
252             $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
254         if (empty($formdata->attachment)) {
255             // explicit cast to zero integer
256             $formdata->attachment = 0;
257         }
258         // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
259         $DB->update_record('workshop_submissions', $formdata);
260         $event = \mod_workshop\event\submission_updated::create($params);
261         $event->add_record_snapshot('workshop', $workshoprecord);
262         $event->trigger();
264         // send submitted content for plagiarism detection
265         $fs = get_file_storage();
266         $files = $fs->get_area_files($workshop->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
268         $params['other']['content'] = $formdata->content;
269         $params['other']['pathnamehashes'] = array_keys($files);
271         $event = \mod_workshop\event\assessable_uploaded::create($params);
272         $event->set_legacy_logdata($logdata);
273         $event->trigger();
275         redirect($workshop->submission_url($formdata->id));
276     }
279 // load the form to override grade and/or publish the submission and process the submitted data eventually
280 if (!$edit and ($canoverride or $canpublish)) {
281     $options = array(
282         'editable' => true,
283         'editablepublished' => $canpublish,
284         'overridablegrade' => $canoverride);
285     $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
286     if ($data = $feedbackform->get_data()) {
287         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
288         $record = new stdclass();
289         $record->id = $submission->id;
290         if ($canoverride) {
291             $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
292             $record->gradeoverby = $USER->id;
293             $record->feedbackauthor = $data->feedbackauthor;
294             $record->feedbackauthorformat = $data->feedbackauthorformat;
295         }
296         if ($canpublish) {
297             $record->published = !empty($data->published);
298         }
299         $DB->update_record('workshop_submissions', $record);
300         redirect($workshop->view_url());
301     }
304 $PAGE->set_title($workshop->name);
305 $PAGE->set_heading($course->fullname);
306 if ($edit) {
307     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
308     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
309 } elseif ($ownsubmission) {
310     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
311 } else {
312     $PAGE->navbar->add(get_string('submission', 'workshop'));
315 // Output starts here
316 $output = $PAGE->get_renderer('mod_workshop');
317 echo $output->header();
318 echo $output->heading(format_string($workshop->name), 2);
319 echo $output->heading(get_string('mysubmission', 'workshop'), 3);
321 // show instructions for submitting as thay may contain some list of questions and we need to know them
322 // while reading the submitted answer
323 if (trim($workshop->instructauthors)) {
324     $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
325         'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
326     print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
327     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
328     print_collapsible_region_end();
331 // if in edit mode, display the form to edit the submission
333 if ($edit) {
334     if (!empty($CFG->enableplagiarism)) {
335         require_once($CFG->libdir.'/plagiarismlib.php');
336         echo plagiarism_print_disclosure($cm->id);
337     }
338     $mform->display();
339     echo $output->footer();
340     die();
343 // Confirm deletion (if requested).
344 if ($deletable and $delete) {
345     $prompt = get_string('submissiondeleteconfirm', 'workshop');
346     if ($candeleteall) {
347         $count = count($workshop->get_assessments_of_submission($submission->id));
348         if ($count > 0) {
349             $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
350         }
351     }
352     echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
355 // else display the submission
357 if ($submission->id) {
358     if ($seenaspublished) {
359         $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
360     } else {
361         $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
362     }
363     echo $output->render($workshop->prepare_submission($submission, $showauthor));
364 } else {
365     echo $output->box(get_string('noyoursubmission', 'workshop'));
368 // If not at removal confirmation screen, some action buttons can be displayed.
369 if (!$delete) {
370     if ($editable) {
371         if ($submission->id) {
372             $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
373             $btntxt = get_string('editsubmission', 'workshop');
374         } else {
375             $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
376             $btntxt = get_string('createsubmission', 'workshop');
377         }
378         echo $output->single_button($btnurl, $btntxt, 'get');
379     }
381     if ($submission->id and $deletable) {
382         $url = new moodle_url($PAGE->url, array('delete' => 1));
383         echo $output->single_button($url, get_string('deletesubmission', 'workshop'), 'get');
384     }
386     if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
387         $url = new moodle_url($PAGE->url, array('assess' => 1));
388         echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
389     }
392 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
393     if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
394         echo $output->render(new workshop_feedback_author($submission));
395     }
398 // and possibly display the submission's review(s)
400 if ($isreviewer) {
401     // user's own assessment
402     $strategy   = $workshop->grading_strategy_instance();
403     $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
404     $options    = array(
405         'showreviewer'  => true,
406         'showauthor'    => $showauthor,
407         'showform'      => !is_null($userassessment->grade),
408         'showweight'    => true,
409     );
410     $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
411     $assessment->title = get_string('assessmentbyyourself', 'workshop');
413     if ($workshop->assessing_allowed($USER->id)) {
414         if (is_null($userassessment->grade)) {
415             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
416         } else {
417             $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
418         }
419     }
420     if ($canoverride) {
421         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
422     }
424     echo $output->render($assessment);
426     if ($workshop->phase == workshop::PHASE_CLOSED) {
427         if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
428             echo $output->render(new workshop_feedback_reviewer($userassessment));
429         }
430     }
433 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
434     // other assessments
435     $strategy       = $workshop->grading_strategy_instance();
436     $assessments    = $workshop->get_assessments_of_submission($submission->id);
437     $showreviewer   = has_capability('mod/workshop:viewreviewernames', $workshop->context);
438     foreach ($assessments as $assessment) {
439         if ($assessment->reviewerid == $USER->id) {
440             // own assessment has been displayed already
441             continue;
442         }
443         if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
444             // students do not see peer-assessment that are not graded yet
445             continue;
446         }
447         $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
448         $options    = array(
449             'showreviewer'  => $showreviewer,
450             'showauthor'    => $showauthor,
451             'showform'      => !is_null($assessment->grade),
452             'showweight'    => true,
453         );
454         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
455         if ($canoverride) {
456             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
457         }
458         echo $output->render($displayassessment);
460         if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
461             if (strlen(trim($assessment->feedbackreviewer)) > 0) {
462                 echo $output->render(new workshop_feedback_reviewer($assessment));
463             }
464         }
465     }
468 if (!$edit and $canoverride) {
469     // display a form to override the submission grade
470     $feedbackform->display();
473 echo $output->footer();