Moodle release 2.2beta
[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
aa6eca66
CS
51/**
52 * List of features supported in URL module
53 * @param string $feature FEATURE_xx constant for requested feature
54 * @return mixed True if module supports feature, false if not, null if doesn't know
55 */
56function lti_supports($feature) {
57 switch($feature) {
58 case FEATURE_GROUPS: return false;
59 case FEATURE_GROUPINGS: return false;
60 case FEATURE_GROUPMEMBERSONLY: return true;
61 case FEATURE_MOD_INTRO: return true;
62 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
63 case FEATURE_GRADE_HAS_GRADE: return true;
64 case FEATURE_GRADE_OUTCOMES: return true;
65 case FEATURE_BACKUP_MOODLE2: return true;
66
67 default: return null;
68 }
69}
70
71/**
72 * Given an object containing all the necessary data,
73 * (defined by the form in mod.html) this function
74 * will create a new instance and return the id number
75 * of the new instance.
76 *
77 * @param object $instance An object from the form in mod.html
78 * @return int The id of the newly inserted basiclti record
79 **/
c69bd1af 80function lti_add_instance($lti, $mform) {
42a2c7f1
CS
81 global $DB, $CFG;
82 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 83
c69bd1af
EL
84 $lti->timecreated = time();
85 $lti->timemodified = $lti->timecreated;
86 $lti->servicesalt = uniqid('', true);
e27cb316 87
c69bd1af
EL
88 if (!isset($lti->grade)) {
89 $lti->grade = 100; // TODO: Why is this harcoded here and default @ DB
90 }
aa6eca66 91
c69bd1af 92 $lti->id = $DB->insert_record('lti', $lti);
e27cb316 93
c69bd1af
EL
94 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
95 if (!isset($lti->cmidnumber)) {
96 $lti->cmidnumber = '';
32c079dc 97 }
e27cb316 98
c69bd1af 99 lti_grade_item_update($lti);
aa6eca66
CS
100 }
101
c69bd1af 102 return $lti->id;
aa6eca66
CS
103}
104
105/**
106 * Given an object containing all the necessary data,
107 * (defined by the form in mod.html) this function
108 * will update an existing instance with new data.
109 *
110 * @param object $instance An object from the form in mod.html
111 * @return boolean Success/Fail
112 **/
c69bd1af 113function lti_update_instance($lti, $mform) {
42a2c7f1
CS
114 global $DB, $CFG;
115 require_once($CFG->dirroot.'/mod/lti/locallib.php');
aa6eca66 116
c69bd1af
EL
117 $lti->timemodified = time();
118 $lti->id = $lti->instance;
aa6eca66 119
c69bd1af
EL
120 if (!isset($lti->showtitle)) {
121 $lti->showtitle = 0;
aa6eca66 122 }
e27cb316 123
c69bd1af
EL
124 if (!isset($lti->showdescription)) {
125 $lti->showdescription = 0;
aa6eca66 126 }
e27cb316 127
c69bd1af
EL
128 if (!isset($lti->grade)) {
129 $lti->grade = $DB->get_field('lti', 'grade', array('id' => $lti->id));
130 }
e27cb316 131
c69bd1af
EL
132 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
133 lti_grade_item_update($lti);
aa6eca66 134 } else {
c69bd1af 135 lti_grade_item_delete($lti);
aa6eca66
CS
136 }
137
c69bd1af 138 return $DB->update_record('lti', $lti);
aa6eca66
CS
139}
140
141/**
142 * Given an ID of an instance of this module,
143 * this function will permanently delete the instance
144 * and any data that depends on it.
145 *
146 * @param int $id Id of the module instance
147 * @return boolean Success/Failure
148 **/
149function lti_delete_instance($id) {
150 global $DB;
151
152 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
153 return false;
154 }
155
156 $result = true;
157
158 # Delete any dependent records here #
159 lti_grade_item_delete($basiclti);
160
161 return $DB->delete_records("lti", array("id" => $basiclti->id));
162}
163
6c64e8ac
EL
164/**
165 * Given a coursemodule object, this function returns the extra
166 * information needed to print this activity in various places.
167 * For this module we just need to support external urls as
168 * activity icons
169 *
170 * @param cm_info $coursemodule
171 * @return cached_cm_info info
172 */
ea04a9f9 173function lti_get_coursemodule_info($coursemodule) {
42a2c7f1
CS
174 global $DB, $CFG;
175 require_once($CFG->dirroot.'/mod/lti/locallib.php');
e27cb316 176
6c64e8ac
EL
177 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
178 'icon, secureicon')) {
179 return null;
180 }
c07aec16 181
6c64e8ac 182 $info = new cached_cm_info();
e27cb316 183
6c64e8ac
EL
184 // We want to use the right icon based on whether the
185 // current page is being requested over http or https.
ea04a9f9 186 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
6c64e8ac
EL
187 $info->iconurl = new moodle_url($lti->secureicon);
188 } else if (!empty($lti->icon)) {
189 $info->iconurl = new moodle_url($lti->icon);
c07aec16 190 }
e27cb316 191
c07aec16
CS
192 return $info;
193}
194
aa6eca66
CS
195/**
196 * Return a small object with summary information about what a
197 * user has done with a given particular instance of this module
198 * Used for user activity reports.
199 * $return->time = the time they did it
200 * $return->info = a short text description
201 *
202 * @return null
203 * @TODO: implement this moodle function (if needed)
204 **/
205function lti_user_outline($course, $user, $mod, $basiclti) {
64ce589b 206 return null;
aa6eca66
CS
207}
208
209/**
210 * Print a detailed representation of what a user has done with
211 * a given particular instance of this module, for user activity reports.
212 *
213 * @return boolean
214 * @TODO: implement this moodle function (if needed)
215 **/
216function lti_user_complete($course, $user, $mod, $basiclti) {
217 return true;
218}
219
220/**
221 * Given a course and a time, this module should find recent activity
222 * that has occurred in basiclti activities and print it out.
223 * Return true if there was output, or false is there was none.
224 *
225 * @uses $CFG
226 * @return boolean
227 * @TODO: implement this moodle function
228 **/
229function lti_print_recent_activity($course, $isteacher, $timestart) {
230 return false; // True if anything was printed, otherwise false
231}
232
233/**
234 * Function to be run periodically according to the moodle cron
235 * This function searches for things that need to be done, such
236 * as sending out mail, toggling flags etc ...
237 *
238 * @uses $CFG
239 * @return boolean
240 **/
241function lti_cron () {
242 return true;
243}
244
245/**
246 * Must return an array of grades for a given instance of this module,
247 * indexed by user. It also returns a maximum allowed grade.
248 *
249 * Example:
250 * $return->grades = array of grades;
251 * $return->maxgrade = maximum allowed grade;
252 *
253 * return $return;
254 *
255 * @param int $basicltiid ID of an instance of this module
256 * @return mixed Null or object with an array of grades and with the maximum grade
257 *
258 * @TODO: implement this moodle function (if needed)
259 **/
260function lti_grades($basicltiid) {
261 return null;
262}
263
264/**
265 * Must return an array of user records (all data) who are participants
266 * for a given instance of basiclti. Must include every user involved
267 * in the instance, independient of his role (student, teacher, admin...)
268 * See other modules as example.
269 *
270 * @param int $basicltiid ID of an instance of this module
271 * @return mixed boolean/array of students
272 *
273 * @TODO: implement this moodle function
274 **/
275function lti_get_participants($basicltiid) {
276 return false;
277}
278
279/**
280 * This function returns if a scale is being used by one basiclti
281 * it it has support for grading and scales. Commented code should be
282 * modified if necessary. See forum, glossary or journal modules
283 * as reference.
284 *
285 * @param int $basicltiid ID of an instance of this module
286 * @return mixed
287 *
288 * @TODO: implement this moodle function (if needed)
289 **/
290function lti_scale_used ($basicltiid, $scaleid) {
291 $return = false;
292
293 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
294 //
295 //if (!empty($rec) && !empty($scaleid)) {
296 // $return = true;
297 //}
298
299 return $return;
300}
301
302/**
303 * Checks if scale is being used by any instance of basiclti.
304 * This function was added in 1.9
305 *
306 * This is used to find out if scale used anywhere
307 * @param $scaleid int
308 * @return boolean True if the scale is used by any basiclti
309 *
310 */
311function lti_scale_used_anywhere($scaleid) {
312 global $DB;
313
314 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
315 return true;
316 } else {
317 return false;
318 }
319}
320
321/**
322 * Execute post-install custom actions for the module
323 * This function was added in 1.9
324 *
325 * @return boolean true if success, false on error
326 */
327function lti_install() {
328 return true;
329}
330
331/**
332 * Execute post-uninstall custom actions for the module
333 * This function was added in 1.9
334 *
335 * @return boolean true if success, false on error
336 */
337function lti_uninstall() {
338 return true;
339}
340
341/**
342 * Returns available Basic LTI types
343 *
344 * @return array of basicLTI types
345 */
346function lti_get_lti_types() {
347 global $DB;
348
349 return $DB->get_records('lti_types');
350}
351
aa6eca66
CS
352/**
353 * Create grade item for given basiclti
354 *
355 * @param object $basiclti object with extra cmidnumber
356 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
357 * @return int 0 if ok, error code otherwise
358 */
359function lti_grade_item_update($basiclti, $grades=null) {
360 global $CFG;
361 require_once($CFG->libdir.'/gradelib.php');
362
363 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
364
365 if ($basiclti->grade > 0) {
366 $params['gradetype'] = GRADE_TYPE_VALUE;
367 $params['grademax'] = $basiclti->grade;
368 $params['grademin'] = 0;
369
370 } else if ($basiclti->grade < 0) {
371 $params['gradetype'] = GRADE_TYPE_SCALE;
372 $params['scaleid'] = -$basiclti->grade;
373
374 } else {
375 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
376 }
377
378 if ($grades === 'reset') {
379 $params['reset'] = true;
380 $grades = null;
381 }
382
383 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
384}
385
386/**
387 * Delete grade item for given basiclti
388 *
389 * @param object $basiclti object
390 * @return object basiclti
391 */
392function lti_grade_item_delete($basiclti) {
393 global $CFG;
394 require_once($CFG->libdir.'/gradelib.php');
395
396 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
397}
398
f4f711d7
CS
399function lti_extend_settings_navigation($settings, $parentnode) {
400 global $PAGE;
e27cb316 401
ea04a9f9 402 if (has_capability('mod/lti:grade', get_context_instance(CONTEXT_MODULE, $PAGE->cm->id))) {
c4d80efe 403 $keys = $parentnode->get_children_key_list();
e27cb316 404
c4d80efe
CS
405 $node = navigation_node::create('Submissions',
406 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
407 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
408
409 $parentnode->add_node($node, $keys[1]);
410 }
61eb12d4 411}