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] [Versions 401 and 403]

   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  // This file is part of BasicLTI4Moodle
  18  //
  19  // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
  20  // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
  21  // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
  22  // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
  23  // are already supporting or going to support BasicLTI. This project Implements the consumer
  24  // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
  25  // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
  26  // at the GESSI research group at UPC.
  27  // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
  28  // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
  29  // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
  30  //
  31  // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
  32  // of the Universitat Politecnica de Catalunya http://www.upc.edu
  33  // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
  34  
  35  /**
  36   * This file contains all the restore steps that will be used
  37   * by the restore_lti_activity_task
  38   *
  39   * @package mod_lti
  40   * @copyright  2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
  41   *  marc.alier@upc.edu
  42   * @copyright  2009 Universitat Politecnica de Catalunya http://www.upc.edu
  43   * @author     Marc Alier
  44   * @author     Jordi Piguillem
  45   * @author     Nikolas Galanis
  46   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   */
  48  
  49  defined('MOODLE_INTERNAL') || die;
  50  
  51  use mod_lti\local\ltiopenid\registration_helper;
  52  
  53  /**
  54   * Structure step to restore one lti activity
  55   */
  56  class restore_lti_activity_structure_step extends restore_activity_structure_step {
  57  
  58      /** @var bool */
  59      protected $newltitype = false;
  60  
  61      protected function define_structure() {
  62  
  63          $paths = array();
  64          // To know if we are including userinfo.
  65          $userinfo = $this->get_setting_value('userinfo');
  66  
  67          $lti = new restore_path_element('lti', '/activity/lti');
  68          $paths[] = $lti;
  69          $paths[] = new restore_path_element('ltitype', '/activity/lti/ltitype');
  70          $paths[] = new restore_path_element('ltitypesconfig', '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfig');
  71          $paths[] = new restore_path_element('ltitypesconfigencrypted',
  72              '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfigencrypted');
  73          $paths[] = new restore_path_element('ltitoolproxy', '/activity/lti/ltitype/ltitoolproxy');
  74          $paths[] = new restore_path_element('ltitoolsetting', '/activity/lti/ltitype/ltitoolproxy/ltitoolsettings/ltitoolsetting');
  75  
  76          if ($userinfo) {
  77              $submission = new restore_path_element('ltisubmission', '/activity/lti/ltisubmissions/ltisubmission');
  78              $paths[] = $submission;
  79          }
  80  
  81          // Add support for subplugin structures.
  82          $this->add_subplugin_structure('ltisource', $lti);
  83          $this->add_subplugin_structure('ltiservice', $lti);
  84  
  85          // Return the paths wrapped into standard activity structure.
  86          return $this->prepare_activity_structure($paths);
  87      }
  88  
  89      protected function process_lti($data) {
  90          global $DB;
  91  
  92          $data = (object)$data;
  93          $oldid = $data->id;
  94          $data->course = $this->get_courseid();
  95          $data->servicesalt = uniqid('', true);
  96  
  97          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  98          // See MDL-9367.
  99  
 100           // Grade used to be a float (whole numbers only), restore as int.
 101          $data->grade = (int) $data->grade;
 102  
 103          $data->typeid = 0;
 104  
 105          // Try to decrypt resourcekey and password. Null if not possible (DB default).
 106          // Note these fields were originally encrypted on backup using {link @encrypted_final_element}.
 107          $data->resourcekey = isset($data->resourcekey) ? $this->decrypt($data->resourcekey) : null;
 108          $data->password = isset($data->password) ? $this->decrypt($data->password) : null;
 109  
 110          $newitemid = $DB->insert_record('lti', $data);
 111  
 112          // Immediately after inserting "activity" record, call this.
 113          $this->apply_activity_instance($newitemid);
 114      }
 115  
 116      /**
 117       * Process an lti type restore
 118       * @param mixed $data The data from backup XML file
 119       * @return void
 120       */
 121      protected function process_ltitype($data) {
 122          global $DB, $USER;
 123  
 124          $data = (object)$data;
 125          $oldid = $data->id;
 126          if (!empty($data->createdby)) {
 127              $data->createdby = $this->get_mappingid('user', $data->createdby) ?: $USER->id;
 128          }
 129  
 130          $courseid = $this->get_courseid();
 131          $data->course = ($this->get_mappingid('course', $data->course) == $courseid) ? $courseid : SITEID;
 132  
 133          // Try to find existing lti type with the same properties.
 134          $ltitypeid = $this->find_existing_lti_type($data);
 135  
 136          $this->newltitype = false;
 137          if (!$ltitypeid && $data->course == $courseid) {
 138              unset($data->toolproxyid); // Course tools can not use LTI2.
 139              if (!empty($data->clientid)) {
 140                  // Need to rebuild clientid to ensure uniqueness.
 141                  $data->clientid = registration_helper::get()->new_clientid();
 142              }
 143              $ltitypeid = $DB->insert_record('lti_types', $data);
 144              $this->newltitype = true;
 145              $this->set_mapping('ltitype', $oldid, $ltitypeid);
 146          }
 147  
 148          // Add the typeid entry back to LTI module.
 149          $DB->update_record('lti', ['id' => $this->get_new_parentid('lti'), 'typeid' => $ltitypeid]);
 150      }
 151  
 152      /**
 153       * Attempts to find existing record in lti_type
 154       * @param stdClass $data
 155       * @return int|null field lti_types.id or null if tool is not found
 156       */
 157      protected function find_existing_lti_type($data) {
 158          global $DB;
 159          if ($ltitypeid = $this->get_mappingid('ltitype', $data->id)) {
 160              return $ltitypeid;
 161          }
 162  
 163          $ltitype = null;
 164          $params = (array)$data;
 165          if ($this->task->is_samesite()) {
 166              // If we are restoring on the same site try to find lti type with the same id.
 167              $sql = 'id = :id AND course = :course';
 168              $sql .= ($data->toolproxyid) ? ' AND toolproxyid = :toolproxyid' : ' AND toolproxyid IS NULL';
 169              if ($DB->record_exists_select('lti_types', $sql, $params)) {
 170                  $this->set_mapping('ltitype', $data->id, $data->id);
 171                  if ($data->toolproxyid) {
 172                      $this->set_mapping('ltitoolproxy', $data->toolproxyid, $data->toolproxyid);
 173                  }
 174                  return $data->id;
 175              }
 176          }
 177  
 178          if ($data->course != $this->get_courseid()) {
 179              // Site tools are not backed up and are not restored.
 180              return null;
 181          }
 182  
 183          // Now try to find the same type on the current site available in this course.
 184          // Compare only fields baseurl, course and name, if they are the same we assume it is the same tool.
 185          // LTI2 is not possible in the course so we add "lt.toolproxyid IS NULL" to the query.
 186          $sql = 'SELECT id
 187              FROM {lti_types}
 188             WHERE ' . $DB->sql_compare_text('baseurl', 255) . ' = ' . $DB->sql_compare_text(':baseurl', 255) . ' AND
 189                   course = :course AND name = :name AND toolproxyid IS NULL';
 190          if ($ltitype = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
 191              $this->set_mapping('ltitype', $data->id, $ltitype->id);
 192              return $ltitype->id;
 193          }
 194  
 195          return null;
 196      }
 197  
 198      /**
 199       * Process an lti config restore
 200       * @param mixed $data The data from backup XML file
 201       */
 202      protected function process_ltitypesconfig($data) {
 203          global $DB;
 204  
 205          $data = (object)$data;
 206          $data->typeid = $this->get_new_parentid('ltitype');
 207  
 208          // Only add configuration if the new lti_type was created.
 209          if ($data->typeid && $this->newltitype) {
 210              if ($data->name == 'servicesalt') {
 211                  $data->value = uniqid('', true);
 212              }
 213              $DB->insert_record('lti_types_config', $data);
 214          }
 215      }
 216  
 217      /**
 218       * Process an lti config restore
 219       * @param mixed $data The data from backup XML file
 220       */
 221      protected function process_ltitypesconfigencrypted($data) {
 222          global $DB;
 223  
 224          $data = (object)$data;
 225          $data->typeid = $this->get_new_parentid('ltitype');
 226  
 227          // Only add configuration if the new lti_type was created.
 228          if ($data->typeid && $this->newltitype) {
 229              $data->value = $this->decrypt($data->value);
 230              if (!is_null($data->value)) {
 231                  $DB->insert_record('lti_types_config', $data);
 232              }
 233          }
 234      }
 235  
 236      /**
 237       * Process a restore of LTI tool registration
 238       * This method is empty because we actually process registration as part of process_ltitype()
 239       * @param mixed $data The data from backup XML file
 240       */
 241      protected function process_ltitoolproxy($data) {
 242  
 243      }
 244  
 245      /**
 246       * Process an lti tool registration settings restore (only settings for the current activity)
 247       * @param mixed $data The data from backup XML file
 248       */
 249      protected function process_ltitoolsetting($data) {
 250          global $DB;
 251  
 252          $data = (object)$data;
 253          $data->toolproxyid = $this->get_new_parentid('ltitoolproxy');
 254  
 255          if (!$data->toolproxyid) {
 256              return;
 257          }
 258  
 259          $data->course = $this->get_courseid();
 260          $data->coursemoduleid = $this->task->get_moduleid();
 261          $DB->insert_record('lti_tool_settings', $data);
 262      }
 263  
 264      /**
 265       * Process a submission restore
 266       * @param mixed $data The data from backup XML file
 267       */
 268      protected function process_ltisubmission($data) {
 269          global $DB;
 270  
 271          $data = (object)$data;
 272          $oldid = $data->id;
 273  
 274          $data->ltiid = $this->get_new_parentid('lti');
 275  
 276          $data->datesubmitted = $this->apply_date_offset($data->datesubmitted);
 277          $data->dateupdated = $this->apply_date_offset($data->dateupdated);
 278          if ($data->userid > 0) {
 279              $data->userid = $this->get_mappingid('user', $data->userid);
 280          }
 281  
 282          $newitemid = $DB->insert_record('lti_submission', $data);
 283  
 284          $this->set_mapping('ltisubmission', $oldid, $newitemid);
 285      }
 286  
 287      protected function after_execute() {
 288          // Add lti related files, no need to match by itemname (just internally handled context).
 289          $this->add_related_files('mod_lti', 'intro', null);
 290      }
 291  }