2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 // This file is part of BasicLTI4Moodle
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.
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
36 * This file contains a library of functions and constants for the lti module
40 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
42 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
44 * @author Jordi Piguillem
45 * @author Nikolas Galanis
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 defined('MOODLE_INTERNAL') || die;
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
56 function lti_supports($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 case FEATURE_SHOW_DESCRIPTION: return true;
73 * Given an object containing all the necessary data,
74 * (defined by the form in mod.html) this function
75 * will create a new instance and return the id number
76 * of the new instance.
78 * @param object $instance An object from the form in mod.html
79 * @return int The id of the newly inserted basiclti record
81 function lti_add_instance($lti, $mform) {
83 require_once($CFG->dirroot.'/mod/lti/locallib.php');
85 $lti->timecreated = time();
86 $lti->timemodified = $lti->timecreated;
87 $lti->servicesalt = uniqid('', true);
89 if (!isset($lti->grade)) {
90 $lti->grade = 100; // TODO: Why is this harcoded here and default @ DB
93 $lti->id = $DB->insert_record('lti', $lti);
95 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
96 if (!isset($lti->cmidnumber)) {
97 $lti->cmidnumber = '';
100 lti_grade_item_update($lti);
107 * Given an object containing all the necessary data,
108 * (defined by the form in mod.html) this function
109 * will update an existing instance with new data.
111 * @param object $instance An object from the form in mod.html
112 * @return boolean Success/Fail
114 function lti_update_instance($lti, $mform) {
116 require_once($CFG->dirroot.'/mod/lti/locallib.php');
118 $lti->timemodified = time();
119 $lti->id = $lti->instance;
121 if (!isset($lti->showtitlelaunch)) {
122 $lti->showtitlelaunch = 0;
125 if (!isset($lti->showdescriptionlaunch)) {
126 $lti->showdescriptionlaunch = 0;
129 if (!isset($lti->grade)) {
130 $lti->grade = $DB->get_field('lti', 'grade', array('id' => $lti->id));
133 if ($lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
134 lti_grade_item_update($lti);
136 lti_grade_item_delete($lti);
139 return $DB->update_record('lti', $lti);
143 * Given an ID of an instance of this module,
144 * this function will permanently delete the instance
145 * and any data that depends on it.
147 * @param int $id Id of the module instance
148 * @return boolean Success/Failure
150 function lti_delete_instance($id) {
153 if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
159 # Delete any dependent records here #
160 lti_grade_item_delete($basiclti);
162 return $DB->delete_records("lti", array("id" => $basiclti->id));
166 * Given a coursemodule object, this function returns the extra
167 * information needed to print this activity in various places.
168 * For this module we just need to support external urls as
171 * @param cm_info $coursemodule
172 * @return cached_cm_info info
174 function lti_get_coursemodule_info($coursemodule) {
176 require_once($CFG->dirroot.'/mod/lti/locallib.php');
178 if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
179 'icon, secureicon, intro, introformat, name')) {
183 $info = new cached_cm_info();
185 // We want to use the right icon based on whether the
186 // current page is being requested over http or https.
187 if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
188 $info->iconurl = new moodle_url($lti->secureicon);
189 } else if (!empty($lti->icon)) {
190 $info->iconurl = new moodle_url($lti->icon);
193 if ($coursemodule->showdescription) {
194 // Convert intro to html. Do not filter cached version, filters run at display time.
195 $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
198 $info->name = $lti->name;
204 * Return a small object with summary information about what a
205 * user has done with a given particular instance of this module
206 * Used for user activity reports.
207 * $return->time = the time they did it
208 * $return->info = a short text description
211 * @TODO: implement this moodle function (if needed)
213 function lti_user_outline($course, $user, $mod, $basiclti) {
218 * Print a detailed representation of what a user has done with
219 * a given particular instance of this module, for user activity reports.
222 * @TODO: implement this moodle function (if needed)
224 function lti_user_complete($course, $user, $mod, $basiclti) {
229 * Given a course and a time, this module should find recent activity
230 * that has occurred in basiclti activities and print it out.
231 * Return true if there was output, or false is there was none.
235 * @TODO: implement this moodle function
237 function lti_print_recent_activity($course, $isteacher, $timestart) {
238 return false; // True if anything was printed, otherwise false
242 * Function to be run periodically according to the moodle cron
243 * This function searches for things that need to be done, such
244 * as sending out mail, toggling flags etc ...
249 function lti_cron () {
254 * Must return an array of grades for a given instance of this module,
255 * indexed by user. It also returns a maximum allowed grade.
258 * $return->grades = array of grades;
259 * $return->maxgrade = maximum allowed grade;
263 * @param int $basicltiid ID of an instance of this module
264 * @return mixed Null or object with an array of grades and with the maximum grade
266 * @TODO: implement this moodle function (if needed)
268 function lti_grades($basicltiid) {
273 * Must return an array of user records (all data) who are participants
274 * for a given instance of basiclti. Must include every user involved
275 * in the instance, independient of his role (student, teacher, admin...)
276 * See other modules as example.
278 * @param int $basicltiid ID of an instance of this module
279 * @return mixed boolean/array of students
281 * @TODO: implement this moodle function
283 function lti_get_participants($basicltiid) {
288 * This function returns if a scale is being used by one basiclti
289 * it it has support for grading and scales. Commented code should be
290 * modified if necessary. See forum, glossary or journal modules
293 * @param int $basicltiid ID of an instance of this module
296 * @TODO: implement this moodle function (if needed)
298 function lti_scale_used ($basicltiid, $scaleid) {
301 //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
303 //if (!empty($rec) && !empty($scaleid)) {
311 * Checks if scale is being used by any instance of basiclti.
312 * This function was added in 1.9
314 * This is used to find out if scale used anywhere
315 * @param $scaleid int
316 * @return boolean True if the scale is used by any basiclti
319 function lti_scale_used_anywhere($scaleid) {
322 if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
330 * Execute post-install custom actions for the module
331 * This function was added in 1.9
333 * @return boolean true if success, false on error
335 function lti_install() {
340 * Execute post-uninstall custom actions for the module
341 * This function was added in 1.9
343 * @return boolean true if success, false on error
345 function lti_uninstall() {
350 * Returns available Basic LTI types
352 * @return array of basicLTI types
354 function lti_get_lti_types() {
357 return $DB->get_records('lti_types');
361 * Create grade item for given basiclti
363 * @param object $basiclti object with extra cmidnumber
364 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
365 * @return int 0 if ok, error code otherwise
367 function lti_grade_item_update($basiclti, $grades=null) {
369 require_once($CFG->libdir.'/gradelib.php');
371 $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
373 if ($basiclti->grade > 0) {
374 $params['gradetype'] = GRADE_TYPE_VALUE;
375 $params['grademax'] = $basiclti->grade;
376 $params['grademin'] = 0;
378 } else if ($basiclti->grade < 0) {
379 $params['gradetype'] = GRADE_TYPE_SCALE;
380 $params['scaleid'] = -$basiclti->grade;
383 $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
386 if ($grades === 'reset') {
387 $params['reset'] = true;
391 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
395 * Delete grade item for given basiclti
397 * @param object $basiclti object
398 * @return object basiclti
400 function lti_grade_item_delete($basiclti) {
402 require_once($CFG->libdir.'/gradelib.php');
404 return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
407 function lti_extend_settings_navigation($settings, $parentnode) {
410 if (has_capability('mod/lti:grade', get_context_instance(CONTEXT_MODULE, $PAGE->cm->id))) {
411 $keys = $parentnode->get_children_key_list();
413 $node = navigation_node::create('Submissions',
414 new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
415 navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
417 $parentnode->add_node($node, $keys[1]);