Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
34
 
35
/**
36
 * This file contains a library of functions and constants for the lti module
37
 *
38
 * @package mod_lti
39
 * @copyright  2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
40
 *  marc.alier@upc.edu
41
 * @copyright  2009 Universitat Politecnica de Catalunya http://www.upc.edu
42
 * @author     Marc Alier
43
 * @author     Jordi Piguillem
44
 * @author     Nikolas Galanis
45
 * @author     Chris Scribner
46
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 */
48
 
49
defined('MOODLE_INTERNAL') || die;
50
 
1441 ariadna 51
require_once(__DIR__ . '/deprecatedlib.php');
52
 
1 efrain 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 or string for the module purpose.
57
 */
58
function lti_supports($feature) {
59
    switch ($feature) {
60
        case FEATURE_GROUPS:
61
        case FEATURE_GROUPINGS:
62
            return false;
63
        case FEATURE_MOD_INTRO:
64
        case FEATURE_COMPLETION_TRACKS_VIEWS:
65
        case FEATURE_GRADE_HAS_GRADE:
66
        case FEATURE_GRADE_OUTCOMES:
67
        case FEATURE_BACKUP_MOODLE2:
68
        case FEATURE_SHOW_DESCRIPTION:
69
            return true;
70
        case FEATURE_MOD_PURPOSE:
71
            return MOD_PURPOSE_OTHER;
72
 
73
        default:
74
            return null;
75
    }
76
}
77
 
78
/**
79
 * Given an object containing all the necessary data,
80
 * (defined by the form in mod.html) this function
81
 * will create a new instance and return the id number
82
 * of the new instance.
83
 *
84
 * @param object $instance An object from the form in mod.html
85
 * @return int The id of the newly inserted basiclti record
86
 **/
87
function lti_add_instance($lti, $mform) {
88
    global $DB, $CFG;
89
    require_once($CFG->dirroot.'/mod/lti/locallib.php');
90
 
91
    if (!isset($lti->toolurl)) {
92
        $lti->toolurl = '';
93
    }
94
 
95
    lti_load_tool_if_cartridge($lti);
96
 
97
    $lti->timecreated = time();
98
    $lti->timemodified = $lti->timecreated;
99
    $lti->servicesalt = uniqid('', true);
100
    if (!isset($lti->typeid)) {
101
        $lti->typeid = null;
102
    }
103
 
104
    lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
105
 
106
    if (empty($lti->typeid) && isset($lti->urlmatchedtypeid)) {
107
        $lti->typeid = $lti->urlmatchedtypeid;
108
    }
109
 
110
    if (!isset($lti->instructorchoiceacceptgrades) || $lti->instructorchoiceacceptgrades != LTI_SETTING_ALWAYS) {
111
        // The instance does not accept grades back from the provider, so set to "No grade" value 0.
112
        $lti->grade = 0;
113
    }
114
 
115
    $lti->id = $DB->insert_record('lti', $lti);
116
 
117
    if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
118
        if (!isset($lti->cmidnumber)) {
119
            $lti->cmidnumber = '';
120
        }
121
 
122
        lti_grade_item_update($lti);
123
    }
124
 
125
    $services = lti_get_services();
126
    foreach ($services as $service) {
127
        $service->instance_added( $lti );
128
    }
129
 
130
    $completiontimeexpected = !empty($lti->completionexpected) ? $lti->completionexpected : null;
131
    \core_completion\api::update_completion_date_event($lti->coursemodule, 'lti', $lti->id, $completiontimeexpected);
132
 
133
    return $lti->id;
134
}
135
 
136
/**
137
 * Given an object containing all the necessary data,
138
 * (defined by the form in mod.html) this function
139
 * will update an existing instance with new data.
140
 *
141
 * @param object $instance An object from the form in mod.html
142
 * @return boolean Success/Fail
143
 **/
