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.
   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 truefalse 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_truefalse_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          // Add own qtype stuff.
  48          $elename = 'truefalse';
  49          $elepath = $this->get_pathfor('/truefalse'); // We used get_recommended_name() so this works.
  50          $paths[] = new restore_path_element($elename, $elepath);
  51  
  52          return $paths; // And we return the interesting paths.
  53      }
  54  
  55      /**
  56       * Process the qtype/truefalse element
  57       */
  58      public function process_truefalse($data) {
  59          global $DB;
  60  
  61          $data = (object)$data;
  62          $oldid = $data->id;
  63  
  64          // Detect if the question is created or mapped.
  65          $oldquestionid   = $this->get_old_parentid('question');
  66          $newquestionid   = $this->get_new_parentid('question');
  67          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
  68  
  69          // If the question has been created by restore, we need to create its question_truefalse too.
  70          if ($questioncreated) {
  71              // Adjust some columns.
  72              $data->question = $newquestionid;
  73              $data->trueanswer = $this->get_mappingid('question_answer', $data->trueanswer);
  74              $data->falseanswer = $this->get_mappingid('question_answer', $data->falseanswer);
  75              // Insert record.
  76              $newitemid = $DB->insert_record('question_truefalse', $data);
  77              // Create mapping.
  78              $this->set_mapping('question_truefalse', $oldid, $newitemid);
  79          }
  80      }
  81  
  82      /**
  83       * Given one question_states record, return the answer
  84       * recoded pointing to all the restored stuff for truefalse questions
  85       *
  86       * if not empty, answer is one question_answers->id
  87       */
  88      public function recode_legacy_state_answer($state) {
  89          $answer = $state->answer;
  90          $result = '';
  91          if ($answer) {
  92              $result = $this->get_mappingid('question_answer', $answer);
  93          }
  94          return $result;
  95      }
  96  }