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

/question/import_form.php

http://github.com/moodle/moodle
PHP | 160 lines | 89 code | 31 blank | 40 comment | 8 complexity | 54387fcfb22824f3f82d7638e1833400 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Defines the import questions form.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionbank
  21. * @copyright 2007 Jamie Pratt me@jamiep.org
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . '/formslib.php');
  26. /**
  27. * Form to import questions into the question bank.
  28. *
  29. * @copyright 2007 Jamie Pratt me@jamiep.org
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class question_import_form extends moodleform {
  33. protected function definition() {
  34. global $OUTPUT;
  35. $mform = $this->_form;
  36. $defaultcategory = $this->_customdata['defaultcategory'];
  37. $contexts = $this->_customdata['contexts'];
  38. // Choice of import format, with help icons.
  39. $mform->addElement('header', 'fileformat', get_string('fileformat', 'question'));
  40. $fileformatnames = get_import_export_formats('import');
  41. $radioarray = array();
  42. $separators = array();
  43. foreach ($fileformatnames as $shortname => $fileformatname) {
  44. $radioarray[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname);
  45. $separator = '';
  46. if (get_string_manager()->string_exists('pluginname_help', 'qformat_' . $shortname)) {
  47. $separator .= $OUTPUT->help_icon('pluginname', 'qformat_' . $shortname);
  48. }
  49. $separator .= '<div class="w-100"></div>';
  50. $separators[] = $separator;
  51. }
  52. $radioarray[] = $mform->createElement('static', 'makelasthelpiconshowup', '');
  53. $mform->addGroup($radioarray, "formatchoices", '', $separators, false);
  54. $mform->addRule("formatchoices", null, 'required', null, 'client');
  55. // Import options.
  56. $mform->addElement('header','general', get_string('general', 'form'));
  57. $mform->addElement('questioncategory', 'category', get_string('importcategory', 'question'), compact('contexts'));
  58. $mform->setDefault('category', $defaultcategory);
  59. $mform->addHelpButton('category', 'importcategory', 'question');
  60. $categorygroup = array();
  61. $categorygroup[] = $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question'));
  62. $categorygroup[] = $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question'));
  63. $mform->addGroup($categorygroup, 'categorygroup', '', '', false);
  64. $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked');
  65. $mform->setDefault('catfromfile', 1);
  66. $mform->setDefault('contextfromfile', 1);
  67. $matchgrades = array();
  68. $matchgrades['error'] = get_string('matchgradeserror', 'question');
  69. $matchgrades['nearest'] = get_string('matchgradesnearest', 'question');
  70. $mform->addElement('select', 'matchgrades', get_string('matchgrades', 'question'), $matchgrades);
  71. $mform->addHelpButton('matchgrades', 'matchgrades', 'question');
  72. $mform->setDefault('matchgrades', 'error');
  73. $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'question'));
  74. $mform->setDefault('stoponerror', 1);
  75. $mform->addHelpButton('stoponerror', 'stoponerror', 'question');
  76. // The file to import
  77. $mform->addElement('header', 'importfileupload', get_string('importquestions', 'question'));
  78. $mform->addElement('filepicker', 'newfile', get_string('import'));
  79. $mform->addRule('newfile', null, 'required', null, 'client');
  80. // Submit button.
  81. $mform->addElement('submit', 'submitbutton', get_string('import'));
  82. // Set a template for the format select elements
  83. $renderer = $mform->defaultRenderer();
  84. $template = "{help} {element}\n";
  85. $renderer->setGroupElementTemplate($template, 'format');
  86. }
  87. /**
  88. * Checks that a file has been uploaded, and that it is of a plausible type.
  89. * @param array $data the submitted data.
  90. * @param array $errors the errors so far.
  91. * @return array the updated errors.
  92. * @throws moodle_exception
  93. */
  94. protected function validate_uploaded_file($data, $errors) {
  95. if (empty($data['newfile'])) {
  96. $errors['newfile'] = get_string('required');
  97. return $errors;
  98. }
  99. $files = $this->get_draft_files('newfile');
  100. if (!is_array($files) || count($files) < 1) {
  101. $errors['newfile'] = get_string('required');
  102. return $errors;
  103. }
  104. if (empty($data['format'])) {
  105. $errors['format'] = get_string('required');
  106. return $errors;
  107. }
  108. $formatfile = 'format/' . $data['format'] . '/format.php';
  109. if (!is_readable($formatfile)) {
  110. throw new moodle_exception('formatnotfound', 'question', '', $data['format']);
  111. }
  112. require_once($formatfile);
  113. $classname = 'qformat_' . $data['format'];
  114. $qformat = new $classname();
  115. $file = reset($files);
  116. if (!$qformat->can_import_file($file)) {
  117. $a = new stdClass();
  118. $a->actualtype = $file->get_mimetype();
  119. $a->expectedtype = $qformat->mime_type();
  120. $errors['newfile'] = get_string('importwrongfiletype', 'question', $a);
  121. }
  122. return $errors;
  123. }
  124. public function validation($data, $files) {
  125. $errors = parent::validation($data, $files);
  126. $errors = $this->validate_uploaded_file($data, $errors);
  127. return $errors;
  128. }
  129. }