ILIAS  release_4-3 Revision
 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  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
250  $user = new ilECSUser($ilUser);
251  $ecs_user_data = $user->toGET();
252  $GLOBALS['ilLog']->write(__METHOD__.': Using ecs user data '.$ecs_user_data);
253 
254  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
255  $server_id = ilECSImport::lookupServerId($this->getId());
257 
258 
259  $auth_hash = $this->createAuthResource($this->getRemoteLink().$user->toREALM());
260  $ecs_url_hash = 'ecs_hash_url='.urlencode($server->getServerURI().'/sys/auths/'.$auth_hash);
261 
262  if(strpos($this->getRemoteLink(), '?'))
263  {
264 
265  $link = $this->getRemoteLink().'&ecs_hash='.$auth_hash.$ecs_user_data.'&'.$ecs_url_hash;
266  }
267  else
268  {
269  $link = $this->getRemoteLink().'?ecs_hash='.$auth_hash.$ecs_user_data.'&'.$ecs_url_hash;
270  }
271  $GLOBALS['ilLog']->write(__METHOD__.': ECS full link: '. $link);
272  return $link;
273  }
274 
281  public function createAuthResource($a_plain_realm)
282  {
283  global $ilLog;
284 
285  include_once './Services/WebServices/ECS/classes/class.ilECSAuth.php';
286  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
287  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
288  include_once './Services/WebServices/ECS/classes/class.ilECSSetting.php';
289 
290  try
291  {
292  $server_id = ilECSImport::lookupServerId($this->getId());
293 
294  $connector = new ilECSConnector(ilECSSetting::getInstanceByServerId($server_id));
295  $auth = new ilECSAuth();
296  // URL is deprecated
297  $auth->setUrl($this->getRemoteLink());
298  $realm = sha1($a_plain_realm);
299  $GLOBALS['ilLog']->write(__METHOD__.': Using realm '.$a_plain_realm);
300  $auth->setRealm($realm);
301  $GLOBALS['ilLog']->write(__METHOD__.' Mid is '.$this->getMID());
302  $this->auth_hash = $connector->addAuth(@json_encode($auth),$this->getMID());
303  return $this->auth_hash;
304  }
305  catch(ilECSConnectorException $exc)
306  {
307  $ilLog->write(__METHOD__.': Caught error from ECS Auth resource: '.$exc->getMessage());
308  return false;
309  }
310  }
311 
315  public function doCreate()
316  {
317  global $ilDB;
318 
319  $fields = array(
320  "obj_id" => array("integer", $this->getId()),
321  "local_information" => array("text", ""),
322  "remote_link" => array("text", ""),
323  "mid" => array("integer", 0),
324  "organization" => array("text", "")
325  );
326 
327  $this->doCreateCustomFields($fields);
328 
329  $ilDB->insert($this->getTableName(), $fields);
330  }
331 
336  protected function doCreateCustomFields(array &$a_fields)
337  {
338 
339  }
340 
344  public function doUpdate()
345  {
346  global $ilDB;
347 
348  $fields = array(
349  "local_information" => array("text", $this->getLocalInformation()),
350  "remote_link" => array("text", $this->getRemoteLink()),
351  "mid" => array("integer", $this->getMID()),
352  "organization" => array("text", $this->getOrganization())
353  );
354 
355  $this->doUpdateCustomFields($fields);
356 
357  $where = array("obj_id" => array("integer", $this->getId()));
358 
359  $ilDB->update($this->getTableName(), $fields, $where);
360  }
361 
366  protected function doUpdateCustomFields(array &$a_fields)
367  {
368 
369  }
370 
374  public function doDelete()
375  {
376  global $ilDB;
377 
378  //put here your module specific stuff
379  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
381 
382  $query = "DELETE FROM ".$this->getTableName().
383  " WHERE obj_id = ".$this->db->quote($this->getId() ,'integer')." ";
384  $ilDB->manipulate($query);
385  }
386 
390  public function doRead()
391  {
392  $query = "SELECT * FROM ".$this->getTableName().
393  " WHERE obj_id = ".$this->db->quote($this->getId() ,'integer')." ";
394  $res = $this->db->query($query);
395  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
396  {
397  $this->setLocalInformation($row->local_information);
398  $this->setRemoteLink($row->remote_link);
399  $this->setMID($row->mid);
400  $this->setOrganization($row->organization);
401 
402  $this->doReadCustomFields($row);
403  }
404  }
405 
410  protected function doReadCustomFields($a_row)
411  {
412 
413  }
414 
422  public function createFromECSEContent(ilECSSetting $a_server, $a_ecs_content, $a_owner)
423  {
424  $this->create();
425 
426  // won't work for personal workspace
427  $this->createReference();
428  $this->setPermissions($a_server->getImportId());
429 
430  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
431  $matchable_content = ilECSUtils::getMatchableContent($this->getECSObjectType(),
432  $a_server->getServerId(), $a_ecs_content, $a_owner);
433 
434  include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMapping.php';
436  $matchable_content));
437 
438  $this->updateFromECSContent($a_server, $a_ecs_content, $a_owner);
439  }
440 
448  public function updateFromECSContent(ilECSSetting $a_server, $a_ecs_content, $a_owner)
449  {
450  global $ilLog;
451 
452  $ilLog->write('updateFromECSContent: '.print_r($a_ecs_content, true));
453 
454  // Get organisation for owner (ObjectListGUI performance)
455  $organisation = null;
456  if($a_owner)
457  {
458  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
459  $organisation = ilECSUtils::lookupParticipantName($a_owner, $a_server->getServerId());
460  $ilLog->write('found organisation: '.$organisation);
461  }
462 
463  $this->setMID($a_owner); // obsolete?
464  $this->setOrganization($organisation);
465  $this->setTitle($a_ecs_content->title);
466  $this->setDescription($a_ecs_content->abstract);
467  $this->setRemoteLink($a_ecs_content->url);
468 
469  $ilLog->write('updateCustomFromECSContent');
470  $this->updateCustomFromECSContent($a_server, $a_ecs_content);
471 
472  // we are updating late so custom values can be set
473 
474  $ilLog->write('ilObject->update()');
475  $this->update();
476 
477  include_once './Services/WebServices/ECS/classes/class.ilECSUtils.php';
478  $matchable_content = ilECSUtils::getMatchableContent($this->getECSObjectType(),
479  $a_server->getServerId(), $a_ecs_content, $a_owner);
480 
481  // rule-based category mapping
482  include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMapping.php';
483  ilECSCategoryMapping::handleUpdate($this->getId(), $a_server->getServerId(),
484  $matchable_content);
485  }
486 
495  protected function importMetadataFromJson($a_json, ilECSSetting $a_server, array $a_definition, $a_mapping_mode)
496  {
497  global $ilLog;
498 
499  $ilLog->write("importing metadata from json: ".print_r($a_definition, true));
500 
501  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
502  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
503  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
505 
506  foreach($a_definition as $id => $type)
507  {
508  if(is_array($type))
509  {
510  $target = $type[1];
511  $type = $type[0];
512  }
513  else
514  {
515  $target = $id;
516  }
517 
518  $timePlace = null;
519  if($field = $mappings->getMappingByECSName($a_mapping_mode, $id))
520  {
521  switch($type)
522  {
524  $value = implode(',', (array) $a_json->$target);
525  break;
526 
528  $value = (int) $a_json->$target;
529  break;
530 
532  $value = (string) $a_json->$target;
533  break;
534 
536  if(!is_object($timePlace))
537  {
538  include_once('./Services/WebServices/ECS/classes/class.ilECSTimePlace.php');
539  if(is_object($a_json->$target))
540  {
541  $timePlace = new ilECSTimePlace();
542  $timePlace->loadFromJSON($a_json->$target);
543  }
544  else
545  {
546  $timePlace = new ilECSTimePlace();
547  }
548  }
549  switch($id)
550  {
551  case 'begin':
552  case 'end':
553  $field_type = ilAdvancedMDFieldDefinition::_lookupFieldType($field);
554  if($field_type == ilAdvancedMDFieldDefinition::TYPE_DATE ||
556  {
557  $value = $timePlace->{'getUT'.ucfirst($id)}();
558  break;
559  }
560  // fallthrough
561  case 'room':
562  case 'cycle':
563  $value = $timePlace->{'get'.ucfirst($id)}();
564  break;
565  }
566  break;
567  }
568  include_once './Services/AdvancedMetaData/classes/class.ilAdvancedMDValue.php';
569  $mdv = ilAdvancedMDValue::_getInstance($this->getId(),$field);
570  $mdv->toggleDisabledStatus(true);
571  $mdv->setValue($value);
572  $mdv->save();
573  }
574  }
575  }
576 
583  protected function updateCustomFromECSContent(ilECSSetting $a_server, $ecs_content)
584  {
585 
586  }
587 
593  public function isLocalObject()
594  {
595  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
596  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
598  ilECSImport::_lookupEContentId($this->getId())))
599  {
600  return false;
601  }
602  return true;
603  }
604 
614  public function handleCreate(ilECSSetting $a_server, $a_econtent_id, array $a_mids)
615  {
616  return $this->handleUpdate($a_server, $a_econtent_id, $a_mids);
617  }
618 
629  public function handleUpdate(ilECSSetting $a_server, $a_econtent_id, array $a_mids)
630  {
631  global $ilLog;
632 
633  // get content details
634  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentDetails.php');
635  $details = ilECSEContentDetails::getInstance($a_server->getServerId(),
636  $a_econtent_id, $this->getECSObjectType());
637  if(!$details instanceof ilECSEContentDetails)
638  {
639  $this->handleDelete($a_server, $a_econtent_id);
640  $ilLog->write(__METHOD__.': Handling delete of deprecated remote object. DONE');
641  return;
642  }
643 
644  $ilLog->write(__METHOD__.': Receivers are '. print_r($details->getReceivers(),true));
645  $ilLog->write(__METHOD__.': Senders are '. print_r($details->getSenders(),true));
646 
647  // check owner (sender mid)
648  include_once('./Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php');
649  if(!ilECSParticipantSettings::getInstanceByServerId($a_server->getServerId())->isImportAllowed($details->getSenders()))
650  {
651  $ilLog->write('Ignoring disabled participant. MID: '.$details->getOwner());
652  return true;
653  }
654 
655  // new mids
656  include_once 'Services/WebServices/ECS/classes/class.ilECSImport.php';
657  include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
658  foreach(array_intersect($a_mids,$details->getReceivers()) as $mid)
659  {
660  try
661  {
662  $connector = new ilECSConnector($a_server);
663  $res = $connector->getResource($this->getECSObjectType(), $a_econtent_id);
664  if($res->getHTTPCode() == ilECSConnector::HTTP_CODE_NOT_FOUND)
665  {
666  continue;
667  }
668  $json = $res->getResult();
669  $GLOBALS['ilLog']->write(__METHOD__.': Received json: '.print_r($json,true));
670  if(!is_object($json))
671  {
672  // try as array (workaround for invalid content)
673  $json = $json[0];
674  if(!is_object($json))
675  {
676  throw new ilECSConnectorException('invalid json');
677  }
678  }
679  }
680  catch(ilECSConnectorException $exc)
681  {
682  $ilLog->write(__METHOD__ . ': Error parsing result. '.$exc->getMessage());
683  $ilLog->logStack();
684  return false;
685  }
686 
687  // Update existing
688 
689  // Check receiver mid
690  if($obj_id = ilECSImport::_isImported($a_server->getServerId(),$a_econtent_id,$mid))
691  {
692  $ilLog->write(__METHOD__.': Handling update for existing object');
693  $remote = ilObjectFactory::getInstanceByObjId($obj_id,false);
694  if(!$remote instanceof ilRemoteObjectBase)
695  {
696  $ilLog->write(__METHOD__.': Cannot instantiate remote object. Got object type '.$remote->getType());
697  continue;
698  }
699  $remote->updateFromECSContent($a_server,$json,$details->getMySender());
700  }
701  else
702  {
703  $GLOBALS['ilLog']->write(__METHOD__.': my sender '. $details->getMySender().'vs mid'. $mid);
704 
705  $ilLog->write(__METHOD__.': Handling create for non existing object');
706  $this->createFromECSEContent($a_server,$json,$details->getMySender());
707 
708  // update import status
709  $ilLog->write(__METHOD__.': Updating import status');
710  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
711  $import = new ilECSImport($a_server->getServerId(),$this->getId());
712  $import->setEContentId($a_econtent_id);
713  // Store receiver mid
714  $import->setMID($mid);
715  $import->save();
716 
717  $ilLog->write(__METHOD__.': Sending notification');
718  $this->sendNewContentNotification($a_server->getServerId());
719  }
720  }
721 
722  $ilLog->write(__METHOD__.': done');
723  return true;
724  }
725 
729  protected function sendNewContentNotification($a_server_id)
730  {
731  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
732  $settings = ilECSSetting::getInstanceByServerId($a_server_id);
733  if(!count($rcps = $settings->getEContentRecipients()))
734  {
735  return;
736  }
737 
738  include_once('./Services/Mail/classes/class.ilMail.php');
739  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
740 
742  $lang->loadLanguageModule('ecs');
743 
744  $mail = new ilMail(self::MAIL_SENDER);
745  $message = $lang->txt('ecs_'.$this->getType().'_created_body_a')."\n\n";
746  if(strlen($this->getOrganization()))
747  {
748  $message .= $lang->txt('organization').': '.$this->getOrganization()."\n";
749  }
750  $message .= $lang->txt('title').': '.$this->getTitle()."\n";
751  if(strlen($desc = $this->getDescription()))
752  {
753  $message .= $lang->txt('desc').': '.$desc."\n";
754  }
755 
756  include_once('./Services/Link/classes/class.ilLink.php');
757  $href = ilLink::_getStaticLink($this->getRefId(),$this->getType(),true);
758  $message .= $lang->txt("perma_link").': '.$href."\n\n";
760 
761  $mail->sendMail($settings->getEContentRecipientsAsString(),
762  '','',
763  $lang->txt('ecs_new_econtent_subject'),
764  $message,array(),array('normal'));
765  }
766 
777  public function handleDelete(ilECSSetting $a_server, $a_econtent_id, $a_mid = 0)
778  {
779  global $tree, $ilLog;
780 
781 
782  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
783 
784  // there is no information about the original mid anymore.
785  // Therefor delete any remote objects with given econtent id
786  $obj_ids = ilECSImport::_lookupObjIds($a_server->getServerId(),$a_econtent_id);
787  $ilLog->write(__METHOD__.': Received obj_ids '.print_r($obj_ids,true));
788 
789  foreach($obj_ids as $obj_id)
790  {
791  $references = ilObject::_getAllReferences($obj_id);
792  foreach($references as $ref_id)
793  {
794  if($tmp_obj = ilObjectFactory::getInstanceByRefId($ref_id,false))
795  {
796  $ilLog->write(__METHOD__.': Deleting obsolete remote course: '.$tmp_obj->getTitle());
797  $tmp_obj->delete();
798  $tree->deleteTree($tree->getNodeData($ref_id));
799  }
800  unset($tmp_obj);
801  }
802  }
803  return true;
804  }
805 
813  public function getAllResourceIds(ilECSSetting $a_server, $a_sender_only = false)
814  {
815  global $ilLog;
816 
817  try
818  {
819  include_once './Services/WebServices/ECS/classes/class.ilECSConnector.php';
820  $connector = new ilECSConnector($a_server);
821  $connector->addHeader('X-EcsQueryStrings', $a_sender_only ? 'sender=true' : 'all=true'); // #11301
822  $list = $connector->getResourceList($this->getECSObjectType());
823  if($list instanceof ilECSResult)
824  {
825  return $list->getResult()->getLinkIds();
826  }
827  }
828  catch(ilECSConnectorException $exc)
829  {
830  $ilLog->write(__METHOD__ . ': Error getting resource list. '.$exc->getMessage());
831  }
832  }
833 }
834 ?>