ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $this->log->debug('Handling advanced attribute mapping');
127 return $this->doAttributeMapping($a_content_id, $course);
128 }
129
130 if ($this->getMapping()->isAllInOneCategoryEnabled()) {
131 $this->log->debug('Handling course all in one category setting');
132 $this->doSync($a_content_id, $course, ilObject::_lookupObjId($this->getMapping()->getAllInOneCategory()));
133 return true;
134 }
135
136 $parent_obj_id = $this->syncParentContainer($a_content_id, $course);
137 if ($parent_obj_id) {
138 $this->log->info('Using already mapped category: ' . ilObject::_lookupTitle($parent_obj_id));
139 $this->doSync($a_content_id, $course, $parent_obj_id);
140 return true;
141 }
142 $this->log->info('Using course default category');
143 $this->doSync($a_content_id, $course, ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
144 return true;
145 }
146
152 protected function doAttributeMapping($a_content_id, $course)
153 {
154 // Check if course is already created
155 $course_id = $course->lectureID;
156 $obj_id = $this->getImportId($course_id);
157
158 if ($obj_id) {
159 // do update
160 $this->log->debug('Performing update of already imported course.');
161
162 $refs = ilObject::_getAllReferences($obj_id);
163 $ref = end($refs);
164
165 $this->doSync(
166 $a_content_id,
167 $course,
168 ilObject::_lookupObjId($GLOBALS['tree']->getParentId($ref))
169 );
170 return true;
171 }
172
173 // Get all rules
174 $matching_rules = [];
175 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseMappingRule.php';
176 foreach (ilECSCourseMappingRule::getRuleRefIds($this->getServer()->getServerId(), $this->getMid()) as $ref_id) {
177 $matching_index = ilECSCourseMappingRule::isMatching(
178 $course,
179 $this->getServer()->getServerId(),
180 $this->getMid(),
181 $ref_id
182 );
183 if (strcmp($matching_index, '0') !== 0) {
184 $matching_rules[$matching_index] = $ref_id;
185 }
186 }
187 ksort($matching_rules);
188
189 $this->log->dump($matching_rules);
190
191 if (!count($matching_rules)) {
192 // Put course in default category
193 $this->log->debug('No matching attribute mapping rule found.');
194 $this->log->info('Using course default category');
195 $this->doSync($a_content_id, $course, ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
196 return true;
197 }
198
199 $this->log->debug('Matching rules:');
200 $this->log->dump($matching_rules, ilLogLevel::DEBUG);
201
202 $all_parent_refs = [];
203 foreach ($matching_rules as $matching_rule) {
204 $this->log->debug('Handling matching rule: ' . $matching_rule);
205 $parent_refs = ilECSCourseMappingRule::doMappings($course, $this->getServer()->getServerId(), $this->getMid(), $matching_rule);
206 // map according mapping rules
207 $this->log->debug('Adding parent references: ');
208 $this->log->dump($parent_refs);
209
210 if (count($parent_refs)) {
211 $all_parent_refs = array_unique(array_merge($all_parent_refs, $parent_refs));
212 }
213 }
214
215 // parent refs are an array of created categories
216 // the first ref should contain the main course or parallel courses.
217 // all other refs wil contain course references.
218 $first = true;
219 foreach ($all_parent_refs as $category_ref) {
220 if ($first) {
221 $this->log->debug('Creating new course instance in: ' . $category_ref);
222 $this->doSync($a_content_id, $course, ilObject::_lookupObjId($category_ref));
223 $first = false;
224 continue;
225 } else {
226 $this->log->debug('Creating new course reference instance in: ' . $category_ref);
227 $this->createCourseReferenceObjects($category_ref);
228 }
229 }
230 return true;
231 }
232
237 protected function createCourseReferenceObjects($a_parent_ref_id)
238 {
239 foreach ($this->getCreatedCourses() as $ref_id) {
240 include_once './Modules/CourseReference/classes/class.ilObjCourseReference.php';
241 $crsr = new ilObjCourseReference();
242 $crsr->setOwner(SYTEM_USER_ID);
243 $crsr->setTargetRefId($ref_id);
244 $crsr->setTargetId(ilObject::_lookupObjId($ref_id));
245 $crsr->create();
246 $crsr->update();
247 $crsr->createReference();
248 $crsr->putInTree($a_parent_ref_id);
249 $crsr->setPermissions($a_parent_ref_id);
250
251 $this->log->debug('Created new course reference in : ' . ilObject::_lookupTitle(ilObject::_lookupObjId($a_parent_ref_id)));
252 $this->log->debug('Created new course reference for : ' . ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)));
253 }
254 }
255
261 protected function syncParentContainer($a_content_id, $course)
262 {
263 if (!is_array($course->allocations)) {
264 $GLOBALS['ilLog']->write(__METHOD__ . ': No allocation in course defined.');
265 return 0;
266 }
267 if (!$course->allocations[0]->parentID) {
268 $GLOBALS['ilLog']->write(__METHOD__ . ': No allocation parent in course defined.');
269 return 0;
270 }
271 $parent_id = $course->allocations[0]->parentID;
272
273 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
274 $parent_tid = ilECSCmsData::lookupFirstTreeOfNode($this->getServer()->getServerId(), $this->getMid(), $parent_id);
275 return $this->syncNodetoTop($parent_tid, $parent_id);
276 }
277
284 protected function syncNodeToTop($tree_id, $cms_id)
285 {
286 $obj_id = $this->getImportId($cms_id);
287 if ($obj_id) {
288 // node already imported
289 return $obj_id;
290 }
291
292 $tobj_id = ilECSCmsData::lookupObjId(
293 $this->getServer()->getServerId(),
294 $this->getMid(),
295 $tree_id,
296 $cms_id
297 );
298
299 // node is not imported
300 $GLOBALS['ilLog']->write(__METHOD__ . ': ecs node with id ' . $cms_id . ' is not imported for mid ' . $this->getMid() . ' tree_id ' . $tree_id);
301
302 // check for mapping: if mapping is available create category
303 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
305 $this->getServer()->getServerId(),
306 $this->getMid(),
307 $tree_id,
308 $tobj_id
309 );
310
311 if ($ass->isMapped()) {
312 $GLOBALS['ilLog']->write(__METHOD__ . ': node is mapped');
313 return $this->syncCategory($tobj_id, $ass->getRefId());
314 }
315
316 // Start recursion to top
317 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
318 $tree = new ilECSCmsTree($tree_id);
319 $parent_tobj_id = $tree->getParentId($tobj_id);
320 if ($parent_tobj_id) {
321 $cms_ids = ilECSCmsData::lookupCmsIds(array($parent_tobj_id));
322 $obj_id = $this->syncNodeToTop($tree_id, $cms_ids[0]);
323 }
324
325 if ($obj_id) {
326 $refs = ilObject::_getAllReferences($obj_id);
327 $ref_id = end($refs);
328 return $this->syncCategory($tobj_id, $ref_id);
329 }
330 return 0;
331 }
332
338 protected function syncCategory($tobj_id, $parent_ref_id)
339 {
340 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
341 $data = new ilECSCmsData($tobj_id);
342
343 include_once './Modules/Category/classes/class.ilObjCategory.php';
344 $cat = new ilObjCategory();
345 $cat->setOwner(SYSTEM_USER_ID);
346 $cat->setTitle($data->getTitle());
347 $cat->create(); // true for upload
348 $cat->createReference();
349 $cat->putInTree($parent_ref_id);
350 $cat->setPermissions($parent_ref_id);
351 $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
352 $cat->addTranslation(
353 $data->getTitle(),
354 $cat->getLongDescription(),
355 $GLOBALS['lng']->getDefaultLanguage(),
356 1
357 );
358
359 // set imported
360 $import = new ilECSImport(
361 $this->getServer()->getServerId(),
362 $cat->getId()
363 );
364 $import->setMID($this->getMid());
365 $import->setContentId($data->getCmsId());
366 $import->setImported(true);
367 $import->save();
368
369 return $cat->getId();
370 }
371
378 protected function doSync($a_content_id, $course, $a_parent_obj_id)
379 {
380 // Check if course is already created
381 $course_id = $course->lectureID;
382 $this->getCourseUrl()->setCmsLectureId($course_id);
383
384 $obj_id = $this->getImportId($course_id);
385
386 $this->log->debug('Found obj_id ' . $obj_id . ' for course_id ' . $course_id);
387
388 // Handle parallel groups
389 if ($obj_id) {
390 // update multiple courses/groups according to parallel scenario
391 $this->log->debug('Group scenario ' . $course->groupScenario);
392 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
393 switch ((int) $course->groupScenario) {
395 $this->log->debug('Performing update for parallel groups in course.');
396 $this->updateParallelGroups($a_content_id, $course, $obj_id);
397 break;
398
400 $this->log->debug('Performing update for parallel courses.');
401 $this->updateParallelCourses($a_content_id, $course, $a_parent_obj_id);
402 break;
403
405 default:
406 // nothing to do
407 break;
408
409 }
410
411 // do update
412 $this->updateCourseData($course, $obj_id);
413 } else {
414 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
415 switch ((int) $course->groupScenario) {
417 $this->log->debug('Parallel scenario "groups in courses".');
418 $crs = $this->createCourseData($course);
419 $crs = $this->createCourseReference($crs, $a_parent_obj_id);
420 $this->setImported($course_id, $crs, $a_content_id);
421
422 // Create parallel groups under crs
423 $this->createParallelGroups($a_content_id, $course, $crs->getRefId());
424 break;
425
427 $this->log->debug('Parallel scenario "Courses foreach Lecturer".');
428 // Import empty to store the ecs ressource id (used for course member update).
429 $this->setImported($course_id, null, $a_content_id);
430 break;
431
433 $this->log->debug('Parallel scenario "Many courses".');
434 $refs = ilObject::_getAllReferences($a_parent_obj_id);
435 $ref = end($refs);
436 // do not create master course for this scenario
437 //$crs = $this->createCourseData($course);
438 //$this->createCourseReference($crs, $a_parent_obj_id);
439 //$this->setImported($course_id, $crs, $a_content_id);
440 $this->createParallelCourses($a_content_id, $course, $ref);
441 break;
442
443 default:
445 $this->log->debug('Parallel scenario "One Course".');
446 $crs = $this->createCourseData($course);
447 $this->createCourseReference($crs, $a_parent_obj_id);
448 $this->setImported($course_id, $crs, $a_content_id);
449 break;
450
451
452 }
453 }
454 // finally update course urls
455 $this->handleCourseUrlUpdate();
456 return true;
457 }
458
465 protected function createParallelCourses($a_content_id, $course, $parent_ref)
466 {
467 foreach ((array) $course->groups as $group) {
468 $this->createParallelCourse($a_content_id, $course, $group, $parent_ref);
469 }
470 return true;
471 }
472
479 protected function createParallelCourse($a_content_id, $course, $group, $parent_ref)
480 {
481 if ($this->getImportId($course->lectureID, $group->id)) {
482 $this->log->debug('Parallel course already created');
483 return false;
484 }
485
486 include_once './Modules/Course/classes/class.ilObjCourse.php';
487 $course_obj = new ilObjCourse();
488 $course_obj->setOwner(SYSTEM_USER_ID);
489 $title = $course->title;
490 if (strlen($group->title)) {
491 $title .= ' (' . $group->title . ')';
492 }
493 $this->log->debug('Creating new parallel course instance from ecs : ' . $title);
494 $course_obj->setTitle($title);
495 $course_obj->setSubscriptionMaxMembers((int) $group->maxParticipants);
496 $course_obj->create();
497
498 $this->createCourseReference($course_obj, ilObject::_lookupObjId($parent_ref));
499 $this->setImported($course->lectureID, $course_obj, $a_content_id, $group->id);
500 $this->setObjectCreated(true);
501 return true;
502 }
503
509 protected function updateParallelCourses($a_content_id, $course, $parent_obj)
510 {
511 $parent_refs = ilObject::_getAllReferences($parent_obj);
512 $parent_ref = end($parent_refs);
513
514 foreach ((array) $course->groups as $group) {
515 $title = $course->title;
516 if (strlen($group->title)) {
517 $title .= ' (' . $group->title . ')';
518 }
519
520 $obj_id = $this->getImportId($course->lectureID, $group->id);
521 $this->log->debug('Imported obj id is ' . $obj_id);
522 if (!$obj_id) {
523 $this->createParallelCourse($a_content_id, $course, $group, $parent_ref);
524 } else {
525 $course_obj = ilObjectFactory::getInstanceByObjId($obj_id, false);
526 if ($course_obj instanceof ilObjCourse) {
527 $this->log->debug('New title is ' . $title);
528 $course_obj->setTitle($title);
529 $course_obj->setSubscriptionMaxMembers($group->maxParticipants);
530 $course_obj->update();
531 }
532 }
533 $this->addUrlEntry($this->getImportId($course->lectureID, $group->ID));
534 }
535 return true;
536 }
537
538
539
545 protected function createParallelGroups($a_content_id, $course, $parent_ref)
546 {
547 foreach ((array) $course->groups as $group) {
548 $this->createParallelGroup($a_content_id, $course, $group, $parent_ref);
549 }
550 return true;
551 }
552
558 protected function createParallelGroup($a_content_id, $course, $group, $parent_ref)
559 {
560 include_once './Modules/Group/classes/class.ilObjGroup.php';
561 $group_obj = new ilObjGroup();
562 $group_obj->setOwner(SYSTEM_USER_ID);
563 $title = strlen($group->title) ? $group->title : $course->title;
564 $group_obj->setTitle($title);
565 $group_obj->setMaxMembers((int) $group->maxParticipants);
566 $group_obj->create();
567 $group_obj->createReference();
568 $group_obj->putInTree($parent_ref);
569 $group_obj->setPermissions($parent_ref);
570 $group_obj->updateGroupType(GRP_TYPE_CLOSED);
571 $this->setImported($course->lectureID, $group_obj, $a_content_id, $group->id);
572 $this->setObjectCreated(true);
573 }
574
575
581 protected function updateParallelGroups($a_content_id, $course, $parent_obj)
582 {
583 $parent_refs = ilObject::_getAllReferences($parent_obj);
584 $parent_ref = end($parent_refs);
585
586 foreach ((array) $course->groups as $group) {
587 $obj_id = $this->getImportId($course->lectureID, $group->id);
588 $this->log->debug('Imported obj id is ' . $obj_id);
589 if (!$obj_id) {
590 $this->createParallelGroup($a_content_id, $course, $group, $parent_ref);
591 } else {
592 $group_obj = ilObjectFactory::getInstanceByObjId($obj_id, false);
593 if ($group_obj instanceof ilObjGroup) {
594 $title = strlen($group->title) ? $group->title : $course->title;
595 $GLOBALS['ilLog']->write(__METHOD__ . ': New title is ' . $title);
596 $group_obj->setTitle($title);
597 $group_obj->setMaxMembers((int) $group->maxParticipants);
598 $group_obj->update();
599 }
600 }
601 $this->addUrlEntry($this->getImportId($course->lectureID, $group->id));
602 }
603 }
604
612 protected function getImportId($a_content_id, $a_sub_id = null)
613 {
614 include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
616 $this->getServer()->getServerId(),
617 $this->getMid(),
618 $a_content_id,
619 $a_sub_id
620 );
621 }
622
627 protected function updateCourseData($course, $obj_id)
628 {
629 // do update
630 $refs = ilObject::_getAllReferences($obj_id);
631 $ref_id = end($refs);
632 $crs_obj = ilObjectFactory::getInstanceByRefId($ref_id, false);
633 if (!$crs_obj instanceof ilObject) {
634 $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot instantiate course instance');
635 return true;
636 }
637
638 // Update title
639 $title = $course->title;
640 $GLOBALS['ilLog']->write(__METHOD__ . ': new title is : ' . $title);
641
642 $crs_obj->setTitle($title);
643 $crs_obj->update();
644 return true;
645 }
646
651 protected function createCourseData($course)
652 {
653 include_once './Modules/Course/classes/class.ilObjCourse.php';
654 $course_obj = new ilObjCourse();
655 $course_obj->setOwner(SYSTEM_USER_ID);
656 $title = $course->title;
657 $GLOBALS['ilLog']->write(__METHOD__ . ': Creating new course instance from ecs : ' . $title);
658 $course_obj->setTitle($title);
659 $course_obj->create();
660 return $course_obj;
661 }
662
669 protected function createCourseReference($crs, $a_parent_obj_id)
670 {
671 $ref_ids = ilObject::_getAllReferences($a_parent_obj_id);
672 $ref_id = end($ref_ids);
673
674 $crs->createReference();
675 $crs->putInTree($ref_id);
676 $crs->setPermissions($ref_id);
677
678 $this->setObjectCreated(true);
679 $this->addUrlEntry($crs->getId());
680
681 $this->courses_created[] = $crs->getRefId();
682
683 return $crs;
684 }
685
691 protected function setImported($a_content_id, $object, $a_ecs_id = 0, $a_sub_id = null)
692 {
693 include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
694 $import = new ilECSImport(
695 $this->getServer()->getServerId(),
696 is_object($object) ? $object->getId() : 0
697 );
698
699
700 $import->setSubId($a_sub_id);
701 $import->setMID($this->getMid());
702 $import->setEContentId($a_ecs_id);
703 $import->setContentId($a_content_id);
704 $import->setImported(true);
705 $import->save();
706 return true;
707 }
708
713 protected function addUrlEntry($a_obj_id)
714 {
715 $refs = ilObject::_getAllReferences($a_obj_id);
716 $ref_id = end($refs);
717
718 if (!$ref_id) {
719 return false;
720 }
721 include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseLmsUrl.php';
722 $lms_url = new ilECSCourseLmsUrl();
723 $lms_url->setTitle(ilObject::_lookupTitle($a_obj_id));
724
725 include_once './Services/Link/classes/class.ilLink.php';
726 $lms_url->setUrl(ilLink::_getLink($ref_id));
727 $this->getCourseUrl()->addLmsCourseUrls($lms_url);
728 }
729
733 protected function handleCourseUrlUpdate()
734 {
735 $GLOBALS['ilLog']->write(__METHOD__ . ': Starting course url update');
736 if ($this->isObjectCreated()) {
737 $GLOBALS['ilLog']->write(__METHOD__ . ': Sending new course group url');
738 $this->getCourseUrl()->send($this->getServer(), $this->getMid());
739 } else {
740 $GLOBALS['ilLog']->write(__METHOD__ . ': No courses groups created. Aborting');
741 }
742 }
743}
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.