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 defines the main lti configuration form
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
 
51
require_once($CFG->dirroot.'/course/moodleform_mod.php');
52
require_once($CFG->dirroot.'/mod/lti/locallib.php');
53
 
54
class mod_lti_mod_form extends moodleform_mod {
55
 
56
    /** @var int the tool typeid, or 0 if the instance form is being created for a manually configured tool instance.*/
57
    protected int $typeid;
58
 
59
    /** @var string|null type */
60
    protected ?string $type;
61
 
62
    /**
63
     * Constructor.
64
     *
65
     * Throws an exception if trying to init the form for a new manual instance of a tool, which is not supported in 4.3 onward.
66
     *
67
     * @param \stdClass $current the current form data.
68
     * @param string $section the section number.
69
     * @param \stdClass $cm the course module object.
70
     * @param \stdClass $course the course object.
71
     * @throws moodle_exception if trying to init the form for the creation of a manual instance, which is no longer supported.
72
     */
73
    public function __construct($current, $section, $cm, $course) {
74
 
75
        // Setup some of the pieces used to control display in the form definition() method.
76
        // Type ID parameter being passed when adding an preconfigured tool from activity chooser.
77
        $this->typeid = optional_param('typeid', 0, PARAM_INT);
78
        $this->type = optional_param('type', null, PARAM_ALPHA);
79
 
80
        // Only permit construction if the form deals with editing an existing instance (current->id not empty), or creating an
81
        // instance from a preconfigured tool type ($this->typeid not empty). Make an exception for callers, such as core_completion
82
        // which aren't instantiating the form with the expected data, by checking whether the page has been set up, which is the
83
        // case for normal uses.
84
        global $PAGE;
85
        if ($PAGE->has_set_url() && str_contains($PAGE->url, '/course/modedit.php')) {
86
            if (empty($this->typeid) && empty($current->id)) {
87
                throw new moodle_exception('lti:addmanualinstanceprohibitederror', 'mod_lti');
88
            }
89
        }
90
 
91
        parent::__construct($current, $section, $cm, $course);
92
    }
93
 
94
    /**
95
     * Defines the form for legacy instances. Here tool config is frozen because the manual configuration method is deprecated.
96
     *
97
     * @param array $instancetypes the array of options for the legacy 'preconfigured tools' select.
98
     * @return void
99
     */
100
    protected function legacy_instance_form_definition(array $instancetypes): void {
101
        global $OUTPUT;
102
 
103
        // The legacy form handles instances which are either entirely manually configured (current->typeid = 0), or which are
104
        // manually configured and have been domain-matched to a preconfigured tool (current->typeid != 0).
105
        $manualinstance = empty($this->current->typeid);
106
        $matchestoolnotavailabletocourse = !$manualinstance;
107
        $typeid = $manualinstance ? '0' : $this->current->typeid;
108
 
109
        // Since 'mod/lti:addmanualinstance' capability is deprecated, determining which users may have had access to the certain
110
        // form fields (the manual config fields) isn't straightforward. Users without 'mod/lti:addmanualinstance' would have only
111
        // been permitted to edit the basic instance fields (name, etc.), so care must be taken not to display the config fields to
112
        // these users. Users who can add/edit course tools (mod/lti:addcoursetool) are able to view tool information anyway, via
113
        // the tool definitions, so this capability is used as a replacement, to control access to these tool config fields.
114
        $canviewmanualconfig = has_capability('mod/lti:addcoursetool', $this->context);
115
        $showtypes = has_capability('mod/lti:addpreconfiguredinstance', $this->context);
116
 
117
        if ($manualinstance && !$canviewmanualconfig) {
118
            // If you cannot add a manual instance and this is already a manual instance, then remove the 'types' selector.
119
            $showtypes = false;
120
        }
121
 
122
        $mform =& $this->_form;
123
 
124
        // Show the deprecation notice, regardless of whether the user can view the tool configuration details or not.
125
        // They will still see locked privacy fields and should be informed as to why that is.
126
        $mform->addElement('html', $OUTPUT->notification(
127
            get_string('editmanualinstancedeprecationwarning', 'mod_lti', get_docs_url('External_tool')),
128
            \core\output\notification::NOTIFY_WARNING, false)
129
        );
130
 
131
        // Adding the "general" fieldset, where all the common settings are shown.
132
        $mform->addElement('html', "<div data-attribute='dynamic-import' hidden aria-hidden='true' role='alert'></div>");
133
        $mform->addElement('header', 'general', get_string('general', 'form'));
134
 
135
        // Adding the standard "name" field.
136
        $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), ['size' => '64']);
