MDL-23459 workshop: late submission flag
[moodle.git] / mod / workshop / submission.php
CommitLineData
33e4dea6
DM
1<?php
2
53fad4b9
DM
3// This file is part of Moodle - http://moodle.org/
4//
33e4dea6
DM
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.
53fad4b9 14//
33e4dea6
DM
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/>.
17
33e4dea6 18/**
51508f25 19 * View a single (usually the own) submission, submit own work.
33e4dea6
DM
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 */
25
26require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
127032fe 27require_once(dirname(__FILE__).'/locallib.php');
33e4dea6 28
c1e883bb
DM
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 for editing?
ac239eba 32$assess = optional_param('assess', false, PARAM_BOOL); // instant assessment required
33e4dea6 33
c1e883bb
DM
34$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
35$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
33e4dea6
DM
36
37require_login($course, false, $cm);
33e4dea6 38if (isguestuser()) {
b8ead2e6 39 print_error('guestsarenotallowed');
33e4dea6
DM
40}
41
51508f25
DM
42$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
43$workshop = new workshop($workshop, $cm, $course);
44
a6855934 45$PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id, 'edit' => $edit));
33e4dea6 46
127032fe 47if ($id) { // submission is specified
51508f25 48 $submission = $workshop->get_submission_by_id($id);
127032fe 49} else { // no submission specified
0dc47fb9 50 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
7a789aa8 51 $submission = new stdclass();
127032fe 52 $submission->id = null;
00aca3c1 53 $submission->authorid = $USER->id;
c6b784f0
DM
54 $submission->grade = null;
55 $submission->gradeover = null;
60719642 56 $submission->published = null;
c6b784f0
DM
57 $submission->feedbackauthor = null;
58 $submission->feedbackauthorformat = FORMAT_HTML;
53fad4b9 59 }
33e4dea6 60}
c1e883bb 61
00aca3c1 62$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
63$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
64$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 65$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 66$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 67$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
5a372494
DM
68$userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
69$isreviewer = !empty($userassessment);
cff28ef0 70$editable = ($cansubmit and $ownsubmission and $workshop->submitting_allowed());
514d8c22
DM
71if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
72 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
73 // check that all required examples have been assessed by the user
74 $examples = $workshop->get_examples_for_reviewer($USER->id);
75 foreach ($examples as $exampleid => $example) {
76 if (is_null($example->grade)) {
77 $editable = false;
78 break;
79 }
80 }
81}
82$edit = ($editable and $edit);
51508f25 83
3dc78e5b
DM
84if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
85 // ok you can go
86} elseif (is_null($submission->id) and $cansubmit) {
87 // ok you can go
88} else {
60719642 89 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
90}
91
ac239eba
DM
92if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
93 require_sesskey();
94 $assessmentid = $workshop->add_allocation($submission, $USER->id);
95 redirect($workshop->assess_url($assessmentid));
96}
97
514d8c22 98if ($edit) {
67cd00ba
DM
99 require_once(dirname(__FILE__).'/submission_form.php');
100
101 $maxfiles = $workshop->nattachments;
102 $maxbytes = $workshop->maxbytes;
103 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
104 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
105 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
64f93798 106 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 107 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
64f93798 108 'mod_workshop', 'submission_attachment', $submission->id);
67cd00ba
DM
109
110 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
111 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
112
113 if ($mform->is_cancelled()) {
114 redirect($workshop->view_url());
115
116 } elseif ($cansubmit and $formdata = $mform->get_data()) {
117 $timenow = time();
118 if (empty($formdata->id)) {
119 $formdata->workshopid = $workshop->id;
81eccf0a 120 $formdata->example = 0;
67cd00ba
DM
121 $formdata->authorid = $USER->id;
122 $formdata->timecreated = $timenow;
557a1100 123 $formdata->feedbackauthorformat = FORMAT_HTML; // todo better default
67cd00ba
DM
124 }
125 $formdata->timemodified = $timenow;
126 $formdata->title = trim($formdata->title);
127 $formdata->content = ''; // updated later
128 $formdata->contentformat = FORMAT_HTML; // updated later
129 $formdata->contenttrust = 0; // updated later
130 if (empty($formdata->id)) {
131 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
132 // todo add to log
133 }
134 // save and relink embedded images and save attachments
135 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
64f93798 136 'mod_workshop', 'submission_content', $formdata->id);
67cd00ba 137 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
64f93798 138 'mod_workshop', 'submission_attachment', $formdata->id);
f1b4b387 139 if (empty($formdata->attachment)) {
ac239eba 140 // explicit cast to zero integer
f1b4b387
DM
141 $formdata->attachment = 0;
142 }
67cd00ba
DM
143 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
144 $DB->update_record('workshop_submissions', $formdata);
145 redirect($workshop->submission_url($formdata->id));
33e4dea6 146 }
33e4dea6
DM
147}
148
232175e4
DM
149// load the form to override grade and/or publish the submission and process the submitted data eventually
150if (!$edit and ($canoverride or $canpublish)) {
151 $options = array(
152 'editable' => true,
153 'editablepublished' => $canpublish,
154 'overridablegrade' => $canoverride);
155 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
156 if ($data = $feedbackform->get_data()) {
157 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 158 $record = new stdclass();
557a1100 159 $record->id = $submission->id;
232175e4
DM
160 if ($canoverride) {
161 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
162 $record->gradeoverby = $USER->id;
163 $record->feedbackauthor = $data->feedbackauthor;
164 $record->feedbackauthorformat = $data->feedbackauthorformat;
165 }
166 if ($canpublish) {
167 $record->published = !empty($data->published);
168 }
557a1100
DM
169 $DB->update_record('workshop_submissions', $record);
170 redirect($workshop->view_url());
171 }
172}
173
0dc47fb9
DM
174$PAGE->set_title($workshop->name);
175$PAGE->set_heading($course->fullname);
39861053 176if ($edit) {
b761e6d9 177 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 178 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 179} elseif ($ownsubmission) {
b761e6d9 180 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
181} else {
182 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 183}
33e4dea6 184
c1e883bb 185// Output starts here
39861053 186echo $OUTPUT->header();
0dc47fb9 187echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 188
e0142f7e
DM
189// show instructions for submitting as thay may contain some list of questions and we need to know them
190// while reading the submitted answer
191if (trim($workshop->instructauthors)) {
192 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
193 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
194 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
195 echo $OUTPUT->box(format_text($instructions, $workshop->instructauthorsformat), array('generalbox', 'instructions'));
196 print_collapsible_region_end();
197}
198
3dc78e5b
DM
199// if in edit mode, display the form to edit the submission
200
514d8c22 201if ($edit) {
c1e883bb
DM
202 $mform->display();
203 echo $OUTPUT->footer();
204 die();
205}
206
3dc78e5b
DM
207// else display the submission
208
209if ($submission->id) {
6adbcb80 210 $wsoutput = $PAGE->get_renderer('mod_workshop');
e9b0f0ab 211 echo $wsoutput->submission_full($submission, true);
3dc78e5b
DM
212} else {
213 echo $OUTPUT->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
214}
215
514d8c22 216if ($editable) {
3ba60ee1 217 $url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
c5abdea3 218 echo $OUTPUT->single_button($url, get_string('editsubmission', 'workshop'), 'get');
c1e883bb
DM
219}
220
ac239eba
DM
221if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
222 $url = new moodle_url($PAGE->url, array('assess' => 1));
223 echo $OUTPUT->single_button($url, get_string('assess', 'workshop'), 'post');
224}
225
3dc78e5b
DM
226// and possibly display the submission's review(s)
227
3dc78e5b 228if ($isreviewer) {
3dc78e5b 229 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
230 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
231 echo $OUTPUT->heading(get_string('assessmentbyyourself', 'workshop'), 2);
232 // reviewers can always see the grades they gave even they are not available yet
233 if (is_null($userassessment->grade)) {
234 echo $OUTPUT->heading(get_string('notassessed', 'workshop'), 3);
235 if ($workshop->assessing_allowed()) {
236 echo $OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('assess', 'workshop'), 'get');
237 }
238 } else {
239 $a = new stdclass();
240 $a->max = $workshop->real_grade(100);
241 $a->received = $workshop->real_grade($userassessment->grade);
242 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
243 if ($userassessment->weight != 1) {
244 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $userassessment->weight), 3);
245 }
246 if ($workshop->assessing_allowed()) {
247 echo $OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('reassess', 'workshop'), 'get');
248 }
249 $mform->display();
250 }
3dc78e5b
DM
251}
252
5a372494 253if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
3dc78e5b 254 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
255 $assessments = $workshop->get_assessments_of_submission($submission->id);
256 $canviewreviewernames = has_capability('mod/workshop:viewreviewernames', $workshop->context);
257 foreach ($assessments as $assessment) {
258 if ($assessment->reviewerid == $USER->id) {
259 // own assessment has been displayed already
260 continue;
261 }
262 if (is_null($assessment->grade)) {
263 // not graded assessment are not displayed
264 continue;
265 }
266 if ($canviewreviewernames) {
267 $reviewer = new stdclass();
268 $reviewer->firstname = $assessment->reviewerfirstname;
269 $reviewer->lastname = $assessment->reviewerlastname;
270 echo $OUTPUT->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
271 } else {
272 echo $OUTPUT->heading(get_string('assessmentbyunknown', 'workshop'), 2);
273 }
274 $a = new stdclass();
275 $a->max = $workshop->real_grade(100);
276 $a->received = $workshop->real_grade($assessment->grade);
277 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
278 if ($assessment->weight != 1) {
279 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $assessment->weight), 3);
280 }
281 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
282 $mform->display();
283 }
3dc78e5b
DM
284}
285
557a1100
DM
286if (!$edit and $canoverride) {
287 // display a form to override the submission grade
288 $feedbackform->display();
289}
290
0dc47fb9 291echo $OUTPUT->footer();