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 randomsamatch 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_randomsamatch_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          // Add own qtype stuff.
  45          $elename = 'randomsamatch';
  46          $elepath = $this->get_pathfor('/randomsamatch');
  47          $paths[] = new restore_path_element($elename, $elepath);
  48  
  49          return $paths; // And we return the interesting paths.
  50      }
  51  
  52      /**
  53       * Process the qtype/randomsamatch element
  54       */
  55      public function process_randomsamatch($data) {
  56          global $DB;
  57  
  58          $data = (object)$data;
  59          $oldid = $data->id;
  60  
  61          // Detect if the question is created or mapped.
  62          $oldquestionid   = $this->get_old_parentid('question');
  63          $newquestionid   = $this->get_new_parentid('question');
  64          $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
  65  
  66          // If the question has been created by restore, we need to create its
  67          // qtype_randomsamatch_options too.
  68          if ($questioncreated) {
  69              // Fill in some field that were added in 2.1, and so which may be missing
  70              // from backups made in older versions of Moodle.
  71              if (!isset($data->subcats)) {
  72                  $data->subcats = 1;
  73              }
  74              if (!isset($data->correctfeedback)) {
  75                  $data->correctfeedback = '';
  76                  $data->correctfeedbackformat = FORMAT_HTML;
  77              }
  78              if (!isset($data->partiallycorrectfeedback)) {
  79                  $data->partiallycorrectfeedback = '';
  80                  $data->partiallycorrectfeedbackformat = FORMAT_HTML;
  81              }
  82              if (!isset($data->incorrectfeedback)) {
  83                  $data->incorrectfeedback = '';
  84                  $data->incorrectfeedbackformat = FORMAT_HTML;
  85              }
  86              if (!isset($data->shownumcorrect)) {
  87                  $data->shownumcorrect = 0;
  88              }
  89              $data->questionid = $newquestionid;
  90  
  91              // It is possible for old backup files to contain unique key violations.
  92              // We need to check to avoid that.
  93              if (!$DB->record_exists('qtype_randomsamatch_options', array('questionid' => $data->questionid))) {
  94                  $newitemid = $DB->insert_record('qtype_randomsamatch_options', $data);
  95                  $this->set_mapping('qtype_randomsamatch_options', $oldid, $newitemid);
  96              }
  97          }
  98      }
  99  
 100      /**
 101       * Given one question_states record, return the answer
 102       * recoded pointing to all the restored stuff for randomsamatch questions
 103       *
 104       * answer is one comma separated list of hypen separated pairs
 105       * containing question->id and question_answers->id
 106       */
 107      public function recode_legacy_state_answer($state) {
 108          $answer = $state->answer;
 109          $resultarr = array();
 110          foreach (explode(',', $answer) as $pair) {
 111              $pairarr = explode('-', $pair);
 112              $questionid = $pairarr[0];
 113              $answerid = $pairarr[1];
 114              $newquestionid = $questionid ? $this->get_mappingid('question', $questionid) : 0;
 115              $newanswerid = $answerid ? $this->get_mappingid('question_answer', $answerid) : 0;
 116              $resultarr[] = implode('-', array($newquestionid, $newanswerid));
 117          }
 118          return implode(',', $resultarr);
 119      }
 120  
 121      /**
 122       * Return the contents of this qtype to be processed by the links decoder.
 123       */
 124      public static function define_decode_contents() {
 125  
 126          $contents = array();
 127  
 128          $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
 129          $contents[] = new restore_decode_content('qtype_randomsamatch_options', $fields, 'qtype_randomsamatch_options');
 130  
 131          return $contents;
 132      }
 133  }