ILIAS  Release_4_1_x_branch Revision 61804
 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  global $ilDB;
206 
207  $query = "DELETE FROM ecs_events";
208  $res = $ilDB->manipulate($query);
209  return true;
210  }
211 
217  public function deleteAllEContentEvents()
218  {
219  global $ilDB;
220 
221  $query = "DELETE FROM ecs_events ".
222  "WHERE type = ".$this->db->quote(self::TYPE_ECONTENT,'text');
223  $res = $ilDB->manipulate($query);
224  return true;
225  }
226 
232  public function deleteAllExportedEvents()
233  {
234  global $ilDB;
235 
236  $query = "DELETE FROM ecs_events ".
237  "WHERE type = ".$this->db->quote(self::TYPE_EXPORTED,'text');
238  $res = $ilDB->manipulate($query);
239  return true;
240  }
241 
242 
250  public function refresh()
251  {
252  global $ilLog;
253 
254  try
255  {
256  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
257  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
258 
259  $connector = new ilECSConnector();
260  $res = $connector->getEventQueues();
261 
262  if(!is_array($res->getResult()))
263  {
264  $ilLog->write(__METHOD__.': No new events found.');
265  return true;
266  }
267  $this->log->write(__METHOD__.': Found '.count($res->getResult()).' new events.');
268  foreach($res->getResult() as $event)
269  {
270  // Handle command queue
271  if(isset($event->cmd) and is_object($event->cmd))
272  {
273  if(!isset($event->cmd->admin) and !is_object($event->cmd->admin))
274  {
275  throw new ilECSReaderException('Received invalid command queue structure. Property "admin" is missing');
276  }
277  $admin_cmd = $event->cmd->admin;
278  $this->log->write(__METHOD__.': Received new Commandqueue command: '.$admin_cmd);
279  switch($admin_cmd)
280  {
281  case self::ADMIN_RESET:
283  break;
284  case self::ADMIN_RESET_ALL:
287  break;
288  }
289  }
290  // Handle econtents events
291  if(isset($event->econtents) and is_object($event->econtents))
292  {
293  $operation = $event->econtents->op;
294 
295  if(!in_array($event->econtents->eid,$this->econtent_ids))
296  {
297  // It is not necessary to store multiple entries with the same econtent_id.
298  // since we always have to receive and parse the econtent from the ecs server.
299  $this->add('econtents',$event->econtents->eid,$event->econtents->op);
300  $this->log->write(__METHOD__.': Added new entry for EContentId: '.$event->econtents->eid);
301  }
302  elseif($operation == self::OPERATION_DELETE)
303  {
304  $this->log->write(__METHOD__.': Updating delete operation for EContentId: '.$event->econtents->eid);
305  $this->update('econtents',$event->econtents->eid,$event->econtents->op);
306  }
307  else
308  {
309  // update with last operation
310  $this->log->write(__METHOD__.': Ignoring multiple operations for EContentId: '.$event->econtents->eid);
311  }
312 
313  }
314  }
315  $this->read();
316  }
317  catch(ilECSConnectorException $e)
318  {
319  $ilLog->write(__METHOD__.': Error connecting to ECS server. '.$e->getMessage());
320  throw $e;
321  }
322  catch(ilECSReaderException $e)
323  {
324  $ilLog->write(__METHOD__.': Error reading EventQueue. '.$e->getMessage());
325  throw $e;
326  }
327  }
328 
335  public function shift()
336  {
337  $event = array_shift($this->events);
338  if($event == null)
339  {
340  return array();
341  }
342  else
343  {
344  $this->delete($event['event_id']);
345  return $event;
346  }
347  }
348 
349 
355  public function add($a_type,$a_id,$a_op)
356  {
357  global $ilDB;
358 
359  $next_id = $ilDB->nextId('ecs_events');
360  $query = "INSERT INTO ecs_events (event_id,type,id,op) ".
361  "VALUES (".
362  $ilDB->quote($next_id,'integer').", ".
363  $this->db->quote($a_type,'text').", ".
364  $this->db->quote($a_id,'integer').", ".
365  $this->db->quote($a_op,'text')." ".
366  ")";
367  $res = $ilDB->manipulate($query);
368 
369  $new_event['event_id'] = $next_id;
370  $new_event['type'] = $a_type;
371  $new_event['id'] = $a_id;
372  $new_event['op'] = $a_op;
373 
374  $this->events[] = $new_event;
375  $this->econtent_ids[$a_id] = $a_id;
376  return true;
377  }
378 
385  private function update($a_type,$a_id,$a_operation)
386  {
387  global $ilDB;
388 
389  $query = "UPDATE ecs_events ".
390  "SET op = ".$this->db->quote($a_operation,'text')." ".
391  "WHERE type = ".$this->db->quote($a_type,'text')." ".
392  "AND id = ".$this->db->quote($a_id,'integer')." ";
393  $res = $ilDB->manipulate($query);
394  }
395 
402  private function delete($a_event_id)
403  {
404  global $ilDB;
405 
406  $query = "DELETE FROM ecs_events ".
407  "WHERE event_id = ".$this->db->quote($a_event_id,'integer')." ";
408  $res = $ilDB->manipulate($query);
409  unset($this->econtent_ids[$a_event_id]);
410  return true;
411  }
412 
417  public function read()
418  {
419  global $ilDB;
420 
421  $query = "SELECT * FROM ecs_events ORDER BY event_id ";
422  $res = $this->db->query($query);
423  $counter = 0;
424  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
425  {
426  $this->events[$counter]['event_id'] = $row->event_id;
427  $this->events[$counter]['type'] = $row->type;
428  $this->events[$counter]['id'] = $row->id;
429  $this->events[$counter]['op'] = $row->op;
430 
431  $this->econtent_ids[$row->event_id] = $row->event_id;
432  ++$counter;
433  }
434  return true;
435  }
436 
437 
438 }
439 ?>