ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilECSCourseMappingRule.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 public const SUBDIR_ATTRIBUTE_NAME = 1;
27 public const SUBDIR_VALUE = 2;
28
30 private ilTree $tree;
33
34 private int $rid;
35 private int $sid;
36 private int $mid;
37 private string $attribute;
38 private int $ref_id;
39 private bool $is_filter = false;
40 private string $filter = "";
41 private array $filter_elements = [];
42 private bool $create_subdir = true;
43 private string $directory = '';
44
45 public function __construct(int $a_rid = 0)
46 {
47 global $DIC;
48
49 $this->logger = $DIC->logger()->wsrv();
50 $this->tree = $DIC->repositoryTree();
51 $this->lng = $DIC->language();
52 $this->db = $DIC->database();
53
54 $this->rid = $a_rid;
55 $this->read();
56 }
57
61 public static function lookupLastExistingAttribute(int $a_sid, int $a_mid, int $a_ref_id): string
62 {
63 global $DIC;
64
65 $ilDB = $DIC['ilDB'];
66
67 $query = 'SELECT attribute FROM ecs_cmap_rule ' .
68 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
69 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
70 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
71 'ORDER BY rid ';
72 $res = $ilDB->query($query);
73
74 $attributes = '';
75 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
76 $attributes = $row->attribute;
77 }
78 return $attributes;
79 }
80
84 public static function getRuleRefIds(int $a_sid, int $a_mid): array
85 {
86 global $DIC;
87
88 $ilDB = $DIC['ilDB'];
89
90 $query = 'SELECT DISTINCT(ref_id) ref_id, rid FROM ecs_cmap_rule ' .
91 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
92 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
93 'GROUP BY ref_id' . ' ' .
94 'ORDER BY rid';
95
96 $res = $ilDB->query($query);
97 $ref_ids = array();
98 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
99 $ref_ids[] = (int) $row->ref_id;
100 }
101 // check if ref_ids are in tree
102 $checked_ref_ids = [];
103 foreach ($ref_ids as $ref_id) {
104 if (
105 $DIC->repositoryTree()->isInTree($ref_id)) {
106 $checked_ref_ids[] = $ref_id;
107 }
108 }
109 return $checked_ref_ids;
110 }
111
116 public static function getRulesOfRefId(int $a_sid, int $a_mid, int $a_ref_id): array
117 {
118 global $DIC;
119
120 $ilDB = $DIC['ilDB'];
121
122 $query = 'SELECT rid FROM ecs_cmap_rule ' .
123 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
124 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
125 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer');
126 $res = $ilDB->query($query);
127 $rids = [];
128 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
129 $rids = $row->rid;
130 }
131 return $rids;
132 }
133
134 public static function hasRules(int $a_sid, int $a_mid, int $a_ref_id): bool
135 {
136 global $DIC;
137
138 $ilDB = $DIC['ilDB'];
139
140 $query = 'SELECT ref_id FROM ecs_cmap_rule ' .
141 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
142 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
143 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer');
144 $res = $ilDB->query($query);
145 return $res->numRows() ? true : false;
146 }
147
152 public static function isMatching($course, $a_sid, $a_mid, $a_ref_id): string
153 {
154 global $DIC;
155
156 $ilDB = $DIC['ilDB'];
157
158 $query = 'SELECT rid FROM ecs_cmap_rule ' .
159 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
160 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
161 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
162 'ORDER BY rid';
163 $res = $ilDB->query($query);
164
165 $does_match = false;
166 $sortable_index = '';
167 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
168 $rule = new ilECSCourseMappingRule((int) $row->rid);
169 $matches = $rule->matches($course);
170 if ($matches === -1) {
171 return '0';
172 }
173 $does_match = true;
174 $sortable_index .= str_pad((string) $matches, 4, '0', STR_PAD_LEFT);
175 }
176 if ($does_match) {
177 return $sortable_index;
178 }
179 return "0";
180 }
181
182 public static function doMappings($course, int $a_sid, int $a_mid, int $a_ref_id): array
183 {
184 global $DIC;
185
186 $ilDB = $DIC['ilDB'];
187
188 $query = 'SELECT rid FROM ecs_cmap_rule ' .
189 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
190 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
191 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
192 'ORDER BY rid';
193 $res = $ilDB->query($query);
194
195 $level = 1;
196 $last_level_category = array();
197 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
198 $rule = new ilECSCourseMappingRule((int) $row->rid);
199 if ($level === 1) {
200 $last_level_category[] = $rule->getRefId();
201 }
202
203 $found_new_level = false;
204 $new_level_cats = array();
205 foreach ($last_level_category as $cat_ref_id) {
206 $refs = $rule->doMapping($course, (int) $cat_ref_id);
207 foreach ($refs as $new_ref_id) {
208 $found_new_level = true;
209 $new_level_cats[] = $new_ref_id;
210 }
211 }
212 if ($found_new_level) {
213 $last_level_category = $new_level_cats;
214 }
215 $level++;
216 }
217
218 return $last_level_category;
219 }
220
224 public function doMapping($course, int $parent_ref): array
225 {
226 if (!$this->isSubdirCreationEnabled()) {
227 return [];
228 }
230
231 $childs = $this->tree->getChildsByType($parent_ref, 'cat');
232 $category_references = [];
233 foreach ($values as $value) {
234 $found = false;
235 foreach ($childs as $child) {
236 // category already created
237 if (strcmp($child['title'], $value) === 0) {
238 $found = true;
239 $category_references[] = $child['child'];
240 break;
241 }
242 }
243 if (!$found) {
244 $category_references[] = $this->createCategory($value, $parent_ref);
245 }
246 }
247 return $category_references;
248 }
249
254 protected function createCategory($a_title, $a_parent_ref): int
255 {
256 // Create category
257 $cat = new ilObjCategory();
258 $cat->setOwner(SYSTEM_USER_ID);
259 $cat->setTitle($a_title);
260 $cat->create();
261 $cat->createReference();
262 $cat->putInTree($a_parent_ref);
263 $cat->setPermissions($a_parent_ref);
264 $cat->deleteTranslation($this->lng->getDefaultLanguage());
265 $cat->addTranslation(
266 $a_title,
267 $cat->getLongDescription(),
268 $this->lng->getDefaultLanguage(),
269 $this->lng->getDefaultLanguage()
270 );
271 return $cat->getRefId();
272 }
273
274
279 public function matches($course): int
280 {
281 if ($this->isFilterEnabled()) {
283 $this->logger->dump($values);
284 $index = 0;
285 foreach ($values as $value) {
286 $index++;
287 foreach ($this->getFilterElements() as $filter_element) {
288 $this->logger->debug('Comparing ' . $value . ' with ' . $filter_element);
289 if (strcmp(trim($value), trim($filter_element)) === 0) {
290 $this->logger->debug($value . ' matches ' . $filter_element);
291 $this->logger->debug('Found index: ' . $index);
292 return $index;
293 }
294 }
295 }
296 return -1;
297 }
298 return 0;
299 }
300
301
305 public static function getInstanceByAttribute($a_sid, $a_mid, $a_ref_id, $a_att): \ilECSCourseMappingRule
306 {
307 global $DIC;
308
309 $ilDB = $DIC['ilDB'];
310
311 $query = 'SELECT rid FROM ecs_cmap_rule ' .
312 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
313 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
314 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
315 'AND attribute = ' . $ilDB->quote($a_att, 'text');
316
317 $res = $ilDB->query($query);
318 if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
319 return new ilECSCourseMappingRule((int) $row->rid);
320 }
321 return new ilECSCourseMappingRule();
322 }
323
324 public function setRuleId(int $a_rule_id): void
325 {
326 $this->rid = $a_rule_id;
327 }
328
329 public function getRuleId(): int
330 {
331 return $this->rid;
332 }
333
334 public function setServerId(int $a_server_id): void
335 {
336 $this->sid = $a_server_id;
337 }
338
339 public function getServerId(): int
340 {
341 return $this->sid;
342 }
343
344 public function setMid(int $a_mid): void
345 {
346 $this->mid = $a_mid;
347 }
348
349 public function getMid(): int
350 {
351 return $this->mid;
352 }
353
354 public function setAttribute(string $a_att): void
355 {
356 $this->attribute = $a_att;
357 }
358
359 public function getAttribute(): string
360 {
361 return $this->attribute;
362 }
363
364 public function setRefId(int $a_ref_id): void
365 {
366 $this->ref_id = $a_ref_id;
367 }
368
369 public function getRefId(): int
370 {
371 return $this->ref_id;
372 }
373
374 public function enableFilter(bool $a_status): void
375 {
376 $this->is_filter = $a_status;
377 }
378
379 public function isFilterEnabled(): bool
380 {
381 return $this->is_filter;
382 }
383
384 public function setFilter(string $a_filter): void
385 {
386 $this->filter = $a_filter;
387 }
388
389 public function getFilter(): string
390 {
391 return $this->filter;
392 }
393
394 public function getFilterElements(): array
395 {
397 }
398
399 public function enableSubdirCreation(bool $a_stat): void
400 {
401 $this->create_subdir = $a_stat;
402 }
403
404 public function isSubdirCreationEnabled(): bool
405 {
407 }
408
409 public function getSubDirectoryType(): int
410 {
411 return self::SUBDIR_VALUE;
412 }
413
414 public function setDirectory(string $a_dir): void
415 {
416 $this->directory = $a_dir;
417 }
418
419 public function getDirectory(): string
420 {
421 return $this->directory;
422 }
423
424 public function delete(): bool
425 {
426 $query = 'DELETE from ecs_cmap_rule ' .
427 'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
428 $this->db->manipulate($query);
429 return true;
430 }
431
435 public function save(): int
436 {
437 $this->setRuleId($this->db->nextId('ecs_cmap_rule'));
438 $query = 'INSERT INTO ecs_cmap_rule ' .
439 '(rid,sid,mid,attribute,ref_id,is_filter,filter,create_subdir,subdir_type,directory) ' .
440 'VALUES (' .
441 $this->db->quote($this->getRuleId(), 'integer') . ', ' .
442 $this->db->quote($this->getServerId(), 'integer') . ', ' .
443 $this->db->quote($this->getMid(), 'integer') . ', ' .
444 $this->db->quote($this->getAttribute(), 'text') . ', ' .
445 $this->db->quote($this->getRefId(), 'integer') . ', ' .
446 $this->db->quote($this->isFilterEnabled(), 'integer') . ', ' .
447 $this->db->quote($this->getFilter(), 'text') . ', ' .
448 $this->db->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
449 $this->db->quote($this->getSubDirectoryType(), 'integer') . ', ' .
450 $this->db->quote($this->getDirectory(), 'text') . ' ' .
451 ')';
452 $this->db->manipulate($query);
453 return $this->getRuleId();
454 }
455
459 public function update(): void
460 {
461 $query = 'UPDATE ecs_cmap_rule ' . ' ' .
462 'SET ' .
463 'attribute = ' . $this->db->quote($this->getAttribute(), 'text') . ', ' .
464 'ref_id = ' . $this->db->quote($this->getRefId(), 'integer') . ', ' .
465 'is_filter = ' . $this->db->quote($this->isFilterEnabled(), 'integer') . ', ' .
466 'filter = ' . $this->db->quote($this->getFilter(), 'text') . ', ' .
467 'create_subdir = ' . $this->db->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
468 'subdir_type = ' . $this->db->quote($this->getSubDirectoryType(), 'integer') . ', ' .
469 'directory = ' . $this->db->quote($this->getDirectory(), 'text') . ' ' .
470 'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
471 $this->db->manipulate($query);
472 }
473
477 protected function read(): void
478 {
479 if (!$this->getRuleId()) {
480 return;
481 }
482 $query = 'SELECT * from ecs_cmap_rule ' . ' ' .
483 'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
484 $res = $this->db->query($query);
485 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
486 $this->setServerId((int) $row->sid);
487 $this->setMid((int) $row->mid);
488 $this->setRefId((int) $row->ref_id);
489 $this->setAttribute($row->attribute);
490 $this->enableFilter((bool) $row->is_filter);
491 $this->setFilter($row->filter);
492 $this->enableSubdirCreation((bool) $row->create_subdir);
493 //TODO create database update step to remove unneed variable
494 $this->setDirectory($row->directory);
495 }
496
497 $this->parseFilter();
498 }
499
503 protected function parseFilter(): void
504 {
505 $filter = $this->getFilter();
506 //$this->logger->debug('Original filter: ' . $filter);
507
508 $escaped_filter = str_replace('\,', '#:#', $filter);
509 //$this->logger->debug('Escaped filter: ' . $escaped_filter);
510
511 $filter_elements = explode(',', $escaped_filter);
512 foreach ((array) $filter_elements as $filter_element) {
513 $replaced = str_replace('#:#', ',', $filter_element);
514 if (trim($replaced) !== '') {
515 $this->filter_elements[] = $replaced;
516 }
517 }
518 //$this->logger->dump($this->filter_elements);
519 }
520}
return true
static hasRules(int $a_sid, int $a_mid, int $a_ref_id)
static isMatching($course, $a_sid, $a_mid, $a_ref_id)
Check if rule matches.
static getRuleRefIds(int $a_sid, int $a_mid)
static doMappings($course, int $a_sid, int $a_mid, int $a_ref_id)
static lookupLastExistingAttribute(int $a_sid, int $a_mid, int $a_ref_id)
Lookup existing attributes.
static getRulesOfRefId(int $a_sid, int $a_mid, int $a_ref_id)
Get all rule of ref_id.
matches($course)
Check if rule matches.
doMapping($course, int $parent_ref)
Do mapping.
static getInstanceByAttribute($a_sid, $a_mid, $a_ref_id, $a_att)
Get rule instance by attribute.
createCategory($a_title, $a_parent_ref)
Create attribute category.
static getCourseValueByMappingAttribute($course, $a_field)
Get course value by mapping.
language handling
Component logger with individual log levels by component id.
Class ilObjCategory.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
global $DIC
Definition: shib_login.php:26