ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilECSCourseMappingRule.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
13 const SUBDIR_VALUE = 2;
14
15 private $rid;
16 private $sid;
17 private $mid;
18 private $attribute;
19 private $ref_id;
20 private $is_filter = false;
21 private $filter;
22 private $filter_elements = [];
23 private $create_subdir = true;
25 private $directory = '';
26
30 private $logger = null;
31
36 public function __construct($a_rid = 0)
37 {
38 $this->logger = $GLOBALS['DIC']->logger()->wsrv();
39 $this->rid = $a_rid;
40 $this->read();
41 }
42
48 public static function lookupLastExistingAttribute($a_sid, $a_mid, $a_ref_id)
49 {
50 global $ilDB;
51
52 $query = 'SELECT attribute FROM ecs_cmap_rule ' .
53 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
54 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
55 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
56 'ORDER BY rid ';
57 $res = $ilDB->query($query);
58
59 $attributes = array();
60 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
61 $attributes = $row->attribute;
62 }
63 return $attributes;
64 }
65
71 public static function getRuleRefIds($a_sid, $a_mid)
72 {
73 global $ilDB;
74
75 $query = 'SELECT DISTINCT(ref_id) ref_id, rid FROM ecs_cmap_rule ' .
76 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
77 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
78 'GROUP BY ref_id' . ' ' .
79 'ORDER BY rid';
80
81 $res = $ilDB->query($query);
82 $ref_ids = array();
83 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
84 $ref_ids[] = $row->ref_id;
85 }
86 // check if ref_ids are in tree
87 $checked_ref_ids = [];
88 foreach ($ref_ids as $ref_id) {
89 if (
90 $GLOBALS['DIC']->repositoryTree()->isInTree($ref_id)) {
91 $checked_ref_ids[] = $ref_id;
92 }
93 }
94 return $checked_ref_ids;
95 }
96
105 public static function getRulesOfRefId($a_sid, $a_mid, $a_ref_id)
106 {
107 global $ilDB;
108
109 $query = 'SELECT rid FROM ecs_cmap_rule ' .
110 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
111 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
112 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer');
113 $res = $ilDB->query($query);
114 $rids = array();
115 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
116 $rids = $row->rid;
117 }
118 return (array) $rids;
119 }
120
121 public static function hasRules($a_sid, $a_mid, $a_ref_id)
122 {
123 global $ilDB;
124
125 $query = 'SELECT ref_id FROM ecs_cmap_rule ' .
126 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
127 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
128 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer');
129 $res = $ilDB->query($query);
130 return $res->numRows() ? true : false;
131 }
132
139 public static function isMatching($course, $a_sid, $a_mid, $a_ref_id)
140 {
141 global $ilDB;
142
143 $query = 'SELECT rid FROM ecs_cmap_rule ' .
144 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
145 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
146 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
147 'ORDER BY rid';
148 $res = $ilDB->query($query);
149
150 $does_match = false;
151 $sortable_index = '';
152 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
154 $matches = $rule->matches($course);
155 if ($matches == -1) {
156 return '0';
157 }
158 $does_match = true;
159 $sortable_index .= str_pad($matches, 4, '0', STR_PAD_LEFT);
160 }
161 if ($does_match) {
162 return $sortable_index;
163 }
164 return "0";
165 }
166
175 public static function doMappings($course, $a_sid, $a_mid, $a_ref_id)
176 {
177 global $ilDB;
178
179 $query = 'SELECT rid FROM ecs_cmap_rule ' .
180 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
181 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
182 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
183 'ORDER BY rid';
184 $res = $ilDB->query($query);
185
186 $level = 1;
187 $last_level_category = array();
188 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
190 if ($level == 1) {
191 $last_level_category[] = $rule->getRefId();
192 }
193
194 $found_new_level = false;
195 $new_level_cats = array();
196 foreach ((array) $last_level_category as $cat_ref_id) {
197 $refs = $rule->doMapping($course, $cat_ref_id);
198 foreach ($refs as $new_ref_id) {
199 $found_new_level = true;
200 $new_level_cats[] = $new_ref_id;
201 }
202 }
203 if ($found_new_level) {
204 $last_level_category = $new_level_cats;
205 }
206 $level++;
207 }
208
209 return (array) $last_level_category;
210 }
211
217 public function doMapping($course, $parent_ref)
218 {
219 global $tree;
220
221 if (!$this->isSubdirCreationEnabled()) {
222 return array();
223 }
224 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
226
227 $childs = $tree->getChildsByType($parent_ref, 'cat');
228 foreach ($values as $value) {
229 $found = false;
230 foreach ((array) $childs as $child) {
231 // category already created
232 if (strcmp($child['title'], $value) === 0) {
233 $found = true;
234 $category_references[] = $child['child'];
235 break;
236 }
237 }
238 if (!$found) {
239 $category_references[] = $this->createCategory($value, $parent_ref);
240 }
241 }
242 return (array) $category_references;
243 }
244
249 protected function createCategory($a_title, $a_parent_ref)
250 {
251 // Create category
252 include_once './Modules/Category/classes/class.ilObjCategory.php';
253 $cat = new ilObjCategory();
254 $cat->setOwner(SYSTEM_USER_ID);
255 $cat->setTitle($a_title);
256 $cat->create();
257 $cat->createReference();
258 $cat->putInTree($a_parent_ref);
259 $cat->setPermissions($a_parent_ref);
260 $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
261 $cat->addTranslation(
262 $a_title,
263 $cat->getLongDescription(),
264 $GLOBALS['lng']->getDefaultLanguage(),
265 1
266 );
267 return $cat->getRefId();
268 }
269
270
276 public function matches($course)
277 {
278 if ($this->isFilterEnabled()) {
279 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
281 $this->logger->dump($values);
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
309 public static function getInstanceByAttribute($a_sid, $a_mid, $a_ref_id, $a_att)
310 {
311 global $ilDB;
312
313 $query = 'SELECT rid FROM ecs_cmap_rule ' .
314 'WHERE sid = ' . $ilDB->quote($a_sid, 'integer') . ' ' .
315 'AND mid = ' . $ilDB->quote($a_mid, 'integer') . ' ' .
316 'AND ref_id = ' . $ilDB->quote($a_ref_id, 'integer') . ' ' .
317 'AND attribute = ' . $ilDB->quote($a_att, 'text');
318
319 $res = $ilDB->query($query);
320 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
321 return new ilECSCourseMappingRule($row->rid);
322 }
323 return new ilECSCourseMappingRule();
324 }
325
326 public function setRuleId($a_rule_id)
327 {
328 $this->rid = $a_rule_id;
329 }
330
331 public function getRuleId()
332 {
333 return $this->rid;
334 }
335
336 public function setServerId($a_server_id)
337 {
338 $this->sid = $a_server_id;
339 }
340
341 public function getServerId()
342 {
343 return $this->sid;
344 }
345
346 public function setMid($a_mid)
347 {
348 $this->mid = $a_mid;
349 }
350
351 public function getMid()
352 {
353 return $this->mid;
354 }
355
356 public function setAttribute($a_att)
357 {
358 $this->attribute = $a_att;
359 }
360
361 public function getAttribute()
362 {
363 return $this->attribute;
364 }
365
366 public function setRefId($a_ref_id)
367 {
368 $this->ref_id = $a_ref_id;
369 }
370
371 public function getRefId()
372 {
373 return $this->ref_id;
374 }
375
376 public function enableFilter($a_status)
377 {
378 $this->is_filter = $a_status;
379 }
380
381 public function isFilterEnabled()
382 {
383 return $this->is_filter;
384 }
385
386 public function setFilter($a_filter)
387 {
388 $this->filter = $a_filter;
389 }
390
391 public function getFilter()
392 {
393 return $this->filter;
394 }
395
396 public function getFilterElements()
397 {
398 return (array) $this->filter_elements;
399 }
400
401 public function enableSubdirCreation($a_stat)
402 {
403 $this->create_subdir = $a_stat;
404 }
405
406 public function isSubdirCreationEnabled()
407 {
409 }
410
412 {
413 $this->subdir_type = $a_type;
414 }
415
416 public function getSubDirectoryType()
417 {
418 return self::SUBDIR_VALUE;
419 }
420
421 public function setDirectory($a_dir)
422 {
423 $this->directory = $a_dir;
424 }
425
426 public function getDirectory()
427 {
428 return $this->directory;
429 }
430
431 public function delete()
432 {
433 global $ilDB;
434
435 $query = 'DELETE from ecs_cmap_rule ' .
436 'WHERE rid = ' . $ilDB->quote($this->getRuleId(), 'integer');
437 $ilDB->manipulate($query);
438 return true;
439 }
440
446 public function save()
447 {
448 global $ilDB;
449
450 $this->setRuleId($ilDB->nextId('ecs_cmap_rule'));
451 $query = 'INSERT INTO ecs_cmap_rule ' .
452 '(rid,sid,mid,attribute,ref_id,is_filter,filter,create_subdir,subdir_type,directory) ' .
453 'VALUES (' .
454 $ilDB->quote($this->getRuleId(), 'integer') . ', ' .
455 $ilDB->quote($this->getServerId(), 'integer') . ', ' .
456 $ilDB->quote($this->getMid(), 'integer') . ', ' .
457 $ilDB->quote($this->getAttribute(), 'text') . ', ' .
458 $ilDB->quote($this->getRefId(), 'integer') . ', ' .
459 $ilDB->quote($this->isFilterEnabled(), 'integer') . ', ' .
460 $ilDB->quote($this->getFilter(), 'text') . ', ' .
461 $ilDB->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
462 $ilDB->quote($this->getSubDirectoryType(), 'integer') . ', ' .
463 $ilDB->quote($this->getDirectory(), 'text') . ' ' .
464 ')';
465 $ilDB->manipulate($query);
466 return $this->getRuleId();
467 }
468
473 public function update()
474 {
475 global $ilDB;
476
477 $query = 'UPDATE ecs_cmap_rule ' . ' ' .
478 'SET ' .
479 'attribute = ' . $ilDB->quote($this->getAttribute(), 'text') . ', ' .
480 'ref_id = ' . $ilDB->quote($this->getRefId(), 'integer') . ', ' .
481 'is_filter = ' . $ilDB->quote($this->isFilterEnabled(), 'integer') . ', ' .
482 'filter = ' . $ilDB->quote($this->getFilter(), 'text') . ', ' .
483 'create_subdir = ' . $ilDB->quote($this->isSubdirCreationEnabled(), 'integer') . ', ' .
484 'subdir_type = ' . $ilDB->quote($this->getSubDirectoryType(), 'integer') . ', ' .
485 'directory = ' . $ilDB->quote($this->getDirectory(), 'text') . ' ' .
486 'WHERE rid = ' . $ilDB->quote($this->getRuleId(), 'integer');
487 $ilDB->manipulate($query);
488 }
489
493 protected function read()
494 {
495 global $ilDB;
496
497 if (!$this->getRuleId()) {
498 return true;
499 }
500 $query = 'SELECT * from ecs_cmap_rule ' . ' ' .
501 'WHERE rid = ' . $ilDB->quote($this->getRuleId(), 'integer');
502 $res = $ilDB->query($query);
503 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
504 $this->setServerId($row->sid);
505 $this->setMid($row->mid);
506 $this->setRefId($row->ref_id);
507 $this->setAttribute($row->attribute);
508 $this->enableFilter($row->is_filter);
509 $this->setFilter($row->filter);
510 $this->enableSubdirCreation($row->create_subdir);
511 $this->setSubDirectoryType($row->subdir_type);
512 $this->setDirectory($row->directory);
513 }
514
515 $this->parseFilter();
516 }
517
521 protected function parseFilter()
522 {
523 $filter = $this->getFilter();
524 //$this->logger->debug('Original filter: ' . $filter);
525
526 $escaped_filter = str_replace('\,', '#:#', $filter);
527 //$this->logger->debug('Escaped filter: ' . $escaped_filter);
528
529 $filter_elements = explode(',', $escaped_filter);
530 foreach ((array) $filter_elements as $filter_element) {
531 $replaced = str_replace('#:#', ',', $filter_element);
532 if (strlen(trim($replaced))) {
533 $this->filter_elements[] = $replaced;
534 }
535 }
536 //$this->logger->dump($this->filter_elements);
537 }
538}
An exception for terminatinating execution or to throw for unit testing.
static isMatching($course, $a_sid, $a_mid, $a_ref_id)
Check if rule matches.
static lookupLastExistingAttribute($a_sid, $a_mid, $a_ref_id)
Lookup existing attributes.
save()
Save a new rule @global type $ilDB.
static hasRules($a_sid, $a_mid, $a_ref_id)
doMapping($course, $parent_ref)
Do mapping.
static getRulesOfRefId($a_sid, $a_mid, $a_ref_id)
Get all rule of ref_id @global type $ilDB.
matches($course)
Check if rule matches.
static doMappings($course, $a_sid, $a_mid, $a_ref_id)
static getInstanceByAttribute($a_sid, $a_mid, $a_ref_id, $a_att)
Get rule instance by attribute @global type $ilDB.
static getRuleRefIds($a_sid, $a_mid)
createCategory($a_title, $a_parent_ref)
Create attribute category.
update()
Update mapping rule @global type $ilDB.
static getCourseValueByMappingAttribute($course, $a_field)
Get course value by mapping.
Class ilObjCategory.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$index
Definition: metadata.php:60
$query
foreach($_POST as $key=> $value) $res
$attributes
$rule
Definition: showstats.php:43
global $ilDB
$a_type
Definition: workflow.php:92