ILIAS  Release_5_0_x_branch Revision 61816
 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  if(isset($a_definitions[$field_id]))
99  {
100  $sorted[$field_id] = $a_definitions[$field_id];
101  unset($a_definitions[$field_id]);
102  }
103  }
104  return array_merge($sorted,$a_definitions);
105  }
106 
113  public function isActive()
114  {
115  return $this->active;
116  }
117 
124  public function isDescriptionEnabled()
125  {
126  return (bool) $this->enabled_desc;
127  }
128 
136  public function enableDescription($a_status)
137  {
138  $this->enabled_desc = $a_status;
139  }
140 
147  public function enabledFieldNames()
148  {
149  return (bool) $this->enabled_field_names;
150  }
151 
159  public function enableFieldNames($a_status)
160  {
161  $this->enabled_field_names = $a_status;
162  }
163 
173  public function getParsedSubstitutions($a_ref_id, $a_obj_id)
174  {
175  if(!count($this->getSubstitutions()))
176  {
177  return array();
178  }
179 
180  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
181  $values_records = ilAdvancedMDValues::preloadedRead($this->type, $a_obj_id);
182 
183  $counter = 0;
184  foreach($this->getSubstitutions() as $field_id)
185  {
186  if(!isset($this->active_fields[$field_id]))
187  {
188  continue;
189  }
190 
191  $value = $this->parseValue($field_id, $values_records);
192 
193  if($value === null)
194  {
195  if($this->hasNewline($field_id) and $counter)
196  {
197  $substituted[$counter-1]['newline'] = true;
198  }
199  continue;
200  }
201 
202  $substituted[$counter]['name'] = $this->active_fields[$field_id];
203  $substituted[$counter]['value'] = $value;
204  $substituted[$counter]['bold'] = $this->isBold($field_id);
205  if($this->hasNewline($field_id))
206  {
207  $substituted[$counter]['newline'] = true;
208  }
209  else
210  {
211  $substituted[$counter]['newline'] = false;
212  }
213  $substituted[$counter]['show_field'] = $this->enabledFieldNames();
214  $counter++;
215  }
216 
217  return $substituted ? $substituted : array();
218  }
219 
230  private function parseValue($a_field_id,$a_values_records)
231  {
232  global $ilUser;
233 
234  if($this->type == 'crs' or $this->type == 'rcrs')
235  {
236  // Special handling for ECS fields
237  // @FIXME
238  /*
239  if($a_field_id == self::$mappings->getMappingByECSName('begin') and
240  $end = self::$mappings->getMappingByECSName('end'))
241  {
242  // Parse a duration
243  $start = in_array($a_field_id,$this->date_fields) ?
244  new ilDate($a_values[$a_field_id],IL_CAL_UNIX) :
245  new ilDateTime($a_values[$a_field_id],IL_CAL_UNIX);
246  $end = in_array($end,$this->date_fields) ?
247  new ilDate($a_values[$end],IL_CAL_UNIX) :
248  new ilDateTime($a_values[$end],IL_CAL_UNIX);
249 
250  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
251  $weekday = ilCalendarUtil::_numericDayToString($start->get(IL_CAL_FKT_DATE,'w',$ilUser->getTimeZone()),false);
252 
253  ilDatePresentation::setUseRelativeDates(false);
254  $value = ilDatePresentation::formatPeriod($start,$end);
255  ilDatePresentation::setUseRelativeDates(true);
256  return $weekday.', '.$value;
257  }
258  */
259  }
260 
261  foreach($a_values_records as $a_values)
262  {
263  if($a_values->getADTGroup()->hasElement($a_field_id))
264  {
265  $element = $a_values->getADTGroup()->getElement($a_field_id);
266  if(!$element->isNull())
267  {
268  return ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getList();
269  }
270  }
271  }
272  }
273 
274 
282  public function resetSubstitutions()
283  {
284  $this->substitutions = array();
285  $this->bold = array();
286  $this->newline = array();
287  }
288 
296  public function appendSubstitution($a_field_id,$a_bold = false,$a_newline = false)
297  {
298  $this->substitutions[] = $a_field_id;
299  if($a_bold)
300  {
301  $this->bold[] = $a_field_id;
302  }
303  if($a_newline)
304  {
305  $this->newline[] = $a_field_id;
306  }
307  }
308 
316  public function getSubstitutions()
317  {
318  return $this->substitutions ? $this->substitutions : array();
319  }
320 
328  public function isSubstituted($a_field_id)
329  {
330  return in_array($a_field_id,$this->getSubstitutions());
331  }
332 
340  public function isBold($a_field_id)
341  {
342  #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
343 
344  return in_array($a_field_id,$this->bold);
345  }
346 
354  public function hasNewline($a_field_id)
355  {
356  return in_array($a_field_id,$this->newline);
357  }
364  public function update()
365  {
366  global $ilDB;
367 
368  $counter = 0;
369  $substitutions = array();
370 
371  foreach($this->substitutions as $field_id)
372  {
373  $substitutions[$counter]['field_id'] = $field_id;
374  $substitutions[$counter]['bold'] = $this->isBold($field_id);
375  $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
376  $counter++;
377  }
378 
379  $query = "DELETE FROM adv_md_substitutions WHERE obj_type = ".$ilDB->quote($this->type,'text');
380  $res = $ilDB->manipulate($query);
381 
382 
383  $values = array(
384  'obj_type' => array('text',$this->type),
385  'substitution' => array('clob',serialize($substitutions)),
386  'hide_description' => array('integer',!$this->isDescriptionEnabled()),
387  'hide_field_names' => array('integer',!$this->enabledFieldNames())
388  );
389  $ilDB->insert('adv_md_substitutions',$values);
390  }
391 
398  private function read()
399  {
400  global $ilDB;
401 
402  // Check active status
403  $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr ".
404  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id ".
405  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
406  "WHERE active = 1 ".
407  "AND obj_type = ".$this->db->quote($this->type ,'text')." ";
408  $res = $this->db->query($query);
409  $this->active = $res->numRows() ? true : false;
410  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
411  {
412  $this->active_fields[$row->field_id] = $row->title;
413  }
414 
415  $query = "SELECT * FROM adv_md_substitutions ".
416  "WHERE obj_type = ".$this->db->quote($this->type ,'text')." ";
417  $res = $this->db->query($query);
418  $this->substitutions = array();
419  $this->bold = array();
420  $this->newline = array();
421  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
422  {
423  $tmp_substitutions = unserialize($row->substitution);
424  if(is_array($tmp_substitutions))
425  {
426  foreach($tmp_substitutions as $substitution)
427  {
428  if($substitution['field_id'])
429  {
430  $this->substitutions[] = $substitution['field_id'];
431  }
432  if($substitution['bold'])
433  {
434  $this->bold[] = $substitution['field_id'];
435  }
436  if($substitution['newline'])
437  {
438  $this->newline[] = $substitution['field_id'];
439  }
440 
441  }
442  }
443  $this->enabled_desc = !$row->hide_description;
444  $this->enabled_field_names = !$row->hide_field_names;
445  }
446 
447  if($this->type == 'crs' or $this->type == 'rcrs')
448  {
449  // Handle ECS substitutions
450  /*
451  if($begin = self::$mappings->getMappingByECSName('begin') and
452  $end = self::$mappings->getMappingByECSName('end'))
453  {
454  // Show something like 'Monday, 30.12.2008 9:00 - 12:00'
455  unset($this->active_fields[$end]);
456  }
457  */
458  }
459  }
460 
467  private function initECSMappings()
468  {
469  return true;
470 
471  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
472 
473  if(isset(self::$mappings) and is_object(self::$mappings))
474  {
475  return true;
476  }
477  self::$mappings = ilECSDataMappingSettings::_getInstance();
478  return true;
479  }
480 }
481 ?>