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  
   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   * @package moodlecore
  20   * @subpackage backup-helper
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->dirroot.'/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
  26  
  27  /**
  28   * helper implementation of grouped_parser_processor that will
  29   * load all the categories and questions (header info only) from the questions.xml file
  30   * to the backup_ids table storing the whole structure there for later processing.
  31   * Note: only "needed" categories are loaded (must have question_categoryref record in backup_ids)
  32   * Note: parentitemid will contain the category->contextid for categories
  33   * Note: parentitemid will contain the category->id for questions
  34   *
  35   * TODO: Complete phpdocs
  36   */
  37  class restore_questions_parser_processor extends grouped_parser_processor {
  38  
  39      protected $restoreid;
  40      protected $lastcatid;
  41  
  42      public function __construct($restoreid) {
  43          $this->restoreid = $restoreid;
  44          $this->lastcatid = 0;
  45          parent::__construct(array());
  46          // Set the paths we are interested on
  47          $this->add_path('/question_categories/question_category');
  48          $this->add_path('/question_categories/question_category/questions/question');
  49      }
  50  
  51      protected function dispatch_chunk($data) {
  52          // Prepare question_category record
  53          if ($data['path'] == '/question_categories/question_category') {
  54              $info     = (object)$data['tags'];
  55              $itemname = 'question_category';
  56              $itemid   = $info->id;
  57              $parentitemid = $info->contextid;
  58              $this->lastcatid = $itemid;
  59  
  60          // Prepare question record
  61          } else if ($data['path'] == '/question_categories/question_category/questions/question') {
  62              $info = (object)$data['tags'];
  63              $itemname = 'question';
  64              $itemid   = $info->id;
  65              $parentitemid = $this->lastcatid;
  66  
  67          // Not question_category nor question, impossible. Throw exception.
  68          } else {
  69              throw new progressive_parser_exception('restore_questions_parser_processor_unexpected_path', $data['path']);
  70          }
  71  
  72          // Only load it if needed (exist same question_categoryref itemid in table)
  73          if (restore_dbops::get_backup_ids_record($this->restoreid, 'question_categoryref', $this->lastcatid)) {
  74              restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid, 0, $parentitemid, $info);
  75          }
  76      }
  77  
  78      protected function notify_path_start($path) {
  79          // nothing to do
  80      }
  81  
  82      protected function notify_path_end($path) {
  83          // nothing to do
  84      }
  85  
  86      /**
  87       * Provide NULL decoding
  88       */
  89      public function process_cdata($cdata) {
  90          if ($cdata === '$@NULL@$') {
  91              return null;
  92          }
  93          return $cdata;
  94      }
  95  }