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