Displays the list of submissions
[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/**
c1e883bb 19 * Submit own assignment or edit the already submitted 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
DM
29require_once(dirname(__FILE__).'/submission_form.php');
30
c1e883bb
DM
31$cmid = required_param('cmid', PARAM_INT); // course module id
32$id = optional_param('id', 0, PARAM_INT); // submission id
33$edit = optional_param('edit', false, PARAM_BOOL); // open for editing?
33e4dea6 34
c1e883bb
DM
35$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
33e4dea6
DM
37
38require_login($course, false, $cm);
b8ead2e6 39require_capability('mod/workshop:submit', $PAGE->context);
33e4dea6 40if (isguestuser()) {
b8ead2e6 41 print_error('guestsarenotallowed');
33e4dea6
DM
42}
43
0dc47fb9 44$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
a39d7d87 45$workshop = new workshop($workshop, $cm, $course);
33e4dea6 46
127032fe 47if ($id) { // submission is specified
0dc47fb9 48 $submission = $DB->get_record('workshop_submissions', array('id' => $id, 'workshopid' => $workshop->id), '*', MUST_EXIST);
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;
c1e883bb 53 $submission->userid = $USER->id;
53fad4b9 54 }
33e4dea6 55}
c1e883bb
DM
56
57if ($submission->userid !== $USER->id) {
58 print_error('nopermissiontoviewpage', 'error', $workshop->view_url());
59}
60
61$maxfiles = $workshop->nattachments;
62$maxbytes = $workshop->maxbytes;
63$contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
64$attachmentopts = array('subdirs' => false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
65$submission = file_prepare_standard_editor($submission, 'content', $contentopts, $PAGE->context,
66 'workshop_submission_content', $submission->id);
67$submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $PAGE->context,
68 'workshop_submission_attachment', $submission->id);
69$mform = new workshop_submission_form(null, array('current' => $submission, 'cm' => $cm, 'workshop' => $workshop,
70 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
33e4dea6 71
6e309973 72if ($mform->is_cancelled()) {
c1e883bb
DM
73 redirect($workshop->view_url());
74
75} elseif ($formdata = $mform->get_data()) {
33e4dea6 76 $timenow = time();
c1e883bb
DM
77 if (empty($formdata->id)) {
78 $formdata->workshopid = $workshop->id;
79 $formdata->example = 0; // todo add examples support
80 $formdata->userid = $USER->id;
81 $formdata->timecreated = $timenow;
33e4dea6 82 }
c1e883bb
DM
83 $formdata->timemodified = $timenow;
84 $formdata->title = trim($formdata->title);
85 $formdata->content = ''; // updated later
86 $formdata->contentformat = FORMAT_HTML; // updated later
87 $formdata->contenttrust = 0; // updated later
88 if (empty($formdata->id)) {
89 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
33e4dea6 90 // todo add to log
33e4dea6 91 }
33e4dea6 92 // save and relink embedded images and save attachments
c1e883bb
DM
93 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $PAGE->context,
94 'workshop_submission_content', $formdata->id);
95 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $PAGE->context,
96 'workshop_submission_attachment', $formdata->id);
97 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
98 $DB->update_record('workshop_submissions', $formdata);
99 redirect($workshop->view_url());
33e4dea6
DM
100}
101
0dc47fb9
DM
102$PAGE->set_url('mod/workshop/submission.php', array('cmid' => $cm->id));
103$PAGE->set_title($workshop->name);
104$PAGE->set_heading($course->fullname);
39861053 105if ($edit) {
b761e6d9 106 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 107 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
b761e6d9
DM
108} else {
109 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
39861053 110}
33e4dea6 111
c1e883bb 112// Output starts here
39861053 113echo $OUTPUT->header();
0dc47fb9 114echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb
DM
115
116if ($edit) {
117 $mform->display();
118 echo $OUTPUT->footer();
119 die();
120}
121
122if (!empty($submission->id)) {
123 $wsoutput = $PAGE->theme->get_renderer('mod_workshop', $PAGE);
e9b0f0ab 124 echo $wsoutput->submission_full($submission, true);
c1e883bb
DM
125}
126
127if ($workshop->submitting_allowed()) {
128 $editbutton = new html_form();
129 $editbutton->method = 'get';
130 $editbutton->button->text = get_string('editsubmission', 'workshop');
131 $editbutton->url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
132 echo $OUTPUT->button($editbutton);
133}
134
0dc47fb9 135echo $OUTPUT->footer();