ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSCategoryMappingRule.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 
24 include_once './Services/Calendar/classes/class.ilDate.php';
25 
36 {
37  const ATTR_STRING = 1;
38  const ATTR_INT = 2;
39  const ATTR_ARRAY = 3;
40 
41  const TYPE_FIXED = 0;
42  const TYPE_DURATION = 1;
43  const TYPE_BY_TYPE = 2;
44 
45  const ERR_MISSING_VALUE = 'ecs_err_missing_value';
46  const ERR_INVALID_DATES = 'ecs_err_invalid_dates';
47  const ERR_INVALID_TYPE = 'ecs_err_invalid_type';
48  const ERR_MISSING_BY_TYPE = 'ecs_err_invalid_by_type';
49 
50  protected $db;
51 
52  private $mapping_id;
53  private $container_id;
54  private $field_name;
55  private $mapping_type;
56  private $mapping_value;
57  private $range_dt_start;
58  private $range_dt_end;
59  private $by_type;
60 
65  public function __construct($a_mapping_id = 0)
66  {
67  global $ilDB;
68 
69  $this->mapping_id = $a_mapping_id;
70 
71  $this->db = $ilDB;
72  $this->read();
73  }
74 
80  protected function setMappingId($a_id)
81  {
82  $this->mapping_id = $a_id;
83  }
84 
89  public function getMappingId()
90  {
91  return $this->mapping_id;
92  }
93 
99  public function setContainerId($a_id)
100  {
101  $this->container_id = $a_id;
102  }
103 
108  public function getContainerId()
109  {
110  return $this->container_id;
111  }
112 
118  public function setDateRangeStart($start)
119  {
120  $this->range_dt_start = $start;
121  }
122 
127  public function getDateRangeStart()
128  {
129  return $this->range_dt_start ? $this->range_dt_start : new ilDate(time(),IL_CAL_UNIX);
130  }
131 
137  public function setDateRangeEnd($end)
138  {
139  $this->range_dt_end = $end;
140  }
141 
146  public function getDateRangeEnd()
147  {
148  if($this->range_dt_end)
149  {
150  return $this->range_dt_end;
151  }
152  $this->range_dt_end = $this->getDateRangeStart();
153  $this->range_dt_end->increment(IL_CAL_MONTH,6);
154  return $this->range_dt_end;
155  }
156 
162  public function setFieldName($a_field)
163  {
164  $this->field_name = $a_field;
165  }
166 
171  public function getFieldName()
172  {
173  return $this->field_name;
174  }
175 
181  public function setMappingType($a_type)
182  {
183  $this->mapping_type = $a_type;
184  }
185 
190  public function getMappingType()
191  {
192  return $this->mapping_type;
193  }
194 
200  public function setMappingValue($a_value)
201  {
202  $this->mapping_value = $a_value;
203  }
204 
209  public function getMappingValue()
210  {
211  return $this->mapping_value;
212  }
213 
218  public function getMappingAsArray()
219  {
220  return explode(',',$this->getMappingValue());
221  }
222 
228  public function setByType($a_type)
229  {
230  $this->by_type = $a_type;
231  }
232 
237  public function getByType()
238  {
239  return $this->by_type;
240  }
241 
246  public function delete()
247  {
248  $sta = $this->db->manipulateF('DELETE FROM ecs_container_mapping WHERE mapping_id = %s ',
249  array('integer'),
250  array($this->getMappingId())
251  );
252  }
253 
258  public function update()
259  {
260  if($this->getMappingType() == self::TYPE_BY_TYPE)
261  {
262  $mapping_value = $this->getByType();
263  }
264  else
265  {
266  $mapping_value = $this->getMappingValue();
267  }
268 
269  $sta = $this->db->manipulateF(
270  'UPDATE ecs_container_mapping SET '.
271  'container_id = %s, '.
272  'field_name = %s, '.
273  'mapping_type = %s, '.
274  'mapping_value = %s, '.
275  'date_range_start = %s,'.
276  'date_range_end = %s '.
277  'WHERE mapping_id = %s',
278  array('integer','text','integer','text','integer','integer','integer'),
279  array(
280  $this->getContainerId(),
281  $this->getFieldName(),
282  $this->getMappingType(),
284  $this->getDateRangeStart()->get(IL_CAL_UNIX),
285  $this->getDateRangeEnd()->get(IL_CAL_UNIX),
286  $this->getMappingId())
287  );
288  }
289 
294  public function save()
295  {
296  global $ilDB;
297 
298  if($this->getMappingType() == self::TYPE_BY_TYPE)
299  {
300  $mapping_value = $this->getByType();
301  }
302  else
303  {
304  $mapping_value = $this->getMappingValue();
305  }
306 
307  $mapping_id = $ilDB->nextId('ecs_container_mapping');
308  $sta = $this->db->manipulateF(
309  'INSERT INTO ecs_container_mapping '.
310  '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) '.
311  'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
312  array('integer','integer','text','integer','text','integer','integer'),
313  array(
314  $mapping_id,
315  $this->getContainerId(),
316  $this->getFieldName(),
317  $this->getMappingType(),
319  $this->getDateRangeStart()->get(IL_CAL_UNIX),
320  $this->getDateRangeEnd()->get(IL_CAL_UNIX))
321  );
322  }
323 
328  public function validate()
329  {
331  {
332  return self::ERR_INVALID_TYPE;
333  }
335  {
337  }
338  if($this->getMappingType() == self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end')))
339  {
341  }
342  // handled by form gui?
343  if($this->getMappingType() == self::TYPE_FIXED and !$this->getMappingValue())
344  {
346  }
347  if($this->getMappingType() == self::TYPE_BY_TYPE && $this->getFieldName() != 'type')
348  {
350  }
351  if($this->getMappingType() != self::TYPE_BY_TYPE && $this->getFieldName() == 'type')
352  {
354  }
355  return 0;
356  }
357 
362  public function conditionToString()
363  {
364  global $lng;
365 
366  switch($this->getMappingType())
367  {
368  case self::TYPE_FIXED:
369 
370  if($this->getFieldName() == 'part_id')
371  {
372  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.$this->participantsToString();
373  }
374  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.$this->getMappingValue();
375 
376  case self::TYPE_DURATION:
377  include_once './Services/Calendar/classes/class.ilDatePresentation.php';
378  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.ilDatePresentation::formatPeriod(
379  $this->getDateRangeStart(),
380  $this->getDateRangeEnd());
381 
382  case self::TYPE_BY_TYPE:
383  return $lng->txt('type').': '.$lng->txt('obj_'.$this->getByType());
384  }
385  }
386 
392  public function participantsToString()
393  {
394  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
395 
396  $part_string = "";
397  $part = explode(',',$this->getMappingValue());
398  $counter = 0;
399  foreach($part as $part_id)
400  {
401  if($counter++)
402  {
403  $part_string .= ', ';
404  }
405  $part_string .= '"';
406 
407  $part_id_arr = explode('_', $part_id);
408  if($name = ilECSUtils::lookupParticipantName($part_id_arr[1],$part_id_arr[0]))
409  {
410  $part_string .= $name;
411  }
412  else
413  {
414  $part_string .= $part_id;
415  }
416  $part_string .= '"';
417  }
418  return $part_string;
419  }
420 
426  public function matches(array $a_matchable_content)
427  {
428  if(isset($a_matchable_content[$this->getFieldName()]))
429  {
430  $value = $a_matchable_content[$this->getFieldName()];
431  return $this->matchesValue($value[0], $value[1]);
432  }
433  return false;
434  }
435 
442  protected function matchesValue($a_value,$a_type)
443  {
444  global $ilLog;
445 
446 
447  switch($a_type)
448  {
449  case self::ATTR_ARRAY:
450  $values = explode(',',$a_value);
451  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
452  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
453  break;
454 
455  case self::ATTR_INT:
456  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
457  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
458  $values = array($a_value);
459  break;
460 
461  case self::ATTR_STRING:
462  $values = array($a_value);
463  break;
464  }
465  $values = explode(',',$a_value);
466 
467  foreach($values as $value)
468  {
469  $value = trim($value);
470  switch($this->getMappingType())
471  {
472  case self::TYPE_FIXED:
473 
474  foreach($this->getMappingAsArray() as $attribute_value)
475  {
476  $attribute_value = trim($attribute_value);
477  if(strcasecmp($attribute_value,$value) == 0)
478  {
479  return true;
480  }
481  }
482  break;
483 
484  case self::TYPE_DURATION:
485  include_once './Services/Calendar/classes/class.ilDateTime.php';
486  $tmp_date = new ilDate($a_value,IL_CAL_UNIX);
487  return ilDateTime::_after($tmp_date,$this->getDateRangeStart()) and
488  ilDateTime::_before($tmp_date,$this->getDateRangeEnd());
489  }
490  }
491  return false;
492  }
493 
498  protected function read()
499  {
500  if(!$this->getMappingId())
501  {
502  return false;
503  }
504  $res = $this->db->queryF('SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
505  array('integer'),
506  array($this->getMappingId())
507  );
508  while($row = $this->db->fetchObject($res))
509  {
510  $this->setMappingId($row->mapping_id);
511  $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start,IL_CAL_UNIX) : null);
512  $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end,IL_CAL_UNIX) : null);
513  $this->setMappingType($row->mapping_type);
514  $this->setFieldName($row->field_name);
515  $this->setContainerId($row->container_id);
516 
517  if($this->getMappingType() == self::TYPE_BY_TYPE)
518  {
519  $this->setByType($row->mapping_value);
520  }
521  else
522  {
523  $this->setMappingValue($row->mapping_value);
524  }
525 
526  }
527  return true;
528  }
529 }
530 ?>