Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Restore instructions for the seb (Safe Exam Browser) quiz access subplugin.
  19   *
  20   * @package    quizaccess_seb
  21   * @category   backup
  22   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  23   * @copyright  2020 Catalyst IT
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  use quizaccess_seb\seb_quiz_settings;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->dirroot . '/mod/quiz/backup/moodle2/restore_mod_quiz_access_subplugin.class.php');
  32  
  33  /**
  34   * Restore instructions for the seb (Safe Exam Browser) quiz access subplugin.
  35   *
  36   * @copyright  2020 Catalyst IT
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class restore_quizaccess_seb_subplugin extends restore_mod_quiz_access_subplugin {
  40  
  41      /**
  42       * Provides path structure required to restore data for seb quiz access plugin.
  43       *
  44       * @return array
  45       */
  46      protected function define_quiz_subplugin_structure() {
  47          $paths = [];
  48  
  49          // Quiz settings.
  50          $path = $this->get_pathfor('/quizaccess_seb_quizsettings'); // Subplugin root path.
  51          $paths[] = new restore_path_element('quizaccess_seb_quizsettings', $path);
  52  
  53          // Template settings.
  54          $path = $this->get_pathfor('/quizaccess_seb_quizsettings/quizaccess_seb_template');
  55          $paths[] = new restore_path_element('quizaccess_seb_template', $path);
  56  
  57          return $paths;
  58      }
  59  
  60      /**
  61       * Process the restored data for the quizaccess_seb_quizsettings table.
  62       *
  63       * @param stdClass $data Data for quizaccess_seb_quizsettings retrieved from backup xml.
  64       */
  65      public function process_quizaccess_seb_quizsettings($data) {
  66          global $DB, $USER;
  67  
  68          // Process quizsettings.
  69          $data = (object) $data;
  70          $data->quizid = $this->get_new_parentid('quiz'); // Update quizid with new reference.
  71          $data->cmid = $this->task->get_moduleid();
  72  
  73          unset($data->id);
  74          $data->timecreated = $data->timemodified = time();
  75          $data->usermodified = $USER->id;
  76          $DB->insert_record(quizaccess_seb\seb_quiz_settings::TABLE, $data);
  77  
  78          // Process attached files.
  79          $this->add_related_files('quizaccess_seb', 'filemanager_sebconfigfile', null);
  80      }
  81  
  82      /**
  83       * Process the restored data for the quizaccess_seb_template table.
  84       *
  85       * @param stdClass $data Data for quizaccess_seb_template retrieved from backup xml.
  86       */
  87      public function process_quizaccess_seb_template($data) {
  88          global $DB;
  89  
  90          $data = (object) $data;
  91  
  92          $quizid = $this->get_new_parentid('quiz');
  93  
  94          $template = null;
  95          if ($this->task->is_samesite()) {
  96              $template = \quizaccess_seb\template::get_record(['id' => $data->id]);
  97          } else {
  98              // In a different site, try to find existing template with the same name and content.
  99              $candidates = \quizaccess_seb\template::get_records(['name' => $data->name]);
 100              foreach ($candidates as $candidate) {
 101                  if ($candidate->get('content') == $data->content) {
 102                      $template = $candidate;
 103                      break;
 104                  }
 105              }
 106          }
 107  
 108          if (empty($template)) {
 109              unset($data->id);
 110              $template = new \quizaccess_seb\template(0, $data);
 111              $template->save();
 112          }
 113  
 114          // Update the restored quiz settings to use restored template.
 115          $DB->set_field(\quizaccess_seb\seb_quiz_settings::TABLE, 'templateid', $template->get('id'), ['quizid' => $quizid]);
 116      }
 117  
 118  }
 119