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