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