MDL-52154 mod_lti: Add cartridge support to LTI
[moodle.git] / mod / lti / lib.php
CommitLineData
aa6eca66 1<?php
61eb12d4
CS
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//
aa6eca66
CS
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
e3f69b58 33// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
aa6eca66
CS
34
35/**
61eb12d4 36 * This file contains a library of functions and constants for the lti module
aa6eca66 37 *
2b17ec3d 38 * @package mod_lti
61eb12d4 39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
aa6eca66 40 * marc.alier@upc.edu
61eb12d4
CS
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
8f45215d 45 * @author Chris Scribner
61eb12d4 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa6eca66
CS
47 */
48
49defined('MOODLE_INTERNAL') || die;
50
06ca6cc9
TH
51/**
52 * Returns all other caps used in module.
53 *
54 * @return array
55 */
56function lti_get_extra_capabilities() {
57 return array('moodle/site:accessallgroups');
58}
59
aa6eca66
CS
60/**
61 * List of features supported in URL module
62 * @param string $feature FEATURE_xx constant for requested feature
63 * @return mixed True if module supports feature, false if not, null if doesn't know
64 */
65function lti_supports($feature) {
e3f69b58 66 switch ($feature) {
67 case FEATURE_GROUPS:
68 case FEATURE_GROUPINGS:
69 return false;
70 case FEATURE_MOD_INTRO:
71 case FEATURE_COMPLETION_TRACKS_VIEWS:
72 case FEATURE_GRADE_HAS_GRADE:
73 case FEATURE_GRADE_OUTCOMES:
74 case FEATURE_BACKUP_MOODLE2:
75 case FEATURE_SHOW_DESCRIPTION:
76 return true;
77
78 default:
79 return null;
aa6eca66
CS
80 }
81}
82
83/**
84 * Given an object containing all the necessary data,
85 * (defined by the form in mod.html) this function
86 * will create a new instance and return the id number
87 * of the new instance.
88 *
89 * @param object $instance An object from the form in mod.html
90 * @return int The id of the newly inserted basiclti record
91 **/
c69bd1af 92function lti_add_instance($lti, $mform) {
42a2c7f1
CS
93 global $DB, $CFG;
94 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 95
e3f69b58 96 if (!isset($lti->toolurl)) {
97 $lti->toolurl = '';
98 }
99
8aec804a
JO
100 lti_load_tool_if_cartridge($lti);
101
c69bd1af
EL
102 $lti->timecreated = time();
103 $lti->timemodified = $lti->timecreated;
104 $lti->servicesalt = uniqid('', true);
e27cb316 105
8fa50fdd
MN
106 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
107
5582fe49 108 if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
ea5d0515
AF
109 $lti->typeid = $lti->urlmatchedtypeid;
110 }
111
8fa50fdd
MN
112 if (!isset($lti->instructorchoiceacceptgrades) || $lti->instructorchoiceacceptgrades != LTI_SETTING_ALWAYS) {
113 // The instance does not accept grades back from the provider, so set to "No grade" value 0.
114 $lti->grade = 0;
c69bd1af 115 }
aa6eca66 116
c69bd1af 117 $lti->id = $DB->insert_record('lti', $lti);
e27cb316 118
8fa50fdd 119 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
c69bd1af
EL
120 if (!isset($lti->cmidnumber)) {
121 $lti->cmidnumber = '';
32c079dc 122 }
e27cb316 123
c69bd1af 124 lti_grade_item_update($lti);
aa6eca66
CS
125 }
126
c69bd1af 127 return $lti->id;
aa6eca66
CS
128}
129
130/**
131 * Given an object containing all the necessary data,
132 * (defined by the form in mod.html) this function
133 * will update an existing instance with new data.
134 *
135 * @param object $instance An object from the form in mod.html
136 * @return boolean Success/Fail
137 **/
c69bd1af 138function lti_update_instance($lti, $mform) {
42a2c7f1
CS
139 global $DB, $CFG;
140 require_once($CFG->dirroot.'/mod/lti/locallib.php');
aa6eca66 141
8aec804a
JO
142 lti_load_tool_if_cartridge($lti);
143
c69bd1af
EL
144 $lti->timemodified = time();
145 $lti->id = $lti->instance;
aa6eca66 146
b07878ec
CS
147 if (!isset($lti->showtitlelaunch)) {
148 $lti->showtitlelaunch = 0;
aa6eca66 149 }
e27cb316 150
b07878ec
CS
151 if (!isset($lti->showdescriptionlaunch)) {
152 $lti->showdescriptionlaunch = 0;
aa6eca66 153 }
e27cb316 154
8fa50fdd 155 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
e27cb316 156
8fa50fdd 157 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
c69bd1af 158 lti_grade_item_update($lti);
aa6eca66 159 } else {
8fa50fdd
MN
160 // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
161 $lti->grade = 0;
162 $lti->instructorchoiceacceptgrades = 0;
163
c69bd1af 164 lti_grade_item_delete($lti);
aa6eca66
CS
165 }
166
ea5d0515
AF
167 if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
168 $lti->typeid = $lti->urlmatchedtypeid;
169 }
170
c69bd1af 171 return $DB->update_record('lti', $lti);
aa6eca66
CS
172}
173
174/**
175 * Given an ID of an instance of this module,
176 * this function will permanently delete the instance
177 * and any data that depends on it.
178 *
179 * @param int $id Id of the module instance
180 * @return boolean Success/Failure
181 **/
182function lti_delete_instance($id) {
183 global $DB;
184
185 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
186 return false;
187 }
188
189 $result = true;
190
e3f69b58 191 // Delete any dependent records here.
aa6eca66
CS
192 lti_grade_item_delete($basiclti);
193
e3f69b58 194 $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
496bf3ea
DM
195 if ($ltitype) {
196 $DB->delete_records('lti_tool_settings',
197 array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
198 }
e3f69b58 199
aa6eca66
CS
200 return $DB->delete_records("lti", array("id" => $basiclti->id));
201}
202
01e8bfd7
JO
203/**
204 * Return aliases of this activity. LTI should have an alias for each configured tool type
2348c137 205 * This is so you can add an external tool types directly to the activity chooser
01e8bfd7 206 *
2348c137
MG
207 * @param stdClass $defaultitem default item that would be added to the activity chooser if this callback was not present.
208 * It has properties: archetype, name, title, help, icon, link
209 * @return array An array of aliases for this activity. Each element is an object with same list of properties as $defaultitem.
210 * Properties title and link are required
01e8bfd7 211 **/
2348c137 212function lti_get_shortcuts($defaultitem) {
01e8bfd7
JO
213 global $CFG, $COURSE;
214 require_once($CFG->dirroot.'/mod/lti/locallib.php');
215
2348c137
MG
216 $types = lti_get_configured_types($COURSE->id, $defaultitem->link->param('sr'));
217 $types[] = $defaultitem;
218
219 // Add items defined in ltisource plugins.
220 foreach (core_component::get_plugin_list('ltisource') as $pluginname => $dir) {
221 if ($moretypes = component_callback("ltisource_$pluginname", 'get_types')) {
222 // Callback 'get_types()' in 'ltisource' plugins is deprecated in 3.1 and will be removed in 3.5, TODO MDL-53697.
223 debugging('Deprecated callback get_types() is found in ltisource_' . $pluginname .
224 ', use get_shortcuts() instead', DEBUG_DEVELOPER);
225 $grouptitle = get_string('modulenameplural', 'mod_lti');
226 foreach ($moretypes as $subtype) {
227 // Instead of adding subitems combine the name of the group with the name of the subtype.
228 $subtype->title = get_string('activitytypetitle', '',
229 (object)['activity' => $grouptitle, 'type' => $subtype->typestr]);
230 // Re-implement the logic of get_module_metadata() in Moodle 3.0 and below for converting
231 // subtypes into items in activity chooser.
232 $subtype->type = str_replace('&amp;', '&', $subtype->type);
233 $subtype->name = preg_replace('/.*type=/', '', $subtype->type);
234 $subtype->link = new moodle_url($defaultitem->link, array('type' => $subtype->name));
235 if (empty($subtype->help) && !empty($subtype->name) &&
236 get_string_manager()->string_exists('help' . $subtype->name, $pluginname)) {
237 $subtype->help = get_string('help' . $subtype->name, $pluginname);
238 }
239 unset($subtype->typestr);
240 $types[] = $subtype;
241 }
242 }
243 // LTISOURCE plugins can also implement callback get_shortcuts() to add items to the activity chooser.
244 // The return values are the same as of the 'mod' callbacks except that $defaultitem is only passed for reference and
245 // should not be added to the return value.
246 if ($moretypes = component_callback("ltisource_$pluginname", 'get_shortcuts', array($defaultitem))) {
247 $types = array_merge($types, $moretypes);
976b5bca
CS
248 }
249 }
250 return $types;
251}
252
6c64e8ac
EL
253/**
254 * Given a coursemodule object, this function returns the extra
255 * information needed to print this activity in various places.
256 * For this module we just need to support external urls as
257 * activity icons
258 *
3de55d24 259 * @param stdClass $coursemodule
6c64e8ac
EL
260 * @return cached_cm_info info
261 */
ea04a9f9 262function lti_get_coursemodule_info($coursemodule) {
42a2c7f1
CS
263 global $DB, $CFG;
264 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 265
6c64e8ac 266 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
3f358828 267 'icon, secureicon, intro, introformat, name, typeid, toolurl, launchcontainer')) {
6c64e8ac
EL
268 return null;
269 }
c07aec16 270
6c64e8ac 271 $info = new cached_cm_info();
e27cb316 272
b07878ec
CS
273 if ($coursemodule->showdescription) {
274 // Convert intro to html. Do not filter cached version, filters run at display time.
275 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
276 }
e27cb316 277
3f358828 278 if (!empty($lti->typeid)) {
279 $toolconfig = lti_get_type_config($lti->typeid);
280 } else if ($tool = lti_get_tool_by_url_match($lti->toolurl)) {
3de55d24
TH
281 $toolconfig = lti_get_type_config($tool->id);
282 } else {
283 $toolconfig = array();
284 }
3f358828 285
286 // We want to use the right icon based on whether the
287 // current page is being requested over http or https.
288 if (lti_request_is_using_ssl() &&
289 (!empty($lti->secureicon) || (isset($toolconfig['secureicon']) && !empty($toolconfig['secureicon'])))) {
290 if (!empty($lti->secureicon)) {
291 $info->iconurl = new moodle_url($lti->secureicon);
292 } else {
293 $info->iconurl = new moodle_url($toolconfig['secureicon']);
294 }
295 } else if (!empty($lti->icon)) {
296 $info->iconurl = new moodle_url($lti->icon);
297 } else if (isset($toolconfig['icon']) && !empty($toolconfig['icon'])) {
298 $info->iconurl = new moodle_url($toolconfig['icon']);
299 }
300
301 // Does the link open in a new window?
3de55d24
TH
302 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
303 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
304 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
305 $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
306 }
307
b07878ec 308 $info->name = $lti->name;
194f2c60 309
c07aec16
CS
310 return $info;
311}
312
aa6eca66
CS
313/**
314 * Return a small object with summary information about what a
315 * user has done with a given particular instance of this module
316 * Used for user activity reports.
317 * $return->time = the time they did it
318 * $return->info = a short text description
319 *
320 * @return null
321 * @TODO: implement this moodle function (if needed)
322 **/
323function lti_user_outline($course, $user, $mod, $basiclti) {
64ce589b 324 return null;
aa6eca66
CS
325}
326
327/**
328 * Print a detailed representation of what a user has done with
329 * a given particular instance of this module, for user activity reports.
330 *
331 * @return boolean
332 * @TODO: implement this moodle function (if needed)
333 **/
334function lti_user_complete($course, $user, $mod, $basiclti) {
335 return true;
336}
337
338/**
339 * Given a course and a time, this module should find recent activity
340 * that has occurred in basiclti activities and print it out.
341 * Return true if there was output, or false is there was none.
342 *
343 * @uses $CFG
344 * @return boolean
345 * @TODO: implement this moodle function
346 **/
347function lti_print_recent_activity($course, $isteacher, $timestart) {
e3f69b58 348 return false; // True if anything was printed, otherwise false.
aa6eca66
CS
349}
350
351/**
352 * Function to be run periodically according to the moodle cron
353 * This function searches for things that need to be done, such
354 * as sending out mail, toggling flags etc ...
355 *
356 * @uses $CFG
357 * @return boolean
358 **/
359function lti_cron () {
360 return true;
361}
362
363/**
364 * Must return an array of grades for a given instance of this module,
365 * indexed by user. It also returns a maximum allowed grade.
366 *
367 * Example:
368 * $return->grades = array of grades;
369 * $return->maxgrade = maximum allowed grade;
370 *
371 * return $return;
372 *
373 * @param int $basicltiid ID of an instance of this module
374 * @return mixed Null or object with an array of grades and with the maximum grade
375 *
376 * @TODO: implement this moodle function (if needed)
377 **/
378function lti_grades($basicltiid) {
379 return null;
380}
381
aa6eca66
CS
382/**
383 * This function returns if a scale is being used by one basiclti
384 * it it has support for grading and scales. Commented code should be
385 * modified if necessary. See forum, glossary or journal modules
386 * as reference.
387 *
388 * @param int $basicltiid ID of an instance of this module
389 * @return mixed
390 *
391 * @TODO: implement this moodle function (if needed)
392 **/
393function lti_scale_used ($basicltiid, $scaleid) {
394 $return = false;
395
e3f69b58 396 // $rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
aa6eca66 397 //
e3f69b58 398 // if (!empty($rec) && !empty($scaleid)) {
399 // $return = true;
400 // }
aa6eca66
CS
401
402 return $return;
403}
404
405/**
406 * Checks if scale is being used by any instance of basiclti.
407 * This function was added in 1.9
408 *
409 * This is used to find out if scale used anywhere
410 * @param $scaleid int
411 * @return boolean True if the scale is used by any basiclti
412 *
413 */
414function lti_scale_used_anywhere($scaleid) {
415 global $DB;
416
417 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
418 return true;
419 } else {
420 return false;
421 }
422}
423
424/**
425 * Execute post-install custom actions for the module
426 * This function was added in 1.9
427 *
428 * @return boolean true if success, false on error
429 */
430function lti_install() {
431 return true;
432}
433
434/**
435 * Execute post-uninstall custom actions for the module
436 * This function was added in 1.9
437 *
438 * @return boolean true if success, false on error
439 */
440function lti_uninstall() {
441 return true;
442}
443
444/**
445 * Returns available Basic LTI types
446 *
447 * @return array of basicLTI types
448 */
449function lti_get_lti_types() {
450 global $DB;
451
452 return $DB->get_records('lti_types');
453}
454
aa6eca66
CS
455/**
456 * Create grade item for given basiclti
457 *
a153c9f2 458 * @category grade
aa6eca66
CS
459 * @param object $basiclti object with extra cmidnumber
460 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
461 * @return int 0 if ok, error code otherwise
462 */
e3f69b58 463function lti_grade_item_update($basiclti, $grades = null) {
aa6eca66
CS
464 global $CFG;
465 require_once($CFG->libdir.'/gradelib.php');
466
e3f69b58 467 $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber);
aa6eca66
CS
468
469 if ($basiclti->grade > 0) {
470 $params['gradetype'] = GRADE_TYPE_VALUE;
471 $params['grademax'] = $basiclti->grade;
472 $params['grademin'] = 0;
473
474 } else if ($basiclti->grade < 0) {
475 $params['gradetype'] = GRADE_TYPE_SCALE;
476 $params['scaleid'] = -$basiclti->grade;
477
478 } else {
e3f69b58 479 $params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
aa6eca66
CS
480 }
481
e3f69b58 482 if ($grades === 'reset') {
aa6eca66
CS
483 $params['reset'] = true;
484 $grades = null;
485 }
486
487 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
488}
489
490/**
491 * Delete grade item for given basiclti
492 *
a153c9f2 493 * @category grade
aa6eca66
CS
494 * @param object $basiclti object
495 * @return object basiclti
496 */
497function lti_grade_item_delete($basiclti) {
498 global $CFG;
499 require_once($CFG->libdir.'/gradelib.php');
500
e3f69b58 501 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted' => 1));
aa6eca66
CS
502}
503
8fa50fdd
MN
504/**
505 * Log post actions
506 *
507 * @return array
508 */
509function lti_get_post_actions() {
510 return array();
511}
512
513/**
514 * Log view actions
515 *
516 * @return array
517 */
518function lti_get_view_actions() {
519 return array('view all', 'view');
520}
4bed1682
JL
521
522/**
523 * Mark the activity completed (if required) and trigger the course_module_viewed event.
524 *
525 * @param stdClass $lti lti object
526 * @param stdClass $course course object
527 * @param stdClass $cm course module object
528 * @param stdClass $context context object
529 * @since Moodle 3.0
530 */
531function lti_view($lti, $course, $cm, $context) {
532
533 // Trigger course_module_viewed event.
534 $params = array(
535 'context' => $context,
536 'objectid' => $lti->id
537 );
538
539 $event = \mod_lti\event\course_module_viewed::create($params);
540 $event->add_record_snapshot('course_modules', $cm);
541 $event->add_record_snapshot('course', $course);
542 $event->add_record_snapshot('lti', $lti);
543 $event->trigger();
544
545 // Completion.
546 $completion = new completion_info($course);
547 $completion->set_module_viewed($cm);
548}
8aec804a
JO
549
550/**
551 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
552 *
553 * @param stdClass $type The tool type object to be filled in
554 * @since Moodle 3.1
555 */
556function lti_load_type_if_cartridge($type) {
557 if (lti_is_cartridge($type->lti_toolurl)) {
558 lti_load_type_from_cartridge($type->lti_toolurl, $type);
559 }
560}
561
562/**
563 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
564 *
565 * @param stdClass $lti The tools config
566 * @since Moodle 3.1
567 */
568function lti_load_tool_if_cartridge($lti) {
569 if (lti_is_cartridge($lti->toolurl)) {
570 lti_load_tool_from_cartridge($lti->toolurl, $lti);
571 }
572}
573
574/**
575 * Determines if the given url is for a IMS basic cartridge
576 *
577 * @param string $type The tool type object to be filled in
578 * @return True if the url is for a cartridge
579 * @since Moodle 3.1
580 */
581function lti_is_cartridge($url) {
582 // If it has xml at the end of the url, it's a cartridge.
583 if (preg_match('/\.xml$/', $url)) {
584 return true;
585 }
586 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
587 $toolinfo = lti_load_cartridge($url,
588 array(
589 "launch_url" => "launchurl"
590 )
591 );
592 if (!empty($toolinfo['launchurl'])) {
593 return true;
594 }
595 return false;
596}
597
598/**
599 * Allows you to load settings for an external tool type from an IMS cartridge.
600 *
601 * @param string $url The URL to the cartridge
602 * @param stdClass $type The tool type object to be filled in
603 * @since Moodle 3.1
604 */
605function lti_load_type_from_cartridge($url, $type) {
606 $toolinfo = lti_load_cartridge($url,
607 array(
608 "title" => "lti_typename",
609 "launch_url" => "lti_toolurl",
610 "secure_launch_url" => "lti_securltoolurl",
611 "description" => "lti_description",
612 ),
613 array(
614 "icon_url" => "lti_icon"
615 )
616 );
617 foreach($toolinfo as $property => $value) {
618 $type->$property = $value;
619 }
620}
621
622/**
623 * Allows you to load in the configuration for an external tool from an IMS cartridge.
624 *
625 * @param string $url The URL to the cartridge
626 * @param stdClass $lti LTI object
627 * @since Moodle 3.1
628 */
629function lti_load_tool_from_cartridge($url, $lti) {
630 $toolinfo = lti_load_cartridge($url,
631 array(
632 "launch_url" => "toolurl",
633 "secure_launch_url" => "securetoolurl",
634 "title" => "name"
635 )
636 );
637 foreach($toolinfo as $property => $value) {
638 $lti->$property = $value;
639 }
640}
641
642/**
643 * Search for a tag within an XML DOMDocument
644 *
645 * @param stdClass $tagName The name of the tag to search for
646 * @param array $map The map of tags to keys in the return array
647 * @param array $properties The map of properties to keys in the return array
648 * @return array An associative array with the given keys and their values from the cartridge
649 * @since Moodle 3.1
650 */
651function lti_load_cartridge($url, $map, $propertiesmap = array()) {
652 global $CFG;
653 require_once($CFG->libdir. "/filelib.php");
654 $curl = new curl();
655 $response = $curl->get($url);
656 $document = new DOMDocument();
657 $document->loadXML($response);
658 $cartridge = new DomXpath($document);
659 $errors = libxml_get_errors();
660 foreach ($errors as $error) {
661 print_error(sprintf("%s at line %d. ", trim($error->message, "\n\r\t ."), $error->line));
662 }
663
664 $toolinfo = array();
665 foreach ($map as $tag => $key) {
666 $value = get_tag($tag, $cartridge);
667 if ($value) {
668 $toolinfo[$key] = $value;
669 }
670 }
671 if (!empty($propertiesmap)) {
672 foreach ($propertiesmap as $property => $key) {
673 $value = get_tag("property", $cartridge, $property);
674 error_log("\n\nProperty is: " . $value . "\n\n");
675 if ($value) {
676 $toolinfo[$key] = $value;
677 }
678 }
679 }
680 return $toolinfo;
681}
682
683/**
684 * Search for a tag within an XML DOMDocument
685 *
686 * @param stdClass $tagName The name of the tag to search for
687 * @param XPath $xpath The XML to find the tag in
688 * @since Moodle 3.1
689 */
690function get_tag($tagname, $xpath, $attribute = null) {
691 if ($attribute) {
692 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
693 } else {
694 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
695 }
696 if ($result->length > 0) {
697 return $result->item(0)->nodeValue;
698 }
699 return null;
700}