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   * This file keeps track of upgrades to the h5pactivity module
  19   *
  20   * Sometimes, changes between versions involve
  21   * alterations to database structures and other
  22   * major things that may break installations.
  23   *
  24   * The upgrade function in this file will attempt
  25   * to perform all the necessary actions to upgrade
  26   * your older installation to the current version.
  27   *
  28   * If there's something it cannot do itself, it
  29   * will tell you what you need to do.
  30   *
  31   * The commands in here will all be database-neutral,
  32   * using the methods of database_manager class
  33   *
  34   * Please do not forget to use upgrade_set_timeout()
  35   * before any action that may take longer time to finish.
  36   *
  37   * @package   mod_h5pactivity
  38   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  
  42  defined('MOODLE_INTERNAL') || die();
  43  
  44  /**
  45   * Function to upgrade mod_h5pactivity.
  46   * @param int $oldversion the version we are upgrading from
  47   * @return bool result
  48   */
  49  function xmldb_h5pactivity_upgrade($oldversion) {
  50      global $DB;
  51  
  52      $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
  53  
  54      if ($oldversion < 2020032300) {
  55  
  56          // Changing the default of field timecreated on table h5pactivity to drop it.
  57          $table = new xmldb_table('h5pactivity');
  58          $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'name');
  59  
  60          // Launch change of default for field timecreated.
  61          $dbman->change_field_default($table, $field);
  62  
  63          // Changing the default of field timemodified on table h5pactivity to drop it.
  64          $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated');
  65  
  66          // Launch change of default for field timemodified.
  67          $dbman->change_field_default($table, $field);
  68  
  69          // Define table h5pactivity_attempts to be created.
  70          $table = new xmldb_table('h5pactivity_attempts');
  71  
  72          // Adding fields to table h5pactivity_attempts.
  73          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  74          $table->add_field('h5pactivityid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  75          $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null);
  76          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  77          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  78          $table->add_field('attempt', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '1');
  79          $table->add_field('rawscore', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
  80          $table->add_field('maxscore', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
  81  
  82          // Adding keys to table h5pactivity_attempts.
  83          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
  84          $table->add_key('fk_h5pactivityid', XMLDB_KEY_FOREIGN, ['h5pactivityid'], 'h5pactivity', ['id']);
  85          $table->add_key('uq_activityuserattempt', XMLDB_KEY_UNIQUE, ['h5pactivityid', 'userid', 'attempt']);
  86  
  87          // Adding indexes to table h5pactivity_attempts.
  88          $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, ['timecreated']);
  89          $table->add_index('h5pactivityid-timecreated', XMLDB_INDEX_NOTUNIQUE, ['h5pactivityid', 'timecreated']);
  90          $table->add_index('h5pactivityid-userid', XMLDB_INDEX_NOTUNIQUE, ['h5pactivityid', 'userid']);
  91  
  92          // Conditionally launch create table for h5pactivity_attempts.
  93          if (!$dbman->table_exists($table)) {
  94              $dbman->create_table($table);
  95          }
  96  
  97          // Define table h5pactivity_attempts_results to be created.
  98          $table = new xmldb_table('h5pactivity_attempts_results');
  99  
 100          // Adding fields to table h5pactivity_attempts_results.
 101          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 102          $table->add_field('attemptid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 103          $table->add_field('subcontent', XMLDB_TYPE_CHAR, '128', null, null, null, null);
 104          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 105          $table->add_field('interactiontype', XMLDB_TYPE_CHAR, '128', null, null, null, null);
 106          $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
 107          $table->add_field('correctpattern', XMLDB_TYPE_TEXT, null, null, null, null, null);
 108          $table->add_field('response', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
 109          $table->add_field('additionals', XMLDB_TYPE_TEXT, null, null, null, null, null);
 110          $table->add_field('rawscore', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 111          $table->add_field('maxscore', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
 112  
 113          // Adding keys to table h5pactivity_attempts_results.
 114          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 115          $table->add_key('fk_attemptid', XMLDB_KEY_FOREIGN, ['attemptid'], 'h5pactivity_attempts', ['id']);
 116  
 117          // Adding indexes to table h5pactivity_attempts_results.
 118          $table->add_index('attemptid-timecreated', XMLDB_INDEX_NOTUNIQUE, ['attemptid', 'timecreated']);
 119  
 120          // Conditionally launch create table for h5pactivity_attempts_results.
 121          if (!$dbman->table_exists($table)) {
 122              $dbman->create_table($table);
 123          }
 124  
 125          // H5pactivity savepoint reached.
 126          upgrade_mod_savepoint(true, 2020032300, 'h5pactivity');
 127      }
 128  
 129      if ($oldversion < 2020041400) {
 130  
 131          // Define field duration to be added to h5pactivity_attempts.
 132          $table = new xmldb_table('h5pactivity_attempts');
 133          $field = new xmldb_field('duration', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'maxscore');
 134  
 135          // Conditionally launch add field duration.
 136          if (!$dbman->field_exists($table, $field)) {
 137              $dbman->add_field($table, $field);
 138          }
 139  
 140          // Define field completion to be added to h5pactivity_attempts.
 141          $field = new xmldb_field('completion', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'duration');
 142  
 143          // Conditionally launch add field completion.
 144          if (!$dbman->field_exists($table, $field)) {
 145              $dbman->add_field($table, $field);
 146          }
 147  
 148          // Define field success to be added to h5pactivity_attempts.
 149          $field = new xmldb_field('success', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'completion');
 150  
 151          // Conditionally launch add field success.
 152          if (!$dbman->field_exists($table, $field)) {
 153              $dbman->add_field($table, $field);
 154          }
 155  
 156          // Define field duration to be added to h5pactivity_attempts_results.
 157          $table = new xmldb_table('h5pactivity_attempts_results');
 158          $field = new xmldb_field('duration', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'maxscore');
 159  
 160          // Conditionally launch add field duration.
 161          if (!$dbman->field_exists($table, $field)) {
 162              $dbman->add_field($table, $field);
 163          }
 164  
 165          // Define field completion to be added to h5pactivity_attempts_results.
 166          $field = new xmldb_field('completion', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'duration');
 167  
 168          // Conditionally launch add field completion.
 169          if (!$dbman->field_exists($table, $field)) {
 170              $dbman->add_field($table, $field);
 171          }
 172  
 173          // Define field success to be added to h5pactivity_attempts_results.
 174          $field = new xmldb_field('success', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'completion');
 175  
 176          // Conditionally launch add field success.
 177          if (!$dbman->field_exists($table, $field)) {
 178              $dbman->add_field($table, $field);
 179          }
 180  
 181          // H5pactivity savepoint reached.
 182          upgrade_mod_savepoint(true, 2020041400, 'h5pactivity');
 183      }
 184  
 185      if ($oldversion < 2020041401) {
 186  
 187          // Define field enabletracking to be added to h5pactivity.
 188          $table = new xmldb_table('h5pactivity');
 189          $field = new xmldb_field('enabletracking', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'displayoptions');
 190  
 191          // Conditionally launch add field enabletracking.
 192          if (!$dbman->field_exists($table, $field)) {
 193              $dbman->add_field($table, $field);
 194          }
 195  
 196          // Define field grademethod to be added to h5pactivity.
 197          $field = new xmldb_field('grademethod', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1', 'enabletracking');
 198  
 199          // Conditionally launch add field grademethod.
 200          if (!$dbman->field_exists($table, $field)) {
 201              $dbman->add_field($table, $field);
 202          }
 203  
 204          // Define field scaled to be added to h5pactivity_attempts.
 205          $table = new xmldb_table('h5pactivity_attempts');
 206          $field = new xmldb_field('scaled', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'maxscore');
 207  
 208          // Conditionally launch add field scaled.
 209          if (!$dbman->field_exists($table, $field)) {
 210              $dbman->add_field($table, $field);
 211          }
 212  
 213          // Calculate all scaled values from current attempts.
 214          $rs = $DB->get_recordset('h5pactivity_attempts');
 215          foreach ($rs as $record) {
 216              if (empty($record->maxscore)) {
 217                  continue;
 218              }
 219              $record->scaled = $record->rawscore / $record->maxscore;
 220              $DB->update_record('h5pactivity_attempts', $record);
 221          }
 222          $rs->close();
 223  
 224          // H5pactivity savepoint reached.
 225          upgrade_mod_savepoint(true, 2020041401, 'h5pactivity');
 226      }
 227  
 228      if ($oldversion < 2020042202) {
 229  
 230          // Define field reviewmode to be added to h5pactivity.
 231          $table = new xmldb_table('h5pactivity');
 232          $field = new xmldb_field('reviewmode', XMLDB_TYPE_INTEGER, '4', null, null, null, '1', 'grademethod');
 233  
 234          // Conditionally launch add field reviewmode.
 235          if (!$dbman->field_exists($table, $field)) {
 236              $dbman->add_field($table, $field);
 237          }
 238  
 239          // H5pactivity savepoint reached.
 240          upgrade_mod_savepoint(true, 2020042202, 'h5pactivity');
 241      }
 242  
 243      // Automatically generated Moodle v3.9.0 release upgrade line.
 244      // Put any upgrade step following this.
 245  
 246      // Automatically generated Moodle v4.0.0 release upgrade line.
 247      // Put any upgrade step following this.
 248  
 249      // Automatically generated Moodle v4.1.0 release upgrade line.
 250      // Put any upgrade step following this.
 251  
 252      if ($oldversion < 2022112801) {
 253  
 254          // Remove any orphaned attempt/result records (pointing to non-existing activities).
 255          $DB->delete_records_select('h5pactivity_attempts', 'NOT EXISTS (
 256              SELECT 1 FROM {h5pactivity} h5p WHERE h5p.id = {h5pactivity_attempts}.h5pactivityid
 257          )');
 258  
 259          $DB->delete_records_select('h5pactivity_attempts_results', 'NOT EXISTS (
 260              SELECT 1 FROM {h5pactivity_attempts} attempt WHERE attempt.id = {h5pactivity_attempts_results}.attemptid
 261          )');
 262  
 263          // H5pactivity savepoint reached.
 264          upgrade_mod_savepoint(true, 2022112801, 'h5pactivity');
 265      }
 266  
 267      return true;
 268  }