Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
  • Differences Between: [Versions 37 and 311] [Versions 38 and 311] [Versions 39 and 311]

       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_deeplinking', 'lti'));
     218          $mform->addHelpButton('lti_contentitem', 'contentitem_deeplinking', 'lti');
     219          if ($istool) {
     220              $mform->disabledIf('lti_contentitem', null);
     221          }
     222  
     223          $mform->addElement('text', 'lti_toolurl_ContentItemSelectionRequest',
     224              get_string('toolurl_contentitemselectionrequest', 'lti'), array('size' => '64'));
     225          $mform->setType('lti_toolurl_ContentItemSelectionRequest', PARAM_URL);
     226          $mform->addHelpButton('lti_toolurl_ContentItemSelectionRequest', 'toolurl_contentitemselectionrequest', 'lti');
     227          $mform->disabledIf('lti_toolurl_ContentItemSelectionRequest', 'lti_contentitem', 'notchecked');
     228          if ($istool) {
     229              $mform->disabledIf('lti_toolurl__ContentItemSelectionRequest', null);
     230          }
     231  
     232          $mform->addElement('hidden', 'oldicon');
     233          $mform->setType('oldicon', PARAM_URL);
     234  
     235          $mform->addElement('text', 'lti_icon', get_string('icon_url', 'lti'), array('size' => '64'));
     236          $mform->setType('lti_icon', PARAM_URL);
     237          $mform->setAdvanced('lti_icon');
     238          $mform->addHelpButton('lti_icon', 'icon_url', 'lti');
     239  
     240          $mform->addElement('text', 'lti_secureicon', get_string('secure_icon_url', 'lti'), array('size' => '64'));
     241          $mform->setType('lti_secureicon', PARAM_URL);
     242          $mform->setAdvanced('lti_secureicon');
     243          $mform->addHelpButton('lti_secureicon', 'secure_icon_url', 'lti');
     244  
     245          if (!$istool) {
     246              // Display the lti advantage services.
     247              $this->get_lti_advantage_services($mform);
     248          }
     249  
     250          if (!$istool) {
     251              // Add privacy preferences fieldset where users choose whether to send their data.
     252              $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));
     253  
     254              $options = array();
     255              $options[0] = get_string('never', 'lti');
     256              $options[1] = get_string('always', 'lti');
     257              $options[2] = get_string('delegate', 'lti');
     258  
     259              $mform->addElement('select', 'lti_sendname', get_string('share_name_admin', 'lti'), $options);
     260              $mform->setType('lti_sendname', PARAM_INT);
     261              $mform->setDefault('lti_sendname', '2');
     262              $mform->addHelpButton('lti_sendname', 'share_name_admin', 'lti');
     263  
     264              $mform->addElement('select', 'lti_sendemailaddr', get_string('share_email_admin', 'lti'), $options);
     265              $mform->setType('lti_sendemailaddr', PARAM_INT);
     266              $mform->setDefault('lti_sendemailaddr', '2');
     267              $mform->addHelpButton('lti_sendemailaddr', 'share_email_admin', 'lti');
     268  
     269              // LTI Extensions.
     270  
     271              // Add grading preferences fieldset where the tool is allowed to return grades.
     272              $gradeoptions = array();
     273              $gradeoptions[] = get_string('never', 'lti');
     274              $gradeoptions[] = get_string('always', 'lti');
     275              $gradeoptions[] = get_string('delegate_tool', 'lti');
     276  
     277              $mform->addElement('select', 'lti_acceptgrades', get_string('accept_grades_admin', 'lti'), $gradeoptions);
     278              $mform->setType('lti_acceptgrades', PARAM_INT);
     279              $mform->setDefault('lti_acceptgrades', '2');
     280              $mform->addHelpButton('lti_acceptgrades', 'accept_grades_admin', 'lti');
     281  
     282              $mform->addElement('checkbox', 'lti_forcessl', get_string('force_ssl', 'lti'), '', $options);
     283              $mform->setType('lti_forcessl', PARAM_BOOL);
     284              if (!empty($CFG->mod_lti_forcessl)) {
     285                  $mform->setDefault('lti_forcessl', '1');
     286                  $mform->freeze('lti_forcessl');
     287              } else {
     288                  $mform->setDefault('lti_forcessl', '0');
     289              }
     290              $mform->addHelpButton('lti_forcessl', 'force_ssl', 'lti');
     291  
     292              if (!empty($this->_customdata->isadmin)) {
     293                  // Add setup parameters fieldset.
     294                  $mform->addElement('header', 'setupoptions', get_string('miscellaneous', 'lti'));
     295  
     296                  $options = array(
     297                      LTI_DEFAULT_ORGID_SITEID => get_string('siteid', 'lti'),
     298                      LTI_DEFAULT_ORGID_SITEHOST => get_string('sitehost', 'lti'),
     299                  );
     300  
     301                  $mform->addElement('select', 'lti_organizationid_default', get_string('organizationid_default', 'lti'), $options);
     302                  $mform->setType('lti_organizationid_default', PARAM_TEXT);
     303                  $mform->setDefault('lti_organizationid_default', LTI_DEFAULT_ORGID_SITEID);
     304                  $mform->addHelpButton('lti_organizationid_default', 'organizationid_default', 'lti');
     305  
     306                  $mform->addElement('text', 'lti_organizationid', get_string('organizationidguid', 'lti'));
     307                  $mform->setType('lti_organizationid', PARAM_TEXT);
     308                  $mform->addHelpButton('lti_organizationid', 'organizationidguid', 'lti');
     309  
     310                  $mform->addElement('text', 'lti_organizationurl', get_string('organizationurl', 'lti'));
     311                  $mform->setType('lti_organizationurl', PARAM_URL);
     312                  $mform->addHelpButton('lti_organizationurl', 'organizationurl', 'lti');
     313              }
     314          }
     315  
     316          /* Suppress this for now - Chuck
     317           * mform->addElement('text', 'lti_organizationdescr', get_string('organizationdescr', 'lti'))
     318           * mform->setType('lti_organizationdescr', PARAM_TEXT)
     319           * mform->addHelpButton('lti_organizationdescr', 'organizationdescr', 'lti')
     320           */
     321  
     322          /*
     323          // Add a hidden element to signal a tool fixing operation after a problematic backup - restore process
     324          //$mform->addElement('hidden', 'lti_fix');
     325          */
     326  
     327          $tab = optional_param('tab', '', PARAM_ALPHAEXT);
     328          $mform->addElement('hidden', 'tab', $tab);
     329          $mform->setType('tab', PARAM_ALPHAEXT);
     330  
     331          $courseid = optional_param('course', 1, PARAM_INT);
     332          $mform->addElement('hidden', 'course', $courseid);
     333          $mform->setType('course', PARAM_INT);
     334  
     335          // Add standard buttons, common to all modules.
     336          $this->add_action_buttons();
     337  
     338      }
     339  
     340      /**
     341       * Retrieves the data of the submitted form.
     342       *
     343       * @return stdClass
     344       */
     345      public function get_data() {
     346          $data = parent::get_data();
     347          if ($data && !empty($this->_customdata->istool)) {
     348              // Content item checkbox is disabled in tool settings, so this cannot be edited. Just unset it.
     349              unset($data->lti_contentitem);
     350          }
     351          return $data;
     352      }
     353  
     354      /**
     355       * Generates the lti advantage extra configuration adding it to the mform
     356       *
     357       * @param MoodleQuickForm $mform
     358       */
     359      public function get_lti_advantage_services(&$mform) {
     360          // For each service add the label and get the array of configuration.
     361          $services = lti_get_services();
     362          $mform->addElement('header', 'services', get_string('services', 'lti'));
     363          foreach ($services as $service) {
     364              /** @var \mod_lti\local\ltiservice\service_base $service */
     365              $service->get_configuration_options($mform);
     366          }
     367      }
     368  
     369      /**
     370       * Validate the form data before we allow them to save the tool type.
     371       *
     372       * @param array $data
     373       * @param array $files
     374       * @return array Error messages
     375       */
     376      public function validation($data, $files) {
     377          global $CFG;
     378  
     379          $errors = parent::validation($data, $files);
     380  
     381          // LTI2 tools do not contain a ltiversion field.
     382          if (isset($data['lti_ltiversion']) && $data['lti_ltiversion'] == LTI_VERSION_1P3) {
     383              require_once($CFG->dirroot . '/mod/lti/upgradelib.php');
     384  
     385              $warning = mod_lti_verify_private_key();
     386              if (!empty($warning)) {
     387                  $errors['lti_ltiversion'] = $warning;
     388                  return $errors;
     389              }
     390          }
     391          return $errors;
     392      }
     393  }