MDL-59239 mod_workshop: Move submission event trigger
[moodle.git] / mod / workshop / submission.php
CommitLineData
33e4dea6 1<?php
53fad4b9
DM
2// This file is part of Moodle - http://moodle.org/
3//
33e4dea6
DM
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.
53fad4b9 13//
33e4dea6
DM
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/>.
16
33e4dea6 17/**
51508f25 18 * View a single (usually the own) submission, submit own work.
33e4dea6 19 *
5cdcfcb9 20 * @package mod_workshop
65601f04
DM
21 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33e4dea6
DM
23 */
24
1fcf0ca8
RS
25require(__DIR__.'/../../config.php');
26require_once(__DIR__.'/locallib.php');
33e4dea6 27
3fa22899
DM
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.
33e4dea6 34
3fa22899
DM
35$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
33e4dea6
DM
37
38require_login($course, false, $cm);
33e4dea6 39if (isguestuser()) {
b8ead2e6 40 print_error('guestsarenotallowed');
33e4dea6
DM
41}
42
fef8ee91
RT
43$workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44$workshop = new workshop($workshoprecord, $cm, $course);
51508f25 45
2c75324e
DM
46$PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
47
48if ($edit) {
49 $PAGE->url->param('edit', $edit);
50}
33e4dea6 51
127032fe 52if ($id) { // submission is specified
51508f25 53 $submission = $workshop->get_submission_by_id($id);
5450f7b6 54
127032fe 55} else { // no submission specified
0dc47fb9 56 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
7a789aa8 57 $submission = new stdclass();
127032fe 58 $submission->id = null;
00aca3c1 59 $submission->authorid = $USER->id;
2e7bd6c2 60 $submission->example = 0;
c6b784f0
DM
61 $submission->grade = null;
62 $submission->gradeover = null;
60719642 63 $submission->published = null;
c6b784f0 64 $submission->feedbackauthor = null;
884482fb 65 $submission->feedbackauthorformat = editors_get_preferred_format();
53fad4b9 66 }
33e4dea6 67}
c1e883bb 68
00aca3c1 69$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
70$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
71$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 72$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 73$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 74$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
deabe48b 75$candeleteall = has_capability('mod/workshop:deletesubmissions', $workshop->context);
5a372494
DM
76$userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
77$isreviewer = !empty($userassessment);
2f289d36 78$editable = ($cansubmit and $ownsubmission);
deabe48b 79$deletable = $candeleteall;
d67c20b8
DM
80$ispublished = ($workshop->phase == workshop::PHASE_CLOSED
81 and $submission->published == 1
82 and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
2f289d36 83
9ddff589 84if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
2f289d36
DM
85 $editable = false;
86}
9ddff589 87if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
2f289d36
DM
88 $editable = false;
89}
90
931bba60 91$canviewall = $canviewall && $workshop->check_group_membership($submission->authorid);
f65bc55f 92
86928d2a 93$editable = ($editable && $workshop->check_examples_assessed_before_submission($USER->id));
514d8c22 94$edit = ($editable and $edit);
51508f25 95
deabe48b
PN
96if (!$candeleteall and $ownsubmission and $editable) {
97 // Only allow the student to delete their own submission if it's still editable and hasn't been assessed.
98 if (count($workshop->get_assessments_of_submission($submission->id)) > 0) {
99 $deletable = false;
100 } else {
101 $deletable = true;
102 }
103}
104
105if ($submission->id and $delete and $confirm and $deletable) {
106 require_sesskey();
107 $workshop->delete_submission($submission);
30c52da9 108
deabe48b
PN
109 redirect($workshop->view_url());
110}
111
d67c20b8
DM
112$seenaspublished = false; // is the submission seen as a published submission?
113
3dc78e5b
DM
114if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
115 // ok you can go
d67c20b8
DM
116} elseif ($submission->id and $ispublished) {
117 // ok you can go
118 $seenaspublished = true;
3dc78e5b
DM
119} elseif (is_null($submission->id) and $cansubmit) {
120 // ok you can go
121} else {
60719642 122 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
123}
124
27fa2482
JL
125if ($submission->id) {
126 // Trigger submission viewed event.
127 $workshop->set_submission_viewed($submission);
128}
129
9ddff589 130if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
ac239eba
DM
131 require_sesskey();
132 $assessmentid = $workshop->add_allocation($submission, $USER->id);
133 redirect($workshop->assess_url($assessmentid));
134}
135
514d8c22 136if ($edit) {
1fcf0ca8 137 require_once(__DIR__.'/submission_form.php');
67cd00ba 138
996f7e82
DM
139 $submission = file_prepare_standard_editor($submission, 'content', $workshop->submission_content_options(),
140 $workshop->context, 'mod_workshop', 'submission_content', $submission->id);
141
142 $submission = file_prepare_standard_filemanager($submission, 'attachment', $workshop->submission_attachment_options(),
143 $workshop->context, 'mod_workshop', 'submission_attachment', $submission->id);
144
145 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
146 'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
67cd00ba
DM
147
148 if ($mform->is_cancelled()) {
149 redirect($workshop->view_url());
150
151 } elseif ($cansubmit and $formdata = $mform->get_data()) {
4c776cae
JL
152
153 $formdata->id = $submission->id;
154 // Creates or updates submission.
155 $submission->id = $workshop->edit_submission($formdata);
156
157 redirect($workshop->submission_url($submission->id));
33e4dea6 158 }
33e4dea6
DM
159}
160
232175e4
DM
161// load the form to override grade and/or publish the submission and process the submitted data eventually
162if (!$edit and ($canoverride or $canpublish)) {
163 $options = array(
164 'editable' => true,
165 'editablepublished' => $canpublish,
166 'overridablegrade' => $canoverride);
167 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
168 if ($data = $feedbackform->get_data()) {
169 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 170 $record = new stdclass();
557a1100 171 $record->id = $submission->id;
232175e4
DM
172 if ($canoverride) {
173 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
174 $record->gradeoverby = $USER->id;
175 $record->feedbackauthor = $data->feedbackauthor;
176 $record->feedbackauthorformat = $data->feedbackauthorformat;
177 }
178 if ($canpublish) {
179 $record->published = !empty($data->published);
180 }
557a1100
DM
181 $DB->update_record('workshop_submissions', $record);
182 redirect($workshop->view_url());
183 }
184}
185
0dc47fb9
DM
186$PAGE->set_title($workshop->name);
187$PAGE->set_heading($course->fullname);
39861053 188if ($edit) {
b761e6d9 189 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 190 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 191} elseif ($ownsubmission) {
b761e6d9 192 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
193} else {
194 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 195}
33e4dea6 196
c1e883bb 197// Output starts here
81b22887
DM
198$output = $PAGE->get_renderer('mod_workshop');
199echo $output->header();
200echo $output->heading(format_string($workshop->name), 2);
ddc9cff6 201echo $output->heading(get_string('mysubmission', 'workshop'), 3);
c1e883bb 202
e0142f7e
DM
203// show instructions for submitting as thay may contain some list of questions and we need to know them
204// while reading the submitted answer
205if (trim($workshop->instructauthors)) {
206 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
e46aec4c 207 'mod_workshop', 'instructauthors', null, workshop::instruction_editors_options($PAGE->context));
e0142f7e 208 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
367a75fa 209 echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
e0142f7e
DM
210 print_collapsible_region_end();
211}
212
3dc78e5b
DM
213// if in edit mode, display the form to edit the submission
214
514d8c22 215if ($edit) {
67ba9007
KG
216 if (!empty($CFG->enableplagiarism)) {
217 require_once($CFG->libdir.'/plagiarismlib.php');
218 echo plagiarism_print_disclosure($cm->id);
219 }
c1e883bb 220 $mform->display();
81b22887 221 echo $output->footer();
c1e883bb
DM
222 die();
223}
224
deabe48b
PN
225// Confirm deletion (if requested).
226if ($deletable and $delete) {
227 $prompt = get_string('submissiondeleteconfirm', 'workshop');
228 if ($candeleteall) {
58489e11
DM
229 $count = count($workshop->get_assessments_of_submission($submission->id));
230 if ($count > 0) {
231 $prompt = get_string('submissiondeleteconfirmassess', 'workshop', ['count' => $count]);
deabe48b
PN
232 }
233 }
58489e11 234 echo $output->confirm($prompt, new moodle_url($PAGE->url, ['delete' => 1, 'confirm' => 1]), $workshop->view_url());
deabe48b
PN
235}
236
3dc78e5b
DM
237// else display the submission
238
239if ($submission->id) {
d67c20b8
DM
240 if ($seenaspublished) {
241 $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
242 } else {
243 $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
244 }
245 echo $output->render($workshop->prepare_submission($submission, $showauthor));
3dc78e5b 246} else {
81b22887 247 echo $output->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
248}
249
58489e11
DM
250// If not at removal confirmation screen, some action buttons can be displayed.
251if (!$delete) {
0cc5c646 252 // Display create/edit button.
58489e11
DM
253 if ($editable) {
254 if ($submission->id) {
255 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
256 $btntxt = get_string('editsubmission', 'workshop');
257 } else {
258 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
259 $btntxt = get_string('createsubmission', 'workshop');
260 }
261 echo $output->single_button($btnurl, $btntxt, 'get');
2f289d36 262 }
c1e883bb 263
0cc5c646 264 // Display delete button.
58489e11
DM
265 if ($submission->id and $deletable) {
266 $url = new moodle_url($PAGE->url, array('delete' => 1));
267 echo $output->single_button($url, get_string('deletesubmission', 'workshop'), 'get');
268 }
deabe48b 269
0cc5c646 270 // Display assess button.
58489e11
DM
271 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
272 $url = new moodle_url($PAGE->url, array('assess' => 1));
273 echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
274 }
ac239eba
DM
275}
276
0dfb4bad
DM
277if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
278 if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
279 echo $output->render(new workshop_feedback_author($submission));
280 }
281}
282
3dc78e5b
DM
283// and possibly display the submission's review(s)
284
3dc78e5b 285if ($isreviewer) {
38504a44
DM
286 // user's own assessment
287 $strategy = $workshop->grading_strategy_instance();
288 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
289 $options = array(
290 'showreviewer' => true,
291 'showauthor' => $showauthor,
292 'showform' => !is_null($userassessment->grade),
293 'showweight' => true,
294 );
295 $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
296 $assessment->title = get_string('assessmentbyyourself', 'workshop');
297
298 if ($workshop->assessing_allowed($USER->id)) {
299 if (is_null($userassessment->grade)) {
300 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
301 } else {
302 $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
5a372494 303 }
5a372494 304 }
38504a44
DM
305 if ($canoverride) {
306 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
307 }
308
309 echo $output->render($assessment);
f68648e9
DM
310
311 if ($workshop->phase == workshop::PHASE_CLOSED) {
312 if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
313 echo $output->render(new workshop_feedback_reviewer($userassessment));
314 }
315 }
3dc78e5b
DM
316}
317
0cc5c646 318if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
38504a44
DM
319 // other assessments
320 $strategy = $workshop->grading_strategy_instance();
321 $assessments = $workshop->get_assessments_of_submission($submission->id);
322 $showreviewer = has_capability('mod/workshop:viewreviewernames', $workshop->context);
5a372494
DM
323 foreach ($assessments as $assessment) {
324 if ($assessment->reviewerid == $USER->id) {
325 // own assessment has been displayed already
326 continue;
327 }
3779dcae
DM
328 if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
329 // students do not see peer-assessment that are not graded yet
330 continue;
331 }
38504a44
DM
332 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
333 $options = array(
334 'showreviewer' => $showreviewer,
335 'showauthor' => $showauthor,
336 'showform' => !is_null($assessment->grade),
337 'showweight' => true,
338 );
f68648e9 339 $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
38504a44 340 if ($canoverride) {
f68648e9
DM
341 $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
342 }
343 echo $output->render($displayassessment);
344
345 if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
346 if (strlen(trim($assessment->feedbackreviewer)) > 0) {
347 echo $output->render(new workshop_feedback_reviewer($assessment));
348 }
7a5f4be0 349 }
5a372494 350 }
3dc78e5b
DM
351}
352
557a1100
DM
353if (!$edit and $canoverride) {
354 // display a form to override the submission grade
355 $feedbackform->display();
356}
357
0cc5c646
DM
358// If portfolios are enabled and we are not on the edit/removal confirmation screen, display a button to export this page.
359// The export is not offered if the submission is seen as a published one (it has no relation to the current user.
360if (!empty($CFG->enableportfolios)) {
361 if (!$delete and !$edit and !$seenaspublished and $submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
362 if (has_capability('mod/workshop:exportsubmissions', $workshop->context)) {
363 require_once($CFG->libdir.'/portfoliolib.php');
364
365 $button = new portfolio_add_button();
366 $button->set_callback_options('mod_workshop_portfolio_caller', array(
367 'id' => $workshop->cm->id,
368 'submissionid' => $submission->id,
369 ), 'mod_workshop');
370 $button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
371 echo html_writer::start_tag('div', array('class' => 'singlebutton'));
372 echo $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportsubmission', 'workshop'));
373 echo html_writer::end_tag('div');
374 }
375 }
376}
377
81b22887 378echo $output->footer();