ILIAS  eassessment Revision 61809
 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 
44  const ERR_MISSING_VALUE = 'ecs_err_missing_value';
45  const ERR_INVALID_DATES = 'ecs_err_invalid_dates';
46  const ERR_INVALID_TYPE = 'ecs_err_invalid_type';
47 
48  protected $db;
49 
50  private $mapping_id;
51  private $container_id;
52  private $field_name;
53  private $mapping_type;
54  private $mapping_value;
55  private $range_dt_start;
56  private $range_dt_end;
57 
62  public function __construct($a_mapping_id = 0)
63  {
64  global $ilDB;
65 
66  $this->mapping_id = $a_mapping_id;
67 
68  $this->db = $ilDB;
69  $this->read();
70  }
71 
77  protected function setMappingId($a_id)
78  {
79  $this->mapping_id = $a_id;
80  }
81 
86  public function getMappingId()
87  {
88  return $this->mapping_id;
89  }
90 
96  public function setContainerId($a_id)
97  {
98  $this->container_id = $a_id;
99  }
100 
105  public function getContainerId()
106  {
107  return $this->container_id;
108  }
109 
115  public function setDateRangeStart($start)
116  {
117  $this->range_dt_start = $start;
118  }
119 
124  public function getDateRangeStart()
125  {
126  return $this->range_dt_start ? $this->range_dt_start : new ilDate(time(),IL_CAL_UNIX);
127  }
128 
134  public function setDateRangeEnd($end)
135  {
136  $this->range_dt_end = $end;
137  }
138 
143  public function getDateRangeEnd()
144  {
145  if($this->range_dt_end)
146  {
147  return $this->range_dt_end;
148  }
149  $this->range_dt_end = $this->getDateRangeStart();
150  $this->range_dt_end->increment(IL_CAL_MONTH,6);
151  return $this->range_dt_end;
152  }
153 
159  public function setFieldName($a_field)
160  {
161  $this->field_name = $a_field;
162  }
163 
168  public function getFieldName()
169  {
170  return $this->field_name;
171  }
172 
178  public function setMappingType($a_type)
179  {
180  $this->mapping_type = $a_type;
181  }
182 
187  public function getMappingType()
188  {
189  return $this->mapping_type;
190  }
191 
197  public function setMappingValue($a_value)
198  {
199  $this->mapping_value = $a_value;
200  }
201 
206  public function getMappingValue()
207  {
208  return $this->mapping_value;
209  }
210 
215  public function getMappingAsArray()
216  {
217  return explode(',',$this->getMappingValue());
218  }
219 
224  public function delete()
225  {
226  $sta = $this->db->manipulateF('DELETE FROM ecs_container_mapping WHERE mapping_id = %s ',
227  array('integer'),
228  array($this->getMappingId())
229  );
230  }
231 
236  public function update()
237  {
238  $sta = $this->db->manipulateF(
239  'UPDATE ecs_container_mapping SET '.
240  'container_id = %s, '.
241  'field_name = %s, '.
242  'mapping_type = %s, '.
243  'mapping_value = %s, '.
244  'date_range_start = %s,'.
245  'date_range_end = %s '.
246  'WHERE mapping_id = %s',
247  array('integer','text','integer','text','integer','integer','integer'),
248  array(
249  $this->getContainerId(),
250  $this->getFieldName(),
251  $this->getMappingType(),
252  $this->getMappingValue(),
253  $this->getDateRangeStart()->get(IL_CAL_UNIX),
254  $this->getDateRangeEnd()->get(IL_CAL_UNIX),
255  $this->getMappingId())
256  );
257  }
258 
263  public function save()
264  {
265  global $ilDB;
266 
267  $mapping_id = $ilDB->nextId('ecs_container_mapping');
268  $sta = $this->db->manipulateF(
269  'INSERT INTO ecs_container_mapping '.
270  '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) '.
271  'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
272  array('integer','integer','text','integer','text','integer','integer'),
273  array(
274  $mapping_id,
275  $this->getContainerId(),
276  $this->getFieldName(),
277  $this->getMappingType(),
278  $this->getMappingValue(),
279  $this->getDateRangeStart()->get(IL_CAL_UNIX),
280  $this->getDateRangeEnd()->get(IL_CAL_UNIX))
281  );
282  }
283 
288  public function validate()
289  {
291  {
292  return self::ERR_INVALID_TYPE;
293  }
295  {
297  }
298  if($this->getMappingType() == self::TYPE_FIXED and !$this->getMappingValue())
299  {
301  }
302  return 0;
303  }
304 
309  public function conditionToString()
310  {
311  global $lng;
312 
313  switch($this->getMappingType())
314  {
315  case self::TYPE_FIXED:
316 
317  if($this->getFieldName() == 'part_id')
318  {
319  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.$this->participantsToString();
320  }
321  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.$this->getMappingValue();
322 
323  case self::TYPE_DURATION:
324  include_once './Services/Calendar/classes/class.ilDatePresentation.php';
325  return $lng->txt('ecs_field_'.$this->getFieldName()).': '.ilDatePresentation::formatPeriod(
326  $this->getDateRangeStart(),
327  $this->getDateRangeEnd());
328  }
329  }
330 
336  public function participantsToString()
337  {
338  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
339 
340  $part_string = "";
341  $part = explode(',',$this->getMappingValue());
342  $counter = 0;
343  foreach($part as $part_id)
344  {
345  if($counter++)
346  {
347  $part_string .= ', ';
348  }
349  $part_string .= '"';
351  {
352  $part_string .= $name;
353  }
354  else
355  {
356  $part_string .= $part_id;
357  }
358  $part_string .= '"';
359  }
360  return $part_string;
361  }
362 
368  public function matches(ilECSEcontent $econtent)
369  {
370  global $ilLog;
371 
372  switch($this->getFieldName())
373  {
374  case 'study_courses':
375  return $this->matchesValue($econtent->getStudyCourses(),self::ATTR_ARRAY);
376 
377  case 'part_id':
378  return $this->matchesValue($econtent->getOwner(),self::ATTR_INT);
379 
380  case 'begin':
381  if(!is_object($econtent->getTimePlace()))
382  {
383  return false;
384  }
385  return $this->matchesValue($econtent->getTimePlace()->getUTBegin(),self::ATTR_INT);
386 
387  case 'courseType':
388  return $this->matchesValue($econtent->getCourseType(),self::ATTR_STRING);
389 
390  case 'term':
391  return $this->matchesValue($econtent->getTerm(),self::ATTR_STRING);
392 
393  case 'credits':
394  return $this->matchesValue($econtent->getCredits(),self::ATTR_STRING);
395  }
396  return false;
397  }
398 
405  protected function matchesValue($a_value,$a_type)
406  {
407  global $ilLog;
408 
409 
410  switch($a_type)
411  {
412  case self::ATTR_ARRAY:
413  $values = explode(',',$a_value);
414  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
415  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
416  break;
417 
418  case self::ATTR_INT:
419  $ilLog->write(__METHOD__.': Checking for value: '. $a_value);
420  $ilLog->write(__METHOD__.': Checking against attribute values: '. $this->getMappingValue());
421  $values = array($a_value);
422  break;
423 
424  case self::ATTR_INT:
425  $values = array($a_value);
426  break;
427  }
428  $values = explode(',',$a_value);
429 
430  foreach($values as $value)
431  {
432  $value = trim($value);
433  switch($this->getMappingType())
434  {
435  case self::TYPE_FIXED:
436 
437  foreach($this->getMappingAsArray() as $attribute_value)
438  {
439  $attribute_value = trim($attribute_value);
440  if(strcasecmp($attribute_value,$value) == 0)
441  {
442  return true;
443  }
444  }
445  break;
446 
447  case self::TYPE_DURATION:
448  include_once './Services/Calendar/classes/class.ilDateTime.php';
449  $tmp_date = new ilDate($a_value,IL_CAL_UNIX);
450  return ilDateTime::_after($tmp_date,$this->getDateRangeStart()) and
451  ilDateTime::_before($tmp_date,$this->getDateRangeEnd());
452  }
453  }
454  return false;
455  }
456 
461  protected function read()
462  {
463  if(!$this->getMappingId())
464  {
465  return false;
466  }
467  $res = $this->db->queryF('SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
468  array('integer'),
469  array($this->getMappingId())
470  );
471  while($row = $this->db->fetchObject($res))
472  {
473  $this->setMappingId($row->mapping_id);
474  $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start,IL_CAL_UNIX) : null);
475  $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end,IL_CAL_UNIX) : null);
476  $this->setMappingType($row->mapping_type);
477  $this->setMappingValue($row->mapping_value);
478  $this->setFieldName($row->field_name);
479  $this->setContainerId($row->container_id);
480  }
481  return true;
482  }
483 }
484 ?>