137
        $mform->setType('name', PARAM_TEXT);
138
        $mform->addRule('name', null, 'required', null, 'client');
139
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
140
 
141
        // Adding the optional "intro" and "introformat" pair of fields.
142
        $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
143
        $mform->setAdvanced('introeditor');
144
 
145
        // Display the label to the right of the checkbox so it looks better & matches rest of the form.
146
        if ($mform->elementExists('showdescription')) {
147
            $coursedesc = $mform->getElement('showdescription');
148
            if (!empty($coursedesc)) {
149
                $coursedesc->setText(' ' . $coursedesc->getLabel());
150
                $coursedesc->setLabel('&nbsp');
151
            }
152
        }
153
 
154
        $mform->setAdvanced('showdescription');
155
 
156
        $mform->addElement('checkbox', 'showtitlelaunch', get_string('display_name', 'lti'));
157
        $mform->setAdvanced('showtitlelaunch');
158
        $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
159
 
160
        $mform->addElement('checkbox', 'showdescriptionlaunch', get_string('display_description', 'lti'));
161
        $mform->setAdvanced('showdescriptionlaunch');
162
        $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
163
 
164
        if ($showtypes) {
165
            if ($manualinstance) {
166
                // Legacy, manually configured instances: only freeze the element (not hardFreeze) so that disabledIf() still works.
167
                // The data in the select is restricted so that only the current value is deemed valid, preventing DOM-edit changes,
168
                // which are possible with frozen elements.
169
                $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'));
170
                $mform->addHelpButton('typeid', 'external_tool_type', 'lti');
171
                $manualinstanceoption = $instancetypes[0]; // The 'Automatic, based on tool URL' option.
172
                $tooltypes->addOption($manualinstanceoption->name, 0, []);
173
                $mform->freeze('typeid');
174
            } else if ($matchestoolnotavailabletocourse) {
175
                // Legacy instances domain-matched to site tools: use a hidden field for typeid and a static visual element when
176
                // displaying these instances so that the string value of typeid is still visible when the element is frozen.
177
                // This gets around the fact that a frozen select without a selected option will display nothing.
178
                $mform->addElement('hidden', 'typeid', $typeid);
179
                $mform->setType('typeid', PARAM_INT);
180
 
181
                $manualinstanceoption = $instancetypes[0]; // The 'Automatic, based on tool URL' option.
182
                $mform->addElement('static', 'typeiddisplayonly', get_string('external_tool_type', 'lti'),
183
                    $manualinstanceoption->name);
184
            }
185
        } else {
186
            // Need to submit these still, but hidden to avoid instructor modification.
187
            $mform->addElement('hidden', 'typeid', $typeid);
188
            $mform->setType('typeid', PARAM_INT);
189
        }
190
 
191
        // Disable the content selection button unconditionally. Freeze/hardFreeze is unsuitable for buttons.
192
        $mform->addElement('button', 'selectcontent', get_string('selectcontent', 'lti'), ['disabled' => 'disabled']);
193
        $mform->disabledIf('selectcontent', 'typeid', 'eq', $typeid);
194
 
195
        if ($canviewmanualconfig) {
196
            $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), ['size' => '64']);
