ILIAS  release_8 Revision v8.23
BasicBucket.php
Go to the documentation of this file.
1 <?php
2 
20 
27 
28 class BasicBucket implements Bucket
29 {
30  protected int $user_id;
31  protected Task $root_task;
32  protected Task $current_task;
36  protected array $tasks = [];
37  protected int $state;
38  protected int $total_number_of_tasks;
39  protected array $percentages = [];
40  protected string $title = "";
41  protected string $description = "";
42  protected int $percentage = 0;
43  protected int $last_heartbeat = 0;
44 
45  public function getUserId(): int
46  {
47  return $this->user_id ?? 0;
48  }
49 
50  public function setUserId(int $user_id): void
51  {
52  $this->user_id = $user_id;
53  }
54 
55  public function setPercentage(Task $task, int $percentage): void
56  {
57  $this->percentages[spl_object_hash($task)] = $percentage;
58  $this->calculateOverallPercentage();
59  }
60 
61  public function setOverallPercentage(int $percentage): void
62  {
63  $this->percentage = $percentage;
64  }
65 
66  public function setCurrentTask(Task $task): void
67  {
68  $this->current_task = $task;
69  }
70 
71  public function setTask(Task $task): void
72  {
73  $this->tasks = $task->unfoldTask();
74  $this->total_number_of_tasks = count($this->tasks);
75  $this->root_task = $task;
76  foreach ($this->tasks as $subTask) {
77  $this->percentages[spl_object_hash($subTask)] = 0;
78  }
79  }
80 
84  public function calculateOverallPercentage(): void
85  {
86  $countable_tasks = 0;
90  foreach ($this->tasks as $task) {
91  if ($task instanceof Task\Job) {
92  $countable_tasks++;
93  }
94  }
95 
96  $this->percentage = array_sum($this->percentages) / $countable_tasks;
97  }
98 
99  public function getOverallPercentage(): int
100  {
101  return $this->percentage;
102  }
103 
104  public function setState(int $state): void
105  {
106  $this->state = $state;
107  }
108 
109  public function getCurrentTask(): Task
110  {
111  return $this->current_task;
112  }
113 
114  public function hasCurrentTask(): bool
115  {
116  return isset($this->current_task);
117  }
118 
119  public function getTask(): Task
120  {
121  return $this->root_task;
122  }
123 
124  public function getState(): int
125  {
126  return $this->state;
127  }
128 
129  public function checkIntegrity(): bool
130  {
131  if ($this->getUserId() === 0) {
132  foreach ($this->getTask()->unfoldTask() as $task) {
133  if ($task instanceof Task\UserInteraction) {
134  throw new Exception("Your task contains user interactions and thus needs a user that observes the task.");
135  }
136  }
137  }
138 
139  return true;
140  }
141 
145  public function userInteraction(Option $option): void
146  {
147  $currentTask = $this->getCurrentTask();
148 
149  if ($this->getState() != State::USER_INTERACTION) {
150  throw new Exception("Cannot continue a task that is not in the state 'user interaction'");
151  }
152  if (!$currentTask instanceof Task\UserInteraction) {
153  // TODO: Maybe cleanup task?
154  throw new Exception("Observer is in an invalid state! state: userInteraction but current task is not a user interaction!");
155  }
156 
157  // From the current task we do the interaction.
158  $inputs = $currentTask->getInput();
159  $resulting_value = $currentTask->interaction($inputs, $option, $this);
160 
161  if ($currentTask === $this->root_task) {
162  // If this user interaction was the last thing to do, we set the state to finished. We can throw away the resulting value.
163  $this->setState(State::FINISHED);
164  } else {
165  // Then we replace the thunk value with the resulting value.
166  $this->replaceThunkValue($currentTask, $resulting_value);
167  }
168  }
169 
174  protected function replaceThunkValue(Task $currentTask, Value $resulting_value): void
175  {
176  $tasks = $this->getTask()->unfoldTask();
177 
178  foreach ($tasks as $task) {
179  $newInputs = [];
180  foreach ($task->getInput() as $input) {
181  if ($input instanceof ThunkValue && $input->getParentTask() === $currentTask) {
182  $newInputs[] = $resulting_value;
183  } else {
184  $newInputs[] = $input;
185  }
186  }
187  $task->setInput($newInputs);
188  }
189  }
190 
191  public function getTitle(): string
192  {
193  return $this->title;
194  }
195 
196  public function setTitle(string $title): void
197  {
198  $this->title = $title;
199  }
200 
201  public function getDescription(): string
202  {
203  return $this->description;
204  }
205 
206  public function setDescription(string $description): void
207  {
208  $this->description = $description;
209  }
210 
214  public function heartbeat(): void
215  {
216  $now = new \DateTime('now', new \DateTimeZone('UTC'));
217  $this->last_heartbeat = $now->getTimestamp();
218  }
219 
220  public function setLastHeartbeat(int $timestamp): void
221  {
222  $this->last_heartbeat = $timestamp;
223  }
224 
225  public function getLastHeartbeat(): int
226  {
227  return $this->last_heartbeat;
228  }
229 }
getLastHeartbeat()
When was the last time that something happened on this bucket?
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: BasicBucket.php:19
setPercentage(Task $task, int $percentage)
Used by a job to notify his percentage.
Definition: BasicBucket.php:55
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Job.php:19
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
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...