ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4define('UDF_TYPE_TEXT',1);
5define('UDF_TYPE_SELECT',2);
6define('UDF_TYPE_WYSIWYG',3);
7define('UDF_NO_VALUES',1);
8define('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
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
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 }
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 }
309 {
310 return $this->field_required;
311 }
312 function enableSearchable($a_searchable)
313 {
314 $this->field_searchable = $a_searchable;
315 }
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 }
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 {
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 // #16953
507 $tmp = $sort = array();
508 $is_numeric = true;
509 foreach((array) unserialize($row->field_values) as $item)
510 {
511 if(!is_numeric($item))
512 {
513 $is_numeric = false;
514 }
515 $sort[] = array("value"=>$item);
516 }
517 foreach(ilUtil::sortArray($sort, "value", "asc", $is_numeric) as $item)
518 {
519 $tmp[] = $item["value"];
520 }
521
522 $this->definitions[$row->field_id]['field_values'] = $tmp;
523 $this->definitions[$row->field_id]['visible'] = $row->visible;
524 $this->definitions[$row->field_id]['changeable'] = $row->changeable;
525 $this->definitions[$row->field_id]['required'] = $row->required;
526 $this->definitions[$row->field_id]['searchable'] = $row->searchable;
527 $this->definitions[$row->field_id]['export'] = $row->export;
528 $this->definitions[$row->field_id]['course_export'] = $row->course_export;
529 $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
530 $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
531 $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
532 $this->definitions[$row->field_id]['group_export'] = $row->group_export;
533 // fraunhpatch start
534 $this->definitions[$row->field_id]['certificate'] = $row->certificate;
535 // fraunhpatch end
536 }
537
538 return true;
539 }
540
541
542 function deleteValue($a_field_id,$a_value_id)
543 {
544 global $ilDB;
545
546 $definition = $this->getDefinition($a_field_id);
547
548 $counter = 0;
549 $new_values = array();
550 foreach($definition['field_values'] as $value)
551 {
552 if($counter++ != $a_value_id)
553 {
554 $new_values[] = $value;
555 }
556 else
557 {
558 $old_value = $value;
559 }
560 }
561
562 $values = array(
563 'field_values' => array('clob',serialize($new_values)));
564 $ilDB->update('udf_definition',$values,array('field_id' => array('integer',$a_field_id)));
565
566
567 // sets value to '' where old value is $old_value
568 include_once("./Services/User/classes/class.ilUserDefinedData.php");
569 ilUserDefinedData::deleteFieldValue($a_field_id, $old_value);
570
571 // fianally read data
572 $this->__read();
573
574 return true;
575 }
576
577 function toXML()
578 {
579 include_once './Services/Xml/classes/class.ilXmlWriter.php';
580 $xml_writer = new ilXmlWriter();
581
582 $this->addToXML ($xml_writer);
583
584 return $xml_writer->xmlDumpMem(false);
585 }
586
591 function addToXML($xml_writer)
592 {
593 $xml_writer->xmlStartTag ("UDFDefinitions");
594 foreach($this->getDefinitions() as $definition)
595 {
596 $attributes = array(
597 "Id" => $definition ["il_id"],
598 "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
599 "Visible" => $definition["visible"]? "TRUE" : "FALSE",
600 "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
601 "Required" => $definition["required"]? "TRUE" : "FALSE",
602 "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
603 "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
604 "GroupExport" => $definition["group_export"]? "TRUE" : "FALSE",
605 "Certificate" => $definition["certificate"]? "TRUE" : "FALSE",
606 "Export" => $definition["export"]? "TRUE" : "FALSE",
607 "RegistrationVisible" => $definition["visib_reg"]? "TRUE" : "FALSE",
608 "LocalUserAdministrationVisible" => $definition["visib_lua"]? "TRUE" : "FALSE",
609 "LocalUserAdministrationChangeable" => $definition["changeable_lua"]? "TRUE" : "FALSE",
610
611 );
612 $xml_writer->xmlStartTag ("UDFDefinition", $attributes);
613 $xml_writer->xmlElement('UDFName', null, $definition['field_name']);
614 if ($definition["field_type"] == UDF_TYPE_SELECT ) {
615 $field_values = $definition["field_values"];
616 foreach ($field_values as $field_value)
617 {
618 $xml_writer->xmlElement('UDFValue', null, $field_value);
619 }
620 }
621 $xml_writer->xmlEndTag ("UDFDefinition");
622 }
623 $xml_writer->xmlEndTag ("UDFDefinitions");
624
625 }
626
627
628 function _newInstance()
629 {
630 static $udf = null;
631
632 return $udf = new ilUserDefinedFields();
633 }
634
635}
636?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
const UDF_TYPE_SELECT
const UDF_DUPLICATE_VALUES
const UDF_NO_VALUES
static _lookupType($a_id, $a_reference=false)
lookup object type
static deleteFieldValue($a_field_id, $a_value)
Delete data of particular value of a (selection) field.
static deleteEntriesOfField($a_field_id)
Delete data of particular field.
Additional user data fields definition.
enableVisibleRegistration($a_visible_registration)
enableVisibleLocalUserAdministration($a_visible)
getExportableFields($a_obj_id)
Get exportable field.
enableCourseExport($a_course_export)
getGroupExportableFields()
get fields visible in groups
addToXML($xml_writer)
add user defined field data to xml (using usr dtd)
static _getInstance()
Get instance.
deleteValue($a_field_id, $a_value_id)
enableChangeableLocalUserAdministration($a_changeable)
__construct()
Constructor is private -> use getInstance These definition are used e.g in User XML import.
static sortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
XML writer class.
global $ilSetting
Definition: privfeed.php:40
global $ilDB