PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lesson/import_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 96 lines | 43 code | 19 blank | 34 comment | 4 complexity | 6dd131d3bbf91bb2cf4612859001ecac MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Form used to select a file and file format for the import
  18. *
  19. * @package mod
  20. * @subpackage lesson
  21. * @copyright 2009 Sam Hemelryk
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. **/
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Form used to select a file and file format for the import
  27. * @copyright 2009 Sam Hemelryk
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class lesson_import_form extends moodleform {
  31. public function definition() {
  32. $mform = $this->_form;
  33. $mform->addElement('hidden', 'id');
  34. $mform->setType('id', PARAM_INT);
  35. $mform->addElement('hidden', 'pageid');
  36. $mform->setType('pageid', PARAM_INT);
  37. $mform->addElement('select', 'format', get_string('fileformat', 'lesson'), $this->_customdata['formats']);
  38. $mform->setDefault('format', 'gift');
  39. $mform->setType('format', 'text');
  40. $mform->addRule('format', null, 'required');
  41. //Using filemanager as filepicker
  42. $mform->addElement('filepicker', 'questionfile', get_string('upload'));
  43. $mform->addRule('questionfile', null, 'required', null, 'client');
  44. $this->add_action_buttons(null, get_string("import"));
  45. }
  46. /**
  47. * Checks that a file has been uploaded, and that it is of a plausible type.
  48. * @param array $data the submitted data.
  49. * @param array $errors the errors so far.
  50. * @return array the updated errors.
  51. */
  52. protected function validate_uploaded_file($data, $errors) {
  53. global $CFG;
  54. if (empty($data['questionfile'])) {
  55. $errors['questionfile'] = get_string('required');
  56. return $errors;
  57. }
  58. $files = $this->get_draft_files('questionfile');
  59. if (count($files) < 1) {
  60. $errors['questionfile'] = get_string('required');
  61. return $errors;
  62. }
  63. $formatfile = $CFG->dirroot.'/question/format/'.$data['format'].'/format.php';
  64. if (!is_readable($formatfile)) {
  65. throw new moodle_exception('formatnotfound', 'lesson', '', $data['format']);
  66. }
  67. require_once($formatfile);
  68. $classname = 'qformat_' . $data['format'];
  69. $qformat = new $classname();
  70. return $errors;
  71. }
  72. public function validation($data, $files) {
  73. $errors = parent::validation($data, $files);
  74. $errors = $this->validate_uploaded_file($data, $errors);
  75. return $errors;
  76. }
  77. }