ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilUserFormSettings.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
31 {
32  protected $db;
33  protected $user_id;
34  protected $id;
35  protected $settings = array();
36 
43  public function __construct($a_id,$a_user_id = null)
44  {
45  global $ilDB, $ilUser;
46 
47  $this->user_id = (int)$a_user_id;
48  $this->id = (string)$a_id;
49  $this->db = $ilDB;
50 
51  if(!$this->user_id)
52  {
53  $this->user_id = $ilUser->getId();
54  }
55 
56  $this->read();
57  }
58 
64  public function set($a_data)
65  {
66  $this->settings = $a_data;
67  }
68 
72  public function reset()
73  {
74  $this->settings = array();
75  }
76 
83  public function enabled($a_option)
84  {
85  return (bool)$this->getValue($a_option);
86  }
87 
94  public function getValue($a_option)
95  {
96  if($this->valueExists($a_option))
97  {
98  return $this->settings[$a_option];
99  }
100  }
101 
108  public function setValue($a_option,$a_value)
109  {
110  $this->settings[$a_option] = $a_value;
111  }
112 
118  public function deleteValue($a_option)
119  {
120  if($this->valueExists($a_option))
121  {
122  unset($this->settings[$a_option]);
123  }
124  }
125 
132  public function valueExists($a_option)
133  {
134  return array_key_exists($a_option,(array)$this->settings);
135  }
136 
140  public function store()
141  {
142  $this->delete(false);
143 
144  $query = "INSERT INTO usr_form_settings (user_id,id,settings) ".
145  "VALUES( ".
146  $this->db->quote($this->user_id,'integer').", ".
147  $this->db->quote($this->id,'text').", ".
148  $this->db->quote(serialize($this->settings),'text')." ".
149  ")";
150  $this->db->manipulate($query);
151  }
152 
160  protected function read()
161  {
162  $query = "SELECT * FROM usr_form_settings".
163  " WHERE user_id = ".$this->db->quote($this->user_id,'integer').
164  " AND id = ".$this->db->quote($this->id,'text');
165  $res = $this->db->query($query);
166 
167  $this->reset();
168  if($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
169  {
170  $this->settings = unserialize($row->settings);
171  }
172  return true;
173  }
174 
180  public function delete($a_reset = true)
181  {
182  $query = "DELETE FROM usr_form_settings".
183  " WHERE user_id = ".$this->db->quote($this->user_id,'integer').
184  " AND id = ".$this->db->quote($this->id,'text');
185  $this->db->manipulate($query);
186 
187  if($a_reset)
188  {
189  $this->reset();
190  }
191  }
192 
196  public static function deleteAllForUser($a_user_id)
197  {
198  $query = "DELETE FROM usr_form_settings".
199  " WHERE user_id = ".$this->db->quote($a_user_id,'integer');
200  $this->db->manipulate($query);
201  }
202 
208  public function importFromForm(ilPropertyFormGUI $a_form)
209  {
210  $this->reset();
211 
212  foreach($a_form->getItems() as $item)
213  {
214  if(method_exists($item, "getPostVar"))
215  {
216  $field = $item->getPostVar();
217 
218  if(method_exists($item, "getDate"))
219  {
220  $value = $item->getDate();
221  if($value && !$value->isNull())
222  {
223  $value = $value->get(IL_CAL_DATETIME);
224  }
225  }
226  else if(method_exists($item, "getMulti") && $item->getMulti())
227  {
228  $value = $item->getMultiValues();
229  }
230  else if(method_exists($item, "getValue"))
231  {
232  $value = $item->getValue();
233  }
234 
235  $this->setValue($field, $value);
236  }
237  }
238  }
239 
245  public function exportToForm(ilPropertyFormGUI $a_form)
246  {
247  foreach($a_form->getItems() as $item)
248  {
249  if(method_exists($item, "getPostVar"))
250  {
251  $field = $item->getPostVar();
252 
253  if($this->valueExists($field))
254  {
255  $value = $this->getValue($field);
256 
257  if(method_exists($item, "setDate"))
258  {
259  $date = new ilDateTime($value, IL_CAL_DATETIME);
260  $item->setDate($date);
261  }
262  else if(method_exists($item, "setValue"))
263  {
264  $item->setValue($value);
265 
266  if(method_exists($item, "setChecked"))
267  {
268  $item->setChecked(true);
269  }
270  }
271  }
272  }
273  }
274  }
275 }
276 
277 ?>