ILIAS  Release_5_0_x_branch Revision 61816
 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  public function getModule()
73  {
74  return $this->module;
75  }
76 
80  function read()
81  {
82  global $ilDB;
83 
84  // get the settings from the cache if they exist.
85  // The setting array of the class is a reference to the cache.
86  // So changing settings in one instance will change them in all.
87  // This is the same behaviour as if the are read from the DB.
88  if (!$this->cache_disabled)
89  {
90  if (isset(self::$settings_cache[$this->module]))
91  {
92  $this->setting =& self::$settings_cache[$this->module];
93  return;
94  }
95  else
96  {
97  $this->setting = array();
98  self::$settings_cache[$this->module] =& $this->setting;
99  }
100  }
101 
102  $query = "SELECT * FROM settings WHERE module=".$ilDB->quote($this->module, "text");
103  $res = $ilDB->query($query);
104 
105  while ($row = $ilDB->fetchAssoc($res))
106  {
107  $this->setting[$row["keyword"]] = $row["value"];
108  }
109 
110  }
111 
122  function get($a_keyword, $a_default_value = false)
123  {
124  if ($a_keyword == "ilias_version")
125  {
126  return ILIAS_VERSION;
127  }
128 
129  if (isset($this->setting[$a_keyword]))
130  {
131  return $this->setting[$a_keyword];
132  }
133  else
134  {
135  return $a_default_value;
136  }
137  }
138 
145  public function deleteAll()
146  {
147  global $ilDB;
148 
149  $query = "DELETE FROM settings WHERE module = ".$ilDB->quote($this->module, "text");
150  $ilDB->manipulate($query);
151 
152  $this->setting = array();
153 
154  return true;
155  }
156 
163  public function deleteLike($a_like)
164  {
165  global $ilDB;
166 
167  $query = "SELECT keyword FROM settings".
168  " WHERE module = ".$ilDB->quote($this->module, "text").
169  " AND ".$ilDB->like("keyword", "text", $a_like);
170  $res = $ilDB->query($query);
171  while($row = $ilDB->fetchAssoc($res))
172  {
173  $this->delete($row["keyword"]);
174  }
175 
176  return true;
177  }
178 
185  function delete($a_keyword)
186  {
187  global $ilDB;
188 
189  $st = $ilDB->manipulate("DELETE FROM settings WHERE keyword = ".
190  $ilDB->quote($a_keyword, "text")." AND module = ".
191  $ilDB->quote($this->module, "text"));
192 
193  unset($this->setting[$a_keyword]);
194 
195  return true;
196  }
197 
198 
199 
205  function getAll()
206  {
207  return $this->setting;
208  }
209 
217  function set($a_key, $a_val)
218  {
219  global $lng, $ilDB;
220 
221  $this->delete($a_key);
222 
223  if (!isset(self::$value_type))
224  {
225  self::$value_type = self::_getValueType();
226  }
227 
228  if (self::$value_type == 'text' and strlen($a_val) >= 4000)
229  {
230  ilUtil::sendFailure($lng->txt('setting_value_truncated'), true);
231  $a_val = substr($a_val, 0, 4000);
232  }
233 
234  $ilDB->insert("settings", array(
235  "module" => array("text", $this->module),
236  "keyword" => array("text", $a_key),
237  "value" => array(self::$value_type, $a_val)));
238 
239  $this->setting[$a_key] = $a_val;
240 
241  return true;
242  }
243 
244  function setScormDebug($a_key, $a_val)
245  {
246  global $ilDB;
247  if ($a_val != "1") {
248  $ilDB->query("UPDATE sahs_lm SET debug = 'n'");
249  }
250  $setreturn = ilSetting::set($a_key, $a_val);
251  return $setreturn;
252  }
253 
254  public static function _lookupValue($a_module, $a_keyword)
255  {
256  global $ilDB;
257 
258  $query = "SELECT value FROM settings WHERE module = %s AND keyword = %s";
259  $res = $ilDB->queryF($query, array('text', 'text'), array($a_module, $a_keyword));
260  $data = $ilDB->fetchAssoc($res);
261  return $data['value'];
262  }
263 
269  public static function _getValueType()
270  {
271  include_once ('./Services/Database/classes/class.ilDBAnalyzer.php');
272  $analyzer = new ilDBAnalyzer;
273  $info = $analyzer->getFieldInformation('settings');
274 
275  if ($info['value']['type'] == 'clob')
276  {
277  return 'clob';
278  }
279  else
280  {
281  return 'text';
282  }
283  }
284 
285 
292  public static function _changeValueType($a_new_type = 'text')
293  {
294  global $ilDB;
295 
296  $old_type = self::_getValueType();
297 
298  if ($a_new_type == $old_type)
299  {
300  return false;
301  }
302  elseif ($a_new_type == 'clob')
303  {
304  $ilDB->addTableColumn('settings','value2',
305  array( "type" => "clob",
306  "notnull" => false,
307  "default" => NULL));
308 
309  $ilDB->query("UPDATE settings SET value2 = value");
310  $ilDB->dropTableColumn('settings','value');
311  $ilDB->renameTableColumn('settings','value2','value');
312 
313  return true;
314  }
315  elseif ($a_new_type == 'text')
316  {
317  $ilDB->addTableColumn('settings','value2',
318  array( "type" => "text",
319  "length" => 4000,
320  "notnull" => false,
321  "default" => NULL));
322 
323  $ilDB->query("UPDATE settings SET value2 = value");
324  $ilDB->dropTableColumn('settings','value');
325  $ilDB->renameTableColumn('settings','value2','value');
326 
327  return true;
328  }
329  else
330  {
331  return false;
332  }
333  }
334 
335 
342  public static function _getLongerSettings($a_limit = '4000')
343  {
344  global $ilDB;
345 
346  $settings = array();
347 
348  $query = "SELECT * FROM settings WHERE LENGTH(value) > "
349  . $ilDB->quote($a_limit, 'integer');
350 
351  $result = $ilDB->query($query);
352 
353  while ($row = $ilDB->fetchAssoc($result))
354  {
355  $settings[] = $row;
356  }
357 
358  return $settings;
359  }
360 }
361 ?>