Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Script for importing questions into the question bank.
  19   *
  20   * @package    qbank_exportquestions
  21   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->dirroot . '/question/editlib.php');
  27  require_once($CFG->dirroot . '/question/format.php');
  28  require_once($CFG->dirroot . '/question/renderer.php');
  29  
  30  use qbank_exportquestions\exportquestions_helper;
  31  use qbank_exportquestions\form\export_form;
  32  
  33  require_login();
  34  core_question\local\bank\helper::require_plugin_enabled('qbank_exportquestions');
  35  
  36  list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
  37          question_edit_setup('export', '/question/bank/exportquestions/export.php');
  38  
  39  // Get display strings.
  40  $strexportquestions = get_string('exportquestions', 'question');
  41  
  42  list($catid, $catcontext) = explode(',', $pagevars['cat']);
  43  $category = $DB->get_record('question_categories', ["id" => $catid, 'contextid' => $catcontext], '*', MUST_EXIST);
  44  
  45  // Header.
  46  $PAGE->set_url($thispageurl);
  47  $PAGE->set_title($strexportquestions);
  48  $PAGE->set_heading($COURSE->fullname);
  49  $PAGE->activityheader->disable();
  50  
  51  echo $OUTPUT->header();
  52  
  53  // Print horizontal nav if needed.
  54  $renderer = $PAGE->get_renderer('core_question', 'bank');
  55  
  56  $qbankaction = new \core_question\output\qbank_action_menu($thispageurl);
  57  echo $renderer->render($qbankaction);
  58  
  59  $exportform = new export_form($thispageurl,
  60          ['contexts' => $contexts->having_one_edit_tab_cap('export'), 'defaultcategory' => $pagevars['cat']]);
  61  
  62  if ($fromform = $exportform->get_data()) {
  63      $thiscontext = $contexts->lowest();
  64      if (!is_readable($CFG->dirroot . "/question/format/{$fromform->format}/format.php")) {
  65          throw new moodle_exception('unknowformat', '', '', $fromform->format);
  66      }
  67      $withcategories = 'nocategories';
  68      if (!empty($fromform->cattofile)) {
  69          $withcategories = 'withcategories';
  70      }
  71      $withcontexts = 'nocontexts';
  72      if (!empty($fromform->contexttofile)) {
  73          $withcontexts = 'withcontexts';
  74      }
  75  
  76      $classname = 'qformat_' . $fromform->format;
  77      $qformat = new $classname();
  78      $filename = question_default_export_filename($COURSE, $category) .
  79              $qformat->export_file_extension();
  80      $exporturl = exportquestions_helper::question_make_export_url($thiscontext->id, $category->id,
  81              $fromform->format, $withcategories, $withcontexts, $filename);
  82  
  83      echo $OUTPUT->box_start();
  84      echo get_string('yourfileshoulddownload', 'question', $exporturl->out());
  85      echo $OUTPUT->box_end();
  86  
  87      // Log the export of these questions.
  88      $eventparams = [
  89              'contextid' => $category->contextid,
  90              'other' => ['format' => $fromform->format, 'categoryid' => $category->id],
  91      ];
  92      $event = \core\event\questions_exported::create($eventparams);
  93      $event->trigger();
  94  
  95      // Don't allow force download for behat site, as pop-up can't be handled by selenium.
  96      if (!defined('BEHAT_SITE_RUNNING')) {
  97          $PAGE->requires->js_function_call('document.location.replace', [$exporturl->out(false)], false, 1);
  98      }
  99  
 100      echo $OUTPUT->continue_button(new moodle_url($PAGE->settingsnav->find(
 101                                              'questionbank',
 102                                              \navigation_node::TYPE_CONTAINER)->action, $thispageurl->params()));
 103      echo $OUTPUT->footer();
 104      exit;
 105  }
 106  
 107  // Display export form.
 108  echo $OUTPUT->heading_with_help($strexportquestions, 'exportquestions', 'question');
 109  
 110  $exportform->display();
 111  
 112  echo $OUTPUT->footer();