PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/lti/edit_form.php

http://github.com/moodle/moodle
PHP | 390 lines | 238 code | 56 blank | 96 comment | 25 complexity | f49b655957c5ed267087561d270b58ec 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 de main basiclti 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 Charles Severance
  45. * @author Chris Scribner
  46. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47. */
  48. defined('MOODLE_INTERNAL') || die;
  49. global $CFG;
  50. require_once($CFG->libdir.'/formslib.php');
  51. require_once($CFG->dirroot.'/mod/lti/locallib.php');
  52. /**
  53. * LTI Edit Form
  54. *
  55. * @package mod_lti
  56. * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
  57. * marc.alier@upc.edu
  58. * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  59. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  60. */
  61. class mod_lti_edit_types_form extends moodleform {
  62. /**
  63. * Define this form.
  64. */
  65. public function definition() {
  66. global $CFG, $PAGE;
  67. $mform =& $this->_form;
  68. $istool = $this->_customdata && isset($this->_customdata->istool) && $this->_customdata->istool;
  69. $typeid = $this->_customdata->id ?? '';
  70. $clientid = $this->_customdata->clientid ?? '';
  71. // Add basiclti elements.
  72. $mform->addElement('header', 'setup', get_string('tool_settings', 'lti'));
  73. $mform->addElement('text', 'lti_typename', get_string('typename', 'lti'));
  74. $mform->setType('lti_typename', PARAM_TEXT);
  75. $mform->addHelpButton('lti_typename', 'typename', 'lti');
  76. $mform->addRule('lti_typename', null, 'required', null, 'client');
  77. $mform->addElement('text', 'lti_toolurl', get_string('toolurl', 'lti'), array('size' => '64'));
  78. $mform->setType('lti_toolurl', PARAM_URL);
  79. $mform->addHelpButton('lti_toolurl', 'toolurl', 'lti');
  80. $mform->addElement('textarea', 'lti_description', get_string('tooldescription', 'lti'), array('rows' => 4, 'cols' => 60));
  81. $mform->setType('lti_description', PARAM_TEXT);
  82. $mform->addHelpButton('lti_description', 'tooldescription', 'lti');
  83. if (!$istool) {
  84. $mform->addRule('lti_toolurl', null, 'required', null, 'client');
  85. } else {
  86. $mform->disabledIf('lti_toolurl', null);
  87. }
  88. if (!$istool) {
  89. $options = array(
  90. LTI_VERSION_1 => get_string('oauthsecurity', 'lti'),
  91. LTI_VERSION_1P3 => get_string('jwtsecurity', 'lti'),
  92. );
  93. $mform->addElement('select', 'lti_ltiversion', get_string('ltiversion', 'lti'), $options);
  94. $mform->setType('lti_ltiversion', PARAM_TEXT);
  95. $mform->addHelpButton('lti_ltiversion', 'ltiversion', 'lti');
  96. $mform->setDefault('lti_ltiversion', LTI_VERSION_1);
  97. $mform->addElement('text', 'lti_resourcekey', get_string('resourcekey_admin', 'lti'));
  98. $mform->setType('lti_resourcekey', PARAM_TEXT);
  99. $mform->addHelpButton('lti_resourcekey', 'resourcekey_admin', 'lti');
  100. $mform->hideIf('lti_resourcekey', 'lti_ltiversion', 'eq', LTI_VERSION_1P3);
  101. $mform->setForceLtr('lti_resourcekey');
  102. $mform->addElement('passwordunmask', 'lti_password', get_string('password_admin', 'lti'));
  103. $mform->setType('lti_password', PARAM_TEXT);
  104. $mform->addHelpButton('lti_password', 'password_admin', 'lti');
  105. $mform->hideIf('lti_password', 'lti_ltiversion', 'eq', LTI_VERSION_1P3);
  106. if (!empty($typeid)) {
  107. $mform->addElement('text', 'lti_clientid_disabled', get_string('clientidadmin', 'lti'));
  108. $mform->setType('lti_clientid_disabled', PARAM_TEXT);
  109. $mform->addHelpButton('lti_clientid_disabled', 'clientidadmin', 'lti');
  110. $mform->hideIf('lti_clientid_disabled', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  111. $mform->disabledIf('lti_clientid_disabled', null);
  112. $mform->setForceLtr('lti_clientid_disabled');
  113. $mform->addElement('hidden', 'lti_clientid');
  114. $mform->setType('lti_clientid', PARAM_TEXT);
  115. }
  116. $keyoptions = [
  117. LTI_RSA_KEY => get_string('keytype_rsa', 'lti'),
  118. LTI_JWK_KEYSET => get_string('keytype_keyset', 'lti'),
  119. ];
  120. $mform->addElement('select', 'lti_keytype', get_string('keytype', 'lti'), $keyoptions);
  121. $mform->setType('lti_keytype', PARAM_TEXT);
  122. $mform->addHelpButton('lti_keytype', 'keytype', 'lti');
  123. $mform->setDefault('lti_keytype', LTI_JWK_KEYSET);
  124. $mform->hideIf('lti_keytype', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  125. $mform->addElement('textarea', 'lti_publickey', get_string('publickey', 'lti'), ['rows' => 8, 'cols' => 60]);
  126. $mform->setType('lti_publickey', PARAM_TEXT);
  127. $mform->addHelpButton('lti_publickey', 'publickey', 'lti');
  128. $mform->hideIf('lti_publickey', 'lti_keytype', 'neq', LTI_RSA_KEY);
  129. $mform->hideIf('lti_publickey', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  130. $mform->setForceLtr('lti_publickey');
  131. $mform->addElement('text', 'lti_publickeyset', get_string('publickeyset', 'lti'), ['size' => '64']);
  132. $mform->setType('lti_publickeyset', PARAM_TEXT);
  133. $mform->addHelpButton('lti_publickeyset', 'publickeyset', 'lti');
  134. $mform->hideIf('lti_publickeyset', 'lti_keytype', 'neq', LTI_JWK_KEYSET);
  135. $mform->hideIf('lti_publickeyset', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  136. $mform->setForceLtr('lti_publickeyset');
  137. $mform->addElement('text', 'lti_initiatelogin', get_string('initiatelogin', 'lti'), array('size' => '64'));
  138. $mform->setType('lti_initiatelogin', PARAM_URL);
  139. $mform->addHelpButton('lti_initiatelogin', 'initiatelogin', 'lti');
  140. $mform->hideIf('lti_initiatelogin', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  141. $mform->addElement('textarea', 'lti_redirectionuris', get_string('redirectionuris', 'lti'),
  142. array('rows' => 3, 'cols' => 60));
  143. $mform->setType('lti_redirectionuris', PARAM_TEXT);
  144. $mform->addHelpButton('lti_redirectionuris', 'redirectionuris', 'lti');
  145. $mform->hideIf('lti_redirectionuris', 'lti_ltiversion', 'neq', LTI_VERSION_1P3);
  146. $mform->setForceLtr('lti_redirectionuris');
  147. }
  148. if ($istool) {
  149. $mform->addElement('textarea', 'lti_parameters', get_string('parameter', 'lti'), array('rows' => 4, 'cols' => 60));
  150. $mform->setType('lti_parameters', PARAM_TEXT);
  151. $mform->addHelpButton('lti_parameters', 'parameter', 'lti');
  152. $mform->disabledIf('lti_parameters', null);
  153. $mform->setForceLtr('lti_parameters');
  154. }
  155. $mform->addElement('textarea', 'lti_customparameters', get_string('custom', 'lti'), array('rows' => 4, 'cols' => 60));
  156. $mform->setType('lti_customparameters', PARAM_TEXT);
  157. $mform->addHelpButton('lti_customparameters', 'custom', 'lti');
  158. $mform->setForceLtr('lti_customparameters');
  159. if (!empty($this->_customdata->isadmin)) {
  160. $options = array(
  161. LTI_COURSEVISIBLE_NO => get_string('show_in_course_no', 'lti'),
  162. LTI_COURSEVISIBLE_PRECONFIGURED => get_string('show_in_course_preconfigured', 'lti'),
  163. LTI_COURSEVISIBLE_ACTIVITYCHOOSER => get_string('show_in_course_activity_chooser', 'lti'),
  164. );
  165. if ($istool) {
  166. // LTI2 tools can not be matched by URL, they have to be either in preconfigured tools or in activity chooser.
  167. unset($options[LTI_COURSEVISIBLE_NO]);
  168. $stringname = 'show_in_course_lti2';
  169. } else {
  170. $stringname = 'show_in_course_lti1';
  171. }
  172. $mform->addElement('select', 'lti_coursevisible', get_string($stringname, 'lti'), $options);
  173. $mform->addHelpButton('lti_coursevisible', $stringname, 'lti');
  174. $mform->setDefault('lti_coursevisible', '1');
  175. } else {
  176. $mform->addElement('hidden', 'lti_coursevisible', LTI_COURSEVISIBLE_PRECONFIGURED);
  177. }
  178. $mform->setType('lti_coursevisible', PARAM_INT);
  179. $mform->addElement('hidden', 'typeid');
  180. $mform->setType('typeid', PARAM_INT);
  181. $launchoptions = array();
  182. $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');
  183. $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');
  184. $launchoptions[LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW] = get_string('existing_window', 'lti');
  185. $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');
  186. $mform->addElement('select', 'lti_launchcontainer', get_string('default_launch_container', 'lti'), $launchoptions);
  187. $mform->setDefault('lti_launchcontainer', LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS);
  188. $mform->addHelpButton('lti_launchcontainer', 'default_launch_container', 'lti');
  189. $mform->setType('lti_launchcontainer', PARAM_INT);
  190. $mform->addElement('advcheckbox', 'lti_contentitem', get_string('contentitem', 'lti'));
  191. $mform->addHelpButton('lti_contentitem', 'contentitem', 'lti');
  192. $mform->setAdvanced('lti_contentitem');
  193. if ($istool) {
  194. $mform->disabledIf('lti_contentitem', null);
  195. }
  196. $mform->addElement('text', 'lti_toolurl_ContentItemSelectionRequest',
  197. get_string('toolurl_contentitemselectionrequest', 'lti'), array('size' => '64'));
  198. $mform->setType('lti_toolurl_ContentItemSelectionRequest', PARAM_URL);
  199. $mform->setAdvanced('lti_toolurl_ContentItemSelectionRequest');
  200. $mform->addHelpButton('lti_toolurl_ContentItemSelectionRequest', 'toolurl_contentitemselectionrequest', 'lti');
  201. $mform->disabledIf('lti_toolurl_ContentItemSelectionRequest', 'lti_contentitem', 'notchecked');
  202. if ($istool) {
  203. $mform->disabledIf('lti_toolurl__ContentItemSelectionRequest', null);
  204. }
  205. $mform->addElement('hidden', 'oldicon');
  206. $mform->setType('oldicon', PARAM_URL);
  207. $mform->addElement('text', 'lti_icon', get_string('icon_url', 'lti'), array('size' => '64'));
  208. $mform->setType('lti_icon', PARAM_URL);
  209. $mform->setAdvanced('lti_icon');
  210. $mform->addHelpButton('lti_icon', 'icon_url', 'lti');
  211. $mform->addElement('text', 'lti_secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
  212. $mform->setType('lti_secureicon', PARAM_URL);
  213. $mform->setAdvanced('lti_secureicon');
  214. $mform->addHelpButton('lti_secureicon', 'secure_icon_url', 'lti');
  215. if (!$istool) {
  216. // Display the lti advantage services.
  217. $this->get_lti_advantage_services($mform);
  218. }
  219. if (!$istool) {
  220. // Add privacy preferences fieldset where users choose whether to send their data.
  221. $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
  222. $options = array();
  223. $options[0] = get_string('never', 'lti');
  224. $options[1] = get_string('always', 'lti');
  225. $options[2] = get_string('delegate', 'lti');
  226. $mform->addElement('select', 'lti_sendname', get_string('share_name_admin', 'lti'), $options);
  227. $mform->setType('lti_sendname', PARAM_INT);
  228. $mform->setDefault('lti_sendname', '2');
  229. $mform->addHelpButton('lti_sendname', 'share_name_admin', 'lti');
  230. $mform->addElement('select', 'lti_sendemailaddr', get_string('share_email_admin', 'lti'), $options);
  231. $mform->setType('lti_sendemailaddr', PARAM_INT);
  232. $mform->setDefault('lti_sendemailaddr', '2');
  233. $mform->addHelpButton('lti_sendemailaddr', 'share_email_admin', 'lti');
  234. // LTI Extensions.
  235. // Add grading preferences fieldset where the tool is allowed to return grades.
  236. $mform->addElement('select', 'lti_acceptgrades', get_string('accept_grades_admin', 'lti'), $options);
  237. $mform->setType('lti_acceptgrades', PARAM_INT);
  238. $mform->setDefault('lti_acceptgrades', '2');
  239. $mform->addHelpButton('lti_acceptgrades', 'accept_grades_admin', 'lti');
  240. $mform->addElement('checkbox', 'lti_forcessl', '&nbsp;', ' ' . get_string('force_ssl', 'lti'), $options);
  241. $mform->setType('lti_forcessl', PARAM_BOOL);
  242. if (!empty($CFG->mod_lti_forcessl)) {
  243. $mform->setDefault('lti_forcessl', '1');
  244. $mform->freeze('lti_forcessl');
  245. } else {
  246. $mform->setDefault('lti_forcessl', '0');
  247. }
  248. $mform->addHelpButton('lti_forcessl', 'force_ssl', 'lti');
  249. if (!empty($this->_customdata->isadmin)) {
  250. // Add setup parameters fieldset.
  251. $mform->addElement('header', 'setupoptions', get_string('miscellaneous', 'lti'));
  252. $options = array(
  253. LTI_DEFAULT_ORGID_SITEID => get_string('siteid', 'lti'),
  254. LTI_DEFAULT_ORGID_SITEHOST => get_string('sitehost', 'lti'),
  255. );
  256. $mform->addElement('select', 'lti_organizationid_default', get_string('organizationid_default', 'lti'), $options);
  257. $mform->setType('lti_organizationid_default', PARAM_TEXT);
  258. $mform->setDefault('lti_organizationid_default', LTI_DEFAULT_ORGID_SITEID);
  259. $mform->addHelpButton('lti_organizationid_default', 'organizationid_default', 'lti');
  260. $mform->addElement('text', 'lti_organizationid', get_string('organizationidguid', 'lti'));
  261. $mform->setType('lti_organizationid', PARAM_TEXT);
  262. $mform->addHelpButton('lti_organizationid', 'organizationidguid', 'lti');
  263. $mform->addElement('text', 'lti_organizationurl', get_string('organizationurl', 'lti'));
  264. $mform->setType('lti_organizationurl', PARAM_URL);
  265. $mform->addHelpButton('lti_organizationurl', 'organizationurl', 'lti');
  266. }
  267. }
  268. /* Suppress this for now - Chuck
  269. * mform->addElement('text', 'lti_organizationdescr', get_string('organizationdescr', 'lti'))
  270. * mform->setType('lti_organizationdescr', PARAM_TEXT)
  271. * mform->addHelpButton('lti_organizationdescr', 'organizationdescr', 'lti')
  272. */
  273. /*
  274. // Add a hidden element to signal a tool fixing operation after a problematic backup - restore process
  275. //$mform->addElement('hidden', 'lti_fix');
  276. */
  277. $tab = optional_param('tab', '', PARAM_ALPHAEXT);
  278. $mform->addElement('hidden', 'tab', $tab);
  279. $mform->setType('tab', PARAM_ALPHAEXT);
  280. $courseid = optional_param('course', 1, PARAM_INT);
  281. $mform->addElement('hidden', 'course', $courseid);
  282. $mform->setType('course', PARAM_INT);
  283. // Add standard buttons, common to all modules.
  284. $this->add_action_buttons();
  285. }
  286. /**
  287. * Retrieves the data of the submitted form.
  288. *
  289. * @return stdClass
  290. */
  291. public function get_data() {
  292. $data = parent::get_data();
  293. if ($data && !empty($this->_customdata->istool)) {
  294. // Content item checkbox is disabled in tool settings, so this cannot be edited. Just unset it.
  295. unset($data->lti_contentitem);
  296. }
  297. return $data;
  298. }
  299. /**
  300. * Generates the lti advantage extra configuration adding it to the mform
  301. *
  302. * @param MoodleQuickForm $mform
  303. */
  304. public function get_lti_advantage_services(&$mform) {
  305. // For each service add the label and get the array of configuration.
  306. $services = lti_get_services();
  307. $mform->addElement('header', 'services', get_string('services', 'lti'));
  308. foreach ($services as $service) {
  309. /** @var \mod_lti\local\ltiservice\service_base $service */
  310. $service->get_configuration_options($mform);
  311. }
  312. }
  313. /**
  314. * Validate the form data before we allow them to save the tool type.
  315. *
  316. * @param array $data
  317. * @param array $files
  318. * @return array Error messages
  319. */
  320. public function validation($data, $files) {
  321. global $CFG;
  322. $errors = parent::validation($data, $files);
  323. // LTI2 tools do not contain a ltiversion field.
  324. if (isset($data['lti_ltiversion']) && $data['lti_ltiversion'] == LTI_VERSION_1P3) {
  325. require_once($CFG->dirroot . '/mod/lti/upgradelib.php');
  326. $warning = mod_lti_verify_private_key();
  327. if (!empty($warning)) {
  328. $errors['lti_ltiversion'] = $warning;
  329. return $errors;
  330. }
  331. }
  332. return $errors;
  333. }
  334. }