ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLPObjectStatisticsLPTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=0);
20
21use ILIAS\UI\Factory as UIFactory;
22use ILIAS\UI\Renderer as UIRenderer;
23
31{
32 protected UIFactory $ui_factory;
33 protected UIRenderer $ui_renderer;
34
35 protected array $types = array("min", "avg", "max");
40 );
41 protected bool $is_chart = false;
42 protected bool $is_details = false;
43 protected array $chart_data = array();
44 protected ?array $preselected;
45 protected array $status_map = [];
46
50 public function __construct(
51 ?object $a_parent_obj,
52 string $a_parent_cmd,
53 ?array $a_preselect = null,
54 bool $a_is_chart = false,
55 bool $a_is_details = false
56 ) {
57 global $DIC;
58
59 $this->ui_factory = $DIC->ui()->factory();
60 $this->ui_renderer = $DIC->ui()->renderer();
61
62 $this->preselected = $a_preselect;
63 $this->is_chart = $a_is_chart;
64 $this->is_details = $a_is_details;
65
66 $this->setId("lpobjstatlptbl");
67 parent::__construct($a_parent_obj, $a_parent_cmd);
68 }
69
70 public function init(): void
71 {
72 if (!$this->is_details) {
73 $this->setShowRowsSelector(true);
74 $this->addColumn("", "", "1%", true);
75 $this->addColumn($this->lng->txt("trac_title"), "title");
76 $all_columns = $this->getSelectableColumns();
77 foreach ($this->getSelectedColumns() as $col_name => $col_info) {
78 $column_definition = $all_columns[$col_name];
79 $this->addColumn(
80 $column_definition['txt'],
81 $column_definition['sortable'] ? $column_definition['field'] : '',
82 $column_definition['width']
83 );
84 }
85 } else {
86 $this->setLimit(20);
87 $this->setShowRowsSelector(false); // see #35492
88 $this->addColumn($this->lng->txt("trac_figure"));
89 }
90 $this->initFilter();
91
92 if (strpos($this->filter["yearmonth"], "-") === false) {
93 foreach ($this->getMonthsYear(
94 $this->filter["yearmonth"]
95 ) as $num => $caption) {
96 if ($this->is_details) {
97 $this->addColumn($caption); // see #35492
98 } else {
99 $this->addColumn($caption, "month_" . $num);
100 }
101 }
102 } else {
103 foreach ($this->types as $type) {
104 if ($type != "avg") {
105 $caption = " " . $this->lng->txt(
106 "trac_object_stat_lp_" . $type
107 );
108 } else {
109 $caption = " &#216;";
110 }
111 $this->addColumn(
112 $this->lng->txt("trac_members_short") . $caption,
113 "mem_cnt_" . $type
114 );
115 }
116
118
119 foreach ($this->status as $status) {
120 $icon = $icons->renderIconForStatus($status);
121
122 foreach ($this->types as $type) {
123 if ($type != "avg") {
124 $caption = $icon . " " . $this->lng->txt(
125 "trac_object_stat_lp_" . $type
126 );
127 } else {
128 $caption = $icon . " &#216;";
129 }
130 $this->addColumn($caption, $status . "_" . $type);
131 }
132 }
133 }
134
135 if (!$this->is_details) {
136 $this->setTitle($this->lng->txt("trac_object_stat_lp"));
137
138 // $this->setSelectAllCheckbox("item_id");
139 $this->addMultiCommand(
140 "showLearningProgressGraph",
141 $this->lng->txt("trac_show_graph")
142 );
143 $this->setResetCommand("resetLearningProgressFilter");
144 $this->setFilterCommand("applyLearningProgressFilter");
145 }
146
147 $this->setFormAction(
148 $this->ctrl->getFormAction(
149 $this->getParentObject(),
150 $this->getParentCmd()
151 )
152 );
153 $this->setRowTemplate(
154 "tpl.lp_object_statistics_lp_row.html",
155 "components/ILIAS/Tracking"
156 );
157 $this->setEnableHeader(true);
158 $this->setEnableNumInfo(true);
159 $this->setEnableTitle(true);
160 $this->setDefaultOrderField("title");
161 $this->setDefaultOrderDirection("asc");
162
163 $this->status_map = array(ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM => "not_attempted",
167 );
168 }
169
170 public function loadItems(): void
171 {
172 if ($this->is_details) {
173 $this->getDetailItems($this->preselected[0]);
174 } else {
175 $this->getItems();
176 }
177 }
178 public function getSelectableColumns(): array
179 {
180 if ($this->is_details) {
181 return [];
182 }
183 $columns = [];
184 $columns['obj_id'] = [
185 'field' => 'obj_id',
186 'txt' => $this->lng->txt('object_id'),
187 'default' => false,
188 'optional' => true,
189 'sortable' => true,
190 'width' => '5%'
191 ];
192 $columns['reference_ids'] = [
193 'field' => 'reference_ids',
194 'txt' => $this->lng->txt('trac_reference_ids_column'),
195 'default' => false,
196 'optional' => true,
197 'sortable' => true,
198 'width' => '5%'
199 ];
200 $columns['paths'] = [
201 'field' => 'paths',
202 'txt' => $this->lng->txt('trac_paths'),
203 'default' => false,
204 'optional' => true,
205 'sortable' => false,
206 'width' => '25%'
207 ];
208 return $columns;
209 }
210
211
212 public function numericOrdering(string $a_field): bool
213 {
214 $alphabetic_ordering = [
215 'title'
216 ];
217 if (!in_array($a_field, $alphabetic_ordering)) {
218 return true;
219 }
220 return false;
221 }
222
223 protected function isForwardingToFormDispatcher(): bool
224 {
225 return true;
226 }
227
228
232 public function initFilter(): void
233 {
234 $this->setDisableFilterHiding(true);
235
236 // object type selection
237 $this->filter["type"] = "crs";
238
239 // title/description
240 $ti = new ilTextInputGUI(
241 $this->lng->txt("trac_title_description"),
242 "query"
243 );
244 $ti->setMaxLength(64);
245 $ti->setSize(20);
246 $this->addFilterItem($ti);
247 $ti->readFromSession();
248 $this->filter["query"] = $ti->getValue();
249
250 // year/month
251 $si = new ilSelectInputGUI(
252 $this->lng->txt("year") . " / " . $this->lng->txt("month"),
253 "yearmonth"
254 );
255 $si->setOptions($this->getMonthsFilter());
256 $this->addFilterItem($si);
257 $si->readFromSession();
258 if (!$si->getValue()) {
259 $si->setValue(date("Y-m"));
260 }
261 $this->filter["yearmonth"] = $si->getValue();
262
263 if (!strpos($this->filter["yearmonth"], "-")) {
264 $si = new ilSelectInputGUI(
265 $this->lng->txt("trac_figure"),
266 "figure"
267 );
268 $options = array(
269 "mem_cnt_max" => $this->lng->txt(
270 "members"
271 ) . " " . $this->lng->txt("trac_object_stat_lp_max"),
272 "mem_cnt_avg" => $this->lng->txt("members") . " &#216;",
273 // we are using the db column names here (not the lp constants)!
276 ) . " " . $this->lng->txt("trac_object_stat_lp_max"),
279 ) . " &#216;"
280 );
281 $si->setOptions($options);
282 $this->addFilterItem($si);
283 $si->readFromSession();
284 if (!$si->getValue()) {
285 $si->setValue("mem_cnt_max");
286 }
287 $this->filter["measure"] = $si->getValue();
288 }
289
290 $this->filter = $this->initRepositoryFilter($this->filter);
291
292 if ($this->is_details) {
293 $this->filters = array();
294 }
295 }
296
297 public function getItems()
298 {
299 $data = array();
300 $all_status = array_merge(array("mem_cnt"), $this->status);
301
302 $objects = $this->searchObjects(
303 $this->getCurrentFilter(true),
304 "read",
305 null,
306 false
307 );
308 if ($objects) {
309 $objects = array_keys($objects);
310
311 $yearmonth = explode("-", $this->filter["yearmonth"]);
312 if (sizeof($yearmonth) == 1) {
314 $objects,
315 $yearmonth[0]
316 ) as $item) {
317 $obj_id = $item["obj_id"];
318 if (!isset($data[$obj_id])) {
319 $data[$obj_id]["obj_id"] = $obj_id;
320 $data[$obj_id]["title"] = ilObject::_lookupTitle(
321 $obj_id
322 );
323 $data[$obj_id]['reference_ids'] = $this->findReferencesForObjId($obj_id);
324 }
325 $measure_type = substr($this->filter["measure"], -3);
326 $measure_field = substr($this->filter["measure"], 0, -4);
327 $value = $item[$measure_field . "_" . $measure_type];
328 $idx = $item["yyyy"] . "-" . str_pad(
329 $item["mm"],
330 2,
331 "0",
332 STR_PAD_LEFT
333 );
334 $data[$obj_id]["month_" . $idx] = $value;
335 }
336
337 if ($this->is_chart) {
338 // get data for single days (used in chart display)
339 foreach (array_keys(
340 $this->getMonthsYear($yearmonth[0])
341 ) as $num) {
342 $num_string = explode('-', $num);
343 $num = (int) array_pop($num_string);
345 $objects,
346 $yearmonth[0],
347 $num,
348 true
349 ) as $item) {
350 $idx = $yearmonth[0] .
351 "-" . str_pad(
352 (string) $num,
353 2,
354 "0",
355 STR_PAD_LEFT
356 ) .
357 "-" . str_pad(
358 $item["dd"],
359 2,
360 "0",
361 STR_PAD_LEFT
362 );
363 $this->chart_data[$item["obj_id"]][$idx] = $item;
364 }
365 }
366 }
367 } else {
368 // get data aggregated for month
370 $objects,
371 $yearmonth[0],
372 (int) $yearmonth[1]
373 ) as $item) {
374 $obj_id = $item["obj_id"];
375 if (!isset($data[$obj_id])) {
376 $data[$obj_id]["obj_id"] = $obj_id;
377 $data[$obj_id]["title"] = ilObject::_lookupTitle(
378 $obj_id
379 );
380 $data[$obj_id]['reference_ids'] = $this->findReferencesForObjId($obj_id);
381 $this->initRow($data[$obj_id]);
382 }
383
384 foreach ($all_status as $status) {
385 // status-id to field name
386 if (is_numeric($status)) {
387 $field = $this->status_map[$status];
388 } else {
389 $field = $status;
390 }
391
392 // aggregated fields
393 foreach ($this->types as $type) {
394 $value = $item[$field . "_" . $type];
395 $data[$obj_id][$status . "_" . $type] = $value;
396 }
397 }
398 }
399
400 if ($this->is_chart) {
401 // get data for single days (used in chart display)
403 $objects,
404 $yearmonth[0],
405 (int) $yearmonth[1],
406 true
407 ) as $item) {
408 $this->chart_data[$item["obj_id"]][$item["dd"]] = $item;
409 }
410 }
411 }
412
413 // add objects with no usage data
414 foreach ($objects as $obj_id) {
415 if (!isset($data[$obj_id])) {
416 $data[$obj_id]["obj_id"] = $obj_id;
417 $data[$obj_id]["title"] = ilObject::_lookupTitle($obj_id);
418 $data[$obj_id]['reference_ids'] = $this->findReferencesForObjId($obj_id);
419 }
420 }
421 }
422 $this->setData($data);
423 }
424
425 protected function getDetailItems(int $a_obj_id): void
426 {
427 $data = array();
428 $all_status = array_merge(array("mem_cnt"), $this->status);
429
431 array($a_obj_id),
432 $this->filter["yearmonth"]
433 ) as $item) {
434 $month = "month_" . $item["yyyy"] . "-" . str_pad(
435 $item["mm"],
436 2,
437 "0",
438 STR_PAD_LEFT
439 );
440
441 foreach ($all_status as $status) {
442 // status-id to field name
443 if ($status != "mem_cnt") {
444 $field = $this->status_map[$status];
445 } else {
446 $field = $status;
447 }
448 // aggregated fields
449 foreach ($this->types as $type) {
450 $value = $item[$field . "_" . $type];
451 $idx = $item["yyyy"] . "-" . str_pad(
452 $item["mm"],
453 2,
454 "0",
455 STR_PAD_LEFT
456 );
457 $data[$status . "_" . $type]["month_" . $idx] = $value;
458 }
459 }
460 }
461
463
464 // add captions
465 foreach (array_keys($data) as $figure) {
466 $status = substr($figure, 0, -4);
467 $type = substr($figure, -3);
468
469 if ($status != "mem_cnt") {
471 (int) $status
472 );
473 $icon = $icons->renderIconForStatus((int) $status);
474 $text = $icon . " " . $text;
475 } else {
476 $text = $this->lng->txt("members");
477 }
478 if ($type != "avg") {
479 $caption = $text . " " . $this->lng->txt(
480 "trac_object_stat_lp_" . $type
481 );
482 } else {
483 $caption = $text . " &#216;";
484 }
485 $data[$figure]["figure"] = $caption;
486 }
487
488 $this->setData($data);
489 }
490
491 protected function initRow(array &$a_row): void
492 {
493 foreach ($this->types as $type) {
494 $a_row["mem_cnt_" . $type] = null;
495 }
496 foreach ($this->status as $status) {
497 foreach ($this->types as $type) {
498 $a_row[$status . "_" . $type] = null;
499 }
500 }
501 }
502
506 protected function fillRow(array $a_set): void
507 {
508 global $DIC;
509
510 $ilCtrl = $DIC['ilCtrl'];
511
512 if (!$this->is_details) {
513 $type = ilObject::_lookupType($a_set["obj_id"]);
514
515 // ajax details layer link
516 if (strpos($this->filter["yearmonth"], "-") === false) {
517 $this->ctrl->setParameter(
518 $this->parent_obj,
519 "item_id",
520 $a_set["obj_id"]
521 );
522 $url = $this->ctrl->getLinkTarget(
523 $this->parent_obj,
524 "showLearningProgressDetails"
525 );
526
527 $details_modal = $this->ui_factory->modal()->roundtrip('', [])->withAsyncRenderUrl($url);
528 $details_button = $this->ui_factory->button()->shy(
529 ' (' . $this->lng->txt('details') . ')',
530 $details_modal->getShowSignal()
531 );
532 $a_set["title"] .= $this->ui_renderer->render([$details_button, $details_modal]);
533 $this->ctrl->setParameter($this->parent_obj, "item_id", "");
534 }
535
536 // optional columns before parsing outer "checkbox" block
537 if ($this->isColumnSelected('obj_id')) {
538 $this->tpl->setVariable('OBJ_ID_COL_VALUE', (string) $a_set['obj_id']);
539 }
540 if ($this->isColumnSelected('reference_ids')) {
541 $this->tpl->setVariable('REF_IDS', implode(', ', $a_set['reference_ids']));
542 }
543 if ($this->isColumnSelected('paths')) {
544 $paths = [];
545 foreach ($a_set['reference_ids'] as $reference_id) {
546 $path_gui = new ilPathGUI();
547 $path_gui->enableTextOnly(false);
548 $path_gui->enableHideLeaf(false);
549 $path_gui->setUseImages(true);
550 $paths[] = $path_gui->getPath(ROOT_FOLDER_ID, $reference_id);
551 }
552 $this->tpl->setVariable('PATHS', implode('<br />', $paths));
553 }
554 $this->tpl->setCurrentBlock("checkbox");
555 $this->tpl->setVariable("OBJ_ID", $a_set["obj_id"]);
556 $this->tpl->setVariable(
557 "ICON_SRC",
558 ilObject::_getIcon(0, "tiny", $type)
559 );
560 $this->tpl->setVariable("ICON_ALT", $this->lng->txt($type));
561 $this->tpl->setVariable("TITLE_TEXT", $a_set["title"]);
562 if ($this->preselected && in_array(
563 $a_set["obj_id"],
564 $this->preselected
565 )) {
566 $this->tpl->setVariable(
567 "CHECKBOX_STATE",
568 " checked=\"checked\""
569 );
570 }
571 $this->tpl->parseCurrentBlock();
572 } else {
573 $this->tpl->setCurrentBlock("details");
574 $this->tpl->setVariable("TXT_FIGURE", $a_set["figure"]);
575 $this->tpl->parseCurrentBlock();
576 }
577
578 $this->tpl->setCurrentBlock("item");
579
580 if (strpos($this->filter["yearmonth"], "-") === false) {
581 foreach (array_keys(
582 $this->getMonthsYear($this->filter["yearmonth"])
583 ) as $num) {
584 $value = $this->anonymizeValue((int) ($a_set["month_" . $num] ?? 0));
585 $this->tpl->setVariable("ITEM_VALUE", $value);
586 $this->tpl->parseCurrentBlock();
587 }
588 } else {
589 foreach ($this->types as $type) {
590 $this->tpl->setVariable(
591 "ITEM_VALUE",
592 $this->anonymizeValue(
593 (int) ($a_set["mem_cnt_" . $type] ?? 0)
594 )
595 );
596 $this->tpl->parseCurrentBlock();
597 }
598 foreach ($this->status as $status) {
599 foreach ($this->types as $type) {
600 $this->tpl->setVariable(
601 "ITEM_VALUE",
602 $this->anonymizeValue(
603 (int) ($a_set[$status . "_" . $type] ?? 0)
604 )
605 );
606 $this->tpl->parseCurrentBlock();
607 }
608 }
609 }
610 }
611
612 public function getGraph(array $a_graph_items): string
613 {
614 $a_graph_items = array(array_pop($a_graph_items));
615
617 $chart->setSize("700", "500");
618
619 $legend = new ilChartLegend();
620 $chart->setLegend($legend);
621
622 // needed for correct stacking
623 $custom_order = array(
624 ilLPStatus::LP_STATUS_IN_PROGRESS_NUM => array("#f7d408",
625 "#fffa00"
626 ),
627 ilLPStatus::LP_STATUS_FAILED_NUM => array("#cf0202", "#f15b5b"),
628 ilLPStatus::LP_STATUS_COMPLETED_NUM => array("#17aa0e", "#6ce148"),
630 "#c4c4c4"
631 )
632 );
633
634 $chart->setColors(array());
635
636 $max_value = 0;
637 foreach ($this->chart_data as $object_id => $days) {
638 if (in_array($object_id, $a_graph_items)) {
639 $series = array();
640 foreach ($custom_order as $status => $colors) {
641 $series[$status] = $chart->getDataInstance(
643 );
644 $series[$status]->setLabel(
646 );
647 $chart_colors[] = $colors[0];
648 }
649 $chart->setColors($chart_colors);
650
651 if (strpos($this->filter["yearmonth"], "-") === false) {
652 $x_axis = $this->lng->txt("month");
653
654 $counter = 0;
655 foreach (array_keys(
656 $this->getMonthsYear(
657 $this->filter["yearmonth"]
658 )
659 ) as $month) {
660 for ($loop = 1; $loop < 32; $loop++) {
661 $item_day = $month . "-" . str_pad(
662 (string) $loop,
663 2,
664 "0",
665 STR_PAD_LEFT
666 );
667 foreach (array_keys($custom_order) as $status) {
668 if (isset($days[$item_day])) {
669 // as there is only 1 entry per day, avg == sum
670 $value = (int) $days[$item_day][$this->status_map[$status] . "_avg"];
671 } else {
672 $value = 0;
673 }
674 $max_value = max($max_value, $value);
675 $value = $this->anonymizeValue($value, true);
676 $series[$status]->addPoint($counter, $value);
677 }
678 $counter++;
679 }
680 }
681 } else {
682 $x_axis = $this->lng->txt("day");
683 for ($loop = 1; $loop < 32; $loop++) {
684 foreach (array_keys($custom_order) as $status) {
685 if (isset($days[$loop])) {
686 // as there is only 1 entry per day, avg == sum
687 $value = (int) $days[$loop][$this->status_map[$status] . "_avg"];
688 } else {
689 $value = 0;
690 }
691 $max_value = max($max_value, $value);
692 $value = $this->anonymizeValue($value, true);
693 $series[$status]->addPoint($loop, $value);
694 }
695 }
696 }
697
698 foreach (array_keys($custom_order) as $status) {
699 $chart->addData($series[$status]);
700 }
701 }
702 }
703
704 $value_ticks = $this->buildValueScale($max_value, true);
705
706 $labels = array();
707 if (strpos($this->filter["yearmonth"], "-") === false) {
708 $counter = 0;
709 foreach ($this->getMonthsYear(
710 $this->filter["yearmonth"],
711 true
712 ) as $caption) {
713 $labels[$counter] = $caption;
714 $counter += 31;
715 }
716 } else {
717 for ($loop = 1; $loop < 32; $loop++) {
718 $labels[$loop] = $loop . ".";
719 }
720 }
721 $chart->setTicks($labels, $value_ticks, true);
722
723 return $chart->getHTML();
724 }
725}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByType(int $a_type, string $a_id)
const TYPE_GRID
TableGUI class for learning progress.
numericOrdering(string $a_field)
Should this field be sorted numeric?
__construct(?object $a_parent_obj, string $a_parent_cmd, ?array $a_preselect=null, bool $a_is_chart=false, bool $a_is_details=false)
Constructor.
static getInstance(int $variant=ilLPStatusIcons::ICON_VARIANT_DEFAULT, ?\ILIAS\UI\Renderer $renderer=null, ?\ILIAS\UI\Factory $factory=null)
const LP_STATUS_COMPLETED_NUM
const LP_STATUS_IN_PROGRESS_NUM
const LP_STATUS_NOT_ATTEMPTED_NUM
const LP_STATUS_FAILED_NUM
TableGUI class for learning progress.
getMonthsYear($a_year=null, $a_short=false)
anonymizeValue($a_value, bool $a_force_number=false)
initRepositoryFilter(array $filter)
findReferencesForObjId(int $a_obj_id)
getCurrentFilter(bool $as_query=false)
getMonthsFilter($a_short=false)
buildValueScale(int $a_max_value, bool $a_anonymize=false, bool $a_format_seconds=false)
searchObjects(array $filter, string $permission, ?array $preset_obj_ids=null, bool $a_check_lp_activation=true)
Search objects that match current filters.
static _getStatusText(int $a_status, ?ilLanguage $a_lng=null)
Get status alt text.
static _lookupType(int $id, bool $reference=false)
static _getIcon(int $obj_id=0, string $size="big", string $type="", bool $offline=false)
Get icon for repository item.
static _lookupTitle(int $obj_id)
This class represents a selection list property in a property form.
isColumnSelected(string $col)
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
setFilterCommand(string $a_val, string $a_caption="")
setLimit(int $a_limit=0, int $a_default_limit=0)
set max.
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
setEnableNumInfo(bool $a_val)
addFilterItem(ilTableFilterItem $a_input_item, bool $a_optional=false)
setEnableTitle(bool $a_enabletitle)
addMultiCommand(string $a_cmd, string $a_text)
setFormAction(string $a_form_action, bool $a_multipart=false)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
setEnableHeader(bool $a_enableheader)
setDefaultOrderField(string $a_defaultorderfield)
setDisableFilterHiding(bool $a_val=true)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
setId(string $a_val)
setDefaultOrderDirection(string $a_defaultorderdirection)
setResetCommand(string $a_val, string $a_caption="")
setData(array $a_data)
Set table data.
This class represents a text property in a property form.
static getObjectLPStatistics(array $a_obj_ids, int $a_year, ?int $a_month=null, bool $a_group_by_day=false)
const ROOT_FOLDER_ID
Definition: constants.php:32
An entity that renders components to a string output.
Definition: Renderer.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
filter(string $filter_id, $class_path, string $cmd, bool $activated=true, bool $expanded=true)
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68
$counter