144
function lti_update_instance($lti, $mform) {
145
    global $DB, $CFG;
146
    require_once($CFG->dirroot.'/mod/lti/locallib.php');
147
 
148
    lti_load_tool_if_cartridge($lti);
149
 
150
    $lti->timemodified = time();
151
    $lti->id = $lti->instance;
152
 
153
    if (!isset($lti->showtitlelaunch)) {
154
        $lti->showtitlelaunch = 0;
155
    }
156
 
157
    if (!isset($lti->showdescriptionlaunch)) {
158
        $lti->showdescriptionlaunch = 0;
159
    }
160
 
161
    lti_force_type_config_settings($lti, lti_get_type_config_by_instance($lti));
162
 
163
    if (isset($lti->instructorchoiceacceptgrades) && $lti->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
164
        lti_grade_item_update($lti);
165
    } else {
166
        // Instance is no longer accepting grades from Provider, set grade to "No grade" value 0.
167
        $lti->grade = 0;
168
        $lti->instructorchoiceacceptgrades = 0;
169
 
170
        lti_grade_item_delete($lti);
171
    }
172
 
173
    if ($lti->typeid == 0 && isset($lti->urlmatchedtypeid)) {
174
        $lti->typeid = $lti->urlmatchedtypeid;
175
    }
176
 
177
    $services = lti_get_services();
178
    foreach ($services as $service) {
179
        $service->instance_updated( $lti );
180
    }
181
 
182
    $completiontimeexpected = !empty($lti->completionexpected) ? $lti->completionexpected : null;
183
    \core_completion\api::update_completion_date_event($lti->coursemodule, 'lti', $lti->id, $completiontimeexpected);
184
 
185
    return $DB->update_record('lti', $lti);
186
}
187
 
188
/**
189
 * Given an ID of an instance of this module,
190
 * this function will permanently delete the instance
191
 * and any data that depends on it.
192
 *
193
 * @param int $id Id of the module instance
194
 * @return boolean Success/Failure
195
 **/
196
function lti_delete_instance($id) {
197
    global $DB, $CFG;
198
    require_once($CFG->dirroot.'/mod/lti/locallib.php');
199
 
200
    if (! $basiclti = $DB->get_record("lti", array("id" => $id))) {
201
        return false;
202
    }
203
 
204
    $result = true;
205
 
206
    // Delete any dependent records here.
207
    lti_grade_item_delete($basiclti);
208
 
209
    $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
210
    if ($ltitype) {
211
        $DB->delete_records('lti_tool_settings',
212
            array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
213
    }
214
 
215
    $cm = get_coursemodule_from_instance('lti', $id);
216
    \core_completion\api::update_completion_date_event($cm->id, 'lti', $id, null);
217
 
218
    // We must delete the module record after we delete the grade item.
219
    if ($DB->delete_records("lti", array("id" => $basiclti->id)) ) {
220
        $services = lti_get_services();
221
        foreach ($services as $service) {
222
            $service->instance_deleted( $id );
223
        }
224
        return true;
225
    }
226
    return false;
227
 
228
}
229
 
230
/**
231
 * Return the preconfigured tools which are configured for inclusion in the activity picker.
232
 *
233
 * @param \core_course\local\entity\content_item $defaultmodulecontentitem reference to the content item for the LTI module.
234
 * @param \stdClass $user the user object, to use for cap checks if desired.
235
 * @param stdClass $course the course to scope items to.
236
 * @return array the array of content items.
237
 */
238
function lti_get_course_content_items(\core_course\local\entity\content_item $defaultmodulecontentitem, \stdClass $user,
239
        \stdClass $course) {
240
    global $CFG, $OUTPUT;
241
    require_once($CFG->dirroot.'/mod/lti/locallib.php');
242
 
243
    $types = [];
244
 
245
    // Use of a tool type, whether site or course level, is controlled by the following cap.
246
    if (!has_capability('mod/lti:addpreconfiguredinstance', \core\context\course::instance($course->id), $user)) {
247
        return $types;
248
    }
249
    $preconfiguredtools = lti_get_configured_types($course->id, $defaultmodulecontentitem->get_link()->param('sr'));
250
 
251
    foreach ($preconfiguredtools as $preconfiguredtool) {
252
 
253
        // Append the help link to the help text.
254
        if (isset($preconfiguredtool->help)) {
255
            if (isset($preconfiguredtool->helplink)) {
256
                $linktext = get_string('morehelp');
257
                $preconfiguredtool->help .= html_writer::tag('div',
258
                    $OUTPUT->doc_link($preconfiguredtool->helplink, $linktext, true), ['class' => 'helpdoclink']);
259
            }
260
        } else {
261
            $preconfiguredtool->help = '';
262
        }
263
 
264
        // Preconfigured tools take their own id + 1. This logic exists because, previously, the entry permitting manual instance
265
        // creation (the $defaultmodulecontentitem, or 'External tool' item) was included and had the id 1. This logic prevented id
266
        // collisions.
267
        $types[] = new \core_course\local\entity\content_item(
268
            $preconfiguredtool->id + 1,
269
            $preconfiguredtool->name,
270
            new \core_course\local\entity\string_title($preconfiguredtool->title),
271
            $preconfiguredtool->link,
272
            $preconfiguredtool->icon,
273
            $preconfiguredtool->help,
274
            $defaultmodulecontentitem->get_archetype(),
275
            $defaultmodulecontentitem->get_component_name(),
276
            $defaultmodulecontentitem->get_purpose()
277
        );
278
    }
279
    return $types;
280
}
281
 
282
/**
283
 * Return all content items which can be added to any course.
284
 *
285
 * @param \core_course\local\entity\content_item $defaultmodulecontentitem
286
 * @return array the array of content items.
287
 */
288
function mod_lti_get_all_content_items(\core_course\local\entity\content_item $defaultmodulecontentitem): array {
289
    global $OUTPUT, $CFG;
290
    require_once($CFG->dirroot . '/mod/lti/locallib.php'); // For access to constants.
291
 
292
    $types = [];
293
 
294
    foreach (lti_get_lti_types() as $ltitype) {
295
        if ($ltitype->coursevisible != LTI_COURSEVISIBLE_ACTIVITYCHOOSER) {
296
            continue;
297
        }
298
        $type           = new stdClass();
299
        $type->id       = $ltitype->id;
300
        $type->modclass = MOD_CLASS_ACTIVITY;
301
        $type->name     = 'lti_type_' . $ltitype->id;
302
        // Clean the name. We don't want tags here.
303
        $type->title    = clean_param($ltitype->name, PARAM_NOTAGS);
304
        $trimmeddescription = trim($ltitype->description ?? '');
305
        $type->help = '';
306
        if ($trimmeddescription != '') {
307
            // Clean the description. We don't want tags here.
308
            $type->help     = clean_param($trimmeddescription, PARAM_NOTAGS);
309
            $type->helplink = get_string('modulename_shortcut_link', 'lti');
310
        }
311
        if (empty($ltitype->icon)) {
312
            $type->icon = $OUTPUT->pix_icon('monologo', '', 'lti', array('class' => 'icon'));
313
        } else {
314
            $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => $ltitype->name, 'class' => 'icon'));
315
        }
316
        $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'typeid' => $ltitype->id));