197
            $mform->setType('toolurl', PARAM_URL);
198
            $mform->addHelpButton('toolurl', 'launch_url', 'lti');
199
 
200
            $mform->addElement('text', 'securetoolurl', get_string('secure_launch_url', 'lti'), ['size' => '64']);
201
            $mform->setType('securetoolurl', PARAM_URL);
202
            $mform->setAdvanced('securetoolurl');
203
            $mform->addHelpButton('securetoolurl', 'secure_launch_url', 'lti');
204
        } else {
205
            // Need to submit these still, but hidden to avoid instructor modification.
206
            $mform->addElement('hidden', 'toolurl', '', ['id' => 'id_toolurl']);
207
            $mform->setType('toolurl', PARAM_URL);
208
            $mform->addElement('hidden', 'securetoolurl', '', ['id' => 'id_securetoolurl']);
209
            $mform->setType('securetoolurl', PARAM_URL);
210
        }
211
 
212
        $mform->addElement('hidden', 'lineitemresourceid', '', ['id' => 'id_lineitemresourceid']);
213
        $mform->setType('lineitemresourceid', PARAM_TEXT);
214
 
215
        $mform->addElement('hidden', 'lineitemtag', '', ['id' => 'id_lineitemtag']);
216
        $mform->setType('lineitemtag', PARAM_TEXT);
217
 
218
        $mform->addElement('hidden', 'lineitemsubreviewurl', '', ['id' => 'id_lineitemsubreviewurl']);
219
        $mform->setType('lineitemsubreviewurl', PARAM_URL);
220
 
221
        $mform->addElement('hidden', 'lineitemsubreviewparams', '', ['id' => 'id_lineitemsubreviewparams']);
222
        $mform->setType('lineitemsubreviewparams', PARAM_TEXT);
223
 
224
        $launchoptions = [
225
            LTI_LAUNCH_CONTAINER_DEFAULT => get_string('default', 'lti'),
226
            LTI_LAUNCH_CONTAINER_EMBED => get_string('embed', 'lti'),
227
            LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS => get_string('embed_no_blocks', 'lti'),
228
            LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW => get_string('existing_window', 'lti'),
229
            LTI_LAUNCH_CONTAINER_WINDOW => get_string('new_window', 'lti')
230
        ];
231
        $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);
232
        $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti');
233
        $mform->setAdvanced('launchcontainer');
234
 
235
        if ($canviewmanualconfig) {
236
            $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));
237
            $mform->setType('resourcekey', PARAM_TEXT);
238
            $mform->setAdvanced('resourcekey');
239
            $mform->addHelpButton('resourcekey', 'resourcekey', 'lti');
240
            $mform->setForceLtr('resourcekey');
241
 
242
            $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));
243
            $mform->setType('password', PARAM_TEXT);
244
            $mform->setAdvanced('password');
245
            $mform->addHelpButton('password', 'password', 'lti');
246
 
247
            $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), ['rows' => 4, 'cols' => 60]);
248
            $mform->setType('instructorcustomparameters', PARAM_TEXT);
249
            $mform->setAdvanced('instructorcustomparameters');
250
            $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
251
            $mform->setForceLtr('instructorcustomparameters');
252
 
253
            $mform->addElement('text', 'icon', get_string('icon_url', 'lti'), ['size' => '64']);
254
            $mform->setType('icon', PARAM_URL);
255
            $mform->setAdvanced('icon');
256
            $mform->addHelpButton('icon', 'icon_url', 'lti');
257
 
258
            $mform->addElement('text', 'secureicon', get_string('secure_icon_url', 'lti'), ['size' => '64']);
259
            $mform->setType('secureicon', PARAM_URL);
260
            $mform->setAdvanced('secureicon');
261
            $mform->addHelpButton('secureicon', 'secure_icon_url', 'lti');
