MDL-42190 workshop: Fix the coding style of the patch
[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         $assessments = $workshop->get_assessments_of_submission($submission->id);
355         if (count($assessments) > 0) {
356             $prompt = html_writer::tag('p', $prompt);
357             $prompt .= html_writer::tag('p', get_string('submissiondeleteconfirmteacher', 'workshop'));
358             $affected = '';
359             $fields = get_all_user_name_fields(true);
360             $reviewers = array();
361             foreach ($assessments as $assessment) {
362                 if (!in_array($assessment->reviewerid, $reviewers)) {
363                     $reviewers[] = $assessment->reviewerid;
364                     $names = $DB->get_record('user', array('id' => $assessment->reviewerid), $fields);
365                     $affected .= html_writer::tag('li', fullname($names));
366                 }
367             }
368             $prompt .= html_writer::tag('ul', $affected);
369         }
370     }
371     echo $output->confirm($prompt,
372             new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
375 // else display the submission
377 if ($submission->id) {
378     if ($seenaspublished) {
379         $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
380     } else {
381         $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
382     }
383     echo $output->render($workshop->prepare_submission($submission, $showauthor));
384 } else {
385     echo $output->box(get_string('noyoursubmission', 'workshop'));
388 if ($editable) {
389     if ($submission->id) {
390         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
391         $btntxt = get_string('editsubmission', 'workshop');
392     } else {
393         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
394         $btntxt = get_string('createsubmission', 'workshop');
395     }
396     echo $output->single_button($btnurl, $btntxt, 'get');
399 if ($submission->id and $deletable) {
400     $url = new moodle_url($PAGE->url, array('delete' => 1));
401     echo $output->single_button($url, get_string('deletesubmission', 'workshop'), 'get');
404 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
405     $url = new moodle_url($PAGE->url, array('assess' => 1));
406     echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
409 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
410     if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
411         echo $output->render(new workshop_feedback_author($submission));
412     }
415 // and possibly display the submission's review(s)
417 if ($isreviewer) {
418     // user's own assessment
419     $strategy   = $workshop->grading_strategy_instance();
420     $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
421     $options    = array(
422         'showreviewer'  => true,
423         'showauthor'    => $showauthor,
424         'showform'      => !is_null($userassessment->grade),
425         'showweight'    => true,
426     );
427     $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
428     $assessment->title = get_string('assessmentbyyourself', 'workshop');
430     if ($workshop->assessing_allowed($USER->id)) {
431         if (is_null($userassessment->grade)) {
432             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
433         } else {
434             $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
435         }
436     }
437     if ($canoverride) {
438         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
439     }
441     echo $output->render($assessment);
443     if ($workshop->phase == workshop::PHASE_CLOSED) {
444         if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
445             echo $output->render(new workshop_feedback_reviewer($userassessment));
446         }
447     }
450 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
451     // other assessments
452     $strategy       = $workshop->grading_strategy_instance();
453     $assessments    = $workshop->get_assessments_of_submission($submission->id);
454     $showreviewer   = has_capability('mod/workshop:viewreviewernames', $workshop->context);
455     foreach ($assessments as $assessment) {
456         if ($assessment->reviewerid == $USER->id) {
457             // own assessment has been displayed already
458             continue;
459         }
460         if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
461             // students do not see peer-assessment that are not graded yet
462             continue;
463         }
464         $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
465         $options    = array(
466             'showreviewer'  => $showreviewer,
467             'showauthor'    => $showauthor,
468             'showform'      => !is_null($assessment->grade),
469             'showweight'    => true,
470         );
471         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
472         if ($canoverride) {
473             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
474         }
475         echo $output->render($displayassessment);
477         if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
478             if (strlen(trim($assessment->feedbackreviewer)) > 0) {
479                 echo $output->render(new workshop_feedback_reviewer($assessment));
480             }
481         }
482     }
485 if (!$edit and $canoverride) {
486     // display a form to override the submission grade
487     $feedbackform->display();
490 echo $output->footer();