f93967ba683d404fd7eee1828f32b3ad1092ff9f
[moodle.git] / mod / workshop / submission.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * View a single (usually the own) submission, submit own work.
19  *
20  * @package    mod_workshop
21  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require(__DIR__.'/../../config.php');
26 require_once(__DIR__.'/locallib.php');
28 $cmid = required_param('cmid', PARAM_INT); // Course module id.
29 $id = optional_param('id', 0, PARAM_INT); // Submission id.
30 $edit = optional_param('edit', false, PARAM_BOOL); // Open the page for editing?
31 $assess = optional_param('assess', false, PARAM_BOOL); // Instant assessment required.
32 $delete = optional_param('delete', false, PARAM_BOOL); // Submission removal requested.
33 $confirm = optional_param('confirm', false, PARAM_BOOL); // Submission removal request confirmed.
35 $cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38 require_login($course, false, $cm);
39 if (isguestuser()) {
40     print_error('guestsarenotallowed');
41 }
43 $workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44 $workshop = new workshop($workshoprecord, $cm, $course);
46 $PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
48 if ($edit) {
49     $PAGE->url->param('edit', $edit);
50 }
52 if ($id) { // submission is specified
53     $submission = $workshop->get_submission_by_id($id);
55     $params = array(
56         'objectid' => $submission->id,
57         'context' => $workshop->context,
58         'courseid' => $workshop->course->id,
59         'relateduserid' => $submission->authorid,
60         'other' => array(
61             'workshopid' => $workshop->id
62         )
63     );
65     $event = \mod_workshop\event\submission_viewed::create($params);
66     $event->trigger();
68 } else { // no submission specified
69     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
70         $submission = new stdclass();
71         $submission->id = null;
72         $submission->authorid = $USER->id;
73         $submission->example = 0;
74         $submission->grade = null;
75         $submission->gradeover = null;
76         $submission->published = null;
77         $submission->feedbackauthor = null;
78         $submission->feedbackauthorformat = editors_get_preferred_format();
79     }
80 }
82 $ownsubmission  = $submission->authorid == $USER->id;
83 $canviewall     = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
84 $cansubmit      = has_capability('mod/workshop:submit', $workshop->context);
85 $canallocate    = has_capability('mod/workshop:allocate', $workshop->context);
86 $canpublish     = has_capability('mod/workshop:publishsubmissions', $workshop->context);
87 $canoverride    = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
88 $candeleteall   = has_capability('mod/workshop:deletesubmissions', $workshop->context);
89 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
90 $isreviewer     = !empty($userassessment);
91 $editable       = ($cansubmit and $ownsubmission);
92 $deletable      = $candeleteall;
93 $ispublished    = ($workshop->phase == workshop::PHASE_CLOSED
94                     and $submission->published == 1
95                     and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
97 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
98     $editable = false;
99 }
100 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
101     $editable = false;
104 $canviewall = $canviewall && $workshop->check_group_membership($submission->authorid);
106 $editable = ($editable && $workshop->check_examples_assessed_before_submission($USER->id));
107 $edit = ($editable and $edit);
109 if (!$candeleteall and $ownsubmission and $editable) {
110     // Only allow the student to delete their own submission if it's still editable and hasn't been assessed.
111     if (count($workshop->get_assessments_of_submission($submission->id)) > 0) {
112         $deletable = false;
113     } else {
114         $deletable = true;
115     }
118 if ($submission->id and $delete and $confirm and $deletable) {
119     require_sesskey();
120     $workshop->delete_submission($submission);
122     redirect($workshop->view_url());
125 $seenaspublished = false; // is the submission seen as a published submission?
127 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
128     // ok you can go
129 } elseif ($submission->id and $ispublished) {
130     // ok you can go
131     $seenaspublished = true;
132 } elseif (is_null($submission->id) and $cansubmit) {
133     // ok you can go
134 } else {
135     print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
138 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
139     require_sesskey();
140     $assessmentid = $workshop->add_allocation($submission, $USER->id);
141     redirect($workshop->assess_url($assessmentid));
144 if ($edit) {
145     require_once(__DIR__.'/submission_form.php');
147     $submission = file_prepare_standard_editor($submission, 'content', $workshop->submission_content_options(),
148         $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
150     $submission = file_prepare_standard_filemanager($submission, 'attachment', $workshop->submission_attachment_options(),
151         $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
153     $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
154         'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
156     if ($mform->is_cancelled()) {
157         redirect($workshop->view_url());
159     } elseif ($cansubmit and $formdata = $mform->get_data()) {
161         $formdata->id = $submission->id;
162         // Creates or updates submission.
163         $submission->id = $workshop->edit_submission($formdata);
165         redirect($workshop->submission_url($submission->id));
166     }
169 // load the form to override grade and/or publish the submission and process the submitted data eventually
170 if (!$edit and ($canoverride or $canpublish)) {
171     $options = array(
172         'editable' => true,
173         'editablepublished' => $canpublish,
174         'overridablegrade' => $canoverride);
175     $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
176     if ($data = $feedbackform->get_data()) {
177         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
178         $record = new stdclass();
179         $record->id = $submission->id;
180         if ($canoverride) {
181             $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
182             $record->gradeoverby = $USER->id;
183             $record->feedbackauthor = $data->feedbackauthor;
184             $record->feedbackauthorformat = $data->feedbackauthorformat;
185         }
186         if ($canpublish) {
187             $record->published = !empty($data->published);
188         }
189         $DB->update_record('workshop_submissions', $record);
190         redirect($workshop->view_url());
191     }
194 $PAGE->set_title($workshop->name);
195 $PAGE->set_heading($course->fullname);
196 if ($edit) {
197     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
198     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
199 } elseif ($ownsubmission) {
200     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
201 } else {
202     $PAGE->navbar->add(get_string('submission', 'workshop'));
205 // Output starts here
206 $output = $PAGE->get_renderer('mod_workshop');
207 echo $output->header();
208 echo $output->heading(format_string($workshop->name), 2);
209 echo $output->heading(get_string('mysubmission', 'workshop'), 3);
211 // show instructions for submitting as thay may contain some list of questions and we need to know them
212 // while reading the submitted answer
213 if (trim($workshop->instructauthors)) {
214     $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
215         'mod_workshop', 'instructauthors', null, workshop::instruction_editors_options($PAGE->context));
216     print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
217     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
218     print_collapsible_region_end();
221 // if in edit mode, display the form to edit the submission
223 if ($edit) {
224     if (!empty($CFG->enableplagiarism)) {
225         require_once($CFG->libdir.'/plagiarismlib.php');
226         echo plagiarism_print_disclosure($cm->id);
227     }
228     $mform->display();
229     echo $output->footer();
230     die();
233 // Confirm deletion (if requested).
234 if ($deletable and $delete) {
235     $prompt = get_string('submissiondeleteconfirm', 'workshop');
236     if ($candeleteall) {
237         $count = count($workshop->get_assessments_of_submission($submission->id));
238         if ($count > 0) {
239             $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
240         }
241     }
242     echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
245 // else display the submission
247 if ($submission->id) {
248     if ($seenaspublished) {
249         $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
250     } else {
251         $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
252     }
253     echo $output->render($workshop->prepare_submission($submission, $showauthor));
254 } else {
255     echo $output->box(get_string('noyoursubmission', 'workshop'));
258 // If not at removal confirmation screen, some action buttons can be displayed.
259 if (!$delete) {
260     // Display create/edit button.
261     if ($editable) {
262         if ($submission->id) {
263             $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
264             $btntxt = get_string('editsubmission', 'workshop');
265         } else {
266             $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
267             $btntxt = get_string('createsubmission', 'workshop');
268         }
269         echo $output->single_button($btnurl, $btntxt, 'get');
270     }
272     // Display delete button.
273     if ($submission->id and $deletable) {
274         $url = new moodle_url($PAGE->url, array('delete' => 1));
275         echo $output->single_button($url, get_string('deletesubmission', 'workshop'), 'get');
276     }
278     // Display assess button.
279     if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
280         $url = new moodle_url($PAGE->url, array('assess' => 1));
281         echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
282     }
285 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
286     if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
287         echo $output->render(new workshop_feedback_author($submission));
288     }
291 // and possibly display the submission's review(s)
293 if ($isreviewer) {
294     // user's own assessment
295     $strategy   = $workshop->grading_strategy_instance();
296     $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
297     $options    = array(
298         'showreviewer'  => true,
299         'showauthor'    => $showauthor,
300         'showform'      => !is_null($userassessment->grade),
301         'showweight'    => true,
302     );
303     $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
304     $assessment->title = get_string('assessmentbyyourself', 'workshop');
306     if ($workshop->assessing_allowed($USER->id)) {
307         if (is_null($userassessment->grade)) {
308             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
309         } else {
310             $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
311         }
312     }
313     if ($canoverride) {
314         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
315     }
317     echo $output->render($assessment);
319     if ($workshop->phase == workshop::PHASE_CLOSED) {
320         if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
321             echo $output->render(new workshop_feedback_reviewer($userassessment));
322         }
323     }
326 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
327     // other assessments
328     $strategy       = $workshop->grading_strategy_instance();
329     $assessments    = $workshop->get_assessments_of_submission($submission->id);
330     $showreviewer   = has_capability('mod/workshop:viewreviewernames', $workshop->context);
331     foreach ($assessments as $assessment) {
332         if ($assessment->reviewerid == $USER->id) {
333             // own assessment has been displayed already
334             continue;
335         }
336         if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
337             // students do not see peer-assessment that are not graded yet
338             continue;
339         }
340         $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
341         $options    = array(
342             'showreviewer'  => $showreviewer,
343             'showauthor'    => $showauthor,
344             'showform'      => !is_null($assessment->grade),
345             'showweight'    => true,
346         );
347         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
348         if ($canoverride) {
349             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
350         }
351         echo $output->render($displayassessment);
353         if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
354             if (strlen(trim($assessment->feedbackreviewer)) > 0) {
355                 echo $output->render(new workshop_feedback_reviewer($assessment));
356             }
357         }
358     }
361 if (!$edit and $canoverride) {
362     // display a form to override the submission grade
363     $feedbackform->display();
366 // If portfolios are enabled and we are not on the edit/removal confirmation screen, display a button to export this page.
367 // The export is not offered if the submission is seen as a published one (it has no relation to the current user.
368 if (!empty($CFG->enableportfolios)) {
369     if (!$delete and !$edit and !$seenaspublished and $submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
370         if (has_capability('mod/workshop:exportsubmissions', $workshop->context)) {
371             require_once($CFG->libdir.'/portfoliolib.php');
373             $button = new portfolio_add_button();
374             $button->set_callback_options('mod_workshop_portfolio_caller', array(
375                 'id' => $workshop->cm->id,
376                 'submissionid' => $submission->id,
377             ), 'mod_workshop');
378             $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
379             echo html_writer::start_tag('div', array('class' => 'singlebutton'));
380             echo $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportsubmission', 'workshop'));
381             echo html_writer::end_tag('div');
382         }
383     }
386 echo $output->footer();