262
        } else {
263
            // Need to submit these still, but hidden to avoid instructor modification.
264
            $mform->addElement('hidden', 'resourcekey', '', ['id' => 'id_resourcekey']);
265
            $mform->setType('resourcekey', PARAM_TEXT);
266
            $mform->addElement('hidden', 'password', '', ['id' => 'id_password']);
267
            $mform->setType('password', PARAM_TEXT);
268
            $mform->addElement('hidden', 'instructorcustomparameters', '', ['id' => 'id_instructorcustomparameters']);
269
            $mform->setType('instructorcustomparameters', PARAM_TEXT);
270
            $mform->addElement('hidden', 'icon', '', ['id' => 'id_icon']);
271
            $mform->setType('icon', PARAM_URL);
272
            $mform->addElement('hidden', 'secureicon', '', ['id' => 'id_secureicon']);
273
            $mform->setType('secureicon', PARAM_URL);
274
        }
275
 
276
        // Add privacy preferences fieldset where users choose whether to send their data.
277
        $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
278
 
279
        $mform->addElement('advcheckbox', 'instructorchoicesendname', get_string('share_name', 'lti'));
280
        $mform->addHelpButton('instructorchoicesendname', 'share_name', 'lti');
281
 
282
        $mform->addElement('advcheckbox', 'instructorchoicesendemailaddr', get_string('share_email', 'lti'));
283
        $mform->addHelpButton('instructorchoicesendemailaddr', 'share_email', 'lti');
284
 
285
        $mform->addElement('advcheckbox', 'instructorchoiceacceptgrades', get_string('accept_grades', 'lti'));
286
        $mform->addHelpButton('instructorchoiceacceptgrades', 'accept_grades', 'lti');
287
 
288
        // Add standard course module grading elements.
289
        $this->standard_grading_coursemodule_elements();
290
 
291
        // Add standard elements, common to all modules.
292
        $this->standard_coursemodule_elements();
293
        $mform->setAdvanced('cmidnumber');
294
 
295
        // Add standard buttons, common to all modules.
296
        $this->add_action_buttons();
297
 
298
        $mform->hardFreeze([
299
            'toolurl',
300
            'securetoolurl',
301
            'launchcontainer',
302
            'instructorcustomparameters',
303
            'icon',
304
            'secureicon',
305
            'instructorchoicesendname',
306
            'instructorchoicesendemailaddr',
307
            'instructorchoiceacceptgrades'
308
        ]);
309
    }
310
 
