MDL-22893, assignment module should use filepicker and filemanager
[moodle.git] / mod / assignment / type / upload / upload.php
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.
14 //
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  *
20  * @package   mod-assignment
21  * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config.php');
26 require_once(dirname(__FILE__).'/upload_form.php');
27 require_once(dirname(__FILE__).'/assignment.class.php');
28 require_once("$CFG->dirroot/repository/lib.php");
30 $contextid = required_param('contextid', PARAM_INT);
32 $url = new moodle_url('/mod/assignment/type/upload/upload.php', array('contextid'=>$contextid));
34 list($context, $course, $cm) = get_context_info_array($contextid);
36 require_login($course, true, $cm);
37 if (isguestuser()) {
38     die();
39 }
41 if (!$assignment = $DB->get_record('assignment', array('id'=>$cm->instance))) {
42     print_error('invalidid', 'assignment');
43 }
45 $PAGE->set_url($url);
46 $PAGE->set_context($context);
47 $title = strip_tags($course->fullname.': '.get_string('modulename', 'assignment').': '.format_string($assignment->name,true));
48 $PAGE->set_title($title);
49 $PAGE->set_heading($title);
51 $instance = new assignment_upload($cm->id, $assignment, $cm, $course);
52 $submission = $instance->get_submission($USER->id, true);
54 $filemanager_options = array('subdirs'=>1, 'maxbytes'=>$assignment->maxbytes, 'maxfiles'=>$assignment->var1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
56 $mform = new mod_assignment_upload_form(null, array('contextid'=>$contextid, 'options'=>$filemanager_options));
58 if ($mform->is_cancelled()) {
59     redirect(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id)));
60 } else if ($formdata = $mform->get_data()) {
61     $instance->upload($mform, $filemanager_options);
62     die;
63 }
65 echo $OUTPUT->header();
67 echo $OUTPUT->box_start('generalbox');
68 if ($instance->can_upload_file($submission)) {
69     $data = new stdclass;
70     // move submission files to user draft area
71     $data = file_prepare_standard_filemanager($data, 'files', $filemanager_options, $context, 'mod_assignment', 'submission', $submission->id);
72     // set file manager itemid, so it will find the files in draft area
73     $mform->set_data($data);
74     $mform->display();
75 } else {
76     echo $OUTPUT->notification(get_string('uploaderror', 'assignment'));
77     echo $OUTPUT->continue_button(new moodle_url('/mod/assignment/view.php', array('id'=>$cm->id)));
78 }
79 echo $OUTPUT->box_end();
81 echo $OUTPUT->footer();