ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilTrSummaryTableGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/Tracking/classes/class.ilLPTableBaseGUI.php");
5 
15 {
19  function __construct($a_parent_obj, $a_parent_cmd, $a_ref_id, $a_print_mode = false)
20  {
21  global $ilCtrl, $lng, $ilAccess, $lng;
22 
23  $this->setId("trsmy");
24 
25  $this->ref_id = $a_ref_id;
26  $this->obj_id = ilObject::_lookupObjId($a_ref_id);
27 
28  parent::__construct($a_parent_obj, $a_parent_cmd);
29 
30  if($a_print_mode)
31  {
32  $this->setPrintMode(true);
33  }
34 
35  $this->parseTitle($this->obj_id, "trac_summary");
36  $this->setLimit(9999);
37  $this->setShowTemplates(true);
38  $this->setExportFormats(array(self::EXPORT_CSV, self::EXPORT_EXCEL));
39 
40  $this->addColumn($this->lng->txt("title"), "title");
41 
42  $this->setDefaultOrderField("title");
43 
44  $labels = $this->getSelectableColumns();
45  foreach ($this->getSelectedColumns() as $c)
46  {
47  $this->addColumn($labels[$c]["txt"], $c);
48  }
49 
50  // $this->setExternalSorting(true);
51  $this->setEnableHeader(true);
52  $this->setFormAction($ilCtrl->getFormActionByClass(get_class($this)));
53  $this->setRowTemplate("tpl.trac_summary_row.html", "Services/Tracking");
54  $this->initFilter($a_parent_obj->getObjId());
55 
56 
57  $this->getItems($a_parent_obj->getObjId(), $a_ref_id);
58  }
59 
61  {
62  global $lng, $ilSetting;
63 
64  $lng_map = array("user_total" => "users", "first_access_min" => "trac_first_access",
65  "last_access_max" => "trac_last_access", "mark" => "trac_mark", "status" => "trac_status",
66  "spent_seconds_avg" => "trac_spent_seconds", "percentage_avg" => "trac_percentage",
67  "read_count_sum" => "trac_read_count", "read_count_avg" => "trac_read_count",
68  );
69 
70 
71  $all = array("user_total");
72  $default = array();
73 
74  // show only if extended data was activated in lp settings
75  include_once 'Services/Tracking/classes/class.ilObjUserTracking.php';
76  $tracking = new ilObjUserTracking();
77  if($tracking->hasExtendedData(ilObjUserTracking::EXTENDED_DATA_READ_COUNT))
78  {
79  $all[] = "read_count_sum";
80  $all[] = "read_count_avg";
81  $default[] = "read_count_sum";
82  }
83  if($tracking->hasExtendedData(ilObjUserTracking::EXTENDED_DATA_SPENT_SECONDS))
84  {
85  $all[] = "spent_seconds_avg";
86  $default[] = "spent_seconds_avg";
87  }
88 
89  $all[] = "percentage_avg";
90  $all[] = "status";
91  $all[] = "mark";
92 
93  $privacy = array("gender", "city", "country", "sel_country");
94  foreach($privacy as $field)
95  {
96  if($ilSetting->get("usr_settings_course_export_".$field))
97  {
98  $all[] = $field;
99  }
100  }
101 
102  $all[] = "language";
103 
104  $default[] = "percentage_avg";
105  $default[] = "status";
106  $default[] = "mark";
107 
108  if($tracking->hasExtendedData(ilObjUserTracking::EXTENDED_DATA_LAST_ACCESS))
109  {
110  $all[] = "first_access_min";
111  $all[] = "last_access_max";
112  }
113 
114  $all[] = "create_date_min";
115  $all[] = "create_date_max";
116 
117 
118  $columns = array();
119  foreach($all as $column)
120  {
121  $l = $column;
122 
123  $prefix = false;
124  if(substr($l, -3) == "avg")
125  {
126  $prefix = "&#216; ";
127  }
128  else if(substr($l, -3) == "sum" || $l == "user_total")
129  {
130  $prefix = "&#8721; ";
131  }
132 
133  if(isset($lng_map[$l]))
134  {
135  $l = $lng_map[$l];
136  }
137 
138  $txt = $prefix.$lng->txt($l);
139 
140  $columns[$column] = array(
141  "txt" => $txt,
142  "default" => (in_array($column, $default) ? true :false)
143  );
144  }
145  return $columns;
146  }
147 
151  function initFilter($a_obj_id)
152  {
153  global $lng, $ilSetting;
154 
155  $item = $this->addFilterItemByMetaType("user_total", ilTable2GUI::FILTER_NUMBER_RANGE, true);
156  $this->filter["user_total"] = $item->getValue();
157 
158  $item = $this->addFilterItemByMetaType("read_count", ilTable2GUI::FILTER_NUMBER_RANGE, true,
159  $lng->txt("trac_read_count"));
160  $this->filter["read_count"] = $item->getValue();
161 
162  $item = $this->addFilterItemByMetaType("spent_seconds", ilTable2GUI::FILTER_DURATION_RANGE,
163  true, $lng->txt("trac_spent_seconds"));
164  $this->filter["spent_seconds"]["from"] = $item->getCombinationItem("from")->getValueInSeconds();
165  $this->filter["spent_seconds"]["to"] = $item->getCombinationItem("to")->getValueInSeconds();
166 
167  $item = $this->addFilterItemByMetaType("percentage", ilTable2GUI::FILTER_NUMBER_RANGE, true,
168  $lng->txt("trac_percentage"));
169  $this->filter["percentage"] = $item->getValue();
170 
171  include_once "Services/Tracking/classes/class.ilLPStatus.php";
172  $item = $this->addFilterItemByMetaType("status", ilTable2GUI::FILTER_SELECT, true);
173  $item->setOptions(array("" => $lng->txt("trac_all"),
177  LP_STATUS_FAILED_NUM+1 => $lng->txt(LP_STATUS_FAILED)));
178  $this->filter["status"] = $item->getValue();
179  if($this->filter["status"])
180  {
181  $this->filter["status"]--;
182  }
183 
184  $item = $this->addFilterItemByMetaType("mark", ilTable2GUI::FILTER_TEXT, true,
185  $lng->txt("trac_mark"));
186  $this->filter["mark"] = $item->getValue();
187 
188  if($ilSetting->get("usr_settings_course_export_gender"))
189  {
190  $item = $this->addFilterItemByMetaType("gender", ilTable2GUI::FILTER_SELECT, true);
191  $item->setOptions(array("" => $lng->txt("trac_all"), "m" => $lng->txt("gender_m"),
192  "f" => $lng->txt("gender_f")));
193  $this->filter["gender"] = $item->getValue();
194  }
195 
196  if($ilSetting->get("usr_settings_course_export_city"))
197  {
198  $item = $this->addFilterItemByMetaType("city", ilTable2GUI::FILTER_TEXT, true);
199  $this->filter["city"] = $item->getValue();
200  }
201 
202  if($ilSetting->get("usr_settings_course_export_country"))
203  {
204  $item = $this->addFilterItemByMetaType("country", ilTable2GUI::FILTER_TEXT, true);
205  $this->filter["country"] = $item->getValue();
206  }
207 
208  if($ilSetting->get("usr_settings_course_export_sel_country"))
209  {
210  $item = $this->addFilterItemByMetaType("sel_country", ilTable2GUI::FILTER_SELECT, true);
211  $item->setOptions(array("" => $lng->txt("trac_all"))+$this->getSelCountryCodes());
212  $this->filter["sel_country"] = $item->getValue();
213  }
214 
215  $item = $this->addFilterItemByMetaType("language", ilTable2GUI::FILTER_LANGUAGE, true);
216  $this->filter["language"] = $item->getValue();
217 
218  $item = $this->addFilterItemByMetaType("trac_first_access", ilTable2GUI::FILTER_DATE_RANGE, true);
219  $this->filter["first_access"] = $item->getDate();
220 
221  $item = $this->addFilterItemByMetaType("trac_last_access", ilTable2GUI::FILTER_DATE_RANGE, true);
222  $this->filter["last_access"] = $item->getDate();
223 
224  $item = $this->addFilterItemByMetaType("registration_filter", ilTable2GUI::FILTER_DATE_RANGE, true);
225  $this->filter["registration"] = $item->getDate();
226  }
227 
229  {
230  global $lng;
231 
232  include_once("./Services/Utilities/classes/class.ilCountry.php");
233  $options = array();
234  foreach (ilCountry::getCountryCodes() as $c)
235  {
236  $options[$c] = $lng->txt("meta_c_".$c);
237  }
238  asort($options);
239  return $options;
240  }
241 
248  function getItems($a_object_id, $a_ref_id)
249  {
250  global $lng;
251 
252  include_once("./Services/Tracking/classes/class.ilTrQuery.php");
253 
255  $a_object_id,
256  $a_ref_id,
260  ilUtil::stripSlashes($this->getLimit()),
261  $this->getCurrentFilter(),
262  $this->getSelectedColumns()
263  );
264 
265  $rows = array();
266  foreach($data["set"] as $idx => $result)
267  {
268  // sessions have no title
269  if($result["title"] == "" && $result["type"] == "sess")
270  {
271  include_once "Modules/Session/classes/class.ilObjSession.php";
272  $sess = new ilObjSession($result["obj_id"], false);
273  $data["set"][$idx]["title"] = $sess->getFirstAppointment()->appointmentToString();
274  }
275 
276  // percentages
277  $users_no = $result["user_total"];
278  $data["set"][$idx]["country"] = $this->getItemsPercentages($result["country"], $users_no);
279  $data["set"][$idx]["gender"] = $this->getItemsPercentages($result["gender"], $users_no, array("m"=>$lng->txt("gender_m"), "f"=>$lng->txt("gender_f")));
280  $data["set"][$idx]["city"] = $this->getItemsPercentages($result["city"], $users_no);
281  $data["set"][$idx]["sel_country"] = $this->getItemsPercentages($result["sel_country"], $users_no, $this->getSelCountryCodes());
282 
283  $languages = array();
284  foreach ($lng->getInstalledLanguages() as $lang_key)
285  {
286  $languages[$lang_key] = $lng->txt("lang_".$lang_key);
287  }
288  $data["set"][$idx]["language"] = $this->getItemsPercentages($result["language"], $users_no, $languages);
289 
290  include_once("./Services/Tracking/classes/class.ilLearningProgressBaseGUI.php");
291  include_once("./Services/Tracking/classes/class.ilLPStatus.php");
292  $map = array();
294  {
297  $map[$status] = ilUtil::img($path, $text);
298  }
299  $map[""] = $map[0];
300  $data["set"][$idx]["status"] = $this->getItemsPercentages($result["status"], $users_no, $map);
301  $data["set"][$idx]["mark"] = $this->getItemsPercentages($result["mark"], $users_no);
302 
303  if(!$this->isPercentageAvailable($result["obj_id"]))
304  {
305  $data["set"][$idx]["percentage_avg"] = NULL;
306  }
307  }
308 
309  $this->setMaxCount($data["cnt"]);
310  $this->setData($data["set"]);
311  }
312 
322  protected function getItemsPercentages(array $data = NULL, $overall, array $value_map = NULL, $limit = 3)
323  {
324  global $lng;
325 
326  if(!$overall)
327  {
328  return false;
329  }
330 
331  $result = array();
332 
333  if($data)
334  {
335  $counter = $others_counter = $others_sum = 0;
336  foreach($data as $id => $count)
337  {
338  $counter++;
339  if($counter <= $limit)
340  {
341  $caption = $id;
342 
343  if($value_map && isset($value_map[$id]))
344  {
345  $caption = $value_map[$id];
346  }
347 
348  if($caption == "")
349  {
350  $caption = $lng->txt("none");
351  }
352 
353  $perc = round($count/$overall*100);
354  $result[] = array(
355  "caption" => $caption,
356  "absolute" => $count." ".($count > 1 ? $lng->txt("users") : $lng->txt("user")),
357  "percentage" => $perc
358  );
359  }
360  else
361  {
362  $others_sum += $count;
363  $others_counter++;
364  }
365  }
366 
367  if($others_counter)
368  {
369  $perc = round($others_sum/$overall*100);
370  $result[] = array(
371  "caption" => $otherss_counter." ".$lng->txt("trac_others"),
372  "absolute" => $others_sum." ".($others_sum > 1 ? $lng->txt("users") : $lng->txt("user")),
373  "percentage" => $perc
374  );
375  }
376  }
377 
378 
379  return $result;
380  }
381 
382  protected function parseValue($id, $value, $type)
383  {
384  global $lng;
385 
386  // get rid of aggregation
387  $pos = strrpos($id, "_");
388  if($pos !== false)
389  {
390  $function = strtoupper(substr($id, $pos+1));
391  if(in_array($function, array("MIN", "MAX", "SUM", "AVG", "COUNT")))
392  {
393  $id = substr($id, 0, $pos);
394  }
395  }
396 
397  if(trim($value) == "")
398  {
399  if($id == "title")
400  {
401  return "--".$lng->txt("none")."--";
402  }
403  return "";
404  }
405 
406  switch($id)
407  {
408  case "first_access":
409  case "create_date":
411  break;
412 
413  case "last_access":
415  break;
416 
417  case "spent_seconds":
418  if(in_array($type, array("exc")))
419  {
420  $value = "-";
421  }
422  else
423  {
424  include_once("./classes/class.ilFormat.php");
425  $value = ilFormat::_secondsToString($value);
426  }
427  break;
428 
429  case "percentage":
430  /* :TODO:
431  if(in_array(strtolower($this->status_class),
432  array("illpstatusmanual", "illpstatusscormpackage", "illpstatustestfinished")) ||
433  $type == "exc"))
434  */
435  if(false)
436  {
437  $value = "-";
438  }
439  else
440  {
441  $value = $value."%";
442  }
443  break;
444 
445  case "mark":
446  if(in_array($type, array("lm", "dbk")))
447  {
448  $value = "-";
449  }
450  break;
451  }
452 
453  return $value;
454  }
455 
459  protected function fillRow($a_set)
460  {
461  global $lng;
462 
463  $this->tpl->setVariable("ICON", ilUtil::getTypeIconPath($a_set["type"], $a_set["obj_id"], "tiny"));
464  $this->tpl->setVariable("ICON_ALT", $lng->txt($a_set["type"]));
465  $this->tpl->setVariable("TITLE", $a_set["title"]);
466 
467  foreach ($this->getSelectedColumns() as $c)
468  {
469  switch($c)
470  {
471  case "country":
472  case "gender":
473  case "city":
474  case "language":
475  case "status":
476  case "mark":
477  case "sel_country":
478  $this->renderPercentages($c, $a_set[$c]);
479  break;
480 
481  case "percentage_avg":
482  if((int)$a_set[$c] === 0 || !$this->isPercentageAvailable($a_set["obj_id"]))
483  {
484  $this->tpl->setVariable(strtoupper($c), "");
485  break;
486  }
487 
488  default:
489  $value = $this->parseValue($c, $a_set[$c], $a_set["type"]);
490  $this->tpl->setVariable(strtoupper($c), $value);
491  break;
492  }
493  }
494  }
495 
496  protected function renderPercentages($id, $data)
497  {
498  if($data)
499  {
500  foreach($data as $item)
501  {
502  $this->tpl->setCurrentBlock($id."_row");
503  $this->tpl->setVariable("CAPTION", $item["caption"]);
504  $this->tpl->setVariable("ABSOLUTE", $item["absolute"]);
505  $this->tpl->setVariable("PERCENTAGE", $item["percentage"]);
506  $this->tpl->parseCurrentBlock();
507  }
508  }
509  else
510  {
511  $this->tpl->touchBlock($id);;
512  }
513  }
514 
515  protected function isArrayColumn($a_name)
516  {
517  if(in_array($a_name, array("country", "gender", "city", "language", "status", "mark")))
518  {
519  return true;
520  }
521  return false;
522  }
523 
524  protected function fillHeaderExcel($worksheet, &$a_row)
525  {
526  $worksheet->write($a_row, 0, $this->lng->txt("title"));
527 
528  $labels = $this->getSelectableColumns();
529  $cnt = 1;
530  foreach ($this->getSelectedColumns() as $c)
531  {
532  $label = $labels[$c]["txt"];
533  $label = str_replace("&#216;", $this->lng->txt("trac_average"), $label);
534  $label = str_replace("&#8721;", $this->lng->txt("trac_sum"), $label);
535 
536  if(!$this->isArrayColumn($c))
537  {
538  $worksheet->write($a_row, $cnt, $label);
539  $cnt++;
540  }
541  else
542  {
543  $worksheet->write($a_row, $cnt, $label." #1");
544  $worksheet->write($a_row, ++$cnt, $label." #1");
545  $worksheet->write($a_row, ++$cnt, $label." #1 %");
546  $worksheet->write($a_row, ++$cnt, $label." #2");
547  $worksheet->write($a_row, ++$cnt, $label." #2");
548  $worksheet->write($a_row, ++$cnt, $label." #2 %");
549  $worksheet->write($a_row, ++$cnt, $label." #3");
550  $worksheet->write($a_row, ++$cnt, $label." #3");
551  $worksheet->write($a_row, ++$cnt, $label." #3 %");
552  $worksheet->write($a_row, ++$cnt, $label." ".$this->lng->txt("trac_others"));
553  $worksheet->write($a_row, ++$cnt, $label." ".$this->lng->txt("trac_others"));
554  $worksheet->write($a_row, ++$cnt, $label." ".$this->lng->txt("trac_others")." %");
555  $cnt++;
556  }
557  }
558  }
559 
560  protected function fillRowExcel($worksheet, &$a_row, $a_set)
561  {
562  $worksheet->write($a_row, 0, $a_set["title"]);
563 
564  $cnt = 1;
565  foreach ($this->getSelectedColumns() as $c)
566  {
567  if(!$this->isArrayColumn($c))
568  {
569  $val = $this->parseValue($c, $a_set[$c], "user");
570  $worksheet->write($a_row, $cnt, $val);
571  $cnt++;
572  }
573  else
574  {
575  foreach($a_set[$c] as $idx => $value)
576  {
577  if($c == "status")
578  {
579  preg_match("/alt=\"([^\"]+)\"/", $value["caption"], $res);
580  $value["caption"] = $res[1];
581  }
582  $worksheet->write($a_row, $cnt, $value["caption"]);
583  $worksheet->write($a_row, ++$cnt, (int)$value["absolute"]);
584  $worksheet->write($a_row, ++$cnt, $value["percentage"]);
585  $cnt++;
586  }
587  if(sizeof($a_set[$c]) < 4)
588  {
589  for($loop = 4; $loop > sizeof($a_set[$c]); $loop--)
590  {
591  $worksheet->write($a_row, $cnt, "");
592  $worksheet->write($a_row, ++$cnt, "");
593  $worksheet->write($a_row, ++$cnt, "");
594  $cnt++;
595  }
596  }
597  }
598  }
599  }
600 
601  protected function fillHeaderCSV($a_csv)
602  {
603  $a_csv->addColumn($this->lng->txt("title"));
604 
605  $labels = $this->getSelectableColumns();
606  foreach ($this->getSelectedColumns() as $c)
607  {
608  $label = $labels[$c]["txt"];
609  $label = str_replace("&#216;", $this->lng->txt("trac_average"), $label);
610  $label = str_replace("&#8721;", $this->lng->txt("trac_sum"), $label);
611 
612  if(!$this->isArrayColumn($c))
613  {
614  $a_csv->addColumn($label);
615  }
616  else
617  {
618  $a_csv->addColumn($label." #1");
619  $a_csv->addColumn($label." #1");
620  $a_csv->addColumn($label." #1 %");
621  $a_csv->addColumn($label." #2");
622  $a_csv->addColumn($label." #2");
623  $a_csv->addColumn($label." #2 %");
624  $a_csv->addColumn($label." #3");
625  $a_csv->addColumn($label." #3");
626  $a_csv->addColumn($label." #3 %");
627  $a_csv->addColumn($label." ".$this->lng->txt("trac_others"));
628  $a_csv->addColumn($label." ".$this->lng->txt("trac_others"));
629  $a_csv->addColumn($label." ".$this->lng->txt("trac_others")." %");
630  }
631  }
632 
633  $a_csv->addRow();
634  }
635 
636  protected function fillRowCSV($a_csv, $a_set)
637  {
638  $a_csv->addColumn($a_set["title"]);
639 
640  foreach ($this->getSelectedColumns() as $c)
641  {
642  if(!$this->isArrayColumn($c))
643  {
644  $val = $this->parseValue($c, $a_set[$c], "user");
645  $a_csv->addColumn($val);
646  }
647  else
648  {
649  foreach($a_set[$c] as $idx => $value)
650  {
651  if($c == "status")
652  {
653  preg_match("/alt=\"([^\"]+)\"/", $value["caption"], $res);
654  $value["caption"] = $res[1];
655  }
656  $a_csv->addColumn($value["caption"]);
657  $a_csv->addColumn((int)$value["absolute"]);
658  $a_csv->addColumn($value["percentage"]);
659  }
660  if(sizeof($a_set[$c]) < 4)
661  {
662  for($loop = 4; $loop > sizeof($a_set[$c]); $loop--)
663  {
664  $a_csv->addColumn("");
665  $a_csv->addColumn("");
666  $a_csv->addColumn("");
667  }
668  }
669  }
670  }
671 
672  $a_csv->addRow();
673  }
674 }
675 ?>