ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilECSCategoryMappingRule.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const ATTR_STRING = 1;
29 public const ATTR_INT = 2;
30 public const ATTR_ARRAY = 3;
31
32 public const TYPE_FIXED = 0;
33 public const TYPE_DURATION = 1;
34 public const TYPE_BY_TYPE = 2;
35
36 public const ERR_MISSING_VALUE = 'ecs_err_missing_value';
37 public const ERR_INVALID_DATES = 'ecs_err_invalid_dates';
38 public const ERR_INVALID_TYPE = 'ecs_err_invalid_type';
39 public const ERR_MISSING_BY_TYPE = 'ecs_err_invalid_by_type';
40
44
45 private int $mapping_id;
46 private ?int $container_id = null;
47 private ?string $field_name = null;
49 private ?string $mapping_value = null;
50 private ?ilDate $range_dt_start = null;
51 private ?ilDate $range_dt_end = null;
52 private ?string $by_type = null;
53
58 public function __construct(int $a_mapping_id = 0)
59 {
60 global $DIC;
61
62 $this->db = $DIC->database();
63 $this->language = $DIC->language();
64 $this->logger = $DIC->logger()->wsrv();
65
66 $this->mapping_id = $a_mapping_id;
67
68 $this->read();
69 }
70
76 protected function setMappingId(int $a_id): void
77 {
78 $this->mapping_id = $a_id;
79 }
80
85 public function getMappingId(): int
86 {
87 return $this->mapping_id;
88 }
89
95 public function setContainerId(int $a_id): void
96 {
97 $this->container_id = $a_id;
98 }
99
103 public function getContainerId(): ?int
104 {
105 return $this->container_id;
106 }
107
112 public function setDateRangeStart(ilDate $start): void
113 {
114 $this->range_dt_start = $start;
115 }
116
120 public function getDateRangeStart(): ilDate
121 {
122 return $this->range_dt_start ?: new ilDate(time(), IL_CAL_UNIX);
123 }
124
130 public function setDateRangeEnd(ilDate $end): void
131 {
132 $this->range_dt_end = $end;
133 }
134
139 public function getDateRangeEnd(): ilDate
140 {
141 if ($this->range_dt_end) {
142 return $this->range_dt_end;
143 }
144 $this->range_dt_end = $this->getDateRangeStart();
145 $this->range_dt_end->increment(IL_CAL_MONTH, 6);
146 return $this->range_dt_end;
147 }
148
154 public function setFieldName(string $a_field): void
155 {
156 $this->field_name = $a_field;
157 }
158
163 public function getFieldName(): ?string
164 {
165 return $this->field_name;
166 }
167
172 public function setMappingType(int $a_type): void
173 {
174 $this->mapping_type = $a_type;
175 }
176
180 public function getMappingType(): int
181 {
182 return $this->mapping_type;
183 }
184
189 public function setMappingValue(string $a_value): void
190 {
191 $this->mapping_value = $a_value;
192 }
193
198 public function getMappingValue(): ?string
199 {
201 }
202
207 public function getMappingAsArray(): array
208 {
209 return explode(',', $this->getMappingValue());
210 }
211
217 public function setByType(string $a_type): void
218 {
219 $this->by_type = $a_type;
220 }
221
226 public function getByType(): ?string
227 {
228 return $this->by_type;
229 }
230
236 public function delete(): void
237 {
238 $this->db->manipulateF(
239 'DELETE FROM ecs_container_mapping WHERE mapping_id = %s ',
240 array('integer'),
241 array($this->getMappingId())
242 );
243 }
244
249 public function update(): void
250 {
251 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
252 $mapping_value = $this->getByType();
253 } else {
254 $mapping_value = $this->getMappingValue();
255 }
256
257 $this->db->manipulateF(
258 'UPDATE ecs_container_mapping SET ' .
259 'container_id = %s, ' .
260 'field_name = %s, ' .
261 'mapping_type = %s, ' .
262 'mapping_value = %s, ' .
263 'date_range_start = %s,' .
264 'date_range_end = %s ' .
265 'WHERE mapping_id = %s',
266 array('integer','text','integer','text','integer','integer','integer'),
267 array(
268 $this->getContainerId(),
269 $this->getFieldName(),
270 $this->getMappingType(),
271 $mapping_value,
272 $this->getDateRangeStart()->get(IL_CAL_UNIX),
273 $this->getDateRangeEnd()->get(IL_CAL_UNIX),
274 $this->getMappingId())
275 );
276 }
277
282 public function save(): void
283 {
284 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
285 $mapping_value = $this->getByType();
286 } else {
287 $mapping_value = $this->getMappingValue();
288 }
289
290 $mapping_id = $this->db->nextId('ecs_container_mapping');
291 $this->db->manipulateF(
292 'INSERT INTO ecs_container_mapping ' .
293 '(mapping_id,container_id,field_name,mapping_type,mapping_value,date_range_start,date_range_end) ' .
294 'VALUES(%s,%s,%s,%s,%s,%s,%s) ',
295 array('integer','integer','text','integer','text','integer','integer'),
296 array(
298 $this->getContainerId(),
299 $this->getFieldName(),
300 $this->getMappingType(),
301 $mapping_value,
302 $this->getDateRangeStart()->get(IL_CAL_UNIX),
303 $this->getDateRangeEnd()->get(IL_CAL_UNIX))
304 );
305 }
306
310 public function validate(): string
311 {
314 }
317 }
318 if ($this->getMappingType() === self::TYPE_DURATION && !in_array($this->getFieldName(), array('begin', 'end'))) {
320 }
321 // handled by form gui?
322 if ($this->getMappingType() === self::TYPE_FIXED && !$this->getMappingValue()) {
324 }
325 if ($this->getMappingType() === self::TYPE_BY_TYPE && $this->getFieldName() !== 'type') {
327 }
328 if ($this->getMappingType() !== self::TYPE_BY_TYPE && $this->getFieldName() === 'type') {
330 }
331 return '';
332 }
333
338 public function conditionToString(): string
339 {
340 switch ($this->getMappingType()) {
341 case self::TYPE_FIXED:
342
343 if ($this->getFieldName() === 'part_id') {
344 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->participantsToString();
345 }
346 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . $this->getMappingValue();
347
349 return $this->language->txt('ecs_field_' . $this->getFieldName()) . ': ' . ilDatePresentation::formatPeriod(
350 $this->getDateRangeStart(),
351 $this->getDateRangeEnd()
352 );
353
355 return $this->language->txt('type') . ': ' . $this->language->txt('obj_' . $this->getByType());
356 }
357 return "";
358 }
359
363 public function participantsToString(): string
364 {
365 $part_string = "";
366 $part = explode(',', $this->getMappingValue());
367 $counter = 0;
368 foreach ($part as $part_id) {
369 if ($counter++) {
370 $part_string .= ', ';
371 }
372 $part_string .= '"';
373
374 $part_id_arr = explode('_', $part_id);
375 $name = (count($part_id_arr) === 2) ? ilECSCommunityReader::getInstanceByServerId((int) $part_id_arr[0])
376 ->getParticipantNameByMid((int) $part_id_arr[1]) : "Broken mapping entry in database";
377 if ($name) {
378 $part_string .= $name;
379 } else {
380 $part_string .= $part_id;
381 }
382 $part_string .= '"';
383 }
384 return $part_string;
385 }
386
390 public function matches(array $a_matchable_content): bool
391 {
392 if (isset($a_matchable_content[$this->getFieldName()])) {
393 $value = $a_matchable_content[$this->getFieldName()];
394 return $this->matchesValue($value[0], $value[1]);
395 }
396 return false;
397 }
398
405 protected function matchesValue($a_value, int $a_type): bool
406 {
407 switch ($a_type) {
408 case self::ATTR_ARRAY:
409 $values = explode(',', $a_value);
410 $this->logger->info(__METHOD__ . ': Checking for value: ' . $a_value);
411 $this->logger->info(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
412 break;
413
414 case self::ATTR_INT:
415 $this->logger->info(__METHOD__ . ': Checking for value: ' . $a_value);
416 $this->logger->info(__METHOD__ . ': Checking against attribute values: ' . $this->getMappingValue());
417 $values = array((string) $a_value);
418 break;
419
421 $values = array($a_value);
422 break;
423 }
424
425 foreach ($values as $value) {
426 $value = trim($value);
427 switch ($this->getMappingType()) {
428 case self::TYPE_FIXED:
429
430 foreach ($this->getMappingAsArray() as $attribute_value) {
431 $attribute_value = trim($attribute_value);
432 if (strcasecmp($attribute_value, $value) === 0) {
433 return true;
434 }
435 }
436 break;
437
439 $tmp_date = new ilDate($a_value, IL_CAL_UNIX);
440 return ilDateTime::_after($tmp_date, $this->getDateRangeStart()) and
441 ilDateTime::_before($tmp_date, $this->getDateRangeEnd());
442 }
443 }
444 return false;
445 }
446
450 protected function read(): bool
451 {
452 if (!$this->getMappingId()) {
453 return false;
454 }
455 $res = $this->db->queryF(
456 'SELECT * FROM ecs_container_mapping WHERE mapping_id = %s',
457 array('integer'),
458 array($this->getMappingId())
459 );
460 while ($row = $this->db->fetchObject($res)) {
461 $this->setMappingId((int) $row->mapping_id);
462 $this->setDateRangeStart($row->date_range_start ? new ilDate($row->date_range_start, IL_CAL_UNIX) : null);
463 $this->setDateRangeEnd($row->date_range_end ? new ilDate($row->date_range_end, IL_CAL_UNIX) : null);
464 $this->setMappingType((int) $row->mapping_type);
465 $this->setFieldName($row->field_name);
466 $this->setContainerId((int) $row->container_id);
467
468 if ($this->getMappingType() === self::TYPE_BY_TYPE) {
469 $this->setByType($row->mapping_value);
470 } else {
471 $this->setMappingValue($row->mapping_value);
472 }
473 }
474 return true;
475 }
476}
const IL_CAL_UNIX
const IL_CAL_MONTH
const IL_CAL_DAY
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
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)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26
$counter