Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * Script to download the export of a single question.
  19   *
  20   * @package    qbank_exporttoxml
  21   * @copyright  2015 the Open University
  22   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once(__DIR__ . '/../../../config.php');
  27  require_once($CFG->libdir . '/questionlib.php');
  28  require_once($CFG->dirroot . '/question/format/xml/format.php');
  29  
  30  // Get the parameters from the URL.
  31  $questionid = required_param('id', PARAM_INT);
  32  $cmid = optional_param('cmid', 0, PARAM_INT);
  33  $courseid = optional_param('courseid', 0, PARAM_INT);
  34  $urlparams = ['id' => $questionid, 'sesskey' => sesskey()];
  35  
  36  \core_question\local\bank\helper::require_plugin_enabled('qbank_exporttoxml');
  37  
  38  if ($cmid) {
  39      $cm = get_coursemodule_from_id(null, $cmid);
  40      require_login($cm->course, false, $cm);
  41      $thiscontext = context_module::instance($cmid);
  42      $urlparams['cmid'] = $cmid;
  43  } else if ($courseid) {
  44      require_login($courseid, false);
  45      $thiscontext = context_course::instance($courseid);
  46      $urlparams['courseid'] = $courseid;
  47  } else {
  48      throw new moodle_exception('missingcourseorcmid', 'question');
  49  }
  50  require_sesskey();
  51  
  52  // Load the necessary data.
  53  $contexts = new core_question\local\bank\question_edit_contexts($thiscontext);
  54  $questiondata = question_bank::load_question_data($questionid);
  55  
  56  // Check permissions.
  57  question_require_capability_on($questiondata, 'view');
  58  
  59  // Initialise $PAGE. Nothing is output, so this does not really matter. Just avoids notices.
  60  $nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams);
  61  $PAGE->set_url('/question/bank/exporttoxml/exportone.php', $urlparams);
  62  $PAGE->set_heading($COURSE->fullname);
  63  $PAGE->set_pagelayout('admin');
  64  
  65  // Set up the export format.
  66  $qformat = new qformat_xml();
  67  $filename = question_default_export_filename($COURSE, $questiondata) .
  68          $qformat->export_file_extension();
  69  $qformat->setContexts($contexts->having_one_edit_tab_cap('export'));
  70  $qformat->setCourse($COURSE);
  71  $qformat->setQuestions([$questiondata]);
  72  $qformat->setCattofile(false);
  73  $qformat->setContexttofile(false);
  74  
  75  // Do the export.
  76  if (!$qformat->exportpreprocess()) {
  77      send_file_not_found();
  78  }
  79  if (!$content = $qformat->exportprocess(true)) {
  80      send_file_not_found();
  81  }
  82  send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());