Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Defines restore_qtype_extrafields_plugin class
  19   *
  20   * @package    core_backup
  21   * @copyright  2012 Oleg Sychev, Volgograd State Technical University
  22   * @author     Valeriy Streltsov <vostreltsov@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/engine/bank.php');
  30  
  31  /**
  32   * Class extending restore_qtype_plugin in order to use extra fields method
  33   *
  34   * See qtype_shortanswer for an example
  35   *
  36   * @copyright  2012 Oleg Sychev, Volgograd State Technical University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class restore_qtype_extrafields_plugin extends restore_qtype_plugin {
  40  
  41      /**
  42       * Question type class for a particular question type
  43       * @var question_type
  44       */
  45      protected $qtypeobj;
  46  
  47      /**
  48       * Constructor
  49       *
  50       * @param string $plugintype plugin type
  51       * @param string $pluginname plugin name
  52       * @param restore_step $step step
  53       */
  54      public function __construct($plugintype, $pluginname, $step) {
  55          parent::__construct($plugintype, $pluginname, $step);
  56          $this->qtypeobj = question_bank::get_qtype($this->pluginname);
  57      }
  58  
  59      /**
  60       * Returns the paths to be handled by the plugin at question level.
  61       */
  62      protected function define_question_plugin_structure() {
  63          $paths = array();
  64  
  65          // This qtype uses question_answers, add them.
  66          $this->add_question_question_answers($paths);
  67  
  68          // Add own qtype stuff.
  69          $elepath = $this->get_pathfor('/' . $this->qtypeobj->name());
  70          $paths[] = new restore_path_element($this->qtypeobj->name(), $elepath);
  71  
  72          $elepath = $this->get_pathfor('/answers/answer/extraanswerdata');
  73          $paths[] = new restore_path_element('extraanswerdata', $elepath);
  74  
  75          return $paths;
  76      }
  77  
  78      /**
  79       * Processes the extra answer data
  80       *
  81       * @param array $data extra answer data
  82       */
  83      public function process_extraanswerdata($data) {
  84          global $DB;
  85  
  86          $extra = $this->qtypeobj->extra_answer_fields();
  87          $tablename = array_shift($extra);
  88  
  89          $oldquestionid = $this->get_old_parentid('question');
  90          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
  91  
  92          if ($questioncreated) {
  93              $data['answerid'] = $this->get_mappingid('question_answer', $data['id']);
  94              $DB->insert_record($tablename, $data);
  95          } else {
  96              $DB->update_record($tablename, $data);
  97          }
  98      }
  99  
 100      /**
 101       * Process the qtype/... element.
 102       *
 103       * @param array $data question data
 104       */
 105      public function really_process_extra_question_fields($data) {
 106          global $DB;
 107  
 108          $oldid = $data['id'];
 109  
 110          // Detect if the question is created or mapped.
 111          $oldquestionid = $this->get_old_parentid('question');
 112          $newquestionid = $this->get_new_parentid('question');
 113          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
 114  
 115          // If the question has been created by restore, we need to create its qtype_... too.
 116          if ($questioncreated) {
 117              $extraquestionfields = $this->qtypeobj->extra_question_fields();
 118              $tablename = array_shift($extraquestionfields);
 119  
 120              // Adjust some columns.
 121              $qtfield = $this->qtypeobj->questionid_column_name();
 122              $data[$qtfield] = $newquestionid;
 123  
 124              // Insert record.
 125              $newitemid = $DB->insert_record($tablename, $data);
 126  
 127              // Create mapping.
 128              $this->set_mapping($tablename, $oldid, $newitemid);
 129          }
 130      }
 131  }