ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilECSEventQueueReader.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
35 {
36  const TYPE_ECONTENT = 'econtents';
37  const TYPE_EXPORTED = 'exported';
38 
39  const OPERATION_DELETE = 'delete';
40  const OPERATION_UPDATE = 'update';
41  const OPERATION_CREATE = 'create';
42  const OPERATION_NEWLY_CREATED = 'newly-created';
43 
44  const ADMIN_RESET = 'reset';
45  const ADMIN_RESET_ALL = 'reset_all';
46 
47  protected $log;
48  protected $db;
49 
50  protected $events = array();
51  protected $econtent_ids = array();
52 
58  public function __construct()
59  {
60  global $ilLog,$ilDB;
61 
62  include_once('Services/WebServices/ECS/classes/class.ilECSSettings.php');
63  include_once('Services/WebServices/ECS/classes/class.ilECSReaderException.php');
64 
65  $this->settings = ilECSSettings::_getInstance();
66  $this->log = $ilLog;
67  $this->db = $ilDB;
68 
69  $this->read();
70  }
71 
78  public static function handleImportReset()
79  {
80  global $ilLog;
81 
82  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
83  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
84 
85  try
86  {
87  include_once('./Services/WebServices/ECS/classes/class.ilECSEContentReader.php');
88  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
89  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
90  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
91 
92  $event_queue = new ilECSEventQueueReader();
93  $event_queue->deleteAllEContentEvents();
94 
96  $reader->read();
97  $all_content = $reader->getEContent();
98 
99 
100  $imported = ilECSImport::_getAllImportedLinks();
101  $exported = ilECSExport::_getAllEContentIds();
102 
103  // read update events
104  foreach($all_content as $content)
105  {
106  // Ask if this is desired
107  if(!isset($imported[$content->getEContentId()]) and 0)
108  {
109  $event_queue->add(
111  $content->getEContentId(),
113  );
114  }
115  else
116  {
117  $event_queue->add(
119  $content->getEContentId(),
121  );
122  }
123 
124  if(isset($imported[$content->getEContentId()]))
125  {
126  unset($imported[$content->getEContentId()]);
127  }
128  if(isset($exported[$content->getEContentId()]))
129  {
130  unset($exported[$content->getEContentId()]);
131  }
132 
133  }
134  // read delete events
135  if(is_array($imported))
136  {
137  foreach($imported as $econtent_id => $null)
138  {
139  $event_queue->add(ilECSEventQueueReader::TYPE_ECONTENT,
140  $econtent_id,
142 
143  }
144  }
145  // delete all deprecated export information
146  if(is_array($exported))
147  {
149  }
150  }
151  catch(ilECSConnectorException $e1)
152  {
153  $ilLog->write('Cannot connect to ECS server: '.$e1->getMessage());
154  return false;
155  }
156  catch(ilException $e2)
157  {
158  $ilLog->write('Update failed: '.$e1->getMessage());
159  return false;
160  }
161  return true;
162  }
163 
171  public static function handleExportReset()
172  {
173  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
174 
175  $queue = new ilECSEventQueueReader();
176  $queue->deleteAllExportedEvents();
177 
178  foreach(ilECSExport::_getExportedIDs() as $obj_id)
179  {
180  $queue->add(self::TYPE_EXPORTED,$obj_id,self::OPERATION_NEWLY_CREATED);
181  }
182  return true;
183  }
184 
185 
186 
193  public function getEvents()
194  {
195  return $this->events ? $this->events : array();
196  }
197 
203  public function deleteAll()
204  {
205  $query = "DELETE FROM ecs_events";
206  $this->db->query($query);
207  return true;
208  }
209 
215  public function deleteAllEContentEvents()
216  {
217  $query = "DELETE FROM ecs_events ".
218  "WHERE type = ".$this->db->quote(self::TYPE_ECONTENT);
219  $this->db->query($query);
220  return true;
221  }
222 
228  public function deleteAllExportedEvents()
229  {
230  $query = "DELETE FROM ecs_events ".
231  "WHERE type = ".$this->db->quote(self::TYPE_EXPORTED);
232  $this->db->query($query);
233  return true;
234  }
235 
236 
244  public function refresh()
245  {
246  global $ilLog;
247 
248  try
249  {
250  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
251  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
252 
253  $connector = new ilECSConnector();
254  $res = $connector->getEventQueues();
255 
256  if(!is_array($res->getResult()))
257  {
258  $ilLog->write(__METHOD__.': No new events found.');
259  return true;
260  }
261  $this->log->write(__METHOD__.': Found '.count($res->getResult()).' new events.');
262  foreach($res->getResult() as $event)
263  {
264  // Handle command queue
265  if(isset($event->cmd) and is_object($event->cmd))
266  {
267  if(!isset($event->cmd->admin) and !is_object($event->cmd->admin))
268  {
269  throw new ilECSReaderException('Received invalid command queue structure. Property "admin" is missing');
270  }
271  $admin_cmd = $event->cmd->admin;
272  $this->log->write(__METHOD__.': Received new Commandqueue command: '.$admin_cmd);
273  switch($admin_cmd)
274  {
275  case self::ADMIN_RESET:
277  break;
278  case self::ADMIN_RESET_ALL:
281  break;
282  }
283  }
284  // Handle econtents events
285  if(isset($event->econtents) and is_object($event->econtents))
286  {
287  $operation = $event->econtents->op;
288 
289  if(!in_array($event->econtents->eid,$this->econtent_ids))
290  {
291  // It is not necessary to store multiple entries with the same econtent_id.
292  // since we always have to receive and parse the econtent from the ecs server.
293  $this->add('econtents',$event->econtents->eid,$event->econtents->op);
294  $this->log->write(__METHOD__.': Added new entry for EContentId: '.$event->econtents->eid);
295  }
296  elseif($operation == self::OPERATION_DELETE)
297  {
298  $this->log->write(__METHOD__.': Updating delete operation for EContentId: '.$event->econtents->eid);
299  $this->update('econtents',$event->econtents->eid,$event->econtents->op);
300  }
301  else
302  {
303  // update with last operation
304  $this->log->write(__METHOD__.': Ignoring multiple operations for EContentId: '.$event->econtents->eid);
305  }
306 
307  }
308  }
309  $this->read();
310  }
311  catch(ilECSConnectorException $e)
312  {
313  $ilLog->write(__METHOD__.': Error connecting to ECS server. '.$e->getMessage());
314  throw $e;
315  }
316  catch(ilECSReaderException $e)
317  {
318  $ilLog->write(__METHOD__.': Error reading EventQueue. '.$e->getMessage());
319  throw $e;
320  }
321  }
322 
329  public function shift()
330  {
331  $event = array_shift($this->events);
332  if($event == null)
333  {
334  return array();
335  }
336  else
337  {
338  $this->delete($event['event_id']);
339  return $event;
340  }
341  }
342 
343 
349  public function add($a_type,$a_id,$a_op)
350  {
351  $query = "INSERT INTO ecs_events ".
352  "SET type = ".$this->db->quote($a_type).", ".
353  "id = ".$this->db->quote($a_id).", ".
354  "op = ".$this->db->quote($a_op)." ";
355  $res = $this->db->query($query);
356 
357  $new_event['event_id'] = $this->db->getLastInsertId();
358  $new_event['type'] = $a_type;
359  $new_event['id'] = $a_id;
360  $new_event['op'] = $a_op;
361 
362  $this->events[] = $new_event;
363  $this->econtent_ids[$a_id] = $a_id;
364  return true;
365  }
366 
373  private function update($a_type,$a_id,$a_operation)
374  {
375  $query = "UPDATE ecs_events ".
376  "SET op = ".$this->db->quote($a_operation)." ".
377  "WHERE type = ".$this->db->quote($a_type)." ".
378  "AND id = ".$this->db->quote($a_id)." ";
379  $this->db->query($query);
380  }
381 
388  private function delete($a_event_id)
389  {
390  $query = "DELETE FROM ecs_events ".
391  "WHERE event_id = ".$this->db->quote($a_event_id)." ";
392  $this->db->query($query);
393  unset($this->econtent_ids[$a_event_id]);
394  return true;
395  }
396 
401  public function read()
402  {
403  $query = "SELECT * FROM ecs_events ORDER BY event_id ";
404  $res = $this->db->query($query);
405  $counter = 0;
406  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
407  {
408  $this->events[$counter]['event_id'] = $row->event_id;
409  $this->events[$counter]['type'] = $row->type;
410  $this->events[$counter]['id'] = $row->id;
411  $this->events[$counter]['op'] = $row->op;
412 
413  $this->econtent_ids[$row->event_id] = $row->event_id;
414  ++$counter;
415  }
416  return true;
417  }
418 
419 
420 }
421 ?>