MDL-20534 lti: B3, revert old outputlib hack for external icons
[moodle.git] / mod / lti / lib.php
1 <?php
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 //
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
35 /**
36  * This file contains a library of functions and constants for the lti module
37  *
38  * @package    mod
39  * @subpackage lti
40  * @copyright  2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41  *  marc.alier@upc.edu
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
47  */
49 defined('MOODLE_INTERNAL') || die;
51 require_once($CFG->dirroot.'/mod/lti/locallib.php');
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  */
58 function 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;
69         default: return null;
70     }
71 }
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  **/
82 function lti_add_instance($formdata) {
83     global $DB;
84     $formdata->timecreated = time();
85     $formdata->timemodified = $formdata->timecreated;
86     $formdata->servicesalt = uniqid('', true);
88     if (!isset($formdata->grade)) {
89         $formdata->grade = 100;
90     }
92     $id = $DB->insert_record("lti", $formdata);
94     if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
95         $basiclti = $DB->get_record('lti', array('id'=>$id));
97         if (!isset($formdata->cmidnumber)) {
98             $formdata->cmidnumber = '';
99         }
101         $basiclti->cmidnumber = $formdata->cmidnumber;
103         lti_grade_item_update($basiclti);
104     }
106     return $id;
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  **/
117 function lti_update_instance($formdata) {
118     global $DB;
120     $formdata->timemodified = time();
121     $formdata->id = $formdata->instance;
123     if (!isset($formdata->showtitle)) {
124         $formdata->showtitle = 0;
125     }
127     if (!isset($formdata->showdescription)) {
128         $formdata->showdescription = 0;
129     }
131     if ($formdata->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
132         $basicltirec = $DB->get_record("lti", array("id" => $formdata->id));
133         $basicltirec->cmidnumber = $formdata->cmidnumber;
135         lti_grade_item_update($basicltirec);
136     } else {
137         lti_grade_item_delete($formdata);
138     }
140     return $DB->update_record("lti", $formdata);
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  **/
151 function lti_delete_instance($id) {
152     global $DB;
154     if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
155         return false;
156     }
158     $result = true;
160     # Delete any dependent records here #
161     lti_grade_item_delete($basiclti);
163     return $DB->delete_records("lti", array("id" => $basiclti->id));
166 function lti_get_coursemodule_info($coursemodule) {
167     global $DB;
169     $lti = $DB->get_record('lti', array('id' => $coursemodule->instance), 'icon, secureicon');
171     $info = new stdClass();
173     //We want to use the right icon based on whether the current page is being requested over http or https.
174     //There's a potential problem here as the icon URLs are cached in the modinfo field and won't be updated for each request.
175     if (lti_request_is_using_ssl() && !empty($lti->secureicon)) {
176         $info->icon = $lti->secureicon;
177     } else {
178         if (!empty($lti->icon)) {
179             $info->icon = $lti->icon;
180         }
181     }
183     return $info;
186 /**
187  * Return a small object with summary information about what a
188  * user has done with a given particular instance of this module
189  * Used for user activity reports.
190  * $return->time = the time they did it
191  * $return->info = a short text description
192  *
193  * @return null
194  * @TODO: implement this moodle function (if needed)
195  **/
196 function lti_user_outline($course, $user, $mod, $basiclti) {
197     return $return;
200 /**
201  * Print a detailed representation of what a user has done with
202  * a given particular instance of this module, for user activity reports.
203  *
204  * @return boolean
205  * @TODO: implement this moodle function (if needed)
206  **/
207 function lti_user_complete($course, $user, $mod, $basiclti) {
208     return true;
211 /**
212  * Given a course and a time, this module should find recent activity
213  * that has occurred in basiclti activities and print it out.
214  * Return true if there was output, or false is there was none.
215  *
216  * @uses $CFG
217  * @return boolean
218  * @TODO: implement this moodle function
219  **/
220 function lti_print_recent_activity($course, $isteacher, $timestart) {
221     return false;  //  True if anything was printed, otherwise false
224 /**
225  * Function to be run periodically according to the moodle cron
226  * This function searches for things that need to be done, such
227  * as sending out mail, toggling flags etc ...
228  *
229  * @uses $CFG
230  * @return boolean
231  **/
232 function lti_cron () {
233     return true;
236 /**
237  * Must return an array of grades for a given instance of this module,
238  * indexed by user.  It also returns a maximum allowed grade.
239  *
240  * Example:
241  *    $return->grades = array of grades;
242  *    $return->maxgrade = maximum allowed grade;
243  *
244  *    return $return;
245  *
246  * @param int $basicltiid ID of an instance of this module
247  * @return mixed Null or object with an array of grades and with the maximum grade
248  *
249  * @TODO: implement this moodle function (if needed)
250  **/
251 function lti_grades($basicltiid) {
252     return null;
255 /**
256  * Must return an array of user records (all data) who are participants
257  * for a given instance of basiclti. Must include every user involved
258  * in the instance, independient of his role (student, teacher, admin...)
259  * See other modules as example.
260  *
261  * @param int $basicltiid ID of an instance of this module
262  * @return mixed boolean/array of students
263  *
264  * @TODO: implement this moodle function
265  **/
266 function lti_get_participants($basicltiid) {
267     return false;
270 /**
271  * This function returns if a scale is being used by one basiclti
272  * it it has support for grading and scales. Commented code should be
273  * modified if necessary. See forum, glossary or journal modules
274  * as reference.
275  *
276  * @param int $basicltiid ID of an instance of this module
277  * @return mixed
278  *
279  * @TODO: implement this moodle function (if needed)
280  **/
281 function lti_scale_used ($basicltiid, $scaleid) {
282     $return = false;
284     //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
285     //
286     //if (!empty($rec)  && !empty($scaleid)) {
287     //    $return = true;
288     //}
290     return $return;
293 /**
294  * Checks if scale is being used by any instance of basiclti.
295  * This function was added in 1.9
296  *
297  * This is used to find out if scale used anywhere
298  * @param $scaleid int
299  * @return boolean True if the scale is used by any basiclti
300  *
301  */
302 function lti_scale_used_anywhere($scaleid) {
303     global $DB;
305     if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
306         return true;
307     } else {
308         return false;
309     }
312 /**
313  * Execute post-install custom actions for the module
314  * This function was added in 1.9
315  *
316  * @return boolean true if success, false on error
317  */
318 function lti_install() {
319      return true;
322 /**
323  * Execute post-uninstall custom actions for the module
324  * This function was added in 1.9
325  *
326  * @return boolean true if success, false on error
327  */
328 function lti_uninstall() {
329     return true;
332 /**
333  * Returns available Basic LTI types
334  *
335  * @return array of basicLTI types
336  */
337 function lti_get_lti_types() {
338     global $DB;
340     return $DB->get_records('lti_types');
343 /**
344  * Create grade item for given basiclti
345  *
346  * @param object $basiclti object with extra cmidnumber
347  * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
348  * @return int 0 if ok, error code otherwise
349  */
350 function lti_grade_item_update($basiclti, $grades=null) {
351     global $CFG;
352     require_once($CFG->libdir.'/gradelib.php');
354     $params = array('itemname'=>$basiclti->name, 'idnumber'=>$basiclti->cmidnumber);
356     if ($basiclti->grade > 0) {
357         $params['gradetype'] = GRADE_TYPE_VALUE;
358         $params['grademax']  = $basiclti->grade;
359         $params['grademin']  = 0;
361     } else if ($basiclti->grade < 0) {
362         $params['gradetype'] = GRADE_TYPE_SCALE;
363         $params['scaleid']   = -$basiclti->grade;
365     } else {
366         $params['gradetype'] = GRADE_TYPE_TEXT; // allow text comments only
367     }
369     if ($grades  === 'reset') {
370         $params['reset'] = true;
371         $grades = null;
372     }
374     return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
377 /**
378  * Delete grade item for given basiclti
379  *
380  * @param object $basiclti object
381  * @return object basiclti
382  */
383 function lti_grade_item_delete($basiclti) {
384     global $CFG;
385     require_once($CFG->libdir.'/gradelib.php');
387     return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted'=>1));
390 function lti_extend_settings_navigation($settings, $parentnode) {
391     global $PAGE;
393     if (has_capability('mod/lti:grade', get_context_instance(CONTEXT_MODULE, $PAGE->cm->id))) {
394         $keys = $parentnode->get_children_key_list();
396         $node = navigation_node::create('Submissions',
397             new moodle_url('/mod/lti/grade.php', array('id'=>$PAGE->cm->id)),
398             navigation_node::TYPE_SETTING, null, 'mod_lti_submissions');
400         $parentnode->add_node($node, $keys[1]);
401     }