quiz MDL-24727 column missed from install.xml
[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 20 *
65601f04
DM
21 * @package mod
22 * @subpackage workshop
23 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33e4dea6
DM
25 */
26
27require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
127032fe 28require_once(dirname(__FILE__).'/locallib.php');
33e4dea6 29
c1e883bb
DM
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 for editing?
ac239eba 33$assess = optional_param('assess', false, PARAM_BOOL); // instant assessment required
33e4dea6 34
c1e883bb
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
51508f25
DM
43$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44$workshop = new workshop($workshop, $cm, $course);
45
a6855934 46$PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id, 'edit' => $edit));
33e4dea6 47
127032fe 48if ($id) { // submission is specified
51508f25 49 $submission = $workshop->get_submission_by_id($id);
127032fe 50} else { // no submission specified
0dc47fb9 51 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
7a789aa8 52 $submission = new stdclass();
127032fe 53 $submission->id = null;
00aca3c1 54 $submission->authorid = $USER->id;
2e7bd6c2 55 $submission->example = 0;
c6b784f0
DM
56 $submission->grade = null;
57 $submission->gradeover = null;
60719642 58 $submission->published = null;
c6b784f0 59 $submission->feedbackauthor = null;
884482fb 60 $submission->feedbackauthorformat = editors_get_preferred_format();
53fad4b9 61 }
33e4dea6 62}
c1e883bb 63
00aca3c1 64$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
65$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
66$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 67$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 68$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 69$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
5a372494
DM
70$userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
71$isreviewer = !empty($userassessment);
2f289d36
DM
72$editable = ($cansubmit and $ownsubmission);
73
74if (empty($submission->id) and !$workshop->creating_submission_allowed()) {
75 $editable = false;
76}
77if ($submission->id and !$workshop->modifying_submission_allowed()) {
78 $editable = false;
79}
80
514d8c22
DM
81if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
82 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
83 // check that all required examples have been assessed by the user
84 $examples = $workshop->get_examples_for_reviewer($USER->id);
85 foreach ($examples as $exampleid => $example) {
86 if (is_null($example->grade)) {
87 $editable = false;
88 break;
89 }
90 }
91}
92$edit = ($editable and $edit);
51508f25 93
3dc78e5b
DM
94if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
95 // ok you can go
96} elseif (is_null($submission->id) and $cansubmit) {
97 // ok you can go
98} else {
60719642 99 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
100}
101
ac239eba
DM
102if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
103 require_sesskey();
104 $assessmentid = $workshop->add_allocation($submission, $USER->id);
105 redirect($workshop->assess_url($assessmentid));
106}
107
514d8c22 108if ($edit) {
67cd00ba
DM
109 require_once(dirname(__FILE__).'/submission_form.php');
110
111 $maxfiles = $workshop->nattachments;
112 $maxbytes = $workshop->maxbytes;
113 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
114 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
115 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
64f93798 116 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 117 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
64f93798 118 'mod_workshop', 'submission_attachment', $submission->id);
67cd00ba
DM
119
120 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
121 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
122
123 if ($mform->is_cancelled()) {
124 redirect($workshop->view_url());
125
126 } elseif ($cansubmit and $formdata = $mform->get_data()) {
2e7bd6c2
DM
127 if ($formdata->example == 0) {
128 // this was used just for validation, it must be set to zero when dealing with normal submissions
129 unset($formdata->example);
130 } else {
131 throw new coding_exception('Invalid submission form data value: example');
132 }
67cd00ba 133 $timenow = time();
2e7bd6c2 134 if (is_null($submission->id)) {
67cd00ba 135 $formdata->workshopid = $workshop->id;
81eccf0a 136 $formdata->example = 0;
67cd00ba
DM
137 $formdata->authorid = $USER->id;
138 $formdata->timecreated = $timenow;
884482fb 139 $formdata->feedbackauthorformat = editors_get_preferred_format();
67cd00ba
DM
140 }
141 $formdata->timemodified = $timenow;
142 $formdata->title = trim($formdata->title);
143 $formdata->content = ''; // updated later
144 $formdata->contentformat = FORMAT_HTML; // updated later
145 $formdata->contenttrust = 0; // updated later
2f289d36
DM
146 $formdata->late = 0x0; // bit mask
147 if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
148 $formdata->late = $formdata->late | 0x1;
149 }
150 if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
151 $formdata->late = $formdata->late | 0x2;
152 }
2e7bd6c2
DM
153 if (is_null($submission->id)) {
154 $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
67cd00ba 155 // todo add to log
2e7bd6c2
DM
156 } else {
157 if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
158 throw new moodle_exception('err_submissionid', 'workshop');
159 }
67cd00ba
DM
160 }
161 // save and relink embedded images and save attachments
162 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
2e7bd6c2 163 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 164 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
2e7bd6c2 165 'mod_workshop', 'submission_attachment', $submission->id);
f1b4b387 166 if (empty($formdata->attachment)) {
ac239eba 167 // explicit cast to zero integer
f1b4b387
DM
168 $formdata->attachment = 0;
169 }
67cd00ba
DM
170 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
171 $DB->update_record('workshop_submissions', $formdata);
172 redirect($workshop->submission_url($formdata->id));
33e4dea6 173 }
33e4dea6
DM
174}
175
232175e4
DM
176// load the form to override grade and/or publish the submission and process the submitted data eventually
177if (!$edit and ($canoverride or $canpublish)) {
178 $options = array(
179 'editable' => true,
180 'editablepublished' => $canpublish,
181 'overridablegrade' => $canoverride);
182 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
183 if ($data = $feedbackform->get_data()) {
184 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 185 $record = new stdclass();
557a1100 186 $record->id = $submission->id;
232175e4
DM
187 if ($canoverride) {
188 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
189 $record->gradeoverby = $USER->id;
190 $record->feedbackauthor = $data->feedbackauthor;
191 $record->feedbackauthorformat = $data->feedbackauthorformat;
192 }
193 if ($canpublish) {
194 $record->published = !empty($data->published);
195 }
557a1100
DM
196 $DB->update_record('workshop_submissions', $record);
197 redirect($workshop->view_url());
198 }
199}
200
0dc47fb9
DM
201$PAGE->set_title($workshop->name);
202$PAGE->set_heading($course->fullname);
39861053 203if ($edit) {
b761e6d9 204 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 205 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 206} elseif ($ownsubmission) {
b761e6d9 207 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
208} else {
209 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 210}
33e4dea6 211
c1e883bb 212// Output starts here
39861053 213echo $OUTPUT->header();
0dc47fb9 214echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 215
e0142f7e
DM
216// show instructions for submitting as thay may contain some list of questions and we need to know them
217// while reading the submitted answer
218if (trim($workshop->instructauthors)) {
219 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
220 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
221 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
222 echo $OUTPUT->box(format_text($instructions, $workshop->instructauthorsformat), array('generalbox', 'instructions'));
223 print_collapsible_region_end();
224}
225
3dc78e5b
DM
226// if in edit mode, display the form to edit the submission
227
514d8c22 228if ($edit) {
c1e883bb
DM
229 $mform->display();
230 echo $OUTPUT->footer();
231 die();
232}
233
3dc78e5b
DM
234// else display the submission
235
236if ($submission->id) {
6adbcb80 237 $wsoutput = $PAGE->get_renderer('mod_workshop');
7a5f4be0 238 echo $wsoutput->submission_full($submission, has_capability('mod/workshop:viewauthornames', $workshop->context));
3dc78e5b
DM
239} else {
240 echo $OUTPUT->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
241}
242
514d8c22 243if ($editable) {
2f289d36
DM
244 if ($submission->id) {
245 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
246 $btntxt = get_string('editsubmission', 'workshop');
247 } else {
248 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
249 $btntxt = get_string('createsubmission', 'workshop');
250 }
251 echo $OUTPUT->single_button($btnurl, $btntxt, 'get');
c1e883bb
DM
252}
253
ac239eba
DM
254if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
255 $url = new moodle_url($PAGE->url, array('assess' => 1));
256 echo $OUTPUT->single_button($url, get_string('assess', 'workshop'), 'post');
257}
258
3dc78e5b
DM
259// and possibly display the submission's review(s)
260
3dc78e5b 261if ($isreviewer) {
3dc78e5b 262 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
263 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
264 echo $OUTPUT->heading(get_string('assessmentbyyourself', 'workshop'), 2);
265 // reviewers can always see the grades they gave even they are not available yet
266 if (is_null($userassessment->grade)) {
267 echo $OUTPUT->heading(get_string('notassessed', 'workshop'), 3);
268 if ($workshop->assessing_allowed()) {
7a5f4be0
DM
269 echo $OUTPUT->container($OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('assess', 'workshop'), 'get'),
270 array('class' => 'buttonsbar'));
5a372494
DM
271 }
272 } else {
273 $a = new stdclass();
274 $a->max = $workshop->real_grade(100);
275 $a->received = $workshop->real_grade($userassessment->grade);
276 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
277 if ($userassessment->weight != 1) {
278 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $userassessment->weight), 3);
279 }
280 if ($workshop->assessing_allowed()) {
7a5f4be0
DM
281 echo $OUTPUT->container($OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('reassess', 'workshop'), 'get'),
282 array('class' => 'buttonsbar'));
5a372494
DM
283 }
284 $mform->display();
285 }
3dc78e5b
DM
286}
287
5a372494 288if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
3dc78e5b 289 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
290 $assessments = $workshop->get_assessments_of_submission($submission->id);
291 $canviewreviewernames = has_capability('mod/workshop:viewreviewernames', $workshop->context);
292 foreach ($assessments as $assessment) {
293 if ($assessment->reviewerid == $USER->id) {
294 // own assessment has been displayed already
295 continue;
296 }
297 if (is_null($assessment->grade)) {
298 // not graded assessment are not displayed
299 continue;
300 }
301 if ($canviewreviewernames) {
302 $reviewer = new stdclass();
303 $reviewer->firstname = $assessment->reviewerfirstname;
304 $reviewer->lastname = $assessment->reviewerlastname;
305 echo $OUTPUT->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
306 } else {
307 echo $OUTPUT->heading(get_string('assessmentbyunknown', 'workshop'), 2);
308 }
309 $a = new stdclass();
310 $a->max = $workshop->real_grade(100);
311 $a->received = $workshop->real_grade($assessment->grade);
312 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
313 if ($assessment->weight != 1) {
314 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $assessment->weight), 3);
315 }
7a5f4be0
DM
316 if (has_capability('mod/workshop:overridegrades', $workshop->context)) {
317 echo $OUTPUT->container($OUTPUT->single_button($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'), 'get'),
318 array('class' => 'buttonsbar'));
319 }
5a372494
DM
320 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
321 $mform->display();
322 }
3dc78e5b
DM
323}
324
557a1100
DM
325if (!$edit and $canoverride) {
326 // display a form to override the submission grade
327 $feedbackform->display();
328}
329
0dc47fb9 330echo $OUTPUT->footer();