ILIAS  Release_4_2_x_branch Revision 61807
 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 = "DELETE FROM settings WHERE module = ".$ilDB->quote($this->module, "text").
160  " AND ".$ilDB->like("keyword", "text", $a_like);
161  $ilDB->manipulate($query);
162 
163  $this->read();
164  return true;
165  }
166 
173  function delete($a_keyword)
174  {
175  global $ilDB;
176 
177  $st = $ilDB->manipulate("DELETE FROM settings WHERE keyword = ".
178  $ilDB->quote($a_keyword, "text")." AND module = ".
179  $ilDB->quote($this->module, "text"));
180 
181  unset($this->setting[$a_keyword]);
182 
183  return true;
184  }
185 
186 
187 
193  function getAll()
194  {
195  return $this->setting;
196  }
197 
205  function set($a_key, $a_val)
206  {
207  global $lng, $ilDB;
208 
209  $this->delete($a_key);
210 
211  if (!isset(self::$value_type))
212  {
213  self::$value_type = self::_getValueType();
214  }
215 
216  if (self::$value_type == 'text' and strlen($a_val) >= 4000)
217  {
218  ilUtil::sendFailure($lng->txt('setting_value_truncated'), true);
219  $a_val = substr($a_val, 0, 4000);
220  }
221 
222  $ilDB->insert("settings", array(
223  "module" => array("text", $this->module),
224  "keyword" => array("text", $a_key),
225  "value" => array(self::$value_type, $a_val)));
226 
227  $this->setting[$a_key] = $a_val;
228 
229  return true;
230  }
231 
232  function setScormDebug($a_key, $a_val)
233  {
234  global $ilDB;
235  if ($a_val != "1") {
236  $ilDB->query("UPDATE sahs_lm SET debug = 'n'");
237  }
238  $setreturn = ilSetting::set($a_key, $a_val);
239  return $setreturn;
240  }
241 
242  public static function _lookupValue($a_module, $a_keyword)
243  {
244  global $ilDB;
245 
246  $query = "SELECT value FROM settings WHERE module = %s AND keyword = %s";
247  $res = $ilDB->queryF($query, array('text', 'text'), array($a_module, $a_keyword));
248  $data = $ilDB->fetchAssoc($res);
249  return $data['value'];
250  }
251 
257  public static function _getValueType()
258  {
259  include_once ('./Services/Database/classes/class.ilDBAnalyzer.php');
260  $analyzer = new ilDBAnalyzer;
261  $info = $analyzer->getFieldInformation('settings');
262 
263  if ($info['value']['type'] == 'clob')
264  {
265  return 'clob';
266  }
267  else
268  {
269  return 'text';
270  }
271  }
272 
273 
280  public static function _changeValueType($a_new_type = 'text')
281  {
282  global $ilDB;
283 
284  $old_type = self::_getValueType();
285 
286  if ($a_new_type == $old_type)
287  {
288  return false;
289  }
290  elseif ($a_new_type == 'clob')
291  {
292  $ilDB->addTableColumn('settings','value2',
293  array( "type" => "clob",
294  "notnull" => false,
295  "default" => NULL));
296 
297  $ilDB->query("UPDATE settings SET value2 = value");
298  $ilDB->dropTableColumn('settings','value');
299  $ilDB->renameTableColumn('settings','value2','value');
300 
301  return true;
302  }
303  elseif ($a_new_type == 'text')
304  {
305  $ilDB->addTableColumn('settings','value2',
306  array( "type" => "text",
307  "length" => 4000,
308  "notnull" => false,
309  "default" => NULL));
310 
311  $ilDB->query("UPDATE settings SET value2 = value");
312  $ilDB->dropTableColumn('settings','value');
313  $ilDB->renameTableColumn('settings','value2','value');
314 
315  return true;
316  }
317  else
318  {
319  return false;
320  }
321  }
322 
323 
330  public static function _getLongerSettings($a_limit = '4000')
331  {
332  global $ilDB;
333 
334  $settings = array();
335 
336  $query = "SELECT * FROM settings WHERE LENGTH(value) > "
337  . $ilDB->quote($a_limit, 'integer');
338 
339  $result = $ilDB->query($query);
340 
341  while ($row = $ilDB->fetchAssoc($result))
342  {
343  $settings[] = $row;
344  }
345 
346  return $settings;
347  }
348 }
349 ?>