-
Notifications
You must be signed in to change notification settings - Fork 2
/
tooltemplate.php
295 lines (251 loc) · 10.8 KB
/
tooltemplate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The main entry point for the external system.
*
* @package enrol_lticoursetemplate
* @copyright 2016 Mark Nelson <markn@moodle.com> 2017 Arek Juszczyk <arek.juszczyk@ed.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot . '/user/lib.php');
require_once($CFG->dirroot . '/enrol/lticoursetemplate/ims-blti/blti.php');
$toolid = required_param('id', PARAM_INT);
// Get the tool.
$oldtool = \enrol_lticoursetemplate\helper::get_lti_tool($toolid);
// Create the BLTI request.
$ltirequest = new BLTI($oldtool->secret, false, false);
// Correct launch request.
if ($ltirequest->valid) {
// Get the new tool.
$tool = \enrol_lticoursetemplate\helper::get_lti_new_tool(
$toolid,
$ltirequest->info['oauth_consumer_key'],
$ltirequest->info['context_id'],
$ltirequest->info['context_title'],
$ltirequest->isInstructor()
);
// Log lti request data.
$event = \enrol_lticoursetemplate\event\lticonnection_launched::create(array(
'objectid' => 0,
'context' => context::instance_by_id($tool->contextid),
'other' => (array) $ltirequest
));
$event->trigger();
// Check if the authentication plugin is disabled.
if (!is_enabled_auth('lti')) {
print_error('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
exit();
}
// Check if the enrolment plugin is disabled.
if (!enrol_is_enabled('lticoursetemplate')) {
print_error('enrolisdisabled', 'enrol_lticoursetemplate');
exit();
}
// Check if the enrolment instance is disabled.
if ($tool->status != ENROL_INSTANCE_ENABLED) {
print_error('enrolisdisabled', 'enrol_lticoursetemplate');
exit();
}
// Before we do anything check that the context is valid.
$context = context::instance_by_id($tool->contextid);
// Set the user data.
$user = new stdClass();
$user->username = \enrol_lticoursetemplate\helper::create_username(
$ltirequest->info['oauth_consumer_key'],
$ltirequest->info['user_id']
);
// Log generated user name.
$event = \enrol_lticoursetemplate\event\ltiname_created::create(array(
'objectid' => 0,
'context' => context::instance_by_id($tool->contextid),
'other' => array(
'username' => $user->username,
'oauth_consumer_key' => $ltirequest->info['oauth_consumer_key'],
'user_id' => $ltirequest->info['user_id'],
)
));
$event->trigger();
if (!empty($ltirequest->info['lis_person_name_given'])) {
$user->firstname = $ltirequest->info['lis_person_name_given'];
} else {
$user->firstname = $ltirequest->info['user_id'];
}
if (!empty($ltirequest->info['lis_person_name_family'])) {
$user->lastname = $ltirequest->info['lis_person_name_family'];
} else {
$user->lastname = $ltirequest->info['context_id'];
}
$user->email = \core_user::clean_field($ltirequest->getUserEmail(), 'email');
// Get the user data from the LTI consumer.
$user = \enrol_lticoursetemplate\helper::assign_user_tool_data($oldtool, $user);
// Check if the user exists.
if (!$dbuser = $DB->get_record('user', array('username' => $user->username, 'deleted' => 0))) {
// If the email was stripped/not set then fill it with a default one. This
// stops the user from being redirected to edit their profile page.
if (empty($user->email)) {
$user->email = $user->username . "@example.com";
}
$user->auth = 'lti';
$user->id = user_create_user($user);
// Get the updated user record.
$user = $DB->get_record('user', array('id' => $user->id));
// Log created user.
$event = \enrol_lticoursetemplate\event\ltiuser_created::create(array(
'objectid' => 0,
'context' => context::instance_by_id($tool->contextid),
'other' => array(
'username' => $user->username,
'email' => $user->email,
)
));
$event->trigger();
} else {
$event = \enrol_lticoursetemplate\event\dbuser_retrieved::create(array(
'objectid' => 0,
'context' => context::instance_by_id($tool->contextid),
'other' => (array) $dbuser
));
$event->trigger();
if ($dbuser->suspended) {
// Log suspended users access try.
$event = \enrol_lticoursetemplate\event\ltiuser_suspended::create(array(
'objectid' => $dbuser->id,
'context' => context::instance_by_id($tool->contextid),
'other' => (array) $dbuser
));
$event->trigger();
throw new moodle_exception('useraccountsuspended', 'enrol_lticoursetemplate');
exit();
}
if (\enrol_lticoursetemplate\helper::user_match($user, $dbuser)) {
$user = $dbuser;
// Log matched users.
$event = \enrol_lticoursetemplate\event\ltiuser_matched::create(array(
'objectid' => 0,
'context' => context::instance_by_id($tool->contextid),
'other' => array(
'username' => $user->username,
'db_username' => $dbuser->username,
)
));
$event->trigger();
} else {
// If email is empty remove it, so we don't update the user with an empty email.
if (empty($user->email)) {
unset($user->email);
}
$user->id = $dbuser->id;
user_update_user($user);
// Get the updated user record.
$user = $DB->get_record('user', array('id' => $user->id));
}
}
// Update user image.
$image = false;
if (!empty($ltirequest->info['user_image'])) {
$image = $ltirequest->info['user_image'];
} else if (!empty($ltirequest->info['custom_user_image'])) {
$image = $ltirequest->info['custom_user_image'];
}
// Check if there is an image to process.
if ($image) {
\enrol_lticoursetemplate\helper::update_user_profile_image($user->id, $image);
}
// Check if we are an instructor.
$isinstructor = $ltirequest->isInstructor();
if ($context->contextlevel == CONTEXT_COURSE) {
$courseid = $context->instanceid;
$urltogo = new moodle_url('/course/view.php', array('id' => $courseid));
// May still be set from previous session, so unset it.
unset($SESSION->forcepagelayout);
} else if ($context->contextlevel == CONTEXT_MODULE) {
$cmid = $context->instanceid;
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
$urltogo = new moodle_url('/mod/' . $cm->modname . '/view.php', array('id' => $cm->id));
// If we are a student in the course module context we do not want to display blocks.
if (!$isinstructor) {
// Force the page layout.
$SESSION->forcepagelayout = 'embedded';
} else {
// May still be set from previous session, so unset it.
unset($SESSION->forcepagelayout);
}
} else {
print_error('invalidcontext');
exit();
}
// Enrol the user in the course with no role.
$result = \enrol_lticoursetemplate\helper::enrol_user($tool, $user->id);
// Display an error, if there is one.
if ($result !== \enrol_lticoursetemplate\helper::ENROLMENT_SUCCESSFUL) {
print_error($result, 'enrol_lticoursetemplate');
exit();
}
// Give the user the role in the given context.
$roleid = $isinstructor ? $tool->roleinstructor : $tool->rolelearner;
role_assign($roleid, $user->id, $tool->contextid);
// Login user.
$sourceid = (!empty($ltirequest->info['lis_result_sourcedid'])) ? $ltirequest->info['lis_result_sourcedid'] : '';
$serviceurl = (!empty($ltirequest->info['lis_outcome_service_url'])) ? $ltirequest->info['lis_outcome_service_url'] : '';
// Check if we have recorded this user before.
if ($userlog = $DB->get_record('enrol_lti_ct_users', array('toolid' => $tool->id, 'userid' => $user->id))) {
if ($userlog->sourceid != $sourceid) {
$userlog->sourceid = $sourceid;
}
if ($userlog->serviceurl != $serviceurl) {
$userlog->serviceurl = $serviceurl;
}
$userlog->lastaccess = time();
$DB->update_record('enrol_lti_ct_users', $userlog);
} else {
// Add the user details so we can use it later when syncing grades and members.
$userlog = new stdClass();
$userlog->userid = $user->id;
$userlog->toolid = $tool->id;
$userlog->serviceurl = $serviceurl;
$userlog->sourceid = $sourceid;
$userlog->consumerkey = $ltirequest->info['oauth_consumer_key'];
$userlog->consumersecret = $tool->secret;
$userlog->lastgrade = 0;
$userlog->lastaccess = time();
$userlog->timecreated = time();
if (!empty($ltirequest->info['ext_ims_lis_memberships_url'])) {
$userlog->membershipsurl = $ltirequest->info['ext_ims_lis_memberships_url'];
} else {
$userlog->membershipsurl = '';
}
if (!empty($ltirequest->info['ext_ims_lis_memberships_id'])) {
$userlog->membershipsid = $ltirequest->info['ext_ims_lis_memberships_id'];
} else {
$userlog->membershipsid = '';
}
$DB->insert_record('enrol_lti_ct_users', $userlog);
}
// Finalise the user log in.
complete_user_login($user);
if (empty($CFG->allowframembedding)) {
// Provide an alternative link.
$stropentool = get_string('opentool', 'enrol_lticoursetemplate');
echo html_writer::tag('p', get_string('frameembeddingnotenabled', 'enrol_lticoursetemplate'));
echo html_writer::link($urltogo, $stropentool, array('target' => '_blank'));
} else {
// All done, redirect the user to where they want to go.
redirect($urltogo);
}
} else {
echo $ltirequest->message;
}