317
 
318
        // Preconfigured tools take their own id + 1. This logic exists because, previously, the entry permitting manual instance
319
        // creation (the $defaultmodulecontentitem, or 'External tool' item) was included and had the id 1. This logic prevented id
320
        // collisions.
321
        $types[] = new \core_course\local\entity\content_item(
322
            $type->id + 1,
323
            $type->name,
324
            new \core_course\local\entity\string_title($type->title),
325
            $type->link,
326
            $type->icon,
327
            $type->help,
328
            $defaultmodulecontentitem->get_archetype(),
329
            $defaultmodulecontentitem->get_component_name(),
330
            $defaultmodulecontentitem->get_purpose()
331
        );
332
    }
333
 
334
    return $types;
335
}
336
 
337
/**
338
 * Given a coursemodule object, this function returns the extra
339
 * information needed to print this activity in various places.
340
 * For this module we just need to support external urls as
341
 * activity icons
342
 *
343
 * @param stdClass $coursemodule
344
 * @return cached_cm_info info
345
 */
346
function lti_get_coursemodule_info($coursemodule) {
347
    global $DB, $CFG;
348
    require_once($CFG->dirroot.'/mod/lti/locallib.php');
349
 
350
    if (!$lti = $DB->get_record('lti', array('id' => $coursemodule->instance),
351
            'icon, secureicon, intro, introformat, name, typeid, toolurl, launchcontainer')) {
352
        return null;
353
    }
354
 
355
    $info = new cached_cm_info();
356
 
357
    if ($coursemodule->showdescription) {
358
        // Convert intro to html. Do not filter cached version, filters run at display time.
359
        $info->content = format_module_intro('lti', $lti, $coursemodule->id, false);
360
    }
361
 
362
    if (!empty($lti->typeid)) {
363
        $toolconfig = lti_get_type_config($lti->typeid);
364
    } else if ($tool = lti_get_tool_by_url_match($lti->toolurl)) {
365
        $toolconfig = lti_get_type_config($tool->id);
366
    } else {
367
        $toolconfig = array();
368
    }
369
 
370
    // We want to use the right icon based on whether the
371
    // current page is being requested over http or https.
372
    if (lti_request_is_using_ssl() &&
373
        (!empty($lti->secureicon) || (isset($toolconfig['secureicon']) && !empty($toolconfig['secureicon'])))) {
374
        if (!empty($lti->secureicon)) {
375
            $info->iconurl = new moodle_url($lti->secureicon);
376
        } else {
377
            $info->iconurl = new moodle_url($toolconfig['secureicon']);
378
        }
379
    } else if (!empty($lti->icon)) {
380
        $info->iconurl = new moodle_url($lti->icon);
381
    } else if (isset($toolconfig['icon']) && !empty($toolconfig['icon'])) {
382
        $info->iconurl = new moodle_url($toolconfig['icon']);
383
    }
384
 
385
    // Does the link open in a new window?
386
    $launchcontainer = lti_get_launch_container($lti, $toolconfig);
387
    if ($launchcontainer == LTI_LAUNCH_CONTAINER_WINDOW) {
388
        $launchurl = new moodle_url('/mod/lti/launch.php', array('id' => $coursemodule->id));
389
        $info->onclick = "window.open('" . $launchurl->out(false) . "', 'lti-".$coursemodule->id."'); return false;";
390
    }
391
 
392
    $info->name = $lti->name;
393
 
394
    return $info;
395
}
396
 
