ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLSPlayer.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
6 
13 {
14  const PARAM_LSO_COMMAND = 'lsocmd';
15  const PARAM_LSO_PARAMETER = 'lsov';
16 
17  const LSO_CMD_NEXT = 'lsonext'; //with param directions
18  const LSO_CMD_GOTO = 'lsogoto'; //with param ref_id
19  const LSO_CMD_SUSPEND = 'lsosuspend';
20  const LSO_CMD_FINISH = 'lsofinish';
21 
22  const GS_DATA_LS_KIOSK_MODE = 'ls_kiosk_mode';
23  const GS_DATA_LS_CONTENT = 'ls_content';
24  const GS_DATA_LS_MAINBARCONTROLS = 'ls_mainbar_controls';
25  const GS_DATA_LS_METABARCONTROLS = 'ls_metabar_controls';
26 
27  const RET_EXIT = 'EXIT::';
28  const RET_NOITEMS = 'NOITEMS';
29 
30  public function __construct(
31  string $lso_title,
32  ilLSLearnerItemsQueries $ls_items,
33  LSControlBuilder $control_builder,
34  LSUrlBuilder $url_builder,
35  ilLSCurriculumBuilder $curriculum_builder,
36  ilLSViewFactory $view_factory,
37  ilKioskPageRenderer $renderer,
38  ILIAS\UI\Factory $ui_factory,
39  ScreenContext $current_context
40  ) {
41  $this->lso_title = $lso_title;
42  $this->ls_items = $ls_items;
43  $this->control_builder = $control_builder;
44  $this->url_builder = $url_builder;
45  $this->curriculum_builder = $curriculum_builder;
46  $this->view_factory = $view_factory;
47  $this->page_renderer = $renderer;
48  $this->ui_factory = $ui_factory;
49  $this->current_context = $current_context;
50  }
51 
52  public function play(array $get, array $post = null)
53  {
54  //init state and current item
55  $items = $this->ls_items->getItems();
56  if (count($items) === 0) {
57  return self::RET_NOITEMS;
58  }
59  $current_item = $this->getCurrentItem($items);
60 
61  while ($current_item->getAvailability() !== \ILIAS\UI\Component\Listing\Workflow\Step::AVAILABLE) {
62  $prev_item = $this->getNextItem($items, $current_item, -1);
63  if ($prev_item === $current_item) {
64  throw new \Exception("Cannot view first LSO-item", 1);
65  }
66  $current_item = $prev_item;
67  }
68 
69  $view = $this->view_factory->getViewFor($current_item);
70  $state = $this->ls_items->getStateFor($current_item, $view);
71  $state = $this->updateViewState($state, $view, $get, $post);
72  $items = $this->ls_items->getItems(); //reload items after update viewState
73 
74  $current_item_ref_id = $current_item->getRefId();
75  //now, digest parameter:
76  $command = $_GET[self::PARAM_LSO_COMMAND];
77  $param = (int) $_GET[self::PARAM_LSO_PARAMETER];
78 
79  switch ($command) {
80  case self::LSO_CMD_SUSPEND:
81  case self::LSO_CMD_FINISH:
82  //store state and exit
83  $this->ls_items->storeState($state, $current_item_ref_id, $current_item_ref_id);
84  return 'EXIT::' . $command;
85  case self::LSO_CMD_NEXT:
86  $next_item = $this->getNextItem($items, $current_item, $param);
87  if ($next_item->getAvailability() !== \ILIAS\UI\Component\Listing\Workflow\Step::AVAILABLE) {
88  $next_item = $current_item;
89  }
90  break;
91  case self::LSO_CMD_GOTO:
92  list($position, $next_item) = $this->findItemByRefId($items, $param);
93  break;
94  default: //view-internal / unknown command
95  $next_item = $current_item;
96  }
97  //write State to DB
98  $this->ls_items->storeState($state, $current_item_ref_id, $next_item->getRefId());
99 
100  //get proper view
101  if ($next_item != $current_item) {
102  $view = $this->view_factory->getViewFor($next_item);
103  $state = $this->ls_items->getStateFor($next_item, $view);
104  }
105 
106  //content
107  $obj_title = $next_item->getTitle();
108  $icon = $this->ui_factory->symbol()->icon()
109  ->standard($next_item->getType(), $next_item->getType(), 'medium');
110 
111  $content = $this->renderComponentView($state, $view);
112 
113  $panel = $this->ui_factory->panel()->standard(
114  '', //panel_title
115  $content
116  );
117  $content = [$panel];
118 
119  $items = $this->ls_items->getItems(); //reload items after renderComponentView content
120 
121  //get position
122  list($item_position, $item) = $this->findItemByRefId($items, $next_item->getRefId());
123 
124  //have the view build controls
125  $control_builder = $this->control_builder;
126  $view->buildControls($state, $control_builder);
127 
128  //amend controls not set by the view
129  $control_builder = $this->buildDefaultControls($control_builder, $item, $item_position, $items);
130 
131  $rendered_body = $this->page_renderer->render(
132  $this->lso_title,
133  $control_builder,
134  $obj_title,
135  $icon,
136  $content
137  );
138 
139  $metabar_controls = [
140  'exit' => $control_builder->getExitControl()
141  ];
142 
143  //curriculum
144  $curriculum_slate = $this->page_renderer->buildCurriculumSlate(
145  $this->curriculum_builder
146  ->getLearnerCurriculum(true)
147  ->withActive($item_position)
148  );
149  $mainbar_controls = [
150  'curriculum' => $curriculum_slate
151  ];
152 
153  //ToC
154  $toc = $control_builder->getToc();
155  if ($toc) {
156  $toc_slate = $this->page_renderer->buildToCSlate($toc, $icon);
157  $mainbar_controls['toc'] = $toc_slate;
158  }
159 
160  $cc = $this->current_context;
161  $cc->addAdditionalData(self::GS_DATA_LS_KIOSK_MODE, true);
162  $cc->addAdditionalData(self::GS_DATA_LS_METABARCONTROLS, $metabar_controls);
163  $cc->addAdditionalData(self::GS_DATA_LS_MAINBARCONTROLS, $mainbar_controls);
164  $cc->addAdditionalData(self::GS_DATA_LS_CONTENT, $rendered_body);
165  return;
166  }
167 
171  protected function getCurrentItem(array $items) : LSLearnerItem
172  {
173  $current_item = $items[0];
174  $current_item_ref_id = $this->ls_items->getCurrentItemRefId();
175  if ($current_item_ref_id !== 0) {
176  $valid_ref_ids = array_map(
177  function ($item) {
178  return $item->getRefId();
179  },
180  array_values($this->ls_items->getItems())
181  );
182  if (in_array($current_item_ref_id, $valid_ref_ids)) {
183  list($position, $current_item) = $this->findItemByRefId($items, $current_item_ref_id);
184  }
185  }
186  return $current_item;
187  }
188 
189  protected function updateViewState(
190  ILIAS\KioskMode\State $state,
191  ILIAS\KioskMode\View $view,
192  array $get,
193  array $post = null
194  ) : ILIAS\KioskMode\State {
195  //get view internal command
196  $command = $_GET[self::PARAM_LSO_COMMAND];
197  $param = (int) $_GET[self::PARAM_LSO_PARAMETER];
198  if (!is_null($command)) {
199  $state = $view->updateGet($state, $command, $param);
200  }
201  return $state;
202  }
203 
207  protected function getNextItem(array $items, LSLearnerItem $current_item, int $direction) : LSLearnerItem
208  {
209  list($position, $item) = $this->findItemByRefId($items, $current_item->getRefId());
210  $next = $position + $direction;
211  if ($next >= 0 && $next < count($items)) {
212  return $items[$next];
213  }
214  return $current_item;
215  }
216 
220  protected function findItemByRefId(array $items, int $ref_id) : array
221  {
222  foreach ($items as $index => $item) {
223  if ($item->getRefId() === $ref_id) {
224  return [$index, $item];
225  }
226  }
227  throw new \Exception("This is not a valid item.", 1);
228  }
229 
230  protected function buildDefaultControls(
231  LSControlBuilder $control_builder,
232  LSLearnerItem $item,
233  int $item_position,
234  array $items
235  ) : ControlBuilder {
236  $is_first = $item_position === 0;
237  $is_last = $item_position === count($items) - 1;
238 
239  if (!$control_builder->getExitControl()) {
240  $cmd = self::LSO_CMD_SUSPEND;
241  if ($is_last) {
242  $cmd = self::LSO_CMD_FINISH;
243  }
244  $control_builder = $control_builder->exit($cmd);
245  }
246 
247  if (!$control_builder->getPreviousControl()) {
248  $direction_prev = -1;
249  $cmd = ''; //disables control
250 
251  if (!$is_first) {
252  $available = $this->getNextItem($items, $item, $direction_prev)
253  ->getAvailability() === Step::AVAILABLE;
254 
255  if ($available) {
256  $cmd = self::LSO_CMD_NEXT;
257  }
258  }
259 
260  $control_builder = $control_builder
261  ->previous($cmd, $direction_prev);
262  }
263 
264  if (!$control_builder->getNextControl()) {
265  $direction_next = 1;
266  $cmd = '';
267  if (!$is_last) {
268  $available = $this->getNextItem($items, $item, $direction_next)
269  ->getAvailability() === Step::AVAILABLE;
270 
271  if ($available) {
272  $cmd = self::LSO_CMD_NEXT;
273  }
274  }
275 
276  $control_builder = $control_builder
277  ->next($cmd, $direction_next);
278  }
279 
280  return $control_builder;
281  }
282 
283  protected function renderComponentView(
284  $state,
285  ILIAS\KioskMode\View $view
286  ) {
287  $component = $view->render(
288  $state,
289  $this->ui_factory,
290  $this->url_builder,
291  []
292  );
293  return $component;
294  }
295 
296 
298  {
299  $item = $this->getCurrentItem($this->ls_items->getItems());
300  return $item->getLearningProgressStatus();
301  }
302 }
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...
buildDefaultControls(LSControlBuilder $control_builder, LSLearnerItem $item, int $item_position, array $items)
const GS_DATA_LS_KIOSK_MODE
Class Factory.
updateViewState(ILIAS\KioskMode\State $state, ILIAS\KioskMode\View $view, array $get, array $post=null)
getCurrentItem(array $items)
if(isset($_FILES['img_file']) &&is_array($_FILES['img_file'])) $panel
Definition: imgupload.php:138
$_GET["client_id"]
Class ChatMainBarProvider .
getRefId()
Definition: LSItem.php:129
Class LSUrlBuilder.
const LSO_CMD_SUSPEND
findItemByRefId(array $items, int $ref_id)
const PARAM_LSO_PARAMETER
getNextItem(array $items, LSLearnerItem $current_item, int $direction)
$direction is either -1 or 1;
$index
Definition: metadata.php:128
exit(string $command)
An exit control allows the user to gracefully leave the object providing the kiosk mode...
const LSO_CMD_FINISH
Class ilKioskPageRenderer.
$param
Definition: xapitoken.php:29
const GS_DATA_LS_MAINBARCONTROLS
This combines calls to ProgressDB and StateDB to handle learner-items in the context of a specific LS...
__construct(string $lso_title, ilLSLearnerItemsQueries $ls_items, LSControlBuilder $control_builder, LSUrlBuilder $url_builder, ilLSCurriculumBuilder $curriculum_builder, ilLSViewFactory $view_factory, ilKioskPageRenderer $renderer, ILIAS\UI\Factory $ui_factory, ScreenContext $current_context)
play(array $get, array $post=null)
Implementation of KioskMode Player.
Class LSControlBuilder.
renderComponentView( $state, ILIAS\KioskMode\View $view)
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 ...
const PARAM_LSO_COMMAND
Build controls for the view.
getCurrentItemLearningProgress()
const GS_DATA_LS_CONTENT
Builds the overview (curriculum) of a LearningSequence.
const GS_DATA_LS_METABARCONTROLS
Add learning progress and availability information to the LSItem.