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   * @package    moodlecore
  19   * @subpackage backup-moodle2
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * restore plugin class that provides the necessary information
  30   * needed to restore one calculated qtype plugin
  31   *
  32   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class restore_qtype_calculated_plugin extends restore_qtype_plugin {
  36  
  37      /**
  38       * Returns the paths to be handled by the plugin at question level
  39       */
  40      protected function define_question_plugin_structure() {
  41  
  42          $paths = array();
  43  
  44          // This qtype uses question_answers, add them.
  45          $this->add_question_question_answers($paths);
  46  
  47          // This qtype uses question_numerical_options and question_numerical_units, add them.
  48          $this->add_question_numerical_options($paths);
  49          $this->add_question_numerical_units($paths);
  50  
  51          // This qtype uses question datasets, add them.
  52          $this->add_question_datasets($paths);
  53  
  54          // Add own qtype stuff.
  55          $elename = 'calculated_record';
  56          $elepath = $this->get_pathfor('/calculated_records/calculated_record');
  57          $paths[] = new restore_path_element($elename, $elepath);
  58  
  59          $elename = 'calculated_option';
  60          $elepath = $this->get_pathfor('/calculated_options/calculated_option');
  61          $paths[] = new restore_path_element($elename, $elepath);
  62  
  63          return $paths; // And we return the interesting paths.
  64      }
  65  
  66      /**
  67       * Process the qtype/calculated_record element
  68       */
  69      public function process_calculated_record($data) {
  70          global $DB;
  71  
  72          $data = (object)$data;
  73          $oldid = $data->id;
  74  
  75          // Detect if the question is created or mapped.
  76          $oldquestionid   = $this->get_old_parentid('question');
  77          $newquestionid   = $this->get_new_parentid('question');
  78          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
  79                  true : false;
  80  
  81          // If the question has been created by restore, we need to create its
  82          // question_calculated too.
  83          if ($questioncreated) {
  84              // Adjust some columns.
  85              $data->question = $newquestionid;
  86              $data->answer = $this->get_mappingid('question_answer', $data->answer);
  87              // Insert record.
  88              $newitemid = $DB->insert_record('question_calculated', $data);
  89          }
  90      }
  91  
  92      /**
  93       * Process the qtype/calculated_option element
  94       */
  95      public function process_calculated_option($data) {
  96          global $DB;
  97  
  98          $data = (object)$data;
  99          $oldid = $data->id;
 100  
 101          // Detect if the question is created or mapped.
 102          $oldquestionid   = $this->get_old_parentid('question');
 103          $newquestionid   = $this->get_new_parentid('question');
 104          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ?
 105                  true : false;
 106  
 107          // If the question has been created by restore, we need to create its
 108          // question_calculated too.
 109          if ($questioncreated) {
 110              // Adjust some columns.
 111              $data->question = $newquestionid;
 112              // Insert record.
 113              $newitemid = $DB->insert_record('question_calculated_options', $data);
 114          }
 115      }
 116  }