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