Go workshop, go!
[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
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);
33e4dea6 39if (isguestuser()) {
b8ead2e6 40 print_error('guestsarenotallowed');
33e4dea6
DM
41}
42
51508f25
DM
43$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44$workshop = new workshop($workshop, $cm, $course);
45
46$PAGE->set_url(new moodle_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id, 'edit' => $edit)));
33e4dea6 47
127032fe 48if ($id) { // submission is specified
51508f25 49 $submission = $workshop->get_submission_by_id($id);
127032fe 50} else { // no submission specified
0dc47fb9 51 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
65ba104c 52 $submission = new stdClass();
127032fe 53 $submission->id = null;
c1e883bb 54 $submission->userid = $USER->id;
53fad4b9 55 }
33e4dea6 56}
c1e883bb 57
51508f25
DM
58$ownsubmission = $submission->userid == $USER->id;
59$canviewall = has_capability('mod/workshop:viewallsubmissions', $PAGE->context);
60$cansubmit = has_capability('mod/workshop:submit', $PAGE->context);
61
62if (!$ownsubmission and !$canviewall) {
63 print_error('nopermissions');
64}
65if ($ownsubmission and !$cansubmit) {
66 print_error('nopermissions');
c1e883bb
DM
67}
68
69$maxfiles = $workshop->nattachments;
70$maxbytes = $workshop->maxbytes;
71$contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
72$attachmentopts = array('subdirs' => false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
73$submission = file_prepare_standard_editor($submission, 'content', $contentopts, $PAGE->context,
74 'workshop_submission_content', $submission->id);
75$submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $PAGE->context,
76 'workshop_submission_attachment', $submission->id);
77$mform = new workshop_submission_form(null, array('current' => $submission, 'cm' => $cm, 'workshop' => $workshop,
78 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
33e4dea6 79
6e309973 80if ($mform->is_cancelled()) {
c1e883bb
DM
81 redirect($workshop->view_url());
82
51508f25 83} elseif ($cansubmit and $formdata = $mform->get_data()) {
33e4dea6 84 $timenow = time();
c1e883bb
DM
85 if (empty($formdata->id)) {
86 $formdata->workshopid = $workshop->id;
87 $formdata->example = 0; // todo add examples support
88 $formdata->userid = $USER->id;
89 $formdata->timecreated = $timenow;
33e4dea6 90 }
c1e883bb
DM
91 $formdata->timemodified = $timenow;
92 $formdata->title = trim($formdata->title);
93 $formdata->content = ''; // updated later
94 $formdata->contentformat = FORMAT_HTML; // updated later
95 $formdata->contenttrust = 0; // updated later
96 if (empty($formdata->id)) {
97 $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
33e4dea6 98 // todo add to log
33e4dea6 99 }
33e4dea6 100 // save and relink embedded images and save attachments
c1e883bb
DM
101 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $PAGE->context,
102 'workshop_submission_content', $formdata->id);
103 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $PAGE->context,
104 'workshop_submission_attachment', $formdata->id);
105 // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
106 $DB->update_record('workshop_submissions', $formdata);
107 redirect($workshop->view_url());
33e4dea6
DM
108}
109
0dc47fb9
DM
110$PAGE->set_title($workshop->name);
111$PAGE->set_heading($course->fullname);
39861053 112if ($edit) {
b761e6d9 113 $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
39861053 114 $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
51508f25 115} elseif ($ownsubmission) {
b761e6d9 116 $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
51508f25
DM
117} else {
118 $PAGE->navbar->add(get_string('submission', 'workshop'));
39861053 119}
33e4dea6 120
c1e883bb 121// Output starts here
39861053 122echo $OUTPUT->header();
51508f25
DM
123$currenttab = 'submission';
124include(dirname(__FILE__) . '/tabs.php');
0dc47fb9 125echo $OUTPUT->heading(format_string($workshop->name), 2);
c1e883bb 126
51508f25 127if ($edit and $ownsubmission) {
c1e883bb
DM
128 $mform->display();
129 echo $OUTPUT->footer();
130 die();
131}
132
133if (!empty($submission->id)) {
134 $wsoutput = $PAGE->theme->get_renderer('mod_workshop', $PAGE);
e9b0f0ab 135 echo $wsoutput->submission_full($submission, true);
c1e883bb
DM
136}
137
51508f25 138if ($ownsubmission and $workshop->submitting_allowed()) {
c1e883bb
DM
139 $editbutton = new html_form();
140 $editbutton->method = 'get';
141 $editbutton->button->text = get_string('editsubmission', 'workshop');
142 $editbutton->url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
143 echo $OUTPUT->button($editbutton);
144}
145
0dc47fb9 146echo $OUTPUT->footer();