Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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