ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDSubstitution.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 
33 {
34  private static $instances = null;
35  private static $mappings = null;
36 
37  protected $db;
38 
39  protected $type;
40  protected $substitutions;
41  protected $bold = array();
42  protected $newline = array();
43 
44  protected $enabled_desc = true;
45  protected $enabled_field_names = true;
46  protected $active = false;
47  protected $date_fields = array();
48  protected $datetime_fields = array();
49  protected $active_fields = array();
50 
51 
52  /*
53  * Singleton class
54  * Use _getInstance
55  * @access private
56  * @param
57  */
58  private function __construct($a_type)
59  {
60  global $ilDB;
61 
62  $this->db = $ilDB;
63  $this->type = $a_type;
64 
65  $this->initECSMappings();
66  $this->read();
67  }
68 
77  public static function _getInstanceByObjectType($a_type)
78  {
79  if(isset(self::$instances[$a_type]))
80  {
81  return self::$instances[$a_type];
82  }
83  return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
84  }
85 
93  public function sortDefinitions($a_definitions)
94  {
95  $sorted = array();
96  foreach($this->substitutions as $field_id)
97  {
98  $sorted[] = $field_id;
99  $key = array_search($field_id,$a_definitions);
100  unset($a_definitions[$key]);
101  }
102  return array_merge($sorted,$a_definitions);
103  }
104 
111  public function isActive()
112  {
113  return $this->active;
114  }
115 
122  public function isDescriptionEnabled()
123  {
124  return (bool) $this->enabled_desc;
125  }
126 
134  public function enableDescription($a_status)
135  {
136  $this->enabled_desc = $a_status;
137  }
138 
145  public function enabledFieldNames()
146  {
147  return (bool) $this->enabled_field_names;
148  }
149 
157  public function enableFieldNames($a_status)
158  {
159  $this->enabled_field_names = $a_status;
160  }
161 
171  public function getParsedSubstitutions($a_ref_id,$a_obj_id)
172  {
173  if(!count($this->getSubstitutions()))
174  {
175  return array();
176  }
177 
178  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
179  $values = ilAdvancedMDValues::_getValuesByObjId($a_obj_id);
180 
181  $counter = 0;
182  foreach($this->getSubstitutions() as $field_id)
183  {
184  if(!isset($this->active_fields[$field_id]))
185  {
186  continue;
187  }
188  if(!isset($values[$field_id]) or !$values[$field_id])
189  {
190  if($this->hasNewline($field_id) and $counter)
191  {
192  $substituted[$counter-1]['newline'] = true;
193  }
194  continue;
195  }
196 
197  $value = $this->parseValue($field_id,$values);
198 
199  $substituted[$counter]['name'] = $this->active_fields[$field_id];
200  $substituted[$counter]['value'] = $value;
201  $substituted[$counter]['bold'] = $this->isBold($field_id);
202  if($this->hasNewline($field_id))
203  {
204  $substituted[$counter]['newline'] = true;
205  }
206  else
207  {
208  $substituted[$counter]['newline'] = false;
209  }
210  $substituted[$counter]['show_field'] = $this->enabledFieldNames();
211  $counter++;
212  }
213 
214  return $substituted ? $substituted : array();
215  }
216 
227  private function parseValue($a_field_id,$a_values)
228  {
229  global $ilUser;
230 
231  if($this->type == 'crs' or $this->type == 'rcrs')
232  {
233  // Special handling for ECS fields
234  // @FIXME
235  /*
236  if($a_field_id == self::$mappings->getMappingByECSName('begin') and
237  $end = self::$mappings->getMappingByECSName('end'))
238  {
239  // Parse a duration
240  $start = in_array($a_field_id,$this->date_fields) ?
241  new ilDate($a_values[$a_field_id],IL_CAL_UNIX) :
242  new ilDateTime($a_values[$a_field_id],IL_CAL_UNIX);
243  $end = in_array($end,$this->date_fields) ?
244  new ilDate($a_values[$end],IL_CAL_UNIX) :
245  new ilDateTime($a_values[$end],IL_CAL_UNIX);
246 
247  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
248  $weekday = ilCalendarUtil::_numericDayToString($start->get(IL_CAL_FKT_DATE,'w',$ilUser->getTimeZone()),false);
249 
250  ilDatePresentation::setUseRelativeDates(false);
251  $value = ilDatePresentation::formatPeriod($start,$end);
252  ilDatePresentation::setUseRelativeDates(true);
253  return $weekday.', '.$value;
254  }
255  */
256  }
257  if(in_array($a_field_id,$this->date_fields))
258  {
259  return $value = ilDatePresentation::formatDate(new ilDate((int) $a_values[$a_field_id],IL_CAL_UNIX));
260  }
261  elseif(in_array($a_field_id,$this->datetime_fields))
262  {
263  return $value = ilDatePresentation::formatDate(new ilDateTime((int) $a_values[$a_field_id],IL_CAL_UNIX));
264  }
265  else
266  {
267  return $value = $a_values[$a_field_id];
268  }
269  }
270 
271 
279  public function resetSubstitutions()
280  {
281  $this->substitutions = array();
282  $this->bold = array();
283  $this->newline = array();
284  }
285 
293  public function appendSubstitution($a_field_id,$a_bold = false,$a_newline = false)
294  {
295  $this->substitutions[] = $a_field_id;
296  if($a_bold)
297  {
298  $this->bold[] = $a_field_id;
299  }
300  if($a_newline)
301  {
302  $this->newline[] = $a_field_id;
303  }
304  }
305 
313  public function getSubstitutions()
314  {
315  return $this->substitutions ? $this->substitutions : array();
316  }
317 
325  public function isSubstituted($a_field_id)
326  {
327  return in_array($a_field_id,$this->getSubstitutions());
328  }
329 
337  public function isBold($a_field_id)
338  {
339  #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
340 
341  return in_array($a_field_id,$this->bold);
342  }
343 
351  public function hasNewline($a_field_id)
352  {
353  return in_array($a_field_id,$this->newline);
354  }
361  public function update()
362  {
363  global $ilDB;
364 
365  $counter = 0;
366  $substitutions = array();
367 
368  foreach($this->substitutions as $field_id)
369  {
370  $substitutions[$counter]['field_id'] = $field_id;
371  $substitutions[$counter]['bold'] = $this->isBold($field_id);
372  $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
373  $counter++;
374  }
375 
376  $query = "DELETE FROM adv_md_substitutions WHERE obj_type = ".$ilDB->quote($this->type,'text');
377  $res = $ilDB->manipulate($query);
378 
379 
380  $values = array(
381  'obj_type' => array('text',$this->type),
382  'substitution' => array('clob',serialize($substitutions)),
383  'hide_description' => array('integer',!$this->isDescriptionEnabled()),
384  'hide_field_names' => array('integer',!$this->enabledFieldNames())
385  );
386  $ilDB->insert('adv_md_substitutions',$values);
387  }
388 
395  private function read()
396  {
397  global $ilDB;
398 
399  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
400  $this->date_fields = ilAdvancedMDFieldDefinition::_lookupDateFields();
401  $this->datetime_fields = ilAdvancedMDFieldDefinition::_lookupDatetimeFields();
402 
403  // Check active status
404  $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr ".
405  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id ".
406  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
407  "WHERE active = 1 ".
408  "AND obj_type = ".$this->db->quote($this->type ,'text')." ";
409  $res = $this->db->query($query);
410  $this->active = $res->numRows() ? true : false;
411  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
412  {
413  $this->active_fields[$row->field_id] = $row->title;
414  }
415 
416  $query = "SELECT * FROM adv_md_substitutions ".
417  "WHERE obj_type = ".$this->db->quote($this->type ,'text')." ";
418  $res = $this->db->query($query);
419  $this->substitutions = array();
420  $this->bold = array();
421  $this->newline = array();
422  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
423  {
424  $tmp_substitutions = unserialize($row->substitution);
425  if(is_array($tmp_substitutions))
426  {
427  foreach($tmp_substitutions as $substitution)
428  {
429  if($substitution['field_id'])
430  {
431  $this->substitutions[] = $substitution['field_id'];
432  }
433  if($substitution['bold'])
434  {
435  $this->bold[] = $substitution['field_id'];
436  }
437  if($substitution['newline'])
438  {
439  $this->newline[] = $substitution['field_id'];
440  }
441 
442  }
443  }
444  $this->enabled_desc = !$row->hide_description;
445  $this->enabled_field_names = !$row->hide_field_names;
446  }
447 
448  if($this->type == 'crs' or $this->type == 'rcrs')
449  {
450  // Handle ECS substitutions
451  /*
452  if($begin = self::$mappings->getMappingByECSName('begin') and
453  $end = self::$mappings->getMappingByECSName('end'))
454  {
455  // Show something like 'Monday, 30.12.2008 9:00 - 12:00'
456  unset($this->active_fields[$end]);
457  }
458  */
459  }
460  }
461 
468  private function initECSMappings()
469  {
470  return true;
471 
472  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
473 
474  if(isset(self::$mappings) and is_object(self::$mappings))
475  {
476  return true;
477  }
478  self::$mappings = ilECSDataMappingSettings::_getInstance();
479  return true;
480  }
481 }
482 ?>