MDL-53832 enrol_lti: Map consumer to tool and handle instance deletion
[moodle.git] / enrol / lti / db / upgrade.php
CommitLineData
9161e715
JO
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 */
39function xmldb_enrol_lti_upgrade($oldversion) {
098ee549 40 global $DB;
9161e715
JO
41
42 $dbman = $DB->get_manager();
43
9161e715
JO
44 if ($oldversion < 2016052303) {
45
46 // Define table enrol_lti_lti2_consumer to be created.
47 $table = new xmldb_table('enrol_lti_lti2_consumer');
48
49 // Adding fields to table enrol_lti_lti2_consumer.
50 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
51 $table->add_field('name', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
098ee549 52 $table->add_field('consumer_key256', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
9161e715
JO
53 $table->add_field('consumer_key', XMLDB_TYPE_TEXT, null, null, null, null, null);
54 $table->add_field('secret', XMLDB_TYPE_CHAR, '1024', null, XMLDB_NOTNULL, null, null);
55 $table->add_field('lti_version', XMLDB_TYPE_CHAR, '10', null, null, null, null);
56 $table->add_field('consumer_name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
57 $table->add_field('consumer_version', XMLDB_TYPE_CHAR, '255', null, null, null, null);
58 $table->add_field('consumer_guid', XMLDB_TYPE_CHAR, '1024', null, null, null, null);
59 $table->add_field('profile', XMLDB_TYPE_TEXT, null, null, null, null, null);
60 $table->add_field('tool_proxy', XMLDB_TYPE_TEXT, null, null, null, null, null);
61 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
62 $table->add_field('protected', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
63 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
098ee549
JO
64 $table->add_field('enable_from', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
65 $table->add_field('enable_until', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
66 $table->add_field('last_access', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
67 $table->add_field('created', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
68 $table->add_field('updated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
69
70 // Adding keys to table enrol_lti_lti2_consumer.
71 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
72
098ee549
JO
73 // Adding indexes to table enrol_lti_lti2_consumer.
74 $table->add_index('consumer_key256_uniq', XMLDB_INDEX_UNIQUE, array('consumer_key256'));
75
9161e715
JO
76 // Conditionally launch create table for enrol_lti_lti2_consumer.
77 if (!$dbman->table_exists($table)) {
78 $dbman->create_table($table);
79 }
80
81 // Define table enrol_lti_lti2_tool_proxy to be created.
82 $table = new xmldb_table('enrol_lti_lti2_tool_proxy');
83
84 // Adding fields to table enrol_lti_lti2_tool_proxy.
098ee549 85 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
9161e715
JO
86 $table->add_field('tool_proxy_id', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
87 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
88 $table->add_field('tool_proxy', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
098ee549
JO
89 $table->add_field('created', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
90 $table->add_field('updated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
91
92 // Adding keys to table enrol_lti_lti2_tool_proxy.
098ee549 93 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
9161e715
JO
94 $table->add_key('tool_proxy_id_uniq', XMLDB_KEY_UNIQUE, array('tool_proxy_id'));
95 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
96
97 // Conditionally launch create table for enrol_lti_lti2_tool_proxy.
98 if (!$dbman->table_exists($table)) {
99 $dbman->create_table($table);
100 }
101
9161e715
JO
102 // Define table enrol_lti_lti2_context to be created.
103 $table = new xmldb_table('enrol_lti_lti2_context');
104
105 // Adding fields to table enrol_lti_lti2_context.
098ee549 106 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
9161e715
JO
107 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
108 $table->add_field('lti_context_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
109 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
098ee549
JO
110 $table->add_field('created', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
111 $table->add_field('updated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
112
113 // Adding keys to table enrol_lti_lti2_context.
098ee549 114 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
9161e715
JO
115 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
116
117 // Conditionally launch create table for enrol_lti_lti2_context.
118 if (!$dbman->table_exists($table)) {
119 $dbman->create_table($table);
120 }
121
122 // Define table enrol_lti_lti2_nonce to be created.
123 $table = new xmldb_table('enrol_lti_lti2_nonce');
124
125 // Adding fields to table enrol_lti_lti2_nonce.
098ee549 126 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
9161e715
JO
127 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
128 $table->add_field('value', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
098ee549 129 $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
130
131 // Adding keys to table enrol_lti_lti2_nonce.
098ee549 132 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6092bbc0 133 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
9161e715
JO
134
135 // Conditionally launch create table for enrol_lti_lti2_nonce.
136 if (!$dbman->table_exists($table)) {
137 $dbman->create_table($table);
138 }
139
140 // Define table enrol_lti_lti2_resource_link to be created.
141 $table = new xmldb_table('enrol_lti_lti2_resource_link');
142
143 // Adding fields to table enrol_lti_lti2_resource_link.
144 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
145 $table->add_field('context_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
146 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
147 $table->add_field('lti_resource_link_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
148 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
149 $table->add_field('primary_resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
150 $table->add_field('share_approved', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
098ee549
JO
151 $table->add_field('created', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
152 $table->add_field('updated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
153
154 // Adding keys to table enrol_lti_lti2_resource_link.
155 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
098ee549
JO
156 $table->add_key('context_pk', XMLDB_KEY_FOREIGN, array('context_pk'), 'enrol_lti_lti2_context', array('id'));
157 $table->add_key('primary_resource_link_pk', XMLDB_KEY_FOREIGN, array('primary_resource_link_pk'),
158 'enrol_lti_lti2_resource_link', array('id'));
159 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
9161e715
JO
160
161 // Conditionally launch create table for enrol_lti_lti2_resource_link.
162 if (!$dbman->table_exists($table)) {
163 $dbman->create_table($table);
164 }
165
166 // Define table enrol_lti_lti2_share_key to be created.
167 $table = new xmldb_table('enrol_lti_lti2_share_key');
168
169 // Adding fields to table enrol_lti_lti2_share_key.
098ee549 170 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
9161e715
JO
171 $table->add_field('share_key_id', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
172 $table->add_field('resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
173 $table->add_field('auto_approve', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
098ee549 174 $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
175
176 // Adding keys to table enrol_lti_lti2_share_key.
098ee549
JO
177 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
178 $table->add_key('share_key_id', XMLDB_KEY_UNIQUE, array('share_key_id'));
6092bbc0
JP
179 $table->add_key('resource_link_pk', XMLDB_KEY_FOREIGN_UNIQUE, array('resource_link_pk'),
180 'enrol_lti_lti2_resource_link', array('id'));
9161e715
JO
181
182 // Conditionally launch create table for enrol_lti_lti2_share_key.
183 if (!$dbman->table_exists($table)) {
184 $dbman->create_table($table);
185 }
186
187 // Define table enrol_lti_lti2_user_result to be created.
188 $table = new xmldb_table('enrol_lti_lti2_user_result');
189
190 // Adding fields to table enrol_lti_lti2_user_result.
098ee549 191 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
9161e715
JO
192 $table->add_field('resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
193 $table->add_field('lti_user_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
194 $table->add_field('lti_result_sourcedid', XMLDB_TYPE_CHAR, '1024', null, XMLDB_NOTNULL, null, null);
098ee549
JO
195 $table->add_field('created', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
196 $table->add_field('updated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
9161e715
JO
197
198 // Adding keys to table enrol_lti_lti2_user_result.
098ee549
JO
199 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
200 $table->add_key('resource_link_pk', XMLDB_KEY_FOREIGN, array('resource_link_pk'),
201 'enrol_lti_lti2_resource_link', array('id'));
9161e715
JO
202
203 // Conditionally launch create table for enrol_lti_lti2_user_result.
204 if (!$dbman->table_exists($table)) {
205 $dbman->create_table($table);
206 }
207
6092bbc0
JP
208 // Define table enrol_lti_tool_consumer_map to be created.
209 $table = new xmldb_table('enrol_lti_tool_consumer_map');
210
211 // Adding fields to table enrol_lti_tool_consumer_map.
212 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
213 $table->add_field('toolid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
214 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
215
216 // Adding keys to table enrol_lti_tool_consumer_map.
217 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
218 $table->add_key('toolid', XMLDB_KEY_FOREIGN, array('toolid'), 'enrol_lti_tools', array('id'));
219 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
220
221 // Conditionally launch create table for enrol_lti_tool_consumer_map.
222 if (!$dbman->table_exists($table)) {
223 $dbman->create_table($table);
224 }
225
9161e715
JO
226 // Lti savepoint reached.
227 upgrade_plugin_savepoint(true, 2016052303, 'enrol', 'lti');
228 }
229
230 return true;
231}