moodle/mod/workshop/submission.php

136 lines
5.8 KiB
PHP
Raw Normal View History

2010-01-04 17:38:29 +00:00
<?php
// This file is part of Moodle - http://moodle.org/
//
2010-01-04 17:38:29 +00:00
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
2010-01-04 17:38:29 +00:00
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Submit own assignment or edit the already submitted own work
2010-01-04 17:38:29 +00:00
*
* @package mod-workshop
* @copyright 2009 David Mudrak <david.mudrak@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
2010-01-04 17:42:37 +00:00
require_once(dirname(__FILE__).'/locallib.php');
2010-01-04 17:38:29 +00:00
require_once(dirname(__FILE__).'/submission_form.php');
$cmid = required_param('cmid', PARAM_INT); // course module id
$id = optional_param('id', 0, PARAM_INT); // submission id
$edit = optional_param('edit', false, PARAM_BOOL); // open for editing?
2010-01-04 17:38:29 +00:00
$cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
2010-01-04 17:38:29 +00:00
require_login($course, false, $cm);
require_capability('mod/workshop:submit', $PAGE->context);
2010-01-04 17:38:29 +00:00
if (isguestuser()) {
print_error('guestsarenotallowed');
2010-01-04 17:38:29 +00:00
}
$workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
2010-01-04 17:49:01 +00:00
$workshop = new workshop($workshop, $cm, $course);
2010-01-04 17:38:29 +00:00
2010-01-04 17:42:37 +00:00
if ($id) { // submission is specified
$submission = $DB->get_record('workshop_submissions', array('id' => $id, 'workshopid' => $workshop->id), '*', MUST_EXIST);
2010-01-04 17:42:37 +00:00
} else { // no submission specified
if (!$submission = $workshop->get_submission_by_author($USER->id)) {
$submission = new stdClass();
2010-01-04 17:42:37 +00:00
$submission->id = null;
$submission->userid = $USER->id;
}
2010-01-04 17:38:29 +00:00
}
if ($submission->userid !== $USER->id) {
print_error('nopermissiontoviewpage', 'error', $workshop->view_url());
}
$maxfiles = $workshop->nattachments;
$maxbytes = $workshop->maxbytes;
$contentopts = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
$attachmentopts = array('subdirs' => false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
$submission = file_prepare_standard_editor($submission, 'content', $contentopts, $PAGE->context,
'workshop_submission_content', $submission->id);
$submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $PAGE->context,
'workshop_submission_attachment', $submission->id);
$mform = new workshop_submission_form(null, array('current' => $submission, 'cm' => $cm, 'workshop' => $workshop,
'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
2010-01-04 17:38:29 +00:00
if ($mform->is_cancelled()) {
redirect($workshop->view_url());
} elseif ($formdata = $mform->get_data()) {
2010-01-04 17:38:29 +00:00
$timenow = time();
if (empty($formdata->id)) {
$formdata->workshopid = $workshop->id;
$formdata->example = 0; // todo add examples support
$formdata->userid = $USER->id;
$formdata->timecreated = $timenow;
2010-01-04 17:38:29 +00:00
}
$formdata->timemodified = $timenow;
$formdata->title = trim($formdata->title);
$formdata->content = ''; // updated later
$formdata->contentformat = FORMAT_HTML; // updated later
$formdata->contenttrust = 0; // updated later
if (empty($formdata->id)) {
$formdata->id = $DB->insert_record('workshop_submissions', $formdata);
2010-01-04 17:38:29 +00:00
// todo add to log
}
// save and relink embedded images and save attachments
$formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $PAGE->context,
'workshop_submission_content', $formdata->id);
$formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $PAGE->context,
'workshop_submission_attachment', $formdata->id);
// store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
$DB->update_record('workshop_submissions', $formdata);
redirect($workshop->view_url());
2010-01-04 17:38:29 +00:00
}
$PAGE->set_url('mod/workshop/submission.php', array('cmid' => $cm->id));
$PAGE->set_title($workshop->name);
$PAGE->set_heading($course->fullname);
if ($edit) {
$PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
$PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
} else {
$PAGE->navbar->add(get_string('mysubmission', 'workshop'));
}
2010-01-04 17:38:29 +00:00
// Output starts here
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($workshop->name), 2);
if ($edit) {
$mform->display();
echo $OUTPUT->footer();
die();
}
if (!empty($submission->id)) {
$wsoutput = $PAGE->theme->get_renderer('mod_workshop', $PAGE);
2010-01-04 18:01:40 +00:00
echo $wsoutput->submission_full($submission, true);
}
if ($workshop->submitting_allowed()) {
$editbutton = new html_form();
$editbutton->method = 'get';
$editbutton->button->text = get_string('editsubmission', 'workshop');
$editbutton->url = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
echo $OUTPUT->button($editbutton);
}
echo $OUTPUT->footer();