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