on-demand release 4.2dev+
[moodle.git] / mod / lti / mod_form.php
CommitLineData
996b0fd9 1<?php
61eb12d4
CS
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//
996b0fd9
CS
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
e3f69b58 33// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
996b0fd9
CS
34
35/**
61eb12d4 36 * This file defines the main lti configuration form
996b0fd9 37 *
2b17ec3d 38 * @package mod_lti
61eb12d4 39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
996b0fd9 40 * marc.alier@upc.edu
61eb12d4
CS
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
8f45215d 45 * @author Chris Scribner
61eb12d4 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
996b0fd9
CS
47 */
48
49defined('MOODLE_INTERNAL') || die;
50
51require_once($CFG->dirroot.'/course/moodleform_mod.php');
52require_once($CFG->dirroot.'/mod/lti/locallib.php');
53
b1e9a121 54class mod_lti_mod_form extends moodleform_mod {
996b0fd9 55
ea04a9f9 56 public function definition() {
c1fae2b9 57 global $PAGE, $OUTPUT, $COURSE;
996b0fd9 58
976b5bca
CS
59 if ($type = optional_param('type', false, PARAM_ALPHA)) {
60 component_callback("ltisource_$type", 'add_instance_hook');
61 }
8cf7670e 62
477e8717
DS
63 // Type ID parameter being passed when adding an preconfigured tool from activity chooser.
64 $typeid = optional_param('typeid', false, PARAM_INT);
65
f259405d 66 $showoptions = has_capability('mod/lti:addmanualinstance', $this->context);
477e8717
DS
67 // Show configuration details only if not preset (when new) or user has the capabilities to do so (when editing).
68 if ($this->_instance) {
69 $showtypes = has_capability('mod/lti:addpreconfiguredinstance', $this->context);
477e8717
DS
70 if (!$showoptions && $this->current->typeid == 0) {
71 // If you cannot add a manual instance and this is already a manual instance, then
72 // remove the 'types' selector.
73 $showtypes = false;
74 }
75 } else {
76 $showtypes = !$typeid;
477e8717
DS
77 }
78
996b0fd9 79 $this->typeid = 0;
e27cb316 80
996b0fd9 81 $mform =& $this->_form;
957da4b5 82
e3f69b58 83 // Adding the "general" fieldset, where all the common settings are shown.
957da4b5 84 $mform->addElement('html', "<div data-attribute='dynamic-import' hidden aria-hidden='true' role='alert'></div>");
996b0fd9 85 $mform->addElement('header', 'general', get_string('general', 'form'));
957da4b5 86
e3f69b58 87 // Adding the standard "name" field.
88 $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), array('size' => '64'));
996b0fd9
CS
89 $mform->setType('name', PARAM_TEXT);
90 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 91 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
e3f69b58 92 // Adding the optional "intro" and "introformat" pair of fields.
6398ff53 93 $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
996b0fd9 94 $mform->setAdvanced('introeditor');
194f2c60 95
e3f69b58 96 // Display the label to the right of the checkbox so it looks better & matches rest of the form.
8b40604a 97 if ($mform->elementExists('showdescription')) {
7046798f
DM
98 $coursedesc = $mform->getElement('showdescription');
99 if (!empty($coursedesc)) {
100 $coursedesc->setText(' ' . $coursedesc->getLabel());
101 $coursedesc->setLabel('&nbsp');
102 }
b07878ec 103 }
194f2c60 104
b07878ec 105 $mform->setAdvanced('showdescription');
996b0fd9 106
793aa035 107 $mform->addElement('checkbox', 'showtitlelaunch', get_string('display_name', 'lti'));
b07878ec 108 $mform->setAdvanced('showtitlelaunch');
80fd0072 109 $mform->setDefault('showtitlelaunch', true);
b07878ec 110 $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
e27cb316 111
793aa035 112 $mform->addElement('checkbox', 'showdescriptionlaunch', get_string('display_description', 'lti'));
b07878ec
CS
113 $mform->setAdvanced('showdescriptionlaunch');
114 $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
e27cb316 115
e3f69b58 116 // Tool settings.
e3f69b58 117 $toolproxy = array();
c1fae2b9 118 // Array of tool type IDs that don't support ContentItemSelectionRequest.
2d313053 119 $noncontentitemtypes = [];
c1fae2b9 120
477e8717
DS
121 if ($showtypes) {
122 $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'));
123 if ($typeid) {
124 $mform->getElement('typeid')->setValue($typeid);
996b0fd9 125 }
477e8717
DS
126 $mform->addHelpButton('typeid', 'external_tool_type', 'lti');
127
128 foreach (lti_get_types_for_add_instance() as $id => $type) {
129 if (!empty($type->toolproxyid)) {
130 $toolproxy[] = $type->id;
131 $attributes = array('globalTool' => 1, 'toolproxy' => 1);
132 $enabledcapabilities = explode("\n", $type->enabledcapability);
133 if (!in_array('Result.autocreate', $enabledcapabilities) ||
134 in_array('BasicOutcome.url', $enabledcapabilities)) {
135 $attributes['nogrades'] = 1;
136 }
137 if (!in_array('Person.name.full', $enabledcapabilities) &&
138 !in_array('Person.name.family', $enabledcapabilities) &&
139 !in_array('Person.name.given', $enabledcapabilities)) {
140 $attributes['noname'] = 1;
141 }
142 if (!in_array('Person.email.primary', $enabledcapabilities)) {
143 $attributes['noemail'] = 1;
144 }
145 } else if ($type->course == $COURSE->id) {
146 $attributes = array('editable' => 1, 'courseTool' => 1, 'domain' => $type->tooldomain);
147 } else if ($id != 0) {
148 $attributes = array('globalTool' => 1, 'domain' => $type->tooldomain);
c1fae2b9 149 } else {
477e8717 150 $attributes = array();
d8f9109a 151 }
477e8717
DS
152
153 if ($id) {
154 $config = lti_get_type_config($id);
155 if (!empty($config['contentitem'])) {
156 $attributes['data-contentitem'] = 1;
157 $attributes['data-id'] = $id;
158 } else {
159 $noncontentitemtypes[] = $id;
160 }
161 }
162 $tooltypes->addOption($type->name, $id, $attributes);
d8f9109a 163 }
477e8717
DS
164 } else {
165 $mform->addElement('hidden', 'typeid', $typeid);
166 $mform->setType('typeid', PARAM_INT);
b791ed15
DS
167 if ($typeid) {
168 $config = lti_get_type_config($typeid);
169 if (!empty($config['contentitem'])) {
170 $mform->addElement('hidden', 'contentitem', 1);
171 $mform->setType('contentitem', PARAM_INT);
172 }
173 }
996b0fd9 174 }
c1fae2b9 175
1b669ebe 176 // Add button that launches the content-item selection dialogue.
3a69044f
DS
177 // Set contentitem URL.
178 $contentitemurl = new moodle_url('/mod/lti/contentitem.php');
179 $contentbuttonattributes = [
180 'data-contentitemurl' => $contentitemurl->out(false)
181 ];
182 if (!$showtypes) {
183 if (!$typeid || empty(lti_get_type_config($typeid)['contentitem'])) {
184 $contentbuttonattributes['disabled'] = 'disabled';
185 }
186 }
187 $contentbuttonlabel = get_string('selectcontent', 'lti');
188 $contentbutton = $mform->addElement('button', 'selectcontent', $contentbuttonlabel, $contentbuttonattributes);
189 // Disable select content button if the selected tool doesn't support content item or it's set to Automatic.
190 if ($showtypes) {
477e8717
DS
191 $allnoncontentitemtypes = $noncontentitemtypes;
192 $allnoncontentitemtypes[] = '0'; // Add option value for "Automatic, based on tool URL".
193 $mform->disabledIf('selectcontent', 'typeid', 'in', $allnoncontentitemtypes);
3a69044f 194 }
477e8717 195
3a69044f 196 if ($showoptions) {
477e8717
DS
197 $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), array('size' => '64'));
198 $mform->setType('toolurl', PARAM_URL);
199 $mform->addHelpButton('toolurl', 'launch_url', 'lti');
200 $mform->hideIf('toolurl', 'typeid', 'in', $noncontentitemtypes);
201
202 $mform->addElement('text', 'securetoolurl', get_string('secure_launch_url', 'lti'), array('size' => '64'));
203 $mform->setType('securetoolurl', PARAM_URL);
204 $mform->setAdvanced('securetoolurl');
205 $mform->addHelpButton('securetoolurl', 'secure_launch_url', 'lti');
206 $mform->hideIf('securetoolurl', 'typeid', 'in', $noncontentitemtypes);
f259405d
CV
207 } else {
208 // We still need those on page to support deep linking return, but hidden to avoid instructor modification.
209 $mform->addElement('hidden', 'toolurl', '', array('id' => 'id_toolurl'));
210 $mform->setType('toolurl', PARAM_URL);
211 $mform->addElement('hidden', 'securetoolurl', '', array('id' => 'id_securetoolurl'));
212 $mform->setType('securetoolurl', PARAM_URL);
477e8717 213 }
e27cb316 214
f259405d 215 $mform->addElement('hidden', 'urlmatchedtypeid', '', array('id' => 'id_urlmatchedtypeid'));
ea5d0515
AF
216 $mform->setType('urlmatchedtypeid', PARAM_INT);
217
e5423ddb
CV
218 $mform->addElement('hidden', 'lineitemresourceid', '', array( 'id' => 'id_lineitemresourceid' ));
219 $mform->setType('lineitemresourceid', PARAM_TEXT);
220
221 $mform->addElement('hidden', 'lineitemtag', '', array( 'id' => 'id_lineitemtag'));
222 $mform->setType('lineitemtag', PARAM_TEXT);
223
12e207de
CV
224 $mform->addElement('hidden', 'lineitemsubreviewurl', '', array( 'id' => 'id_lineitemsubreviewurl'));
225 $mform->setType('lineitemsubreviewurl', PARAM_URL);
226
227 $mform->addElement('hidden', 'lineitemsubreviewparams', '', array( 'id' => 'id_lineitemsubreviewparams'));
228 $mform->setType('lineitemsubreviewparams', PARAM_TEXT);
229
3a69044f
DS
230 $launchoptions = array();
231 $launchoptions[LTI_LAUNCH_CONTAINER_DEFAULT] = get_string('default', 'lti');
232 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');
233 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');
234 $launchoptions[LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW] = get_string('existing_window', 'lti');
235 $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');
477e8717 236
3a69044f
DS
237 $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);
238 $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);
239 $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti');
240 $mform->setAdvanced('launchcontainer');
241
242 if ($showoptions) {
477e8717
DS
243 $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));
244 $mform->setType('resourcekey', PARAM_TEXT);
245 $mform->setAdvanced('resourcekey');
246 $mform->addHelpButton('resourcekey', 'resourcekey', 'lti');
247 $mform->setForceLtr('resourcekey');
248 $mform->hideIf('resourcekey', 'typeid', 'in', $noncontentitemtypes);
249
250 $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));
251 $mform->setType('password', PARAM_TEXT);
252 $mform->setAdvanced('password');
253 $mform->addHelpButton('password', 'password', 'lti');
254 $mform->hideIf('password', 'typeid', 'in', $noncontentitemtypes);
255
256 $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), array('rows' => 4, 'cols' => 60));
257 $mform->setType('instructorcustomparameters', PARAM_TEXT);
258 $mform->setAdvanced('instructorcustomparameters');
259 $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
260 $mform->setForceLtr('instructorcustomparameters');
261
262 $mform->addElement('text', 'icon', get_string('icon_url', 'lti'), array('size' => '64'));
263 $mform->setType('icon', PARAM_URL);
264 $mform->setAdvanced('icon');
265 $mform->addHelpButton('icon', 'icon_url', 'lti');
266 $mform->hideIf('icon', 'typeid', 'in', $noncontentitemtypes);
267
268 $mform->addElement('text', 'secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
269 $mform->setType('secureicon', PARAM_URL);
270 $mform->setAdvanced('secureicon');
271 $mform->addHelpButton('secureicon', 'secure_icon_url', 'lti');
272 $mform->hideIf('secureicon', 'typeid', 'in', $noncontentitemtypes);
f259405d 273 } else {
779e753f
CV
274 // Keep those in the form to allow deep linking.
275 $mform->addElement('hidden', 'resourcekey', '', array('id' => 'id_resourcekey'));
276 $mform->setType('resourcekey', PARAM_TEXT);
277 $mform->addElement('hidden', 'password', '', array('id' => 'id_password'));
278 $mform->setType('password', PARAM_TEXT);
f259405d
CV
279 $mform->addElement('hidden', 'instructorcustomparameters', '', array('id' => 'id_instructorcustomparameters'));
280 $mform->setType('instructorcustomparameters', PARAM_TEXT);
281 $mform->addElement('hidden', 'icon', '', array('id' => 'id_icon'));
282 $mform->setType('icon', PARAM_URL);
283 $mform->addElement('hidden', 'secureicon', '', array('id' => 'id_secureicon'));
284 $mform->setType('secureicon', PARAM_URL);
477e8717 285 }
e27cb316 286
e3f69b58 287 // Add privacy preferences fieldset where users choose whether to send their data.
996b0fd9
CS
288 $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
289
793aa035 290 $mform->addElement('advcheckbox', 'instructorchoicesendname', get_string('share_name', 'lti'));
996b0fd9 291 $mform->setDefault('instructorchoicesendname', '1');
b26dc53c 292 $mform->addHelpButton('instructorchoicesendname', 'share_name', 'lti');
e3f69b58 293 $mform->disabledIf('instructorchoicesendname', 'typeid', 'in', $toolproxy);
e27cb316 294
793aa035 295 $mform->addElement('advcheckbox', 'instructorchoicesendemailaddr', get_string('share_email', 'lti'));
996b0fd9 296 $mform->setDefault('instructorchoicesendemailaddr', '1');
b26dc53c 297 $mform->addHelpButton('instructorchoicesendemailaddr', 'share_email', 'lti');
e3f69b58 298 $mform->disabledIf('instructorchoicesendemailaddr', 'typeid', 'in', $toolproxy);
e27cb316 299
793aa035 300 $mform->addElement('advcheckbox', 'instructorchoiceacceptgrades', get_string('accept_grades', 'lti'));
6f4011db 301 $mform->setDefault('instructorchoiceacceptgrades', '0');
b26dc53c 302 $mform->addHelpButton('instructorchoiceacceptgrades', 'accept_grades', 'lti');
e3f69b58 303 $mform->disabledIf('instructorchoiceacceptgrades', 'typeid', 'in', $toolproxy);
996b0fd9 304
8fa50fdd
MN
305 // Add standard course module grading elements.
306 $this->standard_grading_coursemodule_elements();
307
e3f69b58 308 // Add standard elements, common to all modules.
996b0fd9 309 $this->standard_coursemodule_elements();
996b0fd9 310 $mform->setAdvanced('cmidnumber');
e3f69b58 311
312 // Add standard buttons, common to all modules.
996b0fd9
CS
313 $this->add_action_buttons();
314
7302fbf6
TH
315 $editurl = new moodle_url('/mod/lti/instructor_edit_tool_type.php',
316 array('sesskey' => sesskey(), 'course' => $COURSE->id));
6831c7cd 317 $ajaxurl = new moodle_url('/mod/lti/ajax.php');
e27cb316 318
663640f5
DW
319 // All these icon uses are incorrect. LTI JS needs updating to use AMD modules and templates so it can use
320 // the mustache pix helper - until then LTI will have inconsistent icons.
996b0fd9 321 $jsinfo = (object)array(
663640f5
DW
322 'edit_icon_url' => (string)$OUTPUT->image_url('t/edit'),
323 'add_icon_url' => (string)$OUTPUT->image_url('t/add'),
324 'delete_icon_url' => (string)$OUTPUT->image_url('t/delete'),
325 'green_check_icon_url' => (string)$OUTPUT->image_url('i/valid'),
326 'warning_icon_url' => (string)$OUTPUT->image_url('warning', 'lti'),
6831c7cd
CS
327 'instructor_tool_type_edit_url' => $editurl->out(false),
328 'ajax_url' => $ajaxurl->out(true),
329 'courseId' => $COURSE->id
996b0fd9 330 );
e27cb316 331
996b0fd9 332 $module = array(
e3f69b58 333 'name' => 'mod_lti_edit',
334 'fullpath' => '/mod/lti/mod_form.js',
335 'requires' => array('base', 'io', 'querystring-stringify-simple', 'node', 'event', 'json-parse'),
336 'strings' => array(
6831c7cd
CS
337 array('addtype', 'lti'),
338 array('edittype', 'lti'),
339 array('deletetype', 'lti'),
340 array('delete_confirmation', 'lti'),
341 array('cannot_edit', 'lti'),
342 array('cannot_delete', 'lti'),
343 array('global_tool_types', 'lti'),
16e8f130
CS
344 array('course_tool_types', 'lti'),
345 array('using_tool_configuration', 'lti'),
af9d3a92 346 array('using_tool_cartridge', 'lti'),
16e8f130
CS
347 array('domain_mismatch', 'lti'),
348 array('custom_config', 'lti'),
4c598b13 349 array('tool_config_not_found', 'lti'),
af9d3a92
JO
350 array('tooltypeadded', 'lti'),
351 array('tooltypedeleted', 'lti'),
352 array('tooltypenotdeleted', 'lti'),
353 array('tooltypeupdated', 'lti'),
4c598b13 354 array('forced_help', 'lti')
6831c7cd 355 ),
996b0fd9 356 );
e27cb316 357
01e8bfd7
JO
358 if (!empty($typeid)) {
359 $mform->setAdvanced('typeid');
360 $mform->setAdvanced('toolurl');
361 }
362
996b0fd9
CS
363 $PAGE->requires->js_init_call('M.mod_lti.editor.init', array(json_encode($jsinfo)), true, $module);
364 }
365
e5423ddb 366 /**
cfbc361d 367 * Sets the current values handled by services in case of update.
e5423ddb
CV
368 *
369 * @param object $defaultvalues default values to populate the form with.
370 */
371 public function set_data($defaultvalues) {
cfbc361d
CV
372 $services = lti_get_services();
373 if (is_object($defaultvalues)) {
374 foreach ($services as $service) {
375 $service->set_instance_form_values( $defaultvalues );
e5423ddb
CV
376 }
377 }
e5423ddb
CV
378 parent::set_data($defaultvalues);
379 }
996b0fd9 380}