ILIAS  Release_4_1_x_branch Revision 61804
 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 
178  public function getCourseExportableFields()
179  {
180  foreach($this->definitions as $id => $definition)
181  {
182  if($definition['course_export'])
183  {
184  $cexp_definition[$id] = $definition;
185  }
186  }
187  return $cexp_definition ? $cexp_definition : array();
188  }
189 
197  public function getGroupExportableFields()
198  {
199  foreach($this->definitions as $id => $definition)
200  {
201  if($definition['group_export'])
202  {
203  $cexp_definition[$id] = $definition;
204  }
205  }
206  return $cexp_definition ? $cexp_definition : array();
207  }
208 
214  public function getExportableFields($a_obj_id)
215  {
216  if(ilObject::_lookupType($a_obj_id) == 'crs')
217  {
218  return $this->getCourseExportableFields();
219  }
220  if(ilObject::_lookupType($a_obj_id) == 'grp')
221  {
222  return $this->getGroupExportableFields();
223  }
224  return array();
225  }
226 
227 
228  function setFieldName($a_name)
229  {
230  $this->field_name = $a_name;
231  }
232  function getFieldName()
233  {
234  return $this->field_name;
235  }
236  function setFieldType($a_type)
237  {
238  $this->field_type = $a_type;
239  }
240  function getFieldType()
241  {
242  return $this->field_type;
243  }
244  function setFieldValues($a_values)
245  {
246  $this->field_values = array();
247  foreach($a_values as $value)
248  {
249  if(strlen($value))
250  {
251  $this->field_values[] = $value;
252  }
253  }
254  }
255  function getFieldValues()
256  {
257  return $this->field_values ? $this->field_values : array();
258  }
259 
260  function enableVisible($a_visible)
261  {
262  $this->field_visible = $a_visible;
263  }
264  function enabledVisible()
265  {
266  return $this->field_visible;
267  }
269  {
270  $this->field_visib_lua = $a_visible;
271  }
273  {
274  return $this->field_visib_lua;
275  }
276  function enableChangeable($a_changeable)
277  {
278  $this->field_changeable = $a_changeable;
279  }
280  function enabledChangeable()
281  {
282  return $this->field_changeable;
283  }
285  {
286  $this->field_changeable_lua = $a_changeable;
287  }
289  {
290  return $this->field_changeable_lua;
291  }
292  function enableRequired($a_required)
293  {
294  $this->field_required = $a_required;
295  }
296  function enabledRequired()
297  {
298  return $this->field_required;
299  }
300  function enableSearchable($a_searchable)
301  {
302  $this->field_searchable = $a_searchable;
303  }
304  function enabledSearchable()
305  {
306  return $this->field_searchable;
307  }
308  function enableExport($a_export)
309  {
310  $this->field_export = $a_export;
311  }
312  function enabledExport()
313  {
314  return $this->field_export;
315  }
316  function enableCourseExport($a_course_export)
317  {
318  $this->field_course_export = $a_course_export;
319  }
321  {
322  return $this->field_course_export;
323  }
324  function enableGroupExport($a_group_export)
325  {
326  $this->field_group_export = $a_group_export;
327  }
329  {
330  return $this->field_group_export;
331  }
332 
333  public function enableVisibleRegistration($a_visible_registration)
334  {
335  $this->field_visible_registration = $a_visible_registration;
336  }
337  public function enabledVisibleRegistration()
338  {
340  }
341 
342  function fieldValuesToSelectArray($a_values)
343  {
344  foreach($a_values as $value)
345  {
346  $values[$value] = $value;
347  }
348  return $values ? $values : array();
349  }
350 
351  function validateValues()
352  {
353  $number = 0;
354  $unique = array();
355  foreach($this->getFieldValues() as $value)
356  {
357  if(!strlen($value))
358  {
359  continue;
360  }
361  $number++;
362  $unique[$value] = $value;
363  }
364 
365  if(!count($unique))
366  {
367  return UDF_NO_VALUES;
368  }
369  if($number != count($unique))
370  {
371  return UDF_DUPLICATE_VALUES;
372  }
373  return 0;
374  }
375 
376  function nameExists($a_field_name)
377  {
378  global $ilDB;
379 
380  $query = "SELECT * FROM udf_definition ".
381  "WHERE field_name = ".$this->db->quote($a_field_name,'text')." ";
382  $res = $ilDB->query($query);
383 
384  return (bool) $res->numRows();
385  }
386 
387  function add()
388  {
389  global $ilDB;
390 
391  // Add definition entry
392  $next_id = $ilDB->nextId('udf_definition');
393 
394  $values = array(
395  'field_id' => array('integer',$next_id),
396  'field_name' => array('text',$this->getFieldName()),
397  'field_type' => array('integer', (int) $this->getFieldType()),
398  'field_values' => array('clob',serialize($this->getFieldValues())),
399  'visible' => array('integer', (int) $this->enabledVisible()),
400  'changeable' => array('integer', (int) $this->enabledChangeable()),
401  'required' => array('integer', (int) $this->enabledRequired()),
402  'searchable' => array('integer', (int) $this->enabledSearchable()),
403  'export' => array('integer', (int) $this->enabledExport()),
404  'course_export' => array('integer', (int) $this->enabledCourseExport()),
405  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
406  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
407  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
408  'group_export' => array('integer', (int) $this->enabledGroupExport())
409  );
410 
411  $ilDB->insert('udf_definition',$values);
412 
413  // add table field in usr_defined_data
414  $field_id = $next_id;
415 
416 
417  $this->__read();
418 
419  return true;
420  }
421  function delete($a_id)
422  {
423  global $ilDB;
424 
425  // Delete definitions
426  $query = "DELETE FROM udf_definition ".
427  "WHERE field_id = ".$this->db->quote($a_id,'integer')." ";
428  $res = $ilDB->manipulate($query);
429 
430  // Delete usr_data entries
431 // $ilDB->dropTableColumn('udf_data','f_'.$a_id);
432  include_once("./Services/User/classes/class.ilUserDefinedData.php");
434 
435  $this->__read();
436 
437  return true;
438  }
439 
440  function update($a_id)
441  {
442  global $ilDB;
443 
444  $values = array(
445  'field_name' => array('text',$this->getFieldName()),
446  'field_type' => array('integer', (int) $this->getFieldType()),
447  'field_values' => array('clob',serialize($this->getFieldValues())),
448  'visible' => array('integer', (int) $this->enabledVisible()),
449  'changeable' => array('integer', (int) $this->enabledChangeable()),
450  'required' => array('integer', (int) $this->enabledRequired()),
451  'searchable' => array('integer', (int) $this->enabledSearchable()),
452  'export' => array('integer', (int) $this->enabledExport()),
453  'course_export' => array('integer', (int) $this->enabledCourseExport()),
454  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
455  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
456  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
457  'group_export' => array('integer', (int) $this->enabledGroupExport())
458  );
459  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_id)));
460  $this->__read();
461 
462  return true;
463  }
464 
465 
466 
467  // Private
468  function __read()
469  {
470  global $ilSetting;
471 
472  $query = "SELECT * FROM udf_definition ";
473  $res = $this->db->query($query);
474 
475  $this->definitions = array();
476  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
477  {
478  $this->definitions[$row->field_id]['field_id'] = $row->field_id;
479  $this->definitions[$row->field_id]['field_name'] = $row->field_name;
480  $this->definitions[$row->field_id]['field_type'] = $row->field_type;
481  $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
482 
483  $tmp = (array) unserialize($row->field_values);
484  sort($tmp);
485  $this->definitions[$row->field_id]['field_values'] = $tmp;
486 
487  $this->definitions[$row->field_id]['visible'] = $row->visible;
488  $this->definitions[$row->field_id]['changeable'] = $row->changeable;
489  $this->definitions[$row->field_id]['required'] = $row->required;
490  $this->definitions[$row->field_id]['searchable'] = $row->searchable;
491  $this->definitions[$row->field_id]['export'] = $row->export;
492  $this->definitions[$row->field_id]['course_export'] = $row->course_export;
493  $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
494  $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
495  $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
496  $this->definitions[$row->field_id]['group_export'] = $row->group_export;
497  }
498 
499  return true;
500  }
501 
502 
503  function deleteValue($a_field_id,$a_value_id)
504  {
505  global $ilDB;
506 
507  $definition = $this->getDefinition($a_field_id);
508 
509  $counter = 0;
510  $new_values = array();
511  foreach($definition['field_values'] as $value)
512  {
513  if($counter++ != $a_value_id)
514  {
515  $new_values[] = $value;
516  }
517  else
518  {
519  $old_value = $value;
520  }
521  }
522 
523  $values = array(
524  'field_values' => array('clob',serialize($new_values)));
525  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_field_id)));
526 
527 
528  // sets value to '' where old value is $old_value
529  include_once("./Services/User/classes/class.ilUserDefinedData.php");
530  ilUserDefinedData::deleteFieldValue($a_field_id, $old_value);
531 
532  // fianally read data
533  $this->__read();
534 
535  return true;
536  }
537 
538  function toXML()
539  {
540  include_once './Services/Xml/classes/class.ilXmlWriter.php';
541  $xml_writer = new ilXmlWriter();
542 
543  $this->addToXML ($xml_writer);
544 
545  return $xml_writer->xmlDumpMem(false);
546  }
547 
552  function addToXML($xml_writer)
553  {
554  $xml_writer->xmlStartTag ("UDFDefinitions");
555  foreach($this->getDefinitions() as $definition)
556  {
557  $attributes = array(
558  "Id" => $definition ["il_id"],
559  "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
560  "Visible" => $definition["visible"]? "TRUE" : "FALSE",
561  "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
562  "Required" => $definition["required"]? "TRUE" : "FALSE",
563  "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
564  "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
565  "GroupExport" => $definition["group_export"]? "TRUE" : "FALSE",
566  "Export" => $definition["export"]? "TRUE" : "FALSE",
567  "RegistrationVisible" => $definition["visib_reg"]? "TRUE" : "FALSE",
568  "LocalUserAdministrationVisible" => $definition["visib_lua"]? "TRUE" : "FALSE",
569  "LocalUserAdministrationChangeable" => $definition["changeable_lua"]? "TRUE" : "FALSE",
570 
571  );
572  $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
573  $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
574  if ($definition["field_type"] == UDF_TYPE_SELECT ) {
575  $field_values = $definition["field_values"];
576  foreach ($field_values as $field_value)
577  {
578  $xml_writer->xmlElement('UDFValue', null, $field_value);
579  }
580  }
581  $xml_writer->xmlEndTag ("UDFDefinition");
582  }
583  $xml_writer->xmlEndTag ("UDFDefinitions");
584 
585  }
586 
587 
588  function _newInstance()
589  {
590  static $udf = null;
591 
592  return $udf = new ilUserDefinedFields();
593  }
594 
595 }
596 ?>