MDL-45221 Allow unit tests to pass under any OS
[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;
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;
b07878ec 75 case FEATURE_SHOW_DESCRIPTION: return true;
aa6eca66
CS
76
77 default: return null;
78 }
79}
80
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.
86 *
87 * @param object $instance An object from the form in mod.html
88 * @return int The id of the newly inserted basiclti record
89 **/
c69bd1af 90function lti_add_instance($lti, $mform) {
42a2c7f1
CS
91 global $DB, $CFG;
92 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 93
c69bd1af
EL
94 $lti->timecreated = time();
95 $lti->timemodified = $lti->timecreated;
96 $lti->servicesalt = uniqid('', true);
e27cb316 97
5582fe49 98 if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
ea5d0515
AF
99 $lti->typeid = $lti->urlmatchedtypeid;
100 }
101
c69bd1af
EL
102 if (!isset($lti->grade)) {
103 $lti->grade = 100; // TODO: Why is this harcoded here and default @ DB
104 }
aa6eca66 105
c69bd1af 106 $lti->id = $DB->insert_record('lti', $lti);
e27cb316 107
c69bd1af
EL
108 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
109 if (!isset($lti->cmidnumber)) {
110 $lti->cmidnumber = '';
32c079dc 111 }
e27cb316 112
c69bd1af 113 lti_grade_item_update($lti);
aa6eca66
CS
114 }
115
c69bd1af 116 return $lti->id;
aa6eca66
CS
117}
118
119/**
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.
123 *
124 * @param object $instance An object from the form in mod.html
125 * @return boolean Success/Fail
126 **/
c69bd1af 127function lti_update_instance($lti, $mform) {
42a2c7f1
CS
128 global $DB, $CFG;
129 require_once($CFG->dirroot.'/mod/lti/locallib.php');
aa6eca66 130
c69bd1af
EL
131 $lti->timemodified = time();
132 $lti->id = $lti->instance;
aa6eca66 133
b07878ec
CS
134 if (!isset($lti->showtitlelaunch)) {
135 $lti->showtitlelaunch = 0;
aa6eca66 136 }
e27cb316 137
b07878ec
CS
138 if (!isset($lti->showdescriptionlaunch)) {
139 $lti->showdescriptionlaunch = 0;
aa6eca66 140 }
e27cb316 141
c69bd1af
EL
142 if (!isset($lti->grade)) {
143 $lti->grade = $DB->get_field('lti', 'grade', array('id' => $lti->id));
144 }
e27cb316 145
c69bd1af
EL
146 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
147 lti_grade_item_update($lti);
aa6eca66 148 } else {
c69bd1af 149 lti_grade_item_delete($lti);
aa6eca66
CS
150 }
151
ea5d0515
AF
152 if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
153 $lti->typeid = $lti->urlmatchedtypeid;
154 }
155
c69bd1af 156 return $DB->update_record('lti', $lti);
aa6eca66
CS
157}
158
159/**
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.
163 *
164 * @param int $id Id of the module instance
165 * @return boolean Success/Failure
166 **/
167function lti_delete_instance($id) {
168 global $DB;
169
170 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
171 return false;
172 }
173
174 $result = true;
175
176 # Delete any dependent records here #
177 lti_grade_item_delete($basiclti);
178
179 return $DB->delete_records("lti", array("id" => $basiclti->id));
180}
181
976b5bca 182function lti_get_types() {
8cf7670e 183 global $OUTPUT;
976b5bca 184
8cf7670e 185 $subtypes = array();
976b5bca
CS
186 foreach (get_plugin_list('ltisource') as $name => $dir) {
187 if ($moretypes = component_callback("ltisource_$name", 'get_types')) {
8cf7670e 188 $subtypes = array_merge($subtypes, $moretypes);
976b5bca
CS
189 }
190 }
8cf7670e
MN
191 if (empty($subtypes)) {
192 return MOD_SUBTYPE_NO_CHILDREN;
193 }
194
195 $types = array();
196
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;
202
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'));
207
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;
214
215 $types = array_merge($types, $subtypes);
216
217 $type = new stdClass();
218 $type->modclass = MOD_CLASS_ACTIVITY;
219 $type->type = 'lti_group_end';
220 $type->typestr = '--';
221 $types[] = $type;
222
976b5bca
CS
223 return $types;
224}
225
6c64e8ac
EL
226/**
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
231 *
3de55d24 232 * @param stdClass $coursemodule
6c64e8ac
EL
233 * @return cached_cm_info info
234 */
ea04a9f9 235function lti_get_coursemodule_info($coursemodule) {
42a2c7f1
CS
236 global $DB, $CFG;
237 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 238
6c64e8ac 239 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
3de55d24 240 'icon, secureicon, intro, introformat, name, toolurl, launchcontainer')) {
6c64e8ac
EL
241 return null;
242 }
c07aec16 243
6c64e8ac 244 $info = new cached_cm_info();
e27cb316 245
6c64e8ac
EL
246 // We want to use the right icon based on whether the
247 // current page is being requested over http or https.
ea04a9f9 248 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
6c64e8ac
EL
249 $info->iconurl = new moodle_url($lti->secureicon);
250 } else if (!empty($lti->icon)) {
251 $info->iconurl = new moodle_url($lti->icon);
c07aec16 252 }
194f2c60 253
b07878ec
CS
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);
257 }
e27cb316 258
3de55d24
TH
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();
265 }
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;";
270 }
271
b07878ec 272 $info->name = $lti->name;
194f2c60 273
c07aec16
CS
274 return $info;
275}
276
aa6eca66
CS
277/**
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
283 *
284 * @return null
285 * @TODO: implement this moodle function (if needed)
286 **/
287function lti_user_outline($course, $user, $mod, $basiclti) {
64ce589b 288 return null;
aa6eca66
CS
289}
290
291/**
292 * Print a detailed representation of what a user has done with
293 * a given particular instance of this module, for user activity reports.
294 *
295 * @return boolean
296 * @TODO: implement this moodle function (if needed)
297 **/
298function lti_user_complete($course, $user, $mod, $basiclti) {
299 return true;
300}
301
302/**
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.
306 *
307 * @uses $CFG
308 * @return boolean
309 * @TODO: implement this moodle function
310 **/
311function lti_print_recent_activity($course, $isteacher, $timestart) {
312 return false; // True if anything was printed, otherwise false
313}
314
315/**
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 ...
319 *
320 * @uses $CFG
321 * @return boolean
322 **/
323function lti_cron () {
324 return true;
325}
326
327/**
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.
330 *
331 * Example:
332 * $return->grades = array of grades;
333 * $return->maxgrade = maximum allowed grade;
334 *
335 * return $return;
336 *
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
339 *
340 * @TODO: implement this moodle function (if needed)
341 **/
342function lti_grades($basicltiid) {
343 return null;
344}
345
aa6eca66
CS
346/**
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.
351 *
352 * @param int $basicltiid ID of an instance of this module
353 * @return mixed
354 *
355 * @TODO: implement this moodle function (if needed)
356 **/
357function lti_scale_used ($basicltiid, $scaleid) {
358 $return = false;
359
360 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
361 //
362 //if (!empty($rec) && !empty($scaleid)) {
363 // $return = true;
364 //}
365
366 return $return;
367}
368
369/**
370 * Checks if scale is being used by any instance of basiclti.
371 * This function was added in 1.9
372 *
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
376 *
377 */
378function lti_scale_used_anywhere($scaleid) {
379 global $DB;
380
381 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
382 return true;
383 } else {
384 return false;
385 }
386}
387
388/**
389 * Execute post-install custom actions for the module
390 * This function was added in 1.9
391 *
392 * @return boolean true if success, false on error
393 */
394function lti_install() {
395 return true;
396}
397
398/**
399 * Execute post-uninstall custom actions for the module
400 * This function was added in 1.9
401 *
402 * @return boolean true if success, false on error
403 */
404function lti_uninstall() {
405 return true;
406}
407
408/**
409 * Returns available Basic LTI types
410 *
411 * @return array of basicLTI types
412 */
413function lti_get_lti_types() {
414 global $DB;
415
416 return $DB->get_records('lti_types');
417}
418
aa6eca66
CS
419/**
420 * Create grade item for given basiclti
421 *
a153c9f2 422 * @category grade
aa6eca66
CS
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
426 */
427function lti_grade_item_update($basiclti, $grades=null) {
428 global $CFG;
429 require_once($CFG->libdir.'/gradelib.php');
430
431 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
432
433 if ($basiclti->grade > 0) {
434 $params['gradetype'] = GRADE_TYPE_VALUE;
435 $params['grademax'] = $basiclti->grade;
436 $params['grademin'] = 0;
437
438 } else if ($basiclti->grade < 0) {
439 $params['gradetype'] = GRADE_TYPE_SCALE;
440 $params['scaleid'] = -$basiclti->grade;
441
442 } else {
443 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
444 }
445
446 if ($grades === 'reset') {
447 $params['reset'] = true;
448 $grades = null;
449 }
450
451 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
452}
453
454/**
455 * Delete grade item for given basiclti
456 *
a153c9f2 457 * @category grade
aa6eca66
CS
458 * @param object $basiclti object
459 * @return object basiclti
460 */
461function lti_grade_item_delete($basiclti) {
462 global $CFG;
463 require_once($CFG->libdir.'/gradelib.php');
464
465 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
466}
467
f4f711d7
CS
468function lti_extend_settings_navigation($settings, $parentnode) {
469 global $PAGE;
e27cb316 470
c288a3db 471 if (has_capability('mod/lti:grade', context_module::instance($PAGE->cm->id))) {
c4d80efe 472 $keys = $parentnode->get_children_key_list();
e27cb316 473
c4d80efe
CS
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');
477
478 $parentnode->add_node($node, $keys[1]);
479 }
61eb12d4 480}