Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 the restore_enrol_lti_plugin class.
  19   *
  20   * @package   enrol_lti
  21   * @copyright 2016 Mark Nelson <markn@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Define all the restore steps.
  29   *
  30   * @package   enrol_lti
  31   * @copyright 2016 Mark Nelson <markn@moodle.com>
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class restore_enrol_lti_plugin extends restore_enrol_plugin {
  35  
  36      /**
  37       * @var array $tools Stores the IDs of the newly created tools.
  38       */
  39      protected $tools = array();
  40  
  41      /**
  42       * Declares the enrol LTI XML paths attached to the enrol element
  43       *
  44       * @return array of {@link restore_path_element}
  45       */
  46      protected function define_enrol_plugin_structure() {
  47  
  48          $paths = array();
  49          $paths[] = new restore_path_element('enrol_lti_tool', $this->connectionpoint->get_path() . '/tool');
  50          $paths[] = new restore_path_element('enrol_lti_users', $this->connectionpoint->get_path() . '/tool/users/user');
  51  
  52          return $paths;
  53      }
  54  
  55      /**
  56       * Processes LTI tools element data
  57       *
  58       * @param array|stdClass $data
  59       */
  60      public function process_enrol_lti_tool($data) {
  61          global $DB;
  62  
  63          $data = (object) $data;
  64  
  65          // Store the old id.
  66          $oldid = $data->id;
  67  
  68          // Change the values before we insert it.
  69          $data->timecreated = time();
  70          $data->timemodified = $data->timecreated;
  71  
  72          // Set the correct legacy ltiversion when restoring old tools.
  73          if (empty($data->ltiversion)) {
  74              $data->ltiversion = 'LTI-1p0/LTI-2p0';
  75          }
  76  
  77          // Generate a new uuid for LTI Advantage restores.
  78          if ($data->ltiversion == 'LTI-1p3') {
  79              $data->uuid = \core\uuid::generate();
  80          }
  81  
  82          // Now we can insert the new record.
  83          $data->id = $DB->insert_record('enrol_lti_tools', $data);
  84  
  85          // Add the array of tools we need to process later.
  86          $this->tools[$data->id] = $data;
  87  
  88          // Set up the mapping.
  89          $this->set_mapping('enrol_lti_tool', $oldid, $data->id);
  90      }
  91  
  92      /**
  93       * Processes LTI users element data
  94       *
  95       * @param array|stdClass $data The data to insert as a comment
  96       */
  97      public function process_enrol_lti_users($data) {
  98          global $DB;
  99  
 100          $data = (object) $data;
 101  
 102          $data->userid = $this->get_mappingid('user', $data->userid);
 103          $data->toolid = $this->get_mappingid('enrol_lti_tool', $data->toolid);
 104          $data->timecreated = time();
 105  
 106          $DB->insert_record('enrol_lti_users', $data);
 107      }
 108  
 109      /**
 110       * This function is executed after all the tasks in the plan have been finished.
 111       * This must be done here because the activities have not been restored yet.
 112       */
 113      public function after_restore_enrol() {
 114          global $DB;
 115  
 116          // Need to go through and change the values.
 117          foreach ($this->tools as $tool) {
 118              $updatetool = new stdClass();
 119              $updatetool->id = $tool->id;
 120              $updatetool->enrolid = $this->get_mappingid('enrol', $tool->enrolid);
 121              $updatetool->contextid = $this->get_mappingid('context', $tool->contextid);
 122              $DB->update_record('enrol_lti_tools', $updatetool);
 123          }
 124      }
 125  }