1 |
efrain |
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 |
// This file is part of BasicLTI4Moodle
|
|
|
18 |
//
|
|
|
19 |
// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
|
|
|
20 |
// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
|
|
|
21 |
// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
|
|
|
22 |
// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
|
|
|
23 |
// are already supporting or going to support BasicLTI. This project Implements the consumer
|
|
|
24 |
// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
|
|
|
25 |
// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
|
|
|
26 |
// at the GESSI research group at UPC.
|
|
|
27 |
// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
|
|
|
28 |
// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
|
|
|
29 |
// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
|
|
|
30 |
//
|
|
|
31 |
// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
|
|
|
32 |
// of the Universitat Politecnica de Catalunya http://www.upc.edu
|
|
|
33 |
// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* This file contains all the restore steps that will be used
|
|
|
37 |
* by the restore_lti_activity_task
|
|
|
38 |
*
|
|
|
39 |
* @package mod_lti
|
|
|
40 |
* @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
|
|
|
41 |
* marc.alier@upc.edu
|
|
|
42 |
* @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
|
|
|
43 |
* @author Marc Alier
|
|
|
44 |
* @author Jordi Piguillem
|
|
|
45 |
* @author Nikolas Galanis
|
|
|
46 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
47 |
*/
|
|
|
48 |
|
|
|
49 |
defined('MOODLE_INTERNAL') || die;
|
|
|
50 |
|
|
|
51 |
use mod_lti\local\ltiopenid\registration_helper;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Structure step to restore one lti activity
|
|
|
55 |
*/
|
|
|
56 |
class restore_lti_activity_structure_step extends restore_activity_structure_step {
|
|
|
57 |
|
|
|
58 |
/** @var bool */
|
|
|
59 |
protected $newltitype = false;
|
|
|
60 |
|
|
|
61 |
protected function define_structure() {
|
|
|
62 |
|
|
|
63 |
$paths = array();
|
|
|
64 |
// To know if we are including userinfo.
|
|
|
65 |
$userinfo = $this->get_setting_value('userinfo');
|
|
|
66 |
|
|
|
67 |
$lti = new restore_path_element('lti', '/activity/lti');
|
|
|
68 |
$paths[] = $lti;
|
|
|
69 |
$paths[] = new restore_path_element('ltitype', '/activity/lti/ltitype');
|
|
|
70 |
$paths[] = new restore_path_element('ltitypesconfig', '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfig');
|
|
|
71 |
$paths[] = new restore_path_element('ltitypesconfigencrypted',
|
|
|
72 |
'/activity/lti/ltitype/ltitypesconfigs/ltitypesconfigencrypted');
|
|
|
73 |
$paths[] = new restore_path_element('ltitoolproxy', '/activity/lti/ltitype/ltitoolproxy');
|
|
|
74 |
$paths[] = new restore_path_element('ltitoolsetting', '/activity/lti/ltitype/ltitoolproxy/ltitoolsettings/ltitoolsetting');
|
|
|
75 |
|
|
|
76 |
if ($userinfo) {
|
|
|
77 |
$submission = new restore_path_element('ltisubmission', '/activity/lti/ltisubmissions/ltisubmission');
|
|
|
78 |
$paths[] = $submission;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$paths[] = new restore_path_element('lticoursevisible', '/activity/lti/lticoursevisible');
|
|
|
82 |
|
|
|
83 |
// Add support for subplugin structures.
|
|
|
84 |
$this->add_subplugin_structure('ltisource', $lti);
|
|
|
85 |
$this->add_subplugin_structure('ltiservice', $lti);
|
|
|
86 |
|
|
|
87 |
// Return the paths wrapped into standard activity structure.
|
|
|
88 |
return $this->prepare_activity_structure($paths);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
protected function process_lti($data) {
|
|
|
92 |
global $DB;
|
|
|
93 |
|
|
|
94 |
$data = (object)$data;
|
|
|
95 |
$oldid = $data->id;
|
|
|
96 |
$data->course = $this->get_courseid();
|
|
|
97 |
$data->servicesalt = uniqid('', true);
|
|
|
98 |
|
|
|
99 |
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
|
|
|
100 |
// See MDL-9367.
|
|
|
101 |
|
|
|
102 |
// Grade used to be a float (whole numbers only), restore as int.
|
|
|
103 |
$data->grade = (int) $data->grade;
|
|
|
104 |
|
|
|
105 |
$data->typeid = 0;
|
|
|
106 |
|
|
|
107 |
// Try to decrypt resourcekey and password. Null if not possible (DB default).
|
|
|
108 |
// Note these fields were originally encrypted on backup using {link @encrypted_final_element}.
|
|
|
109 |
$data->resourcekey = isset($data->resourcekey) ? $this->decrypt($data->resourcekey) : null;
|
|
|
110 |
$data->password = isset($data->password) ? $this->decrypt($data->password) : null;
|
|
|
111 |
|
|
|
112 |
$newitemid = $DB->insert_record('lti', $data);
|
|
|
113 |
|
|
|
114 |
// Immediately after inserting "activity" record, call this.
|
|
|
115 |
$this->apply_activity_instance($newitemid);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Process an lti type restore
|
|
|
120 |
* @param mixed $data The data from backup XML file
|
|
|
121 |
* @return void
|
|
|
122 |
*/
|
|
|
123 |
protected function process_ltitype($data) {
|
|
|
124 |
global $DB, $USER;
|
|
|
125 |
|
|
|
126 |
$data = (object)$data;
|
|
|
127 |
$oldid = $data->id;
|
|
|
128 |
if (!empty($data->createdby)) {
|
|
|
129 |
$data->createdby = $this->get_mappingid('user', $data->createdby) ?: $USER->id;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
$courseid = $this->get_courseid();
|
|
|
133 |
$data->course = ($this->get_mappingid('course', $data->course) == $courseid) ? $courseid : SITEID;
|
|
|
134 |
|
|
|
135 |
// Try to find existing lti type with the same properties.
|
|
|
136 |
$ltitypeid = $this->find_existing_lti_type($data);
|
|
|
137 |
|
|
|
138 |
$this->newltitype = false;
|
|
|
139 |
if (!$ltitypeid && $data->course == $courseid) {
|
|
|
140 |
unset($data->toolproxyid); // Course tools can not use LTI2.
|
|
|
141 |
if (!empty($data->clientid)) {
|
|
|
142 |
// Need to rebuild clientid to ensure uniqueness.
|
|
|
143 |
$data->clientid = registration_helper::get()->new_clientid();
|
|
|
144 |
}
|
|
|
145 |
$ltitypeid = $DB->insert_record('lti_types', $data);
|
|
|
146 |
$this->newltitype = true;
|
|
|
147 |
$this->set_mapping('ltitype', $oldid, $ltitypeid);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Add the typeid entry back to LTI module.
|
|
|
151 |
$DB->update_record('lti', ['id' => $this->get_new_parentid('lti'), 'typeid' => $ltitypeid]);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Process an lti coursevisible restore
|
|
|
156 |
* @param mixed $data The data from backup XML file
|
|
|
157 |
* @return void
|
|
|
158 |
*/
|
|
|
159 |
protected function process_lticoursevisible($data) {
|
|
|
160 |
global $DB;
|
|
|
161 |
|
|
|
162 |
$data = (object)$data;
|
|
|
163 |
$data->typeid = $this->get_new_parentid('ltitype');
|
|
|
164 |
$data->courseid = $this->get_courseid();
|
|
|
165 |
|
|
|
166 |
if ($data->typeid) {
|
|
|
167 |
$DB->insert_record('lti_coursevisible', $data);
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Attempts to find existing record in lti_type
|
|
|
173 |
* @param stdClass $data
|
|
|
174 |
* @return int|null field lti_types.id or null if tool is not found
|
|
|
175 |
*/
|
|
|
176 |
protected function find_existing_lti_type($data) {
|
|
|
177 |
global $DB;
|
|
|
178 |
if ($ltitypeid = $this->get_mappingid('ltitype', $data->id)) {
|
|
|
179 |
return $ltitypeid;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$ltitype = null;
|
|
|
183 |
$params = (array)$data;
|
|
|
184 |
if ($this->task->is_samesite()) {
|
|
|
185 |
// If we are restoring on the same site try to find lti type with the same id.
|
|
|
186 |
$sql = 'id = :id AND course = :course';
|
|
|
187 |
$sql .= ($data->toolproxyid) ? ' AND toolproxyid = :toolproxyid' : ' AND toolproxyid IS NULL';
|
|
|
188 |
if ($DB->record_exists_select('lti_types', $sql, $params)) {
|
|
|
189 |
$this->set_mapping('ltitype', $data->id, $data->id);
|
|
|
190 |
if ($data->toolproxyid) {
|
|
|
191 |
$this->set_mapping('ltitoolproxy', $data->toolproxyid, $data->toolproxyid);
|
|
|
192 |
}
|
|
|
193 |
return $data->id;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
if ($data->course != $this->get_courseid()) {
|
|
|
198 |
// Site tools are not backed up and are not restored.
|
|
|
199 |
return null;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
// Now try to find the same type on the current site available in this course.
|
|
|
203 |
// Compare only fields baseurl, course and name, if they are the same we assume it is the same tool.
|
|
|
204 |
// LTI2 is not possible in the course so we add "lt.toolproxyid IS NULL" to the query.
|
|
|
205 |
$sql = 'SELECT id
|
|
|
206 |
FROM {lti_types}
|
|
|
207 |
WHERE ' . $DB->sql_compare_text('baseurl', 255) . ' = ' . $DB->sql_compare_text(':baseurl', 255) . ' AND
|
|
|
208 |
course = :course AND name = :name AND toolproxyid IS NULL';
|
|
|
209 |
if ($ltitype = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
|
|
|
210 |
$this->set_mapping('ltitype', $data->id, $ltitype->id);
|
|
|
211 |
return $ltitype->id;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
return null;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Process an lti config restore
|
|
|
219 |
* @param mixed $data The data from backup XML file
|
|
|
220 |
*/
|
|
|
221 |
protected function process_ltitypesconfig($data) {
|
|
|
222 |
global $DB;
|
|
|
223 |
|
|
|
224 |
$data = (object)$data;
|
|
|
225 |
$data->typeid = $this->get_new_parentid('ltitype');
|
|
|
226 |
|
|
|
227 |
// Only add configuration if the new lti_type was created.
|
|
|
228 |
if ($data->typeid && $this->newltitype) {
|
|
|
229 |
if ($data->name == 'servicesalt') {
|
|
|
230 |
$data->value = uniqid('', true);
|
|
|
231 |
}
|
|
|
232 |
$DB->insert_record('lti_types_config', $data);
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Process an lti config restore
|
|
|
238 |
* @param mixed $data The data from backup XML file
|
|
|
239 |
*/
|
|
|
240 |
protected function process_ltitypesconfigencrypted($data) {
|
|
|
241 |
global $DB;
|
|
|
242 |
|
|
|
243 |
$data = (object)$data;
|
|
|
244 |
$data->typeid = $this->get_new_parentid('ltitype');
|
|
|
245 |
|
|
|
246 |
// Only add configuration if the new lti_type was created.
|
|
|
247 |
if ($data->typeid && $this->newltitype) {
|
|
|
248 |
$data->value = $this->decrypt($data->value);
|
|
|
249 |
if (!is_null($data->value)) {
|
|
|
250 |
$DB->insert_record('lti_types_config', $data);
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Process a restore of LTI tool registration
|
|
|
257 |
* This method is empty because we actually process registration as part of process_ltitype()
|
|
|
258 |
* @param mixed $data The data from backup XML file
|
|
|
259 |
*/
|
|
|
260 |
protected function process_ltitoolproxy($data) {
|
|
|
261 |
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* Process an lti tool registration settings restore (only settings for the current activity)
|
|
|
266 |
* @param mixed $data The data from backup XML file
|
|
|
267 |
*/
|
|
|
268 |
protected function process_ltitoolsetting($data) {
|
|
|
269 |
global $DB;
|
|
|
270 |
|
|
|
271 |
$data = (object)$data;
|
|
|
272 |
$data->toolproxyid = $this->get_new_parentid('ltitoolproxy');
|
|
|
273 |
|
|
|
274 |
if (!$data->toolproxyid) {
|
|
|
275 |
return;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
$data->course = $this->get_courseid();
|
|
|
279 |
$data->coursemoduleid = $this->task->get_moduleid();
|
|
|
280 |
$DB->insert_record('lti_tool_settings', $data);
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Process a submission restore
|
|
|
285 |
* @param mixed $data The data from backup XML file
|
|
|
286 |
*/
|
|
|
287 |
protected function process_ltisubmission($data) {
|
|
|
288 |
global $DB;
|
|
|
289 |
|
|
|
290 |
$data = (object)$data;
|
|
|
291 |
$oldid = $data->id;
|
|
|
292 |
|
|
|
293 |
$data->ltiid = $this->get_new_parentid('lti');
|
|
|
294 |
|
|
|
295 |
$data->datesubmitted = $this->apply_date_offset($data->datesubmitted);
|
|
|
296 |
$data->dateupdated = $this->apply_date_offset($data->dateupdated);
|
|
|
297 |
if ($data->userid > 0) {
|
|
|
298 |
$data->userid = $this->get_mappingid('user', $data->userid);
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
$newitemid = $DB->insert_record('lti_submission', $data);
|
|
|
302 |
|
|
|
303 |
$this->set_mapping('ltisubmission', $oldid, $newitemid);
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
protected function after_execute() {
|
|
|
307 |
// Add lti related files, no need to match by itemname (just internally handled context).
|
|
|
308 |
$this->add_related_files('mod_lti', 'intro', null);
|
|
|
309 |
}
|
|
|
310 |
}
|