1<?php
2
3use ILIAS\GlobalScreen\Scope\Tool\Provider\AbstractDynamicToolProvider;
4use ILIAS\GlobalScreen\ScreenContext\Stack\CalledContexts;
5use ILIAS\GlobalScreen\ScreenContext\Stack\ContextCollection;
6
7/**
8 * Taxonomy GS tool provider
9 *
10 * @author Alex Killing <killing@leifos.com>
11 */
12class ilExerciseGSToolProvider extends AbstractDynamicToolProvider
13{
14    const SHOW_EXC_ASSIGNMENT_INFO = 'show_exc_assignment_info';
15    const EXC_ASS_IDS = 'exc_ass_ids';
16    const EXC_ASS_BUTTONS = "exc_ass_buttons";
17
18    /**
19     * @inheritDoc
20     */
21    public function isInterestedInContexts() : ContextCollection
22    {
23        return $this->context_collection->main()->main();
24    }
25
26
27    /**
28     * @inheritDoc
29     */
30    public function getToolsForContextStack(CalledContexts $called_contexts) : array
31    {
32        global $DIC;
33
34        $lng = $DIC->language();
35        $lng->loadLanguageModule("exc");
36
37        $title = $lng->txt("exc_assignment");
38        $icon = $this->dic->ui()->factory()->symbol()->icon()->standard("exc", $title)->withIsOutlined(true);
39
40        $tools = [];
41        $additional_data = $called_contexts->current()->getAdditionalData();
42        if ($additional_data->is(self::SHOW_EXC_ASSIGNMENT_INFO, true)) {
43            $iff = function ($id) {
44                return $this->identification_provider->contextAwareIdentifier($id);
45            };
46            $l = function (string $content) {
47                return $this->dic->ui()->factory()->legacy($content);
48            };
49            $tools[] = $this->factory->tool($iff("exc_ass_info"))
50                ->withTitle($title)
51                ->withSymbol($icon)
52                ->withContentWrapper(function () use ($l, $additional_data) {
53                    return $l($this->getAssignmentInfo(
54                        $additional_data->get(self::EXC_ASS_IDS),
55                        $additional_data->get(self::EXC_ASS_BUTTONS)
56                    ));
57                });
58        }
59
60        return $tools;
61    }
62
63    /**
64     * @param $ass_id
65     * @return string
66     */
67    private function getAssignmentInfo($ass_ids, $buttons) : string
68    {
69        global $DIC;
70
71        $lng = $DIC->language();
72        $user = $DIC->user();
73        $ui = $DIC->ui();
74        $access = $DIC->access();
75
76
77        foreach ($ass_ids as $ass_id) {
78            $info = new ilExAssignmentInfo($ass_id, $user->getId());
79            $exc_id = ilExAssignment::lookupExerciseId($ass_id);
80            foreach (ilObject::_getAllReferences($exc_id) as $ref_id) {
81                if ($access->checkAccess("read", "", $ref_id)) {
82                    $readable_ref_id = $ref_id;
83                }
84            }
85
86            $tpl = new ilTemplate("tpl.ass_info_tool.html", true, true, "Modules/Exercise");
87            $assignment = new ilExAssignment($ass_id);
88
89            $title = ilObject::_lookupTitle($exc_id) . ": " . $assignment->getTitle();
90            if ($readable_ref_id > 0) {
91                $title = $ui->renderer()->render(
92                    $ui->factory()->link()->standard($title, ilLink::_getLink($readable_ref_id))
93                );
94            }
95
96            $this->addSection($tpl, $lng->txt("exc_assignment"), $title);
97
98            // schedule info
99            $schedule = $info->getScheduleInfo();
100            $list = $ui->factory()->listing()->unordered(array_map(function ($i) {
101                return $i["txt"] . ": " . $i["value"];
102            }, $schedule));
103            $this->addSection($tpl, $lng->txt("exc_schedule"), $ui->renderer()->render($list));
104
105            // latest submission
106            $subm = $info->getSubmissionInfo();
107            if (isset($subm["submitted"])) {
108                $this->addSection($tpl, $subm["submitted"]["txt"], $subm["submitted"]["value"]);
109            }
110
111            // instruction
112            $inst = $info->getInstructionInfo();
113            if (count($inst)) {
114                $this->addSection($tpl, $inst["instruction"]["txt"], $inst["instruction"]["value"]);
115            }
116
117            // instruction files
118            $files = $info->getInstructionFileInfo($readable_ref_id);
119            if (is_array($files)) {
120                $list = $ui->factory()->listing()->unordered(array_map(function ($i) use ($ui) {
121                    $v = $i["txt"];
122                    if ($i["value"] != "") {
123                        $v = $ui->renderer()->render($ui->factory()->button()->shy($v, $i["value"]));
124                    }
125                    return $v;
126                }, $files));
127                $this->addSection($tpl, $lng->txt("exc_instruction_files"), $ui->renderer()->render($list));
128            }
129
130            // buttons
131            if (is_array($buttons[$ass_id])) {
132                $tpl->setVariable("BUTTONS", implode(" ", array_map(function ($b) use ($ui) {
133                    return $ui->renderer()->render($b);
134                }, $buttons[$ass_id])));
135            }
136
137            $tpl->setCurrentBlock("ass_info");
138            $tpl->parseCurrentBlock();
139        }
140
141        return $tpl->get();
142    }
143
144    /**
145     * Add section
146     *
147     * @param ilTemplate $tpl
148     * @param string $title
149     * @param string $content
150     */
151    protected function addSection(ilTemplate $tpl, string $title, string $content)
152    {
153        $tpl->setCurrentBlock("section");
154        $tpl->setVariable("TITLE", $title);
155        $tpl->setVariable("CONTENT", $content);
156        $tpl->parseCurrentBlock();
157    }
158}
159