Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  /**
  18   * Defines the import questions form.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionbank
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  require_once(__DIR__ . '/../config.php');
  28  require_once($CFG->dirroot . '/question/editlib.php');
  29  require_once($CFG->dirroot . '/question/import_form.php');
  30  require_once($CFG->dirroot . '/question/format.php');
  31  
  32  list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
  33          question_edit_setup('import', '/question/import.php');
  34  
  35  // get display strings
  36  $txt = new stdClass();
  37  $txt->importerror = get_string('importerror', 'question');
  38  $txt->importquestions = get_string('importquestions', 'question');
  39  
  40  list($catid, $catcontext) = explode(',', $pagevars['cat']);
  41  if (!$category = $DB->get_record("question_categories", array('id' => $catid))) {
  42      print_error('nocategory', 'question');
  43  }
  44  
  45  $categorycontext = context::instance_by_id($category->contextid);
  46  $category->context = $categorycontext;
  47  //this page can be called without courseid or cmid in which case
  48  //we get the context from the category object.
  49  if ($contexts === null) { // need to get the course from the chosen category
  50      $contexts = new question_edit_contexts($categorycontext);
  51      $thiscontext = $contexts->lowest();
  52      if ($thiscontext->contextlevel == CONTEXT_COURSE){
  53          require_login($thiscontext->instanceid, false);
  54      } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
  55          list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
  56          require_login($cm->course, false, $cm);
  57      }
  58      $contexts->require_one_edit_tab_cap($edittab);
  59  }
  60  
  61  $PAGE->set_url($thispageurl);
  62  
  63  $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
  64                                                      'defaultcategory'=>$pagevars['cat']));
  65  
  66  if ($import_form->is_cancelled()){
  67      redirect($thispageurl);
  68  }
  69  //==========
  70  // PAGE HEADER
  71  //==========
  72  $PAGE->set_title($txt->importquestions);
  73  $PAGE->set_heading($COURSE->fullname);
  74  echo $OUTPUT->header();
  75  
  76  // Print horizontal nav if needed.
  77  $renderer = $PAGE->get_renderer('core_question', 'bank');
  78  echo $renderer->extra_horizontal_navigation();
  79  
  80  // file upload form sumitted
  81  if ($form = $import_form->get_data()) {
  82  
  83      // file checks out ok
  84      $fileisgood = false;
  85  
  86      // work out if this is an uploaded file
  87      // or one from the filesarea.
  88      $realfilename = $import_form->get_new_filename('newfile');
  89      $importfile = make_request_directory() . "/{$realfilename}";
  90      if (!$result = $import_form->save_file('newfile', $importfile, true)) {
  91          throw new moodle_exception('uploadproblem');
  92      }
  93  
  94      $formatfile = 'format/' . $form->format . '/format.php';
  95      if (!is_readable($formatfile)) {
  96          throw new moodle_exception('formatnotfound', 'question', '', $form->format);
  97      }
  98  
  99      require_once($formatfile);
 100  
 101      $classname = 'qformat_' . $form->format;
 102      $qformat = new $classname();
 103  
 104      // load data into class
 105      $qformat->setCategory($category);
 106      $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
 107      $qformat->setCourse($COURSE);
 108      $qformat->setFilename($importfile);
 109      $qformat->setRealfilename($realfilename);
 110      $qformat->setMatchgrades($form->matchgrades);
 111      $qformat->setCatfromfile(!empty($form->catfromfile));
 112      $qformat->setContextfromfile(!empty($form->contextfromfile));
 113      $qformat->setStoponerror($form->stoponerror);
 114  
 115      // Do anything before that we need to
 116      if (!$qformat->importpreprocess()) {
 117          print_error('cannotimport', '', $thispageurl->out());
 118      }
 119  
 120      // Process the uploaded file
 121      if (!$qformat->importprocess()) {
 122          print_error('cannotimport', '', $thispageurl->out());
 123      }
 124  
 125      // In case anything needs to be done after
 126      if (!$qformat->importpostprocess()) {
 127          print_error('cannotimport', '', $thispageurl->out());
 128      }
 129  
 130      // Log the import into this category.
 131      $eventparams = [
 132              'contextid' => $qformat->category->contextid,
 133              'other' => ['format' => $form->format, 'categoryid' => $qformat->category->id],
 134      ];
 135      $event = \core\event\questions_imported::create($eventparams);
 136      $event->trigger();
 137  
 138      $params = $thispageurl->params() + array(
 139          'category' => $qformat->category->id . ',' . $qformat->category->contextid);
 140      echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
 141      echo $OUTPUT->footer();
 142      exit;
 143  }
 144  
 145  echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
 146  
 147  /// Print upload form
 148  $import_form->display();
 149  echo $OUTPUT->footer();