Moodle release 2.7.8
[moodle.git] / mod / workshop / submission.php
blobc889f870f7fb66347d5c70474476aa2f5f2916c7
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
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/>.
18 /**
19 * View a single (usually the own) submission, submit own work.
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
26 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27 require_once(dirname(__FILE__).'/locallib.php');
28 require_once($CFG->dirroot . '/repository/lib.php');
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?
33 $assess = optional_param('assess', false, PARAM_BOOL); // instant assessment required
35 $cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38 require_login($course, false, $cm);
39 if (isguestuser()) {
40 print_error('guestsarenotallowed');
43 $workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44 $workshop = new workshop($workshoprecord, $cm, $course);
46 $PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id));
48 if ($edit) {
49 $PAGE->url->param('edit', $edit);
52 if ($id) { // submission is specified
53 $submission = $workshop->get_submission_by_id($id);
55 $params = array(
56 'objectid' => $submission->id,
57 'context' => $workshop->context,
58 'courseid' => $workshop->course->id,
59 'relateduserid' => $submission->authorid,
60 'other' => array(
61 'workshopid' => $workshop->id
65 $event = \mod_workshop\event\submission_viewed::create($params);
66 $event->trigger();
68 } else { // no submission specified
69 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
70 $submission = new stdclass();
71 $submission->id = null;
72 $submission->authorid = $USER->id;
73 $submission->example = 0;
74 $submission->grade = null;
75 $submission->gradeover = null;
76 $submission->published = null;
77 $submission->feedbackauthor = null;
78 $submission->feedbackauthorformat = editors_get_preferred_format();
82 $ownsubmission = $submission->authorid == $USER->id;
83 $canviewall = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
84 $cansubmit = has_capability('mod/workshop:submit', $workshop->context);
85 $canallocate = has_capability('mod/workshop:allocate', $workshop->context);
86 $canpublish = has_capability('mod/workshop:publishsubmissions', $workshop->context);
87 $canoverride = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
88 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
89 $isreviewer = !empty($userassessment);
90 $editable = ($cansubmit and $ownsubmission);
91 $ispublished = ($workshop->phase == workshop::PHASE_CLOSED
92 and $submission->published == 1
93 and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
95 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
96 $editable = false;
98 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
99 $editable = false;
102 if ($canviewall) {
103 // check this flag against the group membership yet
104 if (groups_get_activity_groupmode($workshop->cm) == SEPARATEGROUPS) {
105 // user must have accessallgroups or share at least one group with the submission author
106 if (!has_capability('moodle/site:accessallgroups', $workshop->context)) {
107 $usersgroups = groups_get_activity_allowed_groups($workshop->cm);
108 $authorsgroups = groups_get_all_groups($workshop->course->id, $submission->authorid, $workshop->cm->groupingid, 'g.id');
109 $sharedgroups = array_intersect_key($usersgroups, $authorsgroups);
110 if (empty($sharedgroups)) {
111 $canviewall = false;
117 if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
118 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
119 // check that all required examples have been assessed by the user
120 $examples = $workshop->get_examples_for_reviewer($USER->id);
121 foreach ($examples as $exampleid => $example) {
122 if (is_null($example->grade)) {
123 $editable = false;
124 break;
128 $edit = ($editable and $edit);
130 $seenaspublished = false; // is the submission seen as a published submission?
132 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
133 // ok you can go
134 } elseif ($submission->id and $ispublished) {
135 // ok you can go
136 $seenaspublished = true;
137 } elseif (is_null($submission->id) and $cansubmit) {
138 // ok you can go
139 } else {
140 print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
143 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
144 require_sesskey();
145 $assessmentid = $workshop->add_allocation($submission, $USER->id);
146 redirect($workshop->assess_url($assessmentid));
149 if ($edit) {
150 require_once(dirname(__FILE__).'/submission_form.php');
152 $maxfiles = $workshop->nattachments;
153 $maxbytes = $workshop->maxbytes;
154 $contentopts = array(
155 'trusttext' => true,
156 'subdirs' => false,
157 'maxfiles' => $maxfiles,
158 'maxbytes' => $maxbytes,
159 'context' => $workshop->context,
160 'return_types' => FILE_INTERNAL | FILE_EXTERNAL
163 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes, 'return_types' => FILE_INTERNAL);
164 $submission = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
165 'mod_workshop', 'submission_content', $submission->id);
166 $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
167 'mod_workshop', 'submission_attachment', $submission->id);
169 $mform = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
170 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
172 if ($mform->is_cancelled()) {
173 redirect($workshop->view_url());
175 } elseif ($cansubmit and $formdata = $mform->get_data()) {
176 if ($formdata->example == 0) {
177 // this was used just for validation, it must be set to zero when dealing with normal submissions
178 unset($formdata->example);
179 } else {
180 throw new coding_exception('Invalid submission form data value: example');
182 $timenow = time();
183 if (is_null($submission->id)) {
184 $formdata->workshopid = $workshop->id;
185 $formdata->example = 0;
186 $formdata->authorid = $USER->id;
187 $formdata->timecreated = $timenow;
188 $formdata->feedbackauthorformat = editors_get_preferred_format();
190 $formdata->timemodified = $timenow;
191 $formdata->title = trim($formdata->title);
192 $formdata->content = ''; // updated later
193 $formdata->contentformat = FORMAT_HTML; // updated later
194 $formdata->contenttrust = 0; // updated later
195 $formdata->late = 0x0; // bit mask
196 if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
197 $formdata->late = $formdata->late | 0x1;
199 if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
200 $formdata->late = $formdata->late | 0x2;
203 // Event information.
204 $params = array(
205 'context' => $workshop->context,
206 'courseid' => $workshop->course->id,
207 'other' => array(
208 'submissiontitle' => $formdata->title
211 $logdata = null;
212 if (is_null($submission->id)) {
213 $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
214 $params['objectid'] = $submission->id;
215 $event = \mod_workshop\event\submission_created::create($params);
216 $event->trigger();
217 } else {
218 if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
219 throw new moodle_exception('err_submissionid', 'workshop');
222 $params['objectid'] = $submission->id;
223 // save and relink embedded images and save attachments
224 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
225 'mod_workshop', 'submission_content', $submission->id);
226 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
227 'mod_workshop', 'submission_attachment', $submission->id);
228 if (empty($formdata->attachment)) {
229 // explicit cast to zero integer
230 $formdata->attachment = 0;
232 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
233 $DB->update_record('workshop_submissions', $formdata);
234 $event = \mod_workshop\event\submission_updated::create($params);
235 $event->add_record_snapshot('workshop', $workshoprecord);
236 $event->trigger();
238 // send submitted content for plagiarism detection
239 $fs = get_file_storage();
240 $files = $fs->get_area_files($workshop->context->id, 'mod_workshop', 'submission_attachment', $submission->id);
242 $params['other']['content'] = $formdata->content;
243 $params['other']['pathnamehashes'] = array_keys($files);
245 $event = \mod_workshop\event\assessable_uploaded::create($params);
246 $event->set_legacy_logdata($logdata);
247 $event->trigger();
249 redirect($workshop->submission_url($formdata->id));
253 // load the form to override grade and/or publish the submission and process the submitted data eventually
254 if (!$edit and ($canoverride or $canpublish)) {
255 $options = array(
256 'editable' => true,
257 'editablepublished' => $canpublish,
258 'overridablegrade' => $canoverride);
259 $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
260 if ($data = $feedbackform->get_data()) {
261 $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
262 $record = new stdclass();
263 $record->id = $submission->id;
264 if ($canoverride) {
265 $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
266 $record->gradeoverby = $USER->id;
267 $record->feedbackauthor = $data->feedbackauthor;
268 $record->feedbackauthorformat = $data->feedbackauthorformat;
270 if ($canpublish) {
271 $record->published = !empty($data->published);
273 $DB->update_record('workshop_submissions', $record);
274 redirect($workshop->view_url());
278 $PAGE->set_title($workshop->name);
279 $PAGE->set_heading($course->fullname);
280 if ($edit) {
281 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
282 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
283 } elseif ($ownsubmission) {
284 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
285 } else {
286 $PAGE->navbar->add(get_string('submission', 'workshop'));
289 // Output starts here
290 $output = $PAGE->get_renderer('mod_workshop');
291 echo $output->header();
292 echo $output->heading(format_string($workshop->name), 2);
294 // show instructions for submitting as thay may contain some list of questions and we need to know them
295 // while reading the submitted answer
296 if (trim($workshop->instructauthors)) {
297 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
298 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
299 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
300 echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
301 print_collapsible_region_end();
304 // if in edit mode, display the form to edit the submission
306 if ($edit) {
307 if (!empty($CFG->enableplagiarism)) {
308 require_once($CFG->libdir.'/plagiarismlib.php');
309 echo plagiarism_print_disclosure($cm->id);
311 $mform->display();
312 echo $output->footer();
313 die();
316 // else display the submission
318 if ($submission->id) {
319 if ($seenaspublished) {
320 $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
321 } else {
322 $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
324 echo $output->render($workshop->prepare_submission($submission, $showauthor));
325 } else {
326 echo $output->box(get_string('noyoursubmission', 'workshop'));
329 if ($editable) {
330 if ($submission->id) {
331 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
332 $btntxt = get_string('editsubmission', 'workshop');
333 } else {
334 $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
335 $btntxt = get_string('createsubmission', 'workshop');
337 echo $output->single_button($btnurl, $btntxt, 'get');
340 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
341 $url = new moodle_url($PAGE->url, array('assess' => 1));
342 echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
345 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
346 if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
347 echo $output->render(new workshop_feedback_author($submission));
351 // and possibly display the submission's review(s)
353 if ($isreviewer) {
354 // user's own assessment
355 $strategy = $workshop->grading_strategy_instance();
356 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
357 $options = array(
358 'showreviewer' => true,
359 'showauthor' => $showauthor,
360 'showform' => !is_null($userassessment->grade),
361 'showweight' => true,
363 $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
364 $assessment->title = get_string('assessmentbyyourself', 'workshop');
366 if ($workshop->assessing_allowed($USER->id)) {
367 if (is_null($userassessment->grade)) {
368 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
369 } else {
370 $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
373 if ($canoverride) {
374 $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
377 echo $output->render($assessment);
379 if ($workshop->phase == workshop::PHASE_CLOSED) {
380 if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
381 echo $output->render(new workshop_feedback_reviewer($userassessment));
386 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
387 // other assessments
388 $strategy = $workshop->grading_strategy_instance();
389 $assessments = $workshop->get_assessments_of_submission($submission->id);
390 $showreviewer = has_capability('mod/workshop:viewreviewernames', $workshop->context);
391 foreach ($assessments as $assessment) {
392 if ($assessment->reviewerid == $USER->id) {
393 // own assessment has been displayed already
394 continue;
396 if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
397 // students do not see peer-assessment that are not graded yet
398 continue;
400 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
401 $options = array(
402 'showreviewer' => $showreviewer,
403 'showauthor' => $showauthor,
404 'showform' => !is_null($assessment->grade),
405 'showweight' => true,
407 $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
408 if ($canoverride) {
409 $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
411 echo $output->render($displayassessment);
413 if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
414 if (strlen(trim($assessment->feedbackreviewer)) > 0) {
415 echo $output->render(new workshop_feedback_reviewer($assessment));
421 if (!$edit and $canoverride) {
422 // display a form to override the submission grade
423 $feedbackform->display();
426 echo $output->footer();