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