MDL-45064 course: mod callback get_shortcuts()
[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
c69bd1af
EL
100 $lti->timecreated = time();
101 $lti->timemodified = $lti->timecreated;
102 $lti->servicesalt = uniqid('', true);
e27cb316 103
8fa50fdd
MN
104 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
105
5582fe49 106 if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
ea5d0515
AF
107 $lti->typeid = $lti->urlmatchedtypeid;
108 }
109
8fa50fdd
MN
110 if (!isset($lti->instructorchoiceacceptgrades) || $lti->instructorchoiceacceptgrades != LTI_SETTING_ALWAYS) {
111 // The instance does not accept grades back from the provider, so set to "No grade" value 0.
112 $lti->grade = 0;
c69bd1af 113 }
aa6eca66 114
c69bd1af 115 $lti->id = $DB->insert_record('lti', $lti);
e27cb316 116
8fa50fdd 117 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
c69bd1af
EL
118 if (!isset($lti->cmidnumber)) {
119 $lti->cmidnumber = '';
32c079dc 120 }
e27cb316 121
c69bd1af 122 lti_grade_item_update($lti);
aa6eca66
CS
123 }
124
c69bd1af 125 return $lti->id;
aa6eca66
CS
126}
127
128/**
129 * Given an object containing all the necessary data,
130 * (defined by the form in mod.html) this function
131 * will update an existing instance with new data.
132 *
133 * @param object $instance An object from the form in mod.html
134 * @return boolean Success/Fail
135 **/
c69bd1af 136function lti_update_instance($lti, $mform) {
42a2c7f1
CS
137 global $DB, $CFG;
138 require_once($CFG->dirroot.'/mod/lti/locallib.php');
aa6eca66 139
c69bd1af
EL
140 $lti->timemodified = time();
141 $lti->id = $lti->instance;
aa6eca66 142
b07878ec
CS
143 if (!isset($lti->showtitlelaunch)) {
144 $lti->showtitlelaunch = 0;
aa6eca66 145 }
e27cb316 146
b07878ec
CS
147 if (!isset($lti->showdescriptionlaunch)) {
148 $lti->showdescriptionlaunch = 0;
aa6eca66 149 }
e27cb316 150
8fa50fdd 151 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
e27cb316 152
8fa50fdd 153 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
c69bd1af 154 lti_grade_item_update($lti);
aa6eca66 155 } else {
8fa50fdd
MN
156 // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
157 $lti->grade = 0;
158 $lti->instructorchoiceacceptgrades = 0;
159
c69bd1af 160 lti_grade_item_delete($lti);
aa6eca66
CS
161 }
162
ea5d0515
AF
163 if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
164 $lti->typeid = $lti->urlmatchedtypeid;
165 }
166
c69bd1af 167 return $DB->update_record('lti', $lti);
aa6eca66
CS
168}
169
170/**
171 * Given an ID of an instance of this module,
172 * this function will permanently delete the instance
173 * and any data that depends on it.
174 *
175 * @param int $id Id of the module instance
176 * @return boolean Success/Failure
177 **/
178function lti_delete_instance($id) {
179 global $DB;
180
181 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
182 return false;
183 }
184
185 $result = true;
186
e3f69b58 187 // Delete any dependent records here.
aa6eca66
CS
188 lti_grade_item_delete($basiclti);
189
e3f69b58 190 $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
496bf3ea
DM
191 if ($ltitype) {
192 $DB->delete_records('lti_tool_settings',
193 array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
194 }
e3f69b58 195
aa6eca66
CS
196 return $DB->delete_records("lti", array("id" => $basiclti->id));
197}
198
01e8bfd7
JO
199/**
200 * Return aliases of this activity. LTI should have an alias for each configured tool type
201 * This is so you can add an external tool types directly from the activity chooser
202 *
203 * @return array An array of aliases for this activity
204 **/
205function lti_get_aliases() {
206 global $CFG, $COURSE;
207 require_once($CFG->dirroot.'/mod/lti/locallib.php');
208
209 $types = lti_get_configured_types($COURSE->id);
210
211 return $types;
212}
213
976b5bca 214function lti_get_types() {
8cf7670e 215 global $OUTPUT;
976b5bca 216
8cf7670e 217 $subtypes = array();
976b5bca
CS
218 foreach (get_plugin_list('ltisource') as $name => $dir) {
219 if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
8cf7670e 220 $subtypes = array_merge($subtypes, $moretypes);
976b5bca
CS
221 }
222 }
8cf7670e
MN
223 if (empty($subtypes)) {
224 return MOD_SUBTYPE_NO_CHILDREN;
225 }
226
227 $types = array();
228
229 $type = new stdClass();
230 $type->modclass = MOD_CLASS_ACTIVITY;
231 $type->type = 'lti_group_start';
232 $type->typestr = '--'.get_string('modulenameplural', 'mod_lti');
233 $types[] = $type;
234
235 $link = get_string('modulename_link', 'mod_lti');
236 $linktext = get_string('morehelp');
237 $help = get_string('modulename_help', 'mod_lti');
238 $help .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
239
240 $type = new stdClass();
241 $type->modclass = MOD_CLASS_ACTIVITY;
24fc6dee 242 $type->type = '';
8cf7670e
MN
243 $type->typestr = get_string('generaltool', 'mod_lti');
244 $type->help = $help;
245 $types[] = $type;
246
247 $types = array_merge($types, $subtypes);
248
249 $type = new stdClass();
250 $type->modclass = MOD_CLASS_ACTIVITY;
251 $type->type = 'lti_group_end';
252 $type->typestr = '--';
253 $types[] = $type;
254
976b5bca
CS
255 return $types;
256}
257
6c64e8ac
EL
258/**
259 * Given a coursemodule object, this function returns the extra
260 * information needed to print this activity in various places.
261 * For this module we just need to support external urls as
262 * activity icons
263 *
3de55d24 264 * @param stdClass $coursemodule
6c64e8ac
EL
265 * @return cached_cm_info info
266 */
ea04a9f9 267function lti_get_coursemodule_info($coursemodule) {
42a2c7f1
CS
268 global $DB, $CFG;
269 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 270
6c64e8ac 271 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
3f358828 272 'icon, secureicon, intro, introformat, name, typeid, toolurl, launchcontainer')) {
6c64e8ac
EL
273 return null;
274 }
c07aec16 275
6c64e8ac 276 $info = new cached_cm_info();
e27cb316 277
b07878ec
CS
278 if ($coursemodule->showdescription) {
279 // Convert intro to html. Do not filter cached version, filters run at display time.
280 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
281 }
e27cb316 282
3f358828 283 if (!empty($lti->typeid)) {
284 $toolconfig = lti_get_type_config($lti->typeid);
285 } else if ($tool = lti_get_tool_by_url_match($lti->toolurl)) {
3de55d24
TH
286 $toolconfig = lti_get_type_config($tool->id);
287 } else {
288 $toolconfig = array();
289 }
3f358828 290
291 // We want to use the right icon based on whether the
292 // current page is being requested over http or https.
293 if (lti_request_is_using_ssl() &&
294 (!empty($lti->secureicon) || (isset($toolconfig['secureicon']) && !empty($toolconfig['secureicon'])))) {
295 if (!empty($lti->secureicon)) {
296 $info->iconurl = new moodle_url($lti->secureicon);
297 } else {
298 $info->iconurl = new moodle_url($toolconfig['secureicon']);
299 }
300 } else if (!empty($lti->icon)) {
301 $info->iconurl = new moodle_url($lti->icon);
302 } else if (isset($toolconfig['icon']) && !empty($toolconfig['icon'])) {
303 $info->iconurl = new moodle_url($toolconfig['icon']);
304 }
305
306 // Does the link open in a new window?
3de55d24
TH
307 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
308 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
309 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
310 $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
311 }
312
b07878ec 313 $info->name = $lti->name;
194f2c60 314
c07aec16
CS
315 return $info;
316}
317
aa6eca66
CS
318/**
319 * Return a small object with summary information about what a
320 * user has done with a given particular instance of this module
321 * Used for user activity reports.
322 * $return->time = the time they did it
323 * $return->info = a short text description
324 *
325 * @return null
326 * @TODO: implement this moodle function (if needed)
327 **/
328function lti_user_outline($course, $user, $mod, $basiclti) {
64ce589b 329 return null;
aa6eca66
CS
330}
331
332/**
333 * Print a detailed representation of what a user has done with
334 * a given particular instance of this module, for user activity reports.
335 *
336 * @return boolean
337 * @TODO: implement this moodle function (if needed)
338 **/
339function lti_user_complete($course, $user, $mod, $basiclti) {
340 return true;
341}
342
343/**
344 * Given a course and a time, this module should find recent activity
345 * that has occurred in basiclti activities and print it out.
346 * Return true if there was output, or false is there was none.
347 *
348 * @uses $CFG
349 * @return boolean
350 * @TODO: implement this moodle function
351 **/
352function lti_print_recent_activity($course, $isteacher, $timestart) {
e3f69b58 353 return false; // True if anything was printed, otherwise false.
aa6eca66
CS
354}
355
356/**
357 * Function to be run periodically according to the moodle cron
358 * This function searches for things that need to be done, such
359 * as sending out mail, toggling flags etc ...
360 *
361 * @uses $CFG
362 * @return boolean
363 **/
364function lti_cron () {
365 return true;
366}
367
368/**
369 * Must return an array of grades for a given instance of this module,
370 * indexed by user. It also returns a maximum allowed grade.
371 *
372 * Example:
373 * $return->grades = array of grades;
374 * $return->maxgrade = maximum allowed grade;
375 *
376 * return $return;
377 *
378 * @param int $basicltiid ID of an instance of this module
379 * @return mixed Null or object with an array of grades and with the maximum grade
380 *
381 * @TODO: implement this moodle function (if needed)
382 **/
383function lti_grades($basicltiid) {
384 return null;
385}
386
aa6eca66
CS
387/**
388 * This function returns if a scale is being used by one basiclti
389 * it it has support for grading and scales. Commented code should be
390 * modified if necessary. See forum, glossary or journal modules
391 * as reference.
392 *
393 * @param int $basicltiid ID of an instance of this module
394 * @return mixed
395 *
396 * @TODO: implement this moodle function (if needed)
397 **/
398function lti_scale_used ($basicltiid, $scaleid) {
399 $return = false;
400
e3f69b58 401 // $rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
aa6eca66 402 //
e3f69b58 403 // if (!empty($rec) && !empty($scaleid)) {
404 // $return = true;
405 // }
aa6eca66
CS
406
407 return $return;
408}
409
410/**
411 * Checks if scale is being used by any instance of basiclti.
412 * This function was added in 1.9
413 *
414 * This is used to find out if scale used anywhere
415 * @param $scaleid int
416 * @return boolean True if the scale is used by any basiclti
417 *
418 */
419function lti_scale_used_anywhere($scaleid) {
420 global $DB;
421
422 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
423 return true;
424 } else {
425 return false;
426 }
427}
428
429/**
430 * Execute post-install custom actions for the module
431 * This function was added in 1.9
432 *
433 * @return boolean true if success, false on error
434 */
435function lti_install() {
436 return true;
437}
438
439/**
440 * Execute post-uninstall custom actions for the module
441 * This function was added in 1.9
442 *
443 * @return boolean true if success, false on error
444 */
445function lti_uninstall() {
446 return true;
447}
448
449/**
450 * Returns available Basic LTI types
451 *
452 * @return array of basicLTI types
453 */
454function lti_get_lti_types() {
455 global $DB;
456
457 return $DB->get_records('lti_types');
458}
459
aa6eca66
CS
460/**
461 * Create grade item for given basiclti
462 *
a153c9f2 463 * @category grade
aa6eca66
CS
464 * @param object $basiclti object with extra cmidnumber
465 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
466 * @return int 0 if ok, error code otherwise
467 */
e3f69b58 468function lti_grade_item_update($basiclti, $grades = null) {
aa6eca66
CS
469 global $CFG;
470 require_once($CFG->libdir.'/gradelib.php');
471
e3f69b58 472 $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber);
aa6eca66
CS
473
474 if ($basiclti->grade > 0) {
475 $params['gradetype'] = GRADE_TYPE_VALUE;
476 $params['grademax'] = $basiclti->grade;
477 $params['grademin'] = 0;
478
479 } else if ($basiclti->grade < 0) {
480 $params['gradetype'] = GRADE_TYPE_SCALE;
481 $params['scaleid'] = -$basiclti->grade;
482
483 } else {
e3f69b58 484 $params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
aa6eca66
CS
485 }
486
e3f69b58 487 if ($grades === 'reset') {
aa6eca66
CS
488 $params['reset'] = true;
489 $grades = null;
490 }
491
492 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
493}
494
495/**
496 * Delete grade item for given basiclti
497 *
a153c9f2 498 * @category grade
aa6eca66
CS
499 * @param object $basiclti object
500 * @return object basiclti
501 */
502function lti_grade_item_delete($basiclti) {
503 global $CFG;
504 require_once($CFG->libdir.'/gradelib.php');
505
e3f69b58 506 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted' => 1));
aa6eca66
CS
507}
508
8fa50fdd
MN
509/**
510 * Log post actions
511 *
512 * @return array
513 */
514function lti_get_post_actions() {
515 return array();
516}
517
518/**
519 * Log view actions
520 *
521 * @return array
522 */
523function lti_get_view_actions() {
524 return array('view all', 'view');
525}
4bed1682
JL
526
527/**
528 * Mark the activity completed (if required) and trigger the course_module_viewed event.
529 *
530 * @param stdClass $lti lti object
531 * @param stdClass $course course object
532 * @param stdClass $cm course module object
533 * @param stdClass $context context object
534 * @since Moodle 3.0
535 */
536function lti_view($lti, $course, $cm, $context) {
537
538 // Trigger course_module_viewed event.
539 $params = array(
540 'context' => $context,
541 'objectid' => $lti->id
542 );
543
544 $event = \mod_lti\event\course_module_viewed::create($params);
545 $event->add_record_snapshot('course_modules', $cm);
546 $event->add_record_snapshot('course', $course);
547 $event->add_record_snapshot('lti', $lti);
548 $event->trigger();
549
550 // Completion.
551 $completion = new completion_info($course);
552 $completion->set_module_viewed($cm);
553}