ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
24include_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 $DIC;
67
68 $ilLog = $DIC['ilLog'];
69 $ilDB = $DIC['ilDB'];
70
71 include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
72
73 $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
74 $this->log = $ilLog;
75 $this->db = $ilDB;
76
77 $this->read();
78 }
79
86 protected static function getEventTypeFromObjectType($a_obj_type)
87 {
88 // currently they are the same for all resource types
89 return $a_obj_type;
90 }
91
97 public static function getAllEContentTypes()
98 {
99 return array(self::TYPE_REMOTE_COURSE, self::TYPE_REMOTE_CATEGORY,
100 self::TYPE_REMOTE_FILE, self::TYPE_REMOTE_GLOSSARY, self::TYPE_REMOTE_GROUP,
101 self::TYPE_REMOTE_LEARNING_MODULE, self::TYPE_REMOTE_WIKI, self::TYPE_REMOTE_TEST);
102 }
103
112 protected static function getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only = false)
113 {
114 include_once 'Services/WebServices/ECS/classes/class.ilRemoteObjectBase.php';
115 $list = array();
116 foreach ($a_types as $type) {
118 if ($robj) {
119 $list[$type] = $robj->getAllResourceIds($server, $a_sender_only);
120 }
121 }
122
123 return $list;
124 }
125
133 public static function handleImportReset(ilECSSetting $server)
134 {
135 global $DIC;
136
137 $ilLog = $DIC['ilLog'];
138
139 include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
140 include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
141
142 try {
143 include_once('./Services/WebServices/ECS/classes/class.ilECSEventQueueReader.php');
144 include_once('./Services/WebServices/ECS/classes/class.ilECSImport.php');
145 include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
146
147 $types = self::getAllEContentTypes();
148
149 $event_queue = new ilECSEventQueueReader($server->getServerId());
150 $event_queue->deleteAllEContentEvents($types);
151
152 $list = self::getAllResourceIds($server, $types);
153 $imported = ilECSImport::getAllImportedRemoteObjects($server->getServerId());
154
155 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Imported = ' . print_r($imported, true));
156 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': List = ' . print_r($list, true));
157
158 foreach ($list as $resource_type => $link_ids) {
159 if (!in_array($resource_type, ilECSUtils::getPossibleRemoteTypes())) {
160 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Ignoring resource type ' . $resource_type);
161 continue;
162 }
163
164
165 foreach ((array) $link_ids as $link_id) {
166 if (!isset($imported[$link_id])) {
167 // Add create event for not imported econtent
168 $event_queue->add(
169 $resource_type,
170 $link_id,
172 );
173 } else {
174 // Add update event for already existing events
175 $event_queue->add(
176 $resource_type,
177 $link_id,
179 );
180 }
181
182 if (isset($imported[$link_id])) {
183 unset($imported[$link_id]);
184 }
185 }
186 }
187
188 if (is_array($imported)) {
189 // Delete event for deprecated econtent
190 include_once 'Services/WebServices/ECS/classes/class.ilECSObjectSettings.php';
191 foreach ($imported as $econtent_id => $obj_id) {
193 if ($type) {
194 $event_queue->add(
195 $type,
196 $econtent_id,
198 );
199 }
200 }
201 }
202 } catch (ilECSConnectorException $e1) {
203 $ilLog->write('Cannot connect to ECS server: ' . $e1->getMessage());
204 throw $e1;
205 } catch (ilException $e2) {
206 $ilLog->write('Update failed: ' . $e2->getMessage());
207 throw $e2;
208 }
209 return true;
210 }
211
220 public static function handleExportReset(ilECSSetting $server)
221 {
222 include_once('./Services/WebServices/ECS/classes/class.ilECSExport.php');
223
224 // Delete all export events
225 $queue = new ilECSEventQueueReader($server->getServerId());
226 $queue->deleteAllExportedEvents();
227
228 // Read all local export info
229 $local_econtent_ids = ilECSExport::_getAllEContentIds($server->getServerId());
230
231 $types = self::getAllEContentTypes();
232 $list = self::getAllResourceIds($server, $types, true);
233
234
235 // merge in one array
236 $all_remote_ids = array();
237 foreach ($list as $resource_type => $remote_ids) {
238 $all_remote_ids = array_merge($all_remote_ids, (array) $remote_ids);
239 }
240 $all_remote_ids = array_unique($all_remote_ids);
241
242 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Resources = ' . print_r($all_remote_ids, true));
243 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Local = ' . print_r($local_econtent_ids, true));
244 foreach ($local_econtent_ids as $local_econtent_id => $local_obj_id) {
245 if (!in_array($local_econtent_id, $all_remote_ids)) {
246 // Delete this deprecated info
247 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Deleting deprecated econtent id ' . $local_econtent_id);
248 ilECSExport::_deleteEContentIds($server->getServerId(), array($local_econtent_id));
249 }
250 }
251 return true;
252 }
253
254
259 public function getServer()
260 {
261 return $this->settings;
262 }
263
264
271 public function getEvents()
272 {
273 return $this->events ? $this->events : array();
274 }
275
281 public function deleteAll()
282 {
283 global $DIC;
284
285 $ilDB = $DIC['ilDB'];
286
287 $query = "DELETE FROM ecs_events " .
288 'WHERE server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
289 $res = $ilDB->manipulate($query);
290 return true;
291 }
292
299 public function deleteAllEContentEvents(array $a_types)
300 {
301 global $DIC;
302
303 $ilDB = $DIC['ilDB'];
304
305 $query = "DELETE FROM ecs_events " .
306 "WHERE " . $this->db->in("type", $a_types, "", "text") . ' ' .
307 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
308 $res = $ilDB->manipulate($query);
309 return true;
310 }
311
317 protected function deleteAllExportedEvents()
318 {
319 global $DIC;
320
321 $ilDB = $DIC['ilDB'];
322
323 $query = "DELETE FROM ecs_events " .
324 "WHERE type = " . $this->db->quote(self::TYPE_EXPORTED, 'text') . ' ' .
325 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
326 $res = $ilDB->manipulate($query);
327 return true;
328 }
329
336 public function refresh()
337 {
338 try {
339 include_once('Services/WebServices/ECS/classes/class.ilECSConnector.php');
340 include_once('Services/WebServices/ECS/classes/class.ilECSConnectorException.php');
341
342 $connector = new ilECSConnector($this->getServer());
343 while (true) {
344 $res = $connector->readEventFifo(false);
345
346 if (!count($res->getResult())) {
347 return true;
348 }
349
350 foreach ($res->getResult() as $result) {
351 include_once './Services/WebServices/ECS/classes/class.ilECSEvent.php';
352 $event = new ilECSEvent($result);
353
354 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ' ---------------------------- Handling new event ');
355 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . print_r($event, true));
356 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ' ---------------------------- Done! ');
357
358 // Fill command queue
359 $this->writeEventToDB($event);
360 }
361 // Delete from fifo
362 $connector->readEventFifo(true);
363 }
364 } catch (ilECSConnectorException $e) {
365 $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Cannot read event fifo. Aborting');
366 }
367 }
368
374 public static function deleteServer($a_server_id)
375 {
376 global $DIC;
377
378 $ilDB = $DIC['ilDB'];
379
380 $query = 'DELETE FROM ecs_events ' .
381 'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer');
382 $ilDB->manipulate($query);
383 }
384
388 private function writeEventToDB(ilECSEvent $ev)
389 {
390 global $DIC;
391
392 $ilDB = $DIC['ilDB'];
393
394 // this should probably be moved elsewhere
395 switch ($ev->getRessourceType()) {
396 case 'directory_trees':
398 break;
399
400 case 'course_members':
402 break;
403
404 case 'courses':
406 break;
407
408 case 'courselinks':
410 break;
411
412 case 'categories':
414 break;
415
416 case 'files':
418 break;
419
420 case 'glossaries':
422 break;
423
424 case 'groups':
426 break;
427
428 case 'learningmodules':
430 break;
431
432 case 'wikis':
434 break;
435
436 case 'tests':
438 break;
439
440 case 'course_urls':
442 break;
443
444 case 'member_status':
446 break;
447
448 default:
449 // write custom event type
450 $type = $ev->getRessourceType();
451 break;
452 }
453
454 $query = "SELECT * FROM ecs_events " .
455 "WHERE type = " . $ilDB->quote($type, 'integer') . " " .
456 "AND id = " . $ilDB->quote($ev->getRessourceId(), 'integer') . " " .
457 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
458 $res = $ilDB->query($query);
459
460 $event_id = 0;
461 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
462 $event_id = $row->event_id;
463 }
464
465 if (!$event_id) {
466 // No previous entry exists => perform insert
467 $query = "INSERT ecs_events (event_id,type,id,op,server_id) " .
468 "VALUES( " .
469 $ilDB->quote($ilDB->nextId('ecs_events'), 'integer') . ',' .
470 $ilDB->quote($type, 'text') . ', ' .
471 $ilDB->quote($ev->getRessourceId(), 'integer') . ', ' .
472 $ilDB->quote($ev->getStatus(), 'text') . ', ' .
473 $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
474 ')';
475 $ilDB->manipulate($query);
476 return true;
477 }
478 // Do update
479 $do_update = false;
480 switch ($ev->getStatus()) {
482 // Do update, although impossible
483 $do_update = true;
484 break;
485
487 $do_update = true;
488 break;
489
491 // Do nothing. Old status is ok.
492 break;
493 }
494
495 if (!$do_update) {
496 return true;
497 }
498 $query = "UPDATE ecs_events " .
499 "SET op = " . $ilDB->quote($ev->getStatus(), 'text') . " " .
500 "WHERE event_id = " . $ilDB->quote($event_id, 'integer') . ' ' .
501 'AND type = ' . $ilDB->quote($type) . ' ' .
502 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
503 $ilDB->manipulate($query);
504 return true;
505 }
506
513 public function shift()
514 {
515 $event = array_shift($this->events);
516 if ($event == null) {
517 return array();
518 } else {
519
520 #$this->delete($event['event_id']);
521 return $event;
522 }
523 }
524
525
531 public function add($a_type, $a_id, $a_op)
532 {
533 global $DIC;
534
535 $ilDB = $DIC['ilDB'];
536
537 $next_id = $ilDB->nextId('ecs_events');
538 $query = "INSERT INTO ecs_events (event_id,type,id,op,server_id) " .
539 "VALUES (" .
540 $ilDB->quote($next_id, 'integer') . ", " .
541 $this->db->quote($a_type, 'text') . ", " .
542 $this->db->quote($a_id, 'integer') . ", " .
543 $this->db->quote($a_op, 'text') . ", " .
544 $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
545 ")";
546 $res = $ilDB->manipulate($query);
547
548 $new_event['event_id'] = $next_id;
549 $new_event['type'] = $a_type;
550 $new_event['id'] = $a_id;
551 $new_event['op'] = $a_op;
552
553 $this->events[] = $new_event;
554 $this->econtent_ids[$a_id] = $a_id;
555 return true;
556 }
557
564 private function update($a_type, $a_id, $a_operation)
565 {
566 global $DIC;
567
568 $ilDB = $DIC['ilDB'];
569
570 $query = "UPDATE ecs_events " .
571 "SET op = " . $this->db->quote($a_operation, 'text') . " " .
572 "WHERE type = " . $this->db->quote($a_type, 'text') . " " .
573 "AND id = " . $this->db->quote($a_id, 'integer') . " " .
574 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
575 $res = $ilDB->manipulate($query);
576 }
577
584 public function delete($a_event_id)
585 {
586 global $DIC;
587
588 $ilDB = $DIC['ilDB'];
589
590 $query = "DELETE FROM ecs_events " .
591 "WHERE event_id = " . $this->db->quote($a_event_id, 'integer') . " " .
592 'AND server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer');
593 $res = $ilDB->manipulate($query);
594 unset($this->econtent_ids[$a_event_id]);
595 return true;
596 }
597
602 public function read()
603 {
604 global $DIC;
605
606 $ilDB = $DIC['ilDB'];
607
608 $query = "SELECT * FROM ecs_events " .
609 'WHERE server_id = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ' .
610 'ORDER BY event_id';
611
612 $res = $this->db->query($query);
613 $counter = 0;
614 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
615 $this->events[$counter]['event_id'] = $row->event_id;
616 $this->events[$counter]['type'] = $row->type;
617 $this->events[$counter]['id'] = $row->id;
618 $this->events[$counter]['op'] = $row->op;
619
620 $this->econtent_ids[$row->event_id] = $row->event_id;
621 ++$counter;
622 }
623 return true;
624 }
625
626 public static function deleteByServerId($a_server_id)
627 {
628 global $DIC;
629
630 $ilDB = $DIC['ilDB'];
631
632 $query = 'DELETE FROM ecs_events' .
633 ' WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer');
634 $ilDB->manipulate($query);
635 return true;
636 }
637}
$result
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
An exception for terminatinating execution or to throw for unit testing.
Reads ECS events and stores them in the database.
static deleteServer($a_server_id)
Delete by server id @global ilDB $ilDB.
update($a_type, $a_id, $a_operation)
update one entry
static handleExportReset(ilECSSetting $server)
Handle export reset.
static getAllEContentTypes()
All available content types.
deleteAllEContentEvents(array $a_types)
Delete all econtents.
writeEventToDB(ilECSEvent $ev)
Write event to db.
refresh()
Fetch events from fifo Using fifo @access public.
add($a_type, $a_id, $a_op)
add
shift()
get and delete the first event entry
__construct($a_server_id)
Constructor.
static getEventTypeFromObjectType($a_obj_type)
Convert object type to event type.
static handleImportReset(ilECSSetting $server)
Reread all imported econtent.
deleteAllExportedEvents()
Delete all exported events.
static deleteByServerId($a_server_id)
static getAllResourceIds(ilECSSetting $server, array $a_types, $a_sender_only=false)
Get all resource ids by resource type.
getRessourceType()
Get ressource type.
getStatus()
get title
getRessourceId()
Get ressource id.
static _deleteEContentIds($a_server_id, $a_ids)
Delete econtent ids for server.
static _getAllEContentIds($a_server_id)
get all exported econtent ids per server
static getAllImportedRemoteObjects($a_server_id)
get all imported links
static getInstanceByServerId($a_server_id)
Get singleton instance per server.
static getPossibleRemoteTypes($a_with_captions=false)
Get all possible remote object types.
Base class for ILIAS Exception handling.
static _lookupType($a_id, $a_reference=false)
lookup object type
static getInstanceByEventType($a_type)
Get instance by ilECSEvent(QueueReader) type.
$server
$query
$type
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
global $ilDB
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46