ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilUserFormSettings.php
Go to the documentation of this file.
1<?php
2
23{
24 protected ilDBInterface $db;
25 protected int $user_id;
26 protected string $id;
27 protected array $settings = []; // Missing array type.
28 private bool $has_stored_entry = false;
29
30 public function __construct(
31 string $a_id,
32 ?int $a_user_id = null
33 ) {
34 global $DIC;
35
36 $ilDB = $DIC['ilDB'];
37 $ilUser = $DIC['ilUser'];
38
39 $this->user_id = (int) $a_user_id;
40 $this->id = $a_id;
41 $this->db = $ilDB;
42
43 if (!$this->user_id) {
44 $this->user_id = $ilUser->getId();
45 }
46
47 $this->read();
48 }
49
53 public function hasStoredEntry(): bool
54 {
56 }
57
61 public function set(array $a_data): void // Missing array type.
62 {
63 $this->settings = $a_data;
64 }
65
66 public function reset(): void
67 {
68 $this->settings = [];
69 }
70
74 public function enabled(string $a_option): bool
75 {
76 return (bool) $this->getValue($a_option);
77 }
78
83 public function getValue(string $a_option)
84 {
85 if ($this->valueExists($a_option)) {
86 return $this->settings[$a_option];
87 }
88 return null;
89 }
90
94 public function setValue(string $a_option, $a_value): void
95 {
96 $this->settings[$a_option] = $a_value;
97 }
98
102 public function deleteValue(string $a_option): void
103 {
104 if ($this->valueExists($a_option)) {
105 unset($this->settings[$a_option]);
106 }
107 }
108
112 public function valueExists(string $a_option): bool
113 {
114 return array_key_exists($a_option, $this->settings);
115 }
116
117 public function store(): void
118 {
119 $this->delete(false);
120
121 $query = "INSERT INTO usr_form_settings (user_id,id,settings) " .
122 "VALUES( " .
123 $this->db->quote($this->user_id, 'integer') . ", " .
124 $this->db->quote($this->id, 'text') . ", " .
125 $this->db->quote(serialize($this->settings), 'text') . " " .
126 ")";
127 $this->db->manipulate($query);
128 }
129
130 protected function read(): void
131 {
132 $query = "SELECT * FROM usr_form_settings" .
133 " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
134 " AND id = " . $this->db->quote($this->id, 'text');
135 $res = $this->db->query($query);
136
137 if ($res->numRows()) {
138 $this->has_stored_entry = true;
139 }
140
141 $this->reset();
142 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
143 $this->settings = unserialize($row->settings, ['allowed_classes' => false]);
144 }
145 }
146
150 public function delete(bool $a_reset = true): void
151 {
152 $query = "DELETE FROM usr_form_settings" .
153 " WHERE user_id = " . $this->db->quote($this->user_id, 'integer') .
154 " AND id = " . $this->db->quote($this->id, 'text');
155 $this->db->manipulate($query);
156
157 if ($a_reset) {
158 $this->reset();
159 }
160 }
161
165 public static function deleteAllForUser(int $a_user_id): void
166 {
167 global $DIC;
168
169 $ilDB = $DIC['ilDB'];
170 $query = "DELETE FROM usr_form_settings" .
171 " WHERE user_id = " . $ilDB->quote($a_user_id, 'integer');
172 $ilDB->manipulate($query);
173 }
174
178 public static function deleteAllForId(string $a_id): void
179 {
180 $query = "DELETE FROM usr_form_settings" .
181 " WHERE id = " . $GLOBALS['DIC']['ilDB']->quote($a_id, 'text');
182 $GLOBALS['DIC']['ilDB']->manipulate($query);
183 }
184
188 public static function deleteAllForPrefix(string $a_prefix): void
189 {
190 $query = "DELETE FROM usr_form_settings " .
191 'WHERE ' . $GLOBALS['DIC']['ilDB']->like('id', 'text', $a_prefix . '%');
192
193 $GLOBALS['DIC']['ilDB']->manipulate($query);
194 }
195
199 public function importFromForm(ilPropertyFormGUI $a_form): void
200 {
201 $this->reset();
202 $value = null;
203
204 foreach ($a_form->getItems() as $item) {
205 if (method_exists($item, "getPostVar")) {
206 $field = $item->getPostVar();
207
208 if (method_exists($item, "getDate")) {
209 $value = $item->getDate();
210 if ($value && !$value->isNull()) {
211 $value = $value->get(IL_CAL_DATETIME);
212 }
213 } elseif (method_exists($item, "getChecked")) {
214 $value = $item->getChecked();
215 } elseif (method_exists($item, "getMulti") && $item->getMulti()) {
216 $value = $item->getMultiValues();
217 } elseif (method_exists($item, "getValue")) {
218 $value = $item->getValue();
219 }
220
221 $this->setValue($field, $value);
222 }
223 }
224 }
225
229 public function exportToForm(
230 ilPropertyFormGUI $a_form,
231 bool $a_set_post = false
232 ): void {
233 foreach ($a_form->getItems() as $item) {
234 if (method_exists($item, "getPostVar")) {
235 $field = $item->getPostVar();
236
237 if ($this->valueExists($field)) {
238 $value = $this->getValue($field);
239
240 if (method_exists($item, "setDate")) {
241 $date = new ilDateTime($value, IL_CAL_DATETIME);
242 $item->setDate($date);
243 } elseif (method_exists($item, "setChecked")) {
244 $item->setChecked((bool) $value);
245 } elseif (method_exists($item, "setValue")) {
246 $item->setValue($value);
247 }
248 }
249 }
250 }
251 }
252}
const IL_CAL_DATETIME
@classDescription Date and time handling
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static deleteAllForPrefix(string $a_prefix)
Delete all entries for prefix.
valueExists(string $a_option)
Does value exist in settings?
setValue(string $a_option, $a_value)
__construct(string $a_id, ?int $a_user_id=null)
exportToForm(ilPropertyFormGUI $a_form, bool $a_set_post=false)
Export settings to form.
deleteValue(string $a_option)
Delete value.
getValue(string $a_option)
Get value.
hasStoredEntry()
Check if entry exist.
static deleteAllForUser(int $a_user_id)
Delete all settings for user id.
importFromForm(ilPropertyFormGUI $a_form)
Import settings from form.
static deleteAllForId(string $a_id)
Delete for id.
enabled(string $a_option)
Check if a specific option is enabled.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54