Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * tool_dataprivacy plugin upgrade code
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Function to upgrade tool_dataprivacy.
  29   *
  30   * @param int $oldversion the version we are upgrading from
  31   * @return bool result
  32   */
  33  function xmldb_tool_dataprivacy_upgrade($oldversion) {
  34      global $CFG, $DB;
  35  
  36      $dbman = $DB->get_manager();
  37  
  38      if ($oldversion < 2018051405) {
  39          // Define table tool_dataprivacy_ctxexpired to be created.
  40          $table = new xmldb_table('tool_dataprivacy_ctxexpired');
  41  
  42          // Adding fields to table tool_dataprivacy_ctxexpired.
  43          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  44          $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  45          $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
  46          $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  47          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  48          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  49  
  50          // Adding keys to table tool_dataprivacy_ctxexpired.
  51          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  52          $table->add_key('contextid', XMLDB_KEY_FOREIGN_UNIQUE, array('contextid'), 'context', array('id'));
  53  
  54          // Conditionally launch create table for tool_dataprivacy_ctxexpired.
  55          if (!$dbman->table_exists($table)) {
  56              $dbman->create_table($table);
  57          }
  58  
  59          // Define table tool_dataprivacy_contextlist to be created.
  60          $table = new xmldb_table('tool_dataprivacy_contextlist');
  61  
  62          // Adding fields to table tool_dataprivacy_contextlist.
  63          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  64          $table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
  65          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
  66          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
  67  
  68          // Adding keys to table tool_dataprivacy_contextlist.
  69          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  70  
  71          // Conditionally launch create table for tool_dataprivacy_contextlist.
  72          if (!$dbman->table_exists($table)) {
  73              $dbman->create_table($table);
  74          }
  75  
  76          // Define table tool_dataprivacy_ctxlst_ctx to be created.
  77          $table = new xmldb_table('tool_dataprivacy_ctxlst_ctx');
  78  
  79          // Adding fields to table tool_dataprivacy_ctxlst_ctx.
  80          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  81          $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  82          $table->add_field('contextlistid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  83          $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
  84          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
  85          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
  86  
  87          // Adding keys to table tool_dataprivacy_ctxlst_ctx.
  88          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  89          $table->add_key('contextlistid', XMLDB_KEY_FOREIGN, array('contextlistid'), 'tool_dataprivacy_contextlist', array('id'));
  90  
  91          // Conditionally launch create table for tool_dataprivacy_ctxlst_ctx.
  92          if (!$dbman->table_exists($table)) {
  93              $dbman->create_table($table);
  94          }
  95  
  96          // Define table tool_dataprivacy_rqst_ctxlst to be created.
  97          $table = new xmldb_table('tool_dataprivacy_rqst_ctxlst');
  98  
  99          // Adding fields to table tool_dataprivacy_rqst_ctxlst.
 100          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 101          $table->add_field('requestid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 102          $table->add_field('contextlistid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 103  
 104          // Adding keys to table tool_dataprivacy_rqst_ctxlst.
 105          $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
 106          $table->add_key('requestid', XMLDB_KEY_FOREIGN, array('requestid'), 'tool_dataprivacy_request', array('id'));
 107          $table->add_key('contextlistid', XMLDB_KEY_FOREIGN, array('contextlistid'), 'tool_dataprivacy_contextlist', array('id'));
 108          $table->add_key('request_contextlist', XMLDB_KEY_UNIQUE, array('requestid', 'contextlistid'));
 109  
 110          // Conditionally launch create table for tool_dataprivacy_rqst_ctxlst.
 111          if (!$dbman->table_exists($table)) {
 112              $dbman->create_table($table);
 113          }
 114  
 115          // Define field lawfulbases to be added to tool_dataprivacy_purpose.
 116          $table = new xmldb_table('tool_dataprivacy_purpose');
 117  
 118          // It is a required field. We initially define and add it as null and later update it to XMLDB_NOTNULL.
 119          $field = new xmldb_field('lawfulbases', XMLDB_TYPE_TEXT, null, null, null, null, null, 'descriptionformat');
 120  
 121          // Conditionally launch add field lawfulbases.
 122          if (!$dbman->field_exists($table, $field)) {
 123              $dbman->add_field($table, $field);
 124  
 125              // Set a kind-of-random value to lawfulbasis field.
 126              $DB->set_field('tool_dataprivacy_purpose', 'lawfulbases', 'gdpr_art_6_1_a');
 127  
 128              // We redefine it now as not null.
 129              $field = new xmldb_field('lawfulbases', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'descriptionformat');
 130  
 131              // Launch change of nullability for field lawfulbases.
 132              $dbman->change_field_notnull($table, $field);
 133          }
 134  
 135          // Define field sensitivedatareasons to be added to tool_dataprivacy_purpose.
 136          $table = new xmldb_table('tool_dataprivacy_purpose');
 137          $field = new xmldb_field('sensitivedatareasons', XMLDB_TYPE_TEXT, null, null, null, null, null, 'lawfulbases');
 138  
 139          // Conditionally launch add field sensitivedatareasons.
 140          if (!$dbman->field_exists($table, $field)) {
 141              $dbman->add_field($table, $field);
 142          }
 143  
 144          // Dataprivacy savepoint reached.
 145          upgrade_plugin_savepoint(true, 2018051405, 'tool', 'dataprivacy');
 146      }
 147  
 148      if ($oldversion < 2018051406) {
 149          // Update completed delete requests to new delete status.
 150          $query = "UPDATE {tool_dataprivacy_request}
 151                       SET status = :setstatus
 152                     WHERE type = :type
 153                           AND status = :wherestatus";
 154          $params = array(
 155              'setstatus' => 10, // Request deleted.
 156              'type' => 2, // Delete type.
 157              'wherestatus' => 5, // Request completed.
 158          );
 159  
 160          $DB->execute($query, $params);
 161  
 162          // Update completed data export requests to new download ready status.
 163          $params = array(
 164              'setstatus' => 8, // Request download ready.
 165              'type' => 1, // export type.
 166              'wherestatus' => 5, // Request completed.
 167          );
 168  
 169          $DB->execute($query, $params);
 170  
 171          upgrade_plugin_savepoint(true, 2018051406, 'tool', 'dataprivacy');
 172      }
 173  
 174      if ($oldversion < 2018082100) {
 175  
 176          // Changing precision of field status on table tool_dataprivacy_request to (2).
 177          $table = new xmldb_table('tool_dataprivacy_request');
 178          $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'requestedby');
 179  
 180          // Launch change of precision for field status.
 181          $dbman->change_field_precision($table, $field);
 182  
 183          // Dataprivacy savepoint reached.
 184          upgrade_plugin_savepoint(true, 2018082100, 'tool', 'dataprivacy');
 185      }
 186  
 187      if ($oldversion < 2018100401) {
 188          // Define table tool_dataprivacy_purposerole to be created.
 189          $table = new xmldb_table('tool_dataprivacy_purposerole');
 190  
 191          // Adding fields to table tool_dataprivacy_purposerole.
 192          $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
 193          $table->add_field('purposeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 194          $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 195          $table->add_field('lawfulbases', XMLDB_TYPE_TEXT, null, null, null, null, null);
 196          $table->add_field('sensitivedatareasons', XMLDB_TYPE_TEXT, null, null, null, null, null);
 197          $table->add_field('retentionperiod', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
 198          $table->add_field('protected', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
 199          $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 200          $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 201          $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
 202  
 203          // Adding keys to table tool_dataprivacy_purposerole.
 204          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 205          $table->add_key('purposepurposeid', XMLDB_KEY_FOREIGN, ['purposeid'], 'tool_dataprivacy_purpose', ['id']);
 206          $table->add_key('puproseroleid', XMLDB_KEY_FOREIGN, ['roleid'], 'role', ['id']);
 207  
 208          // Adding indexes to table tool_dataprivacy_purposerole.
 209          $table->add_index('purposerole', XMLDB_INDEX_UNIQUE, ['purposeid', 'roleid']);
 210  
 211          // Conditionally launch create table for tool_dataprivacy_purposerole.
 212          if (!$dbman->table_exists($table)) {
 213              $dbman->create_table($table);
 214          }
 215  
 216          // Update the ctxexpired table.
 217          $table = new xmldb_table('tool_dataprivacy_ctxexpired');
 218  
 219          // Add the unexpiredroles field.
 220          $field = new xmldb_field('unexpiredroles', XMLDB_TYPE_TEXT, null, null, null, null, null, 'contextid');
 221          if (!$dbman->field_exists($table, $field)) {
 222              $dbman->add_field($table, $field);
 223          }
 224          $DB->set_field('tool_dataprivacy_ctxexpired', 'unexpiredroles', '');
 225  
 226          // Add the expiredroles field.
 227          $field = new xmldb_field('expiredroles', XMLDB_TYPE_TEXT, null, null, null, null, null, 'unexpiredroles');
 228          if (!$dbman->field_exists($table, $field)) {
 229              $dbman->add_field($table, $field);
 230          }
 231          $DB->set_field('tool_dataprivacy_ctxexpired', 'expiredroles', '');
 232  
 233          // Add the defaultexpired field.
 234          $field = new xmldb_field('defaultexpired', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'expiredroles');
 235          if (!$dbman->field_exists($table, $field)) {
 236              $dbman->add_field($table, $field);
 237          }
 238  
 239          // Change the default for the expired field to be empty.
 240          $field = new xmldb_field('defaultexpired', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'expiredroles');
 241          $dbman->change_field_default($table, $field);
 242  
 243          // Prevent hte field from being nullable.
 244          $field = new xmldb_field('defaultexpired', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'expiredroles');
 245          $dbman->change_field_notnull($table, $field);
 246  
 247          // Dataprivacy savepoint reached.
 248          upgrade_plugin_savepoint(true, 2018100401, 'tool', 'dataprivacy');
 249      }
 250  
 251      if ($oldversion < 2018100406) {
 252          // Define field sensitivedatareasons to be added to tool_dataprivacy_purpose.
 253          $table = new xmldb_table('tool_dataprivacy_request');
 254          $field = new xmldb_field('creationmethod', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, 0, 'timemodified');
 255  
 256          // Conditionally launch add field sensitivedatareasons.
 257          if (!$dbman->field_exists($table, $field)) {
 258              $dbman->add_field($table, $field);
 259          }
 260  
 261          // Dataprivacy savepoint reached.
 262          upgrade_plugin_savepoint(true, 2018100406, 'tool', 'dataprivacy');
 263      }
 264  
 265  
 266      if ($oldversion < 2018110700) {
 267          // Define table tool_dataprivacy_ctxlst_ctx to be dropped.
 268          $table = new xmldb_table('tool_dataprivacy_ctxlst_ctx');
 269  
 270          // Conditionally launch drop table for tool_dataprivacy_ctxlst_ctx.
 271          if ($dbman->table_exists($table)) {
 272              $dbman->drop_table($table);
 273          }
 274  
 275          // Define table tool_dataprivacy_rqst_ctxlst to be dropped.
 276          $table = new xmldb_table('tool_dataprivacy_rqst_ctxlst');
 277  
 278          // Conditionally launch drop table for tool_dataprivacy_rqst_ctxlst.
 279          if ($dbman->table_exists($table)) {
 280              $dbman->drop_table($table);
 281          }
 282  
 283          // Define table tool_dataprivacy_contextlist to be dropped.
 284          $table = new xmldb_table('tool_dataprivacy_contextlist');
 285  
 286          // Conditionally launch drop table for tool_dataprivacy_contextlist.
 287          if ($dbman->table_exists($table)) {
 288              $dbman->drop_table($table);
 289          }
 290  
 291          // Update all requests which were in states Pending, or Pre-Processing, to Awaiting approval.
 292          $DB->set_field('tool_dataprivacy_request', 'status', 2, ['status' => 0]);
 293          $DB->set_field('tool_dataprivacy_request', 'status', 2, ['status' => 1]);
 294  
 295          // Remove the old initiate_data_request_task adhoc entries.
 296          $DB->delete_records('task_adhoc', ['classname' => '\tool_dataprivacy\task\initiate_data_request_task']);
 297  
 298          // Dataprivacy savepoint reached.
 299          upgrade_plugin_savepoint(true, 2018110700, 'tool', 'dataprivacy');
 300      }
 301  
 302      if ($oldversion < 2018112500) {
 303          // Delete orphaned data privacy requests.
 304          $sql = "SELECT r.id
 305                    FROM {tool_dataprivacy_request} r LEFT JOIN {user} u ON r.userid = u.id
 306                   WHERE u.id IS NULL";
 307          $orphaned = $DB->get_fieldset_sql($sql);
 308  
 309          if ($orphaned) {
 310              $DB->delete_records_list('tool_dataprivacy_request', 'id', $orphaned);
 311          }
 312  
 313          upgrade_plugin_savepoint(true, 2018112500, 'tool', 'dataprivacy');
 314      }
 315  
 316      // Automatically generated Moodle v3.6.0 release upgrade line.
 317      // Put any upgrade step following this.
 318  
 319      // Automatically generated Moodle v3.7.0 release upgrade line.
 320      // Put any upgrade step following this.
 321  
 322      // Automatically generated Moodle v3.8.0 release upgrade line.
 323      // Put any upgrade step following this.
 324  
 325      // Automatically generated Moodle v3.9.0 release upgrade line.
 326      // Put any upgrade step following this.
 327  
 328      if ($oldversion < 2020061501) {
 329  
 330          // Define field commentsformat to be added to tool_dataprivacy_request.
 331          $table = new xmldb_table('tool_dataprivacy_request');
 332          $field = new xmldb_field('commentsformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'comments');
 333  
 334          // Conditionally launch add field commentsformat.
 335          if (!$dbman->field_exists($table, $field)) {
 336              $dbman->add_field($table, $field);
 337          }
 338  
 339          // Define field dpocommentformat to be added to tool_dataprivacy_request.
 340          $field = new xmldb_field('dpocommentformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'dpocomment');
 341  
 342          // Conditionally launch add field dpocommentformat.
 343          if (!$dbman->field_exists($table, $field)) {
 344              $dbman->add_field($table, $field);
 345          }
 346  
 347          // Define field systemapproved to be added to tool_dataprivacy_request.
 348          $field = new xmldb_field('systemapproved', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'dpocommentformat');
 349  
 350          // Conditionally launch add field systemapproved.
 351          if (!$dbman->field_exists($table, $field)) {
 352              $dbman->add_field($table, $field);
 353          }
 354  
 355          // Dataprivacy savepoint reached.
 356          upgrade_plugin_savepoint(true, 2020061501, 'tool', 'dataprivacy');
 357      }
 358  
 359      // Automatically generated Moodle v3.10.0 release upgrade line.
 360      // Put any upgrade step following this.
 361  
 362      return true;
 363  }