ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLearningHistoryGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 {
13  const TAB_ID_LEARNING_HISTORY = 'lhist_learning_history';
14  const TAB_ID_MY_CERTIFICATES = 'certificates';
15  const MAX = 50;
16 
20  protected $ctrl;
21 
25  protected $main_tpl;
26 
30  protected $lng;
31 
35  protected $ui;
36 
39 
41  protected $tabs;
42 
44  protected $show_more = false;
45 
47  protected $last_ts = 0;
48 
52  public function __construct()
53  {
54  global $DIC;
55 
56  $this->ctrl = $DIC->ctrl();
57 
58  $this->lhist_service = $DIC->learningHistory();
59  $this->ui = $this->lhist_service->ui();
60  $this->main_tpl = $this->ui->mainTemplate();
61  $this->lng = $this->lhist_service->language();
62  $this->access = $this->lhist_service->access();
63  $this->tabs = $DIC->tabs();
64 
65  $this->lng->loadLanguageModule("lhist");
66 
67  $this->user_id = $this->lhist_service->user()->getId();
68 
69  $this->certificateSettings = new ilSetting("certificate");
70 
71  $this->from = null;
72  $this->to = ((int) $_GET["to_ts"] > 0)
73  ? (int) $_GET["to_ts"]
74  : null;
75 
76  $this->main_tpl->addJavaScript("./Services/LearningHistory/js/LearningHistory.js");
77  }
78 
84  public function setUserId($user_id)
85  {
86  $this->user_id = $user_id;
87  }
88 
89 
93  public function executeCommand()
94  {
96 
97  $next_class = $ctrl->getNextClass($this);
98  $cmd = $ctrl->getCmd("show");
99 
100  switch ($next_class) {
101  default:
102  if (in_array($cmd, array("show", "renderAsync"))) {
103  $this->$cmd();
104  }
105  }
106  }
107 
108 
112  protected function show()
113  {
115  $lng = $this->lng;
116  $f = $this->ui->factory();
117  $renderer = $this->ui->renderer();
118 
119  $html = $this->getHistoryHtml($this->from, $this->to);
120 
121  if ($html != "") {
122  $main_tpl->setContent($html);
123  } else {
124  $main_tpl->setContent(
125  $renderer->render(
126  $f->messageBox()->info($lng->txt("lhist_no_entries"))
127  )
128  );
129  }
130  }
131 
135  protected function renderAsync()
136  {
137  $response["timeline"] = $this->renderTimeline($this->from, $this->to);
138  $response["more"] = $this->show_more ? $this->renderButton() : "";
139  echo json_encode($response);
140  exit;
141  }
142 
152  public function getEmbeddedHTML($from = null, $to = null, $classes = null, $a_mode = null)
153  {
154  $ctrl = $this->ctrl;
155 
156  return $ctrl->getHTML($this, ["from" => $from, "to" => $to, "classes" => $classes, "mode" => $a_mode]);
157  }
158 
165  public function getHTML($par)
166  {
167  return $this->getHistoryHtml($par["from"], $par["to"], $par["classes"], $par["mode"]);
168  }
169 
175  protected function getHistoryHtml($from = null, $to = null, $classes = null, $mode = null)
176  {
177  $tpl = new ilTemplate("tpl.timeline.html", true, true, "Services/LearningHistory");
178 
179  $tpl->setVariable("TIMELINE", $this->renderTimeline($from, $to, $classes, $mode));
180 
181  if ($this->show_more && $mode != "print") {
182  $tpl->setCurrentBlock("show_more");
183  $tpl->setVariable("SHOW_MORE_BUTTON", $this->renderButton());
184  $tpl->parseCurrentBlock();
185  }
186 
187  return $tpl->get();
188  }
189 
198  protected function renderTimeline(int $from = null, int $to = null, array $classes = null, string $mode = null) : string
199  {
200  $collector = $this->lhist_service->factory()->collector();
201  $ctrl = $this->ctrl;
202 
203  $to = (is_null($to))
204  ? time()
205  : $to;
206  $from = (is_null($from))
207  ? $to - (365 * 24 * 60 * 60)
208  : $from;
209 
210  $entries = $collector->getEntries($from, $to, $this->user_id, $classes);
211 
212  $timeline = ilTimelineGUI::getInstance();
213  $cnt = 0;
214 
215  reset($entries);
217  while (($e = current($entries)) && $cnt < self::MAX) {
218  $timeline->addItem(new ilLearningHistoryTimelineItem(
219  $e,
220  $this->ui,
221  $this->user_id,
222  $this->access,
223  $this->lhist_service->repositoryTree()
224  ));
225  $this->last_ts = $e->getTimestamp();
226  next($entries);
227  $cnt++;
228  }
229 
230  $html = "";
231  if (count($entries) > 0) {
232  $html = $timeline->render($ctrl->isAsynch());
233  }
234 
235  $this->show_more = (count($entries) > $cnt);
236 
237  return $html;
238  }
239 
240 
244  protected function renderButton()
245  {
246  $ctrl = $this->ctrl;
247  $f = $this->ui->factory();
248  $renderer = $this->ui->renderer();
249  $ctrl->setParameter($this, "to_ts", $this->last_ts - 1);
250  $url = $ctrl->getLinkTarget($this, "renderAsync", "", true);
251 
252  $button = $f->button()->standard($this->lng->txt("lhist_show_more"), "")
254  ->withOnLoadCode(function ($id) use ($url) {
255  return "il.LearningHistory.initShowMore('$id', '" . $url . "');";
256  });
257  if ($ctrl->isAsynch()) {
258  return $renderer->renderAsync($button);
259  } else {
260  return $renderer->render($button);
261  }
262  }
263 }
getEmbeddedHTML($from=null, $to=null, $classes=null, $a_mode=null)
Get HTML.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
$tpl
Definition: ilias.php:10
Learning history main GUI class.
if(!array_key_exists('StateId', $_REQUEST)) $id
$from
special template class to simplify handling of ITX/PEAR
withLoadingAnimationOnClick(bool $loading_animation_on_click=true)
getHistoryHtml($from=null, $to=null, $classes=null, $mode=null)
Get history html.
setUserId($user_id)
Set user id.
exit
Definition: backend.php:16
static getInstance()
Get instance.
$url
$response
$html
Definition: example_001.php:87