ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSObjectSettings.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
13 abstract class ilECSObjectSettings
14 {
15  protected $content_obj; // [ilObj]
16 
17  const MAIL_SENDER = 6;
18 
24  public function __construct(ilObject $a_content_object)
25  {
26  $this->content_obj = $a_content_object;
27  }
28 
35  public static function getInstanceByObject(ilObject $a_content_obj)
36  {
37  switch($a_content_obj->getType())
38  {
39  case 'crs':
40  include_once 'Modules/Course/classes/class.ilECSCourseSettings.php';
41  return new ilECSCourseSettings($a_content_obj);
42 
43  case 'cat':
44  include_once 'Modules/Category/classes/class.ilECSCategorySettings.php';
45  return new ilECSCategorySettings($a_content_obj);
46 
47  case 'file':
48  include_once 'Modules/File/classes/class.ilECSFileSettings.php';
49  return new ilECSFileSettings($a_content_obj);
50 
51  case 'glo':
52  include_once 'Modules/Glossary/classes/class.ilECSGlossarySettings.php';
53  return new ilECSGlossarySettings($a_content_obj);
54 
55  case 'grp':
56  include_once 'Modules/Group/classes/class.ilECSGroupSettings.php';
57  return new ilECSGroupSettings($a_content_obj);
58 
59  case 'lm':
60  include_once 'Modules/LearningModule/classes/class.ilECSLearningModuleSettings.php';
61  return new ilECSLearningModuleSettings($a_content_obj);
62 
63  case 'wiki':
64  include_once 'Modules/Wiki/classes/class.ilECSWikiSettings.php';
65  return new ilECSWikiSettings($a_content_obj);
66  }
67  }
68 
73  public function getContentObject()
74  {
75  return $this->content_obj;
76  }
77 
83  abstract protected function getECSObjectType();
84 
90  protected function isActive()
91  {
92  include_once('./Services/WebServices/ECS/classes/class.ilECSServerSettings.php');
93  if(ilECSServerSettings::getInstance()->activeServerExists())
94  {
95  // imported objects cannot be exported
96  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
97  if(!ilECSImport::lookupServerId($this->content_obj->getId()))
98  {
99  return true;
100  }
101  }
102 
103  return false;
104  }
105 
113  public function addSettingsToForm(ilPropertyFormGUI $a_form, $a_type)
114  {
115  global $lng;
116 
117  if(!$this->isActive())
118  {
119  return;
120  }
121 
122  $obj_id = $this->content_obj->getId();
123 
124  // Return if no participant is enabled for export and the current object is not released
125  include_once './Services/WebServices/ECS/classes/class.ilECSExport.php';
126  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
127 
129  if(!$exportablePart and !ilECSExport::_isExported($obj_id))
130  {
131  return true;
132  }
133  if(
134  $GLOBALS['tree']->checkForParentType($GLOBALS['tree']->getParentId($this->getContentObject()->getRefId()),'crs',false) or
135  $GLOBALS['tree']->checkForParentType($GLOBALS['tree']->getParentId($this->getContentObject()->getRefId()),'grp',false)
136  )
137  {
138  return true;
139  }
140 
141  $lng->loadLanguageModule('ecs');
142 
143  // show ecs property form section
144  $ecs = new ilFormSectionHeaderGUI();
145  $ecs->setTitle($lng->txt('ecs_'.$a_type.'_export'));
146  $a_form->addItem($ecs);
147 
148 
149  // release or not
150  $exp = new ilRadioGroupInputGUI($lng->txt('ecs_'.$a_type.'_export_obj_settings'),'ecs_export');
151  $exp->setRequired(true);
152  $exp->setValue(ilECSExport::_isExported($obj_id) ? 1 : 0);
153  $off = new ilRadioOption($lng->txt('ecs_'.$a_type.'_export_disabled'),0);
154  $exp->addOption($off);
155  $on = new ilRadioOption($lng->txt('ecs_'.$a_type.'_export_enabled'),1);
156  $exp->addOption($on);
157  $a_form->addItem($exp);
158 
159  // Show all exportable participants
160  $publish_for = new ilCheckboxGroupInputGUI($lng->txt('ecs_publish_for'),'ecs_sid');
161 
162  // @TODO: Active checkboxes for recipients
163  //$publish_for->setValue((array) $members);
164 
165  // Read receivers
166  $receivers = array();
167  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
168  foreach(ilECSExport::getExportServerIds($obj_id) as $sid)
169  {
170  $exp = new ilECSExport($sid, $obj_id);
171 
172  $participants = null;
173  $details = ilECSEContentDetails::getInstance($sid, $exp->getEContentId(),
174  $this->getECSObjectType());
175  if($details instanceof ilECSEContentDetails)
176  {
177  $participants = $details->getReceivers();
178  }
179  if($participants)
180  {
181  foreach($participants as $mid)
182  {
183  $receivers[] = $sid.'_'.$mid;
184  }
185  }
186  }
187  $publish_for->setValue($receivers);
188 
189  foreach($exportablePart as $pInfo)
190  {
191  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
192  $partSetting = new ilECSParticipantSetting($pInfo['sid'], $pInfo['mid']);
193 
194  $com = new ilCheckboxInputGUI(
195  $partSetting->getCommunityName().': '.$partSetting->getTitle(),
196  'sid_mid'
197  );
198  $com->setValue($pInfo['sid'].'_'.$pInfo['mid']);
199  $publish_for->addOption($com);
200  }
201  $on->addSubItem($publish_for);
202  return true;
203  }
204 
213  public function handleSettingsUpdate()
214  {
215  if(!$this->isActive())
216  {
217  return true;
218  }
219 
220  // Parse post data
221  $mids = array();
222  foreach((array) $_POST['ecs_sid'] as $sid_mid)
223  {
224  $tmp = explode('_',$sid_mid);
225  $mids[$tmp[0]][] = $tmp[1];
226  }
227 
228  try
229  {
230  include_once './Services/WebServices/ECS/classes/class.ilECSCommunitiesCache.php';
231  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
232  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
233 
234  // Update for each server
235  foreach(ilECSParticipantSettings::getExportServers() as $server_id)
236  {
238  if($server->isEnabled())
239  {
240  // Export
241  $export = true;
242  if(!$_POST['ecs_export'])
243  {
244  $export = false;
245  }
246  if(!count($mids[$server_id]))
247  {
248  $export = false;
249  }
250  $this->handleSettingsForServer(
251  $server,
252  $export,
253  $mids[$server_id]
254  );
255  }
256  }
257  }
258  catch(ilECSConnectorException $exc)
259  {
260  ilUtil::sendFailure('Error exporting to ECS server: '.$exc->getMessage());
261  return false;
262  }
263  return true;
264  }
265 
274  protected function handleSettingsForServer(ilECSSetting $a_server,$a_export,$a_mids)
275  {
276  try
277  {
278  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
279  $export_settings = new ilECSExport($a_server->getServerId(), $this->content_obj->getId());
280 
281  // already exported?
282  if($export_settings->isExported())
283  {
284  // still exportable: update ecs
285  if((bool)$a_export)
286  {
287  $this->doUpdate($a_server, $export_settings, $a_mids);
288  }
289  // not exportable anymore
290  else
291  {
292  $this->doDelete($a_server, $export_settings);
293  }
294  }
295  // not exported yet
296  else
297  {
298  // now to be exported
299  if($a_export)
300  {
301  $this->doAdd($a_server, $export_settings, $a_mids);
302  }
303  // was not and will not be exported
304  else
305  {
306 
307  }
308  }
309  }
310  catch(ilECSConnectorException $exc)
311  {
312  throw $exc;
313  }
314  }
315 
323  public function handleContentUpdate()
324  {
325  global $ilLog;
326 
327  if(!$this->isActive())
328  {
329  return true;
330  }
331 
332  include_once './Services/WebServices/ECS/classes/class.ilECSExport.php';
333  $export_servers = ilECSExport::getExportServerIds($this->content_obj->getId());
334  foreach($export_servers as $server_id)
335  {
336  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
338  if($server->isEnabled())
339  {
340  try
341  {
342  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
343  $export_settings = new ilECSExport($server_id, $this->content_obj->getId());
344 
345  // already exported, update ecs
346  if($export_settings->isExported())
347  {
348  $this->doUpdate($server, $export_settings);
349  }
350  // not exported yet, nothing to do
351  else
352  {
353 
354  }
355  }
356  catch(ilECSConnectorException $exc)
357  {
358  $ilLog->write(__METHOD__.': Cannot handle ECS content update. '.$exc->getMessage());
359  return false;
360  }
361  }
362  }
363  return true;
364  }
365 
373  protected function doAdd(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids)
374  {
375  global $ilLog;
376 
377  $ilLog->write(__METHOD__.': Starting ECS add resource...');
378 
379  $json = $this->buildJson($a_server);
380 
381  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
382  $connector = new ilECSConnector($a_server);
383  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$a_mids));
384  $econtent_id = $connector->addResource($this->getECSObjectType(),
385  json_encode($json));
386 
387  // status changed
388  $a_export_settings->setExported(true);
389  $a_export_settings->setEContentId($econtent_id);
390  $a_export_settings->save();
391 
392  $this->handlePermissionUpdate($a_server,true);
393 
394  // Send mail
395  $this->sendNewContentNotification($a_server, $econtent_id);
396  }
397 
406  protected function doUpdate(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids = null)
407  {
408  global $ilLog;
409 
410  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
411 
412  $econtent_id = $a_export_settings->getEContentId();
413  if(!$econtent_id)
414  {
415  $ilLog->write(__METHOD__.': Missing eid. Aborting.');
416  throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
417  }
418  $connector = new ilECSConnector($a_server);
419 
420  if(!$a_mids)
421  {
422  $a_mids = $this->getParticipants($a_server->getServerId(), $econtent_id);
423  }
424  $ilLog->write(__METHOD__.': Start updating ECS content - '.print_r($a_mids,true));
425  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS,implode(',',(array) $a_mids));
426 
427  $json = $this->buildJson($a_server);
428  $connector->updateResource($this->getECSObjectType(),
429  $econtent_id, json_encode($json));
430 
431  $this->handlePermissionUpdate($a_server,true);
432  }
433 
442  public function doDelete(ilECSSetting $a_server, ilECSExport $a_export_settings)
443  {
444  global $ilLog;
445 
446  // already exported?
447  if($a_export_settings->isExported())
448  {
449  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
450  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
451 
452  $econtent_id = $a_export_settings->getEContentId();
453  if(!$econtent_id)
454  {
455  $ilLog->write(__METHOD__.': Missing eid. Aborting.');
456  throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
457  }
458  $connector = new ilECSConnector($a_server);
459 
460  $ilLog->write(__METHOD__.': Start deleting ECS content...');
461  $connector->deleteResource($this->getECSObjectType(),
462  $econtent_id);
463 
464  // status changed
465  $a_export_settings->setExported(false);
466  $a_export_settings->save();
467  }
468  }
469 
477  public static function _handleDelete(array $a_subbtree_nodes)
478  {
479  // active?
480  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
481  if(!ilECSServerSettings::getInstance()->activeServerExists())
482  {
483  return;
484  }
485 
486  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
487  include_once './Services/WebServices/ECS/classes/class.ilECSExport.php' ;
488  $exported = ilECSExport::getExportedIds();
489  foreach($a_subbtree_nodes as $node)
490  {
491  if(in_array($node['obj_id'],$exported))
492  {
493  if($content_obj = ilObjectFactory::getInstanceByRefId($node['child'],false))
494  {
496 
497  // Read export server ids
498  foreach(ilECSExport::getExportServerIds($node['obj_id']) as $sid)
499  {
501  $export_settings = new ilECSExport($sid, $content_obj->getId());
502  $settings->doDelete($server, $export_settings);
503  }
504  }
505  }
506  }
507  }
508 
516  protected function getParticipants($a_server_id, $a_econtent_id)
517  {
518  $receivers = array();
519  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
520  foreach((array) $a_server_id as $sid)
521  {
522  $participants = null;
523  $details = ilECSEContentDetails::getInstance($sid, $a_econtent_id,$this->getECSObjectType());
524  if($details instanceof ilECSEContentDetails)
525  {
526  $participants = $details->getReceivers();
527  }
528  if($participants)
529  {
530  foreach($participants as $mid)
531  {
532  $receivers[] = $mid;
533  }
534  }
535  }
536  return (array) $receivers;
537  }
538 
544  protected function sendNewContentNotification(ilECSSetting $a_server, $a_econtent_id)
545  {
546  global $ilLog;
547 
548  if(!count($rcps = $a_server->getApprovalRecipients()))
549  {
550  return true;
551  }
552 
553  include_once('./Services/Mail/classes/class.ilMail.php');
554  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
555 
557  $lang->loadLanguageModule('ecs');
558 
559  // @TODO: read mail
560  $mail = new ilMail(self::MAIL_SENDER);
561  $message = $lang->txt('ecs_export_created_body_a')."\n\n";
562  $message .= $lang->txt('title').': '.$this->content_obj->getTitle()."\n";
563  if(strlen($desc = $this->content_obj->getDescription()))
564  {
565  $message .= $lang->txt('desc').': '.$desc."\n";
566  }
567 
568  // Participant info
569  $message .= ("\n".$lang->txt('ecs_published_for'));
570 
571  try
572  {
573  $found = false;
574 
575  $receivers = null;
576  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
577  $details = ilECSEContentDetails::getInstance($a_server->getServerId(),
578  $a_econtent_id, $this->getECSObjectType());
579  if($details instanceof ilECSEContentDetails)
580  {
581  $receivers = $details->getReceivers();
582  }
583  if($receivers)
584  {
585  foreach($receivers as $member)
586  {
587  $found = true;
588 
589  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
590  $part = ilECSCommunityReader::getInstanceByServerId($a_server->getServerId())->getParticipantByMID($member);
591 
592  $message .= ("\n\n".$part->getParticipantName()."\n");
593  $message .= ($part->getDescription());
594  }
595  }
596  if($found)
597  {
598  $message .= "\n\n";
599  }
600  else
601  {
602  $message .= (' '.$lang->txt('ecs_not_published')."\n\n");
603  }
604  }
605  catch(ilECSConnectorException $e)
606  {
607  $ilLog->write(__METHOD__.': Cannot read approvements.');
608  return false;
609  }
610 
611  include_once('./Services/Link/classes/class.ilLink.php');
612  $href = ilLink::_getStaticLink($this->content_obj->getRefId(),'crs',true);
613  $message .= $lang->txt("perma_link").': '.$href."\n\n";
615 
616  $mail->sendMail($a_server->getApprovalRecipientsAsString(),
617  '','',
618  $lang->txt('ecs_new_approval_subject'),
619  $message,array(),array('normal'));
620 
621  return true;
622  }
623 
625  {
626  if($this->content_obj->getType() == 'crs')
627  {
628  $GLOBALS['ilLog']->write(__METHOD__.': Permission update');
629  if($this->content_obj->getType() == 'crs')
630  {
631  $GLOBALS['rbacadmin']->grantPermission(
632  $server->getGlobalRole(),
633  ilRbacReview::_getOperationIdsByName(array('join','visible')),
634  $this->content_obj->getRefId()
635  );
636  }
637  }
638  }
639 
646  protected function getJsonCore($a_etype)
647  {
648  $json = new stdClass();
649  $json->lang = 'en_EN'; // :TODO: obsolet?
650  $json->id = 'il_'.IL_INST_ID.'_'.$this->getContentObject()->getType().'_'.$this->getContentObject()->getId();
651  $json->etype = $a_etype;
652  $json->title = $this->content_obj->getTitle();
653  $json->abstract = $this->content_obj->getLongDescription();
654 
655  include_once('./Services/Link/classes/class.ilLink.php');
656  $json->url = ilLink::_getLink($this->content_obj->getRefId(),$this->content_obj->getType());
657 
658  return $json;
659  }
660 
668  protected function addMetadataToJson(&$a_json, ilECSSetting $a_server, array $a_definition)
669  {
670  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
671  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
672  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
674  $values = ilAdvancedMDValues::_getValuesByObjId($this->content_obj->getId());
675 
676  foreach($a_definition as $id => $type)
677  {
678  if(is_array($type))
679  {
680  $target = $type[1];
681  $type = $type[0];
682  }
683  else
684  {
685  $target = $id;
686  }
687 
688  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, $id))
689  {
690  $value = isset($values[$field]) ? $values[$field] : '';
691 
692  switch($type)
693  {
695  $a_json->$target = explode(',', $value);
696  break;
697 
699  $a_json->$target = (int)$value;
700  break;
701 
703  $a_json->$target = (string)$value;
704  break;
705 
707  if(!isset($a_json->$target))
708  {
709  include_once('./Services/WebServices/ECS/classes/class.ilECSTimePlace.php');
710  $a_json->$target = new ilECSTimePlace();
711  }
712  $a_json->$target->{'set'.ucfirst($id)}($value);
713  break;
714  }
715  }
716  }
717  }
718 
725  abstract protected function buildJson(ilECSSetting $a_server);
726 }
727 
728 ?>