311
    public function definition() {
312
        global $PAGE, $OUTPUT, $COURSE;
313
 
314
        if ($this->type) {
315
            component_callback("ltisource_$this->type", 'add_instance_hook');
316
        }
317
 
318
        // Determine whether this tool instance is a manually configure instance (now deprecated).
319
        $manualinstance = empty($this->current->typeid) && empty($this->typeid);
320
 
321
        // Determine whether this tool instance is using a domain-matched site tool which is not visible at the course level.
322
        // In such a case, the instance has a typeid (the site tool) and toolurl (the url used to domain match the site tool) set,
323
        // and the type still exists (is not deleted).
324
        $instancetypes = lti_get_types_for_add_instance();
325
        $matchestoolnotavailabletocourse = false;
326
        if (!$manualinstance && !empty($this->current->toolurl)) {
327
            if (lti_get_type_config($this->current->typeid)) {
328
                $matchestoolnotavailabletocourse = !in_array($this->current->typeid, array_keys($instancetypes));
329
            }
330
        }
331
 
332
        // Display the legacy form, presenting a read-only view of the configuration for unsupported (since 4.3) instances, which:
333
        // - Are manually configured instances (no longer supported. course tools should be configured and used instead).
334
        // - Are domain-matched to a hidden site level tool (no longer supported. to be replaced by URL-based course tool creation)
335
        // Instances based on preconfigured tools and which are not domain matched as above, are still valid and will be shown using
336
        // the non-legacy form.
337
        if ($manualinstance || $matchestoolnotavailabletocourse) {
338
            $this->legacy_instance_form_definition($instancetypes);
339
            return;
340
        }
341
 
342
        $tooltypeid = $this->current->typeid ?? $this->typeid;
343
        $tooltype = lti_get_type($tooltypeid);
344
 
345
        // Store the id of the tool type should it be linked to a tool proxy, to aid in disabling certain form elements.
346
        $toolproxytypeid = $tooltype->toolproxyid ? $tooltypeid : '';
347
 
348
        $issitetooltype = $tooltype->course == get_site()->id;
349
 
350
        $mform =& $this->_form;
351
 
352
        // Adding the "general" fieldset, where all the common settings are shown.
353
        $mform->addElement('html', "<div data-attribute='dynamic-import' hidden aria-hidden='true' role='alert'></div>");
354
        $mform->addElement('header', 'general', get_string('general', 'form'));
355
 
356
        // For tools supporting content selection, add the 'Select content button'.
357
        $config = lti_get_type_config($tooltypeid);
358
        $supportscontentitemselection = !empty($config['contentitem']);
359
 
360
        if ($supportscontentitemselection) {
361
            $contentitemurl = new moodle_url('/mod/lti/contentitem.php');
362
            $contentbuttonattributes = [
363
                'data-contentitemurl' => $contentitemurl->out(false),
364
            ];
365
 
366
            // If this is an instance, was it created based on content selection in a prior-edit (need to infer since not stored).
367
            $iscontentitem = !empty($this->current->id)
368
                && (!empty($this->current->toolurl) || !empty($this->current->instructorcustomparameters)
369
                || !empty($this->current->secureicon) || !empty($this->current->icon));
370
 
371
            $selectcontentindicatorinner = $iscontentitem ?
372
                $OUTPUT->pix_icon('i/valid', get_string('contentselected', 'mod_lti'), 'moodle', ['class' => 'mr-1'])
373
                . get_string('contentselected', 'mod_lti') : '';
374
            $selectcontentindicator = html_writer::div($selectcontentindicatorinner, '',
375
                ['aria-role' => 'status', 'id' => 'id_selectcontentindicator']);
376
            $selectcontentgrp = [
377
                $mform->createElement('button', 'selectcontent', get_string('selectcontent', 'mod_lti'), $contentbuttonattributes,
11 efrain 378
                    ['customclassoverride' => 'btn-secondary']),
1 efrain 379
                $mform->createElement('html', $selectcontentindicator),
380
            ];
381
            $mform->addGroup($selectcontentgrp, 'selectcontentgroup', get_string('content'), ' ', false);
382
        }
383
 
384
        // Adding the standard "name" field.
385
        $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), ['size' => '64']);
386
        $mform->setType('name', PARAM_TEXT);
387
        $mform->addRule('name', null, 'required', null, 'server');
388
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server');
389
 
390
        // Show activity name when launched only applies to embedded type launches.
391
        if (in_array($config['launchcontainer'], [LTI_LAUNCH_CONTAINER_EMBED, LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS])) {
392
            $mform->addElement('checkbox', 'showtitlelaunch', get_string('display_name', 'lti'));
393
            $mform->setDefault('showtitlelaunch', true);
394
            $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
395
        } else {
396
            // Include in the form anyway, so we retain the setting value in case the tool launch container is changed back.
397
            $mform->addElement('hidden', 'showtitlelaunch');
398
            $mform->setType('showtitlelaunch', PARAM_BOOL);
399
        }
400
 
401
        // Adding the optional "intro" and "introformat" pair of fields.
402
        $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
403
 
404
        // Display the label to the right of the checkbox so it looks better & matches rest of the form.
405
        if ($mform->elementExists('showdescription')) {
406
            $coursedesc = $mform->getElement('showdescription');
407
            if (!empty($coursedesc)) {
408
                $coursedesc->setText(' ' . $coursedesc->getLabel());
409
                $coursedesc->setLabel('&nbsp');
410
            }
411
        }
