2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * tool_dataprivacy plugin upgrade code
20 * @package tool_dataprivacy
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Function to upgrade tool_dataprivacy.
30 * @param int $oldversion the version we are upgrading from
33 function xmldb_tool_dataprivacy_upgrade($oldversion) {
36 $dbman = $DB->get_manager();
38 if ($oldversion < 2018051405) {
39 // Define table tool_dataprivacy_ctxexpired to be created.
40 $table = new xmldb_table('tool_dataprivacy_ctxexpired');
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);
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'));
54 // Conditionally launch create table for tool_dataprivacy_ctxexpired.
55 if (!$dbman->table_exists($table)) {
56 $dbman->create_table($table);
59 // Define table tool_dataprivacy_contextlist to be created.
60 $table = new xmldb_table('tool_dataprivacy_contextlist');
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');
68 // Adding keys to table tool_dataprivacy_contextlist.
69 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
71 // Conditionally launch create table for tool_dataprivacy_contextlist.
72 if (!$dbman->table_exists($table)) {
73 $dbman->create_table($table);
76 // Define table tool_dataprivacy_ctxlst_ctx to be created.
77 $table = new xmldb_table('tool_dataprivacy_ctxlst_ctx');
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');
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'));
91 // Conditionally launch create table for tool_dataprivacy_ctxlst_ctx.
92 if (!$dbman->table_exists($table)) {
93 $dbman->create_table($table);
96 // Define table tool_dataprivacy_rqst_ctxlst to be created.
97 $table = new xmldb_table('tool_dataprivacy_rqst_ctxlst');
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);
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'));
110 // Conditionally launch create table for tool_dataprivacy_rqst_ctxlst.
111 if (!$dbman->table_exists($table)) {
112 $dbman->create_table($table);
115 // Define field lawfulbases to be added to tool_dataprivacy_purpose.
116 $table = new xmldb_table('tool_dataprivacy_purpose');
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');
121 // Conditionally launch add field lawfulbases.
122 if (!$dbman->field_exists($table, $field)) {
123 $dbman->add_field($table, $field);
125 // Set a kind-of-random value to lawfulbasis field.
126 $DB->set_field('tool_dataprivacy_purpose', 'lawfulbases', 'gdpr_art_6_1_a');
128 // We redefine it now as not null.
129 $field = new xmldb_field('lawfulbases', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'descriptionformat');
131 // Launch change of nullability for field lawfulbases.
132 $dbman->change_field_notnull($table, $field);
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');
139 // Conditionally launch add field sensitivedatareasons.
140 if (!$dbman->field_exists($table, $field)) {
141 $dbman->add_field($table, $field);
144 // Dataprivacy savepoint reached.
145 upgrade_plugin_savepoint(true, 2018051405, 'tool', 'dataprivacy');