Permalink
Browse files

MDL-27964 lesson - Old file picker is replaced with new filepicker, c…

…urrently all imported questions will be saved in /temp/questionimport/ where questionbank files are saved.
  • Loading branch information...
1 parent 7fde489 commit b89bf55f8467b99ca3d15a8f71a9f09ae1e5b7bd Rajesh Taneja committed Jun 23, 2011
Showing with 54 additions and 23 deletions.
  1. +8 −4 mod/lesson/import.php
  2. +46 −19 mod/lesson/import_form.php
View
@@ -66,9 +66,13 @@
require_sesskey();
- if (!$importfile = $mform->get_importfile_name()) {
- print_error('uploadproblem', 'moodle');
- }
+ $realfilename = $mform->get_new_filename('questionfile');
+ //TODO: Leave all imported questions in Questionimport for now.
+ $importfile = "{$CFG->dataroot}/temp/questionimport/{$realfilename}";
+ make_upload_directory('temp/questionimport');
+ if (!$result = $mform->save_file('questionfile', $importfile, true)) {
+ throw new moodle_exception('uploadproblem');
+ }
$formatclass = 'qformat_'.$data->format;
$formatclassfile = $CFG->dirroot.'/question/format/'.$data->format.'/format.php';
@@ -102,4 +106,4 @@
$mform->display();
}
-echo $OUTPUT->footer();
+echo $OUTPUT->footer();
@@ -32,8 +32,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lesson_import_form extends moodleform {
-
public function definition() {
+ global $COURSE;
$mform = $this->_form;
@@ -48,30 +48,57 @@ public function definition() {
$mform->setType('format', 'text');
$mform->addRule('format', null, 'required');
- $mform->addElement('file', 'newfile', get_string('upload'), array('size'=>'50'));
- $mform->addRule('newfile', null, 'required');
-
- $this->add_action_buttons(null, get_string("uploadthisfile"));
+ //Using filemanager as filepicker
+ $mform->addElement('filepicker', 'questionfile', get_string('upload'));
+ $mform->addRule('questionfile', null, 'required', null, 'client');
+ $this->add_action_buttons(null, get_string("import"));
}
- public function get_importfile_name(){
- if ($this->is_submitted() and $this->is_validated()) {
- // return the temporary filename to process
- return $_FILES['newfile']['tmp_name'];
- }else{
- return NULL;
+ /**
+ * Checks that a file has been uploaded, and that it is of a plausible type.
+ * @param array $data the submitted data.
+ * @param array $errors the errors so far.
+ * @return array the updated errors.
+ */
+ protected function validate_uploaded_file($data, $errors) {
+ global $CFG;
+
+ if (empty($data['questionfile'])) {
+ $errors['questionfile'] = get_string('required');
+ return $errors;
+ }
+
+ $files = $this->get_draft_files('questionfile');
+ if (count($files) < 1) {
+ $errors['questionfile'] = get_string('required');
+ return $errors;
}
- }
- public function get_importfile_realname(){
- if ($this->is_submitted() and $this->is_validated()) {
- // return the temporary filename to process
- // TODO change this to use the files API properly.
- return $_FILES['newfile']['name'];
- }else{
- return NULL;
+ $formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php';
+ if (!is_readable($formatfile)) {
+ throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
}
+
+ require_once($formatfile);
+
+ $classname = 'qformat_' . $data['format'];
+ $qformat = new $classname();
+
+ $file = reset($files);
+ if ($file->get_mimetype() != $qformat->mime_type()) {
+ $a = new stdClass();
+ $a->actualtype = $file->get_mimetype();
+ $a->expectedtype = $qformat->mime_type();
+ $errors['newfile'] = get_string('importwrongfiletype', 'lesson', $a);
+ }
+
+ return $errors;
}
+ public function validation($data, $files) {
+ $errors = parent::validation($data, $files);
+ $errors = $this->validate_uploaded_file($data, $errors);
+ return $errors;
+ }
}

0 comments on commit b89bf55

Please sign in to comment.