PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lti/mod_form.php

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