ILIAS  Release_4_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  $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  if($a_field_id == self::$mappings->getMappingByECSName('begin') and
235  $end = self::$mappings->getMappingByECSName('end'))
236  {
237  // Parse a duration
238  $start = in_array($a_field_id,$this->date_fields) ?
239  new ilDate($a_values[$a_field_id],IL_CAL_UNIX) :
240  new ilDateTime($a_values[$a_field_id],IL_CAL_UNIX);
241  $end = in_array($end,$this->date_fields) ?
242  new ilDate($a_values[$end],IL_CAL_UNIX) :
243  new ilDateTime($a_values[$end],IL_CAL_UNIX);
244 
245  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
246  $weekday = ilCalendarUtil::_numericDayToString($start->get(IL_CAL_FKT_DATE,'w',$ilUser->getTimeZone()),false);
247 
251  return $weekday.', '.$value;
252  }
253  }
254  if(in_array($a_field_id,$this->date_fields))
255  {
256  return $value = ilDatePresentation::formatDate(new ilDate((int) $a_values[$a_field_id],IL_CAL_UNIX));
257  }
258  elseif(in_array($a_field_id,$this->datetime_fields))
259  {
260  return $value = ilDatePresentation::formatDate(new ilDateTime((int) $a_values[$a_field_id],IL_CAL_UNIX));
261  }
262  else
263  {
264  return $value = $a_values[$a_field_id];
265  }
266  }
267 
268 
276  public function resetSubstitutions()
277  {
278  $this->substitutions = array();
279  $this->bold = array();
280  $this->newline = array();
281  }
282 
290  public function appendSubstitution($a_field_id,$a_bold = false,$a_newline = false)
291  {
292  $this->substitutions[] = $a_field_id;
293  if($a_bold)
294  {
295  $this->bold[] = $a_field_id;
296  }
297  if($a_newline)
298  {
299  $this->newline[] = $a_field_id;
300  }
301  }
302 
310  public function getSubstitutions()
311  {
312  return $this->substitutions ? $this->substitutions : array();
313  }
314 
322  public function isSubstituted($a_field_id)
323  {
324  return in_array($a_field_id,$this->getSubstitutions());
325  }
326 
334  public function isBold($a_field_id)
335  {
336  #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
337 
338  return in_array($a_field_id,$this->bold);
339  }
340 
348  public function hasNewline($a_field_id)
349  {
350  return in_array($a_field_id,$this->newline);
351  }
358  public function update()
359  {
360  global $ilDB;
361 
362  $counter = 0;
363  $substitutions = array();
364 
365  foreach($this->substitutions as $field_id)
366  {
367  $substitutions[$counter]['field_id'] = $field_id;
368  $substitutions[$counter]['bold'] = $this->isBold($field_id);
369  $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
370  $counter++;
371  }
372 
373  $query = "DELETE FROM adv_md_substitutions WHERE obj_type = ".$ilDB->quote($this->type,'text');
374  $res = $ilDB->manipulate($query);
375 
376 
377  $values = array(
378  'obj_type' => array('text',$this->type),
379  'substitution' => array('clob',serialize($substitutions)),
380  'hide_description' => array('integer',!$this->isDescriptionEnabled()),
381  'hide_field_names' => array('integer',!$this->enabledFieldNames())
382  );
383  $ilDB->insert('adv_md_substitutions',$values);
384  }
385 
392  private function read()
393  {
394  global $ilDB;
395 
396  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
397  $this->date_fields = ilAdvancedMDFieldDefinition::_lookupDateFields();
398  $this->datetime_fields = ilAdvancedMDFieldDefinition::_lookupDatetimeFields();
399 
400  // Check active status
401  $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr ".
402  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id ".
403  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
404  "WHERE active = 1 ".
405  "AND obj_type = ".$this->db->quote($this->type ,'text')." ";
406  $res = $this->db->query($query);
407  $this->active = $res->numRows() ? true : false;
408  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
409  {
410  $this->active_fields[$row->field_id] = $row->title;
411  }
412 
413  $query = "SELECT * FROM adv_md_substitutions ".
414  "WHERE obj_type = ".$this->db->quote($this->type ,'text')." ";
415  $res = $this->db->query($query);
416  $this->substitutions = array();
417  $this->bold = array();
418  $this->newline = array();
419  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
420  {
421  $tmp_substitutions = unserialize($row->substitution);
422  if(is_array($tmp_substitutions))
423  {
424  foreach($tmp_substitutions as $substitution)
425  {
426  if($substitution['field_id'])
427  {
428  $this->substitutions[] = $substitution['field_id'];
429  }
430  if($substitution['bold'])
431  {
432  $this->bold[] = $substitution['field_id'];
433  }
434  if($substitution['newline'])
435  {
436  $this->newline[] = $substitution['field_id'];
437  }
438 
439  }
440  }
441  $this->enabled_desc = !$row->hide_description;
442  $this->enabled_field_names = !$row->hide_field_names;
443  }
444 
445  if($this->type == 'crs' or $this->type == 'rcrs')
446  {
447  // Handle ECS substitutions
448  if($begin = self::$mappings->getMappingByECSName('begin') and
449  $end = self::$mappings->getMappingByECSName('end'))
450  {
451  // Show something like 'Monday, 30.12.2008 9:00 - 12:00'
452  unset($this->active_fields[$end]);
453  }
454  }
455  }
456 
463  private function initECSMappings()
464  {
465  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
466 
467  if(isset(self::$mappings) and is_object(self::$mappings))
468  {
469  return true;
470  }
471  self::$mappings = ilECSDataMappingSettings::_getInstance();
472  return true;
473  }
474 }
475 ?>