MDL-55825 core_lib: LTI tool provider library fixes
[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);
d1e85a00
JP
52 $table->add_field('consumerkey256', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
53 $table->add_field('consumerkey', XMLDB_TYPE_TEXT, null, null, null, null, null);
9161e715 54 $table->add_field('secret', XMLDB_TYPE_CHAR, '1024', null, XMLDB_NOTNULL, null, null);
d1e85a00
JP
55 $table->add_field('ltiversion', XMLDB_TYPE_CHAR, '10', null, null, null, null);
56 $table->add_field('consumername', XMLDB_TYPE_CHAR, '255', null, null, null, null);
57 $table->add_field('consumerversion', XMLDB_TYPE_CHAR, '255', null, null, null, null);
58 $table->add_field('consumerguid', XMLDB_TYPE_CHAR, '1024', null, null, null, null);
9161e715 59 $table->add_field('profile', XMLDB_TYPE_TEXT, null, null, null, null, null);
d1e85a00 60 $table->add_field('toolproxy', XMLDB_TYPE_TEXT, null, null, null, null, null);
9161e715
JO
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);
d1e85a00
JP
64 $table->add_field('enablefrom', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
65 $table->add_field('enableuntil', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
66 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
098ee549
JO
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 73 // Adding indexes to table enrol_lti_lti2_consumer.
d1e85a00 74 $table->add_index('consumerkey256_uniq', XMLDB_INDEX_UNIQUE, array('consumerkey256'));
098ee549 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);
d1e85a00
JP
86 $table->add_field('toolproxykey', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
87 $table->add_field('consumerid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
88 $table->add_field('toolproxy', 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'));
d1e85a00
JP
94 $table->add_key('toolproxykey_uniq', XMLDB_KEY_UNIQUE, array('toolproxykey'));
95 $table->add_key('consumerid', XMLDB_KEY_FOREIGN, array('consumerid'), 'enrol_lti_lti2_consumer', array('id'));
9161e715
JO
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);
d1e85a00
JP
107 $table->add_field('consumerid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
108 $table->add_field('lticontextkey', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
9161e715 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'));
d1e85a00 115 $table->add_key('consumerid', XMLDB_KEY_FOREIGN, array('consumerid'), 'enrol_lti_lti2_consumer', array('id'));
9161e715
JO
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);
d1e85a00 127 $table->add_field('consumerid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
9161e715 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'));
d1e85a00 133 $table->add_key('consumerid', XMLDB_KEY_FOREIGN, array('consumerid'), '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);
d1e85a00
JP
145 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
146 $table->add_field('consumerid', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
147 $table->add_field('ltiresourcelinkkey', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
9161e715 148 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
d1e85a00
JP
149 $table->add_field('primaryresourcelinkid', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
150 $table->add_field('shareapproved', 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'));
d1e85a00
JP
156 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'enrol_lti_lti2_context', array('id'));
157 $table->add_key('primaryresourcelinkid', XMLDB_KEY_FOREIGN, array('primaryresourcelinkid'),
098ee549 158 'enrol_lti_lti2_resource_link', array('id'));
d1e85a00 159 $table->add_key('consumerid', XMLDB_KEY_FOREIGN, array('consumerid'), '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);
d1e85a00
JP
171 $table->add_field('sharekey', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
172 $table->add_field('resourcelinkid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
173 $table->add_field('autoapprove', 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 177 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
d1e85a00
JP
178 $table->add_key('sharekey', XMLDB_KEY_UNIQUE, array('sharekey'));
179 $table->add_key('resourcelinkid', XMLDB_KEY_FOREIGN_UNIQUE, array('resourcelinkid'),
6092bbc0 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);
d1e85a00
JP
192 $table->add_field('resourcelinkid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
193 $table->add_field('ltiuserkey', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
194 $table->add_field('ltiresultsourcedid', 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 199 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
d1e85a00 200 $table->add_key('resourcelinkid', XMLDB_KEY_FOREIGN, array('resourcelinkid'),
098ee549 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);
d1e85a00 214 $table->add_field('consumerid', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
6092bbc0
JP
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'));
d1e85a00 219 $table->add_key('consumerid', XMLDB_KEY_FOREIGN, array('consumerid'), 'enrol_lti_lti2_consumer', array('id'));
6092bbc0
JP
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}