MDL-44616 behat: Adding steps to test MDLQA-1709 and MDLQA-62
[moodle.git] / mod / lti / lib.php
blob8903475fd5267e6fc2849c1305927a750132fa65
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.
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
35 /**
36 * This file contains a library of functions and constants for the lti module
38 * @package mod_lti
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
40 * marc.alier@upc.edu
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
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;
51 /**
52 * Returns all other caps used in module.
54 * @return array
56 function lti_get_extra_capabilities() {
57 return array('moodle/site:accessallgroups');
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
65 function lti_supports($feature) {
66 switch($feature) {
67 case FEATURE_GROUPS: return false;
68 case FEATURE_GROUPINGS: return false;
69 case FEATURE_GROUPMEMBERSONLY: return true;
70 case FEATURE_MOD_INTRO: return true;
71 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
72 case FEATURE_GRADE_HAS_GRADE: return true;
73 case FEATURE_GRADE_OUTCOMES: return true;
74 case FEATURE_BACKUP_MOODLE2: return true;
75 case FEATURE_SHOW_DESCRIPTION: return true;
77 default: return null;
81 /**
82 * Given an object containing all the necessary data,
83 * (defined by the form in mod.html) this function
84 * will create a new instance and return the id number
85 * of the new instance.
87 * @param object $instance An object from the form in mod.html
88 * @return int The id of the newly inserted basiclti record
89 **/
90 function lti_add_instance($lti, $mform) {
91 global $DB, $CFG;
92 require_once($CFG->dirroot.'/mod/lti/locallib.php');
94 $lti->timecreated = time();
95 $lti->timemodified = $lti->timecreated;
96 $lti->servicesalt = uniqid('', true);
98 if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
99 $lti->typeid = $lti->urlmatchedtypeid;
102 if (!isset($lti->grade)) {
103 $lti->grade = 100; // TODO: Why is this harcoded here and default @ DB
106 $lti->id = $DB->insert_record('lti', $lti);
108 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
109 if (!isset($lti->cmidnumber)) {
110 $lti->cmidnumber = '';
113 lti_grade_item_update($lti);
116 return $lti->id;
120 * Given an object containing all the necessary data,
121 * (defined by the form in mod.html) this function
122 * will update an existing instance with new data.
124 * @param object $instance An object from the form in mod.html
125 * @return boolean Success/Fail
127 function lti_update_instance($lti, $mform) {
128 global $DB, $CFG;
129 require_once($CFG->dirroot.'/mod/lti/locallib.php');
131 $lti->timemodified = time();
132 $lti->id = $lti->instance;
134 if (!isset($lti->showtitlelaunch)) {
135 $lti->showtitlelaunch = 0;
138 if (!isset($lti->showdescriptionlaunch)) {
139 $lti->showdescriptionlaunch = 0;
142 if (!isset($lti->grade)) {
143 $lti->grade = $DB->get_field('lti', 'grade', array('id' => $lti->id));
146 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
147 lti_grade_item_update($lti);
148 } else {
149 lti_grade_item_delete($lti);
152 if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
153 $lti->typeid = $lti->urlmatchedtypeid;
156 return $DB->update_record('lti', $lti);
160 * Given an ID of an instance of this module,
161 * this function will permanently delete the instance
162 * and any data that depends on it.
164 * @param int $id Id of the module instance
165 * @return boolean Success/Failure
167 function lti_delete_instance($id) {
168 global $DB;
170 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
171 return false;
174 $result = true;
176 # Delete any dependent records here #
177 lti_grade_item_delete($basiclti);
179 return $DB->delete_records("lti", array("id" => $basiclti->id));
182 function lti_get_types() {
183 global $OUTPUT;
185 $subtypes = array();
186 foreach (get_plugin_list('ltisource') as $name => $dir) {
187 if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
188 $subtypes = array_merge($subtypes, $moretypes);
191 if (empty($subtypes)) {
192 return MOD_SUBTYPE_NO_CHILDREN;
195 $types = array();
197 $type = new stdClass();
198 $type->modclass = MOD_CLASS_ACTIVITY;
199 $type->type = 'lti_group_start';
200 $type->typestr = '--'.get_string('modulenameplural', 'mod_lti');
201 $types[] = $type;
203 $link = get_string('modulename_link', 'mod_lti');
204 $linktext = get_string('morehelp');
205 $help = get_string('modulename_help', 'mod_lti');
206 $help .= html_writer::tag('div', $OUTPUT->doc_link($link, $linktext, true), array('class' => 'helpdoclink'));
208 $type = new stdClass();
209 $type->modclass = MOD_CLASS_ACTIVITY;
210 $type->type = 'lti';
211 $type->typestr = get_string('generaltool', 'mod_lti');
212 $type->help = $help;
213 $types[] = $type;
215 $types = array_merge($types, $subtypes);
217 $type = new stdClass();
218 $type->modclass = MOD_CLASS_ACTIVITY;
219 $type->type = 'lti_group_end';
220 $type->typestr = '--';
221 $types[] = $type;
223 return $types;
227 * Given a coursemodule object, this function returns the extra
228 * information needed to print this activity in various places.
229 * For this module we just need to support external urls as
230 * activity icons
232 * @param stdClass $coursemodule
233 * @return cached_cm_info info
235 function lti_get_coursemodule_info($coursemodule) {
236 global $DB, $CFG;
237 require_once($CFG->dirroot.'/mod/lti/locallib.php');
239 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
240 'icon, secureicon, intro, introformat, name, toolurl, launchcontainer')) {
241 return null;
244 $info = new cached_cm_info();
246 // We want to use the right icon based on whether the
247 // current page is being requested over http or https.
248 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
249 $info->iconurl = new moodle_url($lti->secureicon);
250 } else if (!empty($lti->icon)) {
251 $info->iconurl = new moodle_url($lti->icon);
254 if ($coursemodule->showdescription) {
255 // Convert intro to html. Do not filter cached version, filters run at display time.
256 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
259 // Does the link open in a new window?
260 $tool = lti_get_tool_by_url_match($lti->toolurl);
261 if ($tool) {
262 $toolconfig = lti_get_type_config($tool->id);
263 } else {
264 $toolconfig = array();
266 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
267 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
268 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
269 $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
272 $info->name = $lti->name;
274 return $info;
278 * Return a small object with summary information about what a
279 * user has done with a given particular instance of this module
280 * Used for user activity reports.
281 * $return->time = the time they did it
282 * $return->info = a short text description
284 * @return null
285 * @TODO: implement this moodle function (if needed)
287 function lti_user_outline($course, $user, $mod, $basiclti) {
288 return null;
292 * Print a detailed representation of what a user has done with
293 * a given particular instance of this module, for user activity reports.
295 * @return boolean
296 * @TODO: implement this moodle function (if needed)
298 function lti_user_complete($course, $user, $mod, $basiclti) {
299 return true;
303 * Given a course and a time, this module should find recent activity
304 * that has occurred in basiclti activities and print it out.
305 * Return true if there was output, or false is there was none.
307 * @uses $CFG
308 * @return boolean
309 * @TODO: implement this moodle function
311 function lti_print_recent_activity($course, $isteacher, $timestart) {
312 return false; // True if anything was printed, otherwise false
316 * Function to be run periodically according to the moodle cron
317 * This function searches for things that need to be done, such
318 * as sending out mail, toggling flags etc ...
320 * @uses $CFG
321 * @return boolean
323 function lti_cron () {
324 return true;
328 * Must return an array of grades for a given instance of this module,
329 * indexed by user. It also returns a maximum allowed grade.
331 * Example:
332 * $return->grades = array of grades;
333 * $return->maxgrade = maximum allowed grade;
335 * return $return;
337 * @param int $basicltiid ID of an instance of this module
338 * @return mixed Null or object with an array of grades and with the maximum grade
340 * @TODO: implement this moodle function (if needed)
342 function lti_grades($basicltiid) {
343 return null;
347 * This function returns if a scale is being used by one basiclti
348 * it it has support for grading and scales. Commented code should be
349 * modified if necessary. See forum, glossary or journal modules
350 * as reference.
352 * @param int $basicltiid ID of an instance of this module
353 * @return mixed
355 * @TODO: implement this moodle function (if needed)
357 function lti_scale_used ($basicltiid, $scaleid) {
358 $return = false;
360 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
362 //if (!empty($rec) && !empty($scaleid)) {
363 // $return = true;
366 return $return;
370 * Checks if scale is being used by any instance of basiclti.
371 * This function was added in 1.9
373 * This is used to find out if scale used anywhere
374 * @param $scaleid int
375 * @return boolean True if the scale is used by any basiclti
378 function lti_scale_used_anywhere($scaleid) {
379 global $DB;
381 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
382 return true;
383 } else {
384 return false;
389 * Execute post-install custom actions for the module
390 * This function was added in 1.9
392 * @return boolean true if success, false on error
394 function lti_install() {
395 return true;
399 * Execute post-uninstall custom actions for the module
400 * This function was added in 1.9
402 * @return boolean true if success, false on error
404 function lti_uninstall() {
405 return true;
409 * Returns available Basic LTI types
411 * @return array of basicLTI types
413 function lti_get_lti_types() {
414 global $DB;
416 return $DB->get_records('lti_types');
420 * Create grade item for given basiclti
422 * @category grade
423 * @param object $basiclti object with extra cmidnumber
424 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
425 * @return int 0 if ok, error code otherwise
427 function lti_grade_item_update($basiclti, $grades=null) {
428 global $CFG;
429 require_once($CFG->libdir.'/gradelib.php');
431 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
433 if ($basiclti->grade > 0) {
434 $params['gradetype'] = GRADE_TYPE_VALUE;
435 $params['grademax'] = $basiclti->grade;
436 $params['grademin'] = 0;
438 } else if ($basiclti->grade < 0) {
439 $params['gradetype'] = GRADE_TYPE_SCALE;
440 $params['scaleid'] = -$basiclti->grade;
442 } else {
443 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
446 if ($grades === 'reset') {
447 $params['reset'] = true;
448 $grades = null;
451 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
455 * Delete grade item for given basiclti
457 * @category grade
458 * @param object $basiclti object
459 * @return object basiclti
461 function lti_grade_item_delete($basiclti) {
462 global $CFG;
463 require_once($CFG->libdir.'/gradelib.php');
465 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
468 function lti_extend_settings_navigation($settings, $parentnode) {
469 global $PAGE;
471 if (has_capability('mod/lti:grade', context_module::instance($PAGE->cm->id))) {
472 $keys = $parentnode->get_children_key_list();
474 $node = navigation_node::create('Submissions',
475 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
476 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
478 $parentnode->add_node($node, $keys[1]);