ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 public function __construct(private readonly int $config_id, private readonly ?stdClass $settings = null)
32 {
33 }
34
40 public static function getDefaultConfiguration(): self
41 {
42 global $DIC;
43
44 $DIC->database()->setLimit(1);
45 $query = 'SELECT * FROM ' . self::$settingsTable;
46
47 $rset = $DIC->database()->query($query);
48 if ($row = $DIC->database()->fetchObject($rset)) {
49 return new self((int) $row->instance_id, $row);
50 }
51
52 throw new LogicException('Could not determine any default configuration');
53 }
54
61 {
63 }
64
69 public function saveGeneralSettings(stdClass $settings): void
70 {
71 global $DIC;
72
73 $res = $DIC->database()->queryF(
74 "SELECT* FROM chatroom_admconfig WHERE instance_id = %s",
75 ['integer'],
76 [$this->config_id]
77 );
78
79 $row = $DIC->database()->fetchAssoc($res);
80
81 $DIC->database()->manipulateF(
82 "DELETE FROM chatroom_admconfig WHERE instance_id = %s",
83 ['integer'],
84 [$this->config_id]
85 );
86
87 $def_conf = '{}';
88 $clnt_set = '{}';
89 if (is_array($row)) {
90 if ($row['default_config'] !== null) {
91 $def_conf = $row['default_config'];
92 }
93
94 if ($row['client_settings'] !== null) {
95 $clnt_set = $row['client_settings'];
96 }
97 }
98
99 $DIC->database()->manipulateF(
100 "
101 INSERT INTO chatroom_admconfig
102 (instance_id, server_settings, default_config, client_settings)
103 VALUES (%s, %s, %s, %s)",
104 ['integer', 'text', 'integer', 'text'],
105 [$this->config_id, json_encode($settings, JSON_THROW_ON_ERROR), $def_conf, $clnt_set]
106 );
107 }
108
112 public function saveClientSettings(stdClass $settings): void
113 {
114 global $DIC;
115
116 $res = $DIC->database()->queryF(
117 "SELECT * FROM chatroom_admconfig WHERE instance_id = %s",
118 ['integer'],
119 [$this->config_id]
120 );
121
122 $row = $DIC->database()->fetchAssoc($res);
123
124 $DIC->database()->manipulateF(
125 "DELETE FROM chatroom_admconfig WHERE instance_id = %s",
126 ['integer'],
127 [$this->config_id]
128 );
129
130 ($row['default_config'] ?? null) !== null ? $def_conf = $row['default_config'] : $def_conf = "{}";
131 ($row['server_settings'] ?? null) !== null ? $srv_set = $row['server_settings'] : $srv_set = "{}";
132
133 $DIC->database()->manipulateF(
134 "
135 INSERT INTO chatroom_admconfig
136 (instance_id, server_settings, default_config, client_settings)
137 VALUES (%s, %s, %s, %s)",
138 [
139 'integer',
140 'text',
141 'integer',
142 'text'
143 ],
144 [
145 $this->config_id,
146 $srv_set,
147 $def_conf,
148 json_encode($settings, JSON_THROW_ON_ERROR)
149 ]
150 );
151 }
152
153 public function loadGeneralSettings(): array
154 {
155 global $DIC;
156
157 $query = 'SELECT * FROM ' . self::$settingsTable .
158 ' WHERE instance_id = ' . $DIC->database()->quote($this->config_id, 'integer');
159
160 if (($row = $DIC->database()->fetchAssoc($DIC->database()->query($query))) && $row['server_settings']) {
161 $settings = json_decode($row['server_settings'], true, 512, JSON_THROW_ON_ERROR);
162
163 if (!isset($settings['protocol'])) {
164 $settings['protocol'] = 'http';
165 }
166
167 if (!isset($settings['log_level'])) {
168 $settings['log_level'] = 'info';
169 }
170
171 return $settings;
172 }
173
174 return [];
175 }
176
177 public function loadClientSettings(): array
178 {
179 global $DIC;
180
181 $query = 'SELECT * FROM ' . self::$settingsTable .
182 ' WHERE instance_id = ' . $DIC->database()->quote($this->config_id, 'integer');
183 if (($row = $DIC->database()->fetchAssoc($DIC->database()->query($query))) && $row['client_settings']) {
184 $settings = json_decode($row['client_settings'], true, 512, JSON_THROW_ON_ERROR);
185
186 if (!isset($settings['client']) || !is_string($settings['client']) || $settings['client'] === '') {
187 $settings['client'] = CLIENT_ID;
188 }
189
190 if (is_string($settings['name'] ?? null) && $settings['name']) {
191 $settings['client_name'] = $settings['name'];
192 } else {
193 $settings['client_name'] = CLIENT_ID;
194 }
195
196 if (isset($settings['conversation_idle_state_in_minutes']) && is_numeric($settings['conversation_idle_state_in_minutes'])) {
197 $settings['conversation_idle_state_in_minutes'] = max(1, $settings['conversation_idle_state_in_minutes']);
198 } else {
199 $settings['conversation_idle_state_in_minutes'] = 1;
200 }
201
202 if (!isset($settings['auth']) || !is_array($settings['auth'])) {
203 $settings['auth'] = [];
204 }
205 if (!isset($settings['auth']['key']) || !is_string($settings['auth']['key'])) {
206 $settings['auth']['key'] = '';
207 }
208 if (!isset($settings['auth']['secret']) || !is_string($settings['auth']['secret'])) {
209 $settings['auth']['secret'] = '';
210 }
211
212 return $settings;
213 }
214
215 return [];
216 }
217}
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...
__construct(private readonly int $config_id, private readonly ?stdClass $settings=null)
saveGeneralSettings(stdClass $settings)
Saves given $settings into settingsTable.
Class ilChatroomServerSettings.
const CLIENT_ID
Definition: constants.php:41
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26