ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSCourseCreationHandler.php
Go to the documentation of this file.
1 <?php
2 
3 include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingSettings.php';
4 include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
5 include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
6 
7 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
8 
15 {
16  private $server = null;
17  private $mapping = null;
18  private $course_url = null;
19  private $object_created = false;
20 
21  private $mid;
22 
23 
28  public function __construct(ilECSSetting $server,$a_mid)
29  {
30  $this->server = $server;
31  $this->mid = $a_mid;
32  $this->mapping = ilECSNodeMappingSettings::getInstanceByServerMid($this->getServer()->getServerId(), $this->getMid());
33 
34  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseUrl.php';
35  $this->course_url = new ilECSCourseUrl();
36  }
37 
38 
43  public function getServer()
44  {
45  return $this->server;
46  }
47 
52  public function getMapping()
53  {
54  return $this->mapping;
55  }
56 
61  public function getCourseUrl()
62  {
63  return $this->course_url;
64  }
65 
70  public function isObjectCreated()
71  {
72  return $this->object_created;
73  }
74 
79  public function setObjectCreated($a_status)
80  {
81  $this->object_created = $a_status;
82  }
83 
88  public function getMid()
89  {
90  return $this->mid;
91  }
92 
98  public function handle($a_content_id,$course)
99  {
100  $GLOBALS['ilLog']->write(__METHOD__.': --------- content id '.$a_content_id);
101 
102  // prepare course url
103  // if any object (course group) will be created, a list of all course urls
104  // will be sent to ecs.
105  $this->setObjectCreated(false);
106  $this->getCourseUrl()->setECSId($a_content_id);
107 
108 
109  if($this->getMapping()->isAttributeMappingEnabled())
110  {
111  $GLOBALS['ilLog']->write(__METHOD__.': Handling advanced attribute mapping');
112  return $this->doAttributeMapping($a_content_id,$course);
113  }
114 
115  if($this->getMapping()->isAllInOneCategoryEnabled())
116  {
117  $GLOBALS['ilLog']->write(__METHOD__.': Handling course all in one category setting');
118  return $this->doSync($a_content_id, $course,ilObject::_lookupObjId($this->getMapping()->getAllInOneCategory()));
119  }
120 
121  $parent_obj_id = $this->syncParentContainer($a_content_id,$course);
122  if($parent_obj_id)
123  {
124  $GLOBALS['ilLog']->write(__METHOD__.': Using already mapped category: '. ilObject::_lookupTitle($parent_obj_id));
125  return $this->doSync($a_content_id,$course,$parent_obj_id);
126  }
127  $GLOBALS['ilLog']->write(__METHOD__.': Using course default category');
128  return $this->doSync($a_content_id,$course,ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
129  }
130 
136  protected function doAttributeMapping($a_content_id, $course)
137  {
138  // Check if course is already created
139  $course_id = $course->lectureID;
140  $obj_id = $this->getImportId($course_id);
141 
142  if($obj_id)
143  {
144  // do update
145  $GLOBALS['ilLog']->write(__METHOD__.' Performing update of already imported course.');
146 
147  $refs = ilObject::_getAllReferences($obj_id);
148  $ref = end($refs);
149 
150  return $this->doSync(
151  $a_content_id,
152  $course,
153  ilObject::_lookupObjId($GLOBALS['tree']->getParentId($ref))
154  );
155  }
156 
157  // Get all rules
158  $matching_rule = 0;
159  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseMappingRule.php';
160  foreach(ilECSCourseMappingRule::getRuleRefIds($this->getServer()->getServerId(), $this->getMid()) as $ref_id)
161  {
163  $course,
164  $this->getServer()->getServerId(),
165  $this->getMid(),
166  $ref_id))
167  {
168  $matching_rule = $ref_id;
169  }
170  }
171  if(!$matching_rule)
172  {
173  // Put course in default category
174  $GLOBALS['ilLog']->write(__METHOD__.': No matching attribute mapping rule found.');
175  $GLOBALS['ilLog']->write(__METHOD__.': Using course default category');
176  return $this->doSync($a_content_id,$course,ilObject::_lookupObjId($this->getMapping()->getDefaultCourseCategory()));
177  }
178  // map according mapping rules
179  $parent_ref = ilECSCourseMappingRule::doMappings($course,$this->getServer()->getServerId(),$this->getMid(),$ref_id);
180  $this->doSync($a_content_id, $course, ilObject::_lookupObjId($parent_ref));
181  return true;
182  }
183 
189  protected function syncParentContainer($a_content_id, $course)
190  {
191  if(!is_array($course->allocations))
192  {
193  $GLOBALS['ilLog']->write(__METHOD__.': No allocation in course defined.');
194  return 0;
195  }
196  if(!$course->allocations[0]->parentID)
197  {
198  $GLOBALS['ilLog']->write(__METHOD__.': No allocation parent in course defined.');
199  return 0;
200  }
201  $parent_id = $course->allocations[0]->parentID;
202 
203  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
204  $parent_tid = ilECSCmsData::lookupFirstTreeOfNode($this->getServer()->getServerId(), $this->getMid(), $parent_id);
205  return $this->syncNodetoTop($parent_tid, $parent_id);
206  }
207 
214  protected function syncNodeToTop($tree_id, $cms_id)
215  {
216  $obj_id = $this->getImportId($cms_id);
217  if($obj_id)
218  {
219  // node already imported
220  return $obj_id;
221  }
222 
223  $tobj_id = ilECSCmsData::lookupObjId(
224  $this->getServer()->getServerId(),
225  $this->getMid(),
226  $tree_id,
227  $cms_id);
228 
229  // node is not imported
230  $GLOBALS['ilLog']->write(__METHOD__.': ecs node with id '. $cms_id. ' is not imported for mid ' . $this->getMid().' tree_id '.$tree_id);
231 
232  // check for mapping: if mapping is available create category
233  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSNodeMappingAssignment.php';
234  $ass = new ilECSNodeMappingAssignment(
235  $this->getServer()->getServerId(),
236  $this->getMid(),
237  $tree_id,
238  $tobj_id);
239 
240  if($ass->isMapped())
241  {
242  $GLOBALS['ilLog']->write(__METHOD__.': node is mapped');
243  return $this->syncCategory($tobj_id,$ass->getRefId());
244  }
245 
246  // Start recursion to top
247  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsTree.php';
248  $tree = new ilECSCmsTree($tree_id);
249  $parent_tobj_id = $tree->getParentId($tobj_id);
250  if($parent_tobj_id)
251  {
252  $cms_ids = ilECSCmsData::lookupCmsIds(array($parent_tobj_id));
253  $obj_id = $this->syncNodeToTop($tree_id, $cms_ids[0]);
254  }
255 
256  if($obj_id)
257  {
258  $refs = ilObject::_getAllReferences($obj_id);
259  $ref_id = end($refs);
260  return $this->syncCategory($tobj_id, $ref_id);
261  }
262  return 0;
263  }
264 
270  protected function syncCategory($tobj_id, $parent_ref_id)
271  {
272  include_once './Services/WebServices/ECS/classes/Tree/class.ilECSCmsData.php';
273  $data = new ilECSCmsData($tobj_id);
274 
275  include_once './Modules/Category/classes/class.ilObjCategory.php';
276  $cat = new ilObjCategory();
277  $cat->setTitle($data->getTitle());
278  $cat->create(); // true for upload
279  $cat->createReference();
280  $cat->putInTree($parent_ref_id);
281  $cat->setPermissions($parent_ref_id);
282  $cat->deleteTranslation($GLOBALS['lng']->getDefaultLanguage());
283  $cat->addTranslation(
284  $data->getTitle(),
285  $cat->getLongDescription(),
286  $GLOBALS['lng']->getDefaultLanguage(),
287  1
288  );
289 
290  // set imported
291  $import = new ilECSImport(
292  $this->getServer()->getServerId(),
293  $cat->getId()
294  );
295  $import->setMID($this->getMid());
296  $import->setContentId($data->getCmsId());
297  $import->setImported(true);
298  $import->save();
299 
300  return $cat->getId();
301  }
302 
308  protected function doSync($a_content_id, $course, $a_parent_obj_id)
309  {
310  // Check if course is already created
311  $course_id = $course->lectureID;
312  $this->getCourseUrl()->setCmsLectureId($course_id);
313 
314  $obj_id = $this->getImportId($course_id);
315 
316  $GLOBALS['ilLog']->write(__METHOD__.': Found obj_id '.$obj_id. ' for course_id '. $course_id );
317 
318  // Handle parallel groups
319  if($obj_id)
320  {
321  // update multiple courses/groups according to parallel scenario
322  $GLOBALS['ilLog']->write(__METHOD__.': Group scenario '.$course->groupScenario);
323  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
324  switch((int) $course->groupScenario)
325  {
327  $GLOBALS['ilLog']->write(__METHOD__.': Performing update for parallel groups in course.');
328  $this->updateParallelGroups($course,$obj_id);
329  break;
330 
332  $GLOBALS['ilLog']->write(__METHOD__.': Performing update for parallel courses.');
333  $this->updateParallelCourses($a_content_id,$course, $a_parent_obj_id);
334  break;
335 
337  default:
338  // nothing to do
339  break;
340 
341  }
342 
343  // do update
344  $this->updateCourseData($course,$obj_id);
345  }
346  else
347  {
348  include_once './Services/WebServices/ECS/classes/Mapping/class.ilECSMappingUtils.php';
349  switch((int) $course->groupScenario)
350  {
352  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "groups in courses".');
353  $crs = $this->createCourseData($course);
354  $crs = $this->createCourseReference($crs, $a_parent_obj_id);
355  $this->setImported($course_id, $crs, $a_content_id);
356 
357  // Create parallel groups under crs
358  $this->createParallelGroups($a_content_id,$course,$crs->getRefId());
359  break;
360 
362  // Import empty to store the ecs ressource id (used for course member update).
363  $this->setImported($course_id,NULL,$a_content_id);
364  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "Courses foreach Lecturer".');
365  break;
366 
368  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "Many courses".');
369 
370  $refs = ilObject::_getAllReferences($a_parent_obj_id);
371  $ref = end($refs);
372  $crs = $this->createCourseData($course);
373  $this->createCourseReference($crs, $a_parent_obj_id);
374  $this->setImported($course_id, $crs, $a_content_id);
375  $this->createParallelCourses($a_content_id,$course, $ref);
376  break;
377 
378  default:
380  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "One Course".');
381  $crs = $this->createCourseData($course);
382  $this->createCourseReference($crs, $a_parent_obj_id);
383  $this->setImported($course_id, $crs, $a_content_id);
384  break;
385 
386 
387  }
388  }
389  // finally update course urls
390  $this->handleCourseUrlUpdate();
391  return true;
392  }
393 
400  protected function createParallelCourses($a_content_id,$course, $parent_ref)
401  {
402  foreach((array) $course->groups as $group)
403  {
404  $this->createParallelCourse($a_content_id,$course, $group, $parent_ref);
405  }
406  return true;
407  }
408 
415  protected function createParallelCourse($a_content_id, $course, $group, $parent_ref)
416  {
417  include_once './Modules/Course/classes/class.ilObjCourse.php';
418  $course_obj = new ilObjCourse();
419  $title = strlen($group->title) ? $group->title : $course->title;
420  $GLOBALS['ilLog']->write(__METHOD__.': Creating new parallel course instance from ecs : '. $title);
421  $course_obj->setTitle($title);
422  $course_obj->setSubscriptionMaxMembers((int) $group->maxParticipants);
423  $course_obj->create();
424 
425  $this->createCourseReference($course_obj, ilObject::_lookupObjId($parent_ref));
426  $this->setImported($course->lectureID, $course_obj,$a_content_id, $group->id);
427  $this->setObjectCreated(true);
428  return true;
429  }
430 
436  protected function updateParallelCourses($a_content_id, $course,$parent_obj)
437  {
438  $parent_refs = ilObject::_getAllReferences($parent_obj);
439  $parent_ref = end($parent_refs);
440 
441  foreach((array) $course->groups as $group)
442  {
443  $title = strlen($group->title) ? $group->title : $course->title;
444  $obj_id = $this->getImportId($course->lectureID, $group->id);
445  $GLOBALS['ilLog']->write(__METHOD__.': Imported obj id is ' .$obj_id);
446  if(!$obj_id)
447  {
448  $this->createParallelCourse($a_content_id, $course, $group, $parent_ref);
449  }
450  else
451  {
452  $course_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
453  if($course_obj instanceof ilObjCourse)
454  {
455  $GLOBALS['ilLog']->write(__METHOD__.': New title is '. $title);
456  $course_obj->setTitle($title);
457  $course_obj->setSubscriptionMaxMembers($group->maxParticipants);
458  $course_obj->update();
459  }
460  }
461  $this->addUrlEntry($this->getImportId($course->lectureID, $group->ID));
462  }
463  return true;
464  }
465 
466 
467 
473  protected function createParallelGroups($a_content_id, $course, $parent_ref)
474  {
475  foreach((array) $course->groups as $group)
476  {
477  $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
478  }
479  return true;
480  }
481 
487  protected function createParallelGroup($a_content_id,$course, $group, $parent_ref)
488  {
489  include_once './Modules/Group/classes/class.ilObjGroup.php';
490  $group_obj = new ilObjGroup();
491  $title = strlen($group->title) ? $group->title : $course->title;
492  $group_obj->setTitle($title);
493  $group_obj->setMaxMembers((int) $group->maxParticipants);
494  $group_obj->create();
495  $group_obj->createReference();
496  $group_obj->putInTree($parent_ref);
497  $group_obj->setPermissions($parent_ref);
498  $group_obj->initGroupStatus(GRP_TYPE_CLOSED);
499  $this->setImported($course->lectureID, $group_obj, $a_content_id, $group->id);
500  $this->setObjectCreated(true);
501  }
502 
503 
509  protected function updateParallelGroups($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  {
516  $obj_id = $this->getImportId($course->lectureID, $group->id);
517  $GLOBALS['ilLog']->write(__METHOD__.': Imported obj id is ' .$obj_id);
518  if(!$obj_id)
519  {
520  $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
521  }
522  else
523  {
524  $group_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
525  if($group_obj instanceof ilObjGroup)
526  {
527  $title = strlen($group->title) ? $group->title : $course->title;
528  $GLOBALS['ilLog']->write(__METHOD__.': New title is '. $title);
529  $group_obj->setTitle($title);
530  $group_obj->setMaxMembers((int) $group->maxParticipants);
531  $group_obj->update();
532  }
533  }
534  $this->addUrlEntry($this->getImportId($course->lectureID, $group->id));
535  }
536  }
537 
545  protected function getImportId($a_content_id,$a_sub_id = NULL)
546  {
547  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
549  $this->getServer()->getServerId(),
550  $this->getMid(),
551  $a_content_id,
552  $a_sub_id
553  );
554  }
555 
560  protected function updateCourseData($course,$obj_id)
561  {
562  // do update
563  $refs = ilObject::_getAllReferences($obj_id);
564  $ref_id = end($refs);
566  if(!$crs_obj instanceof ilObject)
567  {
568  $GLOBALS['ilLog']->write(__METHOD__.': Cannot instantiate course instance');
569  return true;
570  }
571 
572  // Update title
573  $title = $course->title;
574  $GLOBALS['ilLog']->write(__METHOD__.': new title is : '. $title);
575 
576  $crs_obj->setTitle($title);
577  $crs_obj->update();
578  return true;
579  }
580 
585  protected function createCourseData($course)
586  {
587  include_once './Modules/Course/classes/class.ilObjCourse.php';
588  $course_obj = new ilObjCourse();
589  $title = $course->title;
590  $GLOBALS['ilLog']->write(__METHOD__.': Creating new course instance from ecs : '. $title);
591  $course_obj->setTitle($title);
592  $course_obj->create();
593  return $course_obj;
594  }
595 
602  protected function createCourseReference($crs,$a_parent_obj_id)
603  {
604  $ref_ids = ilObject::_getAllReferences($a_parent_obj_id);
605  $ref_id = end($ref_ids);
606 
607  $crs->createReference();
608  $crs->putInTree($ref_id);
609  $crs->setPermissions($ref_id);
610 
611  $this->setObjectCreated(true);
612  $this->addUrlEntry($crs->getId());
613  return $crs;
614  }
615 
621  protected function setImported($a_content_id, $object, $a_ecs_id = 0, $a_sub_id = 0)
622  {
623  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
624  $import = new ilECSImport(
625  $this->getServer()->getServerId(),
626  is_object($object) ? $object->getId() : 0
627  );
628 
629  $GLOBALS['ilLog']->write(__METHOD__.': Imported with ecs id '.$a_ecs_id);
630 
631  $import->setSubId($a_sub_id);
632  $import->setMID($this->getMid());
633  $import->setEContentId($a_ecs_id);
634  $import->setContentId($a_content_id);
635  $import->setImported(true);
636  $import->save();
637  return true;
638  }
639 
644  protected function addUrlEntry($a_obj_id)
645  {
646  $refs = ilObject::_getAllReferences($a_obj_id);
647  $ref_id = end($refs);
648 
649  if(!$ref_id)
650  {
651  return false;
652  }
653  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseLmsUrl.php';
654  $lms_url = new ilECSCourseLmsUrl();
655  $lms_url->setTitle(ilObject::_lookupTitle($a_obj_id));
656 
657  include_once './Services/Link/classes/class.ilLink.php';
658  $lms_url->setUrl(ilLink::_getLink($ref_id));
659  $this->getCourseUrl()->addLmsCourseUrls($lms_url);
660  }
661 
665  protected function handleCourseUrlUpdate()
666  {
667  $GLOBALS['ilLog']->write(__METHOD__.': Starting course url update');
668  if($this->isObjectCreated())
669  {
670  $GLOBALS['ilLog']->write(__METHOD__.': Sending new course group url');
671  $this->getCourseUrl()->send($this->getServer(), $this->getMid());
672  }
673  else
674  {
675  $GLOBALS['ilLog']->write(__METHOD__.': No courses groups created. Aborting');
676  }
677  }
678 }
679 ?>