ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilWikiStatGUI.php
Go to the documentation of this file.
1 <?php
2 
20 
27 {
28  protected \ILIAS\Wiki\InternalGUIService $gui;
30  protected ilCtrl $ctrl;
32  protected ilLanguage $lng;
34  protected int $wiki_id;
35  protected int $page_id;
36 
37  public function __construct(
38  int $a_wiki_id,
39  ?int $a_page_id = null
40  ) {
41  global $DIC;
42 
43  $this->ctrl = $DIC->ctrl();
44  $this->toolbar = $DIC->toolbar();
45  $this->lng = $DIC->language();
46  $this->tpl = $DIC["tpl"];
47  $this->wiki_id = $a_wiki_id;
48  $this->page_id = (int) $a_page_id;
49  $this->request = $DIC
50  ->wiki()
51  ->internal()
52  ->gui()
53  ->request();
54  $this->gui = $DIC->wiki()->internal()->gui();
55  }
56 
57  public function executeCommand(): void
58  {
59  $ilCtrl = $this->ctrl;
60 
61  $next_class = $ilCtrl->getNextClass($this);
62  $cmd = $ilCtrl->getCmd("view");
63 
64  switch ($next_class) {
65  default:
66  $this->$cmd();
67  break;
68  }
69  }
70 
71  protected function viewToolbar(
72  bool $a_is_initial = false
73  ): ?array {
74  $ilToolbar = $this->toolbar;
75  $lng = $this->lng;
76  $ilCtrl = $this->ctrl;
77 
78  $current_figure = $this->request->getStatFig();
79  $current_time_frame = $this->request->getStatTfr();
80  $current_scope = $this->request->getStatScp();
81 
82  $view = new ilSelectInputGUI($lng->txt("wiki_stat_figure"), "fig");
83  $view->setOptions($this->page_id
86  if ($current_figure) {
87  $view->setValue($current_figure);
88  } elseif ($a_is_initial) {
89  // default
90  $current_figure = $this->page_id
93  }
94  $ilToolbar->addInputItem($view, true);
95 
96  $options = array();
97  $lng->loadLanguageModule("dateplaner");
98  foreach (ilWikiStat::getAvailableMonths($this->wiki_id) as $month) {
99  $parts = explode("-", $month);
100  $options[$month] = ilCalendarUtil::_numericMonthToString((int) $parts[1]) .
101  " " . $parts[0];
102  }
103  krsort($options);
104 
105  $tframe = new ilSelectInputGUI($lng->txt("month"), "tfr");
106  $tframe->setOptions($options);
107  if ($current_time_frame) {
108  $tframe->setValue($current_time_frame);
109  } elseif ($a_is_initial) {
110  $opt = array_keys($options);
111  $current_time_frame = array_shift($opt); // default
112  }
113  $ilToolbar->addInputItem($tframe, true);
114 
115  $scope = new ilSelectInputGUI($lng->txt("wiki_stat_scope"), "scp");
116  $scope->setOptions(array(
117  1 => "1 " . $lng->txt("month"),
118  2 => "2 " . $lng->txt("months"),
119  3 => "3 " . $lng->txt("months"),
120  4 => "4 " . $lng->txt("months"),
121  5 => "5 " . $lng->txt("months"),
122  6 => "6 " . $lng->txt("months")
123  ));
124  if ($current_scope) {
125  $scope->setValue($current_scope);
126  } elseif ($a_is_initial) {
127  $current_scope = 1; // default
128  }
129  $ilToolbar->addInputItem($scope, true);
130 
131  $ilToolbar->setFormAction($ilCtrl->getFormAction($this, "view"));
132  $ilToolbar->addFormButton($lng->txt("show"), "view");
133 
134  if ($current_figure && $current_time_frame && $current_scope) {
135  $ilToolbar->addSeparator();
136  $ilToolbar->addFormButton($lng->txt("export"), "export");
137 
138  return array(
139  "figure" => $current_figure,
140  "month" => $current_time_frame,
141  "scope" => $current_scope
142  );
143  }
144  return null;
145  }
146 
147  protected function export(): void
148  {
149  $ilCtrl = $this->ctrl;
150 
151  $params = $this->viewToolbar();
152  if ($params) {
153  // data
154 
155  $tfr = explode("-", (string) $params["month"]);
156  $day_from = date("Y-m-d", mktime(0, 0, 1, $tfr[1] - ($params["scope"] - 1), 1, $tfr[0]));
157  $day_to = date("Y-m-d", mktime(0, 0, 1, $tfr[1] + 1, 0, $tfr[0]));
158  unset($tfr);
159 
160  $chart_data = $this->getChartData($params["figure"], $params["scope"], $day_from, $day_to);
161 
162 
163  // excel
164 
166  new ilDate($day_from, IL_CAL_DATE),
167  new ilDate($day_to, IL_CAL_DATE)
168  );
169 
170  $filename = ilObject::_lookupTitle($this->wiki_id);
171  if ($this->page_id) {
172  $filename .= " - " . ilWikiPage::lookupTitle($this->page_id);
173  }
174  $filename .= " - " . ilWikiStat::getFigureTitle($params["figure"]) . " - " . $period;
175 
176  $excel = new ilExcel();
177  $excel->addSheet($this->lng->txt("statistics"));
178 
179  $row = 1;
180  foreach ($chart_data as $day => $value) {
181  $excel->setCell($row, 0, $day);
182  $excel->setCell($row++, 1, $value);
183  }
184 
185  $excel->sendToClient($filename);
186  }
187 
188  $ilCtrl->redirect($this, "view");
189  }
190 
191  protected function initial(): void
192  {
193  $this->view(true);
194  }
195 
196  protected function view(
197  bool $a_is_initial = false
198  ): void {
199  $tpl = $this->tpl;
200  $lng = $this->lng;
201 
202  $params = $this->viewToolbar($a_is_initial);
203  if (is_array($params)) {
204  // data
205 
206  $tfr = explode("-", (string) $params["month"]);
207  $day_from = date("Y-m-d", mktime(0, 0, 1, $tfr[1] - ($params["scope"] - 1), 1, $tfr[0]));
208  $day_to = date("Y-m-d", mktime(0, 0, 1, $tfr[1] + 1, 0, $tfr[0]));
209  unset($tfr);
210 
211  $chart_data = $this->getChartData($params["figure"], $params["scope"], $day_from, $day_to);
212  $list_data = $this->getListData();
213 
214 
215  // render
216 
217  $vtpl = new ilTemplate("tpl.wiki_stat_list.html", true, true, "components/ILIAS/Wiki");
218 
219  $vtpl->setVariable("CHART", $this->renderGraph($params["figure"], $chart_data));
220 
221  $vtpl->setCurrentBlock("row_bl");
222  foreach ($list_data as $figure => $values) {
223  $vtpl->setVariable("FIGURE", $figure);
224  $vtpl->setVariable("YESTERDAY_VALUE", $values["yesterday"]);
225  $vtpl->setVariable("TODAY_VALUE", $values["today"]);
226  $vtpl->parseCurrentBlock();
227  }
228 
229  $vtpl->setVariable("FIGURE_HEAD", $lng->txt("wiki_stat_figure"));
230  $vtpl->setVariable("YESTERDAY_HEAD", $lng->txt("yesterday"));
231  $vtpl->setVariable("TODAY_HEAD", $lng->txt("today"));
232 
233  $f = $this->gui->ui()->factory();
234  $r = $this->gui->ui()->renderer();
235  $p = $f->panel()->standard(
236  $lng->txt("statistics"),
237  $f->legacy()->content($vtpl->get())
238  );
239  $tpl->setContent($r->render($p));
240  }
241  }
242 
243  protected function getChartData(
244  int $a_figure,
245  int $a_scope,
246  string $a_from,
247  string $a_to
248  ): array {
249  $data = array();
250 
251  $raw = $this->page_id
252  ? ilWikiStat::getFigureDataPage($this->wiki_id, $this->page_id, $a_figure, $a_from, $a_to)
253  : ilWikiStat::getFigureData($this->wiki_id, $a_figure, $a_from, $a_to);
254 
255  $parts = explode("-", $a_from);
256  for ($loop = 0; $loop <= ($a_scope * 31); $loop++) {
257  $current_day = date("Y-m-d", mktime(0, 0, 1, $parts[1], $parts[2] + $loop, $parts[0]));
258  if ($current_day <= $a_to) {
259  $data[$current_day] = (float) ($raw[$current_day] ?? 0);
260  }
261  }
262 
263  return $data;
264  }
265 
266  protected function getListData(): array
267  {
268  $data = array();
269 
270  $today = date("Y-m-d");
271  $yesterday = date("Y-m-d", strtotime("yesterday"));
272 
273  $all = $this->page_id
276  foreach ($all as $figure => $title) {
277  if ($this->page_id) {
278  $tmp = ilWikiStat::getFigureDataPage($this->wiki_id, $this->page_id, $figure, $yesterday, $today);
279  } else {
280  $tmp = ilWikiStat::getFigureData($this->wiki_id, $figure, $yesterday, $today);
281  }
282  $data[$title] = array(
283  "yesterday" => (float) ($tmp[$yesterday] ?? 0),
284  "today" => (float) ($tmp[$today] ?? 0)
285  );
286  }
287 
288  return $data;
289  }
290 
291  protected function renderGraph(
292  int $a_figure,
293  array $a_data
294  ): string {
295  $scope = ceil(count($a_data) / 31);
296 
297  $chart = ilChart::getInstanceByType(ilChart::TYPE_GRID, "wikistat");
298  $chart->setSize("100%", "400");
299  $chart->setColors(array("#C0E0FF"));
300 
301  $legend = new ilChartLegend();
302  $chart->setLegend($legend);
303 
304  // lines vs. bars
305  if (in_array($a_figure, array(
306  // wiki
319  // page
325  ), true)) {
326  $series = $chart->getDataInstance(ilChartGrid::DATA_LINES);
327  $series->setLineSteps(true);
328  $series->setFill(true, "#E0F0FF");
329  } else {
330  $series = $chart->getDataInstance(ilChartGrid::DATA_BARS);
331  $series->setBarOptions(round(10 / ($scope * 2)) / 10);
332  }
333  $series->setLabel(ilWikiStat::getFigureTitle($a_figure));
334 
335  $labels = array();
336  $x = 0;
337  foreach ($a_data as $date => $value) {
338  $series->addPoint($x, $value);
339 
340  $day = (int) substr($date, 8, 2);
341 
342  // match scale to scope
343  if ($scope == 1) {
344  // daily
345  $labels[$x] = substr($date, 8, 2);
346  } elseif ($scope == 2) {
347  // weekly
348  if (!($x % 7)) {
349  $labels[$x] = substr($date, 8, 2) . "." . substr($date, 5, 2) . ".";
350  }
351  } elseif ($day === 1 || $day === 15 || $x === count($a_data) - 1) {
352  // 1st/15th
353  $labels[$x] = substr($date, 8, 2) . "." . substr($date, 5, 2) . ".";
354  }
355 
356  $x++;
357  }
358 
359  $chart->addData($series);
360  $chart->setTicks($labels, null, true);
361 
362  // int vs. float (averages)
363  if (in_array($a_figure, array(
364  // wiki
377  // page
387  ), true)) {
388  $chart->setYAxisToInteger(true);
389  }
390 
391  return $chart->getHTML();
392  }
393 }
static getFigureTitle(int $a_figure)
const KEY_FIGURE_WIKI_PAGE_USER_EDIT
const KEY_FIGURE_WIKI_NUM_RATING
const KEY_FIGURE_WIKI_WORDS
const KEY_FIGURE_WIKI_FOOTNOTES
const KEY_FIGURE_WIKI_USER_EDIT_PAGES
const KEY_FIGURE_WIKI_PAGE_RATINGS
$scope
Definition: ltiregstart.php:47
ilGlobalTemplateInterface $tpl
This class represents a selection list property in a property form.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _numericMonthToString(int $a_month, bool $a_long=true, ?ilLanguage $lng=null)
numeric month to string
const KEY_FIGURE_WIKI_PAGE_READ
const KEY_FIGURE_WIKI_FOOTNOTES_AVG
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
const KEY_FIGURE_WIKI_PAGE_INTERNAL_LINKS
Wiki statistics GUI class.
static lookupTitle(int $a_page_id, string $lang="-")
const KEY_FIGURE_WIKI_INTERNAL_LINKS
const KEY_FIGURE_WIKI_NUM_PAGES
const KEY_FIGURE_WIKI_PAGE_CHARS
const KEY_FIGURE_WIKI_PAGE_EXTERNAL_LINKS
const KEY_FIGURE_WIKI_EXTERNAL_LINKS_AVG
setContent(string $a_html)
Sets content for standard template.
__construct(int $a_wiki_id, ?int $a_page_id=null)
const KEY_FIGURE_WIKI_PAGE_FOOTNOTES
loadLanguageModule(string $a_module)
Load language module.
setOptions(array $a_options)
viewToolbar(bool $a_is_initial=false)
view(bool $a_is_initial=false)
const KEY_FIGURE_WIKI_EXTERNAL_LINKS
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getFigureDataPage(int $a_wiki_id, int $a_page_id, int $a_figure, string $a_from, string $a_to)
getNextClass($a_gui_class=null)
static _lookupTitle(int $obj_id)
getChartData(int $a_figure, int $a_scope, string $a_from, string $a_to)
static getFigureData(int $a_wiki_id, int $a_figure, string $a_from, string $a_to)
static getFigureOptions()
global $DIC
Definition: shib_login.php:22
const KEY_FIGURE_WIKI_INTERNAL_LINKS_AVG
ilToolbarGUI $toolbar
const KEY_FIGURE_WIKI_RATING_AVG
static getAvailableMonths(int $a_wiki_id)
const KEY_FIGURE_WIKI_CHARS
const TYPE_GRID
$filename
Definition: buildRTE.php:78
renderGraph(int $a_figure, array $a_data)
ILIAS Wiki InternalGUIService $gui
const IL_CAL_DATE
const KEY_FIGURE_WIKI_NEW_PAGES
const KEY_FIGURE_WIKI_EDIT_PAGES
const KEY_FIGURE_WIKI_CHARS_AVG
const KEY_FIGURE_WIKI_PAGE_WORDS
static getFigureOptionsPage()
WikiGUIRequest $request
const KEY_FIGURE_WIKI_READ_PAGES
const KEY_FIGURE_WIKI_WORDS_AVG
const KEY_FIGURE_WIKI_DELETED_PAGES
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
const KEY_FIGURE_WIKI_PAGE_CHANGES
static getInstanceByType(int $a_type, string $a_id)
$r