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