ILIAS  Release_4_4_x_branch Revision 61816
 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 
128  if(!$this->getContentObject()->withReferences())
129  {
130  return TRUE;
131  }
132 
134  if(!$exportablePart and !ilECSExport::_isExported($obj_id))
135  {
136  return true;
137  }
138  if(
139  $GLOBALS['tree']->checkForParentType($GLOBALS['tree']->getParentId($this->getContentObject()->getRefId()),'crs',false) or
140  $GLOBALS['tree']->checkForParentType($GLOBALS['tree']->getParentId($this->getContentObject()->getRefId()),'grp',false)
141  )
142  {
143  return true;
144  }
145 
146  $lng->loadLanguageModule('ecs');
147 
148  // show ecs property form section
149  $ecs = new ilFormSectionHeaderGUI();
150  $ecs->setTitle($lng->txt('ecs_'.$a_type.'_export'));
151  $a_form->addItem($ecs);
152 
153 
154  // release or not
155  $exp = new ilRadioGroupInputGUI($lng->txt('ecs_'.$a_type.'_export_obj_settings'),'ecs_export');
156  $exp->setRequired(true);
157  $exp->setValue(ilECSExport::_isExported($obj_id) ? 1 : 0);
158  $off = new ilRadioOption($lng->txt('ecs_'.$a_type.'_export_disabled'),0);
159  $exp->addOption($off);
160  $on = new ilRadioOption($lng->txt('ecs_'.$a_type.'_export_enabled'),1);
161  $exp->addOption($on);
162  $a_form->addItem($exp);
163 
164  // Show all exportable participants
165  $publish_for = new ilCheckboxGroupInputGUI($lng->txt('ecs_publish_for'),'ecs_sid');
166 
167  // @TODO: Active checkboxes for recipients
168  //$publish_for->setValue((array) $members);
169 
170  // Read receivers
171  $receivers = array();
172  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
173  foreach(ilECSExport::getExportServerIds($obj_id) as $sid)
174  {
175  $exp = new ilECSExport($sid, $obj_id);
176 
177  $participants = null;
178  $details = ilECSEContentDetails::getInstance($sid, $exp->getEContentId(),
179  $this->getECSObjectType());
180  if($details instanceof ilECSEContentDetails)
181  {
182  $participants = $details->getReceivers();
183  }
184  if($participants)
185  {
186  foreach($participants as $mid)
187  {
188  $receivers[] = $sid.'_'.$mid;
189  }
190  }
191  }
192  $publish_for->setValue($receivers);
193 
194  foreach($exportablePart as $pInfo)
195  {
196  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
197  $partSetting = new ilECSParticipantSetting($pInfo['sid'], $pInfo['mid']);
198 
199  $com = new ilCheckboxInputGUI(
200  $partSetting->getCommunityName().': '.$partSetting->getTitle(),
201  'sid_mid'
202  );
203  $com->setValue($pInfo['sid'].'_'.$pInfo['mid']);
204  $publish_for->addOption($com);
205  }
206  $on->addSubItem($publish_for);
207  return true;
208  }
209 
218  public function handleSettingsUpdate()
219  {
220  if(!$this->isActive())
221  {
222  return true;
223  }
224 
225  // Parse post data
226  $mids = array();
227  foreach((array) $_POST['ecs_sid'] as $sid_mid)
228  {
229  $tmp = explode('_',$sid_mid);
230  $mids[$tmp[0]][] = $tmp[1];
231  }
232 
233  try
234  {
235  include_once './Services/WebServices/ECS/classes/class.ilECSCommunitiesCache.php';
236  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
237  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
238 
239  // Update for each server
240  foreach(ilECSParticipantSettings::getExportServers() as $server_id)
241  {
243  if($server->isEnabled())
244  {
245  // Export
246  $export = true;
247  if(!$_POST['ecs_export'])
248  {
249  $export = false;
250  }
251  if(!count($mids[$server_id]))
252  {
253  $export = false;
254  }
255  $this->handleSettingsForServer(
256  $server,
257  $export,
258  $mids[$server_id]
259  );
260  }
261  }
262  }
263  catch(ilECSConnectorException $exc)
264  {
265  ilUtil::sendFailure('Error exporting to ECS server: '.$exc->getMessage());
266  return false;
267  }
268  return true;
269  }
270 
279  protected function handleSettingsForServer(ilECSSetting $a_server,$a_export,$a_mids)
280  {
281  try
282  {
283  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
284  $export_settings = new ilECSExport($a_server->getServerId(), $this->content_obj->getId());
285 
286  // already exported?
287  if($export_settings->isExported())
288  {
289  // still exportable: update ecs
290  if((bool)$a_export)
291  {
292  $this->doUpdate($a_server, $export_settings, $a_mids);
293  }
294  // not exportable anymore
295  else
296  {
297  $this->doDelete($a_server, $export_settings);
298  }
299  }
300  // not exported yet
301  else
302  {
303  // now to be exported
304  if($a_export)
305  {
306  $this->doAdd($a_server, $export_settings, $a_mids);
307  }
308  // was not and will not be exported
309  else
310  {
311 
312  }
313  }
314  }
315  catch(ilECSConnectorException $exc)
316  {
317  throw $exc;
318  }
319  }
320 
328  public function handleContentUpdate()
329  {
330  global $ilLog;
331 
332  if(!$this->isActive())
333  {
334  return true;
335  }
336 
337  include_once './Services/WebServices/ECS/classes/class.ilECSExport.php';
338  $export_servers = ilECSExport::getExportServerIds($this->content_obj->getId());
339  foreach($export_servers as $server_id)
340  {
341  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
343  if($server->isEnabled())
344  {
345  try
346  {
347  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
348  $export_settings = new ilECSExport($server_id, $this->content_obj->getId());
349 
350  // already exported, update ecs
351  if($export_settings->isExported())
352  {
353  $this->doUpdate($server, $export_settings);
354  }
355  // not exported yet, nothing to do
356  else
357  {
358 
359  }
360  }
361  catch(ilECSConnectorException $exc)
362  {
363  $ilLog->write(__METHOD__.': Cannot handle ECS content update. '.$exc->getMessage());
364  return false;
365  }
366  }
367  }
368  return true;
369  }
370 
378  protected function doAdd(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids)
379  {
380  global $ilLog;
381 
382  $ilLog->write(__METHOD__.': Starting ECS add resource...');
383 
384  $json = $this->buildJson($a_server);
385 
386  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
387  $connector = new ilECSConnector($a_server);
388  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',',$a_mids));
389  $econtent_id = $connector->addResource($this->getECSObjectType(),
390  json_encode($json));
391 
392  // status changed
393  $a_export_settings->setExported(true);
394  $a_export_settings->setEContentId($econtent_id);
395  $a_export_settings->save();
396 
397  $this->handlePermissionUpdate($a_server,true);
398 
399  // Send mail
400  $this->sendNewContentNotification($a_server, $econtent_id);
401  }
402 
411  protected function doUpdate(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids = null)
412  {
413  global $ilLog;
414 
415  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
416 
417  $econtent_id = $a_export_settings->getEContentId();
418  if(!$econtent_id)
419  {
420  $ilLog->write(__METHOD__.': Missing eid. Aborting.');
421  throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
422  }
423  $connector = new ilECSConnector($a_server);
424 
425  if(!$a_mids)
426  {
427  $a_mids = $this->getParticipants($a_server->getServerId(), $econtent_id);
428  }
429  $ilLog->write(__METHOD__.': Start updating ECS content - '.print_r($a_mids,true));
430  $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS,implode(',',(array) $a_mids));
431 
432  $json = $this->buildJson($a_server);
433  $connector->updateResource($this->getECSObjectType(),
434  $econtent_id, json_encode($json));
435 
436  $this->handlePermissionUpdate($a_server,true);
437  }
438 
447  public function doDelete(ilECSSetting $a_server, ilECSExport $a_export_settings)
448  {
449  global $ilLog;
450 
451  // already exported?
452  if($a_export_settings->isExported())
453  {
454  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
455  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
456 
457  $econtent_id = $a_export_settings->getEContentId();
458  if(!$econtent_id)
459  {
460  $ilLog->write(__METHOD__.': Missing eid. Aborting.');
461  throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
462  }
463  $connector = new ilECSConnector($a_server);
464 
465  $ilLog->write(__METHOD__.': Start deleting ECS content...');
466  $connector->deleteResource($this->getECSObjectType(),
467  $econtent_id);
468 
469  // status changed
470  $a_export_settings->setExported(false);
471  $a_export_settings->save();
472  }
473  }
474 
482  public static function _handleDelete(array $a_subbtree_nodes)
483  {
484  // active?
485  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
486  if(!ilECSServerSettings::getInstance()->activeServerExists())
487  {
488  return;
489  }
490 
491  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
492  include_once './Services/WebServices/ECS/classes/class.ilECSExport.php' ;
493  $exported = ilECSExport::getExportedIds();
494  foreach($a_subbtree_nodes as $node)
495  {
496  if(in_array($node['obj_id'],$exported))
497  {
498  if($content_obj = ilObjectFactory::getInstanceByRefId($node['child'],false))
499  {
501 
502  // Read export server ids
503  foreach(ilECSExport::getExportServerIds($node['obj_id']) as $sid)
504  {
506  $export_settings = new ilECSExport($sid, $content_obj->getId());
507  $settings->doDelete($server, $export_settings);
508  }
509  }
510  }
511  }
512  }
513 
521  protected function getParticipants($a_server_id, $a_econtent_id)
522  {
523  $receivers = array();
524  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
525  foreach((array) $a_server_id as $sid)
526  {
527  $participants = null;
528  $details = ilECSEContentDetails::getInstance($sid, $a_econtent_id,$this->getECSObjectType());
529  if($details instanceof ilECSEContentDetails)
530  {
531  $participants = $details->getReceivers();
532  }
533  if($participants)
534  {
535  foreach($participants as $mid)
536  {
537  $receivers[] = $mid;
538  }
539  }
540  }
541  return (array) $receivers;
542  }
543 
549  protected function sendNewContentNotification(ilECSSetting $a_server, $a_econtent_id)
550  {
551  global $ilLog;
552 
553  if(!count($rcps = $a_server->getApprovalRecipients()))
554  {
555  return true;
556  }
557 
558  include_once('./Services/Mail/classes/class.ilMail.php');
559  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
560 
562  $lang->loadLanguageModule('ecs');
563 
564  // @TODO: read mail
565  $mail = new ilMail(self::MAIL_SENDER);
566  $message = $lang->txt('ecs_export_created_body_a')."\n\n";
567  $message .= $lang->txt('title').': '.$this->content_obj->getTitle()."\n";
568  if(strlen($desc = $this->content_obj->getDescription()))
569  {
570  $message .= $lang->txt('desc').': '.$desc."\n";
571  }
572 
573  // Participant info
574  $message .= ("\n".$lang->txt('ecs_published_for'));
575 
576  try
577  {
578  $found = false;
579 
580  $receivers = null;
581  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
582  $details = ilECSEContentDetails::getInstance($a_server->getServerId(),
583  $a_econtent_id, $this->getECSObjectType());
584  if($details instanceof ilECSEContentDetails)
585  {
586  $receivers = $details->getReceivers();
587  }
588  if($receivers)
589  {
590  foreach($receivers as $member)
591  {
592  $found = true;
593 
594  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
595  $part = ilECSCommunityReader::getInstanceByServerId($a_server->getServerId())->getParticipantByMID($member);
596 
597  $message .= ("\n\n".$part->getParticipantName()."\n");
598  $message .= ($part->getDescription());
599  }
600  }
601  if($found)
602  {
603  $message .= "\n\n";
604  }
605  else
606  {
607  $message .= (' '.$lang->txt('ecs_not_published')."\n\n");
608  }
609  }
610  catch(ilECSConnectorException $e)
611  {
612  $ilLog->write(__METHOD__.': Cannot read approvements.');
613  return false;
614  }
615 
616  include_once('./Services/Link/classes/class.ilLink.php');
617  $href = ilLink::_getStaticLink($this->content_obj->getRefId(),'crs',true);
618  $message .= $lang->txt("perma_link").': '.$href."\n\n";
620 
621  $mail->sendMail($a_server->getApprovalRecipientsAsString(),
622  '','',
623  $lang->txt('ecs_new_approval_subject'),
624  $message,array(),array('normal'));
625 
626  return true;
627  }
628 
630  {
631  if($this->content_obj->getType() == 'crs')
632  {
633  $GLOBALS['ilLog']->write(__METHOD__.': Permission update');
634  if($this->content_obj->getType() == 'crs')
635  {
636  $GLOBALS['rbacadmin']->grantPermission(
637  $server->getGlobalRole(),
638  ilRbacReview::_getOperationIdsByName(array('join','visible')),
639  $this->content_obj->getRefId()
640  );
641  }
642  }
643  }
644 
651  protected function getJsonCore($a_etype)
652  {
653  $json = new stdClass();
654  $json->lang = 'en_EN'; // :TODO: obsolet?
655  $json->id = 'il_'.IL_INST_ID.'_'.$this->getContentObject()->getType().'_'.$this->getContentObject()->getId();
656  $json->etype = $a_etype;
657  $json->title = $this->content_obj->getTitle();
658  $json->abstract = $this->content_obj->getLongDescription();
659 
660  include_once('./Services/Link/classes/class.ilLink.php');
661  $json->url = ilLink::_getLink($this->content_obj->getRefId(),$this->content_obj->getType());
662 
663  return $json;
664  }
665 
673  protected function addMetadataToJson(&$a_json, ilECSSetting $a_server, array $a_definition)
674  {
675  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
676  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
677  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
679  $values = ilAdvancedMDValues::_getValuesByObjId($this->content_obj->getId());
680 
681  foreach($a_definition as $id => $type)
682  {
683  if(is_array($type))
684  {
685  $target = $type[1];
686  $type = $type[0];
687  }
688  else
689  {
690  $target = $id;
691  }
692 
693  if($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, $id))
694  {
695  $value = isset($values[$field]) ? $values[$field] : '';
696 
697  switch($type)
698  {
700  $a_json->$target = explode(',', $value);
701  break;
702 
704  $a_json->$target = (int)$value;
705  break;
706 
708  $a_json->$target = (string)$value;
709  break;
710 
712  if(!isset($a_json->$target))
713  {
714  include_once('./Services/WebServices/ECS/classes/class.ilECSTimePlace.php');
715  $a_json->$target = new ilECSTimePlace();
716  }
717  $a_json->$target->{'set'.ucfirst($id)}($value);
718  break;
719  }
720  }
721  }
722  }
723 
730  abstract protected function buildJson(ilECSSetting $a_server);
731 }
732 
733 ?>