2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 // This file is part of BasicLTI4Moodle
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.
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.
36 * This file contains a library of functions and constants for the lti module
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 * @author Chris Scribner
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 defined('MOODLE_INTERNAL') || die;
52 * Returns all other caps used in module.
56 function lti_get_extra_capabilities() {
57 return array('moodle/site:accessallgroups');
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
65 function lti_supports($feature) {
68 case FEATURE_GROUPINGS:
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:
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.
89 * @param object $instance An object from the form in mod.html
90 * @return int The id of the newly inserted basiclti record
92 function lti_add_instance($lti, $mform) {
94 require_once($CFG->dirroot.'/mod/lti/locallib.php');
96 if (!isset($lti->toolurl)) {
100 lti_load_tool_if_cartridge($lti);
102 $lti->timecreated = time();
103 $lti->timemodified = $lti->timecreated;
104 $lti->servicesalt = uniqid('', true);
106 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
108 if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
109 $lti->typeid = $lti->urlmatchedtypeid;
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.
117 $lti->id = $DB->insert_record('lti', $lti);
119 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
120 if (!isset($lti->cmidnumber)) {
121 $lti->cmidnumber = '';
124 lti_grade_item_update($lti);
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.
135 * @param object $instance An object from the form in mod.html
136 * @return boolean Success/Fail
138 function lti_update_instance($lti, $mform) {
140 require_once($CFG->dirroot.'/mod/lti/locallib.php');
142 lti_load_tool_if_cartridge($lti);
144 $lti->timemodified = time();
145 $lti->id = $lti->instance;
147 if (!isset($lti->showtitlelaunch)) {
148 $lti->showtitlelaunch = 0;
151 if (!isset($lti->showdescriptionlaunch)) {
152 $lti->showdescriptionlaunch = 0;
155 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
157 if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
158 lti_grade_item_update($lti);
160 // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
162 $lti->instructorchoiceacceptgrades = 0;
164 lti_grade_item_delete($lti);
167 if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
168 $lti->typeid = $lti->urlmatchedtypeid;
171 return $DB->update_record('lti', $lti);
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.
179 * @param int $id Id of the module instance
180 * @return boolean Success/Failure
182 function lti_delete_instance($id) {
185 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
191 // Delete any dependent records here.
192 lti_grade_item_delete($basiclti);
194 $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
196 $DB->delete_records('lti_tool_settings',
197 array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
200 return $DB->delete_records("lti", array("id" => $basiclti->id));
204 * Return aliases of this activity. LTI should have an alias for each configured tool type
205 * This is so you can add an external tool types directly to the activity chooser
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 * plus an additional property, helplink.
211 * Properties title and link are required
213 function lti_get_shortcuts($defaultitem) {
214 global $CFG, $COURSE;
215 require_once($CFG->dirroot.'/mod/lti/locallib.php');
217 $types = lti_get_configured_types($COURSE->id, $defaultitem->link->param('sr'));
218 $types[] = $defaultitem;
220 // Add items defined in ltisource plugins.
221 foreach (core_component::get_plugin_list('ltisource') as $pluginname => $dir) {
222 if ($moretypes = component_callback("ltisource_$pluginname", 'get_types')) {
223 // Callback 'get_types()' in 'ltisource' plugins is deprecated in 3.1 and will be removed in 3.5, TODO MDL-53697.
224 debugging('Deprecated callback get_types() is found in ltisource_' . $pluginname .
225 ', use get_shortcuts() instead', DEBUG_DEVELOPER);
226 $grouptitle = get_string('modulenameplural', 'mod_lti');
227 foreach ($moretypes as $subtype) {
228 // Instead of adding subitems combine the name of the group with the name of the subtype.
229 $subtype->title = get_string('activitytypetitle', '',
230 (object)['activity' => $grouptitle, 'type' => $subtype->typestr]);
231 // Re-implement the logic of get_module_metadata() in Moodle 3.0 and below for converting
232 // subtypes into items in activity chooser.
233 $subtype->type = str_replace('&', '&', $subtype->type);
234 $subtype->name = preg_replace('/.*type=/', '', $subtype->type);
235 $subtype->link = new moodle_url($defaultitem->link, array('type' => $subtype->name));
236 if (empty($subtype->help) && !empty($subtype->name) &&
237 get_string_manager()->string_exists('help' . $subtype->name, $pluginname)) {
238 $subtype->help = get_string('help' . $subtype->name, $pluginname);
240 unset($subtype->typestr);
244 // LTISOURCE plugins can also implement callback get_shortcuts() to add items to the activity chooser.
245 // The return values are the same as of the 'mod' callbacks except that $defaultitem is only passed for reference and
246 // should not be added to the return value.
247 if ($moretypes = component_callback("ltisource_$pluginname", 'get_shortcuts', array($defaultitem))) {
248 $types = array_merge($types, $moretypes);
255 * Given a coursemodule object, this function returns the extra
256 * information needed to print this activity in various places.
257 * For this module we just need to support external urls as
260 * @param stdClass $coursemodule
261 * @return cached_cm_info info
263 function lti_get_coursemodule_info($coursemodule) {
265 require_once($CFG->dirroot.'/mod/lti/locallib.php');
267 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
268 'icon, secureicon, intro, introformat, name, typeid, toolurl, launchcontainer')) {
272 $info = new cached_cm_info();
274 if ($coursemodule->showdescription) {
275 // Convert intro to html. Do not filter cached version, filters run at display time.
276 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
279 if (!empty($lti->typeid)) {
280 $toolconfig = lti_get_type_config($lti->typeid);
281 } else if ($tool = lti_get_tool_by_url_match($lti->toolurl)) {
282 $toolconfig = lti_get_type_config($tool->id);
284 $toolconfig = array();
287 // We want to use the right icon based on whether the
288 // current page is being requested over http or https.
289 if (lti_request_is_using_ssl() &&
290 (!empty($lti->secureicon) || (isset($toolconfig['secureicon']) && !empty($toolconfig['secureicon'])))) {
291 if (!empty($lti->secureicon)) {
292 $info->iconurl = new moodle_url($lti->secureicon);
294 $info->iconurl = new moodle_url($toolconfig['secureicon']);
296 } else if (!empty($lti->icon)) {
297 $info->iconurl = new moodle_url($lti->icon);
298 } else if (isset($toolconfig['icon']) && !empty($toolconfig['icon'])) {
299 $info->iconurl = new moodle_url($toolconfig['icon']);
302 // Does the link open in a new window?
303 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
304 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
305 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
306 $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
309 $info->name = $lti->name;
315 * Return a small object with summary information about what a
316 * user has done with a given particular instance of this module
317 * Used for user activity reports.
318 * $return->time = the time they did it
319 * $return->info = a short text description
322 * @TODO: implement this moodle function (if needed)
324 function lti_user_outline($course, $user, $mod, $basiclti) {
329 * Print a detailed representation of what a user has done with
330 * a given particular instance of this module, for user activity reports.
333 * @TODO: implement this moodle function (if needed)
335 function lti_user_complete($course, $user, $mod, $basiclti) {
340 * Given a course and a time, this module should find recent activity
341 * that has occurred in basiclti activities and print it out.
342 * Return true if there was output, or false is there was none.
346 * @TODO: implement this moodle function
348 function lti_print_recent_activity($course, $isteacher, $timestart) {
349 return false; // True if anything was printed, otherwise false.
353 * Function to be run periodically according to the moodle cron
354 * This function searches for things that need to be done, such
355 * as sending out mail, toggling flags etc ...
360 function lti_cron () {
365 * Must return an array of grades for a given instance of this module,
366 * indexed by user. It also returns a maximum allowed grade.
369 * $return->grades = array of grades;
370 * $return->maxgrade = maximum allowed grade;
374 * @param int $basicltiid ID of an instance of this module
375 * @return mixed Null or object with an array of grades and with the maximum grade
377 * @TODO: implement this moodle function (if needed)
379 function lti_grades($basicltiid) {
384 * This function returns if a scale is being used by one basiclti
385 * it it has support for grading and scales. Commented code should be
386 * modified if necessary. See forum, glossary or journal modules
389 * @param int $basicltiid ID of an instance of this module
392 * @TODO: implement this moodle function (if needed)
394 function lti_scale_used ($basicltiid, $scaleid) {
397 // $rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
399 // if (!empty($rec) && !empty($scaleid)) {
407 * Checks if scale is being used by any instance of basiclti.
408 * This function was added in 1.9
410 * This is used to find out if scale used anywhere
411 * @param $scaleid int
412 * @return boolean True if the scale is used by any basiclti
415 function lti_scale_used_anywhere($scaleid) {
418 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
426 * Execute post-install custom actions for the module
427 * This function was added in 1.9
429 * @return boolean true if success, false on error
431 function lti_install() {
436 * Execute post-uninstall custom actions for the module
437 * This function was added in 1.9
439 * @return boolean true if success, false on error
441 function lti_uninstall() {
446 * Returns available Basic LTI types
448 * @return array of basicLTI types
450 function lti_get_lti_types() {
453 return $DB->get_records('lti_types', null, 'state DESC, timemodified DESC');
457 * Returns available Basic LTI types that match the given
460 * @param int $toolproxyid Tool proxy id
461 * @return array of basicLTI types
463 function lti_get_lti_types_from_proxy_id($toolproxyid) {
466 return $DB->get_records('lti_types', array('toolproxyid' => $toolproxyid), 'state DESC, timemodified DESC');
470 * Create grade item for given basiclti
473 * @param object $basiclti object with extra cmidnumber
474 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
475 * @return int 0 if ok, error code otherwise
477 function lti_grade_item_update($basiclti, $grades = null) {
479 require_once($CFG->libdir.'/gradelib.php');
481 $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber);
483 if ($basiclti->grade > 0) {
484 $params['gradetype'] = GRADE_TYPE_VALUE;
485 $params['grademax'] = $basiclti->grade;
486 $params['grademin'] = 0;
488 } else if ($basiclti->grade < 0) {
489 $params['gradetype'] = GRADE_TYPE_SCALE;
490 $params['scaleid'] = -$basiclti->grade;
493 $params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
496 if ($grades === 'reset') {
497 $params['reset'] = true;
501 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
505 * Delete grade item for given basiclti
508 * @param object $basiclti object
509 * @return object basiclti
511 function lti_grade_item_delete($basiclti) {
513 require_once($CFG->libdir.'/gradelib.php');
515 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted' => 1));
523 function lti_get_post_actions() {
532 function lti_get_view_actions() {
533 return array('view all', 'view');
537 * Mark the activity completed (if required) and trigger the course_module_viewed event.
539 * @param stdClass $lti lti object
540 * @param stdClass $course course object
541 * @param stdClass $cm course module object
542 * @param stdClass $context context object
545 function lti_view($lti, $course, $cm, $context) {
547 // Trigger course_module_viewed event.
549 'context' => $context,
550 'objectid' => $lti->id
553 $event = \mod_lti\event\course_module_viewed::create($params);
554 $event->add_record_snapshot('course_modules', $cm);
555 $event->add_record_snapshot('course', $course);
556 $event->add_record_snapshot('lti', $lti);
560 $completion = new completion_info($course);
561 $completion->set_module_viewed($cm);