Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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 communication_matrix;
  18  
  19  use stdClass;
  20  
  21  /**
  22   * Class to manage the updates to the room information in db.
  23   *
  24   * @package    communication_matrix
  25   * @copyright  2023 Safat Shahin <safat.shahin@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class matrix_room {
  29      private const TABLE = 'matrix_room';
  30  
  31      /** @var \stdClass|null $record The matrix room record from db */
  32  
  33      /**
  34       * Load the matrix room record for the supplied processor.
  35       * @param int $processorid
  36       * @return null|self
  37       */
  38      public static function load_by_processor_id(
  39          int $processorid,
  40      ): ?self {
  41          global $DB;
  42  
  43          $record = $DB->get_record(self::TABLE, ['commid' => $processorid]);
  44  
  45          if (!$record) {
  46              return null;
  47          }
  48          return new self($record);
  49      }
  50  
  51      /**
  52       * Matrix rooms constructor to load the matrix room information from matrix_room table.
  53       *
  54       * @param stdClass $record
  55       */
  56      private function __construct(
  57          private stdClass $record,
  58      ) {
  59      }
  60  
  61      /**
  62       * Create matrix room data.
  63       *
  64       * @param int $processorid The id of the communication record
  65       * @param string|null $topic The topic of the room for matrix
  66       * @param string|null $roomid The id of the room from matrix
  67       * @return self
  68       */
  69      public static function create_room_record(
  70          int $processorid,
  71          ?string $topic,
  72          ?string $roomid = null,
  73      ): self {
  74          global $DB;
  75  
  76          $roomrecord = (object) [
  77              'commid' => $processorid,
  78              'roomid' => $roomid,
  79              'topic' => $topic,
  80          ];
  81          $roomrecord->id = $DB->insert_record(self::TABLE, $roomrecord);
  82  
  83          return self::load_by_processor_id($processorid);
  84      }
  85  
  86      /**
  87       * Update matrix room data.
  88       *
  89       * @param string|null $roomid The id of the room from matrix
  90       * @param string|null $topic The topic of the room for matrix
  91       */
  92      public function update_room_record(
  93          ?string $roomid = null,
  94          ?string $topic = null,
  95      ): void {
  96          global $DB;
  97  
  98          if ($roomid !== null) {
  99              $this->record->roomid = $roomid;
 100          }
 101  
 102          if ($topic !== null) {
 103              $this->record->topic = $topic;
 104          }
 105  
 106          $DB->update_record(self::TABLE, $this->record);
 107      }
 108  
 109      /**
 110       * Delete matrix room data.
 111       */
 112      public function delete_room_record(): void {
 113          global $DB;
 114  
 115          $DB->delete_records(self::TABLE, ['commid' => $this->record->commid]);
 116  
 117          unset($this->record);
 118      }
 119  
 120      /**
 121       * Get the processor id.
 122       *
 123       * @return int
 124       */
 125      public function get_processor_id(): int {
 126          return $this->record->commid;
 127      }
 128  
 129      /**
 130       * Get the matrix room id.
 131       *
 132       * @return string|null
 133       */
 134      public function get_room_id(): ?string {
 135          return $this->record->roomid;
 136      }
 137  
 138      /**
 139       * Get the matrix room topic.
 140       *
 141       * @return string
 142       */
 143      public function get_topic(): string {
 144          return $this->record->topic ?? '';
 145      }
 146  }