412
 
413
        // Show activity description when launched only applies to embedded type launches.
414
        if (in_array($config['launchcontainer'], [LTI_LAUNCH_CONTAINER_EMBED, LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS])) {
415
            $mform->addElement('checkbox', 'showdescriptionlaunch', get_string('display_description', 'lti'));
416
            $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
417
        } else {
418
            // Include in the form anyway, so we retain the setting value in case the tool launch container is changed back.
419
            $mform->addElement('hidden', 'showdescriptionlaunch');
420
            $mform->setType('showdescriptionlaunch', PARAM_BOOL);
421
        }
422
 
423
        // Tool settings.
424
        $mform->addElement('hidden', 'typeid', $tooltypeid, ['id' => 'hidden_typeid']);
425
        $mform->setType('typeid', PARAM_INT);
426
        if (!empty($config['contentitem'])) {
427
            $mform->addElement('hidden', 'contentitem', 1);
428
            $mform->setType('contentitem', PARAM_INT);
429
        }
430
 
431
        // Included to support deep linking return, but hidden to avoid instructor modification.
432
        $mform->addElement('hidden', 'toolurl', '', ['id' => 'id_toolurl']);
433
        $mform->setType('toolurl', PARAM_URL);
434
        $mform->addElement('hidden', 'securetoolurl', '', ['id' => 'id_securetoolurl']);
435
        $mform->setType('securetoolurl', PARAM_URL);
436
 
437
        $mform->addElement('hidden', 'urlmatchedtypeid', '', ['id' => 'id_urlmatchedtypeid']);
438
        $mform->setType('urlmatchedtypeid', PARAM_INT);
439
 
440
        $mform->addElement('hidden', 'lineitemresourceid', '', ['id' => 'id_lineitemresourceid']);
441
        $mform->setType('lineitemresourceid', PARAM_TEXT);
442
 
443
        $mform->addElement('hidden', 'lineitemtag', '', ['id' => 'id_lineitemtag']);
444
        $mform->setType('lineitemtag', PARAM_TEXT);
445
 
446
        $mform->addElement('hidden', 'lineitemsubreviewurl', '', ['id' => 'id_lineitemsubreviewurl']);
447
        $mform->setType('lineitemsubreviewurl', PARAM_URL);
448
 
449
        $mform->addElement('hidden', 'lineitemsubreviewparams', '', ['id' => 'id_lineitemsubreviewparams']);
450
        $mform->setType('lineitemsubreviewparams', PARAM_TEXT);
451
 
452
        // Launch container is set to 'LTI_LAUNCH_CONTAINER_DEFAULT', meaning it'll delegate to the tool's configuration.
453
        // Existing instances using values other than this can continue to use their existing value but cannot change it.
454
        $mform->addElement('hidden', 'launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);
455
        $mform->setType('launchcontainer', PARAM_INT);
456
 
457
        // Included to support deep linking return, but hidden to avoid instructor modification.
458
        $mform->addElement('hidden', 'resourcekey', '', ['id' => 'id_resourcekey']);
459
        $mform->setType('resourcekey', PARAM_TEXT);
460
        $mform->addElement('hidden', 'password', '', ['id' => 'id_password']);
461
        $mform->setType('password', PARAM_TEXT);
462
        $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'),
463
            ['rows' => 4, 'cols' => 60]);
464
        $mform->setType('instructorcustomparameters', PARAM_TEXT);
465
        $mform->setAdvanced('instructorcustomparameters');
466
        $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
467
        $mform->setForceLtr('instructorcustomparameters');
468
        $mform->addElement('hidden', 'icon', '', ['id' => 'id_icon']);
469
        $mform->setType('icon', PARAM_URL);
