ILIAS  release_4-3 Revision
 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!');
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->setEContentId($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__.': Handling course '. print_r($course,true));
317 
318  // Handle parallel groups
319  if($obj_id)
320  {
321  // update multiple courses/groups according to parallel scenario
322  $GLOBALS['ilLog']->write(__METHOD__.': '.$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 
361 
362 
364  // Import empty to store the ecs ressource id (used for course member update).
365  $this->setImported($course_id,NULL,$a_content_id);
366  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "Courses foreach Lecturer".');
367  break;
368 
370  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "Many courses".');
371 
372  $refs = ilObject::_getAllReferences($a_parent_obj_id);
373  $ref = end($refs);
374  $crs = $this->createCourseData($course);
375  $this->createCourseReference($crs, $a_parent_obj_id);
376  $this->setImported($course_id, $crs, $a_content_id);
377  $this->createParallelCourses($a_content_id,$course, $ref);
378  break;
379 
380  default:
382  $GLOBALS['ilLog']->write(__METHOD__.': Parallel scenario "One Course".');
383  $crs = $this->createCourseData($course);
384  $this->createCourseReference($crs, $a_parent_obj_id);
385  $this->setImported($course_id, $crs, $a_content_id);
386  break;
387 
388 
389  }
390  }
391  // finally update course urls
392  $this->handleCourseUrlUpdate();
393  return true;
394  }
395 
402  protected function createParallelCourses($a_content_id,$course, $parent_ref)
403  {
404  foreach((array) $course->groups as $group)
405  {
406  $this->createParallelCourse($a_content_id,$course, $group, $parent_ref);
407  }
408  return true;
409  }
410 
417  protected function createParallelCourse($a_content_id, $course, $group, $parent_ref)
418  {
419  include_once './Modules/Course/classes/class.ilObjCourse.php';
420  $course_obj = new ilObjCourse();
421  $title = strlen($group->title) ? $group->title : $course->title;
422  $GLOBALS['ilLog']->write(__METHOD__.': Creating new parallel course instance from ecs : '. $title);
423  $course_obj->setTitle($title);
424  $course_obj->setSubscriptionMaxMembers((int) $group->maxParticipants);
425  $course_obj->create();
426 
427  $this->createCourseReference($course_obj, ilObject::_lookupObjId($parent_ref));
428  $this->setImported($course->lectureID, $course_obj,$a_content_id, $group->id);
429  $this->setObjectCreated(true);
430  return true;
431  }
432 
438  protected function updateParallelCourses($a_content_id, $course,$parent_obj)
439  {
440  $parent_refs = ilObject::_getAllReferences($parent_obj);
441  $parent_ref = end($parent_refs);
442 
443  foreach((array) $course->groups as $group)
444  {
445  $title = strlen($group->title) ? $group->title : $course->title;
446  $obj_id = $this->getImportId($course->lectureID, $group->id);
447  $GLOBALS['ilLog']->write(__METHOD__.': Imported obj id is ' .$obj_id);
448  if(!$obj_id)
449  {
450  $this->createParallelCourse($a_content_id, $course, $group, $parent_ref);
451  }
452  else
453  {
454  $course_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
455  if($course_obj instanceof ilObjCourse)
456  {
457  $GLOBALS['ilLog']->write(__METHOD__.': New title is '. $title);
458  $course_obj->setTitle($title);
459  $course_obj->setSubscriptionMaxMembers($group->maxParticipants);
460  $course_obj->update();
461  }
462  }
463  $this->addUrlEntry($this->getImportId($course->lectureID, $group->ID));
464  }
465  return true;
466  }
467 
468 
469 
475  protected function createParallelGroups($a_content_id, $course, $parent_ref)
476  {
477  foreach((array) $course->groups as $group)
478  {
479  $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
480  }
481  return true;
482  }
483 
489  protected function createParallelGroup($a_content_id,$course, $group, $parent_ref)
490  {
491  include_once './Modules/Group/classes/class.ilObjGroup.php';
492  $group_obj = new ilObjGroup();
493  $title = strlen($group->title) ? $group->title : $course->title;
494  $group_obj->setTitle($title);
495  $group_obj->setMaxMembers((int) $group->maxParticipants);
496  $group_obj->create();
497  $group_obj->createReference();
498  $group_obj->putInTree($parent_ref);
499  $group_obj->setPermissions($parent_ref);
500  $group_obj->initGroupStatus(GRP_TYPE_CLOSED);
501  $this->setImported($course->lectureID, $group_obj, $a_content_id, $group->id);
502  $this->setObjectCreated(true);
503  }
504 
505 
511  protected function updateParallelGroups($a_content_id, $course,$parent_obj)
512  {
513  $parent_refs = ilObject::_getAllReferences($parent_obj);
514  $parent_ref = end($parent_refs);
515 
516  foreach((array) $course->groups as $group)
517  {
518  $obj_id = $this->getImportId($course->lectureID, $group->id);
519  $GLOBALS['ilLog']->write(__METHOD__.': Imported obj id is ' .$obj_id);
520  if(!$obj_id)
521  {
522  $this->createParallelGroup($a_content_id,$course, $group, $parent_ref);
523  }
524  else
525  {
526  $group_obj = ilObjectFactory::getInstanceByObjId($obj_id,false);
527  if($group_obj instanceof ilObjGroup)
528  {
529  $title = strlen($group->title) ? $group->title : $course->title;
530  $GLOBALS['ilLog']->write(__METHOD__.': New title is '. $title);
531  $group_obj->setTitle($title);
532  $group_obj->setMaxMembers((int) $group->maxParticipants);
533  $group_obj->update();
534  }
535  }
536  $this->addUrlEntry($this->getImportId($course->lectureID, $group->id));
537  }
538  }
539 
547  protected function getImportId($a_content_id,$a_sub_id = NULL)
548  {
549  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
551  $this->getServer()->getServerId(),
552  $this->getMid(),
553  $a_content_id,
554  $a_sub_id
555  );
556  }
557 
562  protected function updateCourseData($course,$obj_id)
563  {
564  // do update
565  $refs = ilObject::_getAllReferences($obj_id);
566  $ref_id = end($refs);
568  if(!$crs_obj instanceof ilObject)
569  {
570  $GLOBALS['ilLog']->write(__METHOD__.': Cannot instantiate course instance');
571  return true;
572  }
573 
574  // Update title
575  $title = $course->title;
576  $GLOBALS['ilLog']->write(__METHOD__.': new title is : '. $title);
577 
578  $crs_obj->setTitle($title);
579  $crs_obj->update();
580  return true;
581  }
582 
587  protected function createCourseData($course)
588  {
589  include_once './Modules/Course/classes/class.ilObjCourse.php';
590  $course_obj = new ilObjCourse();
591  $title = $course->title;
592  $GLOBALS['ilLog']->write(__METHOD__.': Creating new course instance from ecs : '. $title);
593  $course_obj->setTitle($title);
594  $course_obj->create();
595  return $course_obj;
596  }
597 
604  protected function createCourseReference($crs,$a_parent_obj_id)
605  {
606  $ref_ids = ilObject::_getAllReferences($a_parent_obj_id);
607  $ref_id = end($ref_ids);
608 
609  $crs->createReference();
610  $crs->putInTree($ref_id);
611  $crs->setPermissions($ref_id);
612 
613  $this->setObjectCreated(true);
614  $this->addUrlEntry($crs->getId());
615  return $crs;
616  }
617 
623  protected function setImported($a_content_id, $object, $a_ecs_id = 0, $a_sub_id = 0)
624  {
625  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
626  $import = new ilECSImport(
627  $this->getServer()->getServerId(),
628  is_object($object) ? $object->getId() : 0
629  );
630 
631  $GLOBALS['ilLog']->write(__METHOD__.': Imported with ecs id '.$a_ecs_id);
632 
633  $import->setSubId($a_sub_id);
634  $import->setMID($this->getMid());
635  $import->setEContentId($a_ecs_id);
636  $import->setContentId($a_content_id);
637  $import->setImported(true);
638  $import->save();
639  return true;
640  }
641 
646  protected function addUrlEntry($a_obj_id)
647  {
648  $refs = ilObject::_getAllReferences($a_obj_id);
649  $ref_id = end($refs);
650 
651  if(!$ref_id)
652  {
653  return false;
654  }
655  include_once './Services/WebServices/ECS/classes/Course/class.ilECSCourseLmsUrl.php';
656  $lms_url = new ilECSCourseLmsUrl();
657  $lms_url->setTitle(ilObject::_lookupTitle($a_obj_id));
658 
659  include_once './Services/Link/classes/class.ilLink.php';
660  $lms_url->setUrl(ilLink::_getLink($ref_id));
661  $this->getCourseUrl()->addLmsCourseUrls($lms_url);
662  }
663 
667  protected function handleCourseUrlUpdate()
668  {
669  $GLOBALS['ilLog']->write(__METHOD__.': Starting course url update');
670  if($this->isObjectCreated())
671  {
672  $GLOBALS['ilLog']->write(__METHOD__.': Sending new course group url');
673  $this->getCourseUrl()->send($this->getServer(), $this->getMid());
674  }
675  else
676  {
677  $GLOBALS['ilLog']->write(__METHOD__.': No courses groups created. Aborting');
678  }
679  }
680 }
681 ?>