PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lti/mod_form.php

http://github.com/moodle/moodle
PHP | 351 lines | 243 code | 42 blank | 66 comment | 37 complexity | 6d7dfe44cd5e52473088bbe98c7cb357 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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('header', 'general', get_string('general', 'form'));
  74. // Adding the standard "name" field.
  75. $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), array('size' => '64'));
  76. $mform->setType('name', PARAM_TEXT);
  77. $mform->addRule('name', null, 'required', null, 'client');
  78. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  79. // Adding the optional "intro" and "introformat" pair of fields.
  80. $this->standard_intro_elements(get_string('basicltiintro', 'lti'));
  81. $mform->setAdvanced('introeditor');
  82. // Display the label to the right of the checkbox so it looks better & matches rest of the form.
  83. if ($mform->elementExists('showdescription')) {
  84. $coursedesc = $mform->getElement('showdescription');
  85. if (!empty($coursedesc)) {
  86. $coursedesc->setText(' ' . $coursedesc->getLabel());
  87. $coursedesc->setLabel('&nbsp');
  88. }
  89. }
  90. $mform->setAdvanced('showdescription');
  91. $mform->addElement('checkbox', 'showtitlelaunch', '&nbsp;', ' ' . get_string('display_name', 'lti'));
  92. $mform->setAdvanced('showtitlelaunch');
  93. $mform->setDefault('showtitlelaunch', true);
  94. $mform->addHelpButton('showtitlelaunch', 'display_name', 'lti');
  95. $mform->addElement('checkbox', 'showdescriptionlaunch', '&nbsp;', ' ' . get_string('display_description', 'lti'));
  96. $mform->setAdvanced('showdescriptionlaunch');
  97. $mform->addHelpButton('showdescriptionlaunch', 'display_description', 'lti');
  98. // Tool settings.
  99. $toolproxy = array();
  100. // Array of tool type IDs that don't support ContentItemSelectionRequest.
  101. $noncontentitemtypes = [];
  102. if ($showtypes) {
  103. $tooltypes = $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'));
  104. if ($typeid) {
  105. $mform->getElement('typeid')->setValue($typeid);
  106. }
  107. $mform->addHelpButton('typeid', 'external_tool_type', 'lti');
  108. foreach (lti_get_types_for_add_instance() as $id => $type) {
  109. if (!empty($type->toolproxyid)) {
  110. $toolproxy[] = $type->id;
  111. $attributes = array('globalTool' => 1, 'toolproxy' => 1);
  112. $enabledcapabilities = explode("\n", $type->enabledcapability);
  113. if (!in_array('Result.autocreate', $enabledcapabilities) ||
  114. in_array('BasicOutcome.url', $enabledcapabilities)) {
  115. $attributes['nogrades'] = 1;
  116. }
  117. if (!in_array('Person.name.full', $enabledcapabilities) &&
  118. !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) {
  126. $attributes = array('editable' => 1, 'courseTool' => 1, 'domain' => $type->tooldomain);
  127. } else if ($id != 0) {
  128. $attributes = array('globalTool' => 1, 'domain' => $type->tooldomain);
  129. } else {
  130. $attributes = array();
  131. }
  132. if ($id) {
  133. $config = lti_get_type_config($id);
  134. if (!empty($config['contentitem'])) {
  135. $attributes['data-contentitem'] = 1;
  136. $attributes['data-id'] = $id;
  137. } else {
  138. $noncontentitemtypes[] = $id;
  139. }
  140. }
  141. $tooltypes->addOption($type->name, $id, $attributes);
  142. }
  143. } else {
  144. $mform->addElement('hidden', 'typeid', $typeid);
  145. $mform->setType('typeid', PARAM_INT);
  146. if ($typeid) {
  147. $config = lti_get_type_config($typeid);
  148. if (!empty($config['contentitem'])) {
  149. $mform->addElement('hidden', 'contentitem', 1);
  150. $mform->setType('contentitem', PARAM_INT);
  151. }
  152. }
  153. }
  154. // Add button that launches the content-item selection dialogue.
  155. // Set contentitem URL.
  156. $contentitemurl = new moodle_url('/mod/lti/contentitem.php');
  157. $contentbuttonattributes = [
  158. 'data-contentitemurl' => $contentitemurl->out(false)
  159. ];
  160. if (!$showtypes) {
  161. if (!$typeid || empty(lti_get_type_config($typeid)['contentitem'])) {
  162. $contentbuttonattributes['disabled'] = 'disabled';
  163. }
  164. }
  165. $contentbuttonlabel = get_string('selectcontent', 'lti');
  166. $contentbutton = $mform->addElement('button', 'selectcontent', $contentbuttonlabel, $contentbuttonattributes);
  167. // Disable select content button if the selected tool doesn't support content item or it's set to Automatic.
  168. if ($showtypes) {
  169. $allnoncontentitemtypes = $noncontentitemtypes;
  170. $allnoncontentitemtypes[] = '0'; // Add option value for "Automatic, based on tool URL".
  171. $mform->disabledIf('selectcontent', 'typeid', 'in', $allnoncontentitemtypes);
  172. }
  173. if ($showoptions) {
  174. $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), array('size' => '64'));
  175. $mform->setType('toolurl', PARAM_URL);
  176. $mform->addHelpButton('toolurl', 'launch_url', 'lti');
  177. $mform->hideIf('toolurl', 'typeid', 'in', $noncontentitemtypes);
  178. $mform->addElement('text', 'securetoolurl', get_string('secure_launch_url', 'lti'), array('size' => '64'));
  179. $mform->setType('securetoolurl', PARAM_URL);
  180. $mform->setAdvanced('securetoolurl');
  181. $mform->addHelpButton('securetoolurl', 'secure_launch_url', 'lti');
  182. $mform->hideIf('securetoolurl', 'typeid', 'in', $noncontentitemtypes);
  183. } else {
  184. // We still need those on page to support deep linking return, but hidden to avoid instructor modification.
  185. $mform->addElement('hidden', 'toolurl', '', array('id' => 'id_toolurl'));
  186. $mform->setType('toolurl', PARAM_URL);
  187. $mform->addElement('hidden', 'securetoolurl', '', array('id' => 'id_securetoolurl'));
  188. $mform->setType('securetoolurl', PARAM_URL);
  189. }
  190. $mform->addElement('hidden', 'urlmatchedtypeid', '', array('id' => 'id_urlmatchedtypeid'));
  191. $mform->setType('urlmatchedtypeid', PARAM_INT);
  192. $launchoptions = array();
  193. $launchoptions[LTI_LAUNCH_CONTAINER_DEFAULT] = get_string('default', 'lti');
  194. $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');
  195. $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');
  196. $launchoptions[LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW] = get_string('existing_window', 'lti');
  197. $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');
  198. $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);
  199. $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);
  200. $mform->addHelpButton('launchcontainer', 'launchinpopup', 'lti');
  201. $mform->setAdvanced('launchcontainer');
  202. if ($showoptions) {
  203. $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));
  204. $mform->setType('resourcekey', PARAM_TEXT);
  205. $mform->setAdvanced('resourcekey');
  206. $mform->addHelpButton('resourcekey', 'resourcekey', 'lti');
  207. $mform->setForceLtr('resourcekey');
  208. $mform->hideIf('resourcekey', 'typeid', 'in', $noncontentitemtypes);
  209. $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));
  210. $mform->setType('password', PARAM_TEXT);
  211. $mform->setAdvanced('password');
  212. $mform->addHelpButton('password', 'password', 'lti');
  213. $mform->hideIf('password', 'typeid', 'in', $noncontentitemtypes);
  214. $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), array('rows' => 4, 'cols' => 60));
  215. $mform->setType('instructorcustomparameters', PARAM_TEXT);
  216. $mform->setAdvanced('instructorcustomparameters');
  217. $mform->addHelpButton('instructorcustomparameters', 'custom', 'lti');
  218. $mform->setForceLtr('instructorcustomparameters');
  219. $mform->addElement('text', 'icon', get_string('icon_url', 'lti'), array('size' => '64'));
  220. $mform->setType('icon', PARAM_URL);
  221. $mform->setAdvanced('icon');
  222. $mform->addHelpButton('icon', 'icon_url', 'lti');
  223. $mform->hideIf('icon', 'typeid', 'in', $noncontentitemtypes);
  224. $mform->addElement('text', 'secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
  225. $mform->setType('secureicon', PARAM_URL);
  226. $mform->setAdvanced('secureicon');
  227. $mform->addHelpButton('secureicon', 'secure_icon_url', 'lti');
  228. $mform->hideIf('secureicon', 'typeid', 'in', $noncontentitemtypes);
  229. } else {
  230. // Keep those in the form to allow deep linking.
  231. $mform->addElement('hidden', 'resourcekey', '', array('id' => 'id_resourcekey'));
  232. $mform->setType('resourcekey', PARAM_TEXT);
  233. $mform->addElement('hidden', 'password', '', array('id' => 'id_password'));
  234. $mform->setType('password', PARAM_TEXT);
  235. $mform->addElement('hidden', 'instructorcustomparameters', '', array('id' => 'id_instructorcustomparameters'));
  236. $mform->setType('instructorcustomparameters', PARAM_TEXT);
  237. $mform->addElement('hidden', 'icon', '', array('id' => 'id_icon'));
  238. $mform->setType('icon', PARAM_URL);
  239. $mform->addElement('hidden', 'secureicon', '', array('id' => 'id_secureicon'));
  240. $mform->setType('secureicon', PARAM_URL);
  241. }
  242. // Add privacy preferences fieldset where users choose whether to send their data.
  243. $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
  244. $mform->addElement('advcheckbox', 'instructorchoicesendname', '&nbsp;', ' ' . get_string('share_name', 'lti'));
  245. $mform->setDefault('instructorchoicesendname', '1');
  246. $mform->addHelpButton('instructorchoicesendname', 'share_name', 'lti');
  247. $mform->disabledIf('instructorchoicesendname', 'typeid', 'in', $toolproxy);
  248. $mform->addElement('advcheckbox', 'instructorchoicesendemailaddr', '&nbsp;', ' ' . get_string('share_email', 'lti'));
  249. $mform->setDefault('instructorchoicesendemailaddr', '1');
  250. $mform->addHelpButton('instructorchoicesendemailaddr', 'share_email', 'lti');
  251. $mform->disabledIf('instructorchoicesendemailaddr', 'typeid', 'in', $toolproxy);
  252. $mform->addElement('advcheckbox', 'instructorchoiceacceptgrades', '&nbsp;', ' ' . get_string('accept_grades', 'lti'));
  253. $mform->setDefault('instructorchoiceacceptgrades', '1');
  254. $mform->addHelpButton('instructorchoiceacceptgrades', 'accept_grades', 'lti');
  255. $mform->disabledIf('instructorchoiceacceptgrades', 'typeid', 'in', $toolproxy);
  256. // Add standard course module grading elements.
  257. $this->standard_grading_coursemodule_elements();
  258. // Add standard elements, common to all modules.
  259. $this->standard_coursemodule_elements();
  260. $mform->setAdvanced('cmidnumber');
  261. // Add standard buttons, common to all modules.
  262. $this->add_action_buttons();
  263. $editurl = new moodle_url('/mod/lti/instructor_edit_tool_type.php',
  264. array('sesskey' => sesskey(), 'course' => $COURSE->id));
  265. $ajaxurl = new moodle_url('/mod/lti/ajax.php');
  266. // All these icon uses are incorrect. LTI JS needs updating to use AMD modules and templates so it can use
  267. // the mustache pix helper - until then LTI will have inconsistent icons.
  268. $jsinfo = (object)array(
  269. 'edit_icon_url' => (string)$OUTPUT->image_url('t/edit'),
  270. 'add_icon_url' => (string)$OUTPUT->image_url('t/add'),
  271. 'delete_icon_url' => (string)$OUTPUT->image_url('t/delete'),
  272. 'green_check_icon_url' => (string)$OUTPUT->image_url('i/valid'),
  273. 'warning_icon_url' => (string)$OUTPUT->image_url('warning', 'lti'),
  274. 'instructor_tool_type_edit_url' => $editurl->out(false),
  275. 'ajax_url' => $ajaxurl->out(true),
  276. 'courseId' => $COURSE->id
  277. );
  278. $module = array(
  279. 'name' => 'mod_lti_edit',
  280. 'fullpath' => '/mod/lti/mod_form.js',
  281. 'requires' => array('base', 'io', 'querystring-stringify-simple', 'node', 'event', 'json-parse'),
  282. 'strings' => array(
  283. array('addtype', 'lti'),
  284. array('edittype', 'lti'),
  285. array('deletetype', 'lti'),
  286. array('delete_confirmation', 'lti'),
  287. array('cannot_edit', 'lti'),
  288. array('cannot_delete', 'lti'),
  289. array('global_tool_types', 'lti'),
  290. array('course_tool_types', 'lti'),
  291. array('using_tool_configuration', 'lti'),
  292. array('using_tool_cartridge', 'lti'),
  293. array('domain_mismatch', 'lti'),
  294. array('custom_config', 'lti'),
  295. array('tool_config_not_found', 'lti'),
  296. array('tooltypeadded', 'lti'),
  297. array('tooltypedeleted', 'lti'),
  298. array('tooltypenotdeleted', 'lti'),
  299. array('tooltypeupdated', 'lti'),
  300. array('forced_help', 'lti')
  301. ),
  302. );
  303. if (!empty($typeid)) {
  304. $mform->setAdvanced('typeid');
  305. $mform->setAdvanced('toolurl');
  306. }
  307. $PAGE->requires->js_init_call('M.mod_lti.editor.init', array(json_encode($jsinfo)), true, $module);
  308. }
  309. }