ILIAS  release_4-3 Revision
 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 = new ilChart("objstacc", 700, 500);
243 
244  $legend = new ilChartLegend();
245  $chart->setLegend($legend);
246 
247  $max_value = 0;
248  foreach($this->getData() as $object)
249  {
250  if(in_array($object["obj_id"], $a_graph_items))
251  {
252  $series = new ilChartData("lines");
253  $series->setLabel(ilObject::_lookupTitle($object["obj_id"]));
254 
255  if(strpos($this->filter["yearmonth"], "-") === false)
256  {
257  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $idx => $num)
258  {
259  $value = (int)$object["month_".$num];
260  $max_value = max($max_value, $value);
261  if($this->filter["measure"] != "spent_seconds")
262  {
263  $value = $this->anonymizeValue($value, true);
264  }
265  $series->addPoint($idx, $value);
266  }
267  }
268  else
269  {
270  for($loop = 1; $loop<32; $loop++)
271  {
272  $value = (int)$object["day_".$loop];
273  $max_value = max($max_value, $value);
274  if($this->filter["measure"] != "spent_seconds")
275  {
276  $value = $this->anonymizeValue($value, true);
277  }
278  $series->addPoint($loop, $value);
279  }
280  }
281 
282  $chart->addData($series);
283  }
284  }
285 
286  $value_ticks = $this->buildValueScale($max_value, ($this->filter["measure"] != "spent_seconds"),
287  ($this->filter["measure"] == "spent_seconds"));
288 
289  $labels = array();
290  if(strpos($this->filter["yearmonth"], "-") === false)
291  {
292  foreach(array_values($this->getMonthsYear($this->filter["yearmonth"], true)) as $idx => $caption)
293  {
294  $labels[$idx] = $caption;
295  }
296  }
297  else
298  {
299  for($loop = 1; $loop<32; $loop++)
300  {
301  $labels[$loop] = $loop.".";
302  }
303  }
304  $chart->setTicks($labels, $value_ticks, true);
305 
306  return $chart->getHTML();
307  }
308 
309  protected function fillMetaExcel()
310  {
311 
312  }
313 
314  protected function fillRowExcel($a_worksheet, &$a_row, $a_set)
315  {
316  $a_worksheet->write($a_row, 0, ilObject::_lookupTitle($a_set["obj_id"]));
317  $a_worksheet->write($a_row, 1, $a_set["obj_id"]);
318 
319  $col = 1;
320  if(strpos($this->filter["yearmonth"], "-") === false)
321  {
322  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $num)
323  {
324  $value = (int)$a_set["month_".$num];
325  if($this->filter["measure"] != "spent_seconds")
326  {
327  $value = $this->anonymizeValue($value);
328  }
329 
330  $col++;
331  $a_worksheet->write($a_row, $col, $value);
332  }
333  }
334 
335  if($this->filter["measure"] == "spent_seconds")
336  {
337  // keep seconds
338  // $sum = $this->formatSeconds((int)$a_set["total"]);
339  $sum = (int)$a_set["total"];
340  }
341  else
342  {
343  $sum = $this->anonymizeValue((int)$a_set["total"]);
344  }
345  $col++;
346  $a_worksheet->write($a_row, $col, $sum);
347  }
348 
349  protected function fillMetaCSV()
350  {
351 
352  }
353 
354  protected function fillRowCSV($a_csv, $a_set)
355  {
356  $a_csv->addColumn(ilObject::_lookupTitle($a_set["obj_id"]));
357  $a_csv->addColumn($a_set["obj_id"]);
358 
359  if(strpos($this->filter["yearmonth"], "-") === false)
360  {
361  foreach(array_keys($this->getMonthsYear($this->filter["yearmonth"])) as $num)
362  {
363  $value = (int)$a_set["month_".$num];
364  if($this->filter["measure"] != "spent_seconds")
365  {
366  $value = $this->anonymizeValue($value);
367  }
368 
369  $a_csv->addColumn($value);
370  }
371  }
372 
373  if($this->filter["measure"] == "spent_seconds")
374  {
375  // keep seconds
376  // $sum = $this->formatSeconds((int)$a_set["total"]);
377  $sum = (int)$a_set["total"];
378  }
379  else
380  {
381  $sum = $this->anonymizeValue((int)$a_set["total"]);
382  }
383  $a_csv->addColumn($sum);
384 
385  $a_csv->addRow();
386  }
387 }
388 
389 ?>