1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
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/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Form used to select a file and file format for the import
|
|
|
20 |
*
|
|
|
21 |
* @package mod_lesson
|
|
|
22 |
* @copyright 2009 Sam Hemelryk
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
**/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
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
|
|
|
32 |
*/
|
|
|
33 |
class lesson_import_form extends moodleform {
|
|
|
34 |
|
|
|
35 |
public function definition() {
|
|
|
36 |
|
|
|
37 |
$mform = $this->_form;
|
|
|
38 |
|
|
|
39 |
$mform->addElement('hidden', 'id');
|
|
|
40 |
$mform->setType('id', PARAM_INT);
|
|
|
41 |
|
|
|
42 |
$mform->addElement('hidden', 'pageid');
|
|
|
43 |
$mform->setType('pageid', PARAM_INT);
|
|
|
44 |
|
|
|
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');
|
|
|
49 |
|
|
|
50 |
//Using filemanager as filepicker
|
|
|
51 |
$mform->addElement('filepicker', 'questionfile', get_string('upload'));
|
|
|
52 |
$mform->addRule('questionfile', null, 'required', null, 'client');
|
|
|
53 |
|
|
|
54 |
$this->add_action_buttons(null, get_string("import"));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
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.
|
|
|
62 |
* @throws moodle_exception
|
|
|
63 |
*/
|
|
|
64 |
protected function validate_uploaded_file($data, $errors) {
|
|
|
65 |
global $CFG;
|
|
|
66 |
|
|
|
67 |
if (empty($data['questionfile'])) {
|
|
|
68 |
$errors['questionfile'] = get_string('required');
|
|
|
69 |
return $errors;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$files = $this->get_draft_files('questionfile');
|
|
|
73 |
if (!is_array($files) || count($files) < 1) {
|
|
|
74 |
$errors['questionfile'] = get_string('required');
|
|
|
75 |
return $errors;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php';
|
|
|
79 |
if (!is_readable($formatfile)) {
|
|
|
80 |
throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
require_once($formatfile);
|
|
|
84 |
|
|
|
85 |
$classname = 'qformat_' . $data['format'];
|
|
|
86 |
$qformat = new $classname();
|
|
|
87 |
|
|
|
88 |
return $errors;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function validation($data, $files) {
|
|
|
92 |
$errors = parent::validation($data, $files);
|
|
|
93 |
$errors = $this->validate_uploaded_file($data, $errors);
|
|
|
94 |
return $errors;
|
|
|
95 |
}
|
|
|
96 |
}
|