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