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