397
/**
398
 * Return a small object with summary information about what a
399
 * user has done with a given particular instance of this module
400
 * Used for user activity reports.
401
 * $return->time = the time they did it
402
 * $return->info = a short text description
403
 *
404
 * @return null
405
 * @TODO: implement this moodle function (if needed)
406
 **/
407
function lti_user_outline($course, $user, $mod, $basiclti) {
408
    return null;
409
}
410
 
411
/**
412
 * Print a detailed representation of what a user has done with
413
 * a given particular instance of this module, for user activity reports.
414
 *
415
 * @return boolean
416
 * @TODO: implement this moodle function (if needed)
417
 **/
418
function lti_user_complete($course, $user, $mod, $basiclti) {
419
    return true;
420
}
421
 
422
/**
423
 * Given a course and a time, this module should find recent activity
424
 * that has occurred in basiclti activities and print it out.
425
 * Return true if there was output, or false is there was none.
426
 *
427
 * @uses $CFG
428
 * @return boolean
429
 * @TODO: implement this moodle function
430
 **/
431
function lti_print_recent_activity($course, $isteacher, $timestart) {
432
    return false;  //  True if anything was printed, otherwise false.
433
}
434
 
435
/**
436
 * Function to be run periodically according to the moodle cron
437
 * This function searches for things that need to be done, such
438
 * as sending out mail, toggling flags etc ...
439
 *
440
 * @uses $CFG
441
 * @return boolean
442
 **/
443
function lti_cron () {
444
    return true;
445
}
446
 
447
/**
448
 * Must return an array of grades for a given instance of this module,
449
 * indexed by user.  It also returns a maximum allowed grade.
450
 *
451
 * Example:
452
 *    $return->grades = array of grades;
453
 *    $return->maxgrade = maximum allowed grade;
454
 *
455
 *    return $return;
456
 *
457
 * @param int $basicltiid ID of an instance of this module
458
 * @return mixed Null or object with an array of grades and with the maximum grade
459
 *
460
 * @TODO: implement this moodle function (if needed)
461
 **/
462
function lti_grades($basicltiid) {
463
    return null;
464
}
465
 
466
/**
467
 * Checks if scale is being used by any instance of basiclti.
468
 * This function was added in 1.9
469
 *
470
 * This is used to find out if scale used anywhere
471
 * @param $scaleid int
472
 * @return boolean True if the scale is used by any basiclti
473
 *
474
 */
475
function lti_scale_used_anywhere($scaleid) {
476
    global $DB;
477
 
478
    if ($scaleid and $DB->record_exists('lti', array('grade' => -$scaleid))) {
479
        return true;
480
    } else {
481
        return false;
482
    }
483
}
484
 
485
/**
486
 * Execute post-install custom actions for the module
487
 * This function was added in 1.9
488
 *
489
 * @return boolean true if success, false on error
490
 */
491
function lti_install() {
492
     return true;
493
}
494
 
495
/**
496
 * Execute post-uninstall custom actions for the module
497
 * This function was added in 1.9
498
 *
499
 * @return boolean true if success, false on error
500
 */
501
function lti_uninstall() {
502
    return true;
503
}
504
 
505
/**
506
 * Returns available Basic LTI types
507
 *
508
 * @return array of basicLTI types
509
 */
