Workshop: published submissions are available at the end of the activity
[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;
56 $submission->feedbackauthor = null;
57 $submission->feedbackauthorformat = FORMAT_HTML;
53fad4b9 58 }
33e4dea6 59}
c1e883bb 60
00aca3c1 61$ownsubmission = $submission->authorid == $USER->id;
67cd00ba
DM
62$canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
63$cansubmit = has_capability('mod/workshop:submit', $workshop->context);
ac239eba 64$canallocate = has_capability('mod/workshop:allocate', $workshop->context);
232175e4 65$canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
090a7907 66$canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
00aca3c1 67$isreviewer = $DB->record_exists('workshop_assessments', array('submissionid' => $submission->id, 'reviewerid' => $USER->id));
cff28ef0 68$editable = ($cansubmit and $ownsubmission and $workshop->submitting_allowed());
514d8c22
DM
69if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
70 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
71 // check that all required examples have been assessed by the user
72 $examples = $workshop->get_examples_for_reviewer($USER->id);
73 foreach ($examples as $exampleid => $example) {
74 if (is_null($example->grade)) {
75 $editable = false;
76 break;
77 }
78 }
79}
80$edit = ($editable and $edit);
51508f25 81
3dc78e5b
DM
82if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
83 // ok you can go
84} elseif (is_null($submission->id) and $cansubmit) {
85 // ok you can go
86} else {
51508f25 87 print_error('nopermissions');
c1e883bb
DM
88}
89
ac239eba
DM
90if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
91 require_sesskey();
92 $assessmentid = $workshop->add_allocation($submission, $USER->id);
93 redirect($workshop->assess_url($assessmentid));
94}
95
514d8c22 96if ($edit) {
67cd00ba
DM
97 require_once(dirname(__FILE__).'/submission_form.php');
98
99 $maxfiles = $workshop->nattachments;
100 $maxbytes = $workshop->maxbytes;
101 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
102 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
103 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
104 'workshop_submission_content', $submission->id);
105 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
106 'workshop_submission_attachment', $submission->id);
107
108 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
109 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
110
111 if ($mform->is_cancelled()) {
112 redirect($workshop->view_url());
113
114 } elseif ($cansubmit and $formdata = $mform->get_data()) {
115 $timenow = time();
116 if (empty($formdata->id)) {
117 $formdata->workshopid = $workshop->id;
81eccf0a 118 $formdata->example = 0;
67cd00ba
DM
119 $formdata->authorid = $USER->id;
120 $formdata->timecreated = $timenow;
557a1100 121 $formdata->feedbackauthorformat = FORMAT_HTML; // todo better default
67cd00ba
DM
122 }
123 $formdata->timemodified = $timenow;
124 $formdata->title = trim($formdata->title);
125 $formdata->content = ''; // updated later
126 $formdata->contentformat = FORMAT_HTML; // updated later
127 $formdata->contenttrust = 0; // updated later
128 if (empty($formdata->id)) {
129 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
130 // todo add to log
131 }
132 // save and relink embedded images and save attachments
133 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
134 'workshop_submission_content', $formdata->id);
135 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
136 'workshop_submission_attachment', $formdata->id);
f1b4b387 137 if (empty($formdata->attachment)) {
ac239eba 138 // explicit cast to zero integer
f1b4b387
DM
139 $formdata->attachment = 0;
140 }
67cd00ba
DM
141 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
142 $DB->update_record('workshop_submissions', $formdata);
143 redirect($workshop->submission_url($formdata->id));
33e4dea6 144 }
33e4dea6
DM
145}
146
232175e4
DM
147// load the form to override grade and/or publish the submission and process the submitted data eventually
148if (!$edit and ($canoverride or $canpublish)) {
149 $options = array(
150 'editable' => true,
151 'editablepublished' => $canpublish,
152 'overridablegrade' => $canoverride);
153 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
557a1100
DM
154 if ($data = $feedbackform->get_data()) {
155 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
7a789aa8 156 $record = new stdclass();
557a1100 157 $record->id = $submission->id;
232175e4
DM
158 if ($canoverride) {
159 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
160 $record->gradeoverby = $USER->id;
161 $record->feedbackauthor = $data->feedbackauthor;
162 $record->feedbackauthorformat = $data->feedbackauthorformat;
163 }
164 if ($canpublish) {
165 $record->published = !empty($data->published);
166 }
557a1100
DM
167 $DB->update_record('workshop_submissions', $record);
168 redirect($workshop->view_url());
169 }
170}
171
0dc47fb9
DM
172$PAGE->set_title($workshop->name);
173$PAGE->set_heading($course->fullname);
39861053 174if ($edit) {
b761e6d9 175 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 176 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 177} elseif ($ownsubmission) {
b761e6d9 178 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
179} else {
180 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 181}
33e4dea6 182
c1e883bb 183// Output starts here
39861053 184echo $OUTPUT->header();
0dc47fb9 185echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 186
3dc78e5b
DM
187// if in edit mode, display the form to edit the submission
188
514d8c22 189if ($edit) {
c1e883bb
DM
190 $mform->display();
191 echo $OUTPUT->footer();
192 die();
193}
194
3dc78e5b
DM
195// else display the submission
196
197if ($submission->id) {
6adbcb80 198 $wsoutput = $PAGE->get_renderer('mod_workshop');
e9b0f0ab 199 echo $wsoutput->submission_full($submission, true);
3dc78e5b
DM
200} else {
201 echo $OUTPUT->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
202}
203
514d8c22 204if ($editable) {
3ba60ee1 205 $url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
c5abdea3 206 echo $OUTPUT->single_button($url, get_string('editsubmission', 'workshop'), 'get');
c1e883bb
DM
207}
208
ac239eba
DM
209if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed()) {
210 $url = new moodle_url($PAGE->url, array('assess' => 1));
211 echo $OUTPUT->single_button($url, get_string('assess', 'workshop'), 'post');
212}
213
3dc78e5b
DM
214// and possibly display the submission's review(s)
215
216$canviewallassessments = false;
217if (has_capability('mod/workshop:viewallassessments', $PAGE->context)) {
218 $canviewallassessments = true;
219} elseif ($ownsubmission and $workshop->assessments_available()) {
220 $canviewallassessments = true;
3dc78e5b
DM
221}
222
223$canviewgrades = false;
224if ($isreviewer) {
225 $canviewgrades = true; // reviewers can always see the grades they gave even they are not available yet
226} elseif ($ownsubmission or $canviewallassessments) {
227 $canviewgrades = $workshop->grades_available(); // bool|null, see the function phpdoc
3dc78e5b
DM
228}
229
230if ($isreviewer) {
407b1e91 231 // display own assessment - todo
3dc78e5b
DM
232 $strategy = $workshop->grading_strategy_instance();
233}
234
235if ($canviewallassessments) {
407b1e91 236 // display all assessments (except the eventual own one - that has been already displayed) - todo
3dc78e5b
DM
237 $strategy = $workshop->grading_strategy_instance();
238}
239
557a1100
DM
240if (!$edit and $canoverride) {
241 // display a form to override the submission grade
242 $feedbackform->display();
243}
244
0dc47fb9 245echo $OUTPUT->footer();