Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace enrol_lti\local\ltiadvantage\form;
  18  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  19  
  20  defined('MOODLE_INTERNAL') || die();
  21  
  22  require_once($CFG->libdir . '/formslib.php');
  23  
  24  /**
  25   * The platform_registration_form class, for registering a platform as a consumer of a published tool.
  26   *
  27   * @package    enrol_lti
  28   * @copyright  2021 Jake Dallimore <jrhdallimore@gmail.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class platform_registration_form extends \moodleform {
  32  
  33      /**
  34       * Define the form.
  35       */
  36      protected function definition() {
  37          $mform = $this->_form;
  38          $strrequired = get_string('required');
  39  
  40          // Id.
  41          $mform->addElement('hidden', 'id');
  42          $mform->setType('id', PARAM_INT);
  43  
  44          // Name.
  45          $mform->addElement('text', 'name', get_string('registerplatform:name', 'enrol_lti'));
  46          $mform->setType('name', PARAM_TEXT);
  47          $mform->addRule('name', $strrequired, 'required', null, 'client');
  48  
  49          // Platform Id.
  50          $mform->addElement('text', 'platformid', get_string('registerplatform:platformid', 'enrol_lti'));
  51          $mform->setType('platformid', PARAM_URL);
  52          $mform->addRule('platformid', $strrequired, 'required', null, 'client');
  53          $mform->addHelpButton('platformid', 'registerplatform:platformid', 'enrol_lti');
  54  
  55          // Client Id.
  56          $mform->addElement('text', 'clientid', get_string('registerplatform:clientid', 'enrol_lti'));
  57          $mform->setType('clientid', PARAM_TEXT);
  58          $mform->addRule('clientid', $strrequired, 'required', null, 'client');
  59          $mform->addHelpButton('clientid', 'registerplatform:clientid', 'enrol_lti');
  60  
  61          // Authentication request URL.
  62          $mform->addElement('text', 'authenticationrequesturl', get_string('registerplatform:authrequesturl', 'enrol_lti'));
  63          $mform->setType('authenticationrequesturl', PARAM_URL);
  64          $mform->addRule('authenticationrequesturl', $strrequired, 'required', null, 'client');
  65          $mform->addHelpButton('authenticationrequesturl', 'registerplatform:authrequesturl', 'enrol_lti');
  66  
  67          // JSON Web Key Set URL.
  68          $mform->addElement('text', 'jwksurl', get_string('registerplatform:jwksurl', 'enrol_lti'));
  69          $mform->setType('jwksurl', PARAM_URL);
  70          $mform->addRule('jwksurl', $strrequired, 'required', null, 'client');
  71          $mform->addHelpButton('jwksurl', 'registerplatform:jwksurl', 'enrol_lti');
  72  
  73          // Access token URL.
  74          $mform->addElement('text', 'accesstokenurl', get_string('registerplatform:accesstokenurl', 'enrol_lti'));
  75          $mform->setType('accesstokenurl', PARAM_URL);
  76          $mform->addRule('accesstokenurl', $strrequired, 'required', null, 'client');
  77          $mform->addHelpButton('accesstokenurl', 'registerplatform:accesstokenurl', 'enrol_lti');
  78  
  79          $buttonarray = [];
  80          $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
  81          $buttonarray[] = $mform->createElement('cancel');
  82          $mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
  83      }
  84  
  85      /**
  86       * Provides validation of URL syntax and issuer uniqueness.
  87       *
  88       * @param array $data the form data.
  89       * @param array $files any submitted files.
  90       * @return array an array of errors.
  91       */
  92      public function validation($data, $files) {
  93          $errors = parent::validation($data, $files);
  94  
  95          // Check URLs look ok.
  96          foreach ($data as $key => $val) {
  97              if (isset($this->_form->_types[$key]) && $this->_form->_types[$key] == 'url') {
  98                  if (!filter_var($val, FILTER_VALIDATE_URL)) {
  99                      $errors[$key] = get_string('registerplatform:invalidurlerror', 'enrol_lti');
 100                  }
 101              }
 102          }
 103  
 104          // Check uniqueness of the {issuer, client_id} tuple.
 105          $appregistrationrepo = new application_registration_repository();
 106          $appreg = $appregistrationrepo->find_by_platform($data['platformid'], $data['clientid']);
 107          if ($appreg) {
 108              if (empty($data['id']) || $appreg->get_id() != $data['id']) {
 109                  $errors['clientid'] = get_string('registerplatform:duplicateregistrationerror', 'enrol_lti');
 110              }
 111          }
 112  
 113          return $errors;
 114      }
 115  }