ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLPTableBaseGUI.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/Table/classes/class.ilTable2GUI.php");
5 include_once './Services/Search/classes/class.ilSearchSettings.php';
6 
16 {
17  protected $filter; // array
18 
19  public function __construct($a_parent_obj, $a_parent_cmd = "", $a_template_context = "")
20  {
21  parent::__construct($a_parent_obj, $a_parent_cmd, $a_template_context);
22 
23  // country names
24  $this->lng->loadLanguageModule("meta");
25 
26  include_once("./Services/Tracking/classes/class.ilLPObjSettings.php");
27  }
28 
29  public function executeCommand()
30  {
31  global $ilCtrl;
32 
33  $this->determineSelectedFilters();
34 
35  if(!$ilCtrl->getNextClass($this))
36  {
37  $to_hide = false;
38 
39  switch($ilCtrl->getCmd())
40  {
41  case "applyFilter":
42  $this->resetOffset();
43  $this->writeFilterToSession();
44  break;
45 
46  case "resetFilter":
47  $this->resetOffset();
48  $this->resetFilter();
49  break;
50 
51  case "hideSelected":
52  $to_hide = $_POST["item_id"];
53  break;
54 
55  case "hide":
56  $to_hide = array((int)$_GET["hide"]);
57  break;
58 
59  // page selector
60  default:
61  $this->determineOffsetAndOrder();
62  $this->storeNavParameter();
63  break;
64  }
65 
66  if($to_hide)
67  {
68  $obj = $this->getFilterItemByPostVar("hide");
69  $value = array_unique(array_merge((array)$obj->getValue(), $to_hide));
70  $obj->setValue($value);
71  $obj->writeToSession();
72  }
73 
74  if(isset($_REQUEST["tbltplcrt"]))
75  {
76  $ilCtrl->setParameter($this->parent_obj, "tbltplcrt", $_REQUEST["tbltplcrt"]);
77  }
78  if(isset($_REQUEST["tbltpldel"]))
79  {
80  $ilCtrl->setParameter($this->parent_obj, "tbltpldel", $_REQUEST["tbltpldel"]);
81  }
82 
83  $ilCtrl->redirect($this->parent_obj, $this->parent_cmd);
84  }
85  else
86  {
87  // e.g. repository selector
88  return parent::executeCommand();
89  }
90  }
91 
99  protected function searchObjects(array $filter, $permission, array $preset_obj_ids = null)
100  {
101  global $ilObjDataCache;
102 
103  /* for performance issues: fast search WITHOUT any permission checks
104  include_once "Services/Tracking/classes/class.ilTrQuery.php";
105  return ilTrQuery::searchObjects($filter["type"], $filter["query"],
106  $filter["area"], $filter["hide"], $preset_obj_ids);
107  */
108 
109  include_once './Services/Search/classes/class.ilQueryParser.php';
110 
111  $query_parser =& new ilQueryParser($filter["query"]);
112  $query_parser->setMinWordLength(0);
113  $query_parser->setCombination(QP_COMBINATION_AND);
114  $query_parser->parse();
115  if(!$query_parser->validate())
116  {
117  // echo $query_parser->getMessage();
118  return false;
119  }
120 
121  if($filter["type"] == "lres")
122  {
123  $filter["type"] = array('lm','sahs','htlm','dbk');
124  }
125  else
126  {
127  $filter["type"] = array($filter["type"]);
128  }
129 
130  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
131  $object_search =& new ilLikeObjectSearch($query_parser);
132  $object_search->setFilter($filter["type"]);
133  if($preset_obj_ids)
134  {
135  $object_search->setIdFilter($preset_obj_ids);
136  }
137  $res =& $object_search->performSearch();
138  $res->setRequiredPermission($permission);
139  $res->setMaxHits(1000);
140  $res->addObserver($this, "searchFilterListener");
141 
142  if(!$this->filter["area"])
143  {
144  $res->filter(ROOT_FOLDER_ID, false);
145  }
146  else
147  {
148  $res->filter($this->filter["area"], false);
149  }
150 
151  $objects = array();
152  foreach($res->getResults() as $obj_data)
153  {
154  $objects[$obj_data['obj_id']][] = $obj_data['ref_id'];
155  }
156 
157  // Check if search max hits is reached
158  $this->limit_reached = $res->isLimitReached();
159 
160  return $objects ? $objects : array();
161  }
162 
168  public function searchFilterListener($a_ref_id, $a_data)
169  {
170  if(is_array($this->filter["hide"]) && in_array($a_data["obj_id"], $this->filter["hide"]))
171  {
172  return false;
173  }
174  // :TODO: mode does not have to be set in db
175  if(ilLPObjSettings::_lookupMode($a_data["obj_id"]) == LP_MODE_DEACTIVATED)
176  {
177  return false;
178  }
179  return true;
180  }
181 
187  public function initFilter($a_split_learning_resources = false, $a_include_no_status_filter = true)
188  {
189  global $lng, $ilObjDataCache;
190 
191  $this->setDisableFilterHiding(true);
192 
193  // object type selection
194  include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
195  $si = new ilSelectInputGUI($this->lng->txt("obj_type"), "type");
196  $si->setOptions($this->getPossibleTypes($a_split_learning_resources));
197  $this->addFilterItem($si);
198  $si->readFromSession();
199  if(!$si->getValue())
200  {
201  $si->setValue("crs");
202  }
203  $this->filter["type"] = $si->getValue();
204 
205  // hidden items
206  include_once("./Services/Form/classes/class.ilMultiSelectInputGUI.php");
207  $msi = new ilMultiSelectInputGUI($lng->txt("trac_filter_hidden"), "hide");
208  $this->addFilterItem($msi);
209  $msi->readFromSession();
210  $this->filter["hide"] = $msi->getValue();
211  if($this->filter["hide"])
212  {
213  // create options from current value
214  $types = $this->getCurrentFilter(true);
215  $type = $types["type"];
216  $options = array();
217  if($type == 'lres')
218  {
219  $type = array('lm','sahs','htlm','dbk');
220  }
221  else
222  {
223  $type = array($type);
224  }
225  foreach($this->filter["hide"] as $obj_id)
226  {
227  if(in_array($ilObjDataCache->lookupType($obj_id), $type))
228  {
229  $options[$obj_id] = $ilObjDataCache->lookupTitle($obj_id);
230  }
231  }
232  $msi->setOptions($options);
233  }
234 
235  // title/description
236  include_once("./Services/Form/classes/class.ilTextInputGUI.php");
237  $ti = new ilTextInputGUI($lng->txt("trac_title_description"), "query");
238  $ti->setMaxLength(64);
239  $ti->setSize(20);
240  $this->addFilterItem($ti);
241  $ti->readFromSession();
242  $this->filter["query"] = $ti->getValue();
243 
244  // repository area selection
245  include_once("./Services/Form/classes/class.ilRepositorySelectorInputGUI.php");
246  $rs = new ilRepositorySelectorInputGUI($lng->txt("trac_filter_area"), "area");
247  $rs->setSelectText($lng->txt("trac_select_area"));
248  $this->addFilterItem($rs);
249  $rs->readFromSession();
250  $this->filter["area"] = $rs->getValue();
251 
252  // hide "not started yet"
253  if($a_include_no_status_filter)
254  {
255  include_once("./Services/Form/classes/class.ilCheckboxInputGUI.php");
256  $cb = new ilCheckboxInputGUI($lng->txt("trac_filter_has_status"), "status");
257  $this->addFilterItem($cb);
258  $cb->readFromSession();
259  $this->filter["status"] = $cb->getChecked();
260  }
261  }
262 
269  protected function buildPath($ref_ids)
270  {
271  global $tree, $ilCtrl;
272 
273  include_once './Services/Link/classes/class.ilLink.php';
274 
275  if(!count($ref_ids))
276  {
277  return false;
278  }
279  foreach($ref_ids as $ref_id)
280  {
281  $path = "...";
282  $counter = 0;
283  $path_full = $tree->getPathFull($ref_id);
284  foreach($path_full as $data)
285  {
286  if(++$counter < (count($path_full)-1))
287  {
288  continue;
289  }
290  $path .= " &raquo; ";
291  if($ref_id != $data['ref_id'])
292  {
293  $path .= $data['title'];
294  }
295  else
296  {
297  $path .= ('<a target="_top" href="'.
298  ilLink::_getLink($data['ref_id'],$data['type']).'">'.
299  $data['title'].'</a>');
300  }
301  }
302 
303  $result[$ref_id] = $path;
304  }
305  return $result;
306  }
307 
315  protected function getPossibleTypes($a_split_learning_resources = false, $a_include_digilib = false, $a_allow_undefined_lp = false)
316  {
317  global $lng, $ilPluginAdmin;
318 
319  $options = array();
320 
321  if($a_split_learning_resources)
322  {
323  $options['lm'] = $lng->txt('objs_lm');
324  $options['sahs'] = $lng->txt('objs_sahs');
325  $options['htlm'] = $lng->txt('objs_htlm');
326 
327  if($a_include_digilib)
328  {
329  $options['dbk'] = $lng->txt('objs_dbk');
330  }
331  }
332  else
333  {
334  $options['lres'] = $lng->txt('learning_resources');
335  }
336 
337  $options['crs'] = $lng->txt('objs_crs');
338  $options['grp'] = $lng->txt('objs_grp');
339  $options['exc'] = $lng->txt('objs_exc');
340  $options['tst'] = $lng->txt('objs_tst');
341 
342  if($a_allow_undefined_lp)
343  {
344  $options["file"] = $lng->txt("objs_file");
345  $options["webr"] = $lng->txt("objs_webr");
346  $options["wiki"] = $lng->txt("objs_wiki");
347  }
348 
349  // repository plugins (currently only active)
350  include_once 'Services/Repository/classes/class.ilRepositoryObjectPluginSlot.php';
351  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "Repository", "robj");
352  foreach ($pl_names as $pl)
353  {
354  $pl_id = $ilPluginAdmin->getId(IL_COMP_SERVICE, "Repository", "robj", $pl);
356  {
357  $options[$pl_id] = ilPlugin::lookupTxt("rep_robj", $pl_id, "objs_".$pl_id);
358  }
359  }
360 
361  asort($options);
362  return $options;
363  }
364 
365  protected function parseValue($id, $value, $type)
366  {
367  global $lng;
368 
369  // get rid of aggregation
370  $pos = strrpos($id, "_");
371  if($pos !== false)
372  {
373  $function = strtoupper(substr($id, $pos+1));
374  if(in_array($function, array("MIN", "MAX", "SUM", "AVG", "COUNT")))
375  {
376  $id = substr($id, 0, $pos);
377  }
378  }
379 
380  if(trim($value) == "" && $id != "status")
381  {
382  if($id == "title" && get_class($this) != "ilTrObjectUsersPropsTableGUI")
383  {
384  return "--".$lng->txt("none")."--";
385  }
386  return " ";
387  }
388 
389  switch($id)
390  {
391  case "first_access":
392  case "create_date":
393  case 'status_changed':
395  break;
396 
397  case "last_access":
399  break;
400 
401  case "birthday":
402  $value = ilDatePresentation::formatDate(new ilDate($value, IL_CAL_DATE));
403  break;
404 
405  case "spent_seconds":
406  if(in_array($type, array("exc")))
407  {
408  $value = "-";
409  }
410  else
411  {
412  include_once("./Services/Utilities/classes/class.ilFormat.php");
413  $value = ilFormat::_secondsToString($value);
414  }
415  break;
416 
417  case "percentage":
418  /* :TODO:
419  if(in_array(strtolower($this->status_class),
420  array("illpstatusmanual", "illpstatusscormpackage", "illpstatustestfinished")) ||
421  $type == "exc"))
422  */
423  if(false)
424  {
425  $value = "-";
426  }
427  else
428  {
429  $value = $value."%";
430  }
431  break;
432 
433  case "mark":
434  if(in_array($type, array("lm", "dbk")))
435  {
436  $value = "-";
437  }
438  break;
439 
440  case "gender":
441  $value = $lng->txt("gender_".$value);
442  break;
443 
444  case "status":
445  include_once("./Services/Tracking/classes/class.ilLearningProgressBaseGUI.php");
448  $value = ilUtil::img($path, $text);
449  break;
450 
451  case "language":
452  $lng->loadLanguageModule("meta");
453  $value = $lng->txt("meta_l_".$value);
454  break;
455 
456  case "sel_country":
457  $value = $lng->txt("meta_c_".$value);
458  break;
459  }
460 
461  return $value;
462  }
463 
464  public function getCurrentFilter($as_query = false)
465  {
466  $result = array();
467  foreach($this->filter as $id => $value)
468  {
469  $item = $this->getFilterItemByPostVar($id);
470  switch($id)
471  {
472  case "title":
473  case "country":
474  case "gender":
475  case "city":
476  case "language":
477  case "login":
478  case "firstname":
479  case "lastname":
480  case "mark":
481  case "u_comment":
482  case "institution":
483  case "department":
484  case "title":
485  case "street":
486  case "zipcode":
487  case "email":
488  case "matriculation":
489  case "sel_country":
490  case "query":
491  case "type":
492  case "area":
493  if($value)
494  {
495  $result[$id] = $value;
496  }
497  break;
498 
499  case "status":
500  if($value !== false)
501  {
502  $result[$id] = $value;
503  }
504  break;
505 
506  case "user_total":
507  case "read_count":
508  case "percentage":
509  case "hide":
510  case "spent_seconds":
511  if(is_array($value) && implode("", $value))
512  {
513  $result[$id] = $value;
514  }
515  break;
516 
517  case "registration":
518  case "create_date":
519  case "first_access":
520  case "last_access":
521  case 'status_changed':
522  if($value)
523  {
524  if($value["from"])
525  {
526  $result[$id]["from"] = $value["from"]->get(IL_CAL_DATETIME);
527  }
528  if($value["to"])
529  {
530  $result[$id]["to"] = $value["to"]->get(IL_CAL_DATETIME);
531  }
532  }
533  break;
534 
535  case "birthday":
536  if($value)
537  {
538  if($value["from"])
539  {
540  $result[$id]["from"] = $value["from"]->get(IL_CAL_DATETIME);
541  $result[$id]["from"] = substr($result[$id]["from"], 0, -8)."00:00:00";
542  }
543  if($value["to"])
544  {
545  $result[$id]["to"] = $value["to"]->get(IL_CAL_DATETIME);
546  $result[$id]["to"] = substr($result[$id]["to"], 0, -8)."23:59:59";
547  }
548  }
549  break;
550  }
551  }
552 
553  return $result;
554  }
555 
556  protected function isPercentageAvailable($a_obj_id)
557  {
558  include_once("./Services/Tracking/classes/class.ilLPObjSettings.php");
559  $mode = ilLPObjSettings::_lookupMode($a_obj_id);
560  if(in_array($mode, array(LP_MODE_TLT, LP_MODE_VISITS, LP_MODE_OBJECTIVES, LP_MODE_SCORM,
562  {
563  return true;
564  }
565  return false;
566  }
567 
568  protected function parseTitle($a_obj_id, $action, $a_user_id = false)
569  {
570  global $lng, $ilObjDataCache, $ilUser;
571 
572  $user = "";
573  if($a_user_id)
574  {
575  if($a_user_id != $ilUser->getId())
576  {
577  $a_user = ilObjectFactory::getInstanceByObjId($a_user_id);
578  }
579  else
580  {
581  $a_user = $ilUser;
582  }
583  $user .= ", ".$a_user->getFullName(); // " [".$a_user->getLogin()."]";
584  }
585 
586  if($a_obj_id != ROOT_FOLDER_ID)
587  {
588  $this->setTitle($lng->txt($action).": ".$ilObjDataCache->lookupTitle($a_obj_id).$user);
589  $this->setDescription($this->lng->txt('trac_mode').": ".ilLPObjSettings::_mode2Text(ilLPObjSettings::_lookupMode($a_obj_id)));
590  }
591  else
592  {
593  $this->setTitle($lng->txt($action));
594  }
595  }
596 
602  protected function getExportMeta()
603  {
604  global $lng, $ilObjDataCache, $ilUser, $ilClientIniFile;
605 
606  /* see spec
607  Name of installation
608  Name of the course
609  Permalink to course
610  Owner of course object
611  Date of report generation
612  Reporting period
613  Name of person who generated the report.
614  */
615 
617  include_once './Services/Link/classes/class.ilLink.php';
618 
619  $data = array(
620  $lng->txt("trac_name_of_installation") => $ilClientIniFile->readVariable('client', 'name'),
621  $lng->txt("trac_object_name") => $ilObjDataCache->lookupTitle($this->obj_id),
622  $lng->txt("trac_object_link") => ilLink::_getLink($this->ref_id, ilObject::_lookupType($this->obj_id)),
623  $lng->txt("trac_object_owner") => ilObjUser::_lookupFullname(ilObject::_lookupOwner($this->obj_id)),
624  $lng->txt("trac_report_date") =>
626  $lng->txt("trac_report_owner") => $ilUser->getFullName()
627  );
628  return $data;
629  }
630 
631  protected function fillMetaExcel($worksheet, &$a_row)
632  {
633  foreach($this->getExportMeta() as $caption => $value)
634  {
635  $worksheet->write($a_row, 0, $caption);
636  $worksheet->write($a_row, 1, $value);
637  $a_row++;
638  }
639  $a_row++;
640  }
641 
642  protected function fillMetaCSV($a_csv)
643  {
644  foreach($this->getExportMeta() as $caption => $value)
645  {
646  $a_csv->addColumn(strip_tags($caption));
647  $a_csv->addColumn(strip_tags($value));
648  $a_csv->addRow();
649  }
650  $a_csv->addRow();
651  }
652 
653  protected function showTimingsWarning($a_ref_id, $a_user_id)
654  {
655  include_once 'Modules/Course/classes/Timings/class.ilTimingCache.php';
656  if(ilTimingCache::_showWarning($a_ref_id, $a_user_id))
657  {
658  $timings = ilTimingCache::_getTimings($a_ref_id);
659  if($timings['item']['changeable'] and $timings['user'][$a_user_id]['end'])
660  {
661  $end = $timings['user'][$a_user_id]['end'];
662  }
663  else if ($timings['item']['suggestion_end'])
664  {
665  $end = $timings['item']['suggestion_end'];
666  }
667  else
668  {
669  $end = true;
670  }
671  return $end;
672  }
673  }
674 
675  protected function formatSeconds($seconds, $a_shorten_zero = false)
676  {
677  $seconds = ((int)$seconds > 0) ? $seconds : 0;
678  if($a_shorten_zero && !$seconds)
679  {
680  return "-";
681  }
682 
683  $hours = floor($seconds / 3600);
684  $rest = $seconds % 3600;
685 
686  $minutes = floor($rest / 60);
687  $rest = $rest % 60;
688 
689  if($rest)
690  {
691  $minutes++;
692  }
693 
694  return sprintf("%dh%02dm",$hours,$minutes);
695  }
696 
697  protected function anonymizeValue($a_value, $a_force_number = false)
698  {
699  // currently inactive
700  return $a_value;
701 
702  if(is_numeric($a_value))
703  {
704  $threshold = 3;
705  $a_value = (int)$a_value;
706  if($a_value <= $threshold)
707  {
708  if(!$a_force_number)
709  {
710  return "0-".$threshold;
711  }
712  else
713  {
714  return $threshold;
715  }
716  }
717  }
718  return $a_value;
719  }
720 
721  protected function buildValueScale($a_max_value, $a_anonymize = false, $a_format_seconds = false)
722  {
723  $step = 0;
724  if($a_max_value)
725  {
726  $step = $a_max_value / 10;
727  $base = ceil(log($step, 10));
728  $fac = ceil($step / pow(10, ($base - 1)));
729  $step = pow(10, $base - 1) * $fac;
730  }
731  if ($step <= 1)
732  {
733  $step = 1;
734  }
735  $ticks = range(0, $a_max_value+$step, $step);
736 
737  $value_ticks = array(0 => 0);
738  foreach($ticks as $tick)
739  {
740  $value = $tvalue = $tick;
741  if($a_anonymize)
742  {
743  $value = $this->anonymizeValue($value, true);
744  $tvalue = $this->anonymizeValue($tvalue);
745  }
746  if($a_format_seconds)
747  {
748  $tvalue = $this->formatSeconds($value);
749  }
750  $value_ticks[$value] = $tvalue;
751  }
752 
753  return $value_ticks;
754  }
755 
756  protected function getMonthsFilter($a_short = false)
757  {
758  global $lng;
759 
760  $options = array();
761  for($loop = 0; $loop < 10; $loop++)
762  {
763  $year = date("Y")-$loop;
764  $options[$year] = $year;
765  for($loop2 = 12; $loop2 > 0; $loop2--)
766  {
767  $month = str_pad($loop2, 2, "0", STR_PAD_LEFT);
768  if($year.$month <= date("Ym"))
769  {
770  if(!$a_short)
771  {
772  $caption = $year." / ".$lng->txt("month_".$month."_long");
773  }
774  else
775  {
776  $caption = $year."/".$month;
777  }
778  $options[$year."-".$month] = $caption;
779  }
780  }
781  }
782  return $options;
783  }
784 
785  protected function getMonthsYear($a_year = null, $a_short = false)
786  {
787  global $lng;
788 
789  if(!$a_year)
790  {
791  $a_year = date("Y");
792  }
793 
794  $all = array();
795  for($loop = 1; $loop<13; $loop++)
796  {
797  $month = str_pad($loop, 2, "0", STR_PAD_LEFT);
798  if($a_year."-".$month <= date("Y-m"))
799  {
800  if(!$a_short)
801  {
802  $caption = $lng->txt("month_".$month."_long");
803  }
804  else
805  {
806  $caption = $lng->txt("month_".$month."_short");
807  }
808  $all[$a_year."-".$month] = $caption;
809  }
810  }
811  return $all;
812  }
813 }
814 
815 ?>