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 400 and 401] [Versions 401 and 402] [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  
  18  /**
  19   * This file keeps track of upgrades to the lti enrolment plugin
  20   *
  21   * @package enrol_lti
  22   * @copyright  2016 John Okely <john@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26   defined('MOODLE_INTERNAL') || die;
  27  
  28  /**
  29   * xmldb_lti_upgrade is the function that upgrades
  30   * the lti module database when is needed
  31   *
  32   * This function is automaticly called when version number in
  33   * version.php changes.
  34   *
  35   * @param int $oldversion New old version number.
  36   *
  37   * @return boolean
  38   */
  39  function xmldb_enrol_lti_upgrade($oldversion) {
  40      global $CFG, $OUTPUT, $DB;
  41      $dbman = $DB->get_manager();
  42  
  43      // Automatically generated Moodle v3.9.0 release upgrade line.
  44      // Put any upgrade step following this.
  45  
  46      if ($oldversion < 2021052501) {
  47          // LTI 1.3: Set a private key for this site (which is acting as a tool in LTI 1.3).
  48          require_once($CFG->dirroot . '/enrol/lti/upgradelib.php');
  49  
  50          $warning = enrol_lti_verify_private_key();
  51          if (!empty($warning)) {
  52              echo $OUTPUT->notification($warning, 'notifyproblem');
  53          }
  54  
  55          // Lti savepoint reached.
  56          upgrade_plugin_savepoint(true, 2021052501, 'enrol', 'lti');
  57      }
  58  
  59      if ($oldversion < 2021052502) {
  60          // Define table enrol_lti_app_registration to be created.
  61          $table = new xmldb_table('enrol_lti_app_registration');
  62  
  63          // Adding fields to table enrol_lti_app_registration.
  64          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  65          $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
  66          $table->add_field('platformid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
  67          $table->add_field('clientid', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null);
  68          $table->add_field('platformclienthash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
  69          $table->add_field('authenticationrequesturl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
  70          $table->add_field('jwksurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
  71          $table->add_field('accesstokenurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
  72          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  73          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  74  
  75          // Adding keys to table enrol_lti_app_registration.
  76          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
  77  
  78          // Add unique index on platformclienthash.
  79          $table->add_index('platformclienthash', XMLDB_INDEX_UNIQUE, ['platformclienthash']);
  80  
  81          // Conditionally launch create table for enrol_lti_app_registration.
  82          if (!$dbman->table_exists($table)) {
  83              $dbman->create_table($table);
  84          }
  85  
  86          // Lti savepoint reached.
  87          upgrade_plugin_savepoint(true, 2021052502, 'enrol', 'lti');
  88      }
  89  
  90      if ($oldversion < 2021052503) {
  91          // Add a new column 'ltiversion' to the enrol_lti_tools table.
  92          $table = new xmldb_table('enrol_lti_tools');
  93  
  94          // Define field ltiversion to be added to enrol_lti_tools.
  95          $field = new xmldb_field('ltiversion', XMLDB_TYPE_CHAR, 15, null, XMLDB_NOTNULL, null, "LTI-1p3", 'contextid');
  96  
  97          // Conditionally launch add field ltiversion, setting it to the legacy value for all published content.
  98          if (!$dbman->field_exists($table, $field)) {
  99              $dbman->add_field($table, $field);
 100              $DB->set_field('enrol_lti_tools', 'ltiversion', 'LTI-1p0/LTI-2p0');
 101          }
 102  
 103          // Define field uuid to be added to enrol_lti_tools.
 104          $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, 36, null, null, null, null, 'ltiversion');
 105  
 106          // Conditionally launch add field uuid, setting it to null for existing rows.
 107          if (!$dbman->field_exists($table, $field)) {
 108              $dbman->add_field($table, $field);
 109              $key = new xmldb_key('uuid', XMLDB_KEY_UNIQUE, ['uuid']);
 110              $dbman->add_key($table, $key);
 111          }
 112  
 113          // Lti savepoint reached.
 114          upgrade_plugin_savepoint(true, 2021052503, 'enrol', 'lti');
 115      }
 116  
 117      if ($oldversion < 2021052504) {
 118          // Define table enrol_lti_deployment to be created.
 119          $table = new xmldb_table('enrol_lti_deployment');
 120  
 121          // Adding fields to table enrol_lti_deployment.
 122          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 123          $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
 124          $table->add_field('deploymentid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
 125          $table->add_field('platformid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 126          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 127          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 128  
 129          // Adding keys to table enrol_lti_deployment.
 130          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 131          $table->add_key('platformid', XMLDB_KEY_FOREIGN, ['platformid'], 'enrol_lti_app_registration', ['id']);
 132  
 133          // Add unique index on platformid (issuer), deploymentid.
 134          $table->add_index('platformid-deploymentid', XMLDB_INDEX_UNIQUE, ['platformid', 'deploymentid']);
 135  
 136          // Conditionally launch create table for enrol_lti_deployment.
 137          if (!$dbman->table_exists($table)) {
 138              $dbman->create_table($table);
 139          }
 140  
 141          // Lti savepoint reached.
 142          upgrade_plugin_savepoint(true, 2021052504, 'enrol', 'lti');
 143      }
 144  
 145      if ($oldversion < 2021052505) {
 146          // Add a new column 'ltideploymentid' to the enrol_lti_users table.
 147          $table = new xmldb_table('enrol_lti_users');
 148  
 149          // Define field ltideploymentid to be added to enrol_lti_users.
 150          $field = new xmldb_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sourceid');
 151  
 152          // Conditionally launch add field deploymentid.
 153          if (!$dbman->field_exists($table, $field)) {
 154              $dbman->add_field($table, $field);
 155          }
 156  
 157          // Launch add key ltideploymentid.
 158          $key = new xmldb_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']);
 159          $dbman->add_key($table, $key);
 160  
 161          // Lti savepoint reached.
 162          upgrade_plugin_savepoint(true, 2021052505, 'enrol', 'lti');
 163      }
 164  
 165      if ($oldversion < 2021052506) {
 166          // Define table enrol_lti_resource_link to be created.
 167          $table = new xmldb_table('enrol_lti_resource_link');
 168  
 169          // Adding fields to table enrol_lti_resource_link.
 170          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 171          $table->add_field('resourcelinkid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
 172          $table->add_field('resourceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 173          $table->add_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 174          $table->add_field('lticontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
 175          $table->add_field('lineitemsservice', XMLDB_TYPE_CHAR, '1333', null, null, null, null);
 176          $table->add_field('lineitemservice', XMLDB_TYPE_CHAR, '1333', null, null, null, null);
 177          $table->add_field('lineitemscope', XMLDB_TYPE_CHAR, '255', null, null, null, null);
 178          $table->add_field('resultscope', XMLDB_TYPE_CHAR, '255', null, null, null, null);
 179          $table->add_field('scorescope', XMLDB_TYPE_CHAR, '255', null, null, null, null);
 180          $table->add_field('contextmembershipsurl', XMLDB_TYPE_CHAR, '1333', null, null, null, null);
 181          $table->add_field('nrpsserviceversions', XMLDB_TYPE_CHAR, '255', null, null, null, null);
 182          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 183          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 184  
 185          // Adding keys to table enrol_lti_resource_link.
 186          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 187          $table->add_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']);
 188          $table->add_key('lticontextid', XMLDB_KEY_FOREIGN, ['lticontextid'], 'enrol_lti_context', ['id']);
 189  
 190          // Add unique index on resourcelinkid, ltideploymentid.
 191          $table->add_index('resourcelinkdid-ltideploymentid', XMLDB_INDEX_UNIQUE, ['resourcelinkid', 'ltideploymentid']);
 192  
 193          // Conditionally launch create table for enrol_lti_resource_link.
 194          if (!$dbman->table_exists($table)) {
 195              $dbman->create_table($table);
 196          }
 197  
 198          // Lti savepoint reached.
 199          upgrade_plugin_savepoint(true, 2021052506, 'enrol', 'lti');
 200      }
 201  
 202      if ($oldversion < 2021052507) {
 203          // Define table enrol_lti_context to be created.
 204          $table = new xmldb_table('enrol_lti_context');
 205  
 206          // Adding fields to table enrol_lti_context.
 207          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 208          $table->add_field('contextid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
 209          $table->add_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 210          $table->add_field('type', XMLDB_TYPE_TEXT, null, null, null, null, null);
 211          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 212          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 213  
 214          // Adding keys to table enrol_lti_context.
 215          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 216          $table->add_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']);
 217  
 218          // Add unique index on ltideploymentid, contextid.
 219          $table->add_index('ltideploymentid-contextid', XMLDB_INDEX_UNIQUE, ['ltideploymentid', 'contextid']);
 220  
 221          // Conditionally launch create table for enrol_lti_context.
 222          if (!$dbman->table_exists($table)) {
 223              $dbman->create_table($table);
 224          }
 225  
 226          upgrade_plugin_savepoint(true, 2021052507, 'enrol', 'lti');
 227      }
 228  
 229      if ($oldversion < 2021052508) {
 230          // Define table enrol_lti_user_resource_link to be created.
 231          $table = new xmldb_table('enrol_lti_user_resource_link');
 232  
 233          // Adding fields to table enrol_lti_user_resource_link.
 234          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 235          $table->add_field('ltiuserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 236          $table->add_field('resourcelinkid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 237  
 238          // Adding keys to table enrol_lti_user_resource_link.
 239          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 240          $table->add_key('ltiuserid', XMLDB_KEY_FOREIGN, ['ltiuserid'], 'enrol_lti_users', ['id']);
 241          $table->add_key('resourcelinkid', XMLDB_KEY_FOREIGN, ['resourcelinkid'], 'enrol_lti_resource_link', ['id']);
 242  
 243          // Add unique index on userid, resourcelinkid.
 244          $table->add_index('ltiuserid-resourcelinkid', XMLDB_INDEX_UNIQUE, ['ltiuserid', 'resourcelinkid']);
 245  
 246          // Conditionally launch create table for enrol_lti_user_resource_link.
 247          if (!$dbman->table_exists($table)) {
 248              $dbman->create_table($table);
 249          }
 250          upgrade_plugin_savepoint(true, 2021052508, 'enrol', 'lti');
 251      }
 252  
 253      if ($oldversion < 2021052512) {
 254          // Define field legacyconsumerkey to be added to enrol_lti_deployment.
 255          $table = new xmldb_table('enrol_lti_deployment');
 256          $field = new xmldb_field('legacyconsumerkey', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'platformid');
 257  
 258          // Conditionally launch add field legacyconsumerkey.
 259          if (!$dbman->field_exists($table, $field)) {
 260              $dbman->add_field($table, $field);
 261          }
 262  
 263          upgrade_plugin_savepoint(true, 2021052512, 'enrol', 'lti');
 264      }
 265  
 266      if ($oldversion < 2021052513) {
 267          // Define table enrol_lti_reg_token to be created.
 268          $table = new xmldb_table('enrol_lti_reg_token');
 269  
 270          // Adding fields to table enrol_lti_reg_token.
 271          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 272          $table->add_field('token', XMLDB_TYPE_CHAR, '60', null, XMLDB_NOTNULL, null, null);
 273          $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 274          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 275  
 276          // Adding keys to table enrol_lti_reg_token.
 277          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 278  
 279          // Conditionally launch create table for enrol_lti_reg_token.
 280          if (!$dbman->table_exists($table)) {
 281              $dbman->create_table($table);
 282          }
 283  
 284          upgrade_plugin_savepoint(true, 2021052513, 'enrol', 'lti');
 285      }
 286  
 287      if ($oldversion < 2021052514) {
 288          // Add a new column 'provisioningmodelearner' to the enrol_lti_tools table.
 289          $table = new xmldb_table('enrol_lti_tools');
 290  
 291          // Define field provisioningmodelearner to be added to enrol_lti_tools.
 292          $field = new xmldb_field('provisioningmodelearner', XMLDB_TYPE_INTEGER, 2, null, null, null, null, 'uuid');
 293  
 294          // Conditionally launch add field provisioningmodelearner.
 295          if (!$dbman->field_exists($table, $field)) {
 296              $dbman->add_field($table, $field);
 297          }
 298  
 299          // Define field provisioningmodeinstructor to be added to enrol_lti_tools.
 300          $field = new xmldb_field('provisioningmodeinstructor', XMLDB_TYPE_INTEGER, 2, null, null, null, null,
 301              'provisioningmodelearner');
 302  
 303          // Conditionally launch add field provisioningmodeinstructor.
 304          if (!$dbman->field_exists($table, $field)) {
 305              $dbman->add_field($table, $field);
 306          }
 307  
 308          // Lti savepoint reached.
 309          upgrade_plugin_savepoint(true, 2021052514, 'enrol', 'lti');
 310      }
 311  
 312      if ($oldversion < 2022031400) {
 313          // Changing the default of field platformid on table enrol_lti_app_registration to null.
 314          $table = new xmldb_table('enrol_lti_app_registration');
 315          $field = new xmldb_field('platformid', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
 316  
 317          // Launch change of nullability for field platformid.
 318          $dbman->change_field_notnull($table, $field);
 319  
 320          // Changing the default of field clientid on table enrol_lti_app_registration to null.
 321          $field = new xmldb_field('clientid', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'platformid');
 322  
 323          // Launch change of nullability for field clientid.
 324          $dbman->change_field_notnull($table, $field);
 325  
 326          // Drop the platformclienthash index, so the field can be modified.
 327          $index = new xmldb_index('platformclienthash', XMLDB_INDEX_UNIQUE, ['platformclienthash']);
 328  
 329          // Conditionally launch drop index platformclienthash.
 330          if ($dbman->index_exists($table, $index)) {
 331              $dbman->drop_index($table, $index);
 332          }
 333  
 334          // Changing the default of field platformclienthash on table enrol_lti_app_registration to null.
 335          $field = new xmldb_field('platformclienthash', XMLDB_TYPE_CHAR, '64', null, null, null, null, 'clientid');
 336  
 337          // Launch change of nullability for field platformclienthash.
 338          $dbman->change_field_notnull($table, $field);
 339  
 340          // Recreate the platformclienthash index.
 341          if (!$dbman->index_exists($table, $index)) {
 342              $dbman->add_index($table, $index);
 343          }
 344  
 345          // Changing the default of field authenticationrequesturl on table enrol_lti_app_registration to null.
 346          $field = new xmldb_field('authenticationrequesturl', XMLDB_TYPE_TEXT, null, null, null, null, null, 'platformclienthash');
 347  
 348          // Launch change of nullability for field authenticationrequesturl.
 349          $dbman->change_field_notnull($table, $field);
 350  
 351          // Changing the default of field jwksurl on table enrol_lti_app_registration to null.
 352          $field = new xmldb_field('jwksurl', XMLDB_TYPE_TEXT, null, null, null, null, null, 'authenticationrequesturl');
 353  
 354          // Launch change of nullability for field jwksurl.
 355          $dbman->change_field_notnull($table, $field);
 356  
 357          // Changing the default of field accesstokenurl on table enrol_lti_app_registration to null.
 358          $field = new xmldb_field('accesstokenurl', XMLDB_TYPE_TEXT, null, null, null, null, null, 'jwksurl');
 359  
 360          // Launch change of nullability for field accesstokenurl.
 361          $dbman->change_field_notnull($table, $field);
 362  
 363          // Lti savepoint reached.
 364          upgrade_plugin_savepoint(true, 2022031400, 'enrol', 'lti');
 365      }
 366  
 367      if ($oldversion < 2022031401) {
 368          // Define field uniqueid to be added to enrol_lti_app_registration (defined as null so it can be set for existing rows).
 369          $table = new xmldb_table('enrol_lti_app_registration');
 370          $field = new xmldb_field('uniqueid', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'accesstokenurl');
 371  
 372          // Conditionally launch add field uniqueid.
 373          if (!$dbman->field_exists($table, $field)) {
 374              $dbman->add_field($table, $field);
 375  
 376              // Set existing values to use a suitable unique id.
 377              $recordset = $DB->get_recordset('enrol_lti_app_registration');
 378              foreach ($recordset as $record) {
 379                  // Create a unique id for the registration. This will be used by:
 380                  // a) The initiate_login endpoint (enrol/lti/login.php), as a stand in for client_id, when that's not provided.
 381                  // b) The dynamic registration endpoint, where it'll be used to identify the incomplete registration to update
 382                  // with the platform details.
 383                  do {
 384                      $bytes = random_bytes(30);
 385                      $record->uniqueid = bin2hex($bytes);
 386                  } while ($DB->record_exists('enrol_lti_app_registration', ['uniqueid' => $record->uniqueid]));
 387  
 388                  $DB->update_record('enrol_lti_app_registration', $record);
 389              }
 390              $recordset->close();
 391  
 392              // Now make the field notnull.
 393              $field = new xmldb_field('uniqueid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'accesstokenurl');
 394              $dbman->change_field_notnull($table, $field);
 395          }
 396  
 397          // Launch add unique key uniqueid.
 398          $key = new xmldb_key('uniqueid', XMLDB_KEY_UNIQUE, ['uniqueid']);
 399          $dbman->add_key($table, $key);
 400  
 401          // Define field status to be added to enrol_lti_app_registration with a default value of 1 (to set existing rows).
 402          $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'uniqueid');
 403  
 404          // Conditionally launch add field status.
 405          if (!$dbman->field_exists($table, $field)) {
 406              $dbman->add_field($table, $field);
 407  
 408              // Now change the default value to '0'.
 409              $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'uniqueid');
 410              $dbman->change_field_default($table, $field);
 411          }
 412  
 413          // Define field platformuniqueidhash to be added to enrol_lti_app_registration.
 414          $field = new xmldb_field('platformuniqueidhash', XMLDB_TYPE_CHAR, '64', null, null, null, null, 'status');
 415          if (!$dbman->field_exists($table, $field)) {
 416              $dbman->add_field($table, $field);
 417  
 418              $recordset = $DB->get_recordset('enrol_lti_app_registration');
 419              foreach ($recordset as $record) {
 420                  $record->platformuniqueidhash = hash('sha256', $record->platformid . ':' . $record->uniqueid);
 421                  $DB->update_record('enrol_lti_app_registration', $record);
 422              }
 423              $recordset->close();
 424          }
 425  
 426          // Add index platformuniqueidhash to enrol_lti_app_registration.
 427          $index = new xmldb_index('platformuniqueidhash', XMLDB_INDEX_UNIQUE, ['platformuniqueidhash']);
 428  
 429          if (!$dbman->index_exists($table, $index)) {
 430              $dbman->add_index($table, $index);
 431          }
 432  
 433          // Lti savepoint reached.
 434          upgrade_plugin_savepoint(true, 2022031401, 'enrol', 'lti');
 435      }
 436  
 437      if ($oldversion < 2022031402) {
 438          // Define table enrol_lti_reg_token to be dropped.
 439          $table = new xmldb_table('enrol_lti_reg_token');
 440  
 441          // Conditionally launch drop table for enrol_lti_reg_token.
 442          if ($dbman->table_exists($table)) {
 443              $dbman->drop_table($table);
 444          }
 445  
 446          // Lti savepoint reached.
 447          upgrade_plugin_savepoint(true, 2022031402, 'enrol', 'lti');
 448      }
 449  
 450      // Automatically generated Moodle v4.0.0 release upgrade line.
 451      // Put any upgrade step following this.
 452  
 453      if ($oldversion < 2022061500) {
 454          // Disable all orphaned enrolment method instances.
 455          $sql = "id IN (SELECT t.enrolid
 456                           FROM {enrol_lti_tools} t
 457                      LEFT JOIN {context} c ON (t.contextid = c.id)
 458                          WHERE c.id IS NULL)";
 459          $DB->set_field_select('enrol', 'status', 1, $sql);
 460  
 461          // Lti savepoint reached.
 462          upgrade_plugin_savepoint(true, 2022061500, 'enrol', 'lti');
 463      }
 464  
 465      if ($oldversion < 2022103100) {
 466          // Update lti user information for LTI 2.0 users having the wrong consumer secret recorded.
 467          // This applies to any LTI 2.0 user who has launched the tool (i.e. has lastaccess) and fixes a non-functional grade sync
 468          // for LTI 2.0 consumers.
 469          $sql = "SELECT lu.id, lc.secret
 470                    FROM {enrol_lti_users} lu
 471                    JOIN {enrol_lti_lti2_consumer} lc
 472                      ON (" . $DB->sql_compare_text('lu.consumerkey', 255) . " = lc.consumerkey256)
 473                   WHERE lc.ltiversion = :ltiversion
 474                     AND " . $DB->sql_compare_text('lu.consumersecret') . " != lc.secret
 475                     AND lu.lastaccess IS NOT NULL";
 476          $affectedltiusersrs = $DB->get_recordset_sql($sql, ['ltiversion' => 'LTI-2p0']);
 477          foreach ($affectedltiusersrs as $ltiuser) {
 478              $DB->set_field('enrol_lti_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
 479          }
 480          $affectedltiusersrs->close();
 481  
 482          // Lti savepoint reached.
 483          upgrade_plugin_savepoint(true, 2022103100, 'enrol', 'lti');
 484      }
 485  
 486      if ($oldversion < 2022110300) {
 487          // Update lti user information for any users missing a consumer secret.
 488          // This applies to any user who has launched the tool (i.e. has lastaccess) but who doesn't have a secret recorded.
 489          // This fixes a bug where enrol_lti_users records are created first during a member sync, and are missing the secret,
 490          // even despite having launched the tool subsequently.
 491          $sql = "SELECT lu.id, lc.secret
 492                    FROM {enrol_lti_users} lu
 493                    JOIN {enrol_lti_lti2_consumer} lc
 494                      ON (" . $DB->sql_compare_text('lu.consumerkey', 255) . " = lc.consumerkey256)
 495                   WHERE lu.consumersecret IS NULL
 496                     AND lu.lastaccess IS NOT NULL";
 497          $affectedltiusersrs = $DB->get_recordset_sql($sql);
 498          foreach ($affectedltiusersrs as $ltiuser) {
 499              $DB->set_field('enrol_lti_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
 500          }
 501          $affectedltiusersrs->close();
 502  
 503          // Lti savepoint reached.
 504          upgrade_plugin_savepoint(true, 2022110300, 'enrol', 'lti');
 505      }
 506  
 507      // Automatically generated Moodle v4.1.0 release upgrade line.
 508      // Put any upgrade step following this.
 509  
 510      return true;
 511  }