ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSetting.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 
24 
32 class ilSetting
33 {
39  private static $settings_cache = array();
40 
46  private static $value_type = NULL;
47 
48 
49  var $setting = array();
50  var $module = "";
51 
55  function ilSetting($a_module = "common", $a_disabled_cache = false)
56  {
57  global $ilDB;
58 
59  $this->cache_disabled = $a_disabled_cache;
60  $this->module = $a_module;
61  // check whether ini file object exists
62  if (!is_object($ilDB))
63  {
64  die ("Fatal Error: ilSettings object instantiated without DB initialisation.");
65  }
66  $this->read();
67  }
68 
72  function read()
73  {
74  global $ilDB;
75 
76  // get the settings from the cache if they exist.
77  // The setting array of the class is a reference to the cache.
78  // So changing settings in one instance will change them in all.
79  // This is the same behaviour as if the are read from the DB.
80  if (!$this->cache_disabled)
81  {
82  if (isset(self::$settings_cache[$this->module]))
83  {
84  $this->setting =& self::$settings_cache[$this->module];
85  return;
86  }
87  else
88  {
89  $this->setting = array();
90  self::$settings_cache[$this->module] =& $this->setting;
91  }
92  }
93 
94  $query = "SELECT * FROM settings WHERE module=".$ilDB->quote($this->module, "text");
95  $res = $ilDB->query($query);
96 
97  while ($row = $ilDB->fetchAssoc($res))
98  {
99  $this->setting[$row["keyword"]] = $row["value"];
100  }
101 
102  }
103 
114  function get($a_keyword, $a_default_value = false)
115  {
116  if ($a_keyword == "ilias_version")
117  {
118  return ILIAS_VERSION;
119  }
120 
121  if (isset($this->setting[$a_keyword]))
122  {
123  return $this->setting[$a_keyword];
124  }
125  else
126  {
127  return $a_default_value;
128  }
129  }
130 
137  public function deleteAll()
138  {
139  global $ilDB;
140 
141  $query = "DELETE FROM settings WHERE module = ".$ilDB->quote($this->module, "text");
142  $ilDB->manipulate($query);
143 
144  $this->setting = array();
145 
146  return true;
147  }
148 
155  public function deleteLike($a_like)
156  {
157  global $ilDB;
158 
159  $query = "SELECT keyword FROM settings".
160  " WHERE module = ".$ilDB->quote($this->module, "text").
161  " AND ".$ilDB->like("keyword", "text", $a_like);
162  $res = $ilDB->query($query);
163  while($row = $ilDB->fetchAssoc($res))
164  {
165  $this->delete($row["keyword"]);
166  }
167 
168  return true;
169  }
170 
177  function delete($a_keyword)
178  {
179  global $ilDB;
180 
181  $st = $ilDB->manipulate("DELETE FROM settings WHERE keyword = ".
182  $ilDB->quote($a_keyword, "text")." AND module = ".
183  $ilDB->quote($this->module, "text"));
184 
185  unset($this->setting[$a_keyword]);
186 
187  return true;
188  }
189 
190 
191 
197  function getAll()
198  {
199  return $this->setting;
200  }
201 
209  function set($a_key, $a_val)
210  {
211  global $lng, $ilDB;
212 
213  $this->delete($a_key);
214 
215  if (!isset(self::$value_type))
216  {
217  self::$value_type = self::_getValueType();
218  }
219 
220  if (self::$value_type == 'text' and strlen($a_val) >= 4000)
221  {
222  ilUtil::sendFailure($lng->txt('setting_value_truncated'), true);
223  $a_val = substr($a_val, 0, 4000);
224  }
225 
226  $ilDB->insert("settings", array(
227  "module" => array("text", $this->module),
228  "keyword" => array("text", $a_key),
229  "value" => array(self::$value_type, $a_val)));
230 
231  $this->setting[$a_key] = $a_val;
232 
233  return true;
234  }
235 
236  function setScormDebug($a_key, $a_val)
237  {
238  global $ilDB;
239  if ($a_val != "1") {
240  $ilDB->query("UPDATE sahs_lm SET debug = 'n'");
241  }
242  $setreturn = ilSetting::set($a_key, $a_val);
243  return $setreturn;
244  }
245 
246  public static function _lookupValue($a_module, $a_keyword)
247  {
248  global $ilDB;
249 
250  $query = "SELECT value FROM settings WHERE module = %s AND keyword = %s";
251  $res = $ilDB->queryF($query, array('text', 'text'), array($a_module, $a_keyword));
252  $data = $ilDB->fetchAssoc($res);
253  return $data['value'];
254  }
255 
261  public static function _getValueType()
262  {
263  include_once ('./Services/Database/classes/class.ilDBAnalyzer.php');
264  $analyzer = new ilDBAnalyzer;
265  $info = $analyzer->getFieldInformation('settings');
266 
267  if ($info['value']['type'] == 'clob')
268  {
269  return 'clob';
270  }
271  else
272  {
273  return 'text';
274  }
275  }
276 
277 
284  public static function _changeValueType($a_new_type = 'text')
285  {
286  global $ilDB;
287 
288  $old_type = self::_getValueType();
289 
290  if ($a_new_type == $old_type)
291  {
292  return false;
293  }
294  elseif ($a_new_type == 'clob')
295  {
296  $ilDB->addTableColumn('settings','value2',
297  array( "type" => "clob",
298  "notnull" => false,
299  "default" => NULL));
300 
301  $ilDB->query("UPDATE settings SET value2 = value");
302  $ilDB->dropTableColumn('settings','value');
303  $ilDB->renameTableColumn('settings','value2','value');
304 
305  return true;
306  }
307  elseif ($a_new_type == 'text')
308  {
309  $ilDB->addTableColumn('settings','value2',
310  array( "type" => "text",
311  "length" => 4000,
312  "notnull" => false,
313  "default" => NULL));
314 
315  $ilDB->query("UPDATE settings SET value2 = value");
316  $ilDB->dropTableColumn('settings','value');
317  $ilDB->renameTableColumn('settings','value2','value');
318 
319  return true;
320  }
321  else
322  {
323  return false;
324  }
325  }
326 
327 
334  public static function _getLongerSettings($a_limit = '4000')
335  {
336  global $ilDB;
337 
338  $settings = array();
339 
340  $query = "SELECT * FROM settings WHERE LENGTH(value) > "
341  . $ilDB->quote($a_limit, 'integer');
342 
343  $result = $ilDB->query($query);
344 
345  while ($row = $ilDB->fetchAssoc($result))
346  {
347  $settings[] = $row;
348  }
349 
350  return $settings;
351  }
352 }
353 ?>