MDL-78251 tiny: Reduce use of UI for setting test expectations
[moodle.git] / mod / lti / mod_form.php
blob3984a97bbeee4d80440b167180607ffc77adf487
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.
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/>.
17 // This file is part of BasicLTI4Moodle
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.
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.
35 /**
36 * This file defines the main lti configuration form
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
49 defined('MOODLE_INTERNAL') || die;
51 require_once($CFG->dirroot.'/course/moodleform_mod.php');
52 require_once($CFG->dirroot.'/mod/lti/locallib.php');
54 class mod_lti_mod_form extends moodleform_mod {
56 public function definition() {
57 global $PAGE, $OUTPUT, $COURSE;
59 if ($type = optional_param('type', false, PARAM_ALPHA)) {
60 component_callback("ltisource_$type", 'add_instance_hook');
63 // Type ID parameter being passed when adding an preconfigured tool from activity chooser.
64 $typeid = optional_param('typeid', false, PARAM_INT);
66 $showoptions = has_capability('mod/lti:addmanualinstance', $this->context);
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);
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;
75 } else {
76 $showtypes = !$typeid;
79 $mform =& $this->_form;
81 // Adding the "general" fieldset, where all the common settings are shown.
82 $mform->addElement('html', "<div data-attribute='dynamic-import' hidden aria-hidden='true' role='alert'></div>");
83 $mform->addElement('header', 'general', get_string('general', 'form'));
85 // Adding the standard "name" field.
86 $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), array('size' => '64'));
87 $mform->setType('name', PARAM_TEXT);
88 $mform->addRule('name', null, 'required', null, 'client');
89 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
90 // Adding the optional "intro" and "introformat" pair of fields.
91 $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
92 $mform->setAdvanced('introeditor');
94 // Display the label to the right of the checkbox so it looks better & matches rest of the form.
95 if ($mform->elementExists('showdescription')) {
96 $coursedesc = $mform->getElement('showdescription');
97 if (!empty($coursedesc)) {
98 $coursedesc->setText(' ' . $coursedesc->getLabel());
99 $coursedesc->setLabel('&nbsp');
103 $mform->setAdvanced('showdescription');
105 $mform->addElement('checkbox', 'showtitlelaunch', get_string('display_name', 'lti'));
106 $mform->setAdvanced('showtitlelaunch');
107 $mform->setDefault('showtitlelaunch', true);
108 $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
110 $mform->addElement('checkbox', 'showdescriptionlaunch', get_string('display_description', 'lti'));
111 $mform->setAdvanced('showdescriptionlaunch');
112 $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
114 // Tool settings.
115 $toolproxy = array();
116 // Array of tool type IDs that don't support ContentItemSelectionRequest.
117 $noncontentitemtypes = [];
119 if ($showtypes) {
120 $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'));
121 if ($typeid) {
122 $mform->getElement('typeid')->setValue($typeid);
124 $mform->addHelpButton('typeid', 'external_tool_type', 'lti');
126 foreach (lti_get_types_for_add_instance() as $id => $type) {
127 if (!empty($type->toolproxyid)) {
128 $toolproxy[] = $type->id;
129 $attributes = array('globalTool' => 1, 'toolproxy' => 1);
130 $enabledcapabilities = explode("\n", $type->enabledcapability);
131 if (!in_array('Result.autocreate', $enabledcapabilities) ||
132 in_array('BasicOutcome.url', $enabledcapabilities)) {
133 $attributes['nogrades'] = 1;
135 if (!in_array('Person.name.full', $enabledcapabilities) &&
136 !in_array('Person.name.family', $enabledcapabilities) &&
137 !in_array('Person.name.given', $enabledcapabilities)) {
138 $attributes['noname'] = 1;
140 if (!in_array('Person.email.primary', $enabledcapabilities)) {
141 $attributes['noemail'] = 1;
143 } else if ($type->course == $COURSE->id) {
144 $attributes = array('editable' => 1, 'courseTool' => 1, 'domain' => $type->tooldomain);
145 } else if ($id != 0) {
146 $attributes = array('globalTool' => 1, 'domain' => $type->tooldomain);
147 } else {
148 $attributes = array();
151 if ($id) {
152 $config = lti_get_type_config($id);
153 if (!empty($config['contentitem'])) {
154 $attributes['data-contentitem'] = 1;
155 $attributes['data-id'] = $id;
156 } else {
157 $noncontentitemtypes[] = $id;
160 $tooltypes->addOption($type->name, $id, $attributes);
162 } else {
163 $mform->addElement('hidden', 'typeid', $typeid);
164 $mform->setType('typeid', PARAM_INT);
165 if ($typeid) {
166 $config = lti_get_type_config($typeid);
167 if (!empty($config['contentitem'])) {
168 $mform->addElement('hidden', 'contentitem', 1);
169 $mform->setType('contentitem', PARAM_INT);
174 // Add button that launches the content-item selection dialogue.
175 // Set contentitem URL.
176 $contentitemurl = new moodle_url('/mod/lti/contentitem.php');
177 $contentbuttonattributes = [
178 'data-contentitemurl' => $contentitemurl->out(false)
180 if (!$showtypes) {
181 if (!$typeid || empty(lti_get_type_config($typeid)['contentitem'])) {
182 $contentbuttonattributes['disabled'] = 'disabled';
185 $contentbuttonlabel = get_string('selectcontent', 'lti');
186 $contentbutton = $mform->addElement('button', 'selectcontent', $contentbuttonlabel, $contentbuttonattributes);
187 // Disable select content button if the selected tool doesn't support content item or it's set to Automatic.
188 if ($showtypes) {
189 $allnoncontentitemtypes = $noncontentitemtypes;
190 $allnoncontentitemtypes[] = '0'; // Add option value for "Automatic, based on tool URL".
191 $mform->disabledIf('selectcontent', 'typeid', 'in', $allnoncontentitemtypes);
194 if ($showoptions) {
195 $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), array('size' => '64'));
196 $mform->setType('toolurl', PARAM_URL);
197 $mform->addHelpButton('toolurl', 'launch_url', 'lti');
198 $mform->hideIf('toolurl', 'typeid', 'in', $noncontentitemtypes);
200 $mform->addElement('text', 'securetoolurl', get_string('secure_launch_url', 'lti'), array('size' => '64'));
201 $mform->setType('securetoolurl', PARAM_URL);
202 $mform->setAdvanced('securetoolurl');
203 $mform->addHelpButton('securetoolurl', 'secure_launch_url', 'lti');
204 $mform->hideIf('securetoolurl', 'typeid', 'in', $noncontentitemtypes);
205 } else {
206 // We still need those on page to support deep linking return, but hidden to avoid instructor modification.
207 $mform->addElement('hidden', 'toolurl', '', array('id' => 'id_toolurl'));
208 $mform->setType('toolurl', PARAM_URL);
209 $mform->addElement('hidden', 'securetoolurl', '', array('id' => 'id_securetoolurl'));
210 $mform->setType('securetoolurl', PARAM_URL);
213 $mform->addElement('hidden', 'urlmatchedtypeid', '', array('id' => 'id_urlmatchedtypeid'));
214 $mform->setType('urlmatchedtypeid', PARAM_INT);
216 $mform->addElement('hidden', 'lineitemresourceid', '', array( 'id' => 'id_lineitemresourceid' ));
217 $mform->setType('lineitemresourceid', PARAM_TEXT);
219 $mform->addElement('hidden', 'lineitemtag', '', array( 'id' => 'id_lineitemtag'));
220 $mform->setType('lineitemtag', PARAM_TEXT);
222 $mform->addElement('hidden', 'lineitemsubreviewurl', '', array( 'id' => 'id_lineitemsubreviewurl'));
223 $mform->setType('lineitemsubreviewurl', PARAM_URL);
225 $mform->addElement('hidden', 'lineitemsubreviewparams', '', array( 'id' => 'id_lineitemsubreviewparams'));
226 $mform->setType('lineitemsubreviewparams', PARAM_TEXT);
228 $launchoptions = array();
229 $launchoptions[LTI_LAUNCH_CONTAINER_DEFAULT] = get_string('default', 'lti');
230 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');
231 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');
232 $launchoptions[LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW] = get_string('existing_window', 'lti');
233 $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');
235 $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);
236 $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);
237 $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti');
238 $mform->setAdvanced('launchcontainer');
240 if ($showoptions) {
241 $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));
242 $mform->setType('resourcekey', PARAM_TEXT);
243 $mform->setAdvanced('resourcekey');
244 $mform->addHelpButton('resourcekey', 'resourcekey', 'lti');
245 $mform->setForceLtr('resourcekey');
246 $mform->hideIf('resourcekey', 'typeid', 'in', $noncontentitemtypes);
248 $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));
249 $mform->setType('password', PARAM_TEXT);
250 $mform->setAdvanced('password');
251 $mform->addHelpButton('password', 'password', 'lti');
252 $mform->hideIf('password', 'typeid', 'in', $noncontentitemtypes);
254 $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), array('rows' => 4, 'cols' => 60));
255 $mform->setType('instructorcustomparameters', PARAM_TEXT);
256 $mform->setAdvanced('instructorcustomparameters');
257 $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
258 $mform->setForceLtr('instructorcustomparameters');
260 $mform->addElement('text', 'icon', get_string('icon_url', 'lti'), array('size' => '64'));
261 $mform->setType('icon', PARAM_URL);
262 $mform->setAdvanced('icon');
263 $mform->addHelpButton('icon', 'icon_url', 'lti');
264 $mform->hideIf('icon', 'typeid', 'in', $noncontentitemtypes);
266 $mform->addElement('text', 'secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
267 $mform->setType('secureicon', PARAM_URL);
268 $mform->setAdvanced('secureicon');
269 $mform->addHelpButton('secureicon', 'secure_icon_url', 'lti');
270 $mform->hideIf('secureicon', 'typeid', 'in', $noncontentitemtypes);
271 } else {
272 // Keep those in the form to allow deep linking.
273 $mform->addElement('hidden', 'resourcekey', '', array('id' => 'id_resourcekey'));
274 $mform->setType('resourcekey', PARAM_TEXT);
275 $mform->addElement('hidden', 'password', '', array('id' => 'id_password'));
276 $mform->setType('password', PARAM_TEXT);
277 $mform->addElement('hidden', 'instructorcustomparameters', '', array('id' => 'id_instructorcustomparameters'));
278 $mform->setType('instructorcustomparameters', PARAM_TEXT);
279 $mform->addElement('hidden', 'icon', '', array('id' => 'id_icon'));
280 $mform->setType('icon', PARAM_URL);
281 $mform->addElement('hidden', 'secureicon', '', array('id' => 'id_secureicon'));
282 $mform->setType('secureicon', PARAM_URL);
285 // Add privacy preferences fieldset where users choose whether to send their data.
286 $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
288 $mform->addElement('advcheckbox', 'instructorchoicesendname', get_string('share_name', 'lti'));
289 $mform->setDefault('instructorchoicesendname', '1');
290 $mform->addHelpButton('instructorchoicesendname', 'share_name', 'lti');
291 $mform->disabledIf('instructorchoicesendname', 'typeid', 'in', $toolproxy);
293 $mform->addElement('advcheckbox', 'instructorchoicesendemailaddr', get_string('share_email', 'lti'));
294 $mform->setDefault('instructorchoicesendemailaddr', '1');
295 $mform->addHelpButton('instructorchoicesendemailaddr', 'share_email', 'lti');
296 $mform->disabledIf('instructorchoicesendemailaddr', 'typeid', 'in', $toolproxy);
298 $mform->addElement('advcheckbox', 'instructorchoiceacceptgrades', get_string('accept_grades', 'lti'));
299 $mform->setDefault('instructorchoiceacceptgrades', '0');
300 $mform->addHelpButton('instructorchoiceacceptgrades', 'accept_grades', 'lti');
301 $mform->disabledIf('instructorchoiceacceptgrades', 'typeid', 'in', $toolproxy);
303 // Add standard course module grading elements.
304 $this->standard_grading_coursemodule_elements();
306 // Add standard elements, common to all modules.
307 $this->standard_coursemodule_elements();
308 $mform->setAdvanced('cmidnumber');
310 // Add standard buttons, common to all modules.
311 $this->add_action_buttons();
313 $editurl = new moodle_url('/mod/lti/instructor_edit_tool_type.php',
314 array('sesskey' => sesskey(), 'course' => $COURSE->id));
315 $ajaxurl = new moodle_url('/mod/lti/ajax.php');
317 // All these icon uses are incorrect. LTI JS needs updating to use AMD modules and templates so it can use
318 // the mustache pix helper - until then LTI will have inconsistent icons.
319 $jsinfo = (object)array(
320 'edit_icon_url' => (string)$OUTPUT->image_url('t/edit'),
321 'add_icon_url' => (string)$OUTPUT->image_url('t/add'),
322 'delete_icon_url' => (string)$OUTPUT->image_url('t/delete'),
323 'green_check_icon_url' => (string)$OUTPUT->image_url('i/valid'),
324 'warning_icon_url' => (string)$OUTPUT->image_url('warning', 'lti'),
325 'instructor_tool_type_edit_url' => $editurl->out(false),
326 'ajax_url' => $ajaxurl->out(true),
327 'courseId' => $COURSE->id
330 $module = array(
331 'name' => 'mod_lti_edit',
332 'fullpath' => '/mod/lti/mod_form.js',
333 'requires' => array('base', 'io', 'querystring-stringify-simple', 'node', 'event', 'json-parse'),
334 'strings' => array(
335 array('addtype', 'lti'),
336 array('edittype', 'lti'),
337 array('deletetype', 'lti'),
338 array('delete_confirmation', 'lti'),
339 array('cannot_edit', 'lti'),
340 array('cannot_delete', 'lti'),
341 array('global_tool_types', 'lti'),
342 array('course_tool_types', 'lti'),
343 array('using_tool_configuration', 'lti'),
344 array('using_tool_cartridge', 'lti'),
345 array('domain_mismatch', 'lti'),
346 array('custom_config', 'lti'),
347 array('tool_config_not_found', 'lti'),
348 array('tooltypeadded', 'lti'),
349 array('tooltypedeleted', 'lti'),
350 array('tooltypenotdeleted', 'lti'),
351 array('tooltypeupdated', 'lti'),
352 array('forced_help', 'lti')
356 if (!empty($typeid)) {
357 $mform->setAdvanced('typeid');
358 $mform->setAdvanced('toolurl');
361 $PAGE->requires->js_init_call('M.mod_lti.editor.init', array(json_encode($jsinfo)), true, $module);
365 * Sets the current values handled by services in case of update.
367 * @param object $defaultvalues default values to populate the form with.
369 public function set_data($defaultvalues) {
370 $services = lti_get_services();
371 if (is_object($defaultvalues)) {
372 foreach ($services as $service) {
373 $service->set_instance_form_values( $defaultvalues );
376 parent::set_data($defaultvalues);