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