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.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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   * Provides support for the conversion of moodle1 backup to the moodle2 format
  19   * Based off of a template @ http://docs.moodle.org/dev/Backup_1.9_conversion_for_developers
  20   *
  21   * @package    mod_quiz
  22   * @copyright  2011 Aparup Banerjee <aparup@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Quiz conversion handler
  30   */
  31  class moodle1_mod_quiz_handler extends moodle1_mod_handler {
  32  
  33      /** @var moodle1_file_manager */
  34      protected $fileman = null;
  35  
  36      /** @var int cmid */
  37      protected $moduleid = null;
  38  
  39      /**
  40       * Declare the paths in moodle.xml we are able to convert
  41       *
  42       * The method returns list of {@link convert_path} instances.
  43       * For each path returned, the corresponding conversion method must be
  44       * defined.
  45       *
  46       * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ does not
  47       * actually exist in the file. The last element with the module name was
  48       * appended by the moodle1_converter class.
  49       *
  50       * @return array of {@link convert_path} instances
  51       */
  52      public function get_paths() {
  53          return array(
  54              new convert_path(
  55                  'quiz', '/MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ',
  56                  array(
  57                      'newfields' => array(
  58                          'showuserpicture'       => 0,
  59                          'questiondecimalpoints' => -1,
  60                          'introformat'           => 0,
  61                          'showblocks'            => 0,
  62                      ),
  63                  )
  64              ),
  65              new convert_path('quiz_question_instances',
  66                      '/MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ/QUESTION_INSTANCES'),
  67              new convert_path('quiz_question_instance',
  68                      '/MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ/QUESTION_INSTANCES/QUESTION_INSTANCE',
  69                  array(
  70                      'renamefields' => array(
  71                          'question' => 'questionid',
  72                          'grade'    => 'maxmark',
  73                      ),
  74                  )
  75              ),
  76              new convert_path('quiz_feedbacks',
  77                      '/MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ/FEEDBACKS'),
  78              new convert_path('quiz_feedback',
  79                      '/MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ/FEEDBACKS/FEEDBACK',
  80                  array(
  81                      'newfields' => array(
  82                          'feedbacktextformat' => FORMAT_HTML,
  83                      )
  84                  )
  85              )
  86          );
  87      }
  88  
  89      /**
  90       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/QUIZ
  91       * data available
  92       */
  93      public function process_quiz($data) {
  94          global $CFG;
  95  
  96          // Replay the upgrade step 2008081501.
  97          if (is_null($data['sumgrades'])) {
  98              $data['sumgrades'] = 0;
  99              // TODO for user data: quiz_attempts SET sumgrades=0 WHERE sumgrades IS NULL.
 100              // TODO for user data: quiz_grades.grade should be not be null , convert to default 0.
 101          }
 102  
 103          // Replay the upgrade step 2009042000.
 104          if ($CFG->texteditors !== 'textarea') {
 105              $data['intro']       = text_to_html($data['intro'], false, false, true);
 106              $data['introformat'] = FORMAT_HTML;
 107          }
 108  
 109          // Replay the upgrade step 2009031001.
 110          $data['timelimit'] *= 60;
 111  
 112          // Get the course module id and context id.
 113          $instanceid     = $data['id'];
 114          $cminfo         = $this->get_cminfo($instanceid);
 115          $this->moduleid = $cminfo['id'];
 116          $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
 117  
 118          // Get a fresh new file manager for this instance.
 119          $this->fileman = $this->converter->get_file_manager($contextid, 'mod_quiz');
 120  
 121          // Convert course files embedded into the intro.
 122          $this->fileman->filearea = 'intro';
 123          $this->fileman->itemid   = 0;
 124          $data['intro'] = moodle1_converter::migrate_referenced_files(
 125                  $data['intro'], $this->fileman);
 126  
 127          // Start writing quiz.xml.
 128          $this->open_xml_writer("activities/quiz_{$this->moduleid}/quiz.xml");
 129          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid,
 130                  'moduleid' => $this->moduleid, 'modulename' => 'quiz',
 131                  'contextid' => $contextid));
 132          $this->xmlwriter->begin_tag('quiz', array('id' => $instanceid));
 133  
 134          foreach ($data as $field => $value) {
 135              if ($field <> 'id') {
 136                  $this->xmlwriter->full_tag($field, $value);
 137              }
 138          }
 139  
 140          return $data;
 141      }
 142  
 143      public function on_quiz_question_instances_start() {
 144          $this->xmlwriter->begin_tag('question_instances');
 145      }
 146  
 147      public function on_quiz_question_instances_end() {
 148          $this->xmlwriter->end_tag('question_instances');
 149      }
 150  
 151      public function process_quiz_question_instance($data) {
 152          $this->write_xml('question_instance', $data, array('/question_instance/id'));
 153      }
 154  
 155      public function on_quiz_feedbacks_start() {
 156          $this->xmlwriter->begin_tag('feedbacks');
 157      }
 158  
 159      public function on_quiz_feedbacks_end() {
 160          $this->xmlwriter->end_tag('feedbacks');
 161      }
 162  
 163      public function process_quiz_feedback($data) {
 164          // Replay the upgrade step 2010122302.
 165          if (is_null($data['mingrade'])) {
 166              $data['mingrade'] = 0;
 167          }
 168          if (is_null($data['maxgrade'])) {
 169              $data['maxgrade'] = 0;
 170          }
 171  
 172          $this->write_xml('feedback', $data, array('/feedback/id'));
 173      }
 174  
 175      /**
 176       * This is executed when we reach the closing </MOD> tag of our 'quiz' path
 177       */
 178      public function on_quiz_end() {
 179  
 180          // Append empty <overrides> subpath element.
 181          $this->write_xml('overrides', array());
 182  
 183          // Finish writing quiz.xml.
 184          $this->xmlwriter->end_tag('quiz');
 185          $this->xmlwriter->end_tag('activity');
 186          $this->close_xml_writer();
 187  
 188          // Write inforef.xml.
 189          $this->open_xml_writer("activities/quiz_{$this->moduleid}/inforef.xml");
 190          $this->xmlwriter->begin_tag('inforef');
 191          $this->xmlwriter->begin_tag('fileref');
 192          foreach ($this->fileman->get_fileids() as $fileid) {
 193              $this->write_xml('file', array('id' => $fileid));
 194          }
 195          $this->xmlwriter->end_tag('fileref');
 196          $this->xmlwriter->end_tag('inforef');
 197          $this->close_xml_writer();
 198      }
 199  }