MDL-53832 enrol_lti: Separate tool launch and proxy registration logic
[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) {
40 global $CFG, $DB;
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);
52 $table->add_field('consumer_key256', XMLDB_TYPE_CHAR, '256', null, XMLDB_NOTNULL, null, null);
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);
64 $table->add_field('enable_from', XMLDB_TYPE_DATETIME, null, null, null, null, null);
65 $table->add_field('enable_until', XMLDB_TYPE_DATETIME, null, null, null, null, null);
66 $table->add_field('last_access', XMLDB_TYPE_DATETIME, null, null, null, null, null);
67 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
68 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
69
70 // Adding keys to table enrol_lti_lti2_consumer.
71 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
72
73 // Conditionally launch create table for enrol_lti_lti2_consumer.
74 if (!$dbman->table_exists($table)) {
75 $dbman->create_table($table);
76 }
77
78 // Define table enrol_lti_lti2_tool_proxy to be created.
79 $table = new xmldb_table('enrol_lti_lti2_tool_proxy');
80
81 // Adding fields to table enrol_lti_lti2_tool_proxy.
82 $table->add_field('tool_proxy_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
83 $table->add_field('tool_proxy_id', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
84 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
85 $table->add_field('tool_proxy', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
86 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
87 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
88
89 // Adding keys to table enrol_lti_lti2_tool_proxy.
90 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('tool_proxy_pk'));
91 $table->add_key('tool_proxy_id_uniq', XMLDB_KEY_UNIQUE, array('tool_proxy_id'));
92 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
93
94 // Conditionally launch create table for enrol_lti_lti2_tool_proxy.
95 if (!$dbman->table_exists($table)) {
96 $dbman->create_table($table);
97 }
98
99 // Define table enrol_lti_item to be created.
100 $table = new xmldb_table('enrol_lti_item');
101
102 // Adding fields to table enrol_lti_item.
103 $table->add_field('item_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
104 $table->add_field('resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
105 $table->add_field('item_title', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
106 $table->add_field('item_text', XMLDB_TYPE_TEXT, null, null, null, null, null);
107 $table->add_field('item_url', XMLDB_TYPE_CHAR, '200', null, null, null, null);
108 $table->add_field('max_rating', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '5');
109 $table->add_field('step', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
110 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
111 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
112 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
113
114 // Adding keys to table enrol_lti_item.
115 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('item_pk'));
da65fcf0
JO
116 $table->add_key(
117 'resource_link_pk',
118 XMLDB_KEY_FOREIGN,
119 array('resource_link_pk'),
120 'enrol_lti_lti2_resource_link',
121 array('id')
122 );
9161e715
JO
123
124 // Conditionally launch create table for enrol_lti_item.
125 if (!$dbman->table_exists($table)) {
126 $dbman->create_table($table);
127 }
128
129 // Define table enrol_lti_lti2_context to be created.
130 $table = new xmldb_table('enrol_lti_lti2_context');
131
132 // Adding fields to table enrol_lti_lti2_context.
133 $table->add_field('context_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
134 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
135 $table->add_field('lti_context_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
136 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
137 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
138 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
139
140 // Adding keys to table enrol_lti_lti2_context.
141 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('context_pk'));
142 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
143
144 // Conditionally launch create table for enrol_lti_lti2_context.
145 if (!$dbman->table_exists($table)) {
146 $dbman->create_table($table);
147 }
148
149 // Define table enrol_lti_lti2_nonce to be created.
150 $table = new xmldb_table('enrol_lti_lti2_nonce');
151
152 // Adding fields to table enrol_lti_lti2_nonce.
153 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
154 $table->add_field('value', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
155 $table->add_field('expires', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
156
157 // Adding keys to table enrol_lti_lti2_nonce.
158 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('consumer_pk', 'value'));
159 $table->add_key('consumer_pk', XMLDB_KEY_FOREIGN, array('consumer_pk'), 'enrol_lti_lti2_consumer', array('id'));
160
161 // Conditionally launch create table for enrol_lti_lti2_nonce.
162 if (!$dbman->table_exists($table)) {
163 $dbman->create_table($table);
164 }
165
166 // Define table enrol_lti_lti2_resource_link to be created.
167 $table = new xmldb_table('enrol_lti_lti2_resource_link');
168
169 // Adding fields to table enrol_lti_lti2_resource_link.
170 $table->add_field('id', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
171 $table->add_field('context_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
172 $table->add_field('consumer_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
173 $table->add_field('lti_resource_link_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
174 $table->add_field('settings', XMLDB_TYPE_TEXT, null, null, null, null, null);
175 $table->add_field('primary_resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
176 $table->add_field('share_approved', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
177 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
178 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
179
180 // Adding keys to table enrol_lti_lti2_resource_link.
181 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
182 $table->add_key('context_pk', XMLDB_KEY_FOREIGN, array('context_pk'), 'enrol_lti_lti2_context', array('context_pk'));
da65fcf0
JO
183 $table->add_key(
184 'primary_resource_link_pk',
185 XMLDB_KEY_FOREIGN,
186 array('primary_resource_link_pk'),
187 'enrol_lti_lti2_resource_link',
188 array('id')
189 );
9161e715
JO
190
191 // Adding indexes to table enrol_lti_lti2_resource_link.
192 $table->add_index('consumer_pk', XMLDB_INDEX_NOTUNIQUE, array('consumer_pk'));
193
194 // Conditionally launch create table for enrol_lti_lti2_resource_link.
195 if (!$dbman->table_exists($table)) {
196 $dbman->create_table($table);
197 }
198
199 // Define table enrol_lti_lti2_share_key to be created.
200 $table = new xmldb_table('enrol_lti_lti2_share_key');
201
202 // Adding fields to table enrol_lti_lti2_share_key.
203 $table->add_field('share_key_id', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
204 $table->add_field('resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
205 $table->add_field('auto_approve', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
206 $table->add_field('expires', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
207
208 // Adding keys to table enrol_lti_lti2_share_key.
209 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('share_key_id'));
da65fcf0
JO
210 $table->add_key(
211 'resource_link_pk',
212 XMLDB_KEY_FOREIGN,
213 array('resource_link_pk'),
214 'enrol_lti_lti2_resource_link',
215 array('id')
216 );
9161e715
JO
217
218 // Conditionally launch create table for enrol_lti_lti2_share_key.
219 if (!$dbman->table_exists($table)) {
220 $dbman->create_table($table);
221 }
222
223 // Define table enrol_lti_lti2_user_result to be created.
224 $table = new xmldb_table('enrol_lti_lti2_user_result');
225
226 // Adding fields to table enrol_lti_lti2_user_result.
227 $table->add_field('user_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
228 $table->add_field('resource_link_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
229 $table->add_field('lti_user_id', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
230 $table->add_field('lti_result_sourcedid', XMLDB_TYPE_CHAR, '1024', null, XMLDB_NOTNULL, null, null);
231 $table->add_field('created', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
232 $table->add_field('updated', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL, null, null);
233
234 // Adding keys to table enrol_lti_lti2_user_result.
235 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('user_pk'));
da65fcf0
JO
236 $table->add_key(
237 'resource_link_pk',
238 XMLDB_KEY_FOREIGN,
239 array('resource_link_pk'),
240 'enrol_lti_lti2_resource_link',
241 array('id')
242 );
9161e715
JO
243
244 // Conditionally launch create table for enrol_lti_lti2_user_result.
245 if (!$dbman->table_exists($table)) {
246 $dbman->create_table($table);
247 }
248
249 // Define table enrol_lti_rating to be created.
250 $table = new xmldb_table('enrol_lti_rating');
251
252 // Adding fields to table enrol_lti_rating.
253 $table->add_field('item_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
254 $table->add_field('user_pk', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
255 $table->add_field('rating', XMLDB_TYPE_NUMBER, '10, 2', null, XMLDB_NOTNULL, null, null);
256
257 // Adding keys to table enrol_lti_rating.
258 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('item_pk', 'user_pk'));
259 $table->add_key('item_pk', XMLDB_KEY_FOREIGN, array('item_pk'), 'enrol_lti_item', array('item_pk'));
260
261 // Conditionally launch create table for enrol_lti_rating.
262 if (!$dbman->table_exists($table)) {
263 $dbman->create_table($table);
264 }
265
266 // Lti savepoint reached.
267 upgrade_plugin_savepoint(true, 2016052303, 'enrol', 'lti');
268 }
269
270 return true;
271}