MDL-36224: Minor updates 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
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 *
61eb12d4
CS
38 * @package mod
39 * @subpackage lti
40 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
aa6eca66 41 * marc.alier@upc.edu
61eb12d4
CS
42 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
43 * @author Marc Alier
44 * @author Jordi Piguillem
45 * @author Nikolas Galanis
8f45215d 46 * @author Chris Scribner
61eb12d4 47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa6eca66
CS
48 */
49
50defined('MOODLE_INTERNAL') || die;
51
06ca6cc9
TH
52/**
53 * Returns all other caps used in module.
54 *
55 * @return array
56 */
57function lti_get_extra_capabilities() {
58 return array('moodle/site:accessallgroups');
59}
60
aa6eca66
CS
61/**
62 * List of features supported in URL module
63 * @param string $feature FEATURE_xx constant for requested feature
64 * @return mixed True if module supports feature, false if not, null if doesn't know
65 */
66function lti_supports($feature) {
67 switch($feature) {
68 case FEATURE_GROUPS: return false;
69 case FEATURE_GROUPINGS: return false;
70 case FEATURE_GROUPMEMBERSONLY: return true;
71 case FEATURE_MOD_INTRO: return true;
72 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
73 case FEATURE_GRADE_HAS_GRADE: return true;
74 case FEATURE_GRADE_OUTCOMES: return true;
75 case FEATURE_BACKUP_MOODLE2: return true;
b07878ec 76 case FEATURE_SHOW_DESCRIPTION: return true;
aa6eca66
CS
77
78 default: return null;
79 }
80}
81
82/**
83 * Given an object containing all the necessary data,
84 * (defined by the form in mod.html) this function
85 * will create a new instance and return the id number
86 * of the new instance.
87 *
88 * @param object $instance An object from the form in mod.html
89 * @return int The id of the newly inserted basiclti record
90 **/
c69bd1af 91function lti_add_instance($lti, $mform) {
42a2c7f1
CS
92 global $DB, $CFG;
93 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 94
c69bd1af
EL
95 $lti->timecreated = time();
96 $lti->timemodified = $lti->timecreated;
97 $lti->servicesalt = uniqid('', true);
e27cb316 98
c69bd1af
EL
99 if (!isset($lti->grade)) {
100 $lti->grade = 100; // TODO: Why is this harcoded here and default @ DB
101 }
aa6eca66 102
c69bd1af 103 $lti->id = $DB->insert_record('lti', $lti);
e27cb316 104
c69bd1af
EL
105 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
106 if (!isset($lti->cmidnumber)) {
107 $lti->cmidnumber = '';
32c079dc 108 }
e27cb316 109
c69bd1af 110 lti_grade_item_update($lti);
aa6eca66
CS
111 }
112
c69bd1af 113 return $lti->id;
aa6eca66
CS
114}
115
116/**
117 * Given an object containing all the necessary data,
118 * (defined by the form in mod.html) this function
119 * will update an existing instance with new data.
120 *
121 * @param object $instance An object from the form in mod.html
122 * @return boolean Success/Fail
123 **/
c69bd1af 124function lti_update_instance($lti, $mform) {
42a2c7f1
CS
125 global $DB, $CFG;
126 require_once($CFG->dirroot.'/mod/lti/locallib.php');
aa6eca66 127
c69bd1af
EL
128 $lti->timemodified = time();
129 $lti->id = $lti->instance;
aa6eca66 130
b07878ec
CS
131 if (!isset($lti->showtitlelaunch)) {
132 $lti->showtitlelaunch = 0;
aa6eca66 133 }
e27cb316 134
b07878ec
CS
135 if (!isset($lti->showdescriptionlaunch)) {
136 $lti->showdescriptionlaunch = 0;
aa6eca66 137 }
e27cb316 138
c69bd1af
EL
139 if (!isset($lti->grade)) {
140 $lti->grade = $DB->get_field('lti', 'grade', array('id' => $lti->id));
141 }
e27cb316 142
c69bd1af
EL
143 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
144 lti_grade_item_update($lti);
aa6eca66 145 } else {
c69bd1af 146 lti_grade_item_delete($lti);
aa6eca66
CS
147 }
148
c69bd1af 149 return $DB->update_record('lti', $lti);
aa6eca66
CS
150}
151
152/**
153 * Given an ID of an instance of this module,
154 * this function will permanently delete the instance
155 * and any data that depends on it.
156 *
157 * @param int $id Id of the module instance
158 * @return boolean Success/Failure
159 **/
160function lti_delete_instance($id) {
161 global $DB;
162
163 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
164 return false;
165 }
166
167 $result = true;
168
169 # Delete any dependent records here #
170 lti_grade_item_delete($basiclti);
171
172 return $DB->delete_records("lti", array("id" => $basiclti->id));
173}
174
976b5bca
CS
175function lti_get_types() {
176 $type = new stdClass();
177 $type->modclass = MOD_CLASS_ACTIVITY;
178 $type->type = 'lti';
179 $type->typestr = get_string('modulename', 'mod_lti');
180
181 $types = array($type);
182
183 foreach (get_plugin_list('ltisource') as $name => $dir) {
184 if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
185 $types = array_merge($types, $moretypes);
186 }
187 }
188 return $types;
189}
190
6c64e8ac
EL
191/**
192 * Given a coursemodule object, this function returns the extra
193 * information needed to print this activity in various places.
194 * For this module we just need to support external urls as
195 * activity icons
196 *
3de55d24 197 * @param stdClass $coursemodule
6c64e8ac
EL
198 * @return cached_cm_info info
199 */
ea04a9f9 200function lti_get_coursemodule_info($coursemodule) {
42a2c7f1
CS
201 global $DB, $CFG;
202 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 203
6c64e8ac 204 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
3de55d24 205 'icon, secureicon, intro, introformat, name, toolurl, launchcontainer')) {
6c64e8ac
EL
206 return null;
207 }
c07aec16 208
6c64e8ac 209 $info = new cached_cm_info();
e27cb316 210
6c64e8ac
EL
211 // We want to use the right icon based on whether the
212 // current page is being requested over http or https.
ea04a9f9 213 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
6c64e8ac
EL
214 $info->iconurl = new moodle_url($lti->secureicon);
215 } else if (!empty($lti->icon)) {
216 $info->iconurl = new moodle_url($lti->icon);
c07aec16 217 }
194f2c60 218
b07878ec
CS
219 if ($coursemodule->showdescription) {
220 // Convert intro to html. Do not filter cached version, filters run at display time.
221 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
222 }
e27cb316 223
3de55d24
TH
224 // Does the link open in a new window?
225 $tool = lti_get_tool_by_url_match($lti->toolurl);
226 if ($tool) {
227 $toolconfig = lti_get_type_config($tool->id);
228 } else {
229 $toolconfig = array();
230 }
231 $launchcontainer = lti_get_launch_container($lti, $toolconfig);
232 if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
233 $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
234 $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti'); return false;";
235 }
236
b07878ec 237 $info->name = $lti->name;
194f2c60 238
c07aec16
CS
239 return $info;
240}
241
aa6eca66
CS
242/**
243 * Return a small object with summary information about what a
244 * user has done with a given particular instance of this module
245 * Used for user activity reports.
246 * $return->time = the time they did it
247 * $return->info = a short text description
248 *
249 * @return null
250 * @TODO: implement this moodle function (if needed)
251 **/
252function lti_user_outline($course, $user, $mod, $basiclti) {
64ce589b 253 return null;
aa6eca66
CS
254}
255
256/**
257 * Print a detailed representation of what a user has done with
258 * a given particular instance of this module, for user activity reports.
259 *
260 * @return boolean
261 * @TODO: implement this moodle function (if needed)
262 **/
263function lti_user_complete($course, $user, $mod, $basiclti) {
264 return true;
265}
266
267/**
268 * Given a course and a time, this module should find recent activity
269 * that has occurred in basiclti activities and print it out.
270 * Return true if there was output, or false is there was none.
271 *
272 * @uses $CFG
273 * @return boolean
274 * @TODO: implement this moodle function
275 **/
276function lti_print_recent_activity($course, $isteacher, $timestart) {
277 return false; // True if anything was printed, otherwise false
278}
279
280/**
281 * Function to be run periodically according to the moodle cron
282 * This function searches for things that need to be done, such
283 * as sending out mail, toggling flags etc ...
284 *
285 * @uses $CFG
286 * @return boolean
287 **/
288function lti_cron () {
289 return true;
290}
291
292/**
293 * Must return an array of grades for a given instance of this module,
294 * indexed by user. It also returns a maximum allowed grade.
295 *
296 * Example:
297 * $return->grades = array of grades;
298 * $return->maxgrade = maximum allowed grade;
299 *
300 * return $return;
301 *
302 * @param int $basicltiid ID of an instance of this module
303 * @return mixed Null or object with an array of grades and with the maximum grade
304 *
305 * @TODO: implement this moodle function (if needed)
306 **/
307function lti_grades($basicltiid) {
308 return null;
309}
310
aa6eca66
CS
311/**
312 * This function returns if a scale is being used by one basiclti
313 * it it has support for grading and scales. Commented code should be
314 * modified if necessary. See forum, glossary or journal modules
315 * as reference.
316 *
317 * @param int $basicltiid ID of an instance of this module
318 * @return mixed
319 *
320 * @TODO: implement this moodle function (if needed)
321 **/
322function lti_scale_used ($basicltiid, $scaleid) {
323 $return = false;
324
325 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
326 //
327 //if (!empty($rec) && !empty($scaleid)) {
328 // $return = true;
329 //}
330
331 return $return;
332}
333
334/**
335 * Checks if scale is being used by any instance of basiclti.
336 * This function was added in 1.9
337 *
338 * This is used to find out if scale used anywhere
339 * @param $scaleid int
340 * @return boolean True if the scale is used by any basiclti
341 *
342 */
343function lti_scale_used_anywhere($scaleid) {
344 global $DB;
345
346 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
347 return true;
348 } else {
349 return false;
350 }
351}
352
353/**
354 * Execute post-install custom actions for the module
355 * This function was added in 1.9
356 *
357 * @return boolean true if success, false on error
358 */
359function lti_install() {
360 return true;
361}
362
363/**
364 * Execute post-uninstall custom actions for the module
365 * This function was added in 1.9
366 *
367 * @return boolean true if success, false on error
368 */
369function lti_uninstall() {
370 return true;
371}
372
373/**
374 * Returns available Basic LTI types
375 *
376 * @return array of basicLTI types
377 */
378function lti_get_lti_types() {
379 global $DB;
380
381 return $DB->get_records('lti_types');
382}
383
aa6eca66
CS
384/**
385 * Create grade item for given basiclti
386 *
a153c9f2 387 * @category grade
aa6eca66
CS
388 * @param object $basiclti object with extra cmidnumber
389 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
390 * @return int 0 if ok, error code otherwise
391 */
392function lti_grade_item_update($basiclti, $grades=null) {
393 global $CFG;
394 require_once($CFG->libdir.'/gradelib.php');
395
396 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
397
398 if ($basiclti->grade > 0) {
399 $params['gradetype'] = GRADE_TYPE_VALUE;
400 $params['grademax'] = $basiclti->grade;
401 $params['grademin'] = 0;
402
403 } else if ($basiclti->grade < 0) {
404 $params['gradetype'] = GRADE_TYPE_SCALE;
405 $params['scaleid'] = -$basiclti->grade;
406
407 } else {
408 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
409 }
410
411 if ($grades === 'reset') {
412 $params['reset'] = true;
413 $grades = null;
414 }
415
416 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
417}
418
419/**
420 * Delete grade item for given basiclti
421 *
a153c9f2 422 * @category grade
aa6eca66
CS
423 * @param object $basiclti object
424 * @return object basiclti
425 */
426function lti_grade_item_delete($basiclti) {
427 global $CFG;
428 require_once($CFG->libdir.'/gradelib.php');
429
430 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
431}
432
f4f711d7
CS
433function lti_extend_settings_navigation($settings, $parentnode) {
434 global $PAGE;
e27cb316 435
c288a3db 436 if (has_capability('mod/lti:grade', context_module::instance($PAGE->cm->id))) {
c4d80efe 437 $keys = $parentnode->get_children_key_list();
e27cb316 438
c4d80efe
CS
439 $node = navigation_node::create('Submissions',
440 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
441 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
442
443 $parentnode->add_node($node, $keys[1]);
444 }
61eb12d4 445}