ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BasicBucket.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
12 class BasicBucket implements Bucket
13 {
14 
18  protected $userId;
22  protected $rootTask;
26  protected $currentTask;
30  protected $tasks;
34  protected $state;
42  protected $percentages = [];
46  protected $title = "";
50  protected $description = "";
54  protected $percentage = 0;
58  protected $lastHeartbeat = 0;
59 
60 
64  public function getUserId()
65  {
66  return $this->userId;
67  }
68 
69 
75  public function setUserId($user_id)
76  {
77  $this->userId = $user_id;
78 
79  return $this;
80  }
81 
82 
89  public function setPercentage(Task $task, $percentage)
90  {
91  $this->percentages[spl_object_hash($task)] = $percentage;
92  $this->calculateOverallPercentage();
93  }
94 
95 
97  {
98  $this->percentage = $percentage;
99  }
100 
101 
107  public function setCurrentTask($task)
108  {
109  $this->currentTask = $task;
110  }
111 
112 
118  public function setTask(Task $task)
119  {
120  $this->tasks = $task->unfoldTask();
121  $this->totalNumberOfTasks = count($this->tasks);
122  $this->rootTask = $task;
123  foreach ($this->tasks as $subTask) {
124  $this->percentages[spl_object_hash($subTask)] = 0;
125  }
126  }
127 
128 
134  public function calculateOverallPercentage()
135  {
136  $countable_tasks = 0;
140  foreach ($this->tasks as $task) {
141  switch (true) {
142  case ($task instanceof Task\Job):
143  $countable_tasks++;
144  break;
145  }
146  }
147 
148  $this->percentage = array_sum($this->percentages) / $countable_tasks;
149  }
150 
151 
152  public function getOverallPercentage()
153  {
154  return $this->percentage;
155  }
156 
157 
161  public function setState($state)
162  {
163  $this->state = $state;
164  }
165 
166 
170  public function getCurrentTask()
171  {
172  return $this->currentTask;
173  }
174 
175 
180  public function getTask()
181  {
182  return $this->rootTask;
183  }
184 
185 
189  public function getState()
190  {
191  return $this->state;
192  }
193 
194 
199  public function checkIntegrity()
200  {
201  if (!$this->getUserId()) {
202  foreach ($this->getTask()->unfoldTask() as $task) {
203  if ($task instanceof Task\UserInteraction) {
204  throw new Exception("Your task contains user interactions and thus needs a user that observes the task.");
205  }
206  }
207  }
208 
209  return true;
210  }
211 
212 
221  public function userInteraction(Option $option)
222  {
223  $currentTask = $this->getCurrentTask();
224 
225  if ($this->getState() != State::USER_INTERACTION) {
226  throw new Exception("Cannot continue a task that is not in the state 'user interaction'");
227  }
228  if (!$currentTask instanceof Task\UserInteraction) {
229  // TODO: Maybe cleanup task?
230  throw new Exception("Observer is in an invalid state! state: userInteraction but current task is not a user interaction!");
231  }
232 
233  // From the current task we do the interaction.
234  $inputs = $currentTask->getInput();
235  $resulting_value = $currentTask->interaction($inputs, $option, $this);
236 
237  if ($currentTask === $this->rootTask) {
238  // If this user interaction was the last thing to do, we set the state to finished. We can throw away the resulting value.
239  $this->setState(State::FINISHED);
240  } else {
241  // Then we replace the thunk value with the resulting value.
242  $this->replaceThunkValue($currentTask, $resulting_value);
243  }
244  }
245 
246 
254  protected function replaceThunkValue(Task $currentTask, Value $resulting_value)
255  {
256  $tasks = $this->getTask()->unfoldTask();
257 
258  foreach ($tasks as $task) {
259  $newInputs = [];
260  foreach ($task->getInput() as $input) {
261  if ($input instanceof ThunkValue && $input->getParentTask() === $currentTask) {
262  $newInputs[] = $resulting_value;
263  } else {
264  $newInputs[] = $input;
265  }
266  }
267  $task->setInput($newInputs);
268  }
269  }
270 
271 
275  public function getTitle()
276  {
277  return $this->title;
278  }
279 
280 
284  public function setTitle($title)
285  {
286  $this->title = $title;
287  }
288 
289 
293  public function getDescription()
294  {
295  return $this->description;
296  }
297 
298 
302  public function setDescription($description)
303  {
304  $this->description = $description;
305  }
306 
307 
313  public function heartbeat()
314  {
315  $now = new \DateTime('now', new \DateTimeZone('UTC'));
316  $this->lastHeartbeat = $now->getTimestamp();
317  }
318 
319 
325  public function setLastHeartbeat($timestamp)
326  {
327  $this->lastHeartbeat = $timestamp;
328  }
329 
330 
336  public function getLastHeartbeat()
337  {
338  return $this->lastHeartbeat;
339  }
340 }
setPercentage(Task $task, $percentage)
Used by a job to notify his percentage.
Definition: BasicBucket.php:89
getLastHeartbeat()
When was the last time that something happened on this bucket?
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
heartbeat()
There was something going on in the bucket, it&#39;s still working.
replaceThunkValue(Task $currentTask, Value $resulting_value)
In the structure of the task of this bucket the result of $currentTask is replaced with the $resultin...
userInteraction(Option $option)
Continue a task with a given option.