ILIAS  Release_5_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 
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  function enableCertificate($a_c)
346  {
347  $this->field_certificate = $a_c;
348  }
350  {
351  return $this->field_certificate;
352  }
353 
354  public function enableVisibleRegistration($a_visible_registration)
355  {
356  $this->field_visible_registration = $a_visible_registration;
357  }
358  public function enabledVisibleRegistration()
359  {
361  }
362 
363  function fieldValuesToSelectArray($a_values)
364  {
365  foreach($a_values as $value)
366  {
367  $values[$value] = $value;
368  }
369  return $values ? $values : array();
370  }
371 
372  function validateValues()
373  {
374  $number = 0;
375  $unique = array();
376  foreach($this->getFieldValues() as $value)
377  {
378  if(!strlen($value))
379  {
380  continue;
381  }
382  $number++;
383  $unique[$value] = $value;
384  }
385 
386  if(!count($unique))
387  {
388  return UDF_NO_VALUES;
389  }
390  if($number != count($unique))
391  {
392  return UDF_DUPLICATE_VALUES;
393  }
394  return 0;
395  }
396 
397  function nameExists($a_field_name)
398  {
399  global $ilDB;
400 
401  $query = "SELECT * FROM udf_definition ".
402  "WHERE field_name = ".$this->db->quote($a_field_name,'text')." ";
403  $res = $ilDB->query($query);
404 
405  return (bool) $res->numRows();
406  }
407 
408  function add()
409  {
410  global $ilDB;
411 
412  // Add definition entry
413  $next_id = $ilDB->nextId('udf_definition');
414 
415  $values = array(
416  'field_id' => array('integer',$next_id),
417  'field_name' => array('text',$this->getFieldName()),
418  'field_type' => array('integer', (int) $this->getFieldType()),
419  'field_values' => array('clob',serialize($this->getFieldValues())),
420  'visible' => array('integer', (int) $this->enabledVisible()),
421  'changeable' => array('integer', (int) $this->enabledChangeable()),
422  'required' => array('integer', (int) $this->enabledRequired()),
423  'searchable' => array('integer', (int) $this->enabledSearchable()),
424  'export' => array('integer', (int) $this->enabledExport()),
425  'course_export' => array('integer', (int) $this->enabledCourseExport()),
426  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
427  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
428  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
429  'group_export' => array('integer', (int) $this->enabledGroupExport()),
430  'certificate' => array('integer', (int) $this->enabledCertificate()),
431  );
432 
433  $ilDB->insert('udf_definition',$values);
434 
435  // add table field in usr_defined_data
436  $field_id = $next_id;
437 
438 
439  $this->__read();
440 
441  return true;
442  }
443  function delete($a_id)
444  {
445  global $ilDB;
446 
447  // Delete definitions
448  $query = "DELETE FROM udf_definition ".
449  "WHERE field_id = ".$this->db->quote($a_id,'integer')." ";
450  $res = $ilDB->manipulate($query);
451 
452  // Delete usr_data entries
453 // $ilDB->dropTableColumn('udf_data','f_'.$a_id);
454  include_once("./Services/User/classes/class.ilUserDefinedData.php");
456 
457  $this->__read();
458 
459  return true;
460  }
461 
462  function update($a_id)
463  {
464  global $ilDB;
465 
466  $values = array(
467  'field_name' => array('text',$this->getFieldName()),
468  'field_type' => array('integer', (int) $this->getFieldType()),
469  'field_values' => array('clob',serialize($this->getFieldValues())),
470  'visible' => array('integer', (int) $this->enabledVisible()),
471  'changeable' => array('integer', (int) $this->enabledChangeable()),
472  'required' => array('integer', (int) $this->enabledRequired()),
473  'searchable' => array('integer', (int) $this->enabledSearchable()),
474  'export' => array('integer', (int) $this->enabledExport()),
475  'course_export' => array('integer', (int) $this->enabledCourseExport()),
476  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
477  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
478  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
479  'group_export' => array('integer', (int) $this->enabledGroupExport()),
480  'certificate' => array('integer', (int) $this->enabledCertificate())
481  );
482  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_id)));
483  $this->__read();
484 
485  return true;
486  }
487 
488 
489 
490  // Private
491  function __read()
492  {
493  global $ilSetting;
494 
495  $query = "SELECT * FROM udf_definition ";
496  $res = $this->db->query($query);
497 
498  $this->definitions = array();
499  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
500  {
501  $this->definitions[$row->field_id]['field_id'] = $row->field_id;
502  $this->definitions[$row->field_id]['field_name'] = $row->field_name;
503  $this->definitions[$row->field_id]['field_type'] = $row->field_type;
504  $this->definitions[$row->field_id]['il_id'] = 'il_'.$ilSetting->get('inst_id',0).'_udf_'.$row->field_id;
505 
506  $tmp = (array) unserialize($row->field_values);
507  sort($tmp);
508  $this->definitions[$row->field_id]['field_values'] = $tmp;
509 
510  $this->definitions[$row->field_id]['visible'] = $row->visible;
511  $this->definitions[$row->field_id]['changeable'] = $row->changeable;
512  $this->definitions[$row->field_id]['required'] = $row->required;
513  $this->definitions[$row->field_id]['searchable'] = $row->searchable;
514  $this->definitions[$row->field_id]['export'] = $row->export;
515  $this->definitions[$row->field_id]['course_export'] = $row->course_export;
516  $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
517  $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
518  $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
519  $this->definitions[$row->field_id]['group_export'] = $row->group_export;
520  // fraunhpatch start
521  $this->definitions[$row->field_id]['certificate'] = $row->certificate;
522  // fraunhpatch end
523  }
524 
525  return true;
526  }
527 
528 
529  function deleteValue($a_field_id,$a_value_id)
530  {
531  global $ilDB;
532 
533  $definition = $this->getDefinition($a_field_id);
534 
535  $counter = 0;
536  $new_values = array();
537  foreach($definition['field_values'] as $value)
538  {
539  if($counter++ != $a_value_id)
540  {
541  $new_values[] = $value;
542  }
543  else
544  {
545  $old_value = $value;
546  }
547  }
548 
549  $values = array(
550  'field_values' => array('clob',serialize($new_values)));
551  $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_field_id)));
552 
553 
554  // sets value to '' where old value is $old_value
555  include_once("./Services/User/classes/class.ilUserDefinedData.php");
556  ilUserDefinedData::deleteFieldValue($a_field_id, $old_value);
557 
558  // fianally read data
559  $this->__read();
560 
561  return true;
562  }
563 
564  function toXML()
565  {
566  include_once './Services/Xml/classes/class.ilXmlWriter.php';
567  $xml_writer = new ilXmlWriter();
568 
569  $this->addToXML ($xml_writer);
570 
571  return $xml_writer->xmlDumpMem(false);
572  }
573 
578  function addToXML($xml_writer)
579  {
580  $xml_writer->xmlStartTag ("UDFDefinitions");
581  foreach($this->getDefinitions() as $definition)
582  {
583  $attributes = array(
584  "Id" => $definition ["il_id"],
585  "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
586  "Visible" => $definition["visible"]? "TRUE" : "FALSE",
587  "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
588  "Required" => $definition["required"]? "TRUE" : "FALSE",
589  "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
590  "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
591  "GroupExport" => $definition["group_export"]? "TRUE" : "FALSE",
592  "Certificate" => $definition["certificate"]? "TRUE" : "FALSE",
593  "Export" => $definition["export"]? "TRUE" : "FALSE",
594  "RegistrationVisible" => $definition["visib_reg"]? "TRUE" : "FALSE",
595  "LocalUserAdministrationVisible" => $definition["visib_lua"]? "TRUE" : "FALSE",
596  "LocalUserAdministrationChangeable" => $definition["changeable_lua"]? "TRUE" : "FALSE",
597 
598  );
599  $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
600  $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
601  if ($definition["field_type"] == UDF_TYPE_SELECT ) {
602  $field_values = $definition["field_values"];
603  foreach ($field_values as $field_value)
604  {
605  $xml_writer->xmlElement('UDFValue', null, $field_value);
606  }
607  }
608  $xml_writer->xmlEndTag ("UDFDefinition");
609  }
610  $xml_writer->xmlEndTag ("UDFDefinitions");
611 
612  }
613 
614 
615  function _newInstance()
616  {
617  static $udf = null;
618 
619  return $udf = new ilUserDefinedFields();
620  }
621 
622 }
623 ?>