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