forked from jleyva/moodle-local_ltiprovider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.php
221 lines (191 loc) · 9.02 KB
/
tool.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
<?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/>.
/**
* Launch destination url. Main entry point for the external system.
*
* @package local
* @subpackage ltiprovider
* @copyright 2011 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->dirroot.'/local/ltiprovider/ims-blti/blti.php');
$toolid = required_param('id', PARAM_INT);
if (! ($tool = $DB->get_record('local_ltiprovider', array('id'=>$toolid)))) {
print_error('invalidtoolid', 'local_ltiprovider');
}
if ($tool->disabled) {
print_error('tooldisabled', 'local_ltiprovider');
}
// Do not set session, do not redirect
$context = new BLTI($tool->secret, false, false);
// Correct launch request
if ($context->valid) {
// Check that we can perform enrolments
if (enrol_is_enabled('manual')) {
$manual = enrol_get_plugin('manual');
} else {
print_error('nomanualenrol', 'local_ltiprovider');
}
// Transform to utf8 all the post and get data
$textlib = textlib_get_instance();
foreach ($_POST as $key => $value) {
$_POST[$key] = $textlib->convert($value, $tool->encoding);
}
foreach ($_GET as $key => $value) {
$_GET[$key] = $textlib->convert($value, $tool->encoding);
}
// We need an username without extended chars
$username = 'ltiprovider'.md5($context->getUserKey());
// Check if the user exists
$user = $DB->get_record('user', array('username' => $username));
if (! $user) {
$user = new stdClass();
// clean_param , email username text
$user->auth = 'nologin';
$user->username = $username;
$user->password = md5(uniqid(rand(), 1));
$user->firstname = optional_param('lis_person_name_given', '', PARAM_TEXT);
$user->lastname = optional_param('lis_person_name_family', '', PARAM_TEXT);
$user->email = clean_param($context->getUserEmail(), PARAM_EMAIL);
$user->city = $tool->city;
$user->country = $tool->country;
$user->institution = $tool->institution;
$user->timezone = $tool->timezone;
$user->maildisplay = $tool->maildisplay;
$user->lang = $tool->lang;
if (! $user->lang and isset($_POST['launch_presentation_locale'])) {
$user->lang = optional_param('launch_presentation_locale', '', PARAM_LANG);
}
if (! $user->lang) {
// TODO: This should be changed for detect the course lang
$user->lang = current_language();
}
$user->id = $DB->insert_record('user', $user);
// Reload full user
$user = $DB->get_record('user', array('id' => $user->id));
}
// Enrol user in course and activity if needed
if (! $context = $DB->get_record('context', array('id' => $tool->contextid))) {
print_error("invalidcontext");
}
if ($context->contextlevel == CONTEXT_COURSE) {
$courseid = $context->instanceid;
$urltogo = $CFG->wwwroot.'/course/view.php?id='.$courseid;
} else if ($context->contextlevel == CONTEXT_MODULE) {
$cmid = $context->instanceid;
$cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST);
$courseid = $cm->course;
$urltogo = $CFG->wwwroot.'/mod/'.$cm->modname.'/view.php?id='.$cm->id;
} else {
print_error("invalidcontext");
}
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
// Enrol the user in the course
$roles = explode(',', $_POST['roles']);
$role =(in_array('Instructor', $roles))? 'Instructor' : 'Learner';
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$timeend = 0;
if ($tool->enrolperiod) {
$timeend = $today + $tool->enrolperiod;
}
// Course role id for the Instructor or Learner
// TODO Do something with lti system admin (urn:lti:sysrole:ims/lis/Administrator)
$roleid = ($role == 'Instructor')? $tool->croleinst: $tool->crolelearn;
if ($instances = enrol_get_instances($course->id, false)) {
foreach ($instances as $instance) {
if ($instance->enrol === 'manual') {
// Check if the user enrolment exists
if (! $ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
// This means a new enrolment, so we have to check enroment starts and end limits and also max occupation
// First we check if there is a max enrolled limit
if($tool->maxenrolled) {
// TODO Improve this count because unenrolled users from Moodle admin panel are not sync with this table
if ($DB->count_records('local_ltiprovider_user', array('toolid'=>$tool->id)) > $tool->maxenrolled) {
// We do not use print_error for the iframe issue allowframembedding
echo get_string('maxenrolledreached', 'local_ltiprovider');
die;
}
}
$timenow = time();
if ($tool->enrolstartdate and $timenow < $tool->enrolstartdate) {
// We do not use print_error for the iframe issue allowframembedding
echo get_string('enrolmentnotstarted', 'local_ltiprovider');
die;
}
if ($tool->enrolenddate and $timenow > $tool->enrolenddate) {
// We do not use print_error for the iframe issue allowframembedding
echo get_string('enrolmentfinished', 'local_ltiprovider');
die;
}
// TODO, delete created users not enrolled
$manual->enrol_user($instance, $user->id, $roleid, $today, $timeend);
}
break;
}
}
}
if ($context->contextlevel == CONTEXT_MODULE) {
// Enrol the user in the activity
if (($tool->aroleinst and $role == 'Instructor') or ($tool->arolelearn and $role == 'Learner')) {
$roleid = ($role == 'Instructor')? $tool->aroleinst: $tool->arolelearn;
role_assign($roleid, $user->id, $tool->contextid);
}
}
// Login user
$sourceid = optional_param('lis_result_sourcedid', '', PARAM_RAW);
if ($userlog = $DB->get_record('local_ltiprovider_user', array('toolid' => $tool->id, 'userid' => $user->id))) {
$DB->set_field('local_ltiprovider_user', 'sourceid', $sourceid, array('id' => $userlog->id));
$DB->set_field('local_ltiprovider_user', 'lastaccess', time(), array('id' => $userlog->id));
} else {
// These data is needed for sending backup outcomes (aka grades)
$userlog = new stdClass();
$userlog->userid = $user->id;
$userlog->toolid = $tool->id;
// TODO Improve these checks
$userlog->serviceurl = optional_param('lis_outcome_service_url', '', PARAM_RAW);
$userlog->sourceid = $sourceid;
$userlog->consumerkey = optional_param('oauth_consumer_key', '', PARAM_RAW);
// TODO Do not store secret here
$userlog->consumersecret = $tool->secret;
$userlog->lastsync = 0;
$userlog->lastgrade = 0;
$userlog->lastaccess = time();
$DB->insert_record('local_ltiprovider_user', $userlog);
}
add_to_log(SITEID, 'user', 'login', $urltogo, "ltiprovider login", 0, $user->id);
$tool->context = $context;
$SESSION->ltiprovider = $tool;
complete_user_login($user);
// Moodle 2.2 and onwards
if (isset($CFG->allowframembedding) and !$CFG->allowframembedding) {
echo '<html>
<head>
</head>
<body onload="window.open(\''. $urltogo .'\', \'_blank\');"></body>';
echo get_string('newpopupnotice', 'local_ltiprovider');
$stropentool = get_string('opentool', 'local_ltiprovider');
echo "<p><a href=\"$urltogo\" target=\"_blank\">$stropentool</a></p>";
echo "<p>".get_string('allowframembedding', 'local_ltiprovider')."</p>";
echo '</html>';
} else {
redirect($urltogo);
}
} else {
//print_error('invalidcredentials', 'local_ltiprovider');
echo $context->message;
}