ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
24 include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
25 
26 
37 {
38  const TYPE_EXPORTED = 'exported';
39  const TYPE_DIRECTORY_TREES = 'directory_trees';
40  const TYPE_CMS_COURSES = 'courses';
41  const TYPE_CMS_COURSE_MEMBERS = 'course_members';
42  const TYPE_REMOTE_COURSE = 'rcrs';
43  const TYPE_REMOTE_CATEGORY = 'rcat';
44  const TYPE_REMOTE_FILE = 'rfil';
45  const TYPE_REMOTE_GLOSSARY = 'rglo';
46  const TYPE_REMOTE_GROUP = 'rgrp';
48  const TYPE_REMOTE_WIKI = 'rwik';
49  const TYPE_REMOTE_TEST = 'rtst';
50  const TYPE_COURSE_URLS = 'course_urls';
51  const TYPE_ENROLMENT_STATUS = 'member_status';
52 
53  protected $log;
54  protected $db;
55 
56  protected $events = array();
57  protected $econtent_ids = array();
58 
64  public function __construct($a_server_id)
65  {
66  global $ilLog,$ilDB;
67 
68  include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
69 
70  $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
71  $this->log = $ilLog;
72  $this->db = $ilDB;
73 
74  $this->read();
75  }
76 
83  protected static function getEventTypeFromObjectType($a_obj_type)
84  {
85  // currently they are the same for all resource types
86  return $a_obj_type;
87  }
88 
94  protected static function getAllEContentTypes()
95  {
96  return array(self::TYPE_REMOTE_COURSE, self::TYPE_REMOTE_CATEGORY,
97  self::TYPE_REMOTE_FILE, self::TYPE_REMOTE_GLOSSARY, self::TYPE_REMOTE_GROUP,
98  self::TYPE_REMOTE_LEARNING_MODULE, self::TYPE_REMOTE_WIKI, self::TYPE_REMOTE_TEST);
99  }
100 
109  protected static function getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only = false)
110  {
111  include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
112  $list = array();
113  foreach ($a_types as $type) {
115  if ($robj) {
116  $list[$type] = $robj->getAllResourceIds($server, $a_sender_only);
117  }
118  }
119 
120  return $list;
121  }
122 
130  public static function handleImportReset(ilECSSetting $server)
131  {
132  global $ilLog;
133 
134  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
135  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
136 
137  try {
138  include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
139  include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
140  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
141 
142  $types = self::getAllEContentTypes();
143 
144  $event_queue = new ilECSEventQueueReader($server->getServerId());
145  $event_queue->deleteAllEContentEvents($types);
146 
147  $list = self::getAllResourceIds($server, $types);
149 
150  $GLOBALS['ilLog']->write(__METHOD__ . ': Imported = ' . print_r($imported, true));
151  $GLOBALS['ilLog']->write(__METHOD__ . ': List = ' . print_r($list, true));
152 
153  foreach ($list as $resource_type => $link_ids) {
154  if (!in_array($resource_type, ilECSUtils::getPossibleRemoteTypes())) {
155  $GLOBALS['ilLog']->write(__METHOD__ . ': Ignoring resource type ' . $resource_type);
156  continue;
157  }
158 
159 
160  foreach ((array) $link_ids as $link_id) {
161  if (!isset($imported[$link_id])) {
162  // Add create event for not imported econtent
163  $event_queue->add(
164  $resource_type,
165  $link_id,
167  );
168  } else {
169  // Add update event for already existing events
170  $event_queue->add(
171  $resource_type,
172  $link_id,
174  );
175  }
176 
177  if (isset($imported[$link_id])) {
178  unset($imported[$link_id]);
179  }
180  }
181  }
182 
183  if (is_array($imported)) {
184  // Delete event for deprecated econtent
185  include_once 'Services/WebServices/ECS/classes/class.ilECSObjectSettings.php';
186  foreach ($imported as $econtent_id => $obj_id) {
187  $type = self::getEventTypeFromObjectType(ilObject::_lookupType($obj_id));
188  if ($type) {
189  $event_queue->add(
190  $type,
191  $econtent_id,
193  );
194  }
195  }
196  }
197  } catch (ilECSConnectorException $e1) {
198  $ilLog->write('Cannot connect to ECS server: ' . $e1->getMessage());
199  throw $e1;
200  } catch (ilException $e2) {
201  $ilLog->write('Update failed: ' . $e2->getMessage());
202  throw $e2;
203  }
204  return true;
205  }
206 
215  public static function handleExportReset(ilECSSetting $server)
216  {
217  include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
218 
219  // Delete all export events
220  $queue = new ilECSEventQueueReader($server->getServerId());
221  $queue->deleteAllExportedEvents();
222 
223  // Read all local export info
224  $local_econtent_ids = ilECSExport::_getAllEContentIds($server->getServerId());
225 
226  $types = self::getAllEContentTypes();
227  $list = self::getAllResourceIds($server, $types, true);
228 
229 
230  // merge in one array
231  $all_remote_ids = array();
232  foreach ($list as $resource_type => $remote_ids) {
233  $all_remote_ids = array_merge($all_remote_ids, (array) $remote_ids);
234  }
235  $all_remote_ids = array_unique($all_remote_ids);
236 
237  $GLOBALS['ilLog']->write(__METHOD__ . ': Resources = ' . print_r($all_remote_ids, true));
238  $GLOBALS['ilLog']->write(__METHOD__ . ': Local = ' . print_r($local_econtent_ids, true));
239  foreach ($local_econtent_ids as $local_econtent_id => $local_obj_id) {
240  if (!in_array($local_econtent_id, $all_remote_ids)) {
241  // Delete this deprecated info
242  $GLOBALS['ilLog']->write(__METHOD__ . ': Deleting deprecated econtent id ' . $local_econtent_id);
243  ilECSExport::_deleteEContentIds($server->getServerId(), array($local_econtent_id));
244  }
245  }
246  return true;
247  }
248 
249 
254  public function getServer()
255  {
256  return $this->settings;
257  }
258 
259 
266  public function getEvents()
267  {
268  return $this->events ? $this->events : array();
269  }
270 
276  public function deleteAll()
277  {
278  global $ilDB;
279 
280  $query = "DELETE FROM ecs_events " .
281  'WHERE server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
282  $res = $ilDB->manipulate($query);
283  return true;
284  }
285 
292  public function deleteAllEContentEvents(array $a_types)
293  {
294  global $ilDB;
295 
296  $query = "DELETE FROM ecs_events " .
297  "WHERE " . $this->db->in("type", $a_types, "", "text") . ' ' .
298  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
299  $res = $ilDB->manipulate($query);
300  return true;
301  }
302 
308  protected function deleteAllExportedEvents()
309  {
310  global $ilDB;
311 
312  $query = "DELETE FROM ecs_events " .
313  "WHERE type = " . $this->db->quote(self::TYPE_EXPORTED, 'text') . ' ' .
314  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
315  $res = $ilDB->manipulate($query);
316  return true;
317  }
318 
325  public function refresh()
326  {
327  try {
328  include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
329  include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
330 
331  $connector = new ilECSConnector($this->getServer());
332  while (true) {
333  $res = $connector->readEventFifo(false);
334 
335  if (!count($res->getResult())) {
336  return true;
337  }
338 
339  foreach ($res->getResult() as $result) {
340  include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
341  $event = new ilECSEvent($result);
342 
343  $GLOBALS['ilLog']->write(__METHOD__ . ' ---------------------------- Handling new event ');
344  $GLOBALS['ilLog']->write(__METHOD__ . print_r($event, true));
345  $GLOBALS['ilLog']->write(__METHOD__ . ' ---------------------------- Done! ');
346 
347  // Fill command queue
348  $this->writeEventToDB($event);
349  }
350  // Delete from fifo
351  $connector->readEventFifo(true);
352  }
353  } catch (ilECSConnectorException $e) {
354  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot read event fifo. Aborting');
355  }
356  }
357 
363  public static function deleteServer($a_server_id)
364  {
365  global $ilDB;
366 
367  $query = 'DELETE FROM ecs_events ' .
368  'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer');
369  $ilDB->manipulate($query);
370  }
371 
375  private function writeEventToDB(ilECSEvent $ev)
376  {
377  global $ilDB;
378 
379  // this should probably be moved elsewhere
380  switch ($ev->getRessourceType()) {
381  case 'directory_trees':
382  $type = self::TYPE_DIRECTORY_TREES;
383  break;
384 
385  case 'course_members':
386  $type = self::TYPE_CMS_COURSE_MEMBERS;
387  break;
388 
389  case 'courses':
390  $type = self::TYPE_CMS_COURSES;
391  break;
392 
393  case 'courselinks':
394  $type = self::TYPE_REMOTE_COURSE;
395  break;
396 
397  case 'categories':
398  $type = self::TYPE_REMOTE_CATEGORY;
399  break;
400 
401  case 'files':
402  $type = self::TYPE_REMOTE_FILE;
403  break;
404 
405  case 'glossaries':
406  $type = self::TYPE_REMOTE_GLOSSARY;
407  break;
408 
409  case 'groups':
410  $type = self::TYPE_REMOTE_GROUP;
411  break;
412 
413  case 'learningmodules':
414  $type = self::TYPE_REMOTE_LEARNING_MODULE;
415  break;
416 
417  case 'wikis':
418  $type = self::TYPE_REMOTE_WIKI;
419  break;
420 
421  case 'tests':
422  $type = self::TYPE_REMOTE_TEST;
423  break;
424 
425  case 'course_urls':
426  $type = self::TYPE_COURSE_URLS;
427  break;
428 
429  case 'member_status':
430  $type = self::TYPE_ENROLMENT_STATUS;
431  break;
432 
433  default:
434  // write custom event type
435  $type = $ev->getRessourceType();
436  break;
437  }
438 
439  $query = "SELECT * FROM ecs_events " .
440  "WHERE type = " . $ilDB->quote($type, 'integer') . " " .
441  "AND id = " . $ilDB->quote($ev->getRessourceId(), 'integer') . " " .
442  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
443  $res = $ilDB->query($query);
444 
445  $event_id = 0;
446  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
447  $event_id = $row->event_id;
448  }
449 
450  if (!$event_id) {
451  // No previous entry exists => perform insert
452  $query = "INSERT ecs_events (event_id,type,id,op,server_id) " .
453  "VALUES( " .
454  $ilDB->quote($ilDB->nextId('ecs_events'), 'integer') . ',' .
455  $ilDB->quote($type, 'text') . ', ' .
456  $ilDB->quote($ev->getRessourceId(), 'integer') . ', ' .
457  $ilDB->quote($ev->getStatus(), 'text') . ', ' .
458  $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
459  ')';
460  $ilDB->manipulate($query);
461  return true;
462  }
463  // Do update
464  $do_update = false;
465  switch ($ev->getStatus()) {
466  case ilECSEvent::CREATED:
467  // Do update, although impossible
468  $do_update = true;
469  break;
470 
472  $do_update = true;
473  break;
474 
475  case ilECSEvent::UPDATED:
476  // Do nothing. Old status is ok.
477  break;
478  }
479 
480  if (!$do_update) {
481  return true;
482  }
483  $query = "UPDATE ecs_events " .
484  "SET op = " . $ilDB->quote($ev->getStatus(), 'text') . " " .
485  "WHERE event_id = " . $ilDB->quote($event_id, 'integer') . ' ' .
486  'AND type = ' . $ilDB->quote($type) . ' ' .
487  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
488  $ilDB->manipulate($query);
489  return true;
490  }
491 
498  public function shift()
499  {
500  $event = array_shift($this->events);
501  if ($event == null) {
502  return array();
503  } else {
504 
505  #$this->delete($event['event_id']);
506  return $event;
507  }
508  }
509 
510 
516  public function add($a_type, $a_id, $a_op)
517  {
518  global $ilDB;
519 
520  $next_id = $ilDB->nextId('ecs_events');
521  $query = "INSERT INTO ecs_events (event_id,type,id,op,server_id) " .
522  "VALUES (" .
523  $ilDB->quote($next_id, 'integer') . ", " .
524  $this->db->quote($a_type, 'text') . ", " .
525  $this->db->quote($a_id, 'integer') . ", " .
526  $this->db->quote($a_op, 'text') . ", " .
527  $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
528  ")";
529  $res = $ilDB->manipulate($query);
530 
531  $new_event['event_id'] = $next_id;
532  $new_event['type'] = $a_type;
533  $new_event['id'] = $a_id;
534  $new_event['op'] = $a_op;
535 
536  $this->events[] = $new_event;
537  $this->econtent_ids[$a_id] = $a_id;
538  return true;
539  }
540 
547  private function update($a_type, $a_id, $a_operation)
548  {
549  global $ilDB;
550 
551  $query = "UPDATE ecs_events " .
552  "SET op = " . $this->db->quote($a_operation, 'text') . " " .
553  "WHERE type = " . $this->db->quote($a_type, 'text') . " " .
554  "AND id = " . $this->db->quote($a_id, 'integer') . " " .
555  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
556  $res = $ilDB->manipulate($query);
557  }
558 
565  public function delete($a_event_id)
566  {
567  global $ilDB;
568 
569  $query = "DELETE FROM ecs_events " .
570  "WHERE event_id = " . $this->db->quote($a_event_id, 'integer') . " " .
571  'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
572  $res = $ilDB->manipulate($query);
573  unset($this->econtent_ids[$a_event_id]);
574  return true;
575  }
576 
581  public function read()
582  {
583  global $ilDB;
584 
585  $query = "SELECT * FROM ecs_events " .
586  'WHERE server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
587  'ORDER BY event_id';
588 
589  $res = $this->db->query($query);
590  $counter = 0;
591  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
592  $this->events[$counter]['event_id'] = $row->event_id;
593  $this->events[$counter]['type'] = $row->type;
594  $this->events[$counter]['id'] = $row->id;
595  $this->events[$counter]['op'] = $row->op;
596 
597  $this->econtent_ids[$row->event_id] = $row->event_id;
598  ++$counter;
599  }
600  return true;
601  }
602 
603  public static function deleteByServerId($a_server_id)
604  {
605  global $ilDB;
606 
607  $query = 'DELETE FROM ecs_events' .
608  ' WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer');
609  $ilDB->manipulate($query);
610  return true;
611  }
612 }
static getAllImportedRemoteObjects($a_server_id)
get all imported links
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
static getEventTypeFromObjectType($a_obj_type)
Convert object type to event type.
static _getAllEContentIds($a_server_id)
get all exported econtent ids per server
static getPossibleRemoteTypes($a_with_captions=false)
Get all possible remote object types.
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
static handleImportReset(ilECSSetting $server)
Reread all imported econtent.
update($a_type, $a_id, $a_operation)
update one entry
$result
static deleteServer($a_server_id)
Delete by server id ilDB $ilDB.
getStatus()
get title
$type
getServerId()
Get current server id.
static _deleteEContentIds($a_server_id, $a_ids)
Delete econtent ids for server.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getRessourceType()
Get ressource type.
deleteAllExportedEvents()
Delete all exported events.
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
$counter
static getAllEContentTypes()
All available content types.
deleteAllEContentEvents(array $a_types)
Delete all econtents.
$a_type
Definition: workflow.php:92
getRessourceId()
Get ressource id.
foreach($_POST as $key=> $value) $res
writeEventToDB(ilECSEvent $ev)
Write event to db.
refresh()
Fetch events from fifo Using fifo public.
Reads ECS events and stores them in the database.
static deleteByServerId($a_server_id)
$query
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
shift()
get and delete the first event entry
$server
Definition: getUserInfo.php:12
settings()
Definition: settings.php:2
__construct($a_server_id)
Constructor.
global $ilDB
static handleExportReset(ilECSSetting $server)
Handle export reset.
static getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only=false)
Get all resource ids by resource type.
add($a_type, $a_id, $a_op)
add