ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSEContentReader.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23  */
24 
34 {
35  const ECS_QUERY_STRINGS = 'X-EcsQueryStrings';
36  const ECS_HEADER_SENDER = 'X-EcsSender';
37  const RECEIVER_ONLY = 'receiver=true';
38  const SENDER_ONLY = 'sender=true';
39  const ALL_ECONTENT = 'all=true';
40 
41  protected $log;
42  protected $settings = null;
43  protected $connector = null;
44  protected $econtent = array();
45  protected $econtent_details = array();
46  protected $econtent_id = 0;
47 
48 
49 
56  public function __construct($a_server_id,$a_econtent_id = 0)
57  {
58  global $ilLog;
59 
60  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
61  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
62  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
63  include_once('Services/WebServices/ECS/classes/class.ilECSEContent.php');
64  include_once('Services/WebServices/ECS/classes/class.ilECSReaderException.php');
65 
66  $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
67  $this->connector = new ilECSConnector($this->settings);
68  $this->log = $ilLog;
69 
70  $this->econtent_id = $a_econtent_id;
71  }
72 
79  public function getEContent()
80  {
81  return $this->econtent ? $this->econtent : array();
82  }
83 
88  public function getEContentDetails()
89  {
90  return $this->econtent_details ? $this->econtent_details : array();
91  }
92 
99  public function readResourceList($a_filter = null)
100  {
101  global $ilLog;
102 
103  try {
104  $this->connector->addHeader(self::ECS_QUERY_STRINGS, $a_filter ? $a_filter : self::ALL_ECONTENT);
105  $res = $this->connector->getResourceList();
106  return $res->getResult();
107  }
108  catch(ilECSConnectorException $e)
109  {
110  $ilLog->write(__METHOD__ . ': Error connecting to ECS server. ' . $e->getMessage());
111  throw $e;
112  }
113  catch(ilECSReaderException $e)
114  {
115  $ilLog->write(__METHOD__ . ': Error reading course links. ' . $e->getMessage());
116  throw $e;
117  }
118  }
119 
127  public function read($details_only = false)
128  {
129  global $ilLog;
130 
131  if($details_only and $this->getEContentDetails() instanceof ilECSEContentDetails)
132  {
133  return true;
134  }
135 
136  try
137  {
138  $res = $this->connector->getResource($this->econtent_id,$details_only);
139 
140  if($res->getHTTPCode() == ilECSConnector::HTTP_CODE_NOT_FOUND)
141  {
142  return false;
143  }
144  if(!is_object($res->getResult()))
145  {
146  $ilLog->write(__METHOD__ . ': Error parsing result. Expected result of type array.');
147  $ilLog->logStack();
148  throw new ilECSReaderException('Error parsing query');
149  }
150 
151  if($details_only)
152  {
153  include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
154  $tmp_content = new ilECSEContentDetails();
155  $tmp_content->loadFromJSON($res->getResult());
156  $this->econtent_details = $tmp_content;
157  }
158  else
159  {
160  $tmp_content = new ilECSEContent($this->econtent_id);
161  $tmp_content->loadFromJSON($res->getResult());
162 
163  $sender = $this->parseSenderHeader($res);
164  if($sender)
165  {
166  $tmp_content->setOwner($sender);
167  $GLOBALS['ilLog']->write(__METHOD__.': Reading sender from header: '.$sender);
168  }
169  else
170  {
171  $details = $this->read(true);
172  $details = $this->getEContentDetails();
173  $GLOBALS['ilLog']->write(__METHOD__.': Reading sender from details: '.$details->getFirstSender());
174  $tmp_content->setOwner($details->getFirstSender());
175  }
176  $this->econtent = $tmp_content;
177  }
178  return true;
179  }
180  catch(ilECSConnectorException $e)
181  {
182  $ilLog->write(__METHOD__ . ': Error connecting to ECS server. ' . $e->getMessage());
183  throw $e;
184  }
185  catch(ilECSReaderException $e)
186  {
187  $ilLog->write(__METHOD__ . ': Error reading EContent. ' . $e->getMessage());
188  throw $e;
189  }
190  return false;
191  }
192 
199  {
200  #$GLOBALS['ilLog']->write(__METHOD__.' Reading sender from header');
201  foreach($res->getHeaders() as $key => $value)
202  {
203  #$GLOBALS['ilLog']->write(__METHOD__.' Key is: '.$key);
204  if($key == self::ECS_HEADER_SENDER)
205  {
206  #$GLOBALS['ilLog']->write(__METHOD__.' Value is: '.$value);
207  $sender = trim($value);
208  $value_arr = explode(',', $value);
209  foreach((array) $value_arr as $s)
210  {
211  return (int) trim($s);
212  }
213  }
214  }
215  return 0;
216  }
217 
218 }
219 ?>