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_customlink; 18 19 use core_communication\processor; 20 21 /** 22 * class communication_feature to handle custom link specific actions. 23 * 24 * @package communication_customlink 25 * @copyright 2023 Michael Hawkins <michaelh@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 class communication_feature implements 29 \core_communication\communication_provider, 30 \core_communication\form_provider, 31 \core_communication\room_chat_provider { 32 /** @var string The database table storing custom link specific data */ 33 protected const CUSTOMLINK_TABLE = 'communication_customlink'; 34 35 /** @var \cache_application $cache The application cache for this provider. */ 36 protected \cache_application $cache; 37 38 /** 39 * Load the communication provider for the communication API. 40 * 41 * @param processor $communication The communication processor object. 42 * @return communication_feature The communication provider object. 43 */ 44 public static function load_for_instance(processor $communication): self { 45 return new self($communication); 46 } 47 48 /** 49 * Constructor for communication provider. 50 * 51 * @param processor $communication The communication processor object. 52 */ 53 private function __construct( 54 private \core_communication\processor $communication, 55 ) { 56 $this->cache = \cache::make('communication_customlink', 'customlink'); 57 } 58 59 /** 60 * Create room - room existence managed externally, always return true. 61 * 62 * @return boolean 63 */ 64 public function create_chat_room(): bool { 65 return true; 66 } 67 68 /** 69 * Update room - room existence managed externally, always return true. 70 * 71 * @return boolean 72 */ 73 public function update_chat_room(): bool { 74 return true; 75 } 76 77 /** 78 * Delete room - room existence managed externally, always return true. 79 * 80 * @return boolean 81 */ 82 public function delete_chat_room(): bool { 83 return true; 84 } 85 86 /** 87 * Fetch the URL for this custom link provider. 88 * 89 * @return string|null The custom URL, or null if not found. 90 */ 91 public function get_chat_room_url(): ?string { 92 global $DB; 93 94 $commid = $this->communication->get_id(); 95 $cachekey = "link_url_{$commid}"; 96 97 // Attempt to fetch the room URL from the cache. 98 if ($url = $this->cache->get($cachekey)) { 99 return $url; 100 } 101 102 // If not found in the cache, fetch the URL from the database. 103 $url = $DB->get_field( 104 self::CUSTOMLINK_TABLE, 105 'url', 106 ['commid' => $commid], 107 ); 108 109 // Cache the URL. 110 $this->cache->set($cachekey, $url); 111 112 return $url; 113 } 114 115 public function save_form_data(\stdClass $instance): void { 116 global $DB; 117 118 $commid = $this->communication->get_id(); 119 $cachekey = "link_url_{$commid}"; 120 121 $newrecord = new \stdClass(); 122 $newrecord->url = $instance->customlinkurl ?? null; 123 124 $existingrecord = $DB->get_record( 125 self::CUSTOMLINK_TABLE, 126 ['commid' => $commid], 127 'id, url' 128 ); 129 130 if (!$existingrecord) { 131 // Create the record if it does not exist. 132 $newrecord->commid = $commid; 133 $DB->insert_record(self::CUSTOMLINK_TABLE, $newrecord); 134 } else if ($newrecord->url !== $existingrecord->url) { 135 // Update record if the URL has changed. 136 $newrecord->id = $existingrecord->id; 137 $DB->update_record(self::CUSTOMLINK_TABLE, $newrecord); 138 } else { 139 // No change made. 140 return; 141 } 142 143 // Cache the new URL. 144 $this->cache->set($cachekey, $newrecord->url); 145 } 146 147 public function set_form_data(\stdClass $instance): void { 148 if (!empty($instance->id) && !empty($this->communication->get_id())) { 149 $instance->customlinkurl = $this->get_chat_room_url(); 150 } 151 } 152 153 public static function set_form_definition(\MoodleQuickForm $mform): void { 154 // Custom link description for the communication provider. 155 $mform->insertElementBefore($mform->createElement( 156 'text', 157 'customlinkurl', 158 get_string('customlinkurl', 'communication_customlink'), 159 'maxlength="255" size="40"' 160 ), 'addcommunicationoptionshere'); 161 $mform->addHelpButton('customlinkurl', 'customlinkurl', 'communication_customlink'); 162 $mform->setType('customlinkurl', PARAM_URL); 163 $mform->addRule('customlinkurl', get_string('required'), 'required', null, 'client'); 164 $mform->addRule('customlinkurl', get_string('maximumchars', '', 255), 'maxlength', 255); 165 $mform->insertElementBefore($mform->createElement( 166 'static', 167 'customlinkurlinfo', 168 '', 169 get_string('customlinkurlinfo', 'communication_customlink'), 170 'addcommunicationoptionshere' 171 ), 'addcommunicationoptionshere'); 172 } 173 174 public static function is_configured(): bool { 175 return true; 176 } 177 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body