ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLPObjectStatisticsTableGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/Tracking/classes/class.ilLPTableBaseGUI.php");
5 
16 {
20  function __construct($a_parent_obj, $a_parent_cmd, array $a_preselect = null, $a_load_items = true)
21  {
22  global $ilCtrl, $lng;
23 
24  $this->preselected = $a_preselect;
25 
26  $this->setId("lpobjstattbl");
27 
28  parent::__construct($a_parent_obj, $a_parent_cmd);
29 
30  $this->setShowRowsSelector(true);
31  // $this->setLimit(ilSearchSettings::getInstance()->getMaxHits());
32  $this->initFilter();
33 
34  $this->addColumn("", "", "1", true);
35  $this->addColumn($lng->txt("trac_title"), "title");
36  $this->addColumn($lng->txt("object_id"), "obj_id");
37  if(strpos($this->filter["yearmonth"], "-") === false)
38  {
39  foreach($this->getMonthsYear($this->filter["yearmonth"]) as $num => $caption)
40  {
41  $this->addColumn($caption, "month_".$num, "", false, "ilRight");
42  }
43  }
44  $this->addColumn($lng->txt("total"), "total", "", false, "ilRight");
45 
46  $this->setTitle($this->lng->txt("trac_object_stat_access"));
47 
48  // $this->setSelectAllCheckbox("item_id");
49  $this->addMultiCommand("showAccessGraph", $lng->txt("trac_show_graph"));
50  $this->setResetCommand("resetAccessFilter");
51  $this->setFilterCommand("applyAccessFilter");
52 
53  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
54  $this->setRowTemplate("tpl.lp_object_statistics_row.html", "Services/Tracking");
55  $this->setEnableHeader(true);
56  $this->setEnableNumInfo(true);
57  $this->setEnableTitle(true);
58  $this->setDefaultOrderField("title");
59  $this->setDefaultOrderDirection("asc");
60 
61  $this->setExportFormats(array(self::EXPORT_EXCEL, self::EXPORT_CSV));
62 
63  if($a_load_items)
64  {
65  $this->getItems();
66  }
67  }
68 
69  public function numericOrdering($a_field)
70  {
71  if($a_field != "title")
72  {
73  return true;
74  }
75  return false;
76  }
77 
81  public function initFilter()
82  {
83  global $lng;
84 
85  $this->setDisableFilterHiding(true);
86 
87  // object type selection
88  include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
89  $si = new ilSelectInputGUI($lng->txt("obj_type"), "type");
90  $si->setOptions($this->getPossibleTypes(true, false, true));
91  $this->addFilterItem($si);
92  $si->readFromSession();
93  if(!$si->getValue())
94  {
95  $si->setValue("crs");
96  }
97  $this->filter["type"] = $si->getValue();
98 
99  // title/description
100  include_once("./Services/Form/classes/class.ilTextInputGUI.php");
101  $ti = new ilTextInputGUI($lng->txt("trac_title_description"), "query");
102  $ti->setMaxLength(64);
103  $ti->setSize(20);
104  $this->addFilterItem($ti);
105  $ti->readFromSession();
106  $this->filter["query"] = $ti->getValue();
107 
108  // read_count/spent_seconds
109  $si = new ilSelectInputGUI($lng->txt("trac_figure"), "figure");
110  $si->setOptions(array("read_count"=>$lng->txt("trac_read_count"),
111  "spent_seconds"=>$lng->txt("trac_spent_seconds"),
112  "users"=>$lng->txt("users")));
113  $this->addFilterItem($si);
114  $si->readFromSession();
115  if(!$si->getValue())
116  {
117  $si->setValue("read_count");
118  }
119  $this->filter["measure"] = $si->getValue();
120 
121  // year/month
122  $si = new ilSelectInputGUI($lng->txt("year")." / ".$lng->txt("month"), "yearmonth");
123  $si->setOptions($this->getMonthsFilter());
124  $this->addFilterItem($si);
125  $si->readFromSession();
126  if(!$si->getValue())
127  {
128  $si->setValue(date("Y-m"));
129  }
130  $this->filter["yearmonth"] = $si->getValue();
131  }
132 
133  function getItems()
134  {
135  $data = array();
136 
137  $objects = $this->searchObjects($this->getCurrentFilter(true), "read");
138  if($objects)
139  {
140  include_once "Services/Tracking/classes/class.ilTrQuery.php";
141 
142  $yearmonth = explode("-", $this->filter["yearmonth"]);
143  if(sizeof($yearmonth) == 1)
144  {
145  foreach(ilTrQuery::getObjectAccessStatistics($objects, $yearmonth[0]) as $obj_id => $months)
146  {
147  $data[$obj_id]["obj_id"] = $obj_id;
148  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
149 
150  foreach($months as $month => $values)
151  {
152  $idx = $yearmonth[0]."-".str_pad($month, 2, "0", STR_PAD_LEFT);
153  $data[$obj_id]["month_".$idx] = (int)$values[$this->filter["measure"]];
154  $data[$obj_id]["total"] += (int)$values[$this->filter["measure"]];
155  }
156  }
157  }
158  else
159  {
160  foreach(ilTrQuery::getObjectAccessStatistics($objects, $yearmonth[0], (int)$yearmonth[1]) as $obj_id => $days)
161  {
162  $data[$obj_id]["obj_id"] = $obj_id;
163  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
164 
165  foreach($days as $day => $values)
166  {
167  $data[$obj_id]["day_".$day] = (int)$values[$this->filter["measure"]];
168  $data[$obj_id]["total"] += (int)$values[$this->filter["measure"]];
169  }
170  }
171  }
172 
173  // add objects with no usage data
174  foreach(array_keys($objects) as $obj_id)
175  {
176  if(!isset($data[$obj_id]))
177  {
178  $data[$obj_id]["obj_id"] = $obj_id;
179  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
180  }
181  }
182  }
183 
184  $this->setData($data);
185  }
186 
190  protected function fillRow($a_set)
191  {
192  global $ilCtrl;
193 
194  $type = ilObject::_lookupType($a_set["obj_id"]);
195 
196  $this->tpl->setVariable("OBJ_ID", $a_set["obj_id"]);
197  $this->tpl->setVariable("ICON_SRC", ilObject::_getIcon("", "tiny", $type));
198  $this->tpl->setVariable("ICON_ALT", $this->lng->txt($type));
199  $this->tpl->setVariable("TITLE_TEXT", $a_set["title"]);
200 
201  if($this->preselected && in_array($a_set["obj_id"], $this->preselected))
202  {
203  $this->tpl->setVariable("CHECKBOX_STATE", " checked=\"checked\"");
204  }
205 
206  $sum = 0;
207  if(strpos($this->filter["yearmonth"], "-") === false)
208  {
209  $this->tpl->setCurrentBlock("month");
210  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $num)
211  {
212  $value = (int)$a_set["month_".$num];
213  if($this->filter["measure"] != "spent_seconds")
214  {
215  $value = $this->anonymizeValue($value);
216  }
217  else
218  {
219  $value = $this->formatSeconds($value, true);
220  }
221  $this->tpl->setVariable("MONTH_VALUE", $value);
222  $this->tpl->parseCurrentBlock();
223  }
224  }
225 
226  if($this->filter["measure"] == "spent_seconds")
227  {
228  $sum = $this->formatSeconds((int)$a_set["total"], true);
229  }
230  else
231  {
232  $sum = $this->anonymizeValue((int)$a_set["total"]);
233  }
234  $this->tpl->setVariable("TOTAL", $sum);
235  }
236 
237  function getGraph(array $a_graph_items)
238  {
239  global $lng;
240 
241  include_once "Services/Chart/classes/class.ilChart.php";
242  $chart = ilChart::getInstanceByType(ilChart::TYPE_GRID, "objstacc");
243  $chart->setSize(700, 500);
244 
245  $legend = new ilChartLegend();
246  $chart->setLegend($legend);
247 
248  $max_value = 0;
249  foreach($this->getData() as $object)
250  {
251  if(in_array($object["obj_id"], $a_graph_items))
252  {
253  $series = $chart->getDataInstance(ilChartGrid::DATA_LINES);
254  $series->setLabel(ilObject::_lookupTitle($object["obj_id"]));
255 
256  if(strpos($this->filter["yearmonth"], "-") === false)
257  {
258  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $idx => $num)
259  {
260  $value = (int)$object["month_".$num];
261  $max_value = max($max_value, $value);
262  if($this->filter["measure"] != "spent_seconds")
263  {
264  $value = $this->anonymizeValue($value, true);
265  }
266  $series->addPoint($idx, $value);
267  }
268  }
269  else
270  {
271  for($loop = 1; $loop<32; $loop++)
272  {
273  $value = (int)$object["day_".$loop];
274  $max_value = max($max_value, $value);
275  if($this->filter["measure"] != "spent_seconds")
276  {
277  $value = $this->anonymizeValue($value, true);
278  }
279  $series->addPoint($loop, $value);
280  }
281  }
282 
283  $chart->addData($series);
284  }
285  }
286 
287  $value_ticks = $this->buildValueScale($max_value, ($this->filter["measure"] != "spent_seconds"),
288  ($this->filter["measure"] == "spent_seconds"));
289 
290  $labels = array();
291  if(strpos($this->filter["yearmonth"], "-") === false)
292  {
293  foreach(array_values($this->getMonthsYear($this->filter["yearmonth"], true)) as $idx => $caption)
294  {
295  $labels[$idx] = $caption;
296  }
297  }
298  else
299  {
300  for($loop = 1; $loop<32; $loop++)
301  {
302  $labels[$loop] = $loop.".";
303  }
304  }
305  $chart->setTicks($labels, $value_ticks, true);
306 
307  return $chart->getHTML();
308  }
309 
310  protected function fillMetaExcel()
311  {
312 
313  }
314 
315  protected function fillRowExcel($a_worksheet, &$a_row, $a_set)
316  {
317  $a_worksheet->write($a_row, 0, ilObject::_lookupTitle($a_set["obj_id"]));
318  $a_worksheet->write($a_row, 1, $a_set["obj_id"]);
319 
320  $col = 1;
321  if(strpos($this->filter["yearmonth"], "-") === false)
322  {
323  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $num)
324  {
325  $value = (int)$a_set["month_".$num];
326  if($this->filter["measure"] != "spent_seconds")
327  {
328  $value = $this->anonymizeValue($value);
329  }
330 
331  $col++;
332  $a_worksheet->write($a_row, $col, $value);
333  }
334  }
335 
336  if($this->filter["measure"] == "spent_seconds")
337  {
338  // keep seconds
339  // $sum = $this->formatSeconds((int)$a_set["total"]);
340  $sum = (int)$a_set["total"];
341  }
342  else
343  {
344  $sum = $this->anonymizeValue((int)$a_set["total"]);
345  }
346  $col++;
347  $a_worksheet->write($a_row, $col, $sum);
348  }
349 
350  protected function fillMetaCSV()
351  {
352 
353  }
354 
355  protected function fillRowCSV($a_csv, $a_set)
356  {
357  $a_csv->addColumn(ilObject::_lookupTitle($a_set["obj_id"]));
358  $a_csv->addColumn($a_set["obj_id"]);
359 
360  if(strpos($this->filter["yearmonth"], "-") === false)
361  {
362  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $num)
363  {
364  $value = (int)$a_set["month_".$num];
365  if($this->filter["measure"] != "spent_seconds")
366  {
367  $value = $this->anonymizeValue($value);
368  }
369 
370  $a_csv->addColumn($value);
371  }
372  }
373 
374  if($this->filter["measure"] == "spent_seconds")
375  {
376  // keep seconds
377  // $sum = $this->formatSeconds((int)$a_set["total"]);
378  $sum = (int)$a_set["total"];
379  }
380  else
381  {
382  $sum = $this->anonymizeValue((int)$a_set["total"]);
383  }
384  $a_csv->addColumn($sum);
385 
386  $a_csv->addRow();
387  }
388 }
389 
390 ?>