ILIAS  Release_4_2_x_branch Revision 61807
 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 
46  public static function _getInstance()
47  {
48  static $udf = null;
49 
50  if(!is_object($udf))
51  {
52  return $udf = new ilUserDefinedFields();
53  }
54  return $udf;
55  }
56 
57  function fetchFieldIdFromImportId($a_import_id)
58  {
59  global $ilSetting;
60 
61  if(!strlen($a_import_id))
62  {
63  return 0;
64  }
65  $parts = explode('_',$a_import_id);
66 
67  if($parts[0] != 'il')
68  {
69  return 0;
70  }
71  if($parts[1] != $ilSetting->get('inst_id',0))
72  {
73  return 0;
74  }
75  if($parts[2] != 'udf')
76  {
77  return 0;
78  }
79  if($parts[3])
80  {
81  // Check if field exists
82  if(is_array($this->definitions["$parts[3]"]))
83  {
84  return $parts[3];
85  }
86  }
87  return 0;
88  }
89  function fetchFieldIdFromName($a_name)
90  {
91  foreach($this->definitions as $definition)
92  {
93  if($definition['field_name'] == $a_name)
94  {
95  return $definition['field_id'];
96  }
97  }
98  return 0;
99  }
100 
101  function getDefinitions()
102  {
103  return $this->definitions ? $this->definitions : array();
104  }
105 
106  function getDefinition($a_id)
107  {
108  return is_array($this->definitions[$a_id]) ? $this->definitions[$a_id] : array();
109  }
110 
112  {
113  foreach($this->definitions as $id => $definition)
114  {
115  if($definition['visible'])
116  {
117  $visible_definition[$id] = $definition;
118  }
119  }
120  return $visible_definition ? $visible_definition : array();
121  }
122 
124  {
125  foreach($this->definitions as $id => $definition)
126  {
127  if($definition['visib_lua'])
128  {
129  $visible_definition[$id] = $definition;
130  }
131  }
132  return $visible_definition ? $visible_definition : array();
133  }
134 
136  {
137  foreach($this->definitions as $id => $definition)
138  {
139  if($definition['changeable_lua'])
140  {
141  $visible_definition[$id] = $definition;
142  }
143  }
144  return $visible_definition ? $visible_definition : array();
145  }
146 
147  public function getRegistrationDefinitions()
148  {
149  foreach($this->definitions as $id => $definition)
150  {
151  if($definition['visib_reg'])
152  {
153  $visible_definition[$id] = $definition;
154  }
155  }
156  return $visible_definition ? $visible_definition : array();
157  }
158 
160  {
161  foreach($this->definitions as $id => $definition)
162  {
163  if($definition['searchable'])
164  {
165  $searchable_definition[$id] = $definition;
166  }
167  }
168  return $searchable_definition ? $searchable_definition : array();
169  }
170 
171  public function getRequiredDefinitions()
172  {
173  foreach($this->definitions as $id => $definition)
174  {
175  if($definition['required'])
176  {
177  $required_definition[$id] = $definition;
178  }
179  }
180  return $required_definition ? $required_definition : array();
181  }
182 
190  public function getCourseExportableFields()
191  {
192  foreach($this->definitions as $id => $definition)
193  {
194  if($definition['course_export'])
195  {
196  $cexp_definition[$id] = $definition;
197  }
198  }
199  return $cexp_definition ? $cexp_definition : array();
200  }
201 
209  public function getGroupExportableFields()
210  {
211  foreach($this->definitions as $id => $definition)
212  {
213  if($definition['group_export'])
214  {
215  $cexp_definition[$id] = $definition;
216  }
217  }
218  return $cexp_definition ? $cexp_definition : array();
219  }
220 
226  public function getExportableFields($a_obj_id)
227  {
228  if(ilObject::_lookupType($a_obj_id) == 'crs')
229  {
230  return $this->getCourseExportableFields();
231  }
232  if(ilObject::_lookupType($a_obj_id) == 'grp')
233  {
234  return $this->getGroupExportableFields();
235  }
236  return array();
237  }
238 
239 
240  function setFieldName($a_name)
241  {
242  $this->field_name = $a_name;
243  }
244  function getFieldName()
245  {
246  return $this->field_name;
247  }
248  function setFieldType($a_type)
249  {
250  $this->field_type = $a_type;
251  }
252  function getFieldType()
253  {
254  return $this->field_type;
255  }
256  function setFieldValues($a_values)
257  {
258  $this->field_values = array();
259  foreach($a_values as $value)
260  {
261  if(strlen($value))
262  {
263  $this->field_values[] = $value;
264  }
265  }
266  }
267  function getFieldValues()
268  {
269  return $this->field_values ? $this->field_values : array();
270  }
271 
272  function enableVisible($a_visible)
273  {
274  $this->field_visible = $a_visible;
275  }
276  function enabledVisible()
277  {
278  return $this->field_visible;
279  }
281  {
282  $this->field_visib_lua = $a_visible;
283  }
285  {
286  return $this->field_visib_lua;
287  }
288  function enableChangeable($a_changeable)
289  {
290  $this->field_changeable = $a_changeable;
291  }
292  function enabledChangeable()
293  {
294  return $this->field_changeable;
295  }
297  {
298  $this->field_changeable_lua = $a_changeable;
299  }
301  {
302  return $this->field_changeable_lua;
303  }
304  function enableRequired($a_required)
305  {
306  $this->field_required = $a_required;
307  }
308  function enabledRequired()
309  {
310  return $this->field_required;
311  }
312  function enableSearchable($a_searchable)
313  {
314  $this->field_searchable = $a_searchable;
315  }
316  function enabledSearchable()
317  {
318  return $this->field_searchable;
319  }
320  function enableExport($a_export)
321  {
322  $this->field_export = $a_export;
323  }
324  function enabledExport()
325  {
326  return $this->field_export;
327  }
328  function enableCourseExport($a_course_export)
329  {
330  $this->field_course_export = $a_course_export;
331  }
333  {
334  return $this->field_course_export;
335  }
336  function enableGroupExport($a_group_export)
337  {
338  $this->field_group_export = $a_group_export;
339  }
341  {
342  return $this->field_group_export;
343  }
344 
345  public function enableVisibleRegistration($a_visible_registration)
346  {
347  $this->field_visible_registration = $a_visible_registration;
348  }
349  public function enabledVisibleRegistration()
350  {
352  }
353 
354  function fieldValuesToSelectArray($a_values)
355  {
356  foreach($a_values as $value)
357  {
358  $values[$value] = $value;
359  }
360  return $values ? $values : array();
361  }
362 
363  function validateValues()
364  {
365  $number = 0;
366  $unique = array();
367  foreach($this->getFieldValues() as $value)
368  {
369  if(!strlen($value))
370  {
371  continue;
372  }
373  $number++;
374  $unique[$value] = $value;
375  }
376 
377  if(!count($unique))
378  {
379  return UDF_NO_VALUES;
380  }
381  if($number != count($unique))
382  {
383  return UDF_DUPLICATE_VALUES;
384  }
385  return 0;
386  }
387 
388  function nameExists($a_field_name)
389  {
390  global $ilDB;
391 
392  $query = "SELECT * FROM udf_definition ".
393  "WHERE field_name = ".$this->db->quote($a_field_name,'text')." ";
394  $res = $ilDB->query($query);
395 
396  return (bool) $res->numRows();
397  }
398 
399  function add()
400  {
401  global $ilDB;
402 
403  // Add definition entry
404  $next_id = $ilDB->nextId('udf_definition');
405 
406  $values = array(
407  'field_id' => array('integer',$next_id),
408  'field_name' => array('text',$this->getFieldName()),
409  'field_type' => array('integer', (int) $this->getFieldType()),
410  'field_values' => array('clob',serialize($this->getFieldValues())),
411  'visible' => array('integer', (int) $this->enabledVisible()),
412  'changeable' => array('integer', (int) $this->enabledChangeable()),
413  'required' => array('integer', (int) $this->enabledRequired()),
414  'searchable' => array('integer', (int) $this->enabledSearchable()),
415  'export' => array('integer', (int) $this->enabledExport()),
416  'course_export' => array('integer', (int) $this->enabledCourseExport()),
417  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
418  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
419  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
420  'group_export' => array('integer', (int) $this->enabledGroupExport())
421  );
422 
423  $ilDB->insert('udf_definition',$values);
424 
425  // add table field in usr_defined_data
426  $field_id = $next_id;
427 
428 
429  $this->__read();
430 
431  return true;
432  }
433  function delete($a_id)
434  {
435  global $ilDB;
436 
437  // Delete definitions
438  $query = "DELETE FROM udf_definition ".
439  "WHERE field_id = ".$this->db->quote($a_id,'integer')." ";
440  $res = $ilDB->manipulate($query);
441 
442  // Delete usr_data entries
443 // $ilDB->dropTableColumn('udf_data','f_'.$a_id);
444  include_once("./Services/User/classes/class.ilUserDefinedData.php");
446 
447  $this->__read();
448 
449  return true;
450  }
451 
452  function update($a_id)
453  {
454  global $ilDB;
455 
456  $values = array(
457  'field_name' => array('text',$this->getFieldName()),
458  'field_type' => array('integer', (int) $this->getFieldType()),
459  'field_values' => array('clob',serialize($this->getFieldValues())),
460  'visible' => array('integer', (int) $this->enabledVisible()),
461  'changeable' => array('integer', (int) $this->enabledChangeable()),
462  'required' => array('integer', (int) $this->enabledRequired()),
463  'searchable' => array('integer', (int) $this->enabledSearchable()),
464  'export' => array('integer', (int) $this->enabledExport()),
465  'course_export' => array('integer', (int) $this->enabledCourseExport()),
466  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
467  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
468  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
469  'group_export' => array('integer', (int) $this->enabledGroupExport())
470  );
471  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_id)));
472  $this->__read();
473 
474  return true;
475  }
476 
477 
478 
479  // Private
480  function __read()
481  {
482  global $ilSetting;
483 
484  $query = "SELECT * FROM udf_definition ";
485  $res = $this->db->query($query);
486 
487  $this->definitions = array();
488  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
489  {
490  $this->definitions[$row->field_id]['field_id'] = $row->field_id;
491  $this->definitions[$row->field_id]['field_name'] = $row->field_name;
492  $this->definitions[$row->field_id]['field_type'] = $row->field_type;
493  $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
494 
495  $tmp = (array) unserialize($row->field_values);
496  sort($tmp);
497  $this->definitions[$row->field_id]['field_values'] = $tmp;
498 
499  $this->definitions[$row->field_id]['visible'] = $row->visible;
500  $this->definitions[$row->field_id]['changeable'] = $row->changeable;
501  $this->definitions[$row->field_id]['required'] = $row->required;
502  $this->definitions[$row->field_id]['searchable'] = $row->searchable;
503  $this->definitions[$row->field_id]['export'] = $row->export;
504  $this->definitions[$row->field_id]['course_export'] = $row->course_export;
505  $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
506  $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
507  $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
508  $this->definitions[$row->field_id]['group_export'] = $row->group_export;
509  }
510 
511  return true;
512  }
513 
514 
515  function deleteValue($a_field_id,$a_value_id)
516  {
517  global $ilDB;
518 
519  $definition = $this->getDefinition($a_field_id);
520 
521  $counter = 0;
522  $new_values = array();
523  foreach($definition['field_values'] as $value)
524  {
525  if($counter++ != $a_value_id)
526  {
527  $new_values[] = $value;
528  }
529  else
530  {
531  $old_value = $value;
532  }
533  }
534 
535  $values = array(
536  'field_values' => array('clob',serialize($new_values)));
537  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_field_id)));
538 
539 
540  // sets value to '' where old value is $old_value
541  include_once("./Services/User/classes/class.ilUserDefinedData.php");
542  ilUserDefinedData::deleteFieldValue($a_field_id, $old_value);
543 
544  // fianally read data
545  $this->__read();
546 
547  return true;
548  }
549 
550  function toXML()
551  {
552  include_once './Services/Xml/classes/class.ilXmlWriter.php';
553  $xml_writer = new ilXmlWriter();
554 
555  $this->addToXML ($xml_writer);
556 
557  return $xml_writer->xmlDumpMem(false);
558  }
559 
564  function addToXML($xml_writer)
565  {
566  $xml_writer->xmlStartTag ("UDFDefinitions");
567  foreach($this->getDefinitions() as $definition)
568  {
569  $attributes = array(
570  "Id" => $definition ["il_id"],
571  "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
572  "Visible" => $definition["visible"]? "TRUE" : "FALSE",
573  "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
574  "Required" => $definition["required"]? "TRUE" : "FALSE",
575  "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
576  "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
577  "GroupExport" => $definition["group_export"]? "TRUE" : "FALSE",
578  "Export" => $definition["export"]? "TRUE" : "FALSE",
579  "RegistrationVisible" => $definition["visib_reg"]? "TRUE" : "FALSE",
580  "LocalUserAdministrationVisible" => $definition["visib_lua"]? "TRUE" : "FALSE",
581  "LocalUserAdministrationChangeable" => $definition["changeable_lua"]? "TRUE" : "FALSE",
582 
583  );
584  $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
585  $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
586  if ($definition["field_type"] == UDF_TYPE_SELECT ) {
587  $field_values = $definition["field_values"];
588  foreach ($field_values as $field_value)
589  {
590  $xml_writer->xmlElement('UDFValue', null, $field_value);
591  }
592  }
593  $xml_writer->xmlEndTag ("UDFDefinition");
594  }
595  $xml_writer->xmlEndTag ("UDFDefinitions");
596 
597  }
598 
599 
600  function _newInstance()
601  {
602  static $udf = null;
603 
604  return $udf = new ilUserDefinedFields();
605  }
606 
607 }
608 ?>