Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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\service;
  18  
  19  use enrol_lti\local\ltiadvantage\entity\deployment;
  20  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  21  use enrol_lti\local\ltiadvantage\repository\deployment_repository;
  22  use enrol_lti\local\ltiadvantage\repository\resource_link_repository;
  23  use enrol_lti\local\ltiadvantage\repository\context_repository;
  24  use enrol_lti\local\ltiadvantage\repository\user_repository;
  25  
  26  /**
  27   * Class tool_deployment_service.
  28   *
  29   * @package enrol_lti
  30   * @copyright  2021 Jake Dallimore <jrhdallimore@gmail.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class tool_deployment_service {
  34  
  35      /** @var application_registration_repository repository to work with application_registration instances. */
  36      private $appregistrationrepo;
  37  
  38      /** @var deployment_repository repository to work with deployment instances. */
  39      private $deploymentrepo;
  40  
  41      /** @var resource_link_repository repository to work with resource link instances. */
  42      private $resourcelinkrepo;
  43  
  44      /** @var context_repository repository to work with context instances. */
  45      private $contextrepo;
  46  
  47      /** @var user_repository repository to work with user instances. */
  48      private $userrepo;
  49  
  50      /**
  51       * The tool_deployment_service constructor.
  52       *
  53       * @param application_registration_repository $appregistrationrepo an application_registration_repository instance.
  54       * @param deployment_repository $deploymentrepo a deployment_repository instance.
  55       * @param resource_link_repository $resourcelinkrepo a resource_link_repository instance.
  56       * @param context_repository $contextrepo a context_repository instance.
  57       * @param user_repository $userrepo a user_repository instance.
  58       */
  59      public function __construct(application_registration_repository $appregistrationrepo,
  60              deployment_repository $deploymentrepo, resource_link_repository $resourcelinkrepo,
  61              context_repository $contextrepo, user_repository $userrepo) {
  62  
  63          $this->appregistrationrepo = $appregistrationrepo;
  64          $this->deploymentrepo = $deploymentrepo;
  65          $this->resourcelinkrepo = $resourcelinkrepo;
  66          $this->contextrepo = $contextrepo;
  67          $this->userrepo = $userrepo;
  68      }
  69  
  70      /**
  71       * Service handling the use case "As an admin I can add a tool deployment to a platform registration".
  72       *
  73       * @param \stdClass $requestdto the required service data.
  74       * @return deployment the deployment instance which has been created.
  75       * @throws \coding_exception if the registration doesn't exist.
  76       */
  77      public function add_tool_deployment(\stdClass $requestdto): deployment {
  78          // DTO contains: registration_id, deployment_id, deployment_name.
  79          [
  80              'registration_id' => $registrationid,
  81              'deployment_id' => $deploymentid,
  82              'deployment_name' => $deploymentname
  83          ] = (array) $requestdto;
  84  
  85          $registration = $this->appregistrationrepo->find($registrationid);
  86          if (is_null($registration)) {
  87              throw new \coding_exception("Cannot add deployment to non-existent application registration ".
  88                  "'$registrationid'");
  89          }
  90  
  91          $deployment = $registration->add_tool_deployment($deploymentname, $deploymentid);
  92  
  93          return $this->deploymentrepo->save($deployment);
  94      }
  95  
  96      /**
  97       * Service handling the use case "As an admin I can delete a tool deployment from a platform registration".
  98       *
  99       * @param int $deploymentid the id of the deployment to remove.
 100       */
 101      public function delete_tool_deployment(int $deploymentid): void {
 102          // Delete any resource links attached to this deployment.
 103          $this->resourcelinkrepo->delete_by_deployment($deploymentid);
 104  
 105          // Delete any context entries for the deployment.
 106          $this->contextrepo->delete_by_deployment($deploymentid);
 107  
 108          // Delete all enrolments for any users tied to this deployment.
 109          global $DB, $CFG;
 110          $sql = "SELECT lu.userid as ltiuserid, e.*
 111                    FROM {enrol_lti_users} lu
 112                    JOIN {enrol_lti_tools} lt
 113                      ON (lt.id = lu.toolid)
 114                    JOIN {enrol} e
 115                      ON (e.id = lt.enrolid)
 116                   WHERE lu.ltideploymentid = :deploymentid";
 117          $instancesrs = $DB->get_recordset_sql($sql, ['deploymentid' => $deploymentid]);
 118          require_once($CFG->dirroot . '/enrol/lti/lib.php');
 119          $enrollti = new \enrol_lti_plugin();
 120          foreach ($instancesrs as $instance) {
 121              $userid = $instance->ltiuserid;
 122              $enrollti->unenrol_user($instance, $userid);
 123          }
 124          $instancesrs->close();
 125  
 126          // Delete any lti user enrolments.
 127          $this->userrepo->delete_by_deployment($deploymentid);
 128  
 129          // Delete the deployment itself.
 130          $this->deploymentrepo->delete($deploymentid);
 131      }
 132  }