ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  public $db = null;
22  public $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  return $udf = new ilUserDefinedFields();
52  }
53  return $udf;
54  }
55 
56  public function fetchFieldIdFromImportId($a_import_id)
57  {
58  global $ilSetting;
59 
60  if (!strlen($a_import_id)) {
61  return 0;
62  }
63  $parts = explode('_', $a_import_id);
64 
65  if ($parts[0] != 'il') {
66  return 0;
67  }
68  if ($parts[1] != $ilSetting->get('inst_id', 0)) {
69  return 0;
70  }
71  if ($parts[2] != 'udf') {
72  return 0;
73  }
74  if ($parts[3]) {
75  // Check if field exists
76  if (is_array($this->definitions["$parts[3]"])) {
77  return $parts[3];
78  }
79  }
80  return 0;
81  }
82  public function fetchFieldIdFromName($a_name)
83  {
84  foreach ($this->definitions as $definition) {
85  if ($definition['field_name'] == $a_name) {
86  return $definition['field_id'];
87  }
88  }
89  return 0;
90  }
91 
92  public function getDefinitions()
93  {
94  return $this->definitions ? $this->definitions : array();
95  }
96 
97  public function getDefinition($a_id)
98  {
99  return is_array($this->definitions[$a_id]) ? $this->definitions[$a_id] : array();
100  }
101 
102  public function getVisibleDefinitions()
103  {
104  foreach ($this->definitions as $id => $definition) {
105  if ($definition['visible']) {
106  $visible_definition[$id] = $definition;
107  }
108  }
109  return $visible_definition ? $visible_definition : array();
110  }
111 
113  {
114  foreach ($this->definitions as $id => $definition) {
115  if ($definition['visib_lua']) {
116  $visible_definition[$id] = $definition;
117  }
118  }
119  return $visible_definition ? $visible_definition : array();
120  }
121 
123  {
124  foreach ($this->definitions as $id => $definition) {
125  if ($definition['changeable_lua']) {
126  $visible_definition[$id] = $definition;
127  }
128  }
129  return $visible_definition ? $visible_definition : array();
130  }
131 
132  public function getRegistrationDefinitions()
133  {
134  foreach ($this->definitions as $id => $definition) {
135  if ($definition['visib_reg']) {
136  $visible_definition[$id] = $definition;
137  }
138  }
139  return $visible_definition ? $visible_definition : array();
140  }
141 
142  public function getSearchableDefinitions()
143  {
144  foreach ($this->definitions as $id => $definition) {
145  if ($definition['searchable']) {
146  $searchable_definition[$id] = $definition;
147  }
148  }
149  return $searchable_definition ? $searchable_definition : array();
150  }
151 
152  public function getRequiredDefinitions()
153  {
154  foreach ($this->definitions as $id => $definition) {
155  if ($definition['required']) {
156  $required_definition[$id] = $definition;
157  }
158  }
159  return $required_definition ? $required_definition : array();
160  }
161 
169  public function getCourseExportableFields()
170  {
171  foreach ($this->definitions as $id => $definition) {
172  if ($definition['course_export']) {
173  $cexp_definition[$id] = $definition;
174  }
175  }
176  return $cexp_definition ? $cexp_definition : array();
177  }
178 
186  public function getGroupExportableFields()
187  {
188  foreach ($this->definitions as $id => $definition) {
189  if ($definition['group_export']) {
190  $cexp_definition[$id] = $definition;
191  }
192  }
193  return $cexp_definition ? $cexp_definition : array();
194  }
195 
201  public function getExportableFields($a_obj_id)
202  {
203  if (ilObject::_lookupType($a_obj_id) == 'crs') {
204  return $this->getCourseExportableFields();
205  }
206  if (ilObject::_lookupType($a_obj_id) == 'grp') {
207  return $this->getGroupExportableFields();
208  }
209  return array();
210  }
211 
212 
213  public function setFieldName($a_name)
214  {
215  $this->field_name = $a_name;
216  }
217  public function getFieldName()
218  {
219  return $this->field_name;
220  }
221  public function setFieldType($a_type)
222  {
223  $this->field_type = $a_type;
224  }
225  public function getFieldType()
226  {
227  return $this->field_type;
228  }
229  public function setFieldValues($a_values)
230  {
231  $this->field_values = array();
232  foreach ($a_values as $value) {
233  if (strlen($value)) {
234  $this->field_values[] = $value;
235  }
236  }
237  }
238  public function getFieldValues()
239  {
240  return $this->field_values ? $this->field_values : array();
241  }
242 
243  public function enableVisible($a_visible)
244  {
245  $this->field_visible = $a_visible;
246  }
247  public function enabledVisible()
248  {
249  return $this->field_visible;
250  }
251  public function enableVisibleLocalUserAdministration($a_visible)
252  {
253  $this->field_visib_lua = $a_visible;
254  }
256  {
257  return $this->field_visib_lua;
258  }
259  public function enableChangeable($a_changeable)
260  {
261  $this->field_changeable = $a_changeable;
262  }
263  public function enabledChangeable()
264  {
265  return $this->field_changeable;
266  }
267  public function enableChangeableLocalUserAdministration($a_changeable)
268  {
269  $this->field_changeable_lua = $a_changeable;
270  }
272  {
273  return $this->field_changeable_lua;
274  }
275  public function enableRequired($a_required)
276  {
277  $this->field_required = $a_required;
278  }
279  public function enabledRequired()
280  {
281  return $this->field_required;
282  }
283  public function enableSearchable($a_searchable)
284  {
285  $this->field_searchable = $a_searchable;
286  }
287  public function enabledSearchable()
288  {
289  return $this->field_searchable;
290  }
291  public function enableExport($a_export)
292  {
293  $this->field_export = $a_export;
294  }
295  public function enabledExport()
296  {
297  return $this->field_export;
298  }
299  public function enableCourseExport($a_course_export)
300  {
301  $this->field_course_export = $a_course_export;
302  }
303  public function enabledCourseExport()
304  {
305  return $this->field_course_export;
306  }
307  public function enableGroupExport($a_group_export)
308  {
309  $this->field_group_export = $a_group_export;
310  }
311  public function enabledGroupExport()
312  {
313  return $this->field_group_export;
314  }
315 
316  public function enableCertificate($a_c)
317  {
318  $this->field_certificate = $a_c;
319  }
320  public function enabledCertificate()
321  {
322  return $this->field_certificate;
323  }
324 
325  public function enableVisibleRegistration($a_visible_registration)
326  {
327  $this->field_visible_registration = $a_visible_registration;
328  }
329  public function enabledVisibleRegistration()
330  {
332  }
333 
339  public function fieldValuesToSelectArray($a_values, $a_with_selection_info = true)
340  {
341  global $DIC;
342 
343  $lng = $DIC->language();
344  $values = [];
345  if ($a_with_selection_info) {
346  $values[''] = $lng->txt('please_select');
347  }
348  foreach ($a_values as $value) {
349  $values[$value] = $value;
350  }
351  if (count($values) > (int) $a_with_selection_info) {
352  return $values;
353  }
354  return [];
355  }
356 
357  public function validateValues()
358  {
359  $number = 0;
360  $unique = array();
361  foreach ($this->getFieldValues() as $value) {
362  if (!strlen($value)) {
363  continue;
364  }
365  $number++;
366  $unique[$value] = $value;
367  }
368 
369  if (!count($unique)) {
370  return UDF_NO_VALUES;
371  }
372  if ($number != count($unique)) {
373  return UDF_DUPLICATE_VALUES;
374  }
375  return 0;
376  }
377 
378  public function nameExists($a_field_name)
379  {
380  global $ilDB;
381 
382  $query = "SELECT * FROM udf_definition " .
383  "WHERE field_name = " . $this->db->quote($a_field_name, 'text') . " ";
384  $res = $ilDB->query($query);
385 
386  return (bool) $res->numRows();
387  }
388 
389  public function add()
390  {
391  global $ilDB;
392 
393  // Add definition entry
394  $next_id = $ilDB->nextId('udf_definition');
395 
396  $values = array(
397  'field_id' => array('integer',$next_id),
398  'field_name' => array('text',$this->getFieldName()),
399  'field_type' => array('integer', (int) $this->getFieldType()),
400  'field_values' => array('clob',serialize($this->getFieldValues())),
401  'visible' => array('integer', (int) $this->enabledVisible()),
402  'changeable' => array('integer', (int) $this->enabledChangeable()),
403  'required' => array('integer', (int) $this->enabledRequired()),
404  'searchable' => array('integer', (int) $this->enabledSearchable()),
405  'export' => array('integer', (int) $this->enabledExport()),
406  'course_export' => array('integer', (int) $this->enabledCourseExport()),
407  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
408  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
409  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
410  'group_export' => array('integer', (int) $this->enabledGroupExport()),
411  'certificate' => array('integer', (int) $this->enabledCertificate()),
412  );
413 
414  $ilDB->insert('udf_definition', $values);
415 
416  // add table field in usr_defined_data
417  $field_id = $next_id;
418 
419 
420  $this->__read();
421 
422  return true;
423  }
424  public function delete($a_id)
425  {
426  global $ilDB;
427 
428  // Delete definitions
429  $query = "DELETE FROM udf_definition " .
430  "WHERE field_id = " . $this->db->quote($a_id, 'integer') . " ";
431  $res = $ilDB->manipulate($query);
432 
433  // Delete usr_data entries
434  // $ilDB->dropTableColumn('udf_data','f_'.$a_id);
435  include_once("./Services/User/classes/class.ilUserDefinedData.php");
437 
438  $this->__read();
439 
440  return true;
441  }
442 
443  public function update($a_id)
444  {
445  global $ilDB;
446 
447  $values = array(
448  'field_name' => array('text',$this->getFieldName()),
449  'field_type' => array('integer', (int) $this->getFieldType()),
450  'field_values' => array('clob',serialize($this->getFieldValues())),
451  'visible' => array('integer', (int) $this->enabledVisible()),
452  'changeable' => array('integer', (int) $this->enabledChangeable()),
453  'required' => array('integer', (int) $this->enabledRequired()),
454  'searchable' => array('integer', (int) $this->enabledSearchable()),
455  'export' => array('integer', (int) $this->enabledExport()),
456  'course_export' => array('integer', (int) $this->enabledCourseExport()),
457  'registration_visible' => array('integer', (int) $this->enabledVisibleRegistration()),
458  'visible_lua' => array('integer', (int) $this->enabledVisibleLocalUserAdministration()),
459  'changeable_lua' => array('integer', (int) $this->enabledChangeableLocalUserAdministration()),
460  'group_export' => array('integer', (int) $this->enabledGroupExport()),
461  'certificate' => array('integer', (int) $this->enabledCertificate())
462  );
463  $ilDB->update('udf_definition', $values, array('field_id' => array('integer',$a_id)));
464  $this->__read();
465 
466  return true;
467  }
468 
469 
470 
471  // Private
472  public function __read()
473  {
474  global $ilSetting;
475 
476  $query = "SELECT * FROM udf_definition ";
477  $res = $this->db->query($query);
478 
479  $this->definitions = array();
480  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
481  $this->definitions[$row->field_id]['field_id'] = $row->field_id;
482  $this->definitions[$row->field_id]['field_name'] = $row->field_name;
483  $this->definitions[$row->field_id]['field_type'] = $row->field_type;
484  $this->definitions[$row->field_id]['il_id'] = 'il_' . $ilSetting->get('inst_id', 0) . '_udf_' . $row->field_id;
485 
486  // #16953
487  $tmp = $sort = array();
488  $is_numeric = true;
489  foreach ((array) unserialize($row->field_values) as $item) {
490  if (!is_numeric($item)) {
491  $is_numeric = false;
492  }
493  $sort[] = array("value"=>$item);
494  }
495  foreach (ilUtil::sortArray($sort, "value", "asc", $is_numeric) as $item) {
496  $tmp[] = $item["value"];
497  }
498 
499  $this->definitions[$row->field_id]['field_values'] = $tmp;
500  $this->definitions[$row->field_id]['visible'] = $row->visible;
501  $this->definitions[$row->field_id]['changeable'] = $row->changeable;
502  $this->definitions[$row->field_id]['required'] = $row->required;
503  $this->definitions[$row->field_id]['searchable'] = $row->searchable;
504  $this->definitions[$row->field_id]['export'] = $row->export;
505  $this->definitions[$row->field_id]['course_export'] = $row->course_export;
506  $this->definitions[$row->field_id]['visib_reg'] = $row->registration_visible;
507  $this->definitions[$row->field_id]['visib_lua'] = $row->visible_lua;
508  $this->definitions[$row->field_id]['changeable_lua'] = $row->changeable_lua;
509  $this->definitions[$row->field_id]['group_export'] = $row->group_export;
510  // fraunhpatch start
511  $this->definitions[$row->field_id]['certificate'] = $row->certificate;
512  // fraunhpatch end
513  }
514 
515  return true;
516  }
517 
518 
519  public function deleteValue($a_field_id, $a_value_id)
520  {
521  global $ilDB;
522 
523  $definition = $this->getDefinition($a_field_id);
524 
525  $counter = 0;
526  $new_values = array();
527  foreach ($definition['field_values'] as $value) {
528  if ($counter++ != $a_value_id) {
529  $new_values[] = $value;
530  } else {
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  public 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  public function addToXML($xml_writer)
565  {
566  $xml_writer->xmlStartTag("UDFDefinitions");
567  foreach ($this->getDefinitions() as $definition) {
568  $attributes = array(
569  "Id" => $definition ["il_id"],
570  "Type" => $definition["field_type"] == UDF_TYPE_SELECT? "SELECT" : "TEXT",
571  "Visible" => $definition["visible"]? "TRUE" : "FALSE",
572  "Changeable" => $definition["changeable"]? "TRUE" : "FALSE",
573  "Required" => $definition["required"]? "TRUE" : "FALSE",
574  "Searchable" => $definition["searchable"]? "TRUE" : "FALSE",
575  "CourseExport" => $definition["course_export"]? "TRUE" : "FALSE",
576  "GroupExport" => $definition["group_export"]? "TRUE" : "FALSE",
577  "Certificate" => $definition["certificate"]? "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  $xml_writer->xmlElement('UDFValue', null, $field_value);
590  }
591  }
592  $xml_writer->xmlEndTag("UDFDefinition");
593  }
594  $xml_writer->xmlEndTag("UDFDefinitions");
595  }
596 
597 
598  public static function _newInstance()
599  {
600  static $udf = null;
601 
602  return $udf = new ilUserDefinedFields();
603  }
604 }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
addToXML($xml_writer)
add user defined field data to xml (using usr dtd)
const UDF_TYPE_SELECT
enableVisibleRegistration($a_visible_registration)
static _getInstance()
Get instance.
global $DIC
Definition: saml.php:7
static deleteEntriesOfField($a_field_id)
Delete data of particular field.
Additional user data fields definition.
deleteValue($a_field_id, $a_value_id)
const UDF_NO_VALUES
if(!array_key_exists('StateId', $_REQUEST)) $id
XML writer class.
$attributes
enableCourseExport($a_course_export)
getExportableFields($a_obj_id)
Get exportable field.
$counter
$a_type
Definition: workflow.php:92
__construct()
Constructor is private -> use getInstance These definition are used e.g in User XML import...
foreach($_POST as $key=> $value) $res
$query
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
getGroupExportableFields()
get fields visible in groups
enableVisibleLocalUserAdministration($a_visible)
global $ilSetting
Definition: privfeed.php:17
global $lng
Definition: privfeed.php:17
global $ilDB
static deleteFieldValue($a_field_id, $a_value)
Delete data of particular value of a (selection) field.
const UDF_DUPLICATE_VALUES
fieldValuesToSelectArray($a_values, $a_with_selection_info=true)
enableChangeableLocalUserAdministration($a_changeable)