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  /**
  18   * LTI 1.3 page to create or edit a platform registration.
  19   *
  20   * This page is only used by LTI 1.3. Older versions do not require platforms to be registered with the tool during
  21   * registration.
  22   *
  23   * @package    enrol_lti
  24   * @copyright  2021 Jake Dallimore <jrhdallimore@gmail.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  use core\output\notification;
  29  use enrol_lti\local\ltiadvantage\form\create_registration_form;
  30  use enrol_lti\local\ltiadvantage\form\platform_registration_form;
  31  use enrol_lti\local\ltiadvantage\entity\application_registration;
  32  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  33  use enrol_lti\local\ltiadvantage\repository\context_repository;
  34  use enrol_lti\local\ltiadvantage\repository\deployment_repository;
  35  use enrol_lti\local\ltiadvantage\repository\resource_link_repository;
  36  use enrol_lti\local\ltiadvantage\repository\user_repository;
  37  use enrol_lti\local\ltiadvantage\service\application_registration_service;
  38  
  39  require_once(__DIR__ . '/../../config.php');
  40  global $CFG, $OUTPUT, $DB;
  41  require_once($CFG->libdir . '/adminlib.php');
  42  require_once($CFG->dirroot . '/enrol/lti/lib.php');
  43  
  44  $action = required_param('action', PARAM_ALPHA);
  45  if (!in_array($action, ['add', 'view', 'edit', 'delete'])) {
  46      throw new coding_exception("Invalid action param '$action'");
  47  }
  48  
  49  // The page to go back to when the respective action has been performed.
  50  $toolregistrationurl = new moodle_url($CFG->wwwroot . "/" . $CFG->admin . "/settings.php",
  51      ['section' => 'enrolsettingslti_registrations']);
  52  
  53  // Local anon helper to extend the nav for this page and call admin_externalpage_setup.
  54  $pagesetup = function(string $pagetitle) {
  55      global $PAGE;
  56      navigation_node::override_active_url(
  57          new moodle_url('/admin/settings.php', ['section' => 'enrolsettingslti_registrations'])
  58      );
  59      admin_externalpage_setup('enrolsettingslti_registrations_edit', '', null, '', ['pagelayout' => 'admin']);
  60      $PAGE->navbar->add($pagetitle);
  61  };
  62  
  63  if ($action == 'view') {
  64      $regid = required_param('regid', PARAM_INT);
  65      $tabselect = optional_param('tabselect', 'platformdetails', PARAM_ALPHA);
  66      global $PAGE;
  67      $pagesetup(get_string('registerplatformedit', 'enrol_lti'));
  68      $pageurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'view', 'regid' => $regid]);
  69  
  70      echo $OUTPUT->header();
  71      echo $OUTPUT->heading(get_string('registerplatformedit', 'enrol_lti'));
  72  
  73      $renderer = $PAGE->get_renderer('enrol_lti');
  74      echo $renderer->render_registration_view($regid, $tabselect);
  75      echo $OUTPUT->footer();
  76      die();
  77  
  78  } else if ($action === 'add') {
  79      $pagesetup(get_string('registerplatformadd', 'enrol_lti'));
  80      $pageurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'add']);
  81  
  82      $mform = new create_registration_form($pageurl->out(false));
  83      if ($data = $mform->get_data()) {
  84          // Create the incomplete registration.
  85          $regservice = new application_registration_service(new application_registration_repository(),
  86              new deployment_repository(), new resource_link_repository(), new context_repository(),
  87              new user_repository());
  88          $draft = $regservice->create_draft_application_registration($data);
  89  
  90          // Redirect to the registration view, which will display endpoints and allow the user to complete the registration.
  91          redirect(new moodle_url('/enrol/lti/register_platform.php',
  92              ['action' => 'view', 'regid' => $draft->get_id(), 'tabselect' => 'tooldetails']));
  93  
  94      } else if (!$mform->is_cancelled()) {
  95          // Display the first step of registration creation.
  96          echo $OUTPUT->header();
  97          echo $OUTPUT->heading(get_string('registerplatformadd', 'enrol_lti'));
  98          $mform->display();
  99          echo $OUTPUT->footer();
 100          die();
 101      }
 102      redirect($toolregistrationurl);
 103  } else if ($action === 'edit') {
 104      $regid = required_param('regid', PARAM_INT);
 105      $pagesetup(get_string('registerplatformedit', 'enrol_lti'));
 106  
 107      $pageurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'edit', 'regid' => $regid]);
 108      $viewurl = new moodle_url('/enrol/lti/register_platform.php', ['action' => 'view', 'regid' => $regid]);
 109  
 110      $mform = new platform_registration_form($pageurl->out(false));
 111      if (($data = $mform->get_data()) && confirm_sesskey()) {
 112          $regservice = new application_registration_service(new application_registration_repository(),
 113              new deployment_repository(), new resource_link_repository(), new context_repository(),
 114              new user_repository());
 115          $regservice->update_application_registration($data);
 116          redirect($viewurl, get_string('registerplatformeditnotice', 'enrol_lti'), null,
 117              notification::NOTIFY_SUCCESS);
 118      } else if (!$mform->is_cancelled()) {
 119          // Anon helper to transform data.
 120          $maptoformdata = function(application_registration $registration): \stdClass {
 121              return (object) [
 122                  'id' => $registration->get_id(),
 123                  'name' => $registration->get_name(),
 124                  'platformid' => $registration->get_platformid(),
 125                  'clientid' => $registration->get_clientid(),
 126                  'authenticationrequesturl' => $registration->get_authenticationrequesturl(),
 127                  'jwksurl' => $registration->get_jwksurl(),
 128                  'accesstokenurl' => $registration->get_accesstokenurl()
 129              ];
 130          };
 131          $appregistrationrepo = new application_registration_repository();
 132          $registration = $appregistrationrepo->find($regid);
 133          if (!$registration) {
 134              throw new coding_exception("cannot edit non-existent registration '{$regid}'.");
 135          }
 136  
 137          $mform->set_data($maptoformdata($registration));
 138  
 139          echo $OUTPUT->header();
 140          echo $OUTPUT->heading(get_string('registerplatformedit', 'enrol_lti'));
 141          $mform->display();
 142          echo $OUTPUT->footer();
 143          die();
 144      }
 145      redirect($viewurl);
 146  } else if ($action === 'delete') {
 147      $regid = required_param('regid', PARAM_INT);
 148      $pagesetup(get_string('registerplatformdelete', 'enrol_lti'));
 149  
 150      if (!optional_param('confirm', false, PARAM_BOOL)) {
 151          $continueparams = ['action' => 'delete', 'regid' => $regid, 'sesskey' => sesskey(), 'confirm' => true];
 152          $continueurl = new moodle_url('/enrol/lti/register_platform.php', $continueparams);
 153          $appregrepo = new application_registration_repository();
 154          $appreg = $appregrepo->find($regid);
 155          if (!$appreg) {
 156              throw new coding_exception("Cannot delete non existent application registration '{$regid}'.");
 157          }
 158  
 159          echo $OUTPUT->header();
 160          echo $OUTPUT->confirm(
 161              get_string('registerplatformdeleteconfirm', 'enrol_lti', format_string($appreg->get_name())),
 162              $continueurl,
 163              $toolregistrationurl
 164          );
 165          echo $OUTPUT->footer();
 166      } else {
 167          require_sesskey();
 168          $regservice = new application_registration_service(new application_registration_repository(),
 169              new deployment_repository(), new resource_link_repository(), new context_repository(),
 170              new user_repository());
 171          $regservice->delete_application_registration($regid);
 172  
 173          redirect($toolregistrationurl,
 174              get_string('registerplatformdeletenotice', 'enrol_lti'), null,  notification::NOTIFY_SUCCESS);
 175      }
 176  }