ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilUserDefinedFields.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 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 
33 define('UDF_TYPE_TEXT',1);
34 define('UDF_TYPE_SELECT',2);
35 define('UDF_NO_VALUES',1);
36 define('UDF_DUPLICATE_VALUES',2);
37 
39 {
40  var $db = null;
41  var $definitions = array();
42 
43 
51  private function __construct()
52  {
53  global $ilDB;
54 
55  $this->db =& $ilDB;
56 
57  $this->__read();
58  }
59 
60  function _getInstance()
61  {
62  static $udf = null;
63 
64  if(!is_object($udf))
65  {
66  return $udf = new ilUserDefinedFields();
67  }
68  return $udf;
69  }
70 
71  function fetchFieldIdFromImportId($a_import_id)
72  {
73  global $ilSetting;
74 
75  if(!strlen($a_import_id))
76  {
77  return 0;
78  }
79  $parts = explode('_',$a_import_id);
80 
81  if($parts[0] != 'il')
82  {
83  return 0;
84  }
85  if($parts[1] != $ilSetting->get('inst_id',0))
86  {
87  return 0;
88  }
89  if($parts[2] != 'udf')
90  {
91  return 0;
92  }
93  if($parts[3])
94  {
95  // Check if field exists
96  if(is_array($this->definitions["$parts[3]"]))
97  {
98  return $parts[3];
99  }
100  }
101  return 0;
102  }
103  function fetchFieldIdFromName($a_name)
104  {
105  foreach($this->definitions as $definition)
106  {
107  if($definition['field_name'] == $a_name)
108  {
109  return $definition['field_id'];
110  }
111  }
112  return 0;
113  }
114 
115  function getDefinitions()
116  {
117  return $this->definitions ? $this->definitions : array();
118  }
119 
120  function getDefinition($a_id)
121  {
122  return is_array($this->definitions[$a_id]) ? $this->definitions[$a_id] : array();
123  }
124 
126  {
127  foreach($this->definitions as $id => $definition)
128  {
129  if($definition['visible'])
130  {
131  $visible_definition[$id] = $definition;
132  }
133  }
134  return $visible_definition ? $visible_definition : array();
135  }
136 
138  {
139  foreach($this->definitions as $id => $definition)
140  {
141  if($definition['searchable'])
142  {
143  $searchable_definition[$id] = $definition;
144  }
145  }
146  return $searchable_definition ? $searchable_definition : array();
147  }
148 
156  public function getCourseExportableFields()
157  {
158  foreach($this->definitions as $id => $definition)
159  {
160  if($definition['course_export'])
161  {
162  $cexp_definition[$id] = $definition;
163  }
164  }
165  return $cexp_definition ? $cexp_definition : array();
166  }
167 
168 
169  function setFieldName($a_name)
170  {
171  $this->field_name = $a_name;
172  }
173  function getFieldName()
174  {
175  return $this->field_name;
176  }
177  function setFieldType($a_type)
178  {
179  $this->field_type = $a_type;
180  }
181  function getFieldType()
182  {
183  return $this->field_type;
184  }
185  function setFieldValues($a_values)
186  {
187  $this->field_values = array();
188  foreach($a_values as $value)
189  {
190  if(strlen($value))
191  {
192  $this->field_values[] = $value;
193  }
194  }
195  }
196  function getFieldValues()
197  {
198  return $this->field_values ? $this->field_values : array();
199  }
200 
201  function enableVisible($a_visible)
202  {
203  $this->field_visible = $a_visible;
204  }
205  function enabledVisible()
206  {
207  return $this->field_visible;
208  }
209  function enableChangeable($a_changeable)
210  {
211  $this->field_changeable = $a_changeable;
212  }
213  function enabledChangeable()
214  {
215  return $this->field_changeable;
216  }
217  function enableRequired($a_required)
218  {
219  $this->field_required = $a_required;
220  }
221  function enabledRequired()
222  {
223  return $this->field_required;
224  }
225  function enableSearchable($a_searchable)
226  {
227  $this->field_searchable = $a_searchable;
228  }
229  function enabledSearchable()
230  {
231  return $this->field_searchable;
232  }
233  function enableExport($a_export)
234  {
235  $this->field_export = $a_export;
236  }
237  function enabledExport()
238  {
239  return $this->field_export;
240  }
241  function enableCourseExport($a_course_export)
242  {
243  $this->field_course_export = $a_course_export;
244  }
246  {
247  return $this->field_course_export;
248  }
249 
250 
251  function fieldValuesToSelectArray($a_values)
252  {
253  foreach($a_values as $value)
254  {
255  $values[$value] = $value;
256  }
257  return $values ? $values : array();
258  }
259 
260  function validateValues()
261  {
262  $number = 0;
263  $unique = array();
264  foreach($this->getFieldValues() as $value)
265  {
266  if(!strlen($value))
267  {
268  continue;
269  }
270  $number++;
271  $unique[$value] = $value;
272  }
273 
274  if(!count($unique))
275  {
276  return UDF_NO_VALUES;
277  }
278  if($number != count($unique))
279  {
280  return UDF_DUPLICATE_VALUES;
281  }
282  return 0;
283  }
284 
285  function nameExists($a_field_name)
286  {
287  $query = "SELECT * FROM user_defined_field_definition ".
288  "WHERE field_name = ".$this->db->quote($a_field_name)." ";
289 
290  $res = $this->db->query($query);
291 
292  return (bool) $res->numRows();
293  }
294 
295  function add()
296  {
297  // Add definition entry
298  $query = "INSERT INTO user_defined_field_definition ".
299  "SET field_name = ".$this->db->quote($this->getFieldName()).", ".
300  "field_type = ".$this->db->quote($this->getFieldType()).", ".
301  "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
302  "visible = '".(int) $this->enabledVisible()."', ".
303  "changeable = '".(int) $this->enabledChangeable()."', ".
304  "required = '".(int) $this->enabledRequired()."', ".
305  "searchable = '".(int) $this->enabledSearchable()."', ".
306  "export = '".(int) $this->enabledExport()."', ".
307  "course_export = '".(int) $this->enabledCourseExport()."'";
308 
309  $this->db->query($query);
310 
311  // add table field in usr_defined_data
312  $field_id = $this->db->getLastInsertId();
313 
314  $query = "ALTER TABLE usr_defined_data ADD `".(int) $field_id."` TEXT NOT NULL";
315  $this->db->query($query);
316 
317  $this->__read();
318 
319  return true;
320  }
321  function delete($a_id)
322  {
323  // Delete definitions
324  $query = "DELETE FROM user_defined_field_definition ".
325  "WHERE field_id = ".$this->db->quote($a_id)." ";
326  $this->db->query($query);
327 
328  // Delete usr_data entries
329  $query = "ALTER TABLE usr_defined_data DROP `".(int) $a_id."`";
330  $this->db->query($query);
331 
332  $this->__read();
333 
334  return true;
335  }
336 
337  function update($a_id)
338  {
339  $query = "UPDATE user_defined_field_definition ".
340  "SET field_name = ".$this->db->quote($this->getFieldName()).", ".
341  "field_type = ".$this->db->quote($this->getFieldType()).", ".
342  "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
343  "visible = '".(int) $this->enabledVisible()."', ".
344  "changeable = '".(int) $this->enabledChangeable()."', ".
345  "required = '".(int) $this->enabledRequired()."', ".
346  "searchable = '".(int) $this->enabledSearchable()."', ".
347  "export = '".(int) $this->enabledExport()."', ".
348  "course_export = '".(int) $this->enabledCourseExport()."' ".
349  "WHERE field_id = ".$this->db->quote($a_id)." ";
350 
351  $this->db->query($query);
352  $this->__read();
353 
354  return true;
355  }
356 
357 
358 
359  // Private
360  function __read()
361  {
362  global $ilSetting;
363 
364  $query = "SELECT * FROM user_defined_field_definition ";
365  $res = $this->db->query($query);
366 
367  $this->definitions = array();
368  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
369  {
370  $this->definitions[$row->field_id]['field_id'] = $row->field_id;
371  $this->definitions[$row->field_id]['field_name'] = $row->field_name;
372  $this->definitions[$row->field_id]['field_type'] = $row->field_type;
373  $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
374 
375  $tmp = unserialize(stripslashes($row->field_values));
376  sort($tmp);
377  $this->definitions[$row->field_id]['field_values'] = $tmp;
378 
379  $this->definitions[$row->field_id]['visible'] = $row->visible;
380  $this->definitions[$row->field_id]['changeable'] = $row->changeable;
381  $this->definitions[$row->field_id]['required'] = $row->required;
382  $this->definitions[$row->field_id]['searchable'] = $row->searchable;
383  $this->definitions[$row->field_id]['export'] = $row->export;
384  $this->definitions[$row->field_id]['course_export'] = $row->course_export;
385 
386  }
387 
388  return true;
389  }
390 
391  function deleteValue($a_field_id,$a_value_id)
392  {
393  $definition = $this->getDefinition($a_field_id);
394 
395  $counter = 0;
396  $new_values = array();
397  foreach($definition['field_values'] as $value)
398  {
399  if($counter++ != $a_value_id)
400  {
401  $new_values[] = $value;
402  }
403  else
404  {
405  $old_value = $value;
406  }
407  }
408  $query = "UPDATE user_defined_field_definition ".
409  "SET field_values = '".addslashes(serialize($new_values))."' ".
410  "WHERE field_id = ".$this->db->quote($a_field_id)."";
411 
412  $this->db->query($query);
413 
414  // Update usr_data
415  $query = "UPDATE usr_defined_data ".
416  "SET `".$this->db->quote($a_field_id)."` = '' ".
417  "WHERE `".$this->db->quote($a_field_id)."` = ".$this->db->quote($old_value)."";
418  $this->db->query($query);
419 
420  // fianally read data
421  $this->__read();
422 
423  return true;
424  }
425 
426  function toXML()
427  {
428  include_once 'classes/class.ilXmlWriter.php';
429  $xml_writer = new ilXmlWriter();
430 
431  $this->addToXML ($xml_writer);
432 
433  return $xml_writer->xmlDumpMem(false);
434  }
435 
440  function addToXML($xml_writer)
441  {
442  $xml_writer->xmlStartTag ("UDFDefinitions");
443  foreach($this->getDefinitions() as $definition)
444  {
445  $attributes = array(
446  "Id" => $definition ["il_id"],
447  "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
448  "Visible" => $definition["visible"]? "TRUE" : "FALSE",
449  "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
450  "Required" => $definition["required"]? "TRUE" : "FALSE",
451  "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
452  "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
453  "Export" => $definition["export"]? "TRUE" : "FALSE",
454  );
455  $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
456  $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
457  if ($definition["field_type"] == UDF_TYPE_SELECT ) {
458  $field_values = $definition["field_values"];
459  foreach ($field_values as $field_value)
460  {
461  $xml_writer->xmlElement('UDFValue', null, $field_value);
462  }
463  }
464  $xml_writer->xmlEndTag ("UDFDefinition");
465  }
466  $xml_writer->xmlEndTag ("UDFDefinitions");
467 
468  }
469 
470 
471  function _newInstance()
472  {
473  static $udf = null;
474 
475  return $udf = new ilUserDefinedFields();
476  }
477 
478 }
479 ?>