ILIAS  release_8 Revision v8.24
class.ilChatroomAdmin.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 private static string $settingsTable = 'chatroom_admconfig';
30
31 private int $config_id;
32 private ?stdClass $settings;
33
34 public function __construct(int $config_id, stdClass $settings = null)
35 {
36 $this->config_id = $config_id;
37 $this->settings = $settings;
38 }
39
45 public static function getDefaultConfiguration(): self
46 {
47 global $DIC;
48
49 $DIC->database()->setLimit(1);
50 $query = 'SELECT * FROM ' . self::$settingsTable;
51
52 $rset = $DIC->database()->query($query);
53 if ($row = $DIC->database()->fetchObject($rset)) {
54 return new self((int) $row->instance_id, $row);
55 }
56
57 throw new LogicException('Could not determine any default configuration');
58 }
59
66 {
68 }
69
74 public function saveGeneralSettings(stdClass $settings): void
75 {
76 global $DIC;
77
78 $res = $DIC->database()->queryF(
79 "SELECT* FROM chatroom_admconfig WHERE instance_id = %s",
80 ['integer'],
81 [$this->config_id]
82 );
83
84 $row = $DIC->database()->fetchAssoc($res);
85
86 $DIC->database()->manipulateF(
87 "DELETE FROM chatroom_admconfig WHERE instance_id = %s",
88 ['integer'],
89 [$this->config_id]
90 );
91
92 $def_conf = '{}';
93 $clnt_set = '{}';
94 if (is_array($row)) {
95 if ($row['default_config'] !== null) {
96 $def_conf = $row['default_config'];
97 }
98
99 if ($row['client_settings'] !== null) {
100 $clnt_set = $row['client_settings'];
101 }
102 }
103
104 $DIC->database()->manipulateF(
105 "
106 INSERT INTO chatroom_admconfig
107 (instance_id, server_settings, default_config, client_settings)
108 VALUES (%s, %s, %s, %s)",
109 ['integer', 'text', 'integer', 'text'],
110 [$this->config_id, json_encode($settings, JSON_THROW_ON_ERROR), $def_conf, $clnt_set]
111 );
112 }
113
118 public function saveClientSettings(stdClass $settings): void
119 {
120 global $DIC;
121
122 $res = $DIC->database()->queryF(
123 "SELECT * FROM chatroom_admconfig WHERE instance_id = %s",
124 ['integer'],
125 [$this->config_id]
126 );
127
128 $row = $DIC->database()->fetchAssoc($res);
129
130 $DIC->database()->manipulateF(
131 "DELETE FROM chatroom_admconfig WHERE instance_id = %s",
132 ['integer'],
133 [$this->config_id]
134 );
135
136 ($row['default_config'] ?? null) !== null ? $def_conf = $row['default_config'] : $def_conf = "{}";
137 ($row['server_settings'] ?? null) !== null ? $srv_set = $row['server_settings'] : $srv_set = "{}";
138
139 $DIC->database()->manipulateF(
140 "
141 INSERT INTO chatroom_admconfig
142 (instance_id, server_settings, default_config, client_settings)
143 VALUES (%s, %s, %s, %s)",
144 [
145 'integer',
146 'text',
147 'integer',
148 'text'
149 ],
150 [
151 $this->config_id,
152 $srv_set,
153 $def_conf,
154 json_encode($settings, JSON_THROW_ON_ERROR)
155 ]
156 );
157 }
158
159 public function loadGeneralSettings(): array
160 {
161 global $DIC;
162
163 $query = 'SELECT * FROM ' . self::$settingsTable .
164 ' WHERE instance_id = ' . $DIC->database()->quote($this->config_id, 'integer');
165
166 if (($row = $DIC->database()->fetchAssoc($DIC->database()->query($query))) && $row['server_settings']) {
167 $settings = json_decode($row['server_settings'], true, 512, JSON_THROW_ON_ERROR);
168
169 if (!isset($settings['protocol'])) {
170 $settings['protocol'] = 'http';
171 }
172
173 if (!isset($settings['log_level'])) {
174 $settings['log_level'] = 'info';
175 }
176
177 return $settings;
178 }
179
180 return [];
181 }
182
183 public function loadClientSettings(): array
184 {
185 global $DIC;
186
187 $query = 'SELECT * FROM ' . self::$settingsTable .
188 ' WHERE instance_id = ' . $DIC->database()->quote($this->config_id, 'integer');
189 if (($row = $DIC->database()->fetchAssoc($DIC->database()->query($query))) && $row['client_settings']) {
190 $settings = json_decode($row['client_settings'], true, 512, JSON_THROW_ON_ERROR);
191
192 if (!isset($settings['client']) || !is_string($settings['client']) || $settings['client'] === '') {
193 $settings['client'] = CLIENT_ID;
194 }
195
196 if (isset($settings['name']) && is_string($settings['name']) && !$settings['name'] === '') {
197 $settings['client_name'] = (string) $settings['name'];
198 } else {
199 $settings['client_name'] = CLIENT_ID;
200 }
201
202 if (isset($settings['conversation_idle_state_in_minutes']) && is_numeric($settings['conversation_idle_state_in_minutes'])) {
203 $settings['conversation_idle_state_in_minutes'] = max(1, $settings['conversation_idle_state_in_minutes']);
204 } else {
205 $settings['conversation_idle_state_in_minutes'] = 1;
206 }
207
208 if (!isset($settings['auth']) || !is_array($settings['auth'])) {
209 $settings['auth'] = [];
210 }
211 if (!isset($settings['auth']['key']) || !is_string($settings['auth']['key'])) {
212 $settings['auth']['key'] = '';
213 }
214 if (!isset($settings['auth']['secret']) || !is_string($settings['auth']['secret'])) {
215 $settings['auth']['secret'] = '';
216 }
217
218 return $settings;
219 }
220
221 return [];
222 }
223}
Class ilChatroomAdmin.
static getDefaultConfiguration()
Instantiates and returns ilChatroomAdmin object using instance_id and settings from settingsTable.
saveClientSettings(stdClass $settings)
Saves given client $settings into settingsTable.
static string $settingsTable
getServerSettings()
Instantiates ilChatroomServerSettings object, sets data using $this->settings->server_settings and re...
saveGeneralSettings(stdClass $settings)
Saves given $settings into settingsTable.
__construct(int $config_id, stdClass $settings=null)
Class ilChatroomServerSettings.
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query