MDL-62815 mod_lti: Capabilities for adding default and preconfigured
[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
996b0fd9 63 $this->typeid = 0;
e27cb316 64
996b0fd9 65 $mform =& $this->_form;
e3f69b58 66 // Adding the "general" fieldset, where all the common settings are shown.
996b0fd9 67 $mform->addElement('header', 'general', get_string('general', 'form'));
e3f69b58 68 // Adding the standard "name" field.
69 $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), array('size' => '64'));
996b0fd9
CS
70 $mform->setType('name', PARAM_TEXT);
71 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 72 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
e3f69b58 73 // Adding the optional "intro" and "introformat" pair of fields.
6398ff53 74 $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
996b0fd9 75 $mform->setAdvanced('introeditor');
194f2c60 76
e3f69b58 77 // Display the label to the right of the checkbox so it looks better & matches rest of the form.
8b40604a 78 if ($mform->elementExists('showdescription')) {
7046798f
DM
79 $coursedesc = $mform->getElement('showdescription');
80 if (!empty($coursedesc)) {
81 $coursedesc->setText(' ' . $coursedesc->getLabel());
82 $coursedesc->setLabel('&nbsp');
83 }
b07878ec 84 }
194f2c60 85
b07878ec 86 $mform->setAdvanced('showdescription');
996b0fd9 87
b07878ec
CS
88 $mform->addElement('checkbox', 'showtitlelaunch', '&nbsp;', ' ' . get_string('display_name', 'lti'));
89 $mform->setAdvanced('showtitlelaunch');
80fd0072 90 $mform->setDefault('showtitlelaunch', true);
b07878ec 91 $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
e27cb316 92
b07878ec
CS
93 $mform->addElement('checkbox', 'showdescriptionlaunch', '&nbsp;', ' ' . get_string('display_description', 'lti'));
94 $mform->setAdvanced('showdescriptionlaunch');
95 $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
e27cb316 96
e3f69b58 97 // Tool settings.
1b669ebe
JP
98 $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'));
99 // Type ID parameter being passed when adding an preconfigured tool from activity chooser.
01e8bfd7 100 $typeid = optional_param('typeid', false, PARAM_INT);
1b669ebe
JP
101 if ($typeid) {
102 $mform->getElement('typeid')->setValue($typeid);
103 }
b26dc53c 104 $mform->addHelpButton('typeid', 'external_tool_type', 'lti');
e3f69b58 105 $toolproxy = array();
e27cb316 106
c1fae2b9 107 // Array of tool type IDs that don't support ContentItemSelectionRequest.
2d313053 108 $noncontentitemtypes = [];
c1fae2b9 109
ea04a9f9 110 foreach (lti_get_types_for_add_instance() as $id => $type) {
e3f69b58 111 if (!empty($type->toolproxyid)) {
112 $toolproxy[] = $type->id;
113 $attributes = array( 'globalTool' => 1, 'toolproxy' => 1);
114 $enabledcapabilities = explode("\n", $type->enabledcapability);
27cbb596 115 if (!in_array('Result.autocreate', $enabledcapabilities) || in_array('BasicOutcome.url', $enabledcapabilities)) {
e3f69b58 116 $attributes['nogrades'] = 1;
117 }
118 if (!in_array('Person.name.full', $enabledcapabilities) && !in_array('Person.name.family', $enabledcapabilities) &&
119 !in_array('Person.name.given', $enabledcapabilities)) {
120 $attributes['noname'] = 1;
121 }
122 if (!in_array('Person.email.primary', $enabledcapabilities)) {
123 $attributes['noemail'] = 1;
124 }
125 } else if ($type->course == $COURSE->id) {
16e8f130 126 $attributes = array( 'editable' => 1, 'courseTool' => 1, 'domain' => $type->tooldomain );
ea04a9f9 127 } else if ($id != 0) {
16e8f130 128 $attributes = array( 'globalTool' => 1, 'domain' => $type->tooldomain);
996b0fd9
CS
129 } else {
130 $attributes = array();
131 }
1b669ebe
JP
132
133 if ($id) {
d8f9109a 134 $config = lti_get_type_config($id);
c1fae2b9
JP
135 if (!empty($config['contentitem'])) {
136 $attributes['data-contentitem'] = 1;
137 $attributes['data-id'] = $id;
138 } else {
139 $noncontentitemtypes[] = $id;
d8f9109a 140 }
141 }
996b0fd9
CS
142 $tooltypes->addOption($type->name, $id, $attributes);
143 }
c1fae2b9 144
1b669ebe 145 // Add button that launches the content-item selection dialogue.
c1fae2b9
JP
146 // Set contentitem URL.
147 $contentitemurl = new moodle_url('/mod/lti/contentitem.php');
1b669ebe
JP
148 $contentbuttonattributes = [
149 'data-contentitemurl' => $contentitemurl->out(false)
150 ];
151 $contentbuttonlabel = get_string('selectcontent', 'lti');
152 $contentbutton = $mform->addElement('button', 'selectcontent', $contentbuttonlabel, $contentbuttonattributes);
153 // Disable select content button if the selected tool doesn't support content item or it's set to Automatic.
154 $allnoncontentitemtypes = $noncontentitemtypes;
155 $allnoncontentitemtypes[] = '0'; // Add option value for "Automatic, based on tool URL".
156 $mform->disabledIf('selectcontent', 'typeid', 'in', $allnoncontentitemtypes);
e27cb316 157
e3f69b58 158 $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), array('size' => '64'));
af9d3a92 159 $mform->setType('toolurl', PARAM_URL);
b26dc53c 160 $mform->addHelpButton('toolurl', 'launch_url', 'lti');
385247d9 161 $mform->hideIf('toolurl', 'typeid', 'in', $noncontentitemtypes);
e27cb316 162
e3f69b58 163 $mform->addElement('text', 'securetoolurl', get_string('secure_launch_url', 'lti'), array('size' => '64'));
af9d3a92 164 $mform->setType('securetoolurl', PARAM_URL);
d8d04121
CS
165 $mform->setAdvanced('securetoolurl');
166 $mform->addHelpButton('securetoolurl', 'secure_launch_url', 'lti');
385247d9 167 $mform->hideIf('securetoolurl', 'typeid', 'in', $noncontentitemtypes);
e27cb316 168
ea5d0515
AF
169 $mform->addElement('hidden', 'urlmatchedtypeid', '', array( 'id' => 'id_urlmatchedtypeid' ));
170 $mform->setType('urlmatchedtypeid', PARAM_INT);
171
e3f69b58 172 $launchoptions = array();
996b0fd9
CS
173 $launchoptions[LTI_LAUNCH_CONTAINER_DEFAULT] = get_string('default', 'lti');
174 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');
175 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');
8fa50fdd 176 $launchoptions[LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW] = get_string('existing_window', 'lti');
996b0fd9
CS
177 $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');
178
179 $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);
180 $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);
b26dc53c 181 $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti');
01e8bfd7 182 $mform->setAdvanced('launchcontainer');
e27cb316 183
996b0fd9
CS
184 $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));
185 $mform->setType('resourcekey', PARAM_TEXT);
186 $mform->setAdvanced('resourcekey');
b26dc53c 187 $mform->addHelpButton('resourcekey', 'resourcekey', 'lti');
525ef9c8 188 $mform->setForceLtr('resourcekey');
385247d9 189 $mform->hideIf('resourcekey', 'typeid', 'in', $noncontentitemtypes);
e27cb316 190
996b0fd9
CS
191 $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));
192 $mform->setType('password', PARAM_TEXT);
193 $mform->setAdvanced('password');
b26dc53c 194 $mform->addHelpButton('password', 'password', 'lti');
385247d9 195 $mform->hideIf('password', 'typeid', 'in', $noncontentitemtypes);
e27cb316 196
e3f69b58 197 $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), array('rows' => 4, 'cols' => 60));
996b0fd9
CS
198 $mform->setType('instructorcustomparameters', PARAM_TEXT);
199 $mform->setAdvanced('instructorcustomparameters');
b26dc53c 200 $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
525ef9c8 201 $mform->setForceLtr('instructorcustomparameters');
e27cb316 202
e3f69b58 203 $mform->addElement('text', 'icon', get_string('icon_url', 'lti'), array('size' => '64'));
3f358828 204 $mform->setType('icon', PARAM_URL);
6d462df8 205 $mform->setAdvanced('icon');
d8d04121 206 $mform->addHelpButton('icon', 'icon_url', 'lti');
385247d9 207 $mform->hideIf('icon', 'typeid', 'in', $noncontentitemtypes);
e27cb316 208
e3f69b58 209 $mform->addElement('text', 'secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
3f358828 210 $mform->setType('secureicon', PARAM_URL);
d8d04121
CS
211 $mform->setAdvanced('secureicon');
212 $mform->addHelpButton('secureicon', 'secure_icon_url', 'lti');
385247d9 213 $mform->hideIf('secureicon', 'typeid', 'in', $noncontentitemtypes);
e27cb316 214
e3f69b58 215 // Add privacy preferences fieldset where users choose whether to send their data.
996b0fd9
CS
216 $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
217
aa74cccb 218 $mform->addElement('advcheckbox', 'instructorchoicesendname', '&nbsp;', ' ' . get_string('share_name', 'lti'));
996b0fd9 219 $mform->setDefault('instructorchoicesendname', '1');
b26dc53c 220 $mform->addHelpButton('instructorchoicesendname', 'share_name', 'lti');
e3f69b58 221 $mform->disabledIf('instructorchoicesendname', 'typeid', 'in', $toolproxy);
e27cb316 222
aa74cccb 223 $mform->addElement('advcheckbox', 'instructorchoicesendemailaddr', '&nbsp;', ' ' . get_string('share_email', 'lti'));
996b0fd9 224 $mform->setDefault('instructorchoicesendemailaddr', '1');
b26dc53c 225 $mform->addHelpButton('instructorchoicesendemailaddr', 'share_email', 'lti');
e3f69b58 226 $mform->disabledIf('instructorchoicesendemailaddr', 'typeid', 'in', $toolproxy);
e27cb316 227
aa74cccb 228 $mform->addElement('advcheckbox', 'instructorchoiceacceptgrades', '&nbsp;', ' ' . get_string('accept_grades', 'lti'));
996b0fd9 229 $mform->setDefault('instructorchoiceacceptgrades', '1');
b26dc53c 230 $mform->addHelpButton('instructorchoiceacceptgrades', 'accept_grades', 'lti');
e3f69b58 231 $mform->disabledIf('instructorchoiceacceptgrades', 'typeid', 'in', $toolproxy);
996b0fd9 232
8fa50fdd
MN
233 // Add standard course module grading elements.
234 $this->standard_grading_coursemodule_elements();
235
e3f69b58 236 // Add standard elements, common to all modules.
996b0fd9 237 $this->standard_coursemodule_elements();
996b0fd9 238 $mform->setAdvanced('cmidnumber');
e3f69b58 239
240 // Add standard buttons, common to all modules.
996b0fd9
CS
241 $this->add_action_buttons();
242
7302fbf6
TH
243 $editurl = new moodle_url('/mod/lti/instructor_edit_tool_type.php',
244 array('sesskey' => sesskey(), 'course' => $COURSE->id));
6831c7cd 245 $ajaxurl = new moodle_url('/mod/lti/ajax.php');
e27cb316 246
663640f5
DW
247 // All these icon uses are incorrect. LTI JS needs updating to use AMD modules and templates so it can use
248 // the mustache pix helper - until then LTI will have inconsistent icons.
996b0fd9 249 $jsinfo = (object)array(
663640f5
DW
250 'edit_icon_url' => (string)$OUTPUT->image_url('t/edit'),
251 'add_icon_url' => (string)$OUTPUT->image_url('t/add'),
252 'delete_icon_url' => (string)$OUTPUT->image_url('t/delete'),
253 'green_check_icon_url' => (string)$OUTPUT->image_url('i/valid'),
254 'warning_icon_url' => (string)$OUTPUT->image_url('warning', 'lti'),
6831c7cd
CS
255 'instructor_tool_type_edit_url' => $editurl->out(false),
256 'ajax_url' => $ajaxurl->out(true),
257 'courseId' => $COURSE->id
996b0fd9 258 );
e27cb316 259
996b0fd9 260 $module = array(
e3f69b58 261 'name' => 'mod_lti_edit',
262 'fullpath' => '/mod/lti/mod_form.js',
263 'requires' => array('base', 'io', 'querystring-stringify-simple', 'node', 'event', 'json-parse'),
264 'strings' => array(
6831c7cd
CS
265 array('addtype', 'lti'),
266 array('edittype', 'lti'),
267 array('deletetype', 'lti'),
268 array('delete_confirmation', 'lti'),
269 array('cannot_edit', 'lti'),
270 array('cannot_delete', 'lti'),
271 array('global_tool_types', 'lti'),
16e8f130
CS
272 array('course_tool_types', 'lti'),
273 array('using_tool_configuration', 'lti'),
af9d3a92 274 array('using_tool_cartridge', 'lti'),
16e8f130
CS
275 array('domain_mismatch', 'lti'),
276 array('custom_config', 'lti'),
4c598b13 277 array('tool_config_not_found', 'lti'),
af9d3a92
JO
278 array('tooltypeadded', 'lti'),
279 array('tooltypedeleted', 'lti'),
280 array('tooltypenotdeleted', 'lti'),
281 array('tooltypeupdated', 'lti'),
4c598b13 282 array('forced_help', 'lti')
6831c7cd 283 ),
996b0fd9 284 );
e27cb316 285
01e8bfd7
JO
286 if (!empty($typeid)) {
287 $mform->setAdvanced('typeid');
288 $mform->setAdvanced('toolurl');
289 }
290
996b0fd9 291 $PAGE->requires->js_init_call('M.mod_lti.editor.init', array(json_encode($jsinfo)), true, $module);
e3b82acd 292
293 // Show configuration details only if not preset (when new) or user has the capabilities to do so (when editing).
294 $ctxcourse = context_course::instance($COURSE->id);
295 if (optional_param('update', 0, PARAM_INT)) {
296 $listtypes = has_capability('mod/lti:addgloballypreconfigedtoolinstance', $ctxcourse);
297 $listoptions = has_capability('mod/lti:adddefaultinstance', $ctxcourse);
298 } else {
299 $listtypes = !$typeid;
300 $listoptions = !$typeid && has_capability('mod/lti:adddefaultinstance', $ctxcourse);
301 }
302 if (!$listtypes) {
303 $mform->removeElement('typeid');
304 $mform->addElement('hidden', 'typeid', $typeid);
305 $mform->setType('typeid', PARAM_INT);
306 }
307 if (!$listoptions) {
308 $mform->removeElement('selectcontent');
309 $mform->removeElement('toolurl');
310 $mform->removeElement('securetoolurl');
311 $mform->removeElement('launchcontainer');
312 $mform->removeElement('resourcekey');
313 $mform->removeElement('password');
314 $mform->removeElement('instructorcustomparameters');
315 $mform->removeElement('icon');
316 $mform->removeElement('secureicon');
317 }
996b0fd9
CS
318 }
319
996b0fd9 320}