MDL-63086 block_rss_client: Move skip time calculation to task
[moodle.git] / mod / lesson / import_form.php
blobb152c0abe242a30ac0284b174fca25c9c212ab52
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.
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 * Form used to select a file and file format for the import
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 defined('MOODLE_INTERNAL') || die();
28 /**
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"));
57 /**
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.
63 protected function validate_uploaded_file($data, $errors) {
64 global $CFG;
66 if (empty($data['questionfile'])) {
67 $errors['questionfile'] = get_string('required');
68 return $errors;
71 $files = $this->get_draft_files('questionfile');
72 if (count($files) < 1) {
73 $errors['questionfile'] = get_string('required');
74 return $errors;
77 $formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php';
78 if (!is_readable($formatfile)) {
79 throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
82 require_once($formatfile);
84 $classname = 'qformat_' . $data['format'];
85 $qformat = new $classname();
87 return $errors;
90 public function validation($data, $files) {
91 $errors = parent::validation($data, $files);
92 $errors = $this->validate_uploaded_file($data, $errors);
93 return $errors;