ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
62 $attributes = $row->attribute;
63 }
64 return $attributes;
65 }
66
72 public static function getRuleRefIds($a_sid, $a_mid)
73 {
74 global $ilDB;
75
76 $query = 'SELECT DISTINCT(ref_id) ref_id, rid FROM ecs_cmap_rule '.
77 'WHERE sid = '.$ilDB->quote($a_sid,'integer').' '.
78 'AND mid = '.$ilDB->quote($a_mid,'integer').' '.
79 'GROUP BY ref_id'.' '.
80 'ORDER BY rid';
81
82 $res = $ilDB->query($query);
83 $ref_ids = array();
84 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
85 {
86 $ref_ids[] = $row->ref_id;
87 }
88 // check if ref_ids are in tree
89 $checked_ref_ids = [];
90 foreach($ref_ids as $ref_id)
91 {
92 if(
93 $GLOBALS['DIC']->repositoryTree()->isInTree($ref_id))
94 {
95 $checked_ref_ids[] = $ref_id;
96 }
97 }
98 return $checked_ref_ids;
99 }
100
109 public static function getRulesOfRefId($a_sid, $a_mid, $a_ref_id)
110 {
111 global $ilDB;
112
113 $query = 'SELECT rid FROM ecs_cmap_rule '.
114 'WHERE sid = '.$ilDB->quote($a_sid,'integer').' '.
115 'AND mid = '.$ilDB->quote($a_mid,'integer').' '.
116 'AND ref_id = '.$ilDB->quote($a_ref_id,'integer');
117 $res = $ilDB->query($query);
118 $rids = array();
119 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
120 {
121 $rids = $row->rid;
122 }
123 return (array) $rids;
124 }
125
126 public static function hasRules($a_sid, $a_mid, $a_ref_id)
127 {
128 global $ilDB;
129
130 $query = 'SELECT ref_id FROM ecs_cmap_rule '.
131 'WHERE sid = '.$ilDB->quote($a_sid,'integer').' '.
132 'AND mid = '.$ilDB->quote($a_mid,'integer').' '.
133 'AND ref_id = '.$ilDB->quote($a_ref_id,'integer');
134 $res = $ilDB->query($query);
135 return $res->numRows() ? true : false;
136 }
137
144 public static function isMatching($course, $a_sid, $a_mid, $a_ref_id)
145 {
146 global $ilDB;
147
148 $query = 'SELECT rid FROM ecs_cmap_rule '.
149 'WHERE sid = '.$ilDB->quote($a_sid,'integer'). ' '.
150 'AND mid = '.$ilDB->quote($a_mid,'integer').' '.
151 'AND ref_id = '.$ilDB->quote($a_ref_id,'integer').' '.
152 'ORDER BY rid';
153 $res = $ilDB->query($query);
154
155 $does_match = false;
156 $sortable_index = '';
157 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
158 {
159 $rule = new ilECSCourseMappingRule($row->rid);
160 $matches = $rule->matches($course);
161 if($matches == -1)
162 {
163 return '0';
164 }
165 $does_match = true;
166 $sortable_index .= str_pad($matches, 4, '0' ,STR_PAD_LEFT);
167 }
168 if($does_match)
169 {
170 return $sortable_index;
171 }
172 return "0";
173 }
174
183 public static function doMappings($course,$a_sid,$a_mid, $a_ref_id)
184 {
185 global $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 {
198 $rule = new ilECSCourseMappingRule($row->rid);
199 if($level == 1)
200 {
201 $last_level_category[] = $rule->getRefId();
202 }
203
204 $found_new_level = false;
205 $new_level_cats = array();
206 foreach((array) $last_level_category as $cat_ref_id)
207 {
208 $refs = $rule->doMapping($course, $cat_ref_id);
209 foreach($refs as $new_ref_id)
210 {
211 $found_new_level = true;
212 $new_level_cats[] = $new_ref_id;
213 }
214 }
215 if($found_new_level)
216 {
217 $last_level_category = $new_level_cats;
218 }
219 $level++;
220 }
221
222 return (array) $last_level_category;
223 }
224
230 public function doMapping($course,$parent_ref)
231 {
232 global $tree;
233
234 if(!$this->isSubdirCreationEnabled())
235 {
236 return array();
237 }
238 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
240
241 $childs = $tree->getChildsByType($parent_ref,'cat');
242 foreach($values as $value)
243 {
244 $found = false;
245 foreach((array) $childs as $child)
246 {
247 // category already created
248 if(strcmp($child['title'], $value) === 0)
249 {
250 $found = true;
251 $category_references[] = $child['child'];
252 break;
253 }
254 }
255 if(!$found)
256 {
257 $category_references[] = $this->createCategory($value, $parent_ref);
258 }
259 }
260 return (array) $category_references;
261 }
262
267 protected function createCategory($a_title, $a_parent_ref)
268 {
269 // Create category
270 include_once './Modules/Category/classes/class.ilObjCategory.php';
271 $cat = new ilObjCategory();
272 $cat->setOwner(SYSTEM_USER_ID);
273 $cat->setTitle($a_title);
274 $cat->create();
275 $cat->createReference();
276 $cat->putInTree($a_parent_ref);
277 $cat->setPermissions($a_parent_ref);
278 $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
279 $cat->addTranslation(
280 $a_title,
281 $cat->getLongDescription(),
282 $GLOBALS['lng']->getDefaultLanguage(),
283 1
284 );
285 return $cat->getRefId();
286 }
287
288
294 public function matches($course)
295 {
296 if($this->isFilterEnabled())
297 {
298 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
300 $this->logger->dump($values);
301 $index = 0;
302 foreach($values as $value)
303 {
304 $index++;
305 foreach($this->getFilterElements() as $filter_element)
306 {
307 $this->logger->debug('Comparing ' . $value . ' with ' . $filter_element);
308 if(strcmp(trim($value), trim($filter_element)) === 0)
309 {
310 $this->logger->debug($value . ' matches ' . $filter_element);
311 $this->logger->debug('Found index: ' . $index);
312 return $index;
313 }
314 }
315 }
316 return -1;
317 }
318 return 0;
319 }
320
321
331 public static function getInstanceByAttribute($a_sid,$a_mid,$a_ref_id,$a_att)
332 {
333 global $ilDB;
334
335 $query = 'SELECT rid FROM ecs_cmap_rule '.
336 'WHERE sid = '.$ilDB->quote($a_sid,'integer').' '.
337 'AND mid = '.$ilDB->quote($a_mid,'integer').' '.
338 'AND ref_id = '.$ilDB->quote($a_ref_id,'integer').' '.
339 'AND attribute = '.$ilDB->quote($a_att,'text');
340
341 $res = $ilDB->query($query);
342 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
343 {
344 return new ilECSCourseMappingRule($row->rid);
345 }
346 return new ilECSCourseMappingRule();
347 }
348
349 public function setRuleId($a_rule_id)
350 {
351 $this->rid = $a_rule_id;
352 }
353
354 public function getRuleId()
355 {
356 return $this->rid;
357 }
358
359 public function setServerId($a_server_id)
360 {
361 $this->sid = $a_server_id;
362 }
363
364 public function getServerId()
365 {
366 return $this->sid;
367 }
368
369 public function setMid($a_mid)
370 {
371 $this->mid = $a_mid;
372 }
373
374 public function getMid()
375 {
376 return $this->mid;
377 }
378
379 public function setAttribute($a_att)
380 {
381 $this->attribute = $a_att;
382 }
383
384 public function getAttribute()
385 {
386 return $this->attribute;
387 }
388
389 public function setRefId($a_ref_id)
390 {
391 $this->ref_id = $a_ref_id;
392 }
393
394 public function getRefId()
395 {
396 return $this->ref_id;
397 }
398
399 public function enableFilter($a_status)
400 {
401 $this->is_filter = $a_status;
402 }
403
404 public function isFilterEnabled()
405 {
406 return $this->is_filter;
407 }
408
409 public function setFilter($a_filter)
410 {
411 $this->filter = $a_filter;
412 }
413
414 public function getFilter()
415 {
416 return $this->filter;
417 }
418
419 public function getFilterElements()
420 {
421 return (array) $this->filter_elements;
422 }
423
424 public function enableSubdirCreation($a_stat)
425 {
426 $this->create_subdir = $a_stat;
427 }
428
429 public function isSubdirCreationEnabled()
430 {
432 }
433
435 {
436 $this->subdir_type = $a_type;
437 }
438
439 public function getSubDirectoryType()
440 {
441 return self::SUBDIR_VALUE;
442 }
443
444 public function setDirectory($a_dir)
445 {
446 $this->directory = $a_dir;
447 }
448
449 public function getDirectory()
450 {
451 return $this->directory;
452 }
453
454 public function delete()
455 {
456 global $ilDB;
457
458 $query = 'DELETE from ecs_cmap_rule '.
459 'WHERE rid = '.$ilDB->quote($this->getRuleId(),'integer');
460 $ilDB->manipulate($query);
461 return true;
462 }
463
469 public function save()
470 {
471 global $ilDB;
472
473 $this->setRuleId($ilDB->nextId('ecs_cmap_rule'));
474 $query = 'INSERT INTO ecs_cmap_rule '.
475 '(rid,sid,mid,attribute,ref_id,is_filter,filter,create_subdir,subdir_type,directory) '.
476 'VALUES ('.
477 $ilDB->quote($this->getRuleId(),'integer').', '.
478 $ilDB->quote($this->getServerId(),'integer').', '.
479 $ilDB->quote($this->getMid(),'integer').', '.
480 $ilDB->quote($this->getAttribute(),'text').', '.
481 $ilDB->quote($this->getRefId(),'integer').', '.
482 $ilDB->quote($this->isFilterEnabled(),'integer').', '.
483 $ilDB->quote($this->getFilter(),'text').', '.
484 $ilDB->quote($this->isSubdirCreationEnabled(),'integer').', '.
485 $ilDB->quote($this->getSubDirectoryType(),'integer').', '.
486 $ilDB->quote($this->getDirectory(),'text').' '.
487 ')';
488 $ilDB->manipulate($query);
489 return $this->getRuleId();
490 }
491
496 public function update()
497 {
498 global $ilDB;
499
500 $query = 'UPDATE ecs_cmap_rule '.' '.
501 'SET '.
502 'attribute = '.$ilDB->quote($this->getAttribute(),'text').', '.
503 'ref_id = '.$ilDB->quote($this->getRefId(),'integer').', '.
504 'is_filter = '.$ilDB->quote($this->isFilterEnabled(),'integer').', '.
505 'filter = '.$ilDB->quote($this->getFilter(),'text').', '.
506 'create_subdir = '.$ilDB->quote($this->isSubdirCreationEnabled(),'integer').', '.
507 'subdir_type = '.$ilDB->quote($this->getSubDirectoryType(),'integer').', '.
508 'directory = '.$ilDB->quote($this->getDirectory(),'text').' '.
509 'WHERE rid = '.$ilDB->quote($this->getRuleId(),'integer');
510 $ilDB->manipulate($query);
511 }
512
516 protected function read()
517 {
518 global $ilDB;
519
520 if(!$this->getRuleId())
521 {
522 return true;
523 }
524 $query = 'SELECT * from ecs_cmap_rule '.' '.
525 'WHERE rid = '.$ilDB->quote($this->getRuleId(),'integer');
526 $res = $ilDB->query($query);
527 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
528 {
529 $this->setServerId($row->sid);
530 $this->setMid($row->mid);
531 $this->setRefId($row->ref_id);
532 $this->setAttribute($row->attribute);
533 $this->enableFilter($row->is_filter);
534 $this->setFilter($row->filter);
535 $this->enableSubdirCreation($row->create_subdir);
536 $this->setSubDirectoryType($row->subdir_type);
537 $this->setDirectory($row->directory);
538 }
539
540 $this->parseFilter();
541 }
542
546 protected function parseFilter()
547 {
548 $filter = $this->getFilter();
549 //$this->logger->debug('Original filter: ' . $filter);
550
551 $escaped_filter = str_replace('\,', '#:#', $filter);
552 //$this->logger->debug('Escaped filter: ' . $escaped_filter);
553
554 $filter_elements = explode(',', $escaped_filter);
555 foreach((array) $filter_elements as $filter_element)
556 {
557 $replaced = str_replace('#:#', ',', $filter_element);
558 if(strlen(trim($replaced)))
559 {
560 $this->filter_elements[] = $replaced;
561 }
562 }
563 //$this->logger->dump($this->filter_elements);
564 }
565}
566?>
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.
global $ilDB
$a_type
Definition: workflow.php:93