ILIAS  Release_4_2_x_branch Revision 61807
 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  if(strpos($this->filter["yearmonth"], "-") === false)
37  {
38  for($loop = 1; $loop<13; $loop++)
39  {
40  $this->addColumn($lng->txt("month_".str_pad($loop, 2, "0", STR_PAD_LEFT)."_short"), "month_".$loop, "", false, "ilRight");
41  }
42  }
43  $this->addColumn($lng->txt("total"), "total", "", false, "ilRight");
44 
45  $this->setTitle($this->lng->txt("trac_object_stat_access"));
46 
47  // $this->setSelectAllCheckbox("item_id");
48  $this->addMultiCommand("showAccessGraph", $lng->txt("trac_show_graph"));
49  $this->setResetCommand("resetAccessFilter");
50  $this->setFilterCommand("applyAccessFilter");
51 
52  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
53  $this->setRowTemplate("tpl.lp_object_statistics_row.html", "Services/Tracking");
54  $this->setEnableHeader(true);
55  $this->setEnableNumInfo(true);
56  $this->setEnableTitle(true);
57  $this->setDefaultOrderField("title");
58  $this->setDefaultOrderDirection("asc");
59 
60  $this->setExportFormats(array(self::EXPORT_EXCEL, self::EXPORT_CSV));
61 
62  include_once("./Services/Tracking/classes/class.ilLPObjSettings.php");
63  include_once "Services/Tracking/classes/class.ilTrQuery.php";
64 
66  $info_date = ilDatePresentation::formatDate(new ilDateTime($info["tstamp"], IL_CAL_UNIX));
67  $link = " <a href=\"".$ilCtrl->getLinkTarget($a_parent_obj, "admin")."\">&raquo;".
68  $lng->txt("trac_log_info_link")."</a>";
69  ilUtil::sendInfo(sprintf($lng->txt("trac_log_info"), $info_date, $info["counter"]).$link);
70 
71  if($a_load_items)
72  {
73  $this->getItems();
74  }
75  }
76 
77  public function numericOrdering($a_field)
78  {
79  $fields = array();
80  $fields[] = "total";
81 
82  if(strpos($this->filter["yearmonth"], "-") === false)
83  {
84  for($loop = 1; $loop<13; $loop++)
85  {
86  $fields[] = "month_".$loop;
87  }
88  }
89 
90  if(in_array($a_field, $fields))
91  {
92  return true;
93  }
94  return false;
95  }
96 
100  public function initFilter()
101  {
102  global $lng;
103 
104  $this->setDisableFilterHiding(true);
105 
106  // object type selection
107  include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
108  $si = new ilSelectInputGUI($lng->txt("obj_type"), "type");
109  $si->setOptions($this->getPossibleTypes(true, false, true));
110  $this->addFilterItem($si);
111  $si->readFromSession();
112  if(!$si->getValue())
113  {
114  $si->setValue("crs");
115  }
116  $this->filter["type"] = $si->getValue();
117 
118  // title/description
119  include_once("./Services/Form/classes/class.ilTextInputGUI.php");
120  $ti = new ilTextInputGUI($lng->txt("trac_title_description"), "query");
121  $ti->setMaxLength(64);
122  $ti->setSize(20);
123  $this->addFilterItem($ti);
124  $ti->readFromSession();
125  $this->filter["query"] = $ti->getValue();
126 
127  // read_count/spent_seconds
128  $si = new ilSelectInputGUI($lng->txt("trac_figure"), "figure");
129  $si->setOptions(array("read_count"=>$lng->txt("trac_read_count"),
130  "spent_seconds"=>$lng->txt("trac_spent_seconds")));
131  $this->addFilterItem($si);
132  $si->readFromSession();
133  if(!$si->getValue())
134  {
135  $si->setValue("read_count");
136  }
137  $this->filter["measure"] = $si->getValue();
138 
139  // year/month
140  $si = new ilSelectInputGUI($lng->txt("year")." / ".$lng->txt("month"), "yearmonth");
141  $options = array();
142  for($loop = 0; $loop < 10; $loop++)
143  {
144  $year = date("Y")-$loop;
145  $options[$year] = $year;
146  for($loop2 = 12; $loop2 > 0; $loop2--)
147  {
148  $month = str_pad($loop2, 2, "0", STR_PAD_LEFT);
149  if($year.$month <= date("Ym"))
150  {
151  $options[$year."-".$month] = $year." / ".
152  $lng->txt("month_".$month."_long");
153  }
154  }
155  }
156  $si->setOptions($options);
157  $this->addFilterItem($si);
158  $si->readFromSession();
159  if(!$si->getValue())
160  {
161  $si->setValue(date("Y-m"));
162  }
163  $this->filter["yearmonth"] = $si->getValue();
164  }
165 
166  function getItems()
167  {
168  $data = array();
169 
170  $objects = $this->searchObjects($this->getCurrentFilter(true), "read");
171  if($objects)
172  {
173  include_once "Services/Tracking/classes/class.ilTrQuery.php";
174 
175  $yearmonth = explode("-", $this->filter["yearmonth"]);
176  if(sizeof($yearmonth) == 1)
177  {
178  foreach(ilTrQuery::getObjectAccessStatistics($objects, $yearmonth[0]) as $obj_id => $months)
179  {
180  $data[$obj_id]["obj_id"] = $obj_id;
181  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
182 
183  foreach($months as $month => $values)
184  {
185  $data[$obj_id]["month_".$month] = (int)$values[$this->filter["measure"]];
186  $data[$obj_id]["total"] += (int)$values[$this->filter["measure"]];
187  }
188  }
189  }
190  else
191  {
192  foreach(ilTrQuery::getObjectAccessStatistics($objects, $yearmonth[0], (int)$yearmonth[1]) as $obj_id => $days)
193  {
194  $data[$obj_id]["obj_id"] = $obj_id;
195  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
196 
197  foreach($days as $day => $values)
198  {
199  $data[$obj_id]["day_".$day] = (int)$values[$this->filter["measure"]];
200  $data[$obj_id]["total"] += (int)$values[$this->filter["measure"]];
201  }
202  }
203  }
204 
205  // add objects with no usage data
206  foreach($objects as $obj_id => $ref_ids)
207  {
208  if(!isset($data[$obj_id]))
209  {
210  $data[$obj_id]["obj_id"] = $obj_id;
211  $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
212  }
213  }
214  }
215 
216  $this->setData($data);
217  }
218 
222  protected function fillRow($a_set)
223  {
224  global $ilCtrl;
225 
226  $type = ilObject::_lookupType($a_set["obj_id"]);
227 
228  $this->tpl->setVariable("OBJ_ID", $a_set["obj_id"]);
229  $this->tpl->setVariable("ICON_SRC", ilUtil::getTypeIconPath($type, $a_set["obj_id"], "tiny"));
230  $this->tpl->setVariable("ICON_ALT", $this->lng->txt($type));
231  $this->tpl->setVariable("TITLE_TEXT", $a_set["title"]);
232 
233  if($this->preselected && in_array($a_set["obj_id"], $this->preselected))
234  {
235  $this->tpl->setVariable("CHECKBOX_STATE", " checked=\"checked\"");
236  }
237 
238  $sum = 0;
239  if(strpos($this->filter["yearmonth"], "-") === false)
240  {
241  $this->tpl->setCurrentBlock("month");
242  for($loop = 1; $loop<13; $loop++)
243  {
244  $value = (int)$a_set["month_".$loop];
245  if($this->filter["measure"] == "read_count")
246  {
247  $value = $this->anonymizeValue($value);
248  }
249  else if($this->filter["measure"] == "spent_seconds")
250  {
251  $value = $this->formatSeconds($value, true);
252  }
253  $this->tpl->setVariable("MONTH_VALUE", $value);
254  $this->tpl->parseCurrentBlock();
255  }
256  }
257 
258  if($this->filter["measure"] == "spent_seconds")
259  {
260  $sum = $this->formatSeconds((int)$a_set["total"], true);
261  }
262  else
263  {
264  $sum = $this->anonymizeValue((int)$a_set["total"]);
265  }
266  $this->tpl->setVariable("TOTAL", $sum);
267  }
268 
269  function getGraph(array $a_graph_items)
270  {
271  global $lng;
272 
273  include_once "Services/Chart/classes/class.ilChart.php";
274  $chart = new ilChart("objstacc", 700, 500);
275 
276  $legend = new ilChartLegend();
277  $chart->setLegend($legend);
278 
279  $max_value = 0;
280  foreach($this->getData() as $object)
281  {
282  if(in_array($object["obj_id"], $a_graph_items))
283  {
284  $series = new ilChartData("lines");
285  $series->setLabel(ilObject::_lookupTitle($object["obj_id"]));
286 
287  if(strpos($this->filter["yearmonth"], "-") === false)
288  {
289  for($loop = 1; $loop<13; $loop++)
290  {
291  $value = (int)$object["month_".$loop];
292  $max_value = max($max_value, $value);
293  if($this->filter["measure"] == "read_count")
294  {
295  $value = $this->anonymizeValue($value, true);
296  }
297  $series->addPoint($loop, $value);
298  }
299  }
300  else
301  {
302  for($loop = 1; $loop<32; $loop++)
303  {
304  $value = (int)$object["day_".$loop];
305  $max_value = max($max_value, $value);
306  if($this->filter["measure"] == "read_count")
307  {
308  $value = $this->anonymizeValue($value, true);
309  }
310  $series->addPoint($loop, $value);
311  }
312  }
313 
314  $chart->addData($series);
315  }
316  }
317 
318  $value_ticks = $this->buildValueScale($max_value, ($this->filter["measure"] == "read_count"),
319  ($this->filter["measure"] == "spent_seconds"));
320 
321  $labels = array();
322  if(strpos($this->filter["yearmonth"], "-") === false)
323  {
324  for($loop = 1; $loop<13; $loop++)
325  {
326  $labels[$loop] = $lng->txt("month_".str_pad($loop, 2, "0", STR_PAD_LEFT)."_short");
327  }
328  }
329  else
330  {
331  for($loop = 1; $loop<32; $loop++)
332  {
333  $labels[$loop] = $loop.".";
334  }
335  }
336  $chart->setTicks($labels, $value_ticks, true);
337 
338  return $chart->getHTML();
339  }
340 
341  protected function fillMetaExcel()
342  {
343 
344  }
345 
346  protected function fillRowExcel($a_worksheet, &$a_row, $a_set)
347  {
348  $a_worksheet->write($a_row, 0, ilObject::_lookupTitle($a_set["obj_id"]));
349 
350  $col = 0;
351  if(strpos($this->filter["yearmonth"], "-") === false)
352  {
353  for($loop = 1; $loop<13; $loop++)
354  {
355  $value = (int)$a_set["month_".$loop];
356  if($this->filter["measure"] == "read_count")
357  {
358  $value = $this->anonymizeValue($value);
359  }
360  else if($this->filter["measure"] == "spent_seconds")
361  {
362  // keep seconds
363  // $value = $this->formatSeconds($value);
364  }
365 
366  $col++;
367  $a_worksheet->write($a_row, $col, $value);
368  }
369  }
370 
371  if($this->filter["measure"] == "spent_seconds")
372  {
373  // keep seconds
374  // $sum = $this->formatSeconds((int)$a_set["total"]);
375  $sum = (int)$a_set["total"];
376  }
377  else
378  {
379  $sum = $this->anonymizeValue((int)$a_set["total"]);
380  }
381  $col++;
382  $a_worksheet->write($a_row, $col, $sum);
383  }
384 
385  protected function fillMetaCSV()
386  {
387 
388  }
389 
390  protected function fillRowCSV($a_csv, $a_set)
391  {
392  $a_csv->addColumn(ilObject::_lookupTitle($a_set["obj_id"]));
393 
394  if(strpos($this->filter["yearmonth"], "-") === false)
395  {
396  for($loop = 1; $loop<13; $loop++)
397  {
398  $value = (int)$a_set["month_".$loop];
399  if($this->filter["measure"] == "read_count")
400  {
401  $value = $this->anonymizeValue($value);
402  }
403  else if($this->filter["measure"] == "spent_seconds")
404  {
405  // keep seconds
406  // $value = $this->formatSeconds($value);
407  }
408 
409  $a_csv->addColumn($value);
410  }
411  }
412 
413  if($this->filter["measure"] == "spent_seconds")
414  {
415  // keep seconds
416  // $sum = $this->formatSeconds((int)$a_set["total"]);
417  $sum = (int)$a_set["total"];
418  }
419  else
420  {
421  $sum = $this->anonymizeValue((int)$a_set["total"]);
422  }
423  $a_csv->addColumn($sum);
424 
425  $a_csv->addRow();
426  }
427 }
428 
429 ?>