ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDFieldDefinition.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 {
33  const TYPE_SELECT = 1;
34  const TYPE_TEXT = 2;
35  const TYPE_DATE = 3;
36  const TYPE_DATETIME = 4;
37 
38  private static $instances = array();
39 
40  protected $db = null;
41 
42  protected $record_id;
43  protected $field_id;
44  protected $import_id;
45  protected $position;
46  protected $field_type;
47  protected $field_values = array();
48  protected $title;
49  protected $description;
50  protected $searchable;
51  protected $required = false;
52 
53 
59  public function __construct($a_field_id = 0)
60  {
61  global $ilDB;
62 
63  $this->db = $ilDB;
64 
65  $this->field_id = $a_field_id;
66  $this->read();
67  }
68 
77  public static function _lookupImportId($a_field_id)
78  {
79  global $ilDB;
80 
81  $query = "SELECT import_id FROM adv_md_field_definition ".
82  "WHERE field_id = ".$ilDB->quote($a_field_id)." ";
83  $res = $ilDB->query($query);
84  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
85  return $row['import_id'] ? $row['import_id'] : '';
86  }
87 
96  public static function _lookupFieldId($a_import_id)
97  {
98  global $ilDB;
99 
100  $query = "SELECT field_id FROM adv_md_field_definition ".
101  "WHERE import_id = ".$ilDB->quote($a_import_id)." ";
102  $res = $ilDB->query($query);
103  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
104  return $row['field_id'] ? $row['field_id'] : 0;
105  }
106 
115  public static function _lookupFieldType($a_field_id)
116  {
117  global $ilDB;
118 
119  $query = "SELECT field_type FROM adv_md_field_definition ".
120  "WHERE field_id = ".$ilDB->quote($a_field_id)." ";
121  $res = $ilDB->query($query);
122  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
123  return $row['field_type'] ? $row['field_type'] : 0;
124  }
125 
126 
135  public static function _lookupDateTimeFields()
136  {
137  global $ilDB;
138 
139  $query = "SELECT field_id FROM adv_md_field_definition ".
140  "WHERE field_type = ".self::TYPE_DATETIME." ";
141  $res = $ilDB->query($query);
142 
143  $date_fields = array();
144  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
145  {
146  $date_fields[] = $row->field_id;
147  }
148  return $date_fields;
149  }
150 
158  public static function _lookupDateFields()
159  {
160  global $ilDB;
161 
162  $query = "SELECT field_id FROM adv_md_field_definition ".
163  "WHERE field_type = ".self::TYPE_DATE." ";
164  $res = $ilDB->query($query);
165 
166  $date_fields = array();
167  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
168  {
169  $date_fields[] = $row->field_id;
170  }
171  return $date_fields;
172  }
173 
182  public static function _getInstanceByFieldId($a_field_id)
183  {
184  if(isset(self::$instances[$a_field_id]))
185  {
186  return self::$instances[$a_field_id];
187  }
188  return self::$instances[$a_field_id] = new ilAdvancedMDFieldDefinition($a_field_id);
189  }
190 
191 
201  public static function _getDefinitionsByRecordId($a_record_id)
202  {
203  global $ilDB;
204 
205  $query = "SELECT field_id FROM adv_md_field_definition ".
206  "WHERE record_id = ".$ilDB->quote($a_record_id)." ".
207  "ORDER BY position ";
208  $res = $ilDB->query($query);
209  $defs = array();
210  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
211  {
212  $defs[] = self::_getInstanceByFieldId($row->field_id);
213  }
214  return $defs ? $defs : array();
215  }
216 
225  public static function _getActiveDefinitionsByObjType($a_type)
226  {
227  global $ilDB;
228 
229  $query = "SELECT field_id FROM adv_md_record_objs AS aro ".
230  "JOIN adv_md_record AS amr ON aro.record_id = amr.record_id ".
231  "JOIN adv_md_field_definition AS amf ON aro.record_id = amf.record_id ".
232  "WHERE active = 1 ".
233  "AND obj_type = ".$ilDB->quote($a_type)." ".
234  "ORDER BY aro.record_id,position ";
235  $res = $ilDB->query($query);
236  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
237  {
238  $field_ids[] = $row->field_id;
239  }
240  return $field_ids ? $field_ids : array();
241  }
242 
249  public static function _getSearchableDefinitionIds()
250  {
251  global $ilDB;
252 
253  $query = "SELECT field_id FROM adv_md_record AS amr ".
254  "JOIN adv_md_field_definition AS amfd USING (record_id) ".
255  "WHERE active = 1 ".
256  "AND searchable = 1";
257  $res = $ilDB->query($query);
258  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
259  {
260  $field_ids[] = $row->field_id;
261  }
262  return $field_ids ? $field_ids : array();
263  }
264 
265 
274  public static function _deleteByRecordId($a_record_id)
275  {
276  global $ilDB;
277 
278  $query = "SELECT field_id FROM adv_md_field_definition ".
279  "WHERE record_id = ".$ilDB->quote($a_record_id);
280  $res = $ilDB->query($query);
281  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
282  {
283  // Delete values
284  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
285  ilAdvancedMDValues::_deleteByFieldId($row->field_id);
286  }
287 
288  // Delete definitions
289  $query = "DELETE FROM adv_md_field_definition ".
290  "WHERE record_id = ".$ilDB->quote($a_record_id)." ";
291  $res = $ilDB->query($query);
292  }
293 
301  public function isDeleted()
302  {
303  return $this->record_id ? false : true;
304  }
305 
306  // Setter, Getter...
314  public function setRecordId($a_id)
315  {
316  $this->record_id = $a_id;
317  }
318 
324  public function getRecordId()
325  {
326  return $this->record_id;
327  }
328 
336  public function getFieldId()
337  {
338  return $this->field_id;
339  }
340 
348  public function setImportId($a_id_string)
349  {
350  $this->import_id = $a_id_string;
351  }
352 
359  public function getImportId()
360  {
361  return $this->import_id;
362  }
363 
371  public function setPosition($a_pos)
372  {
373  $this->position = $a_pos;
374  }
375 
383  public function getPosition()
384  {
385  return $this->position;
386  }
387 
395  public function setFieldType($a_type_id)
396  {
397  $this->field_type = $a_type_id;
398  }
399 
406  public function getFieldType()
407  {
408  return $this->field_type;
409  }
410 
418  public function setFieldValues($a_values)
419  {
420  $this->field_values = $a_values;
421  }
422 
430  public function appendFieldValue($a_value)
431  {
432  if(strlen(trim($a_value)))
433  {
434  $this->field_values[] = trim($a_value);
435  }
436  }
437 
444  public function getFieldValues()
445  {
446  return $this->field_values;
447  }
448 
455  public function getFieldValuesForSelect()
456  {
457  global $lng;
458 
459  $values = array(0 => $lng->txt('select_one'));
460  foreach($this->field_values as $value)
461  {
462  $values[$value] = $value;
463  }
464  return $values;
465  }
466 
474  public function setTitle($a_title)
475  {
476  $this->title = $a_title;
477  }
478 
484  public function getTitle()
485  {
486  return $this->title;
487  }
488 
496  public function setDescription($a_desc)
497  {
498  $this->description = $a_desc;
499  }
500 
506  public function getDescription()
507  {
508  return $this->description;
509  }
510 
518  public function enableSearchable($a_status)
519  {
520  $this->searchable = (bool) $a_status;
521  }
522 
529  public function isSearchable()
530  {
531  return (bool) $this->searchable;
532  }
533 
540  public function isRequired()
541  {
542  return $this->required;
543  }
544 
550  public function delete()
551  {
552  $query = "DELETE FROM adv_md_field_definition ".
553  "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
554  $res = $this->db->query($query);
555 
556  // Also delete all values
557  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
559  return true;
560  }
561 
567  public function add()
568  {
569  sort($values = $this->getFieldValues(),SORT_STRING);
570 
571  $position = $this->getLastPosition();
572 
573  $query = "INSERT INTO adv_md_field_definition ".
574  "SET record_id = ".$this->db->quote($this->getRecordId()).", ".
575  "import_id = ".$this->db->quote($this->getImportId()).", ".
576  "position = ".$this->db->quote($position + 1).", ".
577  "field_type = ".$this->db->quote($this->getFieldType()).", ".
578  "field_values = '".addslashes(serialize($values))."', ".
579  "title = ".$this->db->quote($this->getTitle()).", ".
580  "description = ".$this->db->quote($this->getDescription()).", ".
581  "searchable = ".(int) $this->isSearchable().", ".
582  "required = ".(int) $this->isRequired();
583  $this->db->query($query);
584  $this->field_id = $this->db->getLastInsertId();
585 
586  if(!strlen($this->getImportId()))
587  {
588  $query = "UPDATE adv_md_field_definition ".
589  "SET import_id = ".$this->db->quote($this->generateImportId())." ".
590  "WHERE field_id = ".$this->db->quote($this->field_id)." ";
591  $this->db->query($query);
592  }
593  return true;
594  }
595 
602  public function validate()
603  {
604  global $ilErr,$lng;
605 
606  if(!strlen($this->getTitle()) or !$this->getFieldType())
607  {
608  $ilErr->setMessage('fill_out_all_required_fields');
609  return false;
610  }
611  return true;
612  }
613 
620  public function update()
621  {
622  $query = "UPDATE adv_md_field_definition ".
623  "SET record_id = ".$this->db->quote($this->getRecordId()).", ".
624  "import_id = ".$this->db->quote($this->getImportId()).", ".
625  "position = ".$this->db->quote($this->getPosition()).", ".
626  "field_type = ".$this->db->quote($this->getFieldType()).", ".
627  "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
628  "title = ".$this->db->quote($this->getTitle()).", ".
629  "description = ".$this->db->quote($this->getDescription()).", ".
630  "searchable = ".(int) $this->isSearchable().", ".
631  "required = ".(int) $this->isRequired()." ".
632  "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
633 
634  $this->db->query($query);
635  return true;
636  }
637 
647  public function toXML(ilXmlWriter $writer)
648  {
649  switch($this->getFieldType())
650  {
651  case self::TYPE_TEXT:
652  $type = 'Text';
653  break;
654 
655  case self::TYPE_SELECT:
656  $type = 'Select';
657  break;
658 
659  case self::TYPE_DATE:
660  $type = 'Date';
661  break;
662 
663  case self::TYPE_DATETIME:
664  $type = 'DateTime';
665  break;
666  }
667 
668 
669  $writer->xmlStartTag('Field',array(
670  'id' => $this->generateImportId(),
671  'searchable' => ($this->isSearchable() ? 'Yes' : 'No'),
672  'fieldType' => $type));
673 
674  $writer->xmlElement('FieldTitle',null,$this->getTitle());
675  $writer->xmlElement('FieldDescription',null,$this->getDescription());
676  $writer->xmlElement('FieldPosition',null,$this->getPosition());
677 
678  foreach($this->getFieldValues() as $value)
679  {
680  if(strlen($value))
681  {
682  $writer->xmlElement('FieldValue',null,$value);
683  }
684  }
685 
686  $writer->xmlEndTag('Field');
687  }
688 
689 
696  private function read()
697  {
698  if(!$this->field_id)
699  {
700  return false;
701  }
702  $query = "SELECT * FROM adv_md_field_definition ".
703  "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
704  $res = $this->db->query($query);
705  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
706  {
707  $this->record_id = $row->record_id;
708  $this->import_id = $row->import_id;
709  $this->position = $row->position;
710  $this->field_type = $row->field_type;
711  $this->field_values = unserialize(stripslashes($row->field_values));
712  $this->title = $row->title;
713  $this->description = $row->description;
714  $this->searchable = $row->searchable;
715  $this->required = $row->required;
716  }
717  }
718 
725  private function getLastPosition()
726  {
727  $query = "SELECT max(position) as pos FROM adv_md_field_definition ".
728  "WHERE record_id = ".$this->db->quote($this->getRecordId())." ";
729  $res = $this->db->query($query);
730  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
731  {
732  return $row->pos;
733  }
734  return 0;
735  }
736 
743  protected function generateImportId()
744  {
745  return 'il_'.IL_INST_ID.'_adv_md_field_'.$this->getFieldId();
746  }
747 
748 }
749 ?>