Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 {
11 efrain 116
        if (empty($instance->customlinkurl)) {
117
            return;
118
        }
119
 
1 efrain 120
        global $DB;
121
 
122
        $commid = $this->communication->get_id();
123
        $cachekey = "link_url_{$commid}";
124
 
125
        $newrecord = new \stdClass();
11 efrain 126
        $newrecord->url = $instance->customlinkurl;
1 efrain 127
 
128
        $existingrecord = $DB->get_record(
129
            self::CUSTOMLINK_TABLE,
130
            ['commid' => $commid],
131
            'id, url'
132
        );
133
 
134
        if (!$existingrecord) {
135
            // Create the record if it does not exist.
136
            $newrecord->commid = $commid;
137
            $DB->insert_record(self::CUSTOMLINK_TABLE, $newrecord);
11 efrain 138
        } else if ($instance->customlinkurl !== $existingrecord->url) {
1 efrain 139
            // Update record if the URL has changed.
140
            $newrecord->id = $existingrecord->id;
141
            $DB->update_record(self::CUSTOMLINK_TABLE, $newrecord);
142
        } else {
143
            // No change made.
144
            return;
145
        }
146
 
147
        // Cache the new URL.
148
        $this->cache->set($cachekey, $newrecord->url);
149
    }
150
 
151
    public function set_form_data(\stdClass $instance): void {
152
        if (!empty($instance->id) && !empty($this->communication->get_id())) {
153
            $instance->customlinkurl = $this->get_chat_room_url();
154
        }
155
    }
156
 
157
    public static function set_form_definition(\MoodleQuickForm $mform): void {
158
        // Custom link description for the communication provider.
159
        $mform->insertElementBefore($mform->createElement(
160
            'text',
161
            'customlinkurl',
162
            get_string('customlinkurl', 'communication_customlink'),
163
            'maxlength="255" size="40"'
164
        ), 'addcommunicationoptionshere');
165
        $mform->addHelpButton('customlinkurl', 'customlinkurl', 'communication_customlink');
166
        $mform->setType('customlinkurl', PARAM_URL);
167
        $mform->addRule('customlinkurl', get_string('required'), 'required', null, 'client');
168
        $mform->addRule('customlinkurl', get_string('maximumchars', '', 255), 'maxlength', 255);
169
        $mform->insertElementBefore($mform->createElement(
170
            'static',
171
            'customlinkurlinfo',
172
            '',
173
            get_string('customlinkurlinfo', 'communication_customlink'),
174
            'addcommunicationoptionshere'
175
        ), 'addcommunicationoptionshere');
176
    }
177
 
178
    public static function is_configured(): bool {
179
        return true;
180
    }
181
}