Fix a bug introduced recently that broke launches (adding the return url after signin...
[moodle.git] / mod / lti / lib.php
CommitLineData
aa6eca66
CS
1<?php
2// This file is part of BasicLTI4Moodle
3//
4// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
5// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
6// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
7// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
8// are already supporting or going to support BasicLTI. This project Implements the consumer
9// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
10// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
11// at the GESSI research group at UPC.
12// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
13// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
14// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
15//
16// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
17// of the Universitat Politecnica de Catalunya http://www.upc.edu
18// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu
19//
20// Moodle is free software: you can redistribute it and/or modify
21// it under the terms of the GNU General Public License as published by
22// the Free Software Foundation, either version 3 of the License, or
23// (at your option) any later version.
24//
25// Moodle is distributed in the hope that it will be useful,
26// but WITHOUT ANY WARRANTY; without even the implied warranty of
27// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28// GNU General Public License for more details.
29//
30// You should have received a copy of the GNU General Public License
31// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
32
33/**
34 * This file contains a library of functions and constants for the
35 * BasicLTI module
36 *
37 * @package lti
38 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
39 * marc.alier@upc.edu
40 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
41 *
42 * @author Marc Alier
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 *
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 */
48
49defined('MOODLE_INTERNAL') || die;
50
51require_once($CFG->dirroot.'/mod/lti/locallib.php');
52
53/**
54 * List of features supported in URL module
55 * @param string $feature FEATURE_xx constant for requested feature
56 * @return mixed True if module supports feature, false if not, null if doesn't know
57 */
58function lti_supports($feature) {
59 switch($feature) {
60 case FEATURE_GROUPS: return false;
61 case FEATURE_GROUPINGS: return false;
62 case FEATURE_GROUPMEMBERSONLY: return true;
63 case FEATURE_MOD_INTRO: return true;
64 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
65 case FEATURE_GRADE_HAS_GRADE: return true;
66 case FEATURE_GRADE_OUTCOMES: return true;
67 case FEATURE_BACKUP_MOODLE2: return true;
68
69 default: return null;
70 }
71}
72
73/**
74 * Given an object containing all the necessary data,
75 * (defined by the form in mod.html) this function
76 * will create a new instance and return the id number
77 * of the new instance.
78 *
79 * @param object $instance An object from the form in mod.html
80 * @return int The id of the newly inserted basiclti record
81 **/
82function lti_add_instance($formdata) {
83 global $DB;
84 $formdata->timecreated = time();
85 $formdata->timemodified = $formdata->timecreated;
86 $formdata->servicesalt = uniqid('', true);
87
88 if(!isset($formdata->grade)){
89 $formdata->grade = 100;
90 }
91
92 $id = $DB->insert_record("lti", $formdata);
93
5e078d62 94 if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
aa6eca66 95 $basiclti = $DB->get_record('lti', array('id'=>$id));
32c079dc
CS
96
97 if(!isset($formdata->cmidnumber)){
98 $formdata->cmidnumber = '';
99 }
100
aa6eca66
CS
101 $basiclti->cmidnumber = $formdata->cmidnumber;
102
103 lti_grade_item_update($basiclti);
104 }
105
106 return $id;
107}
108
109/**
110 * Given an object containing all the necessary data,
111 * (defined by the form in mod.html) this function
112 * will update an existing instance with new data.
113 *
114 * @param object $instance An object from the form in mod.html
115 * @return boolean Success/Fail
116 **/
117function lti_update_instance($formdata) {
118 global $DB;
119
120 $formdata->timemodified = time();
121 $formdata->id = $formdata->instance;
122
123 if(!isset($formdata->showtitle)){
124 $formdata->showtitle = 0;
125 }
126
127 if(!isset($formdata->showdescription)){
128 $formdata->showdescription = 0;
129 }
130
5e078d62 131 if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
aa6eca66
CS
132 $basicltirec = $DB->get_record("lti", array("id" => $formdata->id));
133 $basicltirec->cmidnumber = $formdata->cmidnumber;
134
135 lti_grade_item_update($basicltirec);
136 } else {
137 lti_grade_item_delete($formdata);
138 }
139
140 return $DB->update_record("lti", $formdata);
141}
142
143/**
144 * Given an ID of an instance of this module,
145 * this function will permanently delete the instance
146 * and any data that depends on it.
147 *
148 * @param int $id Id of the module instance
149 * @return boolean Success/Failure
150 **/
151function lti_delete_instance($id) {
152 global $DB;
153
154 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
155 return false;
156 }
157
158 $result = true;
159
160 # Delete any dependent records here #
161 lti_grade_item_delete($basiclti);
162
163 return $DB->delete_records("lti", array("id" => $basiclti->id));
164}
165
c07aec16
CS
166function lti_get_coursemodule_info($coursemodule){
167 global $DB;
168
169 $lti = $DB->get_record('lti', array('id' => $coursemodule->instance), 'icon');
170
171 $info = new stdClass();
172
173 if(!empty($lti->icon)){
174 $info->icon = $lti->icon;
175 }
176
177 return $info;
178}
179
aa6eca66
CS
180/**
181 * Return a small object with summary information about what a
182 * user has done with a given particular instance of this module
183 * Used for user activity reports.
184 * $return->time = the time they did it
185 * $return->info = a short text description
186 *
187 * @return null
188 * @TODO: implement this moodle function (if needed)
189 **/
190function lti_user_outline($course, $user, $mod, $basiclti) {
191 return $return;
192}
193
194/**
195 * Print a detailed representation of what a user has done with
196 * a given particular instance of this module, for user activity reports.
197 *
198 * @return boolean
199 * @TODO: implement this moodle function (if needed)
200 **/
201function lti_user_complete($course, $user, $mod, $basiclti) {
202 return true;
203}
204
205/**
206 * Given a course and a time, this module should find recent activity
207 * that has occurred in basiclti activities and print it out.
208 * Return true if there was output, or false is there was none.
209 *
210 * @uses $CFG
211 * @return boolean
212 * @TODO: implement this moodle function
213 **/
214function lti_print_recent_activity($course, $isteacher, $timestart) {
215 return false; // True if anything was printed, otherwise false
216}
217
218/**
219 * Function to be run periodically according to the moodle cron
220 * This function searches for things that need to be done, such
221 * as sending out mail, toggling flags etc ...
222 *
223 * @uses $CFG
224 * @return boolean
225 **/
226function lti_cron () {
227 return true;
228}
229
230/**
231 * Must return an array of grades for a given instance of this module,
232 * indexed by user. It also returns a maximum allowed grade.
233 *
234 * Example:
235 * $return->grades = array of grades;
236 * $return->maxgrade = maximum allowed grade;
237 *
238 * return $return;
239 *
240 * @param int $basicltiid ID of an instance of this module
241 * @return mixed Null or object with an array of grades and with the maximum grade
242 *
243 * @TODO: implement this moodle function (if needed)
244 **/
245function lti_grades($basicltiid) {
246 return null;
247}
248
249/**
250 * Must return an array of user records (all data) who are participants
251 * for a given instance of basiclti. Must include every user involved
252 * in the instance, independient of his role (student, teacher, admin...)
253 * See other modules as example.
254 *
255 * @param int $basicltiid ID of an instance of this module
256 * @return mixed boolean/array of students
257 *
258 * @TODO: implement this moodle function
259 **/
260function lti_get_participants($basicltiid) {
261 return false;
262}
263
264/**
265 * This function returns if a scale is being used by one basiclti
266 * it it has support for grading and scales. Commented code should be
267 * modified if necessary. See forum, glossary or journal modules
268 * as reference.
269 *
270 * @param int $basicltiid ID of an instance of this module
271 * @return mixed
272 *
273 * @TODO: implement this moodle function (if needed)
274 **/
275function lti_scale_used ($basicltiid, $scaleid) {
276 $return = false;
277
278 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
279 //
280 //if (!empty($rec) && !empty($scaleid)) {
281 // $return = true;
282 //}
283
284 return $return;
285}
286
287/**
288 * Checks if scale is being used by any instance of basiclti.
289 * This function was added in 1.9
290 *
291 * This is used to find out if scale used anywhere
292 * @param $scaleid int
293 * @return boolean True if the scale is used by any basiclti
294 *
295 */
296function lti_scale_used_anywhere($scaleid) {
297 global $DB;
298
299 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
300 return true;
301 } else {
302 return false;
303 }
304}
305
306/**
307 * Execute post-install custom actions for the module
308 * This function was added in 1.9
309 *
310 * @return boolean true if success, false on error
311 */
312function lti_install() {
313 return true;
314}
315
316/**
317 * Execute post-uninstall custom actions for the module
318 * This function was added in 1.9
319 *
320 * @return boolean true if success, false on error
321 */
322function lti_uninstall() {
323 return true;
324}
325
326/**
327 * Returns available Basic LTI types
328 *
329 * @return array of basicLTI types
330 */
331function lti_get_lti_types() {
332 global $DB;
333
334 return $DB->get_records('lti_types');
335}
336
aa6eca66
CS
337/**
338 * Create grade item for given basiclti
339 *
340 * @param object $basiclti object with extra cmidnumber
341 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
342 * @return int 0 if ok, error code otherwise
343 */
344function lti_grade_item_update($basiclti, $grades=null) {
345 global $CFG;
346 require_once($CFG->libdir.'/gradelib.php');
347
348 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
349
350 if ($basiclti->grade > 0) {
351 $params['gradetype'] = GRADE_TYPE_VALUE;
352 $params['grademax'] = $basiclti->grade;
353 $params['grademin'] = 0;
354
355 } else if ($basiclti->grade < 0) {
356 $params['gradetype'] = GRADE_TYPE_SCALE;
357 $params['scaleid'] = -$basiclti->grade;
358
359 } else {
360 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
361 }
362
363 if ($grades === 'reset') {
364 $params['reset'] = true;
365 $grades = null;
366 }
367
368 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
369}
370
371/**
372 * Delete grade item for given basiclti
373 *
374 * @param object $basiclti object
375 * @return object basiclti
376 */
377function lti_grade_item_delete($basiclti) {
378 global $CFG;
379 require_once($CFG->libdir.'/gradelib.php');
380
381 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
382}
383
f4f711d7
CS
384function lti_extend_settings_navigation($settings, $parentnode) {
385 global $PAGE;
386
387 $keys = $parentnode->get_children_key_list();
388
389 $node = navigation_node::create('Submissions',
390 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
391 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
392
393 $parentnode->add_node($node, $keys[1]);
394}