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 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 backup steps that will be used
  37   * by the backup_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   * Define the complete assignment structure for backup, with file and id annotations
  53   */
  54  class backup_lti_activity_structure_step extends backup_activity_structure_step {
  55  
  56      /**
  57       * Defines structure of activity backup
  58       * @return backup_nested_element
  59       */
  60      protected function define_structure() {
  61          global $DB;
  62  
  63          // To know if we are including userinfo.
  64          $userinfo = $this->get_setting_value('userinfo');
  65  
  66          // Define each element separated.
  67          $lti = new backup_nested_element('lti', array('id'), array(
  68              'name',
  69              'intro',
  70              'introformat',
  71              'timecreated',
  72              'timemodified',
  73              'typeid',
  74              'toolurl',
  75              'securetoolurl',
  76              'preferheight',
  77              'launchcontainer',
  78              'instructorchoicesendname',
  79              'instructorchoicesendemailaddr',
  80              'instructorchoiceacceptgrades',
  81              'instructorchoiceallowroster',
  82              'instructorchoiceallowsetting',
  83              'grade',
  84              'instructorcustomparameters',
  85              'debuglaunch',
  86              'showtitlelaunch',
  87              'showdescriptionlaunch',
  88              'icon',
  89              'secureicon',
  90              new encrypted_final_element('resourcekey'),
  91              new encrypted_final_element('password'),
  92              )
  93          );
  94  
  95          $ltitype = new backup_nested_element('ltitype', array('id'), array(
  96              'name',
  97              'baseurl',
  98              'tooldomain',
  99              'state',
 100              'course',
 101              'coursevisible',
 102              'ltiversion',
 103              'clientid',
 104              'toolproxyid',
 105              'enabledcapability',
 106              'parameter',
 107              'icon',
 108              'secureicon',
 109              'createdby',
 110              'timecreated',
 111              'timemodified',
 112              'description'
 113              )
 114          );
 115  
 116          $ltitypesconfigs = new backup_nested_element('ltitypesconfigs');
 117          $ltitypesconfig  = new backup_nested_element('ltitypesconfig', array('id'), array(
 118                  'name',
 119                  'value',
 120              )
 121          );
 122          $ltitypesconfigencrypted  = new backup_nested_element('ltitypesconfigencrypted', array('id'), array(
 123                  'name',
 124                  new encrypted_final_element('value'),
 125              )
 126          );
 127  
 128          $ltitoolproxy = new backup_nested_element('ltitoolproxy', array('id'));
 129  
 130          $ltitoolsettings = new backup_nested_element('ltitoolsettings');
 131          $ltitoolsetting  = new backup_nested_element('ltitoolsetting', array('id'), array(
 132                  'settings',
 133                  'timecreated',
 134                  'timemodified',
 135              )
 136          );
 137  
 138          $ltisubmissions = new backup_nested_element('ltisubmissions');
 139          $ltisubmission = new backup_nested_element('ltisubmission', array('id'), array(
 140              'userid',
 141              'datesubmitted',
 142              'dateupdated',
 143              'gradepercent',
 144              'originalgrade',
 145              'launchid',
 146              'state'
 147          ));
 148  
 149          // Build the tree
 150          $lti->add_child($ltitype);
 151          $ltitype->add_child($ltitypesconfigs);
 152          $ltitypesconfigs->add_child($ltitypesconfig);
 153          $ltitypesconfigs->add_child($ltitypesconfigencrypted);
 154          $ltitype->add_child($ltitoolproxy);
 155          $ltitoolproxy->add_child($ltitoolsettings);
 156          $ltitoolsettings->add_child($ltitoolsetting);
 157          $lti->add_child($ltisubmissions);
 158          $ltisubmissions->add_child($ltisubmission);
 159  
 160          // Define sources.
 161          $ltirecord = $DB->get_record('lti', ['id' => $this->task->get_activityid()]);
 162          $lti->set_source_array([$ltirecord]);
 163  
 164          $ltitypedata = $this->retrieve_lti_type($ltirecord);
 165          $ltitype->set_source_array($ltitypedata ? [$ltitypedata] : []);
 166  
 167          if (isset($ltitypedata->baseurl)) {
 168              // Add type config values only if the type was backed up. Encrypt password and resourcekey.
 169              $params = [backup_helper::is_sqlparam($ltitypedata->id),
 170                  backup_helper::is_sqlparam('password'),
 171                  backup_helper::is_sqlparam('resourcekey')];
 172              $ltitypesconfig->set_source_sql("SELECT id, name, value
 173                  FROM {lti_types_config}
 174                  WHERE typeid = ? AND name <> ? AND name <> ?", $params);
 175              $ltitypesconfigencrypted->set_source_sql("SELECT id, name, value
 176                  FROM {lti_types_config}
 177                  WHERE typeid = ? AND (name = ? OR name = ?)", $params);
 178          }
 179  
 180          if (!empty($ltitypedata->toolproxyid)) {
 181              // If this is LTI 2 tool add settings for the current activity.
 182              $ltitoolproxy->set_source_array([['id' => $ltitypedata->toolproxyid]]);
 183              $ltitoolsetting->set_source_sql("SELECT *
 184                  FROM {lti_tool_settings}
 185                  WHERE toolproxyid = ? AND course = ? AND coursemoduleid = ?",
 186                  [backup_helper::is_sqlparam($ltitypedata->toolproxyid), backup::VAR_COURSEID, backup::VAR_MODID]);
 187          } else {
 188              $ltitoolproxy->set_source_array([]);
 189          }
 190  
 191          // All the rest of elements only happen if we are including user info.
 192          if ($userinfo) {
 193              $ltisubmission->set_source_table('lti_submission', array('ltiid' => backup::VAR_ACTIVITYID));
 194          }
 195  
 196          // Define id annotations
 197          $ltitype->annotate_ids('user', 'createdby');
 198          $ltitype->annotate_ids('course', 'course');
 199          $ltisubmission->annotate_ids('user', 'userid');
 200  
 201          // Define file annotations.
 202          $lti->annotate_files('mod_lti', 'intro', null); // This file areas haven't itemid.
 203  
 204          // Add support for subplugin structures.
 205          $this->add_subplugin_structure('ltisource', $lti, true);
 206          $this->add_subplugin_structure('ltiservice', $lti, true);
 207  
 208          // Return the root element (lti), wrapped into standard activity structure.
 209          return $this->prepare_activity_structure($lti);
 210      }
 211  
 212      /**
 213       * Retrieves a record from {lti_type} table associated with the current activity
 214       *
 215       * Information about site tools is not returned because it is insecure to back it up,
 216       * only fields necessary for same-site tool matching are left in the record
 217       *
 218       * @param stdClass $ltirecord record from {lti} table
 219       * @return stdClass|null
 220       */
 221      protected function retrieve_lti_type($ltirecord) {
 222          global $DB;
 223          if (!$ltirecord->typeid) {
 224              return null;
 225          }
 226  
 227          $record = $DB->get_record('lti_types', ['id' => $ltirecord->typeid]);
 228          if ($record && $record->course == SITEID) {
 229              // Site LTI types or registrations are not backed up except for their name (which is visible).
 230              // Predefined course types can be backed up.
 231              $allowedkeys = ['id', 'course', 'name', 'toolproxyid'];
 232              foreach ($record as $key => $value) {
 233                  if (!in_array($key, $allowedkeys)) {
 234                      $record->$key = null;
 235                  }
 236              }
 237          }
 238  
 239          return $record;
 240      }
 241  }