ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilRemoteObjectBase.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/Object/classes/class.ilObject2.php";
5 
15 abstract class ilRemoteObjectBase extends ilObject2
16 {
17  protected $local_information;
18  protected $remote_link;
19  protected $organization;
20  protected $mid;
21  protected $auth_hash = '';
22 
23  protected $realm_plain = '';
24 
25  const MAIL_SENDER = 6;
26  const OBJECT_OWNER = 6;
27 
35  public function __construct($a_id = 0,$a_call_by_reference = true)
36  {
37  global $ilDB;
38 
39  parent::__construct($a_id,$a_call_by_reference);
40  $this->db = $ilDB;
41  }
42 
49  public static function getInstanceByEventType($a_type)
50  {
51  switch($a_type)
52  {
54  include_once 'Modules/RemoteCourse/classes/class.ilObjRemoteCourse.php';
55  return new ilObjRemoteCourse();
56 
58  include_once 'Modules/RemoteCategory/classes/class.ilObjRemoteCategory.php';
59  return new ilObjRemoteCategory();
60 
62  include_once 'Modules/RemoteFile/classes/class.ilObjRemoteFile.php';
63  return new ilObjRemoteFile();
64 
66  include_once 'Modules/RemoteGlossary/classes/class.ilObjRemoteGlossary.php';
67  return new ilObjRemoteGlossary();
68 
70  include_once 'Modules/RemoteGroup/classes/class.ilObjRemoteGroup.php';
71  return new ilObjRemoteGroup();
72 
74  include_once 'Modules/RemoteLearningModule/classes/class.ilObjRemoteLearningModule.php';
75  return new ilObjRemoteLearningModule();
76 
78  include_once 'Modules/RemoteWiki/classes/class.ilObjRemoteWiki.php';
79  return new ilObjRemoteWiki();
80 
82  include_once 'Modules/RemoteTest/classes/class.ilObjRemoteTest.php';
83  return new ilObjRemoteTest();
84  }
85  }
86 
87  public function beforeCreate()
88  {
89  $this->setOwner(self::OBJECT_OWNER);
90  return parent::beforeCreate();
91  }
92 
98  abstract protected function getTableName();
99 
105  abstract protected function getECSObjectType();
106 
114  public static function _lookupOrganization($a_obj_id, $a_table)
115  {
116  global $ilDB;
117 
118  $query = "SELECT organization FROM ".$a_table.
119  " WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
120  $res = $ilDB->query($query);
121  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
122  {
123  return $row->organization;
124  }
125  return '';
126  }
127 
132  public function getRealmPlain()
133  {
134  return $this->realm_plain;
135  }
136 
142  public function setOrganization($a_organization)
143  {
144  $this->organization = $a_organization;
145  }
146 
152  public function getOrganization()
153  {
154  return $this->organization;
155  }
156 
162  public function getLocalInformation()
163  {
165  }
166 
172  public function setLocalInformation($a_info)
173  {
174  $this->local_information = $a_info;
175  }
176 
182  public function getMID()
183  {
184  return $this->mid;
185  }
186 
192  public function setMID($a_mid)
193  {
194  $this->mid = $a_mid;
195  }
196 
204  public static function _lookupMID($a_obj_id, $a_table)
205  {
206  global $ilDB;
207 
208  $query = "SELECT mid FROM ".$a_table.
209  " WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
210  $res = $ilDB->query($query);
211  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
212  {
213  return $row->mid;
214  }
215  return 0;
216  }
217 
223  public function setRemoteLink($a_link)
224  {
225  $this->remote_link = $a_link;
226  }
227 
233  public function getRemoteLink()
234  {
235  return $this->remote_link;
236  }
237 
245  public function getFullRemoteLink()
246  {
247  global $ilUser;
248 
249 
250  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
251  $server_id = ilECSImport::lookupServerId($this->getId());
253 
254  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
255  $user = new ilECSUser($ilUser);
256  $ecs_user_data = $user->toGET();
257  $GLOBALS['ilLog']->write(__METHOD__.': Using ecs user data '.$ecs_user_data);
258 
259  // check token mechanism enabled
260  include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
261  $part = new ilECSParticipantSetting($server_id,$this->getMID());
262  if(!$part->isTokenEnabled())
263  {
264  return $this->getRemoteLink();
265  }
266 
267  $auth_hash = $this->createAuthResource($this->getRemoteLink().$user->toREALM());
268  $ecs_url_hash = 'ecs_hash_url='.urlencode($server->getServerURI().'/sys/auths/'.$auth_hash);
269 
270  if(strpos($this->getRemoteLink(), '?'))
271  {
272 
273  $link = $this->getRemoteLink().'&ecs_hash='.$auth_hash.$ecs_user_data.'&'.$ecs_url_hash;
274  }
275  else
276  {
277  $link = $this->getRemoteLink().'?ecs_hash='.$auth_hash.$ecs_user_data.'&'.$ecs_url_hash;
278  }
279  $GLOBALS['ilLog']->write(__METHOD__.': ECS full link: '. $link);
280  return $link;
281  }
282 
289  public function createAuthResource($a_plain_realm)
290  {
291  global $ilLog;
292 
293  include_once './Services/WebServices/ECS/classes/class.ilECSAuth.php';
294  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
295  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
296  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
297 
298  try
299  {
300  $server_id = ilECSImport::lookupServerId($this->getId());
301  $import_info = new ilECSImport($server_id, $this->getId());
302 
303  $connector = new ilECSConnector(ilECSSetting::getInstanceByServerId($server_id));
304  $auth = new ilECSAuth();
305  $auth->setPid($import_info->getMID());
306  // URL is deprecated
307  $auth->setUrl($this->getRemoteLink());
308  $realm = sha1($a_plain_realm);
309  $GLOBALS['ilLog']->write(__METHOD__.': Using realm '.$a_plain_realm);
310  $auth->setRealm($realm);
311  $GLOBALS['ilLog']->write(__METHOD__.' Mid is '.$this->getMID());
312  $this->auth_hash = $connector->addAuth(@json_encode($auth),$this->getMID());
313  return $this->auth_hash;
314  }
315  catch(ilECSConnectorException $exc)
316  {
317  $ilLog->write(__METHOD__.': Caught error from ECS Auth resource: '.$exc->getMessage());
318  return false;
319  }
320  }
321 
325  public function doCreate()
326  {
327  global $ilDB;
328 
329  $fields = array(
330  "obj_id" => array("integer", $this->getId()),
331  "local_information" => array("text", ""),
332  "remote_link" => array("text", ""),
333  "mid" => array("integer", 0),
334  "organization" => array("text", "")
335  );
336 
337  $this->doCreateCustomFields($fields);
338 
339  $ilDB->insert($this->getTableName(), $fields);
340  }
341 
346  protected function doCreateCustomFields(array &$a_fields)
347  {
348 
349  }
350 
354  public function doUpdate()
355  {
356  global $ilDB;
357 
358  $fields = array(
359  "local_information" => array("text", $this->getLocalInformation()),
360  "remote_link" => array("text", $this->getRemoteLink()),
361  "mid" => array("integer", $this->getMID()),
362  "organization" => array("text", $this->getOrganization())
363  );
364 
365  $this->doUpdateCustomFields($fields);
366 
367  $where = array("obj_id" => array("integer", $this->getId()));
368 
369  $ilDB->update($this->getTableName(), $fields, $where);
370  }
371 
376  protected function doUpdateCustomFields(array &$a_fields)
377  {
378 
379  }
380 
384  public function doDelete()
385  {
386  global $ilDB;
387 
388  //put here your module specific stuff
389  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
391 
392  $query = "DELETE FROM ".$this->getTableName().
393  " WHERE obj_id = ".$this->db->quote($this->getId() ,'integer')." ";
394  $ilDB->manipulate($query);
395  }
396 
400  public function doRead()
401  {
402  $query = "SELECT * FROM ".$this->getTableName().
403  " WHERE obj_id = ".$this->db->quote($this->getId() ,'integer')." ";
404  $res = $this->db->query($query);
405  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
406  {
407  $this->setLocalInformation($row->local_information);
408  $this->setRemoteLink($row->remote_link);
409  $this->setMID($row->mid);
410  $this->setOrganization($row->organization);
411 
412  $this->doReadCustomFields($row);
413  }
414  }
415 
420  protected function doReadCustomFields($a_row)
421  {
422 
423  }
424 
432  public function createFromECSEContent(ilECSSetting $a_server, $a_ecs_content, $a_owner)
433  {
434  $this->create();
435 
436  // won't work for personal workspace
437  $this->createReference();
438  $this->setPermissions($a_server->getImportId());
439 
440  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
441  $matchable_content = ilECSUtils::getMatchableContent($this->getECSObjectType(),
442  $a_server->getServerId(), $a_ecs_content, $a_owner);
443 
444  include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMapping.php';
446  $matchable_content));
447 
448  $this->updateFromECSContent($a_server, $a_ecs_content, $a_owner);
449  }
450 
458  public function updateFromECSContent(ilECSSetting $a_server, $a_ecs_content, $a_owner)
459  {
460  global $ilLog;
461 
462  $ilLog->write('updateFromECSContent: '.print_r($a_ecs_content, true));
463 
464  // Get organisation for owner (ObjectListGUI performance)
465  $organisation = null;
466  if($a_owner)
467  {
468  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
469  $organisation = ilECSUtils::lookupParticipantName($a_owner, $a_server->getServerId());
470  $ilLog->write('found organisation: '.$organisation);
471  }
472 
473  $this->setMID($a_owner); // obsolete?
474  $this->setOrganization($organisation);
475  $this->setTitle($a_ecs_content->title);
476  $this->setDescription($a_ecs_content->abstract);
477  $this->setRemoteLink($a_ecs_content->url);
478 
479  $ilLog->write('updateCustomFromECSContent');
480  $this->updateCustomFromECSContent($a_server, $a_ecs_content);
481 
482  // we are updating late so custom values can be set
483 
484  $ilLog->write('ilObject->update()');
485  $this->update();
486 
487  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
488  $matchable_content = ilECSUtils::getMatchableContent($this->getECSObjectType(),
489  $a_server->getServerId(), $a_ecs_content, $a_owner);
490 
491  // rule-based category mapping
492  include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMapping.php';
493  ilECSCategoryMapping::handleUpdate($this->getId(), $a_server->getServerId(),
494  $matchable_content);
495  }
496 
505  protected function importMetadataFromJson($a_json, ilECSSetting $a_server, array $a_definition, $a_mapping_mode)
506  {
507  global $ilLog;
508 
509  $ilLog->write("importing metadata from json: ".print_r($a_json, true));
510 
511  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
512  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
513  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
514 
516  $values_records = ilAdvancedMDValues::getInstancesForObjectId($this->getId(), $this->getType());
517  foreach($values_records as $values_record)
518  {
519  // this correctly binds group and definitions
520  $values_record->read();
521  }
522 
523  $do_save = false;
524 
525  foreach($a_definition as $id => $type)
526  {
527  if(is_array($type))
528  {
529  $target = $type[1];
530  $type = $type[0];
531  }
532  else
533  {
534  $target = $id;
535  }
536 
537  $timePlace = null;
538  if($field = $mappings->getMappingByECSName($a_mapping_mode, $id))
539  {
540  // find element in records
541  $adv_md_def = null;
542  foreach($values_records as $values_record)
543  {
544  $adv_md_defs = $values_record->getDefinitions();
545  if(isset($adv_md_defs[$field]))
546  {
547  $adv_md_def = $adv_md_defs[$field];
548  break;
549  }
550  }
551  if(!$adv_md_def)
552  {
553  continue;
554  }
555 
556  $raw_value = $a_json->$target;
557 
559  {
560  if(!is_object($timePlace))
561  {
562  include_once('./Services/WebServices/ECS/classes/class.ilECSTimePlace.php');
563  if(is_object($raw_value))
564  {
565  $timePlace = new ilECSTimePlace();
566  $timePlace->loadFromJSON($raw_value);
567  }
568  else
569  {
570  $timePlace = new ilECSTimePlace();
571  }
572  }
573  $raw_value = $timePlace;
574  }
575 
576  if($adv_md_def->importFromECS($type, $raw_value, $id))
577  {
578  $do_save = true;
579  }
580  }
581  }
582 
583  if($do_save)
584  {
585  foreach($values_records as $values_record)
586  {
587  $additional = array();
588  foreach($values_record->getADTGroup()->getElements() as $element_id => $element)
589  {
590  if(!$element->isNull())
591  {
592  $additional[$element_id] = array("disabled"=>array("integer", 1));
593  }
594  }
595  $values_record->write($additional);
596  }
597  }
598  }
599 
606  protected function updateCustomFromECSContent(ilECSSetting $a_server, $ecs_content)
607  {
608 
609  }
610 
616  public function isLocalObject()
617  {
618  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
619  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
622  {
623  return false;
624  }
625  return true;
626  }
627 
637  public function handleCreate(ilECSSetting $a_server, $a_econtent_id, array $a_mids)
638  {
639  return $this->handleUpdate($a_server, $a_econtent_id, $a_mids);
640  }
641 
652  public function handleUpdate(ilECSSetting $a_server, $a_econtent_id, array $a_mids)
653  {
654  global $ilLog;
655 
656  // get content details
657  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
658  $details = ilECSEContentDetails::getInstance($a_server->getServerId(),
659  $a_econtent_id, $this->getECSObjectType());
660  if(!$details instanceof ilECSEContentDetails)
661  {
662  $this->handleDelete($a_server, $a_econtent_id);
663  $ilLog->write(__METHOD__.': Handling delete of deprecated remote object. DONE');
664  return;
665  }
666 
667  $ilLog->write(__METHOD__.': Receivers are '. print_r($details->getReceivers(),true));
668  $ilLog->write(__METHOD__.': Senders are '. print_r($details->getSenders(),true));
669 
670  // check owner (sender mid)
671  include_once('./Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php');
672  if(!ilECSParticipantSettings::getInstanceByServerId($a_server->getServerId())->isImportAllowed($details->getSenders()))
673  {
674  $ilLog->write('Ignoring disabled participant. MID: '.$details->getOwner());
675  return true;
676  }
677 
678  // new mids
679  include_once 'Services/WebServices/ECS/classes/class.ilECSImport.php';
680  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
681  foreach(array_intersect($a_mids,$details->getReceivers()) as $mid)
682  {
683  try
684  {
685  $connector = new ilECSConnector($a_server);
686  $res = $connector->getResource($this->getECSObjectType(), $a_econtent_id);
687  if($res->getHTTPCode() == ilECSConnector::HTTP_CODE_NOT_FOUND)
688  {
689  continue;
690  }
691  $json = $res->getResult();
692  $GLOBALS['ilLog']->write(__METHOD__.': Received json: '.print_r($json,true));
693  if(!is_object($json))
694  {
695  // try as array (workaround for invalid content)
696  $json = $json[0];
697  if(!is_object($json))
698  {
699  throw new ilECSConnectorException('invalid json');
700  }
701  }
702  }
703  catch(ilECSConnectorException $exc)
704  {
705  $ilLog->write(__METHOD__ . ': Error parsing result. '.$exc->getMessage());
706  $ilLog->logStack();
707  return false;
708  }
709 
710  // Update existing
711 
712  // Check receiver mid
713  if($obj_id = ilECSImport::_isImported($a_server->getServerId(),$a_econtent_id,$mid))
714  {
715  $ilLog->write(__METHOD__.': Handling update for existing object');
716  $remote = ilObjectFactory::getInstanceByObjId($obj_id,false);
717  if(!$remote instanceof ilRemoteObjectBase)
718  {
719  $ilLog->write(__METHOD__.': Cannot instantiate remote object. Got object type '.$remote->getType());
720  continue;
721  }
722  $remote->updateFromECSContent($a_server,$json,$details->getMySender());
723  }
724  else
725  {
726  $GLOBALS['ilLog']->write(__METHOD__.': my sender '. $details->getMySender().'vs mid'. $mid);
727 
728  $ilLog->write(__METHOD__.': Handling create for non existing object');
729  $this->createFromECSEContent($a_server,$json,$details->getMySender());
730 
731  // update import status
732  $ilLog->write(__METHOD__.': Updating import status');
733  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
734  $import = new ilECSImport($a_server->getServerId(),$this->getId());
735  $import->setEContentId($a_econtent_id);
736  // Store receiver mid
737  $import->setMID($mid);
738  $import->save();
739 
740  $ilLog->write(__METHOD__.': Sending notification');
741  $this->sendNewContentNotification($a_server->getServerId());
742  }
743  }
744 
745  $ilLog->write(__METHOD__.': done');
746  return true;
747  }
748 
752  protected function sendNewContentNotification($a_server_id)
753  {
754  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
755  $settings = ilECSSetting::getInstanceByServerId($a_server_id);
756  if(!count($rcps = $settings->getEContentRecipients()))
757  {
758  return;
759  }
760 
761  include_once('./Services/Mail/classes/class.ilMail.php');
762  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
763 
765  $lang->loadLanguageModule('ecs');
766 
767  $mail = new ilMail(self::MAIL_SENDER);
768  $message = $lang->txt('ecs_'.$this->getType().'_created_body_a')."\n\n";
769  $message .= $lang->txt('title').': '.$this->getTitle()."\n";
770  if(strlen($desc = $this->getDescription()))
771  {
772  $message .= $lang->txt('desc').': '.$desc."\n";
773  }
774 
775  include_once('./Services/Link/classes/class.ilLink.php');
776  $href = ilLink::_getStaticLink($this->getRefId(),$this->getType(),true);
777  $message .= $lang->txt("perma_link").': '.$href."\n\n";
779 
780  $mail->sendMail($settings->getEContentRecipientsAsString(),
781  '','',
782  $lang->txt('ecs_new_econtent_subject'),
783  $message,array(),array('normal'));
784  }
785 
796  public function handleDelete(ilECSSetting $a_server, $a_econtent_id, $a_mid = 0)
797  {
798  global $tree, $ilLog;
799 
800 
801  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
802 
803  // there is no information about the original mid anymore.
804  // Therefor delete any remote objects with given econtent id
805  $obj_ids = ilECSImport::_lookupObjIds($a_server->getServerId(),$a_econtent_id);
806  $ilLog->write(__METHOD__.': Received obj_ids '.print_r($obj_ids,true));
807 
808  foreach($obj_ids as $obj_id)
809  {
810  $references = ilObject::_getAllReferences($obj_id);
811  foreach($references as $ref_id)
812  {
813  if($tmp_obj = ilObjectFactory::getInstanceByRefId($ref_id,false))
814  {
815  $ilLog->write(__METHOD__.': Deleting obsolete remote course: '.$tmp_obj->getTitle());
816  $tmp_obj->delete();
817  $tree->deleteTree($tree->getNodeData($ref_id));
818  }
819  unset($tmp_obj);
820  }
821  }
822  return true;
823  }
824 
832  public function getAllResourceIds(ilECSSetting $a_server, $a_sender_only = false)
833  {
834  global $ilLog;
835 
836  try
837  {
838  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
839  $connector = new ilECSConnector($a_server);
840  $connector->addHeader('X-EcsQueryStrings', $a_sender_only ? 'sender=true' : 'all=true'); // #11301
841  $list = $connector->getResourceList($this->getECSObjectType());
842  if($list instanceof ilECSResult)
843  {
844  return $list->getResult()->getLinkIds();
845  }
846  }
847  catch(ilECSConnectorException $exc)
848  {
849  $ilLog->write(__METHOD__ . ': Error getting resource list. '.$exc->getMessage());
850  }
851  }
852 }
853 ?>