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 * List of features supported in URL module
53 * @param string $feature FEATURE_xx constant for requested feature
54 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
56 function lti_supports($feature) {
59 case FEATURE_GROUPINGS
:
61 case FEATURE_MOD_INTRO
:
62 case FEATURE_COMPLETION_TRACKS_VIEWS
:
63 case FEATURE_GRADE_HAS_GRADE
:
64 case FEATURE_GRADE_OUTCOMES
:
65 case FEATURE_BACKUP_MOODLE2
:
66 case FEATURE_SHOW_DESCRIPTION
:
68 case FEATURE_MOD_PURPOSE
:
69 return MOD_PURPOSE_CONTENT
;
77 * Given an object containing all the necessary data,
78 * (defined by the form in mod.html) this function
79 * will create a new instance and return the id number
80 * of the new instance.
82 * @param object $instance An object from the form in mod.html
83 * @return int The id of the newly inserted basiclti record
85 function lti_add_instance($lti, $mform) {
87 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
89 if (!isset($lti->toolurl
)) {
93 lti_load_tool_if_cartridge($lti);
95 $lti->timecreated
= time();
96 $lti->timemodified
= $lti->timecreated
;
97 $lti->servicesalt
= uniqid('', true);
98 if (!isset($lti->typeid
)) {
102 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
104 if (empty($lti->typeid
) && isset($lti->urlmatchedtypeid
)) {
105 $lti->typeid
= $lti->urlmatchedtypeid
;
108 if (!isset($lti->instructorchoiceacceptgrades
) ||
$lti->instructorchoiceacceptgrades
!= LTI_SETTING_ALWAYS
) {
109 // The instance does not accept grades back from the provider, so set to "No grade" value 0.
113 $lti->id
= $DB->insert_record('lti', $lti);
115 if (isset($lti->instructorchoiceacceptgrades
) && $lti->instructorchoiceacceptgrades
== LTI_SETTING_ALWAYS
) {
116 if (!isset($lti->cmidnumber
)) {
117 $lti->cmidnumber
= '';
120 lti_grade_item_update($lti);
123 $services = lti_get_services();
124 foreach ($services as $service) {
125 $service->instance_added( $lti );
128 $completiontimeexpected = !empty($lti->completionexpected
) ?
$lti->completionexpected
: null;
129 \core_completion\api
::update_completion_date_event($lti->coursemodule
, 'lti', $lti->id
, $completiontimeexpected);
135 * Given an object containing all the necessary data,
136 * (defined by the form in mod.html) this function
137 * will update an existing instance with new data.
139 * @param object $instance An object from the form in mod.html
140 * @return boolean Success/Fail
142 function lti_update_instance($lti, $mform) {
144 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
146 lti_load_tool_if_cartridge($lti);
148 $lti->timemodified
= time();
149 $lti->id
= $lti->instance
;
151 if (!isset($lti->showtitlelaunch
)) {
152 $lti->showtitlelaunch
= 0;
155 if (!isset($lti->showdescriptionlaunch
)) {
156 $lti->showdescriptionlaunch
= 0;
159 lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
161 if (isset($lti->instructorchoiceacceptgrades
) && $lti->instructorchoiceacceptgrades
== LTI_SETTING_ALWAYS
) {
162 lti_grade_item_update($lti);
164 // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
166 $lti->instructorchoiceacceptgrades
= 0;
168 lti_grade_item_delete($lti);
171 if ($lti->typeid
== 0 && isset($lti->urlmatchedtypeid
)) {
172 $lti->typeid
= $lti->urlmatchedtypeid
;
175 $services = lti_get_services();
176 foreach ($services as $service) {
177 $service->instance_updated( $lti );
180 $completiontimeexpected = !empty($lti->completionexpected
) ?
$lti->completionexpected
: null;
181 \core_completion\api
::update_completion_date_event($lti->coursemodule
, 'lti', $lti->id
, $completiontimeexpected);
183 return $DB->update_record('lti', $lti);
187 * Given an ID of an instance of this module,
188 * this function will permanently delete the instance
189 * and any data that depends on it.
191 * @param int $id Id of the module instance
192 * @return boolean Success/Failure
194 function lti_delete_instance($id) {
196 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
198 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
204 // Delete any dependent records here.
205 lti_grade_item_delete($basiclti);
207 $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid
));
209 $DB->delete_records('lti_tool_settings',
210 array('toolproxyid' => $ltitype->toolproxyid
, 'course' => $basiclti->course
, 'coursemoduleid' => $id));
213 $cm = get_coursemodule_from_instance('lti', $id);
214 \core_completion\api
::update_completion_date_event($cm->id
, 'lti', $id, null);
216 // We must delete the module record after we delete the grade item.
217 if ($DB->delete_records("lti", array("id" => $basiclti->id
)) ) {
218 $services = lti_get_services();
219 foreach ($services as $service) {
220 $service->instance_deleted( $id );
229 * Return the preconfigured tools which are configured for inclusion in the activity picker.
231 * @param \core_course\local\entity\content_item $defaultmodulecontentitem reference to the content item for the LTI module.
232 * @param \stdClass $user the user object, to use for cap checks if desired.
233 * @param stdClass $course the course to scope items to.
234 * @return array the array of content items.
236 function lti_get_course_content_items(\core_course\local\entity\content_item
$defaultmodulecontentitem, \stdClass
$user,
238 global $CFG, $OUTPUT;
239 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
243 // The 'External tool' entry (the main module content item), should always take the id of 1.
244 if (has_capability('mod/lti:addmanualinstance', context_course
::instance($course->id
), $user)) {
245 $types = [new \core_course\local\entity\
content_item(
247 $defaultmodulecontentitem->get_name(),
248 $defaultmodulecontentitem->get_title(),
249 $defaultmodulecontentitem->get_link(),
250 $defaultmodulecontentitem->get_icon(),
251 $defaultmodulecontentitem->get_help(),
252 $defaultmodulecontentitem->get_archetype(),
253 $defaultmodulecontentitem->get_component_name(),
254 $defaultmodulecontentitem->get_purpose()
258 // Other, preconfigured tools take their own id + 1, so we'll never clash with the module's entry.
259 $preconfiguredtools = lti_get_configured_types($course->id
, $defaultmodulecontentitem->get_link()->param('sr'));
260 foreach ($preconfiguredtools as $preconfiguredtool) {
262 // Append the help link to the help text.
263 if (isset($preconfiguredtool->help
)) {
264 if (isset($preconfiguredtool->helplink
)) {
265 $linktext = get_string('morehelp');
266 $preconfiguredtool->help
.= html_writer
::tag('div',
267 $OUTPUT->doc_link($preconfiguredtool->helplink
, $linktext, true), ['class' => 'helpdoclink']);
270 $preconfiguredtool->help
= '';
273 $types[] = new \core_course\local\entity\
content_item(
274 $preconfiguredtool->id +
1,
275 $preconfiguredtool->name
,
276 new \core_course\local\entity\
string_title($preconfiguredtool->title
),
277 $preconfiguredtool->link
,
278 $preconfiguredtool->icon
,
279 $preconfiguredtool->help
,
280 $defaultmodulecontentitem->get_archetype(),
281 $defaultmodulecontentitem->get_component_name(),
282 $defaultmodulecontentitem->get_purpose()
289 * Return all content items which can be added to any course.
291 * @param \core_course\local\entity\content_item $defaultmodulecontentitem
292 * @return array the array of content items.
294 function mod_lti_get_all_content_items(\core_course\local\entity\content_item
$defaultmodulecontentitem): array {
295 global $OUTPUT, $CFG;
296 require_once($CFG->dirroot
. '/mod/lti/locallib.php'); // For access to constants.
298 // The 'External tool' entry (the main module content item), should always take the id of 1.
299 $types = [new \core_course\local\entity\
content_item(
301 $defaultmodulecontentitem->get_name(),
302 $defaultmodulecontentitem->get_title(),
303 $defaultmodulecontentitem->get_link(),
304 $defaultmodulecontentitem->get_icon(),
305 $defaultmodulecontentitem->get_help(),
306 $defaultmodulecontentitem->get_archetype(),
307 $defaultmodulecontentitem->get_component_name(),
308 $defaultmodulecontentitem->get_purpose()
311 foreach (lti_get_lti_types() as $ltitype) {
312 if ($ltitype->coursevisible
!= LTI_COURSEVISIBLE_ACTIVITYCHOOSER
) {
315 $type = new stdClass();
316 $type->id
= $ltitype->id
;
317 $type->modclass
= MOD_CLASS_ACTIVITY
;
318 $type->name
= 'lti_type_' . $ltitype->id
;
319 // Clean the name. We don't want tags here.
320 $type->title
= clean_param($ltitype->name
, PARAM_NOTAGS
);
321 $trimmeddescription = trim($ltitype->description ??
'');
323 if ($trimmeddescription != '') {
324 // Clean the description. We don't want tags here.
325 $type->help
= clean_param($trimmeddescription, PARAM_NOTAGS
);
326 $type->helplink
= get_string('modulename_shortcut_link', 'lti');
328 if (empty($ltitype->icon
)) {
329 $type->icon
= $OUTPUT->pix_icon('monologo', '', 'lti', array('class' => 'icon'));
331 $type->icon
= html_writer
::empty_tag('img', array('src' => $ltitype->icon
, 'alt' => $ltitype->name
, 'class' => 'icon'));
333 $type->link
= new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'typeid' => $ltitype->id
));
335 $types[] = new \core_course\local\entity\
content_item(
338 new \core_course\local\entity\
string_title($type->title
),
342 $defaultmodulecontentitem->get_archetype(),
343 $defaultmodulecontentitem->get_component_name(),
344 $defaultmodulecontentitem->get_purpose()
352 * Given a coursemodule object, this function returns the extra
353 * information needed to print this activity in various places.
354 * For this module we just need to support external urls as
357 * @param stdClass $coursemodule
358 * @return cached_cm_info info
360 function lti_get_coursemodule_info($coursemodule) {
362 require_once($CFG->dirroot
.'/mod/lti/locallib.php');
364 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance
),
365 'icon, secureicon, intro, introformat, name, typeid, toolurl, launchcontainer')) {
369 $info = new cached_cm_info();
371 if ($coursemodule->showdescription
) {
372 // Convert intro to html. Do not filter cached version, filters run at display time.
373 $info->content
= format_module_intro('lti', $lti, $coursemodule->id
, false);
376 if (!empty($lti->typeid
)) {
377 $toolconfig = lti_get_type_config($lti->typeid
);
378 } else if ($tool = lti_get_tool_by_url_match($lti->toolurl
)) {
379 $toolconfig = lti_get_type_config($tool->id
);
381 $toolconfig = array();
384 // We want to use the right icon based on whether the
385 // current page is being requested over http or https.
386 if (lti_request_is_using_ssl() &&
387 (!empty($lti->secureicon
) ||
(isset($toolconfig['secureicon']) && !empty($toolconfig['secureicon'])))) {
388 if (!empty($lti->secureicon
)) {
389 $info->iconurl
= new moodle_url($lti->secureicon
);
391 $info->iconurl
= new moodle_url($toolconfig['secureicon']);
393 } else if (!empty($lti->icon
)) {
394 $info->iconurl
= new moodle_url($lti->icon
);
395 } else if (isset($toolconfig['icon']) && !empty($toolconfig['icon'])) {
396 $info->iconurl
= new moodle_url($toolconfig['icon']);
399 // Does the link open in a new window?
400 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
401 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW
) {
402 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id
));
403 $info->onclick
= "window.open('" . $launchurl->out(false) . "', 'lti-".$coursemodule->id
."'); return false;";
406 $info->name
= $lti->name
;
412 * Return a small object with summary information about what a
413 * user has done with a given particular instance of this module
414 * Used for user activity reports.
415 * $return->time = the time they did it
416 * $return->info = a short text description
419 * @TODO: implement this moodle function (if needed)
421 function lti_user_outline($course, $user, $mod, $basiclti) {
426 * Print a detailed representation of what a user has done with
427 * a given particular instance of this module, for user activity reports.
430 * @TODO: implement this moodle function (if needed)
432 function lti_user_complete($course, $user, $mod, $basiclti) {
437 * Given a course and a time, this module should find recent activity
438 * that has occurred in basiclti activities and print it out.
439 * Return true if there was output, or false is there was none.
443 * @TODO: implement this moodle function
445 function lti_print_recent_activity($course, $isteacher, $timestart) {
446 return false; // True if anything was printed, otherwise false.
450 * Function to be run periodically according to the moodle cron
451 * This function searches for things that need to be done, such
452 * as sending out mail, toggling flags etc ...
457 function lti_cron () {
462 * Must return an array of grades for a given instance of this module,
463 * indexed by user. It also returns a maximum allowed grade.
466 * $return->grades = array of grades;
467 * $return->maxgrade = maximum allowed grade;
471 * @param int $basicltiid ID of an instance of this module
472 * @return mixed Null or object with an array of grades and with the maximum grade
474 * @TODO: implement this moodle function (if needed)
476 function lti_grades($basicltiid) {
481 * @deprecated since Moodle 3.8
483 function lti_scale_used() {
484 throw new coding_exception('lti_scale_used() can not be used anymore. Plugins can implement ' .
485 '<modname>_scale_used_anywhere, all implementations of <modname>_scale_used are now ignored');
489 * Checks if scale is being used by any instance of basiclti.
490 * This function was added in 1.9
492 * This is used to find out if scale used anywhere
493 * @param $scaleid int
494 * @return boolean True if the scale is used by any basiclti
497 function lti_scale_used_anywhere($scaleid) {
500 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
508 * Execute post-install custom actions for the module
509 * This function was added in 1.9
511 * @return boolean true if success, false on error
513 function lti_install() {
518 * Execute post-uninstall custom actions for the module
519 * This function was added in 1.9
521 * @return boolean true if success, false on error
523 function lti_uninstall() {
528 * Returns available Basic LTI types
530 * @return array of basicLTI types
532 function lti_get_lti_types() {
535 return $DB->get_records('lti_types', null, 'state DESC, timemodified DESC');
539 * Returns available Basic LTI types that match the given
542 * @param int $toolproxyid Tool proxy id
543 * @return array of basicLTI types
545 function lti_get_lti_types_from_proxy_id($toolproxyid) {
548 return $DB->get_records('lti_types', array('toolproxyid' => $toolproxyid), 'state DESC, timemodified DESC');
552 * Create grade item for given basiclti
555 * @param object $basiclti object with extra cmidnumber
556 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
557 * @return int 0 if ok, error code otherwise
559 function lti_grade_item_update($basiclti, $grades = null) {
561 require_once($CFG->libdir
.'/gradelib.php');
562 require_once($CFG->dirroot
.'/mod/lti/servicelib.php');
564 if (!lti_accepts_grades($basiclti)) {
568 $params = array('itemname' => $basiclti->name
, 'idnumber' => $basiclti->cmidnumber
);
570 if ($basiclti->grade
> 0) {
571 $params['gradetype'] = GRADE_TYPE_VALUE
;
572 $params['grademax'] = $basiclti->grade
;
573 $params['grademin'] = 0;
575 } else if ($basiclti->grade
< 0) {
576 $params['gradetype'] = GRADE_TYPE_SCALE
;
577 $params['scaleid'] = -$basiclti->grade
;
580 $params['gradetype'] = GRADE_TYPE_TEXT
; // Allow text comments only.
583 if ($grades === 'reset') {
584 $params['reset'] = true;
588 return grade_update('mod/lti', $basiclti->course
, 'mod', 'lti', $basiclti->id
, 0, $grades, $params);
592 * Update activity grades
594 * @param stdClass $basiclti The LTI instance
595 * @param int $userid Specific user only, 0 means all.
596 * @param bool $nullifnone Not used
598 function lti_update_grades($basiclti, $userid=0, $nullifnone=true) {
600 require_once($CFG->dirroot
.'/mod/lti/servicelib.php');
601 // LTI doesn't have its own grade table so the only thing to do is update the grade item.
602 if (lti_accepts_grades($basiclti)) {
603 lti_grade_item_update($basiclti);
608 * Delete grade item for given basiclti
611 * @param object $basiclti object
612 * @return object basiclti
614 function lti_grade_item_delete($basiclti) {
616 require_once($CFG->libdir
.'/gradelib.php');
618 return grade_update('mod/lti', $basiclti->course
, 'mod', 'lti', $basiclti->id
, 0, null, array('deleted' => 1));
626 function lti_get_post_actions() {
635 function lti_get_view_actions() {
636 return array('view all', 'view');
640 * Mark the activity completed (if required) and trigger the course_module_viewed event.
642 * @param stdClass $lti lti object
643 * @param stdClass $course course object
644 * @param stdClass $cm course module object
645 * @param stdClass $context context object
648 function lti_view($lti, $course, $cm, $context) {
650 // Trigger course_module_viewed event.
652 'context' => $context,
653 'objectid' => $lti->id
656 $event = \mod_lti\event\course_module_viewed
::create($params);
657 $event->add_record_snapshot('course_modules', $cm);
658 $event->add_record_snapshot('course', $course);
659 $event->add_record_snapshot('lti', $lti);
663 $completion = new completion_info($course);
664 $completion->set_module_viewed($cm);
668 * Check if the module has any update that affects the current user since a given time.
670 * @param cm_info $cm course module data
671 * @param int $from the time to check updates from
672 * @param array $filter if we need to check only specific updates
673 * @return stdClass an object with the different type of areas indicating if they were updated or not
676 function lti_check_updates_since(cm_info
$cm, $from, $filter = array()) {
679 $updates = course_check_module_updates_since($cm, $from, array(), $filter);
681 // Check if there is a new submission.
682 $updates->submissions
= (object) array('updated' => false);
683 $select = 'ltiid = :id AND userid = :userid AND (datesubmitted > :since1 OR dateupdated > :since2)';
684 $params = array('id' => $cm->instance
, 'userid' => $USER->id
, 'since1' => $from, 'since2' => $from);
685 $submissions = $DB->get_records_select('lti_submission', $select, $params, '', 'id');
686 if (!empty($submissions)) {
687 $updates->submissions
->updated
= true;
688 $updates->submissions
->itemids
= array_keys($submissions);
691 // Now, teachers should see other students updates.
692 if (has_capability('mod/lti:manage', $cm->context
)) {
693 $select = 'ltiid = :id AND (datesubmitted > :since1 OR dateupdated > :since2)';
694 $params = array('id' => $cm->instance
, 'since1' => $from, 'since2' => $from);
696 if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS
) {
697 $groupusers = array_keys(groups_get_activity_shared_group_members($cm));
698 if (empty($groupusers)) {
701 list($insql, $inparams) = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED
);
702 $select .= ' AND userid ' . $insql;
703 $params = array_merge($params, $inparams);
706 $updates->usersubmissions
= (object) array('updated' => false);
707 $submissions = $DB->get_records_select('lti_submission', $select, $params, '', 'id');
708 if (!empty($submissions)) {
709 $updates->usersubmissions
->updated
= true;
710 $updates->usersubmissions
->itemids
= array_keys($submissions);
718 * Get icon mapping for font-awesome.
720 function mod_lti_get_fontawesome_icon_map() {
722 'mod_lti:warning' => 'fa-exclamation text-warning',
727 * This function receives a calendar event and returns the action associated with it, or null if there is none.
729 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
730 * is not displayed on the block.
732 * @param calendar_event $event
733 * @param \core_calendar\action_factory $factory
734 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
735 * @return \core_calendar\local\event\entities\action_interface|null
737 function mod_lti_core_calendar_provide_event_action(calendar_event
$event,
738 \core_calendar\action_factory
$factory,
742 if (empty($userid)) {
746 $cm = get_fast_modinfo($event->courseid
, $userid)->instances
['lti'][$event->instance
];
748 if (!$cm->uservisible
) {
749 // The module is not visible to the user for any reason.
753 $completion = new \
completion_info($cm->get_course());
755 $completiondata = $completion->get_data($cm, false, $userid);
757 if ($completiondata->completionstate
!= COMPLETION_INCOMPLETE
) {
761 return $factory->create_instance(
763 new \
moodle_url('/mod/lti/view.php', ['id' => $cm->id
]),
770 * Extend the course navigation with an "LTI External tools" link which redirects to a list of all tools available for course use.
772 * @param settings_navigation $navigation The settings navigation object
773 * @param stdClass $course The course
774 * @param stdclass $context Course context
777 function mod_lti_extend_navigation_course($navigation, $course, $context): void
{
778 if (has_capability('mod/lti:addpreconfiguredinstance', $context)) {
779 $url = new moodle_url('/mod/lti/coursetools.php', ['id' => $course->id
]);
780 $settingsnode = navigation_node
::create(get_string('courseexternaltools', 'mod_lti'), $url, navigation_node
::TYPE_SETTING
,
781 null, 'coursetools', new pix_icon('i/settings', ''));
782 $navigation->add_node($settingsnode);