NOMDL workshop_get_participants() though it does not seem to be used by core any...
[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);
2f289d36
DM
70$editable = ($cansubmit and $ownsubmission);
71
72if (empty($submission->id) and !$workshop->creating_submission_allowed()) {
73 $editable = false;
74}
75if ($submission->id and !$workshop->modifying_submission_allowed()) {
76 $editable = false;
77}
78
514d8c22
DM
79if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
80 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
81 // check that all required examples have been assessed by the user
82 $examples = $workshop->get_examples_for_reviewer($USER->id);
83 foreach ($examples as $exampleid => $example) {
84 if (is_null($example->grade)) {
85 $editable = false;
86 break;
87 }
88 }
89}
90$edit = ($editable and $edit);
51508f25 91
3dc78e5b
DM
92if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
93 // ok you can go
94} elseif (is_null($submission->id) and $cansubmit) {
95 // ok you can go
96} else {
60719642 97 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
c1e883bb
DM
98}
99
ac239eba
DM
100if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
101 require_sesskey();
102 $assessmentid = $workshop->add_allocation($submission, $USER->id);
103 redirect($workshop->assess_url($assessmentid));
104}
105
514d8c22 106if ($edit) {
67cd00ba
DM
107 require_once(dirname(__FILE__).'/submission_form.php');
108
109 $maxfiles = $workshop->nattachments;
110 $maxbytes = $workshop->maxbytes;
111 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
112 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
113 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
64f93798 114 'mod_workshop', 'submission_content', $submission->id);
67cd00ba 115 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
64f93798 116 'mod_workshop', 'submission_attachment', $submission->id);
67cd00ba
DM
117
118 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
119 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
120
121 if ($mform->is_cancelled()) {
122 redirect($workshop->view_url());
123
124 } elseif ($cansubmit and $formdata = $mform->get_data()) {
125 $timenow = time();
126 if (empty($formdata->id)) {
127 $formdata->workshopid = $workshop->id;
81eccf0a 128 $formdata->example = 0;
67cd00ba
DM
129 $formdata->authorid = $USER->id;
130 $formdata->timecreated = $timenow;
557a1100 131 $formdata->feedbackauthorformat = FORMAT_HTML; // todo better default
67cd00ba
DM
132 }
133 $formdata->timemodified = $timenow;
134 $formdata->title = trim($formdata->title);
135 $formdata->content = ''; // updated later
136 $formdata->contentformat = FORMAT_HTML; // updated later
137 $formdata->contenttrust = 0; // updated later
2f289d36
DM
138 $formdata->late = 0x0; // bit mask
139 if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
140 $formdata->late = $formdata->late | 0x1;
141 }
142 if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
143 $formdata->late = $formdata->late | 0x2;
144 }
67cd00ba
DM
145 if (empty($formdata->id)) {
146 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
147 // todo add to log
148 }
149 // save and relink embedded images and save attachments
150 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
64f93798 151 'mod_workshop', 'submission_content', $formdata->id);
67cd00ba 152 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
64f93798 153 'mod_workshop', 'submission_attachment', $formdata->id);
f1b4b387 154 if (empty($formdata->attachment)) {
ac239eba 155 // explicit cast to zero integer
f1b4b387
DM
156 $formdata->attachment = 0;
157 }
67cd00ba
DM
158 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
159 $DB->update_record('workshop_submissions', $formdata);
160 redirect($workshop->submission_url($formdata->id));
33e4dea6 161 }
33e4dea6
DM
162}
163
232175e4
DM
164// load the form to override grade and/or publish the submission and process the submitted data eventually
165if (!$edit and ($canoverride or $canpublish)) {
166 $options = array(
167 'editable' => true,
168 'editablepublished' => $canpublish,
169 'overridablegrade' => $canoverride);
170 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
171 if ($data = $feedbackform->get_data()) {
172 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 173 $record = new stdclass();
557a1100 174 $record->id = $submission->id;
232175e4
DM
175 if ($canoverride) {
176 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
177 $record->gradeoverby = $USER->id;
178 $record->feedbackauthor = $data->feedbackauthor;
179 $record->feedbackauthorformat = $data->feedbackauthorformat;
180 }
181 if ($canpublish) {
182 $record->published = !empty($data->published);
183 }
557a1100
DM
184 $DB->update_record('workshop_submissions', $record);
185 redirect($workshop->view_url());
186 }
187}
188
0dc47fb9
DM
189$PAGE->set_title($workshop->name);
190$PAGE->set_heading($course->fullname);
39861053 191if ($edit) {
b761e6d9 192 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 193 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 194} elseif ($ownsubmission) {
b761e6d9 195 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
196} else {
197 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 198}
33e4dea6 199
c1e883bb 200// Output starts here
39861053 201echo $OUTPUT->header();
0dc47fb9 202echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 203
e0142f7e
DM
204// show instructions for submitting as thay may contain some list of questions and we need to know them
205// while reading the submitted answer
206if (trim($workshop->instructauthors)) {
207 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
208 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
209 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
210 echo $OUTPUT->box(format_text($instructions, $workshop->instructauthorsformat), array('generalbox', 'instructions'));
211 print_collapsible_region_end();
212}
213
3dc78e5b
DM
214// if in edit mode, display the form to edit the submission
215
514d8c22 216if ($edit) {
c1e883bb
DM
217 $mform->display();
218 echo $OUTPUT->footer();
219 die();
220}
221
3dc78e5b
DM
222// else display the submission
223
224if ($submission->id) {
6adbcb80 225 $wsoutput = $PAGE->get_renderer('mod_workshop');
e9b0f0ab 226 echo $wsoutput->submission_full($submission, true);
3dc78e5b
DM
227} else {
228 echo $OUTPUT->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
229}
230
514d8c22 231if ($editable) {
2f289d36
DM
232 if ($submission->id) {
233 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
234 $btntxt = get_string('editsubmission', 'workshop');
235 } else {
236 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
237 $btntxt = get_string('createsubmission', 'workshop');
238 }
239 echo $OUTPUT->single_button($btnurl, $btntxt, 'get');
c1e883bb
DM
240}
241
ac239eba
DM
242if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
243 $url = new moodle_url($PAGE->url, array('assess' => 1));
244 echo $OUTPUT->single_button($url, get_string('assess', 'workshop'), 'post');
245}
246
3dc78e5b
DM
247// and possibly display the submission's review(s)
248
3dc78e5b 249if ($isreviewer) {
3dc78e5b 250 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
251 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
252 echo $OUTPUT->heading(get_string('assessmentbyyourself', 'workshop'), 2);
253 // reviewers can always see the grades they gave even they are not available yet
254 if (is_null($userassessment->grade)) {
255 echo $OUTPUT->heading(get_string('notassessed', 'workshop'), 3);
256 if ($workshop->assessing_allowed()) {
257 echo $OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('assess', 'workshop'), 'get');
258 }
259 } else {
260 $a = new stdclass();
261 $a->max = $workshop->real_grade(100);
262 $a->received = $workshop->real_grade($userassessment->grade);
263 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
264 if ($userassessment->weight != 1) {
265 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $userassessment->weight), 3);
266 }
267 if ($workshop->assessing_allowed()) {
268 echo $OUTPUT->single_button($workshop->assess_url($userassessment->id), get_string('reassess', 'workshop'), 'get');
269 }
270 $mform->display();
271 }
3dc78e5b
DM
272}
273
5a372494 274if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
3dc78e5b 275 $strategy = $workshop->grading_strategy_instance();
5a372494
DM
276 $assessments = $workshop->get_assessments_of_submission($submission->id);
277 $canviewreviewernames = has_capability('mod/workshop:viewreviewernames', $workshop->context);
278 foreach ($assessments as $assessment) {
279 if ($assessment->reviewerid == $USER->id) {
280 // own assessment has been displayed already
281 continue;
282 }
283 if (is_null($assessment->grade)) {
284 // not graded assessment are not displayed
285 continue;
286 }
287 if ($canviewreviewernames) {
288 $reviewer = new stdclass();
289 $reviewer->firstname = $assessment->reviewerfirstname;
290 $reviewer->lastname = $assessment->reviewerlastname;
291 echo $OUTPUT->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
292 } else {
293 echo $OUTPUT->heading(get_string('assessmentbyunknown', 'workshop'), 2);
294 }
295 $a = new stdclass();
296 $a->max = $workshop->real_grade(100);
297 $a->received = $workshop->real_grade($assessment->grade);
298 echo $OUTPUT->heading(get_string('gradeinfo', 'workshop', $a), 3);
299 if ($assessment->weight != 1) {
300 echo $OUTPUT->heading(get_string('weightinfo', 'workshop', $assessment->weight), 3);
301 }
302 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
303 $mform->display();
304 }
3dc78e5b
DM
305}
306
557a1100
DM
307if (!$edit and $canoverride) {
308 // display a form to override the submission grade
309 $feedbackform->display();
310}
311
0dc47fb9 312echo $OUTPUT->footer();