ILIAS  release_8 Revision v8.24
class.ilECSCategoryMappingRule.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
26{
27 public const ATTR_STRING = 1;
28 public const ATTR_INT = 2;
29 public const ATTR_ARRAY = 3;
30
31 public const TYPE_FIXED = 0;
32 public const TYPE_DURATION = 1;
33 public const TYPE_BY_TYPE = 2;
34
35 public const ERR_MISSING_VALUE = 'ecs_err_missing_value';
36 public const ERR_INVALID_DATES = 'ecs_err_invalid_dates';
37 public const ERR_INVALID_TYPE = 'ecs_err_invalid_type';
38 public const ERR_MISSING_BY_TYPE = 'ecs_err_invalid_by_type';
39
43
44 private int $mapping_id;
45 private ?int $container_id = null;
46 private ?string $field_name = null;
48 private ?string $mapping_value = null;
49 private ?ilDate $range_dt_start = null;
50 private ?ilDate $range_dt_end = null;
51 private ?string $by_type = null;
52
57 public function __construct(int $a_mapping_id = 0)
58 {
59 global $DIC;
60
61 $this->db = $DIC->database();
62 $this->language = $DIC->language();
63 $this->logger = $DIC->logger()->wsrv();
64
65 $this->mapping_id = $a_mapping_id;
66
67 $this->read();
68 }
69
75 protected function setMappingId(int $a_id): void
76 {
77 $this->mapping_id = $a_id;
78 }
79
84 public function getMappingId(): int
85 {
86 return $this->mapping_id;
87 }
88
94 public function setContainerId(int $a_id): void
95 {
96 $this->container_id = $a_id;
97 }
98
102 public function getContainerId(): ?int
103 {
104 return $this->container_id;
105 }
106
111 public function setDateRangeStart(ilDate $start): void
112 {
113 $this->range_dt_start = $start;
114 }
115
119 public function getDateRangeStart(): ilDate
120 {
121 return $this->range_dt_start ?: new ilDate(time(), IL_CAL_UNIX);
122 }
123
129 public function setDateRangeEnd(ilDate $end): void
130 {
131 $this->range_dt_end = $end;
132 }
133
138 public function getDateRangeEnd(): ilDate
139 {
140 if ($this->range_dt_end) {
141 return $this->range_dt_end;
142 }
143 $this->range_dt_end = $this->getDateRangeStart();
144 $this->range_dt_end->increment(IL_CAL_MONTH, 6);
145 return $this->range_dt_end;
146 }
147
153 public function setFieldName(string $a_field): void
154 {
155 $this->field_name = $a_field;
156 }
157
162 public function getFieldName(): ?string
163 {
164 return $this->field_name;
165 }
166
171 public function setMappingType(int $a_type): void
172 {
173 $this->mapping_type = $a_type;
174 }
175
179 public function getMappingType(): int
180 {
181 return $this->mapping_type;
182 }
183
188 public function setMappingValue(string $a_value): void
189 {
190 $this->mapping_value = $a_value;
191 }
192
197 public function getMappingValue(): ?string
198 {
200 }
201
206 public function getMappingAsArray(): array
207 {
208 return explode(',', $this->getMappingValue());
209 }
210
216 public function setByType(string $a_type): void
217 {
218 $this->by_type = $a_type;
219 }
220
225 public function getByType(): ?string
226 {
227 return $this->by_type;
228 }
229
235 public function delete(): void
236 {
237 $this->db->manipulateF(
238 'DELETE FROM ecs_container_mapping WHERE mapping_id = %s ',
239 array('integer'),
240 array($this->getMappingId())
241 );
242 }
243
248 public function update(): void
249 {
250 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
251 $mapping_value = $this->getByType();
252 } else {
253 $mapping_value = $this->getMappingValue();
254 }
255
256 $this->db->manipulateF(
257 'UPDATE ecs_container_mapping SET ' .
258 'container_id = %s, ' .
259 'field_name = %s, ' .
260 'mapping_type = %s, ' .
261 'mapping_value = %s, ' .
262 'date_range_start = %s,' .
263 'date_range_end = %s ' .
264 'WHERE mapping_id = %s',
265 array('integer','text','integer','text','integer','integer','integer'),
266 array(
267 $this->getContainerId(),
268 $this->getFieldName(),
269 $this->getMappingType(),
270 $mapping_value,
271 $this->getDateRangeStart()->get(IL_CAL_UNIX),
272 $this->getDateRangeEnd()->get(IL_CAL_UNIX),
273 $this->getMappingId())
274 );
275 }
276
281 public function save(): void
282 {
283 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
284 $mapping_value = $this->getByType();
285 } else {
286 $mapping_value = $this->getMappingValue();
287 }
288
289 $mapping_id = $this->db->nextId('ecs_container_mapping');
290 $this->db->manipulateF(
291 'INSERT INTO ecs_container_mapping ' .
292 '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) ' .
293 'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
294 array('integer','integer','text','integer','text','integer','integer'),
295 array(
297 $this->getContainerId(),
298 $this->getFieldName(),
299 $this->getMappingType(),
300 $mapping_value,
301 $this->getDateRangeStart()->get(IL_CAL_UNIX),
302 $this->getDateRangeEnd()->get(IL_CAL_UNIX))
303 );
304 }
305
309 public function validate(): string
310 {
313 }
316 }
317 if ($this->getMappingType() === self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end'))) {
319 }
320 // handled by form gui?
321 if ($this->getMappingType() === self::TYPE_FIXED && !$this->getMappingValue()) {
323 }
324 if ($this->getMappingType() === self::TYPE_BY_TYPE && $this->getFieldName() !== 'type') {
326 }
327 if ($this->getMappingType() !== self::TYPE_BY_TYPE && $this->getFieldName() === 'type') {
329 }
330 return '';
331 }
332
337 public function conditionToString(): string
338 {
339 switch ($this->getMappingType()) {
340 case self::TYPE_FIXED:
341
342 if ($this->getFieldName() === 'part_id') {
343 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->participantsToString();
344 }
345 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->getMappingValue();
346
348 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . ilDatePresentation::formatPeriod(
349 $this->getDateRangeStart(),
350 $this->getDateRangeEnd()
351 );
352
354 return $this->language->txt('type') . ': ' . $this->language->txt('obj_' . $this->getByType());
355 }
356 return "";
357 }
358
362 public function participantsToString(): string
363 {
364 $part_string = "";
365 $part = explode(',', $this->getMappingValue());
366 $counter = 0;
367 foreach ($part as $part_id) {
368 if ($counter++) {
369 $part_string .= ', ';
370 }
371 $part_string .= '"';
372
373 $part_id_arr = explode('_', $part_id);
374 $name = (count($part_id_arr) === 2) ? ilECSCommunityReader::getInstanceByServerId((int) $part_id_arr[0])
375 ->getParticipantNameByMid((int) $part_id_arr[1]) : "Broken mapping entry in database";
376 if ($name) {
377 $part_string .= $name;
378 } else {
379 $part_string .= $part_id;
380 }
381 $part_string .= '"';
382 }
383 return $part_string;
384 }
385
389 public function matches(array $a_matchable_content): bool
390 {
391 if (isset($a_matchable_content[$this->getFieldName()])) {
392 $value = $a_matchable_content[$this->getFieldName()];
393 return $this->matchesValue($value[0], $value[1]);
394 }
395 return false;
396 }
397
404 protected function matchesValue($a_value, int $a_type): bool
405 {
406 switch ($a_type) {
407 case self::ATTR_ARRAY:
408 $values = explode(',', $a_value);
409 $this->logger->info(__METHOD__ . ': Checking for value: ' . $a_value);
410 $this->logger->info(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
411 break;
412
413 case self::ATTR_INT:
414 $this->logger->info(__METHOD__ . ': Checking for value: ' . $a_value);
415 $this->logger->info(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
416 $values = array((string) $a_value);
417 break;
418
420 $values = array($a_value);
421 break;
422 }
423
424 foreach ($values as $value) {
425 $value = trim($value);
426 switch ($this->getMappingType()) {
427 case self::TYPE_FIXED:
428
429 foreach ($this->getMappingAsArray() as $attribute_value) {
430 $attribute_value = trim($attribute_value);
431 if (strcasecmp($attribute_value, $value) === 0) {
432 return true;
433 }
434 }
435 break;
436
438 $tmp_date = new ilDate($a_value, IL_CAL_UNIX);
439 return ilDateTime::_after($tmp_date, $this->getDateRangeStart()) and
440 ilDateTime::_before($tmp_date, $this->getDateRangeEnd());
441 }
442 }
443 return false;
444 }
445
449 protected function read(): bool
450 {
451 if (!$this->getMappingId()) {
452 return false;
453 }
454 $res = $this->db->queryF(
455 'SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
456 array('integer'),
457 array($this->getMappingId())
458 );
459 while ($row = $this->db->fetchObject($res)) {
460 $this->setMappingId((int) $row->mapping_id);
461 $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start, IL_CAL_UNIX) : null);
462 $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end, IL_CAL_UNIX) : null);
463 $this->setMappingType((int) $row->mapping_type);
464 $this->setFieldName($row->field_name);
465 $this->setContainerId((int) $row->container_id);
466
467 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
468 $this->setByType($row->mapping_value);
469 } else {
470 $this->setMappingValue($row->mapping_value);
471 }
472 }
473 return true;
474 }
475}
const IL_CAL_UNIX
const IL_CAL_MONTH
const IL_CAL_DAY
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
static _after(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
Class for single dates.
Defines a rule for the assignment of ECS remote courses to categories.
setMappingValue(string $a_value)
set mapping value
setDateRangeStart(ilDate $start)
set date range start
participantsToString()
get string presentation of participants
setMappingType(int $a_type)
set mapping type
__construct(int $a_mapping_id=0)
Constructor.
setFieldName(string $a_field)
set field name
matchesValue($a_value, int $a_type)
Check if value matches.
setByType(string $a_type)
set mapping by type
matches(array $a_matchable_content)
Check if rule matches a specific econtent.
getMappingAsArray()
get mapping values as array
setDateRangeEnd(ilDate $end)
set date range end
setContainerId(int $a_id)
set container id
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
language handling
Component logger with individual log levels by component id.
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247