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\context;
  19  
  20  /**
  21   * Class context_repository.
  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 context_repository {
  28  
  29      /** @var string the name of the table storing object data. */
  30      private $contexttable = 'enrol_lti_context';
  31  
  32      /**
  33       * Generate a context instance from a record.
  34       *
  35       * @param \stdClass $record the record.
  36       * @return context the context instance.
  37       */
  38      private function context_from_record(\stdClass $record): context {
  39          $context = context::create(
  40              $record->ltideploymentid,
  41              $record->contextid,
  42              json_decode($record->type),
  43              $record->id
  44          );
  45          return $context;
  46      }
  47  
  48      /**
  49       * Generate a record from a context instance.
  50       *
  51       * @param context $context the context instance.
  52       * @return \stdClass the resulting record.
  53       */
  54      private function record_from_context(context $context): \stdClass {
  55  
  56          $record = [
  57              'contextid' => $context->get_contextid(),
  58              'ltideploymentid' => $context->get_deploymentid(),
  59              'type' => json_encode($context->get_types()),
  60          ];
  61  
  62          if ($id = $context->get_id()) {
  63              $record['id'] = $id;
  64          }
  65  
  66          return (object) $record;
  67      }
  68  
  69      /**
  70       * Save the context to the store.
  71       *
  72       * @param context $context the context to save.
  73       * @return context the saved context instance.
  74       */
  75      public function save(context $context): context {
  76          global $DB;
  77          $id = $context->get_id();
  78          $exists = $id ? $this->exists($id) : false;
  79  
  80          $record = $this->record_from_context($context);
  81          $timenow = time();
  82          if ($exists) {
  83              $record->timemodified = $timenow;
  84              $DB->update_record($this->contexttable, $record);
  85          } else {
  86              $record->timecreated = $record->timemodified = $timenow;
  87              $id = $DB->insert_record($this->contexttable, $record);
  88              $record->id = $id;
  89          }
  90  
  91          return $this->context_from_record($record);
  92      }
  93  
  94      /**
  95       * Find a context by id.
  96       *
  97       * @param int $id the id of the instance.
  98       * @return context|null the context, if found, else null.
  99       */
 100      public function find(int $id): ?context {
 101          global $DB;
 102          try {
 103              $record = $DB->get_record($this->contexttable, ['id' => $id], '*', MUST_EXIST);
 104              return $this->context_from_record($record);
 105          } catch (\dml_missing_record_exception $e) {
 106              return null;
 107          }
 108      }
 109  
 110      /**
 111       * Find a context by it's platform-issued context id string.
 112       *
 113       * @param string $contextid the id of the context on the platform.
 114       * @param int $deploymentid the id of the local deployment instance in which the contextid is unique.
 115       * @return context|null the context instance, if found, else null.
 116       */
 117      public function find_by_contextid(string $contextid, int $deploymentid): ?context {
 118          global $DB;
 119          try {
 120              $record = $DB->get_record($this->contexttable,
 121                  ['contextid' => $contextid, 'ltideploymentid' => $deploymentid], '*', MUST_EXIST);
 122              return $this->context_from_record($record);
 123          } catch (\dml_missing_record_exception $e) {
 124              return null;
 125          }
 126      }
 127  
 128      /**
 129       * Check whether the context identified by 'id' exists in the store.
 130       *
 131       * @param int $id the id of the instance to check.
 132       * @return bool true if found, false otherwise.
 133       */
 134      public function exists(int $id): bool {
 135          global $DB;
 136          return $DB->record_exists($this->contexttable, ['id' => $id]);
 137      }
 138  
 139      /**
 140       * Delete the context identified by 'id' from the store.
 141       *
 142       * @param int $id the id of context to delete.
 143       */
 144      public function delete(int $id): void {
 145          global $DB;
 146          $DB->delete_records($this->contexttable, ['id' => $id]);
 147      }
 148  
 149      /**
 150       * Delete all contexts under a given deployment.
 151       *
 152       * @param int $deploymentid the id of the local deployment instance.
 153       */
 154      public function delete_by_deployment(int $deploymentid): void {
 155          global $DB;
 156          $DB->delete_records($this->contexttable, ['ltideploymentid' => $deploymentid]);
 157      }
 158  }