510
function lti_get_lti_types() {
511
    global $DB;
512
 
513
    return $DB->get_records('lti_types', null, 'state DESC, timemodified DESC');
514
}
515
 
516
/**
517
 * Returns available Basic LTI types that match the given
518
 * tool proxy id
519
 *
520
 * @param int $toolproxyid Tool proxy id
521
 * @return array of basicLTI types
522
 */
523
function lti_get_lti_types_from_proxy_id($toolproxyid) {
524
    global $DB;
525
 
526
    return $DB->get_records('lti_types', array('toolproxyid' => $toolproxyid), 'state DESC, timemodified DESC');
527
}
528
 
529
/**
530
 * Create grade item for given basiclti
531
 *
532
 * @category grade
533
 * @param object $basiclti object with extra cmidnumber
534
 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
535
 * @return int 0 if ok, error code otherwise
536
 */
537
function lti_grade_item_update($basiclti, $grades = null) {
538
    global $CFG;
539
    require_once($CFG->libdir.'/gradelib.php');
540
    require_once($CFG->dirroot.'/mod/lti/servicelib.php');
541
 
542
    if (!lti_accepts_grades($basiclti)) {
543
        return 0;
544
    }
545
 
546
    $params = array('itemname' => $basiclti->name, 'idnumber' => $basiclti->cmidnumber);
547
 
548
    if ($basiclti->grade > 0) {
549
        $params['gradetype'] = GRADE_TYPE_VALUE;
550
        $params['grademax']  = $basiclti->grade;
551
        $params['grademin']  = 0;
552
 
553
    } else if ($basiclti->grade < 0) {
554
        $params['gradetype'] = GRADE_TYPE_SCALE;
555
        $params['scaleid']   = -$basiclti->grade;
556
 
557
    } else {
558
        $params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only.
559
    }
560
 
561
    if ($grades === 'reset') {
562
        $params['reset'] = true;
563
        $grades = null;
564
    }
565
 
566
    return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, $grades, $params);
567
}
568
 
569
/**
570
 * Update activity grades
571
 *
572
 * @param stdClass $basiclti The LTI instance
573
 * @param int      $userid Specific user only, 0 means all.
574
 * @param bool     $nullifnone Not used
575
 */
576
function lti_update_grades($basiclti, $userid=0, $nullifnone=true) {
577
    global $CFG;
578
    require_once($CFG->dirroot.'/mod/lti/servicelib.php');
579
    // LTI doesn't have its own grade table so the only thing to do is update the grade item.
580
    if (lti_accepts_grades($basiclti)) {
581
        lti_grade_item_update($basiclti);
582
    }
583
}
584
 
585
/**
586
 * Delete grade item for given basiclti
587
 *
588
 * @category grade
589
 * @param object $basiclti object
590
 * @return object basiclti
591
 */
592
function lti_grade_item_delete($basiclti) {
593
    global $CFG;
594
    require_once($CFG->libdir.'/gradelib.php');
595
 
596
    return grade_update('mod/lti', $basiclti->course, 'mod', 'lti', $basiclti->id, 0, null, array('deleted' => 1));
597
}
598
 
599
/**
600
 * Log post actions
601
 *
602
 * @return array
603
 */
604
function lti_get_post_actions() {
605
    return array();
606
}
607
 
608
/**
609
 * Log view actions
610
 *
611
 * @return array
612
 */
613
function lti_get_view_actions() {
614
    return array('view all', 'view');
615
}
616
 
617
/**
618
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
619
 *
620
 * @param  stdClass $lti        lti object
621
 * @param  stdClass $course     course object
622
 * @param  stdClass $cm         course module object
623
 * @param  stdClass $context    context object
624
 * @since Moodle 3.0
625
 */
626
function lti_view($lti, $course, $cm, $context) {
627
 
628
    // Trigger course_module_viewed event.
629
    $params = array(
630
        'context' => $context,
631
        'objectid' => $lti->id
632
    );
633
 
634
    $event = \mod_lti\event\course_module_viewed::create($params);
635
    $event->add_record_snapshot('course_modules', $cm);
636
    $event->add_record_snapshot('course', $course);
637
    $event->add_record_snapshot('lti', $lti);
638
    $event->trigger();
639
 
640
    // Completion.
641
    $completion = new completion_info($course);
642
    $completion->set_module_viewed($cm);
643
}
644
 
