ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
68 
69  $ilDB = $DIC['ilDB'];
70 
71  $this->mapping_id = $a_mapping_id;
72 
73  $this->db = $ilDB;
74  $this->read();
75  }
76 
82  protected function setMappingId($a_id)
83  {
84  $this->mapping_id = $a_id;
85  }
86 
91  public function getMappingId()
92  {
93  return $this->mapping_id;
94  }
95 
101  public function setContainerId($a_id)
102  {
103  $this->container_id = $a_id;
104  }
105 
110  public function getContainerId()
111  {
112  return $this->container_id;
113  }
114 
120  public function setDateRangeStart($start)
121  {
122  $this->range_dt_start = $start;
123  }
124 
129  public function getDateRangeStart()
130  {
131  return $this->range_dt_start ? $this->range_dt_start : new ilDate(time(), IL_CAL_UNIX);
132  }
133 
139  public function setDateRangeEnd($end)
140  {
141  $this->range_dt_end = $end;
142  }
143 
148  public function getDateRangeEnd()
149  {
150  if ($this->range_dt_end) {
151  return $this->range_dt_end;
152  }
153  $this->range_dt_end = $this->getDateRangeStart();
154  $this->range_dt_end->increment(IL_CAL_MONTH, 6);
155  return $this->range_dt_end;
156  }
157 
163  public function setFieldName($a_field)
164  {
165  $this->field_name = $a_field;
166  }
167 
172  public function getFieldName()
173  {
174  return $this->field_name;
175  }
176 
182  public function setMappingType($a_type)
183  {
184  $this->mapping_type = $a_type;
185  }
186 
191  public function getMappingType()
192  {
193  return $this->mapping_type;
194  }
195 
201  public function setMappingValue($a_value)
202  {
203  $this->mapping_value = $a_value;
204  }
205 
210  public function getMappingValue()
211  {
212  return $this->mapping_value;
213  }
214 
219  public function getMappingAsArray()
220  {
221  return explode(',', $this->getMappingValue());
222  }
223 
229  public function setByType($a_type)
230  {
231  $this->by_type = $a_type;
232  }
233 
238  public function getByType()
239  {
240  return $this->by_type;
241  }
242 
247  public function delete()
248  {
249  $sta = $this->db->manipulateF(
250  'DELETE FROM ecs_container_mapping WHERE mapping_id = %s ',
251  array('integer'),
252  array($this->getMappingId())
253  );
254  }
255 
260  public function update()
261  {
262  if ($this->getMappingType() == self::TYPE_BY_TYPE) {
263  $mapping_value = $this->getByType();
264  } else {
265  $mapping_value = $this->getMappingValue();
266  }
267 
268  $sta = $this->db->manipulateF(
269  'UPDATE ecs_container_mapping SET ' .
270  'container_id = %s, ' .
271  'field_name = %s, ' .
272  'mapping_type = %s, ' .
273  'mapping_value = %s, ' .
274  'date_range_start = %s,' .
275  'date_range_end = %s ' .
276  'WHERE mapping_id = %s',
277  array('integer','text','integer','text','integer','integer','integer'),
278  array(
279  $this->getContainerId(),
280  $this->getFieldName(),
281  $this->getMappingType(),
283  $this->getDateRangeStart()->get(IL_CAL_UNIX),
284  $this->getDateRangeEnd()->get(IL_CAL_UNIX),
285  $this->getMappingId())
286  );
287  }
288 
293  public function save()
294  {
295  global $DIC;
296 
297  $ilDB = $DIC['ilDB'];
298 
299  if ($this->getMappingType() == self::TYPE_BY_TYPE) {
300  $mapping_value = $this->getByType();
301  } else {
302  $mapping_value = $this->getMappingValue();
303  }
304 
305  $mapping_id = $ilDB->nextId('ecs_container_mapping');
306  $sta = $this->db->manipulateF(
307  'INSERT INTO ecs_container_mapping ' .
308  '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) ' .
309  'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
310  array('integer','integer','text','integer','text','integer','integer'),
311  array(
312  $mapping_id,
313  $this->getContainerId(),
314  $this->getFieldName(),
315  $this->getMappingType(),
317  $this->getDateRangeStart()->get(IL_CAL_UNIX),
318  $this->getDateRangeEnd()->get(IL_CAL_UNIX))
319  );
320  }
321 
326  public function validate()
327  {
329  return self::ERR_INVALID_TYPE;
330  }
332  return self::ERR_INVALID_DATES;
333  }
334  if ($this->getMappingType() == self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end'))) {
335  return self::ERR_MISSING_VALUE;
336  }
337  // handled by form gui?
338  if ($this->getMappingType() == self::TYPE_FIXED and !$this->getMappingValue()) {
339  return self::ERR_MISSING_VALUE;
340  }
341  if ($this->getMappingType() == self::TYPE_BY_TYPE && $this->getFieldName() != 'type') {
342  return self::ERR_MISSING_BY_TYPE;
343  }
344  if ($this->getMappingType() != self::TYPE_BY_TYPE && $this->getFieldName() == 'type') {
345  return self::ERR_MISSING_VALUE;
346  }
347  return 0;
348  }
349 
354  public function conditionToString()
355  {
356  global $DIC;
357 
358  $lng = $DIC['lng'];
359 
360  switch ($this->getMappingType()) {
361  case self::TYPE_FIXED:
362 
363  if ($this->getFieldName() == 'part_id') {
364  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->participantsToString();
365  }
366  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->getMappingValue();
367 
368  case self::TYPE_DURATION:
369  return $lng->txt('ecs_field_' . $this->getFieldName()) . ': ' . ilDatePresentation::formatPeriod(
370  $this->getDateRangeStart(),
371  $this->getDateRangeEnd()
372  );
373 
374  case self::TYPE_BY_TYPE:
375  return $lng->txt('type') . ': ' . $lng->txt('obj_' . $this->getByType());
376  }
377  }
378 
384  public function participantsToString()
385  {
386  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
387 
388  $part_string = "";
389  $part = explode(',', $this->getMappingValue());
390  $counter = 0;
391  foreach ($part as $part_id) {
392  if ($counter++) {
393  $part_string .= ', ';
394  }
395  $part_string .= '"';
396 
397  $part_id_arr = explode('_', $part_id);
398  if ($name = ilECSUtils::lookupParticipantName($part_id_arr[1], $part_id_arr[0])) {
399  $part_string .= $name;
400  } else {
401  $part_string .= $part_id;
402  }
403  $part_string .= '"';
404  }
405  return $part_string;
406  }
407 
413  public function matches(array $a_matchable_content)
414  {
415  if (isset($a_matchable_content[$this->getFieldName()])) {
416  $value = $a_matchable_content[$this->getFieldName()];
417  return $this->matchesValue($value[0], $value[1]);
418  }
419  return false;
420  }
421 
428  protected function matchesValue($a_value, $a_type)
429  {
430  global $DIC;
431 
432  $ilLog = $DIC['ilLog'];
433 
434 
435  switch ($a_type) {
436  case self::ATTR_ARRAY:
437  $values = explode(',', $a_value);
438  $ilLog->write(__METHOD__ . ': Checking for value: ' . $a_value);
439  $ilLog->write(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
440  break;
441 
442  case self::ATTR_INT:
443  $ilLog->write(__METHOD__ . ': Checking for value: ' . $a_value);
444  $ilLog->write(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
445  $values = array($a_value);
446  break;
447 
448  case self::ATTR_STRING:
449  $values = array($a_value);
450  break;
451  }
452  $values = explode(',', $a_value);
453 
454  foreach ($values as $value) {
455  $value = trim($value);
456  switch ($this->getMappingType()) {
457  case self::TYPE_FIXED:
458 
459  foreach ($this->getMappingAsArray() as $attribute_value) {
460  $attribute_value = trim($attribute_value);
461  if (strcasecmp($attribute_value, $value) == 0) {
462  return true;
463  }
464  }
465  break;
466 
467  case self::TYPE_DURATION:
468  include_once './Services/Calendar/classes/class.ilDateTime.php';
469  $tmp_date = new ilDate($a_value, IL_CAL_UNIX);
470  return ilDateTime::_after($tmp_date, $this->getDateRangeStart()) and
471  ilDateTime::_before($tmp_date, $this->getDateRangeEnd());
472  }
473  }
474  return false;
475  }
476 
481  protected function read()
482  {
483  if (!$this->getMappingId()) {
484  return false;
485  }
486  $res = $this->db->queryF(
487  'SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
488  array('integer'),
489  array($this->getMappingId())
490  );
491  while ($row = $this->db->fetchObject($res)) {
492  $this->setMappingId($row->mapping_id);
493  $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start, IL_CAL_UNIX) : null);
494  $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end, IL_CAL_UNIX) : null);
495  $this->setMappingType($row->mapping_type);
496  $this->setFieldName($row->field_name);
497  $this->setContainerId($row->container_id);
498 
499  if ($this->getMappingType() == self::TYPE_BY_TYPE) {
500  $this->setByType($row->mapping_value);
501  } else {
502  $this->setMappingValue($row->mapping_value);
503  }
504  }
505  return true;
506  }
507 }
static lookupParticipantName($a_owner, $a_server_id)
Lookup participant name.
global $DIC
Definition: saml.php:7
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.
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.
$start
Definition: bench.php:8
$a_type
Definition: workflow.php:92
const IL_CAL_DAY
setByType($a_type)
set mapping by type
Class for single dates.
foreach($_POST as $key=> $value) $res
setDateRangeStart($start)
set date range start
$lng
$values
static _lookupObjId($a_id)
participantsToString()
get strong presentation of participants
static _lookupType($a_id, $a_reference=false)
lookup object type
$row
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 $ilDB
setMappingType($a_type)
set mapping type
setDateRangeEnd($end)
set date range end
__construct($a_mapping_id=0)
Constructor.