ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilECSCourseCreationHandler.php
Go to the documentation of this file.
1<?php
2
3include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingSettings.php';
4include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
5include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
6
7/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
8
15{
19 protected $log;
20
21
22 private $server = null;
23 private $mapping = null;
24 private $course_url = null;
25 private $object_created = false;
26 private $courses_created = array();
27
28 private $mid;
29
30
35 public function __construct(ilECSSetting $server,$a_mid)
36 {
37 $this->log = $GLOBALS['DIC']->logger()->wsrv();
38
39 $this->server = $server;
40 $this->mid = $a_mid;
41 $this->mapping = ilECSNodeMappingSettings::getInstanceByServerMid($this->getServer()->getServerId(), $this->getMid());
42
43 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseUrl.php';
44 $this->course_url = new ilECSCourseUrl();
45 }
46
47
52 public function getServer()
53 {
54 return $this->server;
55 }
56
61 public function getMapping()
62 {
63 return $this->mapping;
64 }
65
70 public function getCourseUrl()
71 {
72 return $this->course_url;
73 }
74
79 public function isObjectCreated()
80 {
82 }
83
88 public function setObjectCreated($a_status)
89 {
90 $this->object_created = $a_status;
91 }
92
97 protected function getCreatedCourses()
98 {
100 }
101
106 public function getMid()
107 {
108 return $this->mid;
109 }
110
116 public function handle($a_content_id,$course)
117 {
118 // prepare course url
119 // if any object (course group) will be created, a list of all course urls
120 // will be sent to ecs.
121 $this->setObjectCreated(false);
122 $this->getCourseUrl()->setECSId($a_content_id);
123
124
125 if($this->getMapping()->isAttributeMappingEnabled())
126 {
127 $this->log->debug('Handling advanced attribute mapping');
128 return $this->doAttributeMapping($a_content_id,$course);
129 }
130
131 if($this->getMapping()->isAllInOneCategoryEnabled())
132 {
133 $this->log->debug('Handling course all in one category setting');
134 $this->doSync($a_content_id, $course,ilObject::_lookupObjId($this->getMapping()->getAllInOneCategory()));
135 return true;
136 }
137
138 $parent_obj_id = $this->syncParentContainer($a_content_id,$course);
139 if($parent_obj_id)
140 {
141 $this->log->info('Using already mapped category: '. ilObject::_lookupTitle($parent_obj_id));
142 $this->doSync($a_content_id,$course,$parent_obj_id);
143 return true;
144 }
145 $this->log->info('Using course default category');
146 $this->doSync($a_content_id,$course,ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
147 return true;
148 }
149
155 protected function doAttributeMapping($a_content_id, $course)
156 {
157 // Check if course is already created
158 $course_id = $course->lectureID;
159 $obj_id = $this->getImportId($course_id);
160
161 if($obj_id)
162 {
163 // do update
164 $this->log->debug('Performing update of already imported course.');
165
166 $refs = ilObject::_getAllReferences($obj_id);
167 $ref = end($refs);
168
169 $this->doSync(
170 $a_content_id,
171 $course,
172 ilObject::_lookupObjId($GLOBALS['tree']->getParentId($ref))
173 );
174 return true;
175 }
176
177 // Get all rules
178 $matching_rules = [];
179 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseMappingRule.php';
180 foreach(ilECSCourseMappingRule::getRuleRefIds($this->getServer()->getServerId(), $this->getMid()) as $ref_id)
181 {
182 $matching_index = ilECSCourseMappingRule::isMatching(
183 $course,
184 $this->getServer()->getServerId(),
185 $this->getMid(),
186 $ref_id);
187 if(strcmp($matching_index, '0') !== 0)
188 {
189 $matching_rules[$matching_index] = $ref_id;
190 }
191 }
192 ksort($matching_rules);
193
194 $this->log->dump($matching_rules);
195
196 if(!count($matching_rules))
197 {
198 // Put course in default category
199 $this->log->debug('No matching attribute mapping rule found.');
200 $this->log->info('Using course default category');
201 $this->doSync($a_content_id,$course,ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
202 return true;
203 }
204
205 $this->log->debug('Matching rules:');
206 $this->log->dump($matching_rules, ilLogLevel::DEBUG);
207
208 $all_parent_refs = [];
209 foreach($matching_rules as $matching_rule)
210 {
211 $this->log->debug('Handling matching rule: '. $matching_rule);
212 $parent_refs = ilECSCourseMappingRule::doMappings($course,$this->getServer()->getServerId(),$this->getMid(),$matching_rule);
213 // map according mapping rules
214 $this->log->debug('Adding parent references: ');
215 $this->log->dump($parent_refs);
216
217 if(count($parent_refs))
218 {
219 $all_parent_refs = array_unique(array_merge($all_parent_refs,$parent_refs));
220 }
221 }
222
223 // parent refs are an array of created categories
224 // the first ref should contain the main course or parallel courses.
225 // all other refs wil contain course references.
226 $first = true;
227 foreach($all_parent_refs as $category_ref)
228 {
229 if($first)
230 {
231 $this->log->debug('Creating new course instance in: ' . $category_ref);
232 $this->doSync($a_content_id, $course, ilObject::_lookupObjId($category_ref));
233 $first = false;
234 continue;
235 }
236 else
237 {
238 $this->log->debug('Creating new course reference instance in: ' . $category_ref);
239 $this->createCourseReferenceObjects($category_ref);
240 }
241 }
242 return true;
243 }
244
249 protected function createCourseReferenceObjects($a_parent_ref_id)
250 {
251 foreach($this->getCreatedCourses() as $ref_id)
252 {
253 include_once './Modules/CourseReference/classes/class.ilObjCourseReference.php';
254 $crsr = new ilObjCourseReference();
255 $crsr->setOwner(SYTEM_USER_ID);
256 $crsr->setTargetRefId($ref_id);
257 $crsr->setTargetId(ilObject::_lookupObjId($ref_id));
258 $crsr->create();
259 $crsr->update();
260 $crsr->createReference();
261 $crsr->putInTree($a_parent_ref_id);
262 $crsr->setPermissions($a_parent_ref_id);
263
264 $this->log->debug('Created new course reference in : ' . ilObject::_lookupTitle(ilObject::_lookupObjId($a_parent_ref_id)));
265 $this->log->debug('Created new course reference for : ' . ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)));
266 }
267 }
268
274 protected function syncParentContainer($a_content_id, $course)
275 {
276 if(!is_array($course->allocations))
277 {
278 $GLOBALS['ilLog']->write(__METHOD__.': No allocation in course defined.');
279 return 0;
280 }
281 if(!$course->allocations[0]->parentID)
282 {
283 $GLOBALS['ilLog']->write(__METHOD__.': No allocation parent in course defined.');
284 return 0;
285 }
286 $parent_id = $course->allocations[0]->parentID;
287
288 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
289 $parent_tid = ilECSCmsData::lookupFirstTreeOfNode($this->getServer()->getServerId(), $this->getMid(), $parent_id);
290 return $this->syncNodetoTop($parent_tid, $parent_id);
291 }
292
299 protected function syncNodeToTop($tree_id, $cms_id)
300 {
301 $obj_id = $this->getImportId($cms_id);
302 if($obj_id)
303 {
304 // node already imported
305 return $obj_id;
306 }
307
308 $tobj_id = ilECSCmsData::lookupObjId(
309 $this->getServer()->getServerId(),
310 $this->getMid(),
311 $tree_id,
312 $cms_id);
313
314 // node is not imported
315 $GLOBALS['ilLog']->write(__METHOD__.': ecs node with id '. $cms_id. ' is not imported for mid ' . $this->getMid().' tree_id '.$tree_id);
316
317 // check for mapping: if mapping is available create category
318 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
320 $this->getServer()->getServerId(),
321 $this->getMid(),
322 $tree_id,
323 $tobj_id);
324
325 if($ass->isMapped())
326 {
327 $GLOBALS['ilLog']->write(__METHOD__.': node is mapped');
328 return $this->syncCategory($tobj_id,$ass->getRefId());
329 }
330
331 // Start recursion to top
332 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
333 $tree = new ilECSCmsTree($tree_id);
334 $parent_tobj_id = $tree->getParentId($tobj_id);
335 if($parent_tobj_id)
336 {
337 $cms_ids = ilECSCmsData::lookupCmsIds(array($parent_tobj_id));
338 $obj_id = $this->syncNodeToTop($tree_id, $cms_ids[0]);
339 }
340
341 if($obj_id)
342 {
343 $refs = ilObject::_getAllReferences($obj_id);
344 $ref_id = end($refs);
345 return $this->syncCategory($tobj_id, $ref_id);
346 }
347 return 0;
348 }
349
355 protected function syncCategory($tobj_id, $parent_ref_id)
356 {
357 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
358 $data = new ilECSCmsData($tobj_id);
359
360 include_once './Modules/Category/classes/class.ilObjCategory.php';
361 $cat = new ilObjCategory();
362 $cat->setOwner(SYSTEM_USER_ID);
363 $cat->setTitle($data->getTitle());
364 $cat->create(); // true for upload
365 $cat->createReference();
366 $cat->putInTree($parent_ref_id);
367 $cat->setPermissions($parent_ref_id);
368 $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
369 $cat->addTranslation(
370 $data->getTitle(),
371 $cat->getLongDescription(),
372 $GLOBALS['lng']->getDefaultLanguage(),
373 1
374 );
375
376 // set imported
377 $import = new ilECSImport(
378 $this->getServer()->getServerId(),
379 $cat->getId()
380 );
381 $import->setMID($this->getMid());
382 $import->setContentId($data->getCmsId());
383 $import->setImported(true);
384 $import->save();
385
386 return $cat->getId();
387 }
388
395 protected function doSync($a_content_id, $course, $a_parent_obj_id)
396 {
397 // Check if course is already created
398 $course_id = $course->lectureID;
399 $this->getCourseUrl()->setCmsLectureId($course_id);
400
401 $obj_id = $this->getImportId($course_id);
402
403 $this->log->debug('Found obj_id '.$obj_id. ' for course_id '. $course_id );
404
405 // Handle parallel groups
406 if($obj_id)
407 {
408 // update multiple courses/groups according to parallel scenario
409 $this->log->debug('Group scenario '.$course->groupScenario);
410 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
411 switch((int) $course->groupScenario)
412 {
414 $this->log->debug('Performing update for parallel groups in course.');
415 $this->updateParallelGroups($a_content_id,$course, $obj_id);
416 break;
417
419 $this->log->debug('Performing update for parallel courses.');
420 $this->updateParallelCourses($a_content_id,$course, $a_parent_obj_id);
421 break;
422
424 default:
425 // nothing to do
426 break;
427
428 }
429
430 // do update
431 $this->updateCourseData($course,$obj_id);
432 }
433 else
434 {
435 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
436 switch((int) $course->groupScenario)
437 {
439 $this->log->debug('Parallel scenario "groups in courses".');
440 $crs = $this->createCourseData($course);
441 $crs = $this->createCourseReference($crs, $a_parent_obj_id);
442 $this->setImported($course_id, $crs, $a_content_id);
443
444 // Create parallel groups under crs
445 $this->createParallelGroups($a_content_id,$course,$crs->getRefId());
446 break;
447
449 $this->log->debug('Parallel scenario "Courses foreach Lecturer".');
450 // Import empty to store the ecs ressource id (used for course member update).
451 $this->setImported($course_id,NULL,$a_content_id);
452 break;
453
455 $this->log->debug('Parallel scenario "Many courses".');
456 $refs = ilObject::_getAllReferences($a_parent_obj_id);
457 $ref = end($refs);
458 // do not create master course for this scenario
459 //$crs = $this->createCourseData($course);
460 //$this->createCourseReference($crs, $a_parent_obj_id);
461 //$this->setImported($course_id, $crs, $a_content_id);
462 $this->createParallelCourses($a_content_id,$course, $ref);
463 break;
464
465 default:
467 $this->log->debug('Parallel scenario "One Course".');
468 $crs = $this->createCourseData($course);
469 $this->createCourseReference($crs, $a_parent_obj_id);
470 $this->setImported($course_id, $crs, $a_content_id);
471 break;
472
473
474 }
475 }
476 // finally update course urls
477 $this->handleCourseUrlUpdate();
478 return true;
479 }
480
487 protected function createParallelCourses($a_content_id,$course, $parent_ref)
488 {
489 foreach((array) $course->groups as $group)
490 {
491 $this->createParallelCourse($a_content_id,$course, $group, $parent_ref);
492 }
493 return true;
494 }
495
502 protected function createParallelCourse($a_content_id, $course, $group, $parent_ref)
503 {
504 if($this->getImportId($course->lectureID, $group->id))
505 {
506 $this->log->debug('Parallel course already created');
507 return false;
508 }
509
510 include_once './Modules/Course/classes/class.ilObjCourse.php';
511 $course_obj = new ilObjCourse();
512 $course_obj->setOwner(SYSTEM_USER_ID);
513 $title = $course->title;
514 if(strlen($group->title))
515 {
516 $title .= ' ('.$group->title.')';
517 }
518 $this->log->debug('Creating new parallel course instance from ecs : '. $title);
519 $course_obj->setTitle($title);
520 $course_obj->setSubscriptionMaxMembers((int) $group->maxParticipants);
521 $course_obj->create();
522
523 $this->createCourseReference($course_obj, ilObject::_lookupObjId($parent_ref));
524 $this->setImported($course->lectureID, $course_obj,$a_content_id, $group->id);
525 $this->setObjectCreated(true);
526 return true;
527 }
528
534 protected function updateParallelCourses($a_content_id, $course,$parent_obj)
535 {
536 $parent_refs = ilObject::_getAllReferences($parent_obj);
537 $parent_ref = end($parent_refs);
538
539 foreach((array) $course->groups as $group)
540 {
541 $title = $course->title;
542 if(strlen($group->title))
543 {
544 $title .= ' ('.$group->title.')';
545 }
546
547 $obj_id = $this->getImportId($course->lectureID, $group->id);
548 $this->log->debug('Imported obj id is ' .$obj_id);
549 if(!$obj_id)
550 {
551 $this->createParallelCourse($a_content_id, $course, $group, $parent_ref);
552 }
553 else
554 {
555 $course_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
556 if($course_obj instanceof ilObjCourse)
557 {
558 $this->log->debug('New title is '. $title);
559 $course_obj->setTitle($title);
560 $course_obj->setSubscriptionMaxMembers($group->maxParticipants);
561 $course_obj->update();
562 }
563 }
564 $this->addUrlEntry($this->getImportId($course->lectureID, $group->ID));
565 }
566 return true;
567 }
568
569
570
576 protected function createParallelGroups($a_content_id, $course, $parent_ref)
577 {
578 foreach((array) $course->groups as $group)
579 {
580 $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
581 }
582 return true;
583 }
584
590 protected function createParallelGroup($a_content_id,$course, $group, $parent_ref)
591 {
592 include_once './Modules/Group/classes/class.ilObjGroup.php';
593 $group_obj = new ilObjGroup();
594 $group_obj->setOwner(SYSTEM_USER_ID);
595 $title = strlen($group->title) ? $group->title : $course->title;
596 $group_obj->setTitle($title);
597 $group_obj->setMaxMembers((int) $group->maxParticipants);
598 $group_obj->create();
599 $group_obj->createReference();
600 $group_obj->putInTree($parent_ref);
601 $group_obj->setPermissions($parent_ref);
602 $group_obj->initGroupStatus(GRP_TYPE_CLOSED);
603 $this->setImported($course->lectureID, $group_obj, $a_content_id, $group->id);
604 $this->setObjectCreated(true);
605 }
606
607
613 protected function updateParallelGroups($a_content_id, $course,$parent_obj)
614 {
615 $parent_refs = ilObject::_getAllReferences($parent_obj);
616 $parent_ref = end($parent_refs);
617
618 foreach((array) $course->groups as $group)
619 {
620 $obj_id = $this->getImportId($course->lectureID, $group->id);
621 $this->log->debug('Imported obj id is ' .$obj_id);
622 if(!$obj_id)
623 {
624 $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
625 }
626 else
627 {
628 $group_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
629 if($group_obj instanceof ilObjGroup)
630 {
631 $title = strlen($group->title) ? $group->title : $course->title;
632 $GLOBALS['ilLog']->write(__METHOD__.': New title is '. $title);
633 $group_obj->setTitle($title);
634 $group_obj->setMaxMembers((int) $group->maxParticipants);
635 $group_obj->update();
636 }
637 }
638 $this->addUrlEntry($this->getImportId($course->lectureID, $group->id));
639 }
640 }
641
649 protected function getImportId($a_content_id,$a_sub_id = NULL)
650 {
651 include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
653 $this->getServer()->getServerId(),
654 $this->getMid(),
655 $a_content_id,
656 $a_sub_id
657 );
658 }
659
664 protected function updateCourseData($course,$obj_id)
665 {
666 // do update
667 $refs = ilObject::_getAllReferences($obj_id);
668 $ref_id = end($refs);
670 if(!$crs_obj instanceof ilObject)
671 {
672 $GLOBALS['ilLog']->write(__METHOD__.': Cannot instantiate course instance');
673 return true;
674 }
675
676 // Update title
677 $title = $course->title;
678 $GLOBALS['ilLog']->write(__METHOD__.': new title is : '. $title);
679
680 $crs_obj->setTitle($title);
681 $crs_obj->update();
682 return true;
683 }
684
689 protected function createCourseData($course)
690 {
691 include_once './Modules/Course/classes/class.ilObjCourse.php';
692 $course_obj = new ilObjCourse();
693 $course_obj->setOwner(SYSTEM_USER_ID);
694 $title = $course->title;
695 $GLOBALS['ilLog']->write(__METHOD__.': Creating new course instance from ecs : '. $title);
696 $course_obj->setTitle($title);
697 $course_obj->create();
698 return $course_obj;
699 }
700
707 protected function createCourseReference($crs,$a_parent_obj_id)
708 {
709 $ref_ids = ilObject::_getAllReferences($a_parent_obj_id);
710 $ref_id = end($ref_ids);
711
712 $crs->createReference();
713 $crs->putInTree($ref_id);
714 $crs->setPermissions($ref_id);
715
716 $this->setObjectCreated(true);
717 $this->addUrlEntry($crs->getId());
718
719 $this->courses_created[] = $crs->getRefId();
720
721 return $crs;
722 }
723
729 protected function setImported($a_content_id, $object, $a_ecs_id = 0, $a_sub_id = null)
730 {
731 include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
732 $import = new ilECSImport(
733 $this->getServer()->getServerId(),
734 is_object($object) ? $object->getId() : 0
735 );
736
737
738 $import->setSubId($a_sub_id);
739 $import->setMID($this->getMid());
740 $import->setEContentId($a_ecs_id);
741 $import->setContentId($a_content_id);
742 $import->setImported(true);
743 $import->save();
744 return true;
745 }
746
751 protected function addUrlEntry($a_obj_id)
752 {
753 $refs = ilObject::_getAllReferences($a_obj_id);
754 $ref_id = end($refs);
755
756 if(!$ref_id)
757 {
758 return false;
759 }
760 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseLmsUrl.php';
761 $lms_url = new ilECSCourseLmsUrl();
762 $lms_url->setTitle(ilObject::_lookupTitle($a_obj_id));
763
764 include_once './Services/Link/classes/class.ilLink.php';
765 $lms_url->setUrl(ilLink::_getLink($ref_id));
766 $this->getCourseUrl()->addLmsCourseUrls($lms_url);
767 }
768
772 protected function handleCourseUrlUpdate()
773 {
774 $GLOBALS['ilLog']->write(__METHOD__.': Starting course url update');
775 if($this->isObjectCreated())
776 {
777 $GLOBALS['ilLog']->write(__METHOD__.': Sending new course group url');
778 $this->getCourseUrl()->send($this->getServer(), $this->getMid());
779 }
780 else
781 {
782 $GLOBALS['ilLog']->write(__METHOD__.': No courses groups created. Aborting');
783 }
784 }
785}
786?>
An exception for terminatinating execution or to throw for unit testing.
const GRP_TYPE_CLOSED
static lookupObjId($a_server_id, $a_mid, $a_tree_id, $cms_id)
static lookupCmsIds($a_obj_ids)
static lookupFirstTreeOfNode($a_server_id, $a_mid, $cms_id)
Lookup first obj_id of cms node @global $ilDB $ilDB.
syncCategory($tobj_id, $parent_ref_id)
Sync category.
updateCourseData($course, $obj_id)
Update course data.
handle($a_content_id, $course)
Handle sync request.
createParallelGroups($a_content_id, $course, $parent_ref)
This create parallel groups.
getImportId($a_content_id, $a_sub_id=NULL)
Get import id of remote course Return 0 if object isn't imported.
setImported($a_content_id, $object, $a_ecs_id=0, $a_sub_id=null)
Set new course object imported.
doAttributeMapping($a_content_id, $course)
Sync attribute mapping.
createCourseData($course)
Create course data from json.
updateParallelGroups($a_content_id, $course, $parent_obj)
Update parallel group data.
createCourseReferenceObjects($a_parent_ref_id)
Create course reference objects.
createCourseReference($crs, $a_parent_obj_id)
Create course reference.
setObjectCreated($a_status)
Set object created status.
createParallelGroup($a_content_id, $course, $group, $parent_ref)
Create parallel group.
isObjectCreated()
Check if an object (course / group) has been created.
createParallelCourse($a_content_id, $course, $group, $parent_ref)
Create parallel course.
createParallelCourses($a_content_id, $course, $parent_ref)
Create parallel courses.
updateParallelCourses($a_content_id, $course, $parent_obj)
Update parallel group data.
syncParentContainer($a_content_id, $course)
Sync parent container.
doSync($a_content_id, $course, $a_parent_obj_id)
Handle all in one setting.
syncNodeToTop($tree_id, $cms_id)
Sync node to top.
__construct(ilECSSetting $server, $a_mid)
@maybe Constructor
Represents a ecs course lms url.
static isMatching($course, $a_sid, $a_mid, $a_ref_id)
Check if rule matches.
static doMappings($course, $a_sid, $a_mid, $a_ref_id)
static getRuleRefIds($a_sid, $a_mid)
Represents a ecs course url.
Storage of ECS imported objects.
static lookupObjIdByContentId($a_server_id, $a_mid, $a_content_id, $a_sub_id=null)
Lookup obj_id by content id.
static getInstanceByServerMid($a_server_id, $a_mid)
Get instance.
Class ilObjCategory.
Class ilObjCourse.
Class ilObjGroup.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
Class ilObject Basic functions for all objects.
static _lookupObjId($a_id)
static _lookupTitle($a_id)
lookup object title
static _getAllReferences($a_id)
get all reference ids of object
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$ref_id
Definition: sahs_server.php:39