-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternallib.php
More file actions
863 lines (783 loc) · 35.1 KB
/
externallib.php
File metadata and controls
863 lines (783 loc) · 35.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* External API for enumerating LTI tools.
*
* @package local_lti_tools_api
* @copyright 2025 Daniel Landau
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* External API class for LTI tools enumeration
*/
class local_lti_tools_api_external extends external_api {
/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function get_lti_tools_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'Course ID (optional, 0 for site-wide tools)', VALUE_DEFAULT, 0),
'includesite' => new external_value(PARAM_BOOL, 'Include site-wide tools', VALUE_DEFAULT, true),
'includecourse' => new external_value(PARAM_BOOL, 'Include course-specific tools', VALUE_DEFAULT, true),
]);
}
/**
* Get available LTI tools
* @param int $courseid Course ID
* @param bool $includesite Include site-wide tools
* @param bool $includecourse Include course-specific tools
* @return array List of LTI tools
*/
public static function get_lti_tools(
$courseid = 0,
$includesite = true,
$includecourse = true
) {
global $DB, $USER;
// Validate parameters.
$params = self::validate_parameters(self::get_lti_tools_parameters(), [
'courseid' => $courseid,
'includesite' => $includesite,
'includecourse' => $includecourse,
]);
$courseid = $params['courseid'];
$includesite = $params['includesite'];
$includecourse = $params['includecourse'];
// Check capabilities.
if ($courseid > 0) {
$context = context_course::instance($courseid);
// Check if user has the capability to add LTI instances.
if (!has_capability('mod/lti:addinstance', $context)) {
throw new required_capability_exception($context, 'mod/lti:addinstance', 'nopermissions', '');
}
} else {
// For site-wide tools, we still need course context for permission check
// since we're likely being called from a course context.
$context = context_system::instance();
// Check if user has capability to add LTI instances anywhere.
$hascapability = false;
// If user has system-level capability, allow access.
if (has_capability('moodle/site:config', $context)) {
$hascapability = true;
} else {
// Otherwise, check if they have mod/lti:addinstance in any course they're enrolled in.
global $USER;
$courses = enrol_get_my_courses();
foreach ($courses as $course) {
$coursecontext = context_course::instance($course->id);
if (has_capability('mod/lti:addinstance', $coursecontext)) {
$hascapability = true;
break;
}
}
}
if (!$hascapability) {
throw new required_capability_exception($context, 'mod/lti:addinstance', 'nopermissions', '');
}
}
$tools = [];
// Get site-wide LTI tools.
if ($includesite) {
$sitetools = $DB->get_records('lti_types', ['course' => SITEID], 'name ASC');
foreach ($sitetools as $tool) {
$tools[] = [
'id' => $tool->id,
'name' => $tool->name,
'description' => $tool->description ?? '',
'baseurl' => $tool->baseurl,
'toolurl' => $tool->toolurl ?? '',
'state' => self::get_tool_state($tool->state),
'course' => $tool->course,
'scope' => 'site',
'icon' => $tool->icon ?? '',
'secureicon' => $tool->secureicon ?? '',
'createdby' => $tool->createdby,
'timecreated' => $tool->timecreated,
'timemodified' => $tool->timemodified,
];
}
}
// Get course-specific LTI tools.
if ($includecourse && $courseid > 0) {
$coursetools = $DB->get_records('lti_types', ['course' => $courseid], 'name ASC');
foreach ($coursetools as $tool) {
$tools[] = [
'id' => $tool->id,
'name' => $tool->name,
'description' => $tool->description ?? '',
'baseurl' => $tool->baseurl,
'toolurl' => $tool->toolurl ?? '',
'state' => self::get_tool_state($tool->state),
'course' => $tool->course,
'scope' => 'course',
'icon' => $tool->icon ?? '',
'secureicon' => $tool->secureicon ?? '',
'createdby' => $tool->createdby,
'timecreated' => $tool->timecreated,
'timemodified' => $tool->timemodified,
];
}
}
return $tools;
}
/**
* Convert numeric state to readable string
* @param int $state Tool state
* @return string Human readable state
*/
private static function get_tool_state($state) {
switch ($state) {
case LTI_TOOL_STATE_CONFIGURED:
return 'configured';
case LTI_TOOL_STATE_PENDING:
return 'pending';
case LTI_TOOL_STATE_REJECTED:
return 'rejected';
default:
return 'unknown';
}
}
/**
* Returns description of method result value
* @return external_description
*/
public static function get_lti_tools_returns() {
return new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'Tool ID'),
'name' => new external_value(PARAM_TEXT, 'Tool name'),
'description' => new external_value(PARAM_RAW, 'Tool description'),
'baseurl' => new external_value(PARAM_URL, 'Base URL'),
'toolurl' => new external_value(PARAM_URL, 'Tool URL'),
'state' => new external_value(PARAM_TEXT, 'Tool state (configured, pending, rejected)'),
'course' => new external_value(PARAM_INT, 'Course ID (SITEID for site-wide tools)'),
'scope' => new external_value(PARAM_TEXT, 'Tool scope (site or course)'),
'icon' => new external_value(PARAM_URL, 'Tool icon URL'),
'secureicon' => new external_value(PARAM_URL, 'Secure tool icon URL'),
'createdby' => new external_value(PARAM_INT, 'User ID who created the tool'),
'timecreated' => new external_value(PARAM_INT, 'Time created timestamp'),
'timemodified' => new external_value(PARAM_INT, 'Time modified timestamp'),
])
);
}
/**
* Returns description of method parameters for adding LTI instance to course
* @return external_function_parameters
*/
public static function add_lti_instance_parameters() {
return new external_function_parameters([
'courseid' => new external_value(PARAM_INT, 'Course ID where to add the LTI instance'),
'toolid' => new external_value(PARAM_INT, 'LTI Tool type ID'),
'name' => new external_value(PARAM_TEXT, 'Activity name'),
'intro' => new external_value(PARAM_RAW, 'Activity description/intro', VALUE_DEFAULT, ''),
'sectionnum' => new external_value(PARAM_INT, 'Section number (0-based)', VALUE_DEFAULT, 0),
'visible' => new external_value(PARAM_INT, 'Visibility of the activity (0 or 1)', VALUE_DEFAULT, 1),
'customparameters' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_TEXT, 'Parameter name'),
'value' => new external_value(PARAM_RAW, 'Parameter value'),
]),
'Custom parameters for this instance',
VALUE_DEFAULT,
[]
),
'resourcekey' => new external_value(PARAM_TEXT, 'Resource key (if different from tool default)', VALUE_DEFAULT, ''),
'password' => new external_value(
PARAM_TEXT,
'Shared secret (if different from tool default)',
VALUE_DEFAULT,
''
),
'instructorchoicesendname' => new external_value(
PARAM_INT,
'Send student names (0 or 1)',
VALUE_DEFAULT,
0
),
'instructorchoicesendemailaddr' => new external_value(
PARAM_INT,
'Send student emails (0 or 1)',
VALUE_DEFAULT,
0
),
'instructorchoiceacceptgrades' => new external_value(
PARAM_INT,
'Accept grades from tool (0 or 1)',
VALUE_DEFAULT,
0
),
'grade' => new external_value(PARAM_INT, 'Maximum grade (0 for no grading)', VALUE_DEFAULT, 100),
'launchcontainer' => new external_value(
PARAM_INT,
'Launch container (1=default, 2=embed, 3=embed no blocks, 4=new window, 5=existing)',
VALUE_DEFAULT,
1
),
'availablefrom' => new external_value(
PARAM_INT,
'Unix timestamp for availability start (0 for no restriction)',
VALUE_DEFAULT,
0
),
'availableuntil' => new external_value(
PARAM_INT,
'Unix timestamp for availability end (0 for no restriction)',
VALUE_DEFAULT,
0
),
'showavailability' => new external_value(
PARAM_INT,
'Show availability info to students (0 or 1)',
VALUE_DEFAULT,
1
),
'hidewhenrestricted' => new external_value(
PARAM_INT,
'Hide activity when restricted (1) or show greyed-out (0)',
VALUE_DEFAULT,
0
),
]);
}
/**
* Add an LTI tool instance to a course
* @param int $courseid Course ID
* @param int $toolid LTI Tool type ID
* @param string $name Activity name
* @param string $intro Activity description
* @param int $sectionnum Section number
* @param int $visible Visibility (0 or 1)
* @param array $customparameters Custom parameters
* @param string $resourcekey Resource key
* @param string $password Shared secret
* @param int $instructorchoicesendname Send names (0 or 1)
* @param int $instructorchoicesendemailaddr Send emails (0 or 1)
* @param int $instructorchoiceacceptgrades Accept grades (0 or 1)
* @param int $grade Maximum grade
* @param int $launchcontainer Launch container
* @param int $availablefrom Unix timestamp for availability start
* @param int $availableuntil Unix timestamp for availability end
* @param int $showavailability Show availability info to students (0 or 1)
* @param int $hidewhenrestricted Completely hide activity when date restrictions apply (1) or show greyed-out (0)
* @return array LTI instance details
*/
public static function add_lti_instance(
$courseid,
$toolid,
$name,
$intro = '',
$sectionnum = 0,
$visible = 1,
$customparameters = [],
$resourcekey = '',
$password = '',
$instructorchoicesendname = 0,
$instructorchoicesendemailaddr = 0,
$instructorchoiceacceptgrades = 0,
$grade = 100,
$launchcontainer = 1,
$availablefrom = 0,
$availableuntil = 0,
$showavailability = 1,
$hidewhenrestricted = 0
) {
global $DB, $CFG;
require_once($CFG->dirroot . '/course/modlib.php');
require_once($CFG->dirroot . '/mod/lti/lib.php');
// Validate parameters.
$params = self::validate_parameters(self::add_lti_instance_parameters(), [
'courseid' => $courseid,
'toolid' => $toolid,
'name' => $name,
'intro' => $intro,
'sectionnum' => $sectionnum,
'visible' => $visible,
'customparameters' => $customparameters,
'resourcekey' => $resourcekey,
'password' => $password,
'instructorchoicesendname' => $instructorchoicesendname,
'instructorchoicesendemailaddr' => $instructorchoicesendemailaddr,
'instructorchoiceacceptgrades' => $instructorchoiceacceptgrades,
'grade' => $grade,
'launchcontainer' => $launchcontainer,
'availablefrom' => $availablefrom,
'availableuntil' => $availableuntil,
'showavailability' => $showavailability,
'hidewhenrestricted' => $hidewhenrestricted,
]);
// Check capabilities.
$context = context_course::instance($params['courseid']);
require_capability('mod/lti:addinstance', $context);
// Verify course exists.
$course = $DB->get_record('course', ['id' => $params['courseid']], '*', MUST_EXIST);
// Verify tool exists and is accessible.
$tool = $DB->get_record('lti_types', ['id' => $params['toolid']], '*', MUST_EXIST);
if ($tool->course != SITEID && $tool->course != $params['courseid']) {
throw new moodle_exception('invalidtoolid', 'mod_lti');
}
// Prepare module data.
$moduleinfo = new stdClass();
$moduleinfo->course = $params['courseid'];
$moduleinfo->name = $params['name'];
$moduleinfo->intro = $params['intro'];
$moduleinfo->introformat = FORMAT_HTML;
$moduleinfo->module = $DB->get_field('modules', 'id', ['name' => 'lti']);
$moduleinfo->modulename = 'lti';
$moduleinfo->section = $params['sectionnum'];
$moduleinfo->visible = $params['visible'];
$moduleinfo->visibleoncoursepage = $params['visible'];
$moduleinfo->groupmode = 0;
$moduleinfo->groupingid = 0;
// LTI specific settings.
$moduleinfo->typeid = $params['toolid'];
$moduleinfo->toolurl = $tool->baseurl;
// Get the tool configuration for resourcekey and password.
$toolconfig = lti_get_type_config($tool->id);
$moduleinfo->resourcekey = $params['resourcekey'] ?: ($toolconfig['resourcekey'] ?? '');
$moduleinfo->password = $params['password'] ?: ($toolconfig['password'] ?? '');
$moduleinfo->instructorchoicesendname = $params['instructorchoicesendname'];
$moduleinfo->instructorchoicesendemailaddr = $params['instructorchoicesendemailaddr'];
$moduleinfo->instructorchoiceacceptgrades = $params['instructorchoiceacceptgrades'];
$moduleinfo->grade = $params['grade'];
$moduleinfo->launchcontainer = $params['launchcontainer'];
// Handle custom parameters.
$customparams = '';
if (!empty($params['customparameters'])) {
$customparamstrings = [];
foreach ($params['customparameters'] as $param) {
$customparamstrings[] = $param['name'] . '=' . $param['value'];
}
$customparams = implode("\n", $customparamstrings);
}
$moduleinfo->instructorcustomparameters = $customparams;
// Handle availability restrictions.
if (!empty($params['availablefrom']) || !empty($params['availableuntil'])) {
$conditions = [];
// Add "available from" condition.
if (!empty($params['availablefrom'])) {
$conditions[] = (object)[
'type' => 'date',
'd' => '>=',
't' => $params['availablefrom'],
];
}
// Add "available until" condition.
if (!empty($params['availableuntil'])) {
$conditions[] = (object)[
'type' => 'date',
'd' => '<',
't' => $params['availableuntil'],
];
}
if (!empty($conditions)) {
// For AND operations, showc array controls visibility of each condition.
// Showc[i] = true: show activity greyed out with restriction info.
// Showc[i] = false: completely hide the activity.
//
// Hidewhenrestricted: 1 = hide completely, 0 = show greyed out.
// Therefore: showc = !hidewhenrestricted (invert the logic).
$showwhenrestricted = !((bool)$params['hidewhenrestricted']);
$showc = array_fill(0, count($conditions), $showwhenrestricted);
$availability = (object)[
'op' => '&',
'c' => $conditions,
'showc' => $showc,
];
$moduleinfo->availability = json_encode($availability);
}
}
// Add the module.
$moduleinfo = add_moduleinfo($moduleinfo, $course);
$url = new moodle_url('/mod/lti/view.php', ['id' => $moduleinfo->coursemodule]);
return [
'instanceid' => $moduleinfo->instance,
'coursemoduleid' => $moduleinfo->coursemodule,
'name' => $moduleinfo->name,
'url' => $url->out(false),
'success' => true,
'message' => 'LTI instance added successfully',
];
}
/**
* Returns description of method result value for add_lti_instance
* @return external_description
*/
public static function add_lti_instance_returns() {
return new external_single_structure([
'instanceid' => new external_value(PARAM_INT, 'LTI instance ID'),
'coursemoduleid' => new external_value(PARAM_INT, 'Course module ID'),
'name' => new external_value(PARAM_TEXT, 'Activity name'),
'url' => new external_value(PARAM_URL, 'Activity URL'),
'success' => new external_value(PARAM_BOOL, 'Success status'),
'message' => new external_value(PARAM_TEXT, 'Status message'),
]);
}
/**
* Returns description of method parameters for getting tool by name
* @return external_function_parameters
*/
public static function get_lti_tool_by_name_parameters() {
return new external_function_parameters([
'name' => new external_value(PARAM_TEXT, 'Tool name to search for'),
'courseid' => new external_value(PARAM_INT, 'Course ID (optional, 0 for site-wide tools)', VALUE_DEFAULT, 0),
'exactmatch' => new external_value(PARAM_BOOL, 'Exact name match (true) or partial match (false)', VALUE_DEFAULT, true),
]);
}
/**
* Get LTI tool type ID by name
* @param string $name Tool name to search for
* @param int $courseid Course ID (0 for site-wide)
* @param bool $exactmatch Whether to do exact or partial matching
* @return array Tool information
*/
public static function get_lti_tool_by_name($name, $courseid = 0, $exactmatch = true) {
global $DB;
// Validate parameters.
$params = self::validate_parameters(self::get_lti_tool_by_name_parameters(), [
'name' => $name,
'courseid' => $courseid,
'exactmatch' => $exactmatch,
]);
$name = $params['name'];
$courseid = $params['courseid'];
$exactmatch = $params['exactmatch'];
// Check capabilities.
if ($courseid > 0) {
$context = context_course::instance($courseid);
require_capability('mod/lti:addinstance', $context);
} else {
// For site-wide searches, check if user has LTI capability in any course.
$context = context_system::instance();
$hascapability = false;
if (has_capability('moodle/site:config', $context)) {
$hascapability = true;
} else {
$courses = enrol_get_my_courses();
foreach ($courses as $course) {
$coursecontext = context_course::instance($course->id);
if (has_capability('mod/lti:addinstance', $coursecontext)) {
$hascapability = true;
break;
}
}
}
if (!$hascapability) {
throw new required_capability_exception($context, 'mod/lti:addinstance', 'nopermissions', '');
}
}
// Build the search conditions.
$conditions = [];
$paramsdb = [];
if ($exactmatch) {
$conditions[] = 'name = :name';
$paramsdb['name'] = $name;
} else {
$conditions[] = $DB->sql_like('name', ':name', false);
$paramsdb['name'] = '%' . $DB->sql_like_escape($name) . '%';
}
// Add course condition.
if ($courseid > 0) {
// Search in both site-wide and course-specific tools.
$conditions[] = '(course = :siteid OR course = :courseid)';
$paramsdb['siteid'] = SITEID;
$paramsdb['courseid'] = $courseid;
} else {
// Only site-wide tools.
$conditions[] = 'course = :siteid';
$paramsdb['siteid'] = SITEID;
}
$sql = 'SELECT * FROM {lti_types} WHERE ' . implode(' AND ', $conditions) . ' ORDER BY name ASC';
$tools = $DB->get_records_sql($sql, $paramsdb);
$results = [];
foreach ($tools as $tool) {
$results[] = [
'id' => $tool->id,
'name' => $tool->name,
'description' => $tool->description ?? '',
'baseurl' => $tool->baseurl,
'toolurl' => $tool->toolurl ?? '',
'state' => self::get_tool_state($tool->state),
'course' => $tool->course,
'scope' => ($tool->course == SITEID) ? 'site' : 'course',
'icon' => $tool->icon ?? '',
'secureicon' => $tool->secureicon ?? '',
'createdby' => $tool->createdby,
'timecreated' => $tool->timecreated,
'timemodified' => $tool->timemodified,
];
}
return $results;
}
/**
* Returns description of method result value for get_lti_tool_by_name
* @return external_description
*/
public static function get_lti_tool_by_name_returns() {
return new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'Tool ID'),
'name' => new external_value(PARAM_TEXT, 'Tool name'),
'description' => new external_value(PARAM_RAW, 'Tool description'),
'baseurl' => new external_value(PARAM_URL, 'Base URL'),
'toolurl' => new external_value(PARAM_URL, 'Tool URL'),
'state' => new external_value(PARAM_TEXT, 'Tool state (configured, pending, rejected)'),
'course' => new external_value(PARAM_INT, 'Course ID (SITEID for site-wide tools)'),
'scope' => new external_value(PARAM_TEXT, 'Tool scope (site or course)'),
'icon' => new external_value(PARAM_URL, 'Tool icon URL'),
'secureicon' => new external_value(PARAM_URL, 'Secure tool icon URL'),
'createdby' => new external_value(PARAM_INT, 'User ID who created the tool'),
'timecreated' => new external_value(PARAM_INT, 'Time created timestamp'),
'timemodified' => new external_value(PARAM_INT, 'Time modified timestamp'),
])
);
}
/**
* Returns description of method parameters for getting tool details
* @return external_function_parameters
*/
public static function get_lti_tool_details_parameters() {
return new external_function_parameters([
'toolid' => new external_value(PARAM_INT, 'LTI Tool ID'),
]);
}
/**
* Get detailed information about a specific LTI tool
* @param int $toolid Tool ID
* @return array Tool details
*/
public static function get_lti_tool_details($toolid) {
global $DB;
// Validate parameters.
$params = self::validate_parameters(self::get_lti_tool_details_parameters(), [
'toolid' => $toolid,
]);
$toolid = $params['toolid'];
// Check if tool exists.
$tool = $DB->get_record('lti_types', ['id' => $toolid], '*', MUST_EXIST);
// Check capabilities.
if ($tool->course == SITEID) {
// For site-level tools, check if user can manage LTI tools or has admin access.
$context = context_system::instance();
$hasaccess = false;
if (has_capability('moodle/site:config', $context, null, false)) {
$hasaccess = true;
} else {
// Also allow users who have LTI addinstance capability in any course.
global $USER;
$courses = enrol_get_my_courses();
foreach ($courses as $course) {
$coursecontext = context_course::instance($course->id);
if (has_capability('mod/lti:addinstance', $coursecontext)) {
$hasaccess = true;
break;
}
}
}
if (!$hasaccess) {
throw new required_capability_exception($context, 'mod/lti:addinstance', 'nopermissions', '');
}
} else {
$context = context_course::instance($tool->course);
require_capability('mod/lti:addinstance', $context);
}
// Get tool configuration.
$config = $DB->get_records_menu('lti_types_config', ['typeid' => $toolid], '', 'name, value');
return [
'id' => $tool->id,
'name' => $tool->name,
'description' => $tool->description ?? '',
'baseurl' => $tool->baseurl,
'toolurl' => $tool->toolurl ?? '',
'state' => self::get_tool_state($tool->state),
'course' => $tool->course,
'scope' => ($tool->course == SITEID) ? 'site' : 'course',
'icon' => $tool->icon ?? '',
'secureicon' => $tool->secureicon ?? '',
'createdby' => $tool->createdby,
'timecreated' => $tool->timecreated,
'timemodified' => $tool->timemodified,
'configuration' => $config,
];
}
/**
* Returns description of method result value for tool details
* @return external_description
*/
public static function get_lti_tool_details_returns() {
return new external_single_structure([
'id' => new external_value(PARAM_INT, 'Tool ID'),
'name' => new external_value(PARAM_TEXT, 'Tool name'),
'description' => new external_value(PARAM_RAW, 'Tool description'),
'baseurl' => new external_value(PARAM_URL, 'Base URL'),
'toolurl' => new external_value(PARAM_URL, 'Tool URL'),
'state' => new external_value(PARAM_TEXT, 'Tool state'),
'course' => new external_value(PARAM_INT, 'Course ID'),
'scope' => new external_value(PARAM_TEXT, 'Tool scope'),
'icon' => new external_value(PARAM_URL, 'Tool icon URL'),
'secureicon' => new external_value(PARAM_URL, 'Secure tool icon URL'),
'createdby' => new external_value(PARAM_INT, 'Created by user ID'),
'timecreated' => new external_value(PARAM_INT, 'Time created'),
'timemodified' => new external_value(PARAM_INT, 'Time modified'),
'configuration' => new external_multiple_structure(
new external_value(PARAM_RAW, 'Configuration value'),
'Tool configuration parameters'
),
]);
}
/**
* Returns description of method parameters for setting LTI availability
* @return external_function_parameters
*/
public static function set_lti_availability_parameters() {
return new external_function_parameters([
'coursemoduleid' => new external_value(PARAM_INT, 'Course module ID of the LTI instance'),
'availablefrom' => new external_value(
PARAM_INT,
'Unix timestamp for availability start date (0 to remove)',
VALUE_DEFAULT,
0
),
'availableuntil' => new external_value(
PARAM_INT,
'Unix timestamp for availability end date (0 to remove)',
VALUE_DEFAULT,
0
),
'showavailability' => new external_value(
PARAM_INT,
'Show availability info to students (0 or 1)',
VALUE_DEFAULT,
1
),
'hidewhenrestricted' => new external_value(
PARAM_INT,
'Completely hide activity when restricted (1) or show greyed-out (0)',
VALUE_DEFAULT,
0
),
]);
}
/**
* Set or update availability restrictions for an existing LTI instance
* @param int $coursemoduleid Course module ID
* @param int $availablefrom Unix timestamp for availability start
* @param int $availableuntil Unix timestamp for availability end
* @param int $showavailability Show availability info to students (0 or 1)
* @param int $hidewhenrestricted Completely hide activity when date restrictions apply (1) or show greyed-out (0)
* @return array Update status
*/
public static function set_lti_availability(
$coursemoduleid,
$availablefrom = 0,
$availableuntil = 0,
$showavailability = 1,
$hidewhenrestricted = 0
) {
global $DB, $CFG;
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/availability/classes/tree.php');
// Validate parameters.
$params = self::validate_parameters(self::set_lti_availability_parameters(), [
'coursemoduleid' => $coursemoduleid,
'availablefrom' => $availablefrom,
'availableuntil' => $availableuntil,
'showavailability' => $showavailability,
'hidewhenrestricted' => $hidewhenrestricted,
]);
// Get the course module.
$cm = $DB->get_record('course_modules', ['id' => $params['coursemoduleid']], '*', MUST_EXIST);
// Verify it's an LTI module.
$module = $DB->get_record('modules', ['id' => $cm->module], '*', MUST_EXIST);
if ($module->name !== 'lti') {
throw new moodle_exception('invalidmodule', 'error', '', null, 'Module is not an LTI instance');
}
// Check capabilities.
$context = context_module::instance($cm->id);
require_capability('moodle/course:manageactivities', $context);
// Get the course for later.
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
// Build availability conditions.
$availability = null;
if (!empty($CFG->enableavailability) && ($params['availablefrom'] > 0 || $params['availableuntil'] > 0)) {
$conditions = [];
if ($params['availablefrom'] > 0) {
$conditions[] = (object)[
'type' => 'date',
'd' => '>=',
't' => $params['availablefrom'],
];
}
if ($params['availableuntil'] > 0) {
$conditions[] = (object)[
'type' => 'date',
'd' => '<',
't' => $params['availableuntil'],
];
}
// For AND operations, showc array controls visibility of each condition.
// Showc[i] = true: show activity greyed out with restriction info.
// Showc[i] = false: completely hide the activity.
$showwhenrestricted = !((bool)$params['hidewhenrestricted']);
$showc = array_fill(0, count($conditions), $showwhenrestricted);
$availabilitydata = (object)[
'op' => '&', // AND operator - both conditions must be met.
'c' => $conditions,
'showc' => $showc,
];
$availability = json_encode($availabilitydata);
}
// Update the course module.
$cm->availability = $availability;
$DB->update_record('course_modules', $cm);
// Rebuild course cache.
rebuild_course_cache($course->id, true);
// Prepare response.
$response = [
'success' => true,
'coursemoduleid' => $cm->id,
'message' => 'Availability updated successfully',
];
// Add human-readable availability info.
if ($params['availablefrom'] > 0) {
$response['availablefrom'] = $params['availablefrom'];
$response['availablefrom_readable'] = userdate($params['availablefrom']);
}
if ($params['availableuntil'] > 0) {
$response['availableuntil'] = $params['availableuntil'];
$response['availableuntil_readable'] = userdate($params['availableuntil']);
}
return $response;
}
/**
* Returns description of method result value for set_lti_availability
* @return external_description
*/
public static function set_lti_availability_returns() {
return new external_single_structure([
'success' => new external_value(PARAM_BOOL, 'Success status'),
'coursemoduleid' => new external_value(PARAM_INT, 'Course module ID'),
'message' => new external_value(PARAM_TEXT, 'Status message'),
'availablefrom' => new external_value(PARAM_INT, 'Unix timestamp for start date', VALUE_OPTIONAL),
'availablefrom_readable' => new external_value(PARAM_TEXT, 'Human readable start date', VALUE_OPTIONAL),
'availableuntil' => new external_value(PARAM_INT, 'Unix timestamp for end date', VALUE_OPTIONAL),
'availableuntil_readable' => new external_value(PARAM_TEXT, 'Human readable end date', VALUE_OPTIONAL),
]);
}
}