/**
  * Get single directory tree
  * @return array an array of ecs cms directory tree entries
  */
 public function getCourse($course_id, $a_details = false)
 {
     $this->path_postfix = '/campusconnect/courses/' . (int) $course_id;
     if ($a_details and $course_id) {
         $this->path_postfix .= '/details';
     }
     try {
         $this->prepareConnection();
         $this->setHeader(array());
         if ($a_details) {
             $this->addHeader('Accept', 'application/json');
         } else {
             $this->addHeader('Accept', 'text/uri-list');
         }
         $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
         $res = $this->call();
         if (substr($res, 0, 4) == 'http') {
             $json = file_get_contents($res);
             $ecs_result = new ilECSResult($json);
         } else {
             $ecs_result = new ilECSResult($res);
         }
         // Return ECSEContentDetails for details switch
         if ($a_details) {
             include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
             $details = new ilECSEContentDetails();
             $details->loadFromJson($ecs_result->getResult());
             return $details;
         }
         // Return json result
         return $ecs_result->getResult();
     } catch (ilCurlConnectionException $e) {
         throw new ilECSConnectorException('Error calling ECS service: ' . $e->getMessage());
     }
 }
 /**
  * get auth resource
  *
  * @access public
  * @param auth hash (transfered via GET)
  * @throws ilECSConnectorException 
  */
 public function getAuth($a_hash, $a_details_only = FALSE)
 {
     global $ilLog;
     if (!strlen($a_hash)) {
         $ilLog->write(__METHOD__ . ': No auth hash given. Aborting.');
         throw new ilECSConnectorException('No auth hash given.');
     }
     $this->path_postfix = '/sys/auths/' . $a_hash;
     if ($a_details_only) {
         $this->path_postfix .= '/details';
     }
     try {
         $this->prepareConnection();
         $res = $this->call();
         $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
         $ilLog->write(__METHOD__ . ': Checking HTTP status...');
         if ($info != self::HTTP_CODE_OK) {
             $ilLog->write(__METHOD__ . ': Cannot get auth resource, did not receive HTTP 200. ');
             throw new ilECSConnectorException('Received HTTP status code: ' . $info);
         }
         $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
         $ecs_result = new ilECSResult($res);
         // Return ECSEContentDetails for details switch
         if ($a_details_only) {
             include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
             $details = new ilECSEContentDetails();
             $details->loadFromJson($ecs_result->getResult());
             return $details;
         }
         return $ecs_result;
     } catch (ilCurlConnectionException $exc) {
         throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
     }
 }
 /**
  * Handle update event
  * 
  * called by ilTaskScheduler
  * 
  * @param ilECSSetting $a_server
  * @param int $a_econtent_id
  * @param array $a_mids
  * @return boolean
  */
 public function handleUpdate(ilECSSetting $a_server, $a_econtent_id, array $a_mids)
 {
     global $ilLog;
     // get content details
     include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
     $details = ilECSEContentDetails::getInstance($a_server->getServerId(), $a_econtent_id, $this->getECSObjectType());
     if (!$details instanceof ilECSEContentDetails) {
         $this->handleDelete($a_server, $a_econtent_id);
         $ilLog->write(__METHOD__ . ': Handling delete of deprecated remote object. DONE');
         return;
     }
     $ilLog->write(__METHOD__ . ': Receivers are ' . print_r($details->getReceivers(), true));
     $ilLog->write(__METHOD__ . ': Senders are ' . print_r($details->getSenders(), true));
     // check owner (sender mid)
     include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSettings.php';
     if (!ilECSParticipantSettings::getInstanceByServerId($a_server->getServerId())->isImportAllowed($details->getSenders())) {
         $ilLog->write('Ignoring disabled participant. MID: ' . $details->getOwner());
         return true;
     }
     // new mids
     include_once 'Services/WebServices/ECS/classes/class.ilECSImport.php';
     include_once 'Services/WebServices/ECS/classes/class.ilECSConnector.php';
     foreach (array_intersect($a_mids, $details->getReceivers()) as $mid) {
         try {
             $connector = new ilECSConnector($a_server);
             $res = $connector->getResource($this->getECSObjectType(), $a_econtent_id);
             if ($res->getHTTPCode() == ilECSConnector::HTTP_CODE_NOT_FOUND) {
                 continue;
             }
             $json = $res->getResult();
             $GLOBALS['ilLog']->write(__METHOD__ . ': Received json: ' . print_r($json, true));
             if (!is_object($json)) {
                 // try as array (workaround for invalid content)
                 $json = $json[0];
                 if (!is_object($json)) {
                     throw new ilECSConnectorException('invalid json');
                 }
             }
         } catch (ilECSConnectorException $exc) {
             $ilLog->write(__METHOD__ . ': Error parsing result. ' . $exc->getMessage());
             $ilLog->logStack();
             return false;
         }
         // Update existing
         // Check receiver mid
         if ($obj_id = ilECSImport::_isImported($a_server->getServerId(), $a_econtent_id, $mid)) {
             $ilLog->write(__METHOD__ . ': Handling update for existing object');
             $remote = ilObjectFactory::getInstanceByObjId($obj_id, false);
             if (!$remote instanceof ilRemoteObjectBase) {
                 $ilLog->write(__METHOD__ . ': Cannot instantiate remote object. Got object type ' . $remote->getType());
                 continue;
             }
             $remote->updateFromECSContent($a_server, $json, $details->getMySender());
         } else {
             $GLOBALS['ilLog']->write(__METHOD__ . ': my sender ' . $details->getMySender() . 'vs mid' . $mid);
             $ilLog->write(__METHOD__ . ': Handling create for non existing object');
             $this->createFromECSEContent($a_server, $json, $details->getMySender());
             // update import status
             $ilLog->write(__METHOD__ . ': Updating import status');
             include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
             $import = new ilECSImport($a_server->getServerId(), $this->getId());
             $import->setEContentId($a_econtent_id);
             // Store receiver mid
             $import->setMID($mid);
             $import->save();
             $ilLog->write(__METHOD__ . ': Sending notification');
             $this->sendNewContentNotification($a_server->getServerId());
         }
     }
     $ilLog->write(__METHOD__ . ': done');
     return true;
 }
 /**
  * send notifications about new EContent
  *
  * @return bool
  */
 protected function sendNewContentNotification(ilECSSetting $a_server, $a_econtent_id)
 {
     global $ilLog;
     if (!count($rcps = $a_server->getApprovalRecipients())) {
         return true;
     }
     include_once './Services/Mail/classes/class.ilMail.php';
     include_once './Services/Language/classes/class.ilLanguageFactory.php';
     $lang = ilLanguageFactory::_getLanguage();
     $lang->loadLanguageModule('ecs');
     // @TODO: read mail
     $mail = new ilMail(self::MAIL_SENDER);
     $message = $lang->txt('ecs_export_created_body_a') . "\n\n";
     $message .= $lang->txt('title') . ': ' . $this->content_obj->getTitle() . "\n";
     if (strlen($desc = $this->content_obj->getDescription())) {
         $message .= $lang->txt('desc') . ': ' . $desc . "\n";
     }
     // Participant info
     $message .= "\n" . $lang->txt('ecs_published_for');
     try {
         $found = false;
         $receivers = null;
         include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
         $details = ilECSEContentDetails::getInstance($a_server->getServerId(), $a_econtent_id, $this->getECSObjectType());
         if ($details instanceof ilECSEContentDetails) {
             $receivers = $details->getReceivers();
         }
         if ($receivers) {
             foreach ($receivers as $member) {
                 $found = true;
                 include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
                 $part = ilECSCommunityReader::getInstanceByServerId($a_server->getServerId())->getParticipantByMID($member);
                 $message .= "\n\n" . $part->getParticipantName() . "\n";
                 $message .= $part->getDescription();
             }
         }
         if ($found) {
             $message .= "\n\n";
         } else {
             $message .= ' ' . $lang->txt('ecs_not_published') . "\n\n";
         }
     } catch (ilECSConnectorException $e) {
         $ilLog->write(__METHOD__ . ': Cannot read approvements.');
         return false;
     }
     include_once './Services/Link/classes/class.ilLink.php';
     $href = ilLink::_getStaticLink($this->content_obj->getRefId(), 'crs', true);
     $message .= $lang->txt("perma_link") . ': ' . $href . "\n\n";
     $message .= ilMail::_getAutoGeneratedMessageString();
     $mail->sendMail($a_server->getApprovalRecipientsAsString(), '', '', $lang->txt('ecs_new_approval_subject'), $message, array(), array('normal'));
     return true;
 }