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.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  /**
  52   * Structure step to restore one lti activity
  53   */
  54  class restore_lti_activity_structure_step extends restore_activity_structure_step {
  55  
  56      /** @var bool */
  57      protected $newltitype = false;
  58  
  59      protected function define_structure() {
  60  
  61          $paths = array();
  62          // To know if we are including userinfo.
  63          $userinfo = $this->get_setting_value('userinfo');
  64  
  65          $lti = new restore_path_element('lti', '/activity/lti');
  66          $paths[] = $lti;
  67          $paths[] = new restore_path_element('ltitype', '/activity/lti/ltitype');
  68          $paths[] = new restore_path_element('ltitypesconfig', '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfig');
  69          $paths[] = new restore_path_element('ltitypesconfigencrypted',
  70              '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfigencrypted');
  71          $paths[] = new restore_path_element('ltitoolproxy', '/activity/lti/ltitype/ltitoolproxy');
  72          $paths[] = new restore_path_element('ltitoolsetting', '/activity/lti/ltitype/ltitoolproxy/ltitoolsettings/ltitoolsetting');
  73  
  74          if ($userinfo) {
  75              $submission = new restore_path_element('ltisubmission', '/activity/lti/ltisubmissions/ltisubmission');
  76              $paths[] = $submission;
  77          }
  78  
  79          // Add support for subplugin structures.
  80          $this->add_subplugin_structure('ltisource', $lti);
  81          $this->add_subplugin_structure('ltiservice', $lti);
  82  
  83          // Return the paths wrapped into standard activity structure.
  84          return $this->prepare_activity_structure($paths);
  85      }
  86  
  87      protected function process_lti($data) {
  88          global $DB;
  89  
  90          $data = (object)$data;
  91          $oldid = $data->id;
  92          $data->course = $this->get_courseid();
  93          $data->servicesalt = uniqid('', true);
  94  
  95          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  96          // See MDL-9367.
  97  
  98           // Grade used to be a float (whole numbers only), restore as int.
  99          $data->grade = (int) $data->grade;
 100  
 101          $data->typeid = 0;
 102  
 103          // Try to decrypt resourcekey and password. Null if not possible (DB default).
 104          // Note these fields were originally encrypted on backup using {link @encrypted_final_element}.
 105          $data->resourcekey = isset($data->resourcekey) ? $this->decrypt($data->resourcekey) : null;
 106          $data->password = isset($data->password) ? $this->decrypt($data->password) : null;
 107  
 108          $newitemid = $DB->insert_record('lti', $data);
 109  
 110          // Immediately after inserting "activity" record, call this.
 111          $this->apply_activity_instance($newitemid);
 112      }
 113  
 114      /**
 115       * Process an lti type restore
 116       * @param mixed $data The data from backup XML file
 117       * @return void
 118       */
 119      protected function process_ltitype($data) {
 120          global $DB, $USER;
 121  
 122          $data = (object)$data;
 123          $oldid = $data->id;
 124          if (!empty($data->createdby)) {
 125              $data->createdby = $this->get_mappingid('user', $data->createdby) ?: $USER->id;
 126          }
 127  
 128          $courseid = $this->get_courseid();
 129          $data->course = ($this->get_mappingid('course', $data->course) == $courseid) ? $courseid : SITEID;
 130  
 131          // Try to find existing lti type with the same properties.
 132          $ltitypeid = $this->find_existing_lti_type($data);
 133  
 134          $this->newltitype = false;
 135          if (!$ltitypeid && $data->course == $courseid) {
 136              unset($data->toolproxyid); // Course tools can not use LTI2.
 137              $ltitypeid = $DB->insert_record('lti_types', $data);
 138              $this->newltitype = true;
 139              $this->set_mapping('ltitype', $oldid, $ltitypeid);
 140          }
 141  
 142          // Add the typeid entry back to LTI module.
 143          $DB->update_record('lti', ['id' => $this->get_new_parentid('lti'), 'typeid' => $ltitypeid]);
 144      }
 145  
 146      /**
 147       * Attempts to find existing record in lti_type
 148       * @param stdClass $data
 149       * @return int|null field lti_types.id or null if tool is not found
 150       */
 151      protected function find_existing_lti_type($data) {
 152          global $DB;
 153          if ($ltitypeid = $this->get_mappingid('ltitype', $data->id)) {
 154              return $ltitypeid;
 155          }
 156  
 157          $ltitype = null;
 158          $params = (array)$data;
 159          if ($this->task->is_samesite()) {
 160              // If we are restoring on the same site try to find lti type with the same id.
 161              $sql = 'id = :id AND course = :course';
 162              $sql .= ($data->toolproxyid) ? ' AND toolproxyid = :toolproxyid' : ' AND toolproxyid IS NULL';
 163              if ($DB->record_exists_select('lti_types', $sql, $params)) {
 164                  $this->set_mapping('ltitype', $data->id, $data->id);
 165                  if ($data->toolproxyid) {
 166                      $this->set_mapping('ltitoolproxy', $data->toolproxyid, $data->toolproxyid);
 167                  }
 168                  return $data->id;
 169              }
 170          }
 171  
 172          if ($data->course != $this->get_courseid()) {
 173              // Site tools are not backed up and are not restored.
 174              return null;
 175          }
 176  
 177          // Now try to find the same type on the current site available in this course.
 178          // Compare only fields baseurl, course and name, if they are the same we assume it is the same tool.
 179          // LTI2 is not possible in the course so we add "lt.toolproxyid IS NULL" to the query.
 180          $sql = 'SELECT id
 181              FROM {lti_types}
 182             WHERE ' . $DB->sql_compare_text('baseurl', 255) . ' = ' . $DB->sql_compare_text(':baseurl', 255) . ' AND
 183                   course = :course AND name = :name AND toolproxyid IS NULL';
 184          if ($ltitype = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
 185              $this->set_mapping('ltitype', $data->id, $ltitype->id);
 186              return $ltitype->id;
 187          }
 188  
 189          return null;
 190      }
 191  
 192      /**
 193       * Process an lti config restore
 194       * @param mixed $data The data from backup XML file
 195       */
 196      protected function process_ltitypesconfig($data) {
 197          global $DB;
 198  
 199          $data = (object)$data;
 200          $data->typeid = $this->get_new_parentid('ltitype');
 201  
 202          // Only add configuration if the new lti_type was created.
 203          if ($data->typeid && $this->newltitype) {
 204              if ($data->name == 'servicesalt') {
 205                  $data->value = uniqid('', true);
 206              }
 207              $DB->insert_record('lti_types_config', $data);
 208          }
 209      }
 210  
 211      /**
 212       * Process an lti config restore
 213       * @param mixed $data The data from backup XML file
 214       */
 215      protected function process_ltitypesconfigencrypted($data) {
 216          global $DB;
 217  
 218          $data = (object)$data;
 219          $data->typeid = $this->get_new_parentid('ltitype');
 220  
 221          // Only add configuration if the new lti_type was created.
 222          if ($data->typeid && $this->newltitype) {
 223              $data->value = $this->decrypt($data->value);
 224              if (!is_null($data->value)) {
 225                  $DB->insert_record('lti_types_config', $data);
 226              }
 227          }
 228      }
 229  
 230      /**
 231       * Process a restore of LTI tool registration
 232       * This method is empty because we actually process registration as part of process_ltitype()
 233       * @param mixed $data The data from backup XML file
 234       */
 235      protected function process_ltitoolproxy($data) {
 236  
 237      }
 238  
 239      /**
 240       * Process an lti tool registration settings restore (only settings for the current activity)
 241       * @param mixed $data The data from backup XML file
 242       */
 243      protected function process_ltitoolsetting($data) {
 244          global $DB;
 245  
 246          $data = (object)$data;
 247          $data->toolproxyid = $this->get_new_parentid('ltitoolproxy');
 248  
 249          if (!$data->toolproxyid) {
 250              return;
 251          }
 252  
 253          $data->course = $this->get_courseid();
 254          $data->coursemoduleid = $this->task->get_moduleid();
 255          $DB->insert_record('lti_tool_settings', $data);
 256      }
 257  
 258      /**
 259       * Process a submission restore
 260       * @param mixed $data The data from backup XML file
 261       */
 262      protected function process_ltisubmission($data) {
 263          global $DB;
 264  
 265          $data = (object)$data;
 266          $oldid = $data->id;
 267  
 268          $data->ltiid = $this->get_new_parentid('lti');
 269  
 270          $data->datesubmitted = $this->apply_date_offset($data->datesubmitted);
 271          $data->dateupdated = $this->apply_date_offset($data->dateupdated);
 272          if ($data->userid > 0) {
 273              $data->userid = $this->get_mappingid('user', $data->userid);
 274          }
 275  
 276          $newitemid = $DB->insert_record('lti_submission', $data);
 277  
 278          $this->set_mapping('ltisubmission', $oldid, $newitemid);
 279      }
 280  
 281      protected function after_execute() {
 282          // Add lti related files, no need to match by itemname (just internally handled context).
 283          $this->add_related_files('mod_lti', 'intro', null);
 284      }
 285  }