ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
61 
62  $ilDB = $DIC['ilDB'];
63 
64  $this->db = $ilDB;
65  $this->type = $a_type;
66 
67  $this->initECSMappings();
68  $this->read();
69  }
70 
79  public static function _getInstanceByObjectType($a_type)
80  {
81  if (isset(self::$instances[$a_type])) {
82  return self::$instances[$a_type];
83  }
84  return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
85  }
86 
94  public function sortDefinitions($a_definitions)
95  {
96  $sorted = array();
97  foreach ($this->substitutions as $field_id) {
98  if (isset($a_definitions[$field_id])) {
99  $sorted[$field_id] = $a_definitions[$field_id];
100  unset($a_definitions[$field_id]);
101  }
102  }
103  return array_merge($sorted, $a_definitions);
104  }
105 
112  public function isActive()
113  {
114  return $this->active;
115  }
116 
123  public function isDescriptionEnabled()
124  {
125  return (bool) $this->enabled_desc;
126  }
127 
135  public function enableDescription($a_status)
136  {
137  $this->enabled_desc = $a_status;
138  }
139 
146  public function enabledFieldNames()
147  {
148  return (bool) $this->enabled_field_names;
149  }
150 
158  public function enableFieldNames($a_status)
159  {
160  $this->enabled_field_names = $a_status;
161  }
162 
172  public function getParsedSubstitutions($a_ref_id, $a_obj_id)
173  {
174  if (!count($this->getSubstitutions())) {
175  return array();
176  }
177 
178  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
179  $values_records = ilAdvancedMDValues::preloadedRead($this->type, $a_obj_id);
180 
181  $counter = 0;
182  foreach ($this->getSubstitutions() as $field_id) {
183  if (!isset($this->active_fields[$field_id])) {
184  continue;
185  }
186 
187  $value = $this->parseValue($field_id, $values_records);
188 
189  if ($value === null) {
190  if ($this->hasNewline($field_id) and $counter) {
191  $substituted[$counter - 1]['newline'] = true;
192  }
193  continue;
194  }
195 
196  $substituted[$counter]['name'] = $this->active_fields[$field_id];
197  $substituted[$counter]['value'] = $value;
198  $substituted[$counter]['bold'] = $this->isBold($field_id);
199  if ($this->hasNewline($field_id)) {
200  $substituted[$counter]['newline'] = true;
201  } else {
202  $substituted[$counter]['newline'] = false;
203  }
204  $substituted[$counter]['show_field'] = $this->enabledFieldNames();
205  $counter++;
206  }
207 
208  return $substituted ? $substituted : array();
209  }
210 
221  private function parseValue($a_field_id, $a_values_records)
222  {
223  global $DIC;
224 
225  $ilUser = $DIC['ilUser'];
226 
227  if ($this->type == 'crs' or $this->type == 'rcrs') {
228  // Special handling for ECS fields
229  // @FIXME
230  /*
231  if($a_field_id == self::$mappings->getMappingByECSName('begin') and
232  $end = self::$mappings->getMappingByECSName('end'))
233  {
234  // Parse a duration
235  $start = in_array($a_field_id,$this->date_fields) ?
236  new ilDate($a_values[$a_field_id],IL_CAL_UNIX) :
237  new ilDateTime($a_values[$a_field_id],IL_CAL_UNIX);
238  $end = in_array($end,$this->date_fields) ?
239  new ilDate($a_values[$end],IL_CAL_UNIX) :
240  new ilDateTime($a_values[$end],IL_CAL_UNIX);
241 
242  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
243  $weekday = ilCalendarUtil::_numericDayToString($start->get(IL_CAL_FKT_DATE,'w',$ilUser->getTimeZone()),false);
244 
245  ilDatePresentation::setUseRelativeDates(false);
246  $value = ilDatePresentation::formatPeriod($start,$end);
247  ilDatePresentation::setUseRelativeDates(true);
248  return $weekday.', '.$value;
249  }
250  */
251  }
252 
253  foreach ($a_values_records as $a_values) {
254  if ($a_values->getADTGroup()->hasElement($a_field_id)) {
255  $element = $a_values->getADTGroup()->getElement($a_field_id);
256  if (!$element->isNull()) {
257  return ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getList();
258  }
259  }
260  }
261  }
262 
263 
271  public function resetSubstitutions()
272  {
273  $this->substitutions = array();
274  $this->bold = array();
275  $this->newline = array();
276  }
277 
285  public function appendSubstitution($a_field_id, $a_bold = false, $a_newline = false)
286  {
287  $this->substitutions[] = $a_field_id;
288  if ($a_bold) {
289  $this->bold[] = $a_field_id;
290  }
291  if ($a_newline) {
292  $this->newline[] = $a_field_id;
293  }
294  }
295 
303  public function getSubstitutions()
304  {
305  return $this->substitutions ? $this->substitutions : array();
306  }
307 
315  public function isSubstituted($a_field_id)
316  {
317  return in_array($a_field_id, $this->getSubstitutions());
318  }
319 
327  public function isBold($a_field_id)
328  {
329  #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
330 
331  return in_array($a_field_id, $this->bold);
332  }
333 
341  public function hasNewline($a_field_id)
342  {
343  return in_array($a_field_id, $this->newline);
344  }
351  public function update()
352  {
353  global $DIC;
354 
355  $ilDB = $DIC['ilDB'];
356 
357  $counter = 0;
358  $substitutions = array();
359 
360  foreach ($this->substitutions as $field_id) {
361  $substitutions[$counter]['field_id'] = $field_id;
362  $substitutions[$counter]['bold'] = $this->isBold($field_id);
363  $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
364  $counter++;
365  }
366 
367  $query = "DELETE FROM adv_md_substitutions WHERE obj_type = " . $ilDB->quote($this->type, 'text');
368  $res = $ilDB->manipulate($query);
369 
370 
371  $values = array(
372  'obj_type' => array('text',$this->type),
373  'substitution' => array('clob',serialize($substitutions)),
374  'hide_description' => array('integer',!$this->isDescriptionEnabled()),
375  'hide_field_names' => array('integer',!$this->enabledFieldNames())
376  );
377  $ilDB->insert('adv_md_substitutions', $values);
378  }
379 
386  private function read()
387  {
388  global $DIC;
389 
390  $ilDB = $DIC['ilDB'];
391 
392  // Check active status
393  $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr " .
394  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id " .
395  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id " .
396  "WHERE active = 1 " .
397  "AND obj_type = " . $this->db->quote($this->type, 'text') . " ";
398  $res = $this->db->query($query);
399  $this->active = $res->numRows() ? true : false;
400  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
401  $this->active_fields[$row->field_id] = $row->title;
402  }
403 
404  $query = "SELECT * FROM adv_md_substitutions " .
405  "WHERE obj_type = " . $this->db->quote($this->type, 'text') . " ";
406  $res = $this->db->query($query);
407  $this->substitutions = array();
408  $this->bold = array();
409  $this->newline = array();
410  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
411  $tmp_substitutions = unserialize($row->substitution);
412  if (is_array($tmp_substitutions)) {
413  foreach ($tmp_substitutions as $substitution) {
414  if ($substitution['field_id']) {
415  $this->substitutions[] = $substitution['field_id'];
416  }
417  if ($substitution['bold']) {
418  $this->bold[] = $substitution['field_id'];
419  }
420  if ($substitution['newline']) {
421  $this->newline[] = $substitution['field_id'];
422  }
423  }
424  }
425  $this->enabled_desc = !$row->hide_description;
426  $this->enabled_field_names = !$row->hide_field_names;
427  }
428 
429  if ($this->type == 'crs' or $this->type == 'rcrs') {
430  // Handle ECS substitutions
431  /*
432  if($begin = self::$mappings->getMappingByECSName('begin') and
433  $end = self::$mappings->getMappingByECSName('end'))
434  {
435  // Show something like 'Monday, 30.12.2008 9:00 - 12:00'
436  unset($this->active_fields[$end]);
437  }
438  */
439  }
440  }
441 
448  private function initECSMappings()
449  {
450  return true;
451 
452  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
453 
454  if (isset(self::$mappings) and is_object(self::$mappings)) {
455  return true;
456  }
457  self::$mappings = ilECSDataMappingSettings::_getInstance();
458  return true;
459  }
460 }
enableFieldNames($a_status)
enable field names
isSubstituted($a_field_id)
is substituted
global $DIC
Definition: saml.php:7
parseValue($a_field_id, $a_values_records)
special handling for date(time) values and ECS dates
enableDescription($a_status)
Enable description presentation.
static getInstance()
Get singleton.
static _getInstanceByObjectType($a_type)
Singleton: use this method to get an instance.
$a_type
Definition: workflow.php:92
foreach($_POST as $key=> $value) $res
isDescriptionEnabled()
Is description enabled.
$values
getSubstitutions()
get substitution string
$ilUser
Definition: imgupload.php:18
$query
$row
global $ilDB
appendSubstitution($a_field_id, $a_bold=false, $a_newline=false)
append field to substitutions
static preloadedRead($a_type, $a_obj_id)
sortDefinitions($a_definitions)
Sort definitions.
static _getInstance()
Get Singleton instance.
getParsedSubstitutions($a_ref_id, $a_obj_id)
Substitute.