ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  return $this->range_dt_end;
150  }
151  $this->range_dt_end = $this->getDateRangeStart();
152  $this->range_dt_end->increment(IL_CAL_MONTH, 6);
153  return $this->range_dt_end;
154  }
155 
161  public function setFieldName($a_field)
162  {
163  $this->field_name = $a_field;
164  }
165 
170  public function getFieldName()
171  {
172  return $this->field_name;
173  }
174 
180  public function setMappingType($a_type)
181  {
182  $this->mapping_type = $a_type;
183  }
184 
189  public function getMappingType()
190  {
191  return $this->mapping_type;
192  }
193 
199  public function setMappingValue($a_value)
200  {
201  $this->mapping_value = $a_value;
202  }
203 
208  public function getMappingValue()
209  {
210  return $this->mapping_value;
211  }
212 
217  public function getMappingAsArray()
218  {
219  return explode(',', $this->getMappingValue());
220  }
221 
227  public function setByType($a_type)
228  {
229  $this->by_type = $a_type;
230  }
231 
236  public function getByType()
237  {
238  return $this->by_type;
239  }
240 
245  public function delete()
246  {
247  $sta = $this->db->manipulateF(
248  '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  $mapping_value = $this->getByType();
262  } else {
263  $mapping_value = $this->getMappingValue();
264  }
265 
266  $sta = $this->db->manipulateF(
267  'UPDATE ecs_container_mapping SET ' .
268  'container_id = %s, ' .
269  'field_name = %s, ' .
270  'mapping_type = %s, ' .
271  'mapping_value = %s, ' .
272  'date_range_start = %s,' .
273  'date_range_end = %s ' .
274  'WHERE mapping_id = %s',
275  array('integer','text','integer','text','integer','integer','integer'),
276  array(
277  $this->getContainerId(),
278  $this->getFieldName(),
279  $this->getMappingType(),
281  $this->getDateRangeStart()->get(IL_CAL_UNIX),
282  $this->getDateRangeEnd()->get(IL_CAL_UNIX),
283  $this->getMappingId())
284  );
285  }
286 
291  public function save()
292  {
293  global $ilDB;
294 
295  if ($this->getMappingType() == self::TYPE_BY_TYPE) {
296  $mapping_value = $this->getByType();
297  } else {
298  $mapping_value = $this->getMappingValue();
299  }
300 
301  $mapping_id = $ilDB->nextId('ecs_container_mapping');
302  $sta = $this->db->manipulateF(
303  'INSERT INTO ecs_container_mapping ' .
304  '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) ' .
305  'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
306  array('integer','integer','text','integer','text','integer','integer'),
307  array(
308  $mapping_id,
309  $this->getContainerId(),
310  $this->getFieldName(),
311  $this->getMappingType(),
313  $this->getDateRangeStart()->get(IL_CAL_UNIX),
314  $this->getDateRangeEnd()->get(IL_CAL_UNIX))
315  );
316  }
317 
322  public function validate()
323  {
325  return self::ERR_INVALID_TYPE;
326  }
328  return self::ERR_INVALID_DATES;
329  }
330  if ($this->getMappingType() == self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end'))) {
331  return self::ERR_MISSING_VALUE;
332  }
333  // handled by form gui?
334  if ($this->getMappingType() == self::TYPE_FIXED and !$this->getMappingValue()) {
335  return self::ERR_MISSING_VALUE;
336  }
337  if ($this->getMappingType() == self::TYPE_BY_TYPE && $this->getFieldName() != 'type') {
338  return self::ERR_MISSING_BY_TYPE;
339  }
340  if ($this->getMappingType() != self::TYPE_BY_TYPE && $this->getFieldName() == 'type') {
341  return self::ERR_MISSING_VALUE;
342  }
343  return 0;
344  }
345 
350  public function conditionToString()
351  {
352  global $lng;
353 
354  switch ($this->getMappingType()) {
355  case self::TYPE_FIXED:
356 
357  if ($this->getFieldName() == 'part_id') {
358  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->participantsToString();
359  }
360  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->getMappingValue();
361 
362  case self::TYPE_DURATION:
363  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . ilDatePresentation::formatPeriod(
364  $this->getDateRangeStart(),
365  $this->getDateRangeEnd()
366  );
367 
368  case self::TYPE_BY_TYPE:
369  return $lng->txt('type') . ': ' . $lng->txt('obj_' . $this->getByType());
370  }
371  }
372 
378  public function participantsToString()
379  {
380  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
381 
382  $part_string = "";
383  $part = explode(',', $this->getMappingValue());
384  $counter = 0;
385  foreach ($part as $part_id) {
386  if ($counter++) {
387  $part_string .= ', ';
388  }
389  $part_string .= '"';
390 
391  $part_id_arr = explode('_', $part_id);
392  if ($name = ilECSUtils::lookupParticipantName($part_id_arr[1], $part_id_arr[0])) {
393  $part_string .= $name;
394  } else {
395  $part_string .= $part_id;
396  }
397  $part_string .= '"';
398  }
399  return $part_string;
400  }
401 
407  public function matches(array $a_matchable_content)
408  {
409  if (isset($a_matchable_content[$this->getFieldName()])) {
410  $value = $a_matchable_content[$this->getFieldName()];
411  return $this->matchesValue($value[0], $value[1]);
412  }
413  return false;
414  }
415 
422  protected function matchesValue($a_value, $a_type)
423  {
424  global $ilLog;
425 
426 
427  switch ($a_type) {
428  case self::ATTR_ARRAY:
429  $values = explode(',', $a_value);
430  $ilLog->write(__METHOD__ . ': Checking for value: ' . $a_value);
431  $ilLog->write(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
432  break;
433 
434  case self::ATTR_INT:
435  $ilLog->write(__METHOD__ . ': Checking for value: ' . $a_value);
436  $ilLog->write(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
437  $values = array($a_value);
438  break;
439 
440  case self::ATTR_STRING:
441  $values = array($a_value);
442  break;
443  }
444  $values = explode(',', $a_value);
445 
446  foreach ($values as $value) {
447  $value = trim($value);
448  switch ($this->getMappingType()) {
449  case self::TYPE_FIXED:
450 
451  foreach ($this->getMappingAsArray() as $attribute_value) {
452  $attribute_value = trim($attribute_value);
453  if (strcasecmp($attribute_value, $value) == 0) {
454  return true;
455  }
456  }
457  break;
458 
459  case self::TYPE_DURATION:
460  include_once './Services/Calendar/classes/class.ilDateTime.php';
461  $tmp_date = new ilDate($a_value, IL_CAL_UNIX);
462  return ilDateTime::_after($tmp_date, $this->getDateRangeStart()) and
463  ilDateTime::_before($tmp_date, $this->getDateRangeEnd());
464  }
465  }
466  return false;
467  }
468 
473  protected function read()
474  {
475  if (!$this->getMappingId()) {
476  return false;
477  }
478  $res = $this->db->queryF(
479  'SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
480  array('integer'),
481  array($this->getMappingId())
482  );
483  while ($row = $this->db->fetchObject($res)) {
484  $this->setMappingId($row->mapping_id);
485  $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start, IL_CAL_UNIX) : null);
486  $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end, IL_CAL_UNIX) : null);
487  $this->setMappingType($row->mapping_type);
488  $this->setFieldName($row->field_name);
489  $this->setContainerId($row->container_id);
490 
491  if ($this->getMappingType() == self::TYPE_BY_TYPE) {
492  $this->setByType($row->mapping_value);
493  } else {
494  $this->setMappingValue($row->mapping_value);
495  }
496  }
497  return true;
498  }
499 }
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.
$end
Definition: saml1-acs.php:18
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:92
const IL_CAL_DAY
if($format !==null) $name
Definition: metadata.php:146
setByType($a_type)
set mapping by type
Class for single dates.
foreach($_POST as $key=> $value) $res
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
static formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day=false)
Format a period of two date Shows: 14.
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.