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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Imports lesson pages
  20   *
  21   * @package mod_lesson
  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  require_once("../../config.php");
  27  require_once($CFG->libdir.'/questionlib.php');
  28  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  29  require_once($CFG->dirroot.'/mod/lesson/import_form.php');
  30  require_once($CFG->dirroot.'/mod/lesson/format.php');  // Parent class
  31  
  32  $id     = required_param('id', PARAM_INT);         // Course Module ID
  33  $pageid = optional_param('pageid', '', PARAM_INT); // Page ID
  34  
  35  $PAGE->set_url('/mod/lesson/import.php', array('id'=>$id, 'pageid'=>$pageid));
  36  
  37  $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
  38  $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  39  $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
  40  
  41  require_login($course, false, $cm);
  42  $context = context_module::instance($cm->id);
  43  require_capability('mod/lesson:edit', $context);
  44  
  45  $strimportquestions = get_string("importquestions", "lesson");
  46  $strlessons = get_string("modulenameplural", "lesson");
  47  
  48  $manager = lesson_page_type_manager::get($lesson);
  49  
  50  $data = new stdClass;
  51  $data->id = $PAGE->cm->id;
  52  $data->pageid = $pageid;
  53  
  54  $mform = new lesson_import_form(null, array('formats'=>lesson_get_import_export_formats('import')));
  55  $mform->set_data($data);
  56  
  57      $PAGE->navbar->add($strimportquestions);
  58      $PAGE->set_title($strimportquestions);
  59      $PAGE->set_heading($course->fullname);
  60      $PAGE->activityheader->set_attrs([
  61          'hidecompletion' => true,
  62          'description' => ''
  63      ]);
  64      $PAGE->add_body_class('limitedwidth');
  65      echo $OUTPUT->header();
  66      $headinglevel = $PAGE->activityheader->get_heading_level();
  67      echo $OUTPUT->heading_with_help($strimportquestions, 'importquestions', 'lesson', '', '', $headinglevel);
  68  
  69  if ($data = $mform->get_data()) {
  70  
  71      require_sesskey();
  72  
  73      $realfilename = $mform->get_new_filename('questionfile');
  74      $importfile = make_request_directory() . "/{$realfilename}";
  75      if (!$result = $mform->save_file('questionfile', $importfile, true)) {
  76          throw new moodle_exception('uploadproblem');
  77      }
  78  
  79      $formatclass = 'qformat_'.$data->format;
  80      $formatclassfile = $CFG->dirroot.'/question/format/'.$data->format.'/format.php';
  81      if (!is_readable($formatclassfile)) {
  82          throw new \moodle_exception('unknowformat', '', '', $data->format);
  83      }
  84      require_once($formatclassfile);
  85      $format = new $formatclass();
  86  
  87      $format->set_importcontext($context);
  88  
  89      // Do anything before that we need to
  90      if (! $format->importpreprocess()) {
  91                  throw new \moodle_exception('preprocesserror', 'lesson');
  92              }
  93  
  94      // Process the uploaded file
  95      if (! $format->importprocess($importfile, $lesson, $pageid)) {
  96                  throw new \moodle_exception('processerror', 'lesson');
  97              }
  98  
  99      // In case anything needs to be done after
 100      if (! $format->importpostprocess()) {
 101                  throw new \moodle_exception('postprocesserror', 'lesson');
 102              }
 103  
 104              echo "<hr>";
 105      echo $OUTPUT->continue_button('view.php?id='.$PAGE->cm->id);
 106  
 107  } else {
 108  
 109      // Print upload form
 110      $mform->display();
 111  }
 112  
 113  echo $OUTPUT->footer();