470
        $mform->addElement('hidden', 'secureicon', '', ['id' => 'id_secureicon']);
471
        $mform->setType('secureicon', PARAM_URL);
472
 
473
        // Add standard course module grading elements, and show them if the tool type + instance config permits it.
474
        if (!empty($config['acceptgrades']) && in_array($config['acceptgrades'], [LTI_SETTING_ALWAYS, LTI_SETTING_DELEGATE])) {
475
            $elementnamesbeforegrading = $this->_form->_elementIndex;
476
            $this->standard_grading_coursemodule_elements();
477
            $elementnamesaftergrading = $this->_form->_elementIndex;
478
 
479
            // For all 'real' elements (not hidden or header) added as part of the standard grading elements, add a hideIf rule
480
            // making the element dependent on the 'accept grades from the tool' checkbox (instructorchoiceacceptgrades).
481
            $diff = array_diff($elementnamesaftergrading, $elementnamesbeforegrading);
482
            $diff = array_filter($diff, fn($key) => !in_array($this->_form->_elements[$key]->_type, ['hidden', 'header']));
483
            foreach ($diff as $gradeelementname => $gradeelementindex) {
484
                $mform->hideIf($gradeelementname, 'instructorchoiceacceptgrades', 'eq', 0);
485
            }
486
 
487
            // Extend the grade section with the 'accept grades from the tool' checkbox, allowing per-instance overrides of that
488
            // value according to the following rules:
489
            // - Site tools with 'acceptgrades' set to 'ALWAYS' do not permit overrides at the instance level; the checkbox is
490
            // omitted in such cases.
491
            // - Site tools with 'acceptgrades' set to 'DELEGATE' result in a checkbox that is defaulted to unchecked but which
492
            // permits overrides to 'yes/checked'.
493
            // - Course tools with 'acceptgrades' set to 'ALWAYS' result in a checkbox that is defaulted to checked but which
494
            // permits overrides to 'no/unchecked'.
495
            // - Course tools with 'acceptgrades' set to 'DELEGATE' result in a checkbox that is defaulted to unchecked but which
496
            // permits overrides to 'yes/checked'.
497
            if (($issitetooltype && $config['acceptgrades'] == LTI_SETTING_DELEGATE) || !$issitetooltype) {
498
                $mform->insertElementBefore(
499
                    $mform->createElement(
500
                        'advcheckbox',
501
                        'instructorchoiceacceptgrades',
502
                        get_string('accept_grades_from_tool', 'mod_lti', $tooltype->name)
503
                    ),
504
                    array_keys($diff)[0]
505
                );
506
                $acceptgradesdefault = !$issitetooltype && $config['acceptgrades'] == LTI_SETTING_ALWAYS ? '1' : '0';
507
                $mform->setDefault('instructorchoiceacceptgrades', $acceptgradesdefault);
508
                $mform->disabledIf('instructorchoiceacceptgrades', 'typeid', 'in', [$toolproxytypeid]); // LTI 2 only.
509
            }
510
        }
511
 
512
        // Add standard elements, common to all modules.
513
        $this->standard_coursemodule_elements();
514
        $mform->setAdvanced('cmidnumber');
515
 
516
        // Add standard buttons, common to all modules.
517
        $this->add_action_buttons();
518
 
519
        if ($supportscontentitemselection) {
520
            $PAGE->requires->js_call_amd('mod_lti/mod_form', 'init', [$COURSE->id]);
521
        }
522
    }
523
 
524
    /**
525
     * Sets the current values handled by services in case of update.
526
     *
527
     * @param object $defaultvalues default values to populate the form with.
528
     */
529
    public function set_data($defaultvalues) {
530
        $services = lti_get_services();
531
        if (is_object($defaultvalues)) {
532
            foreach ($services as $service) {
533
                $service->set_instance_form_values( $defaultvalues );
534
            }
535
        }
536
        parent::set_data($defaultvalues);
537
    }
538
}