Deletes the embeded media when deleting the assessment dimension
[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
DM
18/**
19 * Submit an assignment or edit the already submitted work
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
31$cmid = required_param('cmid', PARAM_INT); // course module id
32$id = optional_param('id', 0, PARAM_INT); // submission id
33
0dc47fb9
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);
b8ead2e6 38require_capability('mod/workshop:submit', $PAGE->context);
33e4dea6 39if (isguestuser()) {
b8ead2e6 40 print_error('guestsarenotallowed');
33e4dea6
DM
41}
42
0dc47fb9 43$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
a39d7d87 44$workshop = new workshop($workshop, $cm, $course);
33e4dea6 45
127032fe 46if ($id) { // submission is specified
0dc47fb9 47 $submission = $DB->get_record('workshop_submissions', array('id' => $id, 'workshopid' => $workshop->id), '*', MUST_EXIST);
127032fe 48} else { // no submission specified
0dc47fb9 49 if (!$submission = $workshop->get_submission_by_author($USER->id)) {
127032fe
DM
50 $submission = new object();
51 $submission->id = null;
53fad4b9 52 }
33e4dea6 53}
127032fe 54unset($id); // not needed anymore
33e4dea6 55
0dc47fb9
DM
56$maxfiles = $workshop->nattachments;
57$maxbytes = $workshop->maxbytes;
58$contentoptions = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
59$attachmentoptions = array('subdirs' => false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
60$submission = file_prepare_standard_editor($submission, 'content', $contentoptions, $PAGE->context,
61 'workshop_submission_content', $submission->id);
62$submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentoptions, $PAGE->context,
63 'workshop_submission_attachment', $submission->id);
33e4dea6 64// create form and set initial data
0dc47fb9
DM
65$mform = new workshop_submission_form(null, array('current' => $submission, 'cm' => $cm, 'workshop' => $workshop,
66 'contentoptions' => $contentoptions, 'attachmentoptions' => $attachmentoptions));
33e4dea6 67
6e309973
DM
68if ($mform->is_cancelled()) {
69 redirect("view.php?id=$cm->id");
33e4dea6 70} else if ($submission = $mform->get_data()) {
33e4dea6 71 $timenow = time();
33e4dea6
DM
72 if (empty($submission->id)) {
73 $submission->workshopid = $workshop->id;
74 $submission->example = 0; // todo add examples support
75 $submission->userid = $USER->id;
76 $submission->timecreated = $timenow;
77 }
33e4dea6
DM
78 $submission->timemodified = $timenow;
79 $submission->title = trim($submission->title);
0dc47fb9
DM
80 $submission->content = ''; // updated later
81 $submission->contentformat = FORMAT_HTML; // updated later
82 $submission->contenttrust = 0; // updated later
33e4dea6 83 if (empty($submission->id)) {
33e4dea6
DM
84 $submission->id = $DB->insert_record('workshop_submissions', $submission);
85 // todo add to log
33e4dea6 86 }
33e4dea6 87 // save and relink embedded images and save attachments
0dc47fb9
DM
88 $submission = file_postupdate_standard_editor($submission, 'content', $contentoptions, $PAGE->context,
89 'workshop_submission_content', $submission->id);
90 $submission = file_postupdate_standard_filemanager($submission, 'attachment', $attachmentoptions, $PAGE->context,
91 'workshop_submission_attachment', $submission->id);
127032fe 92 // store the updated values or re-save the new submission
33e4dea6 93 $DB->update_record('workshop_submissions', $submission);
33e4dea6
DM
94 redirect("view.php?id=$cm->id");
95}
96
0dc47fb9
DM
97// Output starts here
98$PAGE->set_url('mod/workshop/submission.php', array('cmid' => $cm->id));
99$PAGE->set_title($workshop->name);
100$PAGE->set_heading($course->fullname);
33e4dea6 101
0dc47fb9 102$stredit = empty($submission->id) ? get_string('editingsubmission', 'workshop') : get_string('edit');
33e4dea6 103$navigation = build_navigation($stredit, $cm);
a39d7d87 104// todo $menu = navmenu($course, $cm);
33e4dea6 105
a39d7d87 106echo $OUTPUT->header($navigation);
0dc47fb9 107echo $OUTPUT->heading(format_string($workshop->name), 2);
33e4dea6 108$mform->display();
0dc47fb9 109echo $OUTPUT->footer();