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