MDL-20534 lti: B3, A6, apply new external icons support to lti (MDL-30175)
[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
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
aa6eca66
CS
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);
e27cb316 87
ea04a9f9 88 if (!isset($formdata->grade)) {
aa6eca66
CS
89 $formdata->grade = 100;
90 }
e27cb316 91
aa6eca66
CS
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));
e27cb316 96
ea04a9f9 97 if (!isset($formdata->cmidnumber)) {
32c079dc
CS
98 $formdata->cmidnumber = '';
99 }
e27cb316 100
aa6eca66 101 $basiclti->cmidnumber = $formdata->cmidnumber;
e27cb316 102
aa6eca66
CS
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
ea04a9f9 123 if (!isset($formdata->showtitle)) {
aa6eca66
CS
124 $formdata->showtitle = 0;
125 }
e27cb316 126
ea04a9f9 127 if (!isset($formdata->showdescription)) {
aa6eca66
CS
128 $formdata->showdescription = 0;
129 }
e27cb316 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;
e27cb316 134
aa6eca66
CS
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
6c64e8ac
EL
166/**
167 * Given a coursemodule object, this function returns the extra
168 * information needed to print this activity in various places.
169 * For this module we just need to support external urls as
170 * activity icons
171 *
172 * @param cm_info $coursemodule
173 * @return cached_cm_info info
174 */
ea04a9f9 175function lti_get_coursemodule_info($coursemodule) {
c07aec16 176 global $DB;
e27cb316 177
6c64e8ac
EL
178 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
179 'icon, secureicon')) {
180 return null;
181 }
c07aec16 182
6c64e8ac 183 $info = new cached_cm_info();
e27cb316 184
6c64e8ac
EL
185 // We want to use the right icon based on whether the
186 // current page is being requested over http or https.
ea04a9f9 187 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
6c64e8ac
EL
188 $info->iconurl = new moodle_url($lti->secureicon);
189 } else if (!empty($lti->icon)) {
190 $info->iconurl = new moodle_url($lti->icon);
c07aec16 191 }
e27cb316 192
c07aec16
CS
193 return $info;
194}
195
aa6eca66
CS
196/**
197 * Return a small object with summary information about what a
198 * user has done with a given particular instance of this module
199 * Used for user activity reports.
200 * $return->time = the time they did it
201 * $return->info = a short text description
202 *
203 * @return null
204 * @TODO: implement this moodle function (if needed)
205 **/
206function lti_user_outline($course, $user, $mod, $basiclti) {
207 return $return;
208}
209
210/**
211 * Print a detailed representation of what a user has done with
212 * a given particular instance of this module, for user activity reports.
213 *
214 * @return boolean
215 * @TODO: implement this moodle function (if needed)
216 **/
217function lti_user_complete($course, $user, $mod, $basiclti) {
218 return true;
219}
220
221/**
222 * Given a course and a time, this module should find recent activity
223 * that has occurred in basiclti activities and print it out.
224 * Return true if there was output, or false is there was none.
225 *
226 * @uses $CFG
227 * @return boolean
228 * @TODO: implement this moodle function
229 **/
230function lti_print_recent_activity($course, $isteacher, $timestart) {
231 return false; // True if anything was printed, otherwise false
232}
233
234/**
235 * Function to be run periodically according to the moodle cron
236 * This function searches for things that need to be done, such
237 * as sending out mail, toggling flags etc ...
238 *
239 * @uses $CFG
240 * @return boolean
241 **/
242function lti_cron () {
243 return true;
244}
245
246/**
247 * Must return an array of grades for a given instance of this module,
248 * indexed by user. It also returns a maximum allowed grade.
249 *
250 * Example:
251 * $return->grades = array of grades;
252 * $return->maxgrade = maximum allowed grade;
253 *
254 * return $return;
255 *
256 * @param int $basicltiid ID of an instance of this module
257 * @return mixed Null or object with an array of grades and with the maximum grade
258 *
259 * @TODO: implement this moodle function (if needed)
260 **/
261function lti_grades($basicltiid) {
262 return null;
263}
264
265/**
266 * Must return an array of user records (all data) who are participants
267 * for a given instance of basiclti. Must include every user involved
268 * in the instance, independient of his role (student, teacher, admin...)
269 * See other modules as example.
270 *
271 * @param int $basicltiid ID of an instance of this module
272 * @return mixed boolean/array of students
273 *
274 * @TODO: implement this moodle function
275 **/
276function lti_get_participants($basicltiid) {
277 return false;
278}
279
280/**
281 * This function returns if a scale is being used by one basiclti
282 * it it has support for grading and scales. Commented code should be
283 * modified if necessary. See forum, glossary or journal modules
284 * as reference.
285 *
286 * @param int $basicltiid ID of an instance of this module
287 * @return mixed
288 *
289 * @TODO: implement this moodle function (if needed)
290 **/
291function lti_scale_used ($basicltiid, $scaleid) {
292 $return = false;
293
294 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
295 //
296 //if (!empty($rec) && !empty($scaleid)) {
297 // $return = true;
298 //}
299
300 return $return;
301}
302
303/**
304 * Checks if scale is being used by any instance of basiclti.
305 * This function was added in 1.9
306 *
307 * This is used to find out if scale used anywhere
308 * @param $scaleid int
309 * @return boolean True if the scale is used by any basiclti
310 *
311 */
312function lti_scale_used_anywhere($scaleid) {
313 global $DB;
314
315 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
316 return true;
317 } else {
318 return false;
319 }
320}
321
322/**
323 * Execute post-install custom actions for the module
324 * This function was added in 1.9
325 *
326 * @return boolean true if success, false on error
327 */
328function lti_install() {
329 return true;
330}
331
332/**
333 * Execute post-uninstall custom actions for the module
334 * This function was added in 1.9
335 *
336 * @return boolean true if success, false on error
337 */
338function lti_uninstall() {
339 return true;
340}
341
342/**
343 * Returns available Basic LTI types
344 *
345 * @return array of basicLTI types
346 */
347function lti_get_lti_types() {
348 global $DB;
349
350 return $DB->get_records('lti_types');
351}
352
aa6eca66
CS
353/**
354 * Create grade item for given basiclti
355 *
356 * @param object $basiclti object with extra cmidnumber
357 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
358 * @return int 0 if ok, error code otherwise
359 */
360function lti_grade_item_update($basiclti, $grades=null) {
361 global $CFG;
362 require_once($CFG->libdir.'/gradelib.php');
363
364 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
365
366 if ($basiclti->grade > 0) {
367 $params['gradetype'] = GRADE_TYPE_VALUE;
368 $params['grademax'] = $basiclti->grade;
369 $params['grademin'] = 0;
370
371 } else if ($basiclti->grade < 0) {
372 $params['gradetype'] = GRADE_TYPE_SCALE;
373 $params['scaleid'] = -$basiclti->grade;
374
375 } else {
376 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
377 }
378
379 if ($grades === 'reset') {
380 $params['reset'] = true;
381 $grades = null;
382 }
383
384 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
385}
386
387/**
388 * Delete grade item for given basiclti
389 *
390 * @param object $basiclti object
391 * @return object basiclti
392 */
393function lti_grade_item_delete($basiclti) {
394 global $CFG;
395 require_once($CFG->libdir.'/gradelib.php');
396
397 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
398}
399
f4f711d7
CS
400function lti_extend_settings_navigation($settings, $parentnode) {
401 global $PAGE;
e27cb316 402
ea04a9f9 403 if (has_capability('mod/lti:grade', get_context_instance(CONTEXT_MODULE, $PAGE->cm->id))) {
c4d80efe 404 $keys = $parentnode->get_children_key_list();
e27cb316 405
c4d80efe
CS
406 $node = navigation_node::create('Submissions',
407 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
408 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
409
410 $parentnode->add_node($node, $keys[1]);
411 }
61eb12d4 412}