ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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, int $a_sid, int $a_mid, int $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 = [];
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 = [];
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  $children = $this->tree->getChildsByType($parent_ref, 'cat');
231  $category_references = [];
232  foreach ($values as $value) {
233  $found = false;
234  foreach ($children as $child) {
235  // category already created
236  if (strcmp($child['title'], $value) === 0) {
237  $found = true;
238  $category_references[] = (int) $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  private function matches($course): int
279  {
280  if ($this->isFilterEnabled()) {
282  $index = 0;
283  foreach ($values as $value) {
284  $index++;
285  foreach ($this->getFilterElements() as $filter_element) {
286  $this->logger->debug('Comparing ' . $value . ' with ' . $filter_element);
287  if (strcmp(trim($value), trim($filter_element)) === 0) {
288  $this->logger->debug($value . ' matches ' . $filter_element);
289  $this->logger->debug('Found index: ' . $index);
290  return $index;
291  }
292  }
293  }
294  return -1;
295  }
296  return 0;
297  }
298 
299 
303  public static function getInstanceByAttribute($a_sid, $a_mid, $a_ref_id, $a_att): \ilECSCourseMappingRule
304  {
305  global $DIC;
306 
307  $ilDB = $DIC['ilDB'];
308 
309  $query = 'SELECT rid FROM ecs_cmap_rule ' .
310  'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
311  'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
312  'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
313  'AND attribute = ' . $ilDB->quote($a_att, 'text');
314 
315  $res = $ilDB->query($query);
316  if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
317  return new ilECSCourseMappingRule((int) $row->rid);
318  }
319  return new ilECSCourseMappingRule();
320  }
321 
322  public function setRuleId(int $a_rule_id): void
323  {
324  $this->rid = $a_rule_id;
325  }
326 
327  public function getRuleId(): int
328  {
329  return $this->rid;
330  }
331 
332  public function setServerId(int $a_server_id): void
333  {
334  $this->sid = $a_server_id;
335  }
336 
337  public function getServerId(): int
338  {
339  return $this->sid;
340  }
341 
342  public function setMid(int $a_mid): void
343  {
344  $this->mid = $a_mid;
345  }
346 
347  public function getMid(): int
348  {
349  return $this->mid;
350  }
351 
352  public function setAttribute(string $a_att): void
353  {
354  $this->attribute = $a_att;
355  }
356 
357  public function getAttribute(): string
358  {
359  return $this->attribute;
360  }
361 
362  public function setRefId(int $a_ref_id): void
363  {
364  $this->ref_id = $a_ref_id;
365  }
366 
367  public function getRefId(): int
368  {
369  return $this->ref_id;
370  }
371 
372  public function enableFilter(bool $a_status): void
373  {
374  $this->is_filter = $a_status;
375  }
376 
377  public function isFilterEnabled(): bool
378  {
379  return $this->is_filter;
380  }
381 
382  public function setFilter(string $a_filter): void
383  {
384  $this->filter = $a_filter;
385  }
386 
387  public function getFilter(): string
388  {
389  return $this->filter;
390  }
391 
392  public function getFilterElements(): array
393  {
394  return $this->filter_elements;
395  }
396 
397  public function enableSubdirCreation(bool $a_stat): void
398  {
399  $this->create_subdir = $a_stat;
400  }
401 
402  public function isSubdirCreationEnabled(): bool
403  {
404  return $this->create_subdir;
405  }
406 
407  public function getSubDirectoryType(): int
408  {
409  return self::SUBDIR_VALUE;
410  }
411 
412  public function setDirectory(string $a_dir): void
413  {
414  $this->directory = $a_dir;
415  }
416 
417  public function getDirectory(): string
418  {
419  return $this->directory;
420  }
421 
422  public function delete(): bool
423  {
424  $query = 'DELETE from ecs_cmap_rule ' .
425  'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
426  $this->db->manipulate($query);
427  return true;
428  }
429 
433  public function save(): int
434  {
435  $this->setRuleId($this->db->nextId('ecs_cmap_rule'));
436  $query = 'INSERT INTO ecs_cmap_rule ' .
437  '(rid,sid,mid,attribute,ref_id,is_filter,filter,create_subdir,subdir_type,directory) ' .
438  'VALUES (' .
439  $this->db->quote($this->getRuleId(), 'integer') . ', ' .
440  $this->db->quote($this->getServerId(), 'integer') . ', ' .
441  $this->db->quote($this->getMid(), 'integer') . ', ' .
442  $this->db->quote($this->getAttribute(), 'text') . ', ' .
443  $this->db->quote($this->getRefId(), 'integer') . ', ' .
444  $this->db->quote($this->isFilterEnabled(), 'integer') . ', ' .
445  $this->db->quote($this->getFilter(), 'text') . ', ' .
446  $this->db->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
447  $this->db->quote($this->getSubDirectoryType(), 'integer') . ', ' .
448  $this->db->quote($this->getDirectory(), 'text') . ' ' .
449  ')';
450  $this->db->manipulate($query);
451  return $this->getRuleId();
452  }
453 
457  public function update(): void
458  {
459  $query = 'UPDATE ecs_cmap_rule ' . ' ' .
460  'SET ' .
461  'attribute = ' . $this->db->quote($this->getAttribute(), 'text') . ', ' .
462  'ref_id = ' . $this->db->quote($this->getRefId(), 'integer') . ', ' .
463  'is_filter = ' . $this->db->quote($this->isFilterEnabled(), 'integer') . ', ' .
464  'filter = ' . $this->db->quote($this->getFilter(), 'text') . ', ' .
465  'create_subdir = ' . $this->db->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
466  'subdir_type = ' . $this->db->quote($this->getSubDirectoryType(), 'integer') . ', ' .
467  'directory = ' . $this->db->quote($this->getDirectory(), 'text') . ' ' .
468  'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
469  $this->db->manipulate($query);
470  }
471 
475  protected function read(): void
476  {
477  if (!$this->getRuleId()) {
478  return;
479  }
480  $query = 'SELECT * from ecs_cmap_rule ' . ' ' .
481  'WHERE rid = ' . $this->db->quote($this->getRuleId(), 'integer');
482  $res = $this->db->query($query);
483  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
484  $this->setServerId((int) $row->sid);
485  $this->setMid((int) $row->mid);
486  $this->setRefId((int) $row->ref_id);
487  $this->setAttribute($row->attribute);
488  $this->enableFilter((bool) $row->is_filter);
489  $this->setFilter($row->filter);
490  $this->enableSubdirCreation((bool) $row->create_subdir);
491  //TODO create database update step to remove unneed variable
492  $this->setDirectory($row->directory);
493  }
494 
495  $this->parseFilter();
496  }
497 
501  protected function parseFilter(): void
502  {
503  $filter = $this->getFilter();
504  //$this->logger->debug('Original filter: ' . $filter);
505 
506  $escaped_filter = str_replace('\,', '#:#', $filter);
507  //$this->logger->debug('Escaped filter: ' . $escaped_filter);
508 
509  $filter_elements = explode(',', $escaped_filter);
510  foreach ((array) $filter_elements as $filter_element) {
511  $replaced = str_replace('#:#', ',', $filter_element);
512  if (trim($replaced) !== '') {
513  $this->filter_elements[] = $replaced;
514  }
515  }
516  //$this->logger->dump($this->filter_elements);
517  }
518 }
$attributes
Definition: metadata.php:248
$res
Definition: ltiservices.php:69
createCategory($a_title, $a_parent_ref)
Create attribute category.
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.
$index
Definition: metadata.php:145
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.
static isMatching($course, int $a_sid, int $a_mid, int $a_ref_id)
Check if rule matches.
global $DIC
Definition: feed.php:28
static getRuleRefIds(int $a_sid, int $a_mid)
$query
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)
doMapping($course, int $parent_ref)
Do mapping.