ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilEventDetector.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
7 require_once './Services/WorkflowEngine/interfaces/ilExternalDetector.php';
8 
23 {
39  private $event_type;
40 
52  private $event_content;
53 
63 
72 
84 
93 
98  private $listening_start = 0;
99 
104  private $listening_end = 0;
105 
111  private $db_id = null;
112 
118  public function __construct($context)
119  {
120  parent::__construct($context);
121  }
122 
130  {
131  $this->event_type = (string) $event_type;
132  $this->event_content = (string) $event_content;
133  }
134 
140  public function getEvent()
141  {
142  return array('type' => $this->event_type, 'content' => $this->event_content);
143  }
144 
152  {
153  $this->event_subject_type = (string) $event_subject_type;
154  $this->event_subject_identifier = $event_subject_identifier;
155  }
156 
162  public function getEventSubject()
163  {
164  return array('type' => $this->event_subject_type, 'identifier' => $this->event_subject_identifier);
165  }
166 
174  {
175  $this->event_context_type = (string) $event_context_type;
176  $this->event_context_identifier = $event_context_identifier;
177  }
178 
184  public function getEventContext()
185  {
186  return array('type' => $this->event_context_type, 'identifier' => $this->event_context_identifier);
187  }
188 
209  public function trigger($params)
210  {
211  if (!$this->isListening()) {
212  return;
213  }
214 
215  if ($this->event_type !== $params[0]) {
216  // Wrong event type -> no action here.
217  return;
218  }
219 
220  if ($this->event_content !== $params[1]) {
221  // Wrong event content -> no action here.
222  return;
223  }
224 
225  if ($this->event_subject_type !== $params[2]) {
226  // Wrong event subject type -> no action here.
227  return;
228  }
229 
230  if ($this->event_subject_identifier !== $params[3] && $this->event_subject_identifier != 0) {
231  // Wrong event subject identifier and identifier here not 0 (not *all*) -> no action.
232  return;
233  }
234 
235  if ($this->event_context_type !== $params[4]) {
236  // Wrong event context type -> no action.
237  return;
238  }
239 
240  if ($this->event_context_identifier !== $params[5] && $this->event_context_identifier != 0) {
241  // Wrong event context identifier and identifier here not 0 (not *all*) -> no action.
242  return;
243  }
244 
245  // We're through checks now, let's see if this detector is already satisfied.
246  if ($this->getDetectorState() == false) {
247  // X -> ilNode -> ilWorkflow -> Method...
248  foreach ($params as $key => $value) {
249  $this->getContext()->setRuntimeVar($key, $value);
250  }
251  $this->getContext()->setRuntimeVar('current_event', $params);
252  $this->was_activated = true;
253  $this->setDetectorState(true);
254  return true;
255  }
256 
257  return false;
258  }
259 
265  public function isListening()
266  {
267  // No listening phase = always listening.
268  if ($this->listening_start == 0 && $this->listening_end == 0) {
269  return true;
270  }
271 
272  // Listening started?
273  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
274  if ($this->listening_start <= ilWorkflowUtils::time()) {
275  // Listening not ended or infinite?
276  if ($this->listening_end >= ilWorkflowUtils::time()
277  || $this->listening_end == 0) {
278  return true;
279  }
280  }
281 
282  return false;
283  }
284 
294  {
295  $this->listening_start = $listening_start;
296 
297  if ($this->listening_start > $listening_end && $listening_end != 0) {
298  require_once './Services/WorkflowEngine/exceptions/ilWorkflowInvalidArgumentException.php';
299  throw new ilWorkflowInvalidArgumentException('Listening timeframe is (start vs. end) is invalid.');
300  }
301 
302  $this->listening_end = $listening_end;
303  }
304 
308  public function onActivate()
309  {
310  $this->setDetectorState(false);
311  $this->writeDetectorToDb();
312  }
313 
317  public function onDeactivate()
318  {
319  $this->setDetectorState(false);
320  $this->deleteDetectorFromDb();
321  }
322 
328  public function setDbId($a_id)
329  {
330  $this->db_id = $a_id;
331  }
332 
339  public function getDbId()
340  {
341  if ($this->db_id != null) {
342  return $this->db_id;
343  } else {
344  require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
345  throw new ilWorkflowObjectStateException('No database ID set.');
346  }
347  }
348 
353  public function hasDbId()
354  {
355  if ($this->db_id == null) {
356  return false;
357  }
358 
359  return true;
360  }
361 
366  public function writeDetectorToDb()
367  {
368  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
369  ilWorkflowDbHelper::writeDetector($this);
370  }
371 
376  public function deleteDetectorFromDb()
377  {
378  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
379  ilWorkflowDbHelper::deleteDetector($this);
380  }
381 
387  public function getListeningTimeframe()
388  {
389  return array('listening_start' => $this->listening_start, 'listening_end' => $this->listening_end);
390  }
391 
394 
398  public function getActivated()
399  {
400  return $this->was_activated;
401  }
402 }
isListening()
Returns if the detector is currently listening.
getDetectorState()
Returns if the current detector state is satisfied or not.
getEvent()
Returns the event type and content currently set to the detector.
hasDbId()
Returns, if the detector has a database id.
setDbId($a_id)
Sets the database id of the detector.
getListeningTimeframe()
Returns the listening timefrage of the detector.
getEventSubject()
Get the event subject set to the detector.
setEventSubject($event_subject_type, $event_subject_identifier)
Set the event subject type to the detector.
getEventContext()
Get the event context set to the detector.
PhpIncludeInspection
trigger($params)
Triggers the detector.
setListeningTimeframe($listening_start, $listening_end)
Sets the timeframe, in which the detector is listening.
onActivate()
Method called on activation.
setDetectorState($new_state)
Sets a new detector state.
getDbId()
Returns the database id of the detector if set.
setEventContext($event_context_type, $event_context_identifier)
Set the event context to the detector.
__construct($context)
Default constructor, passing the context to the parent constructor.
deleteDetectorFromDb()
Passes this detector to the ilWorkflowDbHelper in order to remove the detector data from the database...
setEvent($event_type, $event_content)
Sets the event type and content (/qualifier) for the detector.
writeDetectorToDb()
Passes this detector to the ilWorkflowDBHelper in order to write or update the detector data to the d...
PhpIncludeInspection
PhpIncludeInspection
$key
Definition: croninfo.php:18
onDeactivate()
Method called on deactivation.
getContext()
Returns the parent object.