-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathservices.php
153 lines (124 loc) · 5.95 KB
/
services.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
<?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/>.
/**
* Main entry for extra services request.
*
* @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/locallib.php');
require_once($CFG->dirroot.'/local/ltiprovider/ims-blti/blti.php');
$service = required_param('custom_service', PARAM_RAW_TRIMMED);
$toolid = optional_param('id', 0, PARAM_INT);
$lticontextid = optional_param('context_id', false, PARAM_RAW);
if (isset($newinfo['custom_lti_message_encoded_base64']) and $newinfo['custom_lti_message_encoded_base64'] == 1) {
$lticontextid = base64_decode($lticontextid);
$service = base64_decode($service);
}
if (!$toolid and $lticontextid) {
// Check if there is more that one course for this LTI context id.
if ($DB->count_records('course', array('idnumber' => $lticontextid)) > 1) {
print_error('cantdeterminecontext', 'local_ltiprovider');
}
if ($course = $DB->get_record('course', array('idnumber' => $lticontextid))) {
// Look for a course created for this LTI context id.
if ($coursecontext = context_course::instance($course->id)) {
if ($DB->count_records('local_ltiprovider', array('contextid' => $coursecontext->id)) > 1) {
print_error('cantdeterminecontext', 'local_ltiprovider');
}
$toolid = $DB->get_field('local_ltiprovider', 'id', array('contextid' => $coursecontext->id));
}
}
}
$secret = '';
// If we dont receive a request for a specific tool, we use the global shared secret.
if ($tool = $DB->get_record('local_ltiprovider', array('id' => $toolid))) {
if ($tool->disabled) {
print_error('tooldisabled', 'local_ltiprovider');
}
$secret = $tool->secret;
} else {
$secret = get_config('local_ltiprovider', 'globalsharedsecret');
}
if (!$secret) {
print_error('invalidtoolid', 'local_ltiprovider');
}
// Do not set session, do not redirect.
$context = new BLTI($secret, false, false);
// Correct launch request.
if ($context->valid) {
set_time_limit(0);
raise_memory_limit(MEMORY_EXTRA);
// Are we creating a new context (that means a new course tool)?
if ($service == 'create_context') {
$custom_context_template = $context->info['custom_context_template'];
if (!$tplcourse = $DB->get_record('course', array('idnumber' => $custom_context_template), '*', IGNORE_MULTIPLE)) {
print_error('invalidtplcourse', 'local_ltiprovider');
}
require_once("$CFG->dirroot/course/lib.php");
$newcourse = new stdClass();
$newcourse->fullname = local_ltiprovider_get_new_course_info('fullname', $context);
$newcourse->shortname = local_ltiprovider_get_new_course_info('shortname', $context);
$newcourse->idnumber = local_ltiprovider_get_new_course_info('idnumber', $context);
$categories = $DB->get_records('course_categories', null, '', 'id', 0, 1);
$category = array_shift($categories);
$newcourse->category = $category->id;
$course = create_course($newcourse);
$coursecontext = context_course::instance($course->id);
// Create the tool that provide the full course.
$tool = local_ltiprovider_create_tool($course->id, $coursecontext->id, $context);
$username = local_ltiprovider_create_username($context->info['oauth_consumer_key'], $context->info['user_id']);
$userrestoringid = $DB->get_field('user', 'id', array('username' => $username));
// Duplicate course + users.
$course = local_ltiprovider_duplicate_course($tplcourse->id, $course, 1,
$options = array(array('name' => 'users',
'value' => 1)), $userrestoringid, $context);
echo json_encode($course);
} else if ($service == 'duplicate_resource') {
$idnumber = $context->info['custom_resource_link_copy_id'];
$resource_link_id = $context->info['resource_link_id'];
if (!$tool) {
print_error('missingrequiredtool', 'local_ltiprovider');
}
if (! $context = $DB->get_record('context', array('id' => $tool->contextid))) {
print_error("invalidcontext");
}
if ($context->contextlevel != CONTEXT_COURSE) {
print_error('invalidtypetool', 'local_ltiprovider');
}
if (!$cm = $DB->get_record('course_modules', array('idnumber' => $idnumber), '*', IGNORE_MULTIPLE)) {
print_error('invalidresourcecopyid', 'local_ltiprovider');
}
$courseid = $context->instanceid;
$cmid = local_ltiprovider_duplicate_module($cm->id, $courseid, $resource_link_id, $context);
if ($cm = get_coursemodule_from_id(false, $cmid)) {
echo json_encode($cm);
}
} else if ($service == 'force_logout') {
// Force logout.
$authsequence = get_enabled_auth_plugins();
foreach ($authsequence as $authname) {
$authplugin = get_auth_plugin($authname);
$authplugin->logoutpage_hook();
}
require_logout();
}
} else {
echo $context->message;
}