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