ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
336  return self::ERR_INVALID_DATES;
337  }
338  if($this->getMappingType() == self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end')))
339  {
340  return self::ERR_MISSING_VALUE;
341  }
342  // handled by form gui?
343  if($this->getMappingType() == self::TYPE_FIXED and !$this->getMappingValue())
344  {
345  return self::ERR_MISSING_VALUE;
346  }
347  if($this->getMappingType() == self::TYPE_BY_TYPE && $this->getFieldName() != 'type')
348  {
349  return self::ERR_MISSING_BY_TYPE;
350  }
351  if($this->getMappingType() != self::TYPE_BY_TYPE && $this->getFieldName() == 'type')
352  {
353  return self::ERR_MISSING_VALUE;
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  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.ilDatePresentation::formatPeriod(
378  $this->getDateRangeStart(),
379  $this->getDateRangeEnd());
380 
381  case self::TYPE_BY_TYPE:
382  return $lng->txt('type').': '.$lng->txt('obj_'.$this->getByType());
383  }
384  }
385 
391  public function participantsToString()
392  {
393  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
394 
395  $part_string = "";
396  $part = explode(',',$this->getMappingValue());
397  $counter = 0;
398  foreach($part as $part_id)
399  {
400  if($counter++)
401  {
402  $part_string .= ', ';
403  }
404  $part_string .= '"';
405 
406  $part_id_arr = explode('_', $part_id);
407  if($name = ilECSUtils::lookupParticipantName($part_id_arr[1],$part_id_arr[0]))
408  {
409  $part_string .= $name;
410  }
411  else
412  {
413  $part_string .= $part_id;
414  }
415  $part_string .= '"';
416  }
417  return $part_string;
418  }
419 
425  public function matches(array $a_matchable_content)
426  {
427  if(isset($a_matchable_content[$this->getFieldName()]))
428  {
429  $value = $a_matchable_content[$this->getFieldName()];
430  return $this->matchesValue($value[0], $value[1]);
431  }
432  return false;
433  }
434 
441  protected function matchesValue($a_value,$a_type)
442  {
443  global $ilLog;
444 
445 
446  switch($a_type)
447  {
448  case self::ATTR_ARRAY:
449  $values = explode(',',$a_value);
450  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
451  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
452  break;
453 
454  case self::ATTR_INT:
455  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
456  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
457  $values = array($a_value);
458  break;
459 
460  case self::ATTR_STRING:
461  $values = array($a_value);
462  break;
463  }
464  $values = explode(',',$a_value);
465 
466  foreach($values as $value)
467  {
468  $value = trim($value);
469  switch($this->getMappingType())
470  {
471  case self::TYPE_FIXED:
472 
473  foreach($this->getMappingAsArray() as $attribute_value)
474  {
475  $attribute_value = trim($attribute_value);
476  if(strcasecmp($attribute_value,$value) == 0)
477  {
478  return true;
479  }
480  }
481  break;
482 
483  case self::TYPE_DURATION:
484  include_once './Services/Calendar/classes/class.ilDateTime.php';
485  $tmp_date = new ilDate($a_value,IL_CAL_UNIX);
486  return ilDateTime::_after($tmp_date,$this->getDateRangeStart()) and
487  ilDateTime::_before($tmp_date,$this->getDateRangeEnd());
488  }
489  }
490  return false;
491  }
492 
497  protected function read()
498  {
499  if(!$this->getMappingId())
500  {
501  return false;
502  }
503  $res = $this->db->queryF('SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
504  array('integer'),
505  array($this->getMappingId())
506  );
507  while($row = $this->db->fetchObject($res))
508  {
509  $this->setMappingId($row->mapping_id);
510  $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start,IL_CAL_UNIX) : null);
511  $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end,IL_CAL_UNIX) : null);
512  $this->setMappingType($row->mapping_type);
513  $this->setFieldName($row->field_name);
514  $this->setContainerId($row->container_id);
515 
516  if($this->getMappingType() == self::TYPE_BY_TYPE)
517  {
518  $this->setByType($row->mapping_value);
519  }
520  else
521  {
522  $this->setMappingValue($row->mapping_value);
523  }
524 
525  }
526  return true;
527  }
528 }
529 ?>
static lookupParticipantName($a_owner, $a_server_id)
Lookup participant name.
static _after(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
matchesValue($a_value, $a_type)
Check if value matches.
static _before(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
static formatPeriod(ilDateTime $start, ilDateTime $end)
Format a period of two date Shows: 14.
Defines a rule for the assignment of ECS remote courses to categories.
const IL_CAL_MONTH
getMappingAsArray()
get mapping values as array
const IL_CAL_UNIX
matches(array $a_matchable_content)
Check if rule matches a specific econtent.
$counter
$a_type
Definition: workflow.php:93
const IL_CAL_DAY
setByType($a_type)
set mapping by type
Class for single dates.
setDateRangeStart($start)
set date range start
static _lookupObjId($a_id)
participantsToString()
get strong presentation of participants
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
setMappingValue($a_value)
set mapping value
global $lng
Definition: privfeed.php:17
global $ilDB
setMappingType($a_type)
set mapping type
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
setDateRangeEnd($end)
set date range end
__construct($a_mapping_id=0)
Constructor.