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