645
/**
646
 * Check if the module has any update that affects the current user since a given time.
647
 *
648
 * @param  cm_info $cm course module data
649
 * @param  int $from the time to check updates from
650
 * @param  array $filter  if we need to check only specific updates
651
 * @return stdClass an object with the different type of areas indicating if they were updated or not
652
 * @since Moodle 3.2
653
 */
654
function lti_check_updates_since(cm_info $cm, $from, $filter = array()) {
655
    global $DB, $USER;
656
 
657
    $updates = course_check_module_updates_since($cm, $from, array(), $filter);
658
 
659
    // Check if there is a new submission.
660
    $updates->submissions = (object) array('updated' => false);
661
    $select = 'ltiid = :id AND userid = :userid AND (datesubmitted > :since1 OR dateupdated > :since2)';
662
    $params = array('id' => $cm->instance, 'userid' => $USER->id, 'since1' => $from, 'since2' => $from);
663
    $submissions = $DB->get_records_select('lti_submission', $select, $params, '', 'id');
664
    if (!empty($submissions)) {
665
        $updates->submissions->updated = true;
666
        $updates->submissions->itemids = array_keys($submissions);
667
    }
668
 
669
    // Now, teachers should see other students updates.
670
    if (has_capability('mod/lti:manage', $cm->context)) {
671
        $select = 'ltiid = :id AND (datesubmitted > :since1 OR dateupdated > :since2)';
672
        $params = array('id' => $cm->instance, 'since1' => $from, 'since2' => $from);
673
 
674
        if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
675
            $groupusers = array_keys(groups_get_activity_shared_group_members($cm));
676
            if (empty($groupusers)) {
677
                return $updates;
678
            }
679
            list($insql, $inparams) = $DB->get_in_or_equal($groupusers, SQL_PARAMS_NAMED);
680
            $select .= ' AND userid ' . $insql;
681
            $params = array_merge($params, $inparams);
682
        }
683
 
684
        $updates->usersubmissions = (object) array('updated' => false);
685
        $submissions = $DB->get_records_select('lti_submission', $select, $params, '', 'id');
686
        if (!empty($submissions)) {
687
            $updates->usersubmissions->updated = true;
688
            $updates->usersubmissions->itemids = array_keys($submissions);
689
        }
690
    }
691
 
692
    return $updates;
693
}
694
 
695
/**
696
 * Get icon mapping for font-awesome.
697
 */
698
function mod_lti_get_fontawesome_icon_map() {
699
    return [
700
        'mod_lti:warning' => 'fa-exclamation text-warning',
701
    ];
702
}
703
 
704
/**
705
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
706
 *
707
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
708
 * is not displayed on the block.
709
 *
710
 * @param calendar_event $event
711
 * @param \core_calendar\action_factory $factory
712
 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
713
 * @return \core_calendar\local\event\entities\action_interface|null
714
 */
715
function mod_lti_core_calendar_provide_event_action(calendar_event $event,
716
                                                      \core_calendar\action_factory $factory,
717
                                                      int $userid = 0) {
718
    global $USER;
719
 
720
    if (empty($userid)) {
721
        $userid = $USER->id;
722
    }
723
 
724
    $cm = get_fast_modinfo($event->courseid, $userid)->instances['lti'][$event->instance];
725
 
726
    if (!$cm->uservisible) {
727
        // The module is not visible to the user for any reason.
728
        return null;
729
    }
730
 
731
    $completion = new \completion_info($cm->get_course());
732
 
733
    $completiondata = $completion->get_data($cm, false, $userid);
734
 
735
    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
736
        return null;
737
    }
738
 
739
    return $factory->create_instance(
740
        get_string('view'),
741
        new \moodle_url('/mod/lti/view.php', ['id' => $cm->id]),
742
        1,
743
        true
744
    );
745
}
746
 
747
/**
748
 * Extend the course navigation with an "LTI External tools" link which redirects to a list of all tools available for course use.
749
 *
750
 * @param settings_navigation $navigation The settings navigation object
751
 * @param stdClass $course The course
752
 * @param stdclass $context Course context
753
 * @return void
754
 */
755
function mod_lti_extend_navigation_course($navigation, $course, $context): void {
756
    if (has_capability('mod/lti:addpreconfiguredinstance', $context)) {
757
        $url = new moodle_url('/mod/lti/coursetools.php', ['id' => $course->id]);
758
        $settingsnode = navigation_node::create(get_string('courseexternaltools', 'mod_lti'), $url, navigation_node::TYPE_SETTING,
759
            null, 'coursetools', new pix_icon('i/settings', ''));
760
        $navigation->add_node($settingsnode);
761
    }
762
}