Refactor submission.php a bit to prepare for feedback form
[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');
27require_once(dirname(__FILE__).'/lib.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?
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
45$PAGE->set_url(new moodle_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)) {
65ba104c 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);
60$canoverride = has_capability('mod/workshop:overridegrades', $workshop->context);
00aca3c1 61$isreviewer = $DB->record_exists('workshop_assessments', array('submissionid' => $submission->id, 'reviewerid' => $USER->id));
51508f25 62
3dc78e5b
DM
63if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
64 // ok you can go
65} elseif (is_null($submission->id) and $cansubmit) {
66 // ok you can go
67} else {
51508f25 68 print_error('nopermissions');
c1e883bb
DM
69}
70
67cd00ba
DM
71if ($edit and $ownsubmission) {
72 require_once(dirname(__FILE__).'/submission_form.php');
73
74 $maxfiles = $workshop->nattachments;
75 $maxbytes = $workshop->maxbytes;
76 $contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
77 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
78 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
79 'workshop_submission_content', $submission->id);
80 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
81 'workshop_submission_attachment', $submission->id);
82
83 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
84 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
85
86 if ($mform->is_cancelled()) {
87 redirect($workshop->view_url());
88
89 } elseif ($cansubmit and $formdata = $mform->get_data()) {
90 $timenow = time();
91 if (empty($formdata->id)) {
92 $formdata->workshopid = $workshop->id;
93 $formdata->example = 0; // todo add examples support
94 $formdata->authorid = $USER->id;
95 $formdata->timecreated = $timenow;
96 }
97 $formdata->timemodified = $timenow;
98 $formdata->title = trim($formdata->title);
99 $formdata->content = ''; // updated later
100 $formdata->contentformat = FORMAT_HTML; // updated later
101 $formdata->contenttrust = 0; // updated later
102 if (empty($formdata->id)) {
103 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
104 // todo add to log
105 }
106 // save and relink embedded images and save attachments
107 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
108 'workshop_submission_content', $formdata->id);
109 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
110 'workshop_submission_attachment', $formdata->id);
111 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
112 $DB->update_record('workshop_submissions', $formdata);
113 redirect($workshop->submission_url($formdata->id));
33e4dea6 114 }
33e4dea6
DM
115}
116
0dc47fb9
DM
117$PAGE->set_title($workshop->name);
118$PAGE->set_heading($course->fullname);
39861053 119if ($edit) {
b761e6d9 120 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 121 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 122} elseif ($ownsubmission) {
b761e6d9 123 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
124} else {
125 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 126}
33e4dea6 127
c1e883bb 128// Output starts here
39861053 129echo $OUTPUT->header();
51508f25
DM
130$currenttab = 'submission';
131include(dirname(__FILE__) . '/tabs.php');
0dc47fb9 132echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 133
3dc78e5b
DM
134// if in edit mode, display the form to edit the submission
135
51508f25 136if ($edit and $ownsubmission) {
c1e883bb
DM
137 $mform->display();
138 echo $OUTPUT->footer();
139 die();
140}
141
3dc78e5b
DM
142// else display the submission
143
144if ($submission->id) {
c1e883bb 145 $wsoutput = $PAGE->theme->get_renderer('mod_workshop', $PAGE);
e9b0f0ab 146 echo $wsoutput->submission_full($submission, true);
3dc78e5b
DM
147} else {
148 echo $OUTPUT->box(get_string('noyoursubmission', 'workshop'));
c1e883bb
DM
149}
150
51508f25 151if ($ownsubmission and $workshop->submitting_allowed()) {
c1e883bb
DM
152 $editbutton = new html_form();
153 $editbutton->method = 'get';
154 $editbutton->button->text = get_string('editsubmission', 'workshop');
155 $editbutton->url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
156 echo $OUTPUT->button($editbutton);
157}
158
3dc78e5b
DM
159// and possibly display the submission's review(s)
160
161$canviewallassessments = false;
162if (has_capability('mod/workshop:viewallassessments', $PAGE->context)) {
163 $canviewallassessments = true;
164} elseif ($ownsubmission and $workshop->assessments_available()) {
165 $canviewallassessments = true;
166} else {
167 $canviewallassessments = false;
168}
169
170$canviewgrades = false;
171if ($isreviewer) {
172 $canviewgrades = true; // reviewers can always see the grades they gave even they are not available yet
173} elseif ($ownsubmission or $canviewallassessments) {
174 $canviewgrades = $workshop->grades_available(); // bool|null, see the function phpdoc
3dc78e5b
DM
175}
176
177if ($isreviewer) {
407b1e91 178 // display own assessment - todo
3dc78e5b
DM
179 $strategy = $workshop->grading_strategy_instance();
180}
181
182if ($canviewallassessments) {
407b1e91 183 // display all assessments (except the eventual own one - that has been already displayed) - todo
3dc78e5b
DM
184 $strategy = $workshop->grading_strategy_instance();
185}
186
0dc47fb9 187echo $OUTPUT->footer();