ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 = array(); // 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 = array();
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 }
hasStoredEntry()
Check if entry exist.
$res
Definition: ltiservices.php:69
const IL_CAL_DATETIME
static deleteAllForId(string $a_id)
Delete for id.
exportToForm(ilPropertyFormGUI $a_form, bool $a_set_post=false)
Export settings to form.
__construct(string $a_id, ?int $a_user_id=null)
getValue(string $a_option)
Get value.
static deleteAllForUser(int $a_user_id)
Delete all settings for user id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
importFromForm(ilPropertyFormGUI $a_form)
Import settings from form.
global $DIC
Definition: feed.php:28
setValue(string $a_option, $a_value)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$query
deleteValue(string $a_option)
Delete value.
valueExists(string $a_option)
Does value exist in settings?
$ilUser
Definition: imgupload.php:34
static deleteAllForPrefix(string $a_prefix)
Delete all entries for prefix.
enabled(string $a_option)
Check if a specific option is enabled.