ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLSPlayer.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
7
14{
15 const PARAM_LSO_COMMAND = 'lsocmd';
16 const PARAM_LSO_PARAMETER = 'lsov';
17
18 const LSO_CMD_NEXT = 'lsonext'; //with param directions
19 const LSO_CMD_GOTO = 'lsogoto'; //with param ref_id
20 const LSO_CMD_SUSPEND = 'lsosuspend';
21 const LSO_CMD_FINISH = 'lsofinish';
22
23 public function __construct(
24 int $lso_ref_id,
25 string $lso_title,
26 int $usr_id,
27 array $items, //LSLearnerItem[]
28 ilLSStateDB $state_db,
29 LSControlBuilder $control_builder,
30 LSUrlBuilder $url_builder,
31 ilLSCurriculumBuilder $curriculum_builder,
32 ilLSViewFactory $view_factory,
33 ilKioskPageRenderer $renderer,
34 ILIAS\UI\Factory $ui_factory
35 ) {
36 $this->lso_ref_id = $lso_ref_id;
37 $this->lso_title = $lso_title;
38 $this->usr_id = $usr_id;
39 $this->items = $items;
40 $this->state_db = $state_db;
41 $this->control_builder = $control_builder;
42 $this->url_builder = $url_builder;
43 $this->curriculum_builder = $curriculum_builder;
44 $this->view_factory = $view_factory;
45 $this->page_renderer = $renderer;
46 $this->ui_factory = $ui_factory;
47 }
48
49 public function render(array $get, array $post = null)
50 {
51 //init state and current item
52 $stored = $this->state_db->getCurrentItemsFor(
53 $this->lso_ref_id,
54 [$this->usr_id]
55 );
56
57 $current_item = $this->items[0];
58 $current_item_ref_id = $current_item->getRefId();
59
60 if (count($stored) > 0 ||
61 $stored[$this->usr_id] > -1 //returns -1 if there is no current item
62 ) {
63 $current_item_ref_id = $stored[$this->usr_id];
64
65 //maybe item is not available?
66 $valid_ref_ids = array_map(
67 function ($item) {
68 return $item->getRefId();
69 },
70 array_values($this->items)
71 );
72
73 if (in_array($current_item_ref_id, $valid_ref_ids)) {
74 list($position, $current_item) = $this->findItemByRefId($current_item_ref_id);
75 }
76 }
77
79 $param = (int) $_GET[self::PARAM_LSO_PARAMETER];
80
81 while ($current_item->getAvailability() !== \ILIAS\UI\Component\Listing\Workflow\Step::AVAILABLE) {
82 $prev_item = $this->getNextItem($current_item, -1);
83 if ($prev_item === $current_item) {
84 throw new \Exception("Cannot view first LSO-item", 1);
85 }
86 $current_item = $prev_item;
87 }
88
89 $view = $this->view_factory->getViewFor($current_item);
90 $state = $current_item->getState();
91 $state = $this->updateViewState($state, $view, $get, $post);
92
93 switch ($command) {
96 //store state and exit
97 $this->storeState($state, $current_item_ref_id, $current_item_ref_id);
98 return 'EXIT::' . $command;
100 $next_item = $this->getNextItem($current_item, $param);
101 if ($next_item->getAvailability() !== \ILIAS\UI\Component\Listing\Workflow\Step::AVAILABLE) {
102 $next_item = $current_item;
103 }
104 break;
106 list($position, $next_item) = $this->findItemByRefId($param);
107 break;
108 default:
109 $next_item = $current_item;
110 }
111 //write State to DB
112 $this->storeState($state, $current_item_ref_id, $next_item->getRefId());
113
114 //get proper view
115 if ($next_item !== $current_item) {
116 $view = $this->view_factory->getViewFor($next_item);
117 }
118 //get position
119 list($item_position, $item) = $this->findItemByRefId($next_item->getRefId());
120
121 //have the view build controls
122 $control_builder = $this->control_builder;
123 $view->buildControls($state, $control_builder);
124 //amend controls not set by the view
125 $this->buildDefaultControls($control_builder, $item, $item_position);
126
127 //content
128 $obj_title = $next_item->getTitle();
129 $icon = $this->ui_factory->icon()
130 ->standard($next_item->getType(), $next_item->getType(), 'medium');
131
132 $curriculum = $this->curriculum_builder->getLearnerCurriculum(true)
133 ->withActive($item_position);
134
135 $content = $this->renderComponentView($state, $view);
136 $panel = $this->ui_factory->panel()->standard(
137 '', //panel_title
138 $content
139 );
140 $content = [$panel];
141
142 return $this->page_renderer->render(
143 $this->lso_title,
144 $control_builder,
145 $obj_title,
146 $icon,
147 $content,
148 $curriculum
149 );
150 }
151
152 protected function storeState(
153 ILIAS\KioskMode\State $state,
154 int $state_item_ref_id,
155 int $current_item_ref_id
156 ) {
157 $this->state_db->updateState(
158 $this->lso_ref_id,
159 $this->usr_id,
160 $state_item_ref_id,
161 $state,
162 $current_item_ref_id
163 );
164 }
165
166 protected function updateViewState(
167 ILIAS\KioskMode\State $state,
168 ILIAS\KioskMode\View $view,
169 array $get,
170 array $post = null
171 ) : ILIAS\KioskMode\State {
172 //get view internal command
173 $command = $_GET[self::PARAM_LSO_COMMAND];
174 $param = (int) $_GET[self::PARAM_LSO_PARAMETER];
175 if (!is_null($command)) {
176 $state = $view->updateGet($state, $command, $param);
177 }
178 //$state = $view->updatePOST($state, $command, $post);
179 return $state;
180 }
181
185 protected function getNextItem(LSLearnerItem $current_item, int $direction) : LSLearnerItem
186 {
187 list($position, $item) = $this->findItemByRefId($current_item->getRefId());
188 $next = $position + $direction;
189 if ($next >= 0 && $next < count($this->items)) {
190 return $this->items[$next];
191 }
192 return $current_item;
193 }
194
198 protected function findItemByRefId(int $ref_id) : array
199 {
200 foreach ($this->items as $index => $item) {
201 if ($item->getRefId() === $ref_id) {
202 return [$index, $item];
203 }
204 }
205 throw new \Exception("This is not a valid item.", 1);
206 }
207
208 protected function buildDefaultControls(
209 LSControlBuilder $control_builder,
210 LSLearnerItem $item,
211 int $item_position
212 ) : ControlBuilder {
213 $is_first = $item_position === 0;
214 $is_last = $item_position === count($this->items) - 1;
215
216 if (!$control_builder->getExitControl()) {
217 $cmd = self::LSO_CMD_SUSPEND;
218 if ($is_last) {
219 $cmd = self::LSO_CMD_FINISH;
220 }
221 $control_builder = $control_builder->exit($cmd);
222 }
223
224 if (!$control_builder->getPreviousControl()) {
225 $direction_prev = -1;
226 $cmd = ''; //disables control
227
228 if (!$is_first) {
229 $available = $this->getNextItem($item, $direction_prev)
230 ->getAvailability() === Step::AVAILABLE;
231
232 if ($available) {
233 $cmd = self::LSO_CMD_NEXT;
234 }
235 }
236
237 $control_builder = $control_builder
238 ->previous($cmd, $direction_prev);
239 }
240
241 if (!$control_builder->getNextControl()) {
242 $direction_next = 1;
243 $cmd = '';
244 if (!$is_last) {
245 $available = $this->getNextItem($item, $direction_next)
246 ->getAvailability() === Step::AVAILABLE;
247
248 if ($available) {
249 $cmd = self::LSO_CMD_NEXT;
250 }
251 }
252
253 $control_builder = $control_builder
254 ->next($cmd, $direction_next);
255 }
256
257 return $control_builder;
258 }
259
260 protected function renderComponentView(
261 $state,
262 ILIAS\KioskMode\View $view
263 ) {
264 $component = $view->render(
265 $state,
266 $this->ui_factory,
267 $this->url_builder,
268 []
269 );
270 return $component;
271 }
272}
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
Class LSControlBuilder.
previous(string $command, int $parameter=null)
A previous control allows the user to go back to the previous item in the object.The $parameter can b...
exit(string $command)
An exit control allows the user to gracefully leave the object providing the kiosk mode....
next(string $command, int $parameter=null)
A next control allows the user to progress to the next item in the object.The $parameter can be used ...
getRefId()
Definition: LSItem.php:129
Add learning progress and availability information to the LSItem.
Class LSUrlBuilder.
Class ilKioskPageRenderer.
Builds the overview (curriculum) of a LearningSequence.
Implementation of KioskMode Player.
const PARAM_LSO_COMMAND
getNextItem(LSLearnerItem $current_item, int $direction)
$direction is either -1 or 1;
storeState(ILIAS\KioskMode\State $state, int $state_item_ref_id, int $current_item_ref_id)
const LSO_CMD_SUSPEND
updateViewState(ILIAS\KioskMode\State $state, ILIAS\KioskMode\View $view, array $get, array $post=null)
render(array $get, array $post=null)
findItemByRefId(int $ref_id)
buildDefaultControls(LSControlBuilder $control_builder, LSLearnerItem $item, int $item_position)
__construct(int $lso_ref_id, string $lso_title, int $usr_id, array $items, ilLSStateDB $state_db, LSControlBuilder $control_builder, LSUrlBuilder $url_builder, ilLSCurriculumBuilder $curriculum_builder, ilLSViewFactory $view_factory, ilKioskPageRenderer $renderer, ILIAS\UI\Factory $ui_factory)
const LSO_CMD_FINISH
const PARAM_LSO_PARAMETER
renderComponentView( $state, ILIAS\KioskMode\View $view)
Persistence for View-States.
Build controls for the view.
This describes a Workflow Step.
Definition: Step.php:13
$index
Definition: metadata.php:60
Class BaseForm.
Class Factory.
$post
Definition: post.php:34