ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTimerDetector.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3
5require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
7require_once './Services/WorkflowEngine/interfaces/ilExternalDetector.php';
8
21{
30 private $event_type = 'time_passed';
31
33 private $event_content = 'time_passed';
34
36 private $event_subject_type = 'none';
37
40
42 private $event_context_type = 'none';
43
46
49
55 private $timer_start = 0;
56
62 private $timer_limit = 0;
63
68 private $listening_start = 0;
69
74 private $listening_end = 0;
75
81 private $db_id = null;
82
88 public function __construct($context)
89 {
90 parent::__construct($context);
91 }
92
98 public function setTimerStart($timer_start)
99 {
100 $this->timer_start = (int) $timer_start;
101 }
102
108 public function getTimerStart()
109 {
110 return $this->timer_start;
111 }
112
119 {
120 $this->timer_limit = (int) $timer_limit;
121 }
122
128 public function getTimerLimit()
129 {
130 return $this->timer_limit;
131 }
132
143 public function trigger($params)
144 {
145 if ($this->getDetectorState() == true) {
146 return false;
147 }
148
149 require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
150 if ($this->timer_limit + $this->timer_start <= ilWorkflowUtils::time()) {
151 $this->setDetectorState(true);
152 }
153 return true;
154 }
155
161 public function isListening()
162 {
163 // No listening phase = always listening.
164 if ($this->listening_start == 0 && $this->listening_end == 0) {
165 return true;
166 }
167
168 // Listening started?
169 require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
170 if ($this->listening_start < ilWorkflowUtils::time()) {
171 // Listening not ended or infinite?
172 if ($this->listening_end > ilWorkflowUtils::time()
173 || $this->listening_end == 0) {
174 return true;
175 }
176 }
177
178 return false;
179 }
180
190 {
191 $this->listening_start = $listening_start;
192
193 if ($this->listening_start > $listening_end && $listening_end != 0) {
194 require_once './Services/WorkflowEngine/exceptions/ilWorkflowInvalidArgumentException.php';
195 throw new ilWorkflowInvalidArgumentException('Listening timeframe is (start vs. end) is invalid.');
196 }
197
198 $this->listening_end = $listening_end;
199 }
200
204 public function onActivate()
205 {
206 if ($this->timer_relative) {
207 if ($this->timer_start == 0) {
208 $this->listening_start = time();
209 } else {
210 $this->listening_start = $this->timer_start;
211 }
212 if ($this->timer_limit != 0) {
213 $this->listening_end = $this->listening_start + $this->timer_limit;
214 } else {
215 $this->listening_end = 0;
216 }
217 }
218 $this->setDetectorState(false);
219 $this->writeDetectorToDb();
220 }
221
225 public function onDeactivate()
226 {
227 $this->setDetectorState(false);
228 $this->deleteDetectorFromDb();
229 }
230
236 public function setDbId($a_id)
237 {
238 $this->db_id = $a_id;
239 }
240
246 public function getDbId()
247 {
248 if ($this->db_id != null) {
249 return $this->db_id;
250 } else {
251 require_once './Services/WorkflowEngine/exceptions/ilWorkflowObjectStateException.php';
252 throw new ilWorkflowObjectStateException('No database ID set.');
253 }
254 }
255
261 public function hasDbId()
262 {
263 if ($this->db_id == null) {
264 return false;
265 }
266
267 return true;
268 }
269
274 public function writeDetectorToDb()
275 {
276 require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
277 ilWorkflowDbHelper::writeDetector($this);
278 }
279
284 public function deleteDetectorFromDb()
285 {
286 require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
287 ilWorkflowDbHelper::deleteDetector($this);
288 }
289
295 public function getEvent()
296 {
297 return array('type' => $this->event_type, 'content' => $this->event_content);
298 }
299
305 public function getEventSubject()
306 {
307 return array('type' => $this->event_subject_type, 'identifier' => $this->event_subject_identifier);
308 }
309
315 public function getEventContext()
316 {
317 return array('type' => $this->event_context_type, 'identifier' => $this->event_context_identifier);
318 }
319
324 public function getListeningTimeframe()
325 {
326 return array('listening_start' => $this->listening_start, 'listening_end' => $this->listening_end);
327 }
328
332 public function isTimerRelative()
333 {
335 }
336
341 {
342 $this->timer_relative = $timer_relative;
343 }
344}
An exception for terminatinating execution or to throw for unit testing.
@noinspection PhpIncludeInspection
getDetectorState()
Returns if the current detector state is satisfied or not.
setDetectorState($new_state)
Sets a new detector state.
@noinspection PhpIncludeInspection
writeDetectorToDb()
Passes this detector to the ilWorkflowDBHelper in order to write or update the detector data to the d...
setListeningTimeframe($listening_start, $listening_end)
Sets the timeframe, in which the detector is listening.
getListeningTimeframe()
Returns the listening timefrage of the detector.
onActivate()
Method called on activation.
getEventContext()
Get the event context set to the detector.
getTimerStart()
Returns the currently set timer start.
getTimerLimit()
Returns the currently set timers limit.
isListening()
Returns if the detector is currently listening.
getEvent()
Returns the event type and content currently set to the detector.
getDbId()
Returns the database id of the detector if set.
hasDbId()
Returns, if the detector has a database id.
setTimerLimit($timer_limit)
Sets the timers limit.
trigger($params)
Trigger this detector.
deleteDetectorFromDb()
Passes this detector to the ilWorkflowDbHelper in order to remove the detector data from the database...
getEventSubject()
Get the event subject set to the detector.
$event_type
Holds the type of the event to listen for.
setDbId($a_id)
Sets the database id of the detector.
setTimerRelative($timer_relative)
onDeactivate()
Method called on deactivation.
__construct($context)
Default constructor, passing the context to the parent constructor.
setTimerStart($timer_start)
Sets the timers start datetime.
@noinspection PhpIncludeInspection
@noinspection PhpIncludeInspection