3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Form used to select a file and file format for the import
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Form used to select a file and file format for the import
30 * @copyright 2009 Sam Hemelryk
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class lesson_import_form
extends moodleform
{
35 public function definition() {
37 $mform = $this->_form
;
39 $mform->addElement('hidden', 'id');
40 $mform->setType('id', PARAM_INT
);
42 $mform->addElement('hidden', 'pageid');
43 $mform->setType('pageid', PARAM_INT
);
45 $mform->addElement('select', 'format', get_string('fileformat', 'lesson'), $this->_customdata
['formats']);
46 $mform->setDefault('format', 'gift');
47 $mform->setType('format', 'text');
48 $mform->addRule('format', null, 'required');
50 //Using filemanager as filepicker
51 $mform->addElement('filepicker', 'questionfile', get_string('upload'));
52 $mform->addRule('questionfile', null, 'required', null, 'client');
54 $this->add_action_buttons(null, get_string("import"));
58 * Checks that a file has been uploaded, and that it is of a plausible type.
59 * @param array $data the submitted data.
60 * @param array $errors the errors so far.
61 * @return array the updated errors.
62 * @throws moodle_exception
64 protected function validate_uploaded_file($data, $errors) {
67 if (empty($data['questionfile'])) {
68 $errors['questionfile'] = get_string('required');
72 $files = $this->get_draft_files('questionfile');
73 if (!is_array($files) ||
count($files) < 1) {
74 $errors['questionfile'] = get_string('required');
78 $formatfile = $CFG->dirroot
.'/question/format/'.$data['format'].'/format.php';
79 if (!is_readable($formatfile)) {
80 throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
83 require_once($formatfile);
85 $classname = 'qformat_' . $data['format'];
86 $qformat = new $classname();
91 public function validation($data, $files) {
92 $errors = parent
::validation($data, $files);
93 $errors = $this->validate_uploaded_file($data, $errors);