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\repository;
  18  use enrol_lti\local\ltiadvantage\entity\deployment;
  19  
  20  /**
  21   * The deployment_repository class.
  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  class deployment_repository {
  28  
  29      /** @var string $deploymenttable the name of table containing deployments. */
  30      private $deploymenttable = 'enrol_lti_deployment';
  31  
  32      /**
  33       * Create a valid record from a deployment instance.
  34       *
  35       * @param deployment $deployment the deployment.
  36       * @return \stdClass a compatible record.
  37       */
  38      private function record_from_deployment(deployment $deployment): \stdClass {
  39          $record = (object) [
  40              'name' => $deployment->get_deploymentname(),
  41              'deploymentid' => $deployment->get_deploymentid(),
  42              'platformid' => $deployment->get_registrationid(),
  43              'legacyconsumerkey' => $deployment->get_legacy_consumer_key()
  44          ];
  45          if ($id = $deployment->get_id()) {
  46              $record->id = $id;
  47          }
  48          return $record;
  49      }
  50  
  51      /**
  52       * Create a list of deployments based on a list of records.
  53       *
  54       * @param array $records an array of deployment records.
  55       * @return deployment[]
  56       */
  57      private function deployments_from_records(array $records): array {
  58          if (empty($records)) {
  59              return [];
  60          }
  61          return array_map(function($record) {
  62              return $this->deployment_from_record($record);
  63          }, $records);
  64      }
  65  
  66      /**
  67       * Create a valid deployment from a record.
  68       *
  69       * @param \stdClass $record the record.
  70       * @return deployment the deployment instance.
  71       */
  72      private function deployment_from_record(\stdClass $record): deployment {
  73          $deployment = deployment::create(
  74              $record->platformid,
  75              $record->deploymentid,
  76              $record->name,
  77              $record->id,
  78              $record->legacyconsumerkey
  79          );
  80          return $deployment;
  81      }
  82  
  83      /**
  84       * Save a deployment to the store.
  85       *
  86       * @param deployment $deployment the deployment instance to save.
  87       * @return deployment the saved deployment instance.
  88       */
  89      public function save(deployment $deployment): deployment {
  90          global $DB;
  91          $id = $deployment->get_id();
  92          $exists = $id ? $this->exists($id) : false;
  93  
  94          $record = $this->record_from_deployment($deployment);
  95          $timenow = time();
  96          if ($exists) {
  97              $record->timemodified = $timenow;
  98              $DB->update_record($this->deploymenttable, $record);
  99          } else {
 100              $record->timecreated = $record->timemodified = $timenow;
 101              $id = $DB->insert_record($this->deploymenttable, $record);
 102              $record->id = $id;
 103          }
 104  
 105          return $this->deployment_from_record($record);
 106      }
 107  
 108      /**
 109       * Find and return a deployment, by id.
 110       *
 111       * @param int $id the id of the deployment to find.
 112       * @return deployment|null
 113       */
 114      public function find(int $id): ?deployment {
 115          global $DB;
 116          try {
 117              $record = $DB->get_record($this->deploymenttable, ['id' => $id], '*', MUST_EXIST);
 118              return $this->deployment_from_record($record);
 119          } catch (\dml_missing_record_exception $e) {
 120              return null;
 121          }
 122      }
 123  
 124      /**
 125       * Determine whether a deployment exists in the repository.
 126       *
 127       * @param int $id the identifier of the deployment
 128       * @return bool true if the deployment exists, false otherwise.
 129       */
 130      public function exists(int $id): bool {
 131          global $DB;
 132          return $DB->record_exists($this->deploymenttable, ['id' => $id]);
 133      }
 134  
 135      /**
 136       * Delete a deployment from the store.
 137       *
 138       * @param int $id the id of the deployment object to remove.
 139       */
 140      public function delete(int $id): void {
 141          global $DB;
 142          $DB->delete_records($this->deploymenttable, ['id' => $id]);
 143      }
 144  
 145      /**
 146       * Delete all deployments for the given registration.
 147       *
 148       * @param int $registrationid the registration id.
 149       */
 150      public function delete_by_registration(int $registrationid): void {
 151          global $DB;
 152          $DB->delete_records($this->deploymenttable, ['platformid' => $registrationid]);
 153      }
 154  
 155      /**
 156       * Return a count of how many deployments exists for a given application_registration.
 157       *
 158       * @param int $registrationid the id of the application_registration instance.
 159       * @return int the number of deployments found.
 160       */
 161      public function count_by_registration(int $registrationid): int {
 162          global $DB;
 163          return $DB->count_records($this->deploymenttable, ['platformid' => $registrationid]);
 164      }
 165  
 166      /**
 167       * Get a deployment based on its deploymentid and a for a given application registration id.
 168       *
 169       * @param int $registrationid the id of the application_registration to which the deployment belongs.
 170       * @param string $deploymentid the deploymentid of the deployment, as set by the platform.
 171       * @return deployment|null deployment if found, otherwise null.
 172       */
 173      public function find_by_registration(int $registrationid, string $deploymentid): ?deployment {
 174          global $DB;
 175          try {
 176              $sql = "SELECT eld.id, eld.name, eld.deploymentid, eld.platformid, eld.legacyconsumerkey
 177                        FROM {".$this->deploymenttable."} eld
 178                        JOIN {enrol_lti_app_registration} elar
 179                          ON (eld.platformid = elar.id)
 180                       WHERE elar.id = :registrationid
 181                         AND eld.deploymentid = :deploymentid";
 182              $params = ['registrationid' => $registrationid, 'deploymentid' => $deploymentid];
 183              $record = $DB->get_record_sql($sql, $params, MUST_EXIST);
 184              return $this->deployment_from_record($record);
 185          } catch (\dml_missing_record_exception $e) {
 186              return null;
 187          }
 188      }
 189  
 190      /**
 191       * Get all deployments for a given application registration id.
 192       *
 193       * @param int $registrationid the id of the application_registration to which the deployment belongs.
 194       * @return deployment[]|null deployments if found, otherwise null.
 195       */
 196      public function find_all_by_registration(int $registrationid): ?array {
 197          global $DB;
 198  
 199          $sql = "SELECT eld.id, eld.name, eld.deploymentid, eld.platformid, eld.legacyconsumerkey
 200                    FROM {".$this->deploymenttable."} eld
 201                    JOIN {enrol_lti_app_registration} elar
 202                      ON (eld.platformid = elar.id)
 203                   WHERE elar.id = :registrationid";
 204          $records = $DB->get_records_sql($sql, ['registrationid' => $registrationid]);
 205          return $this->deployments_from_records($records);
 206      }
 207  }