ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilWikiStatGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "Modules/Wiki/classes/class.ilWikiStat.php";
6 
15 {
16  protected $wiki_id; // [integer]
17  protected $page_id; // [integer]
18 
19  public function __construct($a_wiki_id, $a_page_id = null)
20  {
21  $this->wiki_id = (int)$a_wiki_id;
22  $this->page_id = (int)$a_page_id;
23  }
24 
25  public function executeCommand()
26  {
27  global $ilCtrl;
28 
29  $next_class = $ilCtrl->getNextClass($this);
30  $cmd = $ilCtrl->getCmd("view");
31 
32  switch($next_class)
33  {
34  default:
35  $this->$cmd();
36  break;
37  }
38  }
39 
40  protected function viewToolbar($a_is_initial = false)
41  {
42  global $ilToolbar, $lng, $ilCtrl;
43 
44  $current_figure = (int)$_POST["fig"];
45  $current_time_frame = (string)$_POST["tfr"];
46  $current_scope = (int)$_POST["scp"];
47 
48  include_once "Services/Form/classes/class.ilPropertyFormGUI.php";
49  $view = new ilSelectInputGUI($lng->txt("wiki_stat_figure"), "fig");
50  $view->setOptions($this->page_id
53  if($current_figure)
54  {
55  $view->setValue($current_figure);
56  }
57  else if($a_is_initial)
58  {
59  // default
60  $current_figure = $this->page_id
63  }
64  $ilToolbar->addInputItem($view, true);
65 
66  $options = array();
67  include_once "Services/Calendar/classes/class.ilCalendarUtil.php";
68  $lng->loadLanguageModule("dateplaner");
69  foreach(ilWikiStat::getAvailableMonths($this->wiki_id) as $month)
70  {
71  $parts = explode("-", $month);
72  $options[$month] = ilCalendarUtil::_numericMonthToString((int)$parts[1]).
73  " ".$parts[0];
74  }
75  krsort($options);
76 
77  $tframe = new ilSelectInputGUI($lng->txt("month"), "tfr");
78  $tframe->setOptions($options);
79  if($current_time_frame)
80  {
81  $tframe->setValue($current_time_frame);
82  }
83  else if($a_is_initial)
84  {
85  $current_time_frame = array_shift(array_keys($options)); // default
86  }
87  $ilToolbar->addInputItem($tframe, true);
88 
89  $scope = new ilSelectInputGUI($lng->txt("wiki_stat_scope"), "scp");
90  $scope->setOptions(array(
91  1 => "1 ".$lng->txt("month"),
92  2 => "2 ".$lng->txt("months"),
93  3 => "3 ".$lng->txt("months"),
94  4 => "4 ".$lng->txt("months"),
95  5 => "5 ".$lng->txt("months"),
96  6 => "6 ".$lng->txt("months")
97  ));
98  if($current_scope)
99  {
100  $scope->setValue($current_scope);
101  }
102  else if($a_is_initial)
103  {
104  $current_scope = 1; // default
105  }
106  $ilToolbar->addInputItem($scope, true);
107 
108  $ilToolbar->setFormAction($ilCtrl->getFormAction($this, "view"));
109  $ilToolbar->addFormButton($lng->txt("show"), "view");
110 
111  if($current_figure && $current_time_frame && $current_scope)
112  {
113  $ilToolbar->addSeparator();
114  $ilToolbar->addFormButton($lng->txt("export"), "export");
115 
116  return array(
117  "figure" => $current_figure,
118  "month" => $current_time_frame,
119  "scope" => $current_scope
120  );
121  }
122  }
123 
124  protected function export()
125  {
126  global $ilCtrl;
127 
128  $params = $this->viewToolbar();
129  if($params)
130  {
131  // data
132 
133  $tfr = explode("-", (string)$params["month"]);
134  $day_from = date("Y-m-d", mktime(0, 0, 1, $tfr[1]-($params["scope"]-1), 1, $tfr[0]));
135  $day_to = date("Y-m-d", mktime(0, 0, 1, $tfr[1]+1, 0, $tfr[0]));
136  unset($tfr);
137 
138  $chart_data = $this->getChartData($params["figure"], $params["scope"], $day_from, $day_to);
139 
140 
141  // excel
142 
144  new ilDate($day_from, IL_CAL_DATE),
145  new ilDate($day_to, IL_CAL_DATE));
146 
147  $filename = ilObject::_lookupTitle($this->wiki_id);
148  if($this->page_id)
149  {
150  $filename .= " - ".ilWikiPage::lookupTitle($this->page_id);
151  }
152  $filename .= " - ".ilWikiStat::getFigureTitle($params["figure"])." - ".$period.".xls";
153 
154  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
155  include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
156  $adapter = new ilExcelWriterAdapter($filename, true);
157  $workbook = $adapter->getWorkbook();
158  $worksheet = $workbook->addWorksheet();
159  // $worksheet->setLandscape();
160 
161  /*
162  $worksheet->setColumn(0, 0, 20);
163  $worksheet->setColumn(1, 1, 40);
164  */
165 
166  $row = 0;
167  foreach($chart_data as $day => $value)
168  {
169  $row++;
170 
171  $worksheet->writeString($row, 0, $day);
172  $worksheet->writeNumber($row, 1, $value);
173  }
174 
175  $workbook->close();
176  exit();
177  }
178 
179  $ilCtrl->redirect($this, "view");
180  }
181 
182  protected function initial()
183  {
184  $this->view(true);
185  }
186 
187  protected function view($a_is_initial = false)
188  {
189  global $tpl, $lng;
190 
191  $params = $this->viewToolbar($a_is_initial);
192  if(is_array($params))
193  {
194  // data
195 
196  $tfr = explode("-", (string)$params["month"]);
197  $day_from = date("Y-m-d", mktime(0, 0, 1, $tfr[1]-($params["scope"]-1), 1, $tfr[0]));
198  $day_to = date("Y-m-d", mktime(0, 0, 1, $tfr[1]+1, 0, $tfr[0]));
199  unset($tfr);
200 
201  $chart_data = $this->getChartData($params["figure"], $params["scope"], $day_from, $day_to);
202  $list_data = $this->getListData();
203 
204 
205  // render
206 
207  $vtpl = new ilTemplate("tpl.wiki_stat_list.html", true, true, "Modules/Wiki");
208 
209  include_once("./Services/UIComponent/Panel/classes/class.ilPanelGUI.php");
210  $chart_panel = ilPanelGUI::getInstance();
211 
212  $vtpl->setVariable("CHART", $this->renderGraph($params["figure"], $chart_data));
213 
214  $vtpl->setCurrentBlock("row_bl");
215  $counter = 0;
216  foreach($list_data as $figure => $values)
217  {
218  $day = (int)substr($day, 8);
219  $vtpl->setVariable("CSS_ROW", ($counter++%2) ? "tblrow1" : "tblrow2");
220  $vtpl->setVariable("FIGURE", $figure);
221  $vtpl->setVariable("YESTERDAY_VALUE", $values["yesterday"]);
222  $vtpl->setVariable("TODAY_VALUE", $values["today"]);
223  $vtpl->parseCurrentBlock();
224  }
225 
226  $vtpl->setVariable("FIGURE_HEAD", $lng->txt("wiki_stat_figure"));
227  $vtpl->setVariable("YESTERDAY_HEAD", $lng->txt("yesterday"));
228  $vtpl->setVariable("TODAY_HEAD", $lng->txt("today"));
229 
230  $chart_panel->setHeading($lng->txt("statistics"));
231  $chart_panel->setBody($vtpl->get());
232  $chart_panel->setHeadingStyle(ilPanelGUI::HEADING_STYLE_SUBHEADING);
233 
234  $tpl->setContent($chart_panel->getHTML());
235  }
236  }
237 
238  protected function getChartData($a_figure, $a_scope, $a_from, $a_to)
239  {
240  $data = array();
241 
242  $raw = $this->page_id
243  ? ilWikiStat::getFigureDataPage($this->wiki_id, $this->page_id, $a_figure, $a_from, $a_to)
244  : ilWikiStat::getFigureData($this->wiki_id, $a_figure, $a_from, $a_to);
245 
246  $parts = explode("-", $a_from);
247  for($loop = 0; $loop <= ($a_scope*31); $loop++)
248  {
249  $current_day = date("Y-m-d", mktime(0, 0, 1, $parts[1], $parts[2]+$loop, $parts[0]));
250  if($current_day <= $a_to)
251  {
252  $data[$current_day] = (float)$raw[$current_day];
253  }
254  }
255 
256  return $data;
257  }
258 
259  protected function getListData()
260  {
261  $data = array();
262 
263  $today = date("Y-m-d");
264  $yesterday = date("Y-m-d", strtotime("yesterday"));
265 
266  $all = $this->page_id
269  foreach($all as $figure => $title)
270  {
271  if($this->page_id)
272  {
273  $tmp = (array)ilWikiStat::getFigureDataPage($this->wiki_id, $this->page_id, $figure, $yesterday, $today);
274  }
275  else
276  {
277  $tmp = (array)ilWikiStat::getFigureData($this->wiki_id, $figure, $yesterday, $today);
278  }
279  $data[$title] = array(
280  "yesterday" => (float)$tmp[$yesterday],
281  "today" => (float)$tmp[$today]
282  );
283  }
284 
285  return $data;
286  }
287 
288  protected function renderGraph($a_figure, array $a_data)
289  {
290  $scope = ceil(sizeof($a_data)/31);
291 
292  include_once "Services/Chart/classes/class.ilChartGrid.php";
293  $chart = ilChart::getInstanceByType(ilChart::TYPE_GRID, "wikistat");
294  $chart->setSize("100%", 400);
295  $chart->setColors(array("#C0E0FF"));
296 
297  $legend = new ilChartLegend();
298  $chart->setLegend($legend);
299 
300  // lines vs. bars
301  if(in_array($a_figure, array(
302  // wiki
315  // page
321  )))
322  {
323  $series = $chart->getDataInstance(ilChartGrid::DATA_LINES);
324  $series->setLineSteps(true);
325  $series->setFill(true, "#E0F0FF");
326  }
327  else
328  {
329  $series = $chart->getDataInstance(ilChartGrid::DATA_BARS);
330  $series->setBarOptions(round(10/($scope*2))/10);
331  }
332  $series->setLabel(ilWikiStat::getFigureTitle($a_figure));
333 
334  $labels = array();
335  $x = 0;
336  foreach($a_data as $date => $value)
337  {
338  $series->addPoint($x, $value);
339 
340  $day = (int)substr($date, 8, 2);
341 
342  // match scale to scope
343  if($scope == 1)
344  {
345  // daily
346  $labels[$x] = substr($date, 8, 2);
347  }
348  elseif($scope == 2)
349  {
350  // weekly
351  if(!($x%7))
352  {
353  $labels[$x] = substr($date, 8, 2).".".substr($date, 5, 2).".";
354  }
355  }
356  else
357  {
358  // 1st/15th
359  if($day == 1 || $day == 15 || $x == sizeof($a_data)-1)
360  {
361  $labels[$x] = substr($date, 8, 2).".".substr($date, 5, 2).".";
362  }
363  }
364 
365  $x++;
366  }
367 
368  $chart->addData($series);
369  $chart->setTicks($labels, null, true);
370 
371  // int vs. float (averages)
372  if(in_array($a_figure, array(
373  // wiki
386  // page
396  )))
397  {
398  $chart->setYAxisToInteger(true);
399  }
400 
401  return $chart->getHTML();
402  }
403 }
404 
405 ?>