1<?php
2
3/* Copyright (c) 2015 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once("Services/Block/classes/class.ilBlockGUI.php");
6require_once('./Modules/StudyProgramme/classes/class.ilObjStudyProgrammeAdmin.php');
7
8/**
9 * Personal Desktop-Presentation for the Study Programme
10 *
11 * @author : Richard Klees <richard.klees@concepts-and-training.de>
12 * @author : Stefan Hecken <stefan.hecken@concepts-and-training.de>
13 * @ilCtrl_IsCalledBy ilPDStudyProgrammeSimpleListGUI: ilColumnGUI
14 */
15class ilPDStudyProgrammeSimpleListGUI extends ilBlockGUI
16{
17    const BLOCK_TYPE = "prgsimplelist";
18
19    /**
20     * @var ilLanguage
21     */
22    protected $il_lng;
23
24    /**
25     * @var ilUser
26     */
27    protected $il_user;
28
29    /**
30     * @var ilAccessHandler
31     */
32    protected $il_access;
33
34    /**
35     * @var ilSetting
36     */
37    protected $il_setting;
38
39    /**
40     * @var ilStudyProgrammeUserAssignment[]
41     */
42    protected $users_assignments;
43
44    /**
45    * @var visible_on_pd_mode
46    */
47    protected $visible_on_pd_mode;
48
49    /**
50    * @var show_info_message
51    */
52    protected $show_info_message;
53
54    public function __construct()
55    {
56        global $DIC;
57
58        parent::__construct();
59
60        $lng = $DIC['lng'];
61        $ilUser = $DIC['ilUser'];
62        $ilAccess = $DIC['ilAccess'];
63        $ilSetting = $DIC['ilSetting'];
64        $this->il_lng = $lng;
65        $this->il_user = $ilUser;
66        $this->il_access = $ilAccess;
67        $this->il_setting = $ilSetting;
68        $this->il_logger = ilLoggerFactory::getLogger('prg');
69
70        $this->sp_user_assignment_db = ilStudyProgrammeDIC::dic()['ilStudyProgrammeUserAssignmentDB'];
71
72        // No need to load data, as we won't display this.
73        if (!$this->shouldShowThisList()) {
74            return;
75        }
76
77        $this->readUsersAssignments();
78        //check which kind of option is selected in settings
79        $this->readVisibleOnPDMode();
80        //check to display info message if option "read" is selected
81        $this->readToShowInfoMessage();
82
83        // As this won't be visible we don't have to initialize this.
84        if (!$this->userHasReadableStudyProgrammes()) {
85            return;
86        }
87
88        $this->setTitle($this->il_lng->txt("objs_prg"));
89    }
90
91    public function getHTML()
92    {
93        // TODO: This should be determined from somewhere up in the hierarchy, as
94        // this will lead to problems, when e.g. a command changes. But i don't see
95        // how atm...
96        if (!$this->shouldShowThisList()) {
97            return "";
98        }
99
100        if (!$this->userHasReadableStudyProgrammes()) {
101            return "";
102        }
103        return parent::getHTML();
104    }
105
106    public function getDataSectionContent()
107    {
108        $content = "";
109        foreach ($this->users_assignments as $assignment) {
110            if (!$this->isReadable($assignment)) {
111                continue;
112            }
113
114            try {
115                $list_item = $this->new_ilStudyProgrammeAssignmentListGUI($assignment);
116                $list_item->setShowInfoMessage($this->show_info_message);
117                $list_item->setVisibleOnPDMode($this->visible_on_pd_mode);
118                $content .= $list_item->getHTML();
119            } catch (ilStudyProgrammeNoProgressForAssignmentException $e) {
120                $this->il_logger->alert("$e");
121            } catch (ilStudyProgrammeTreeException $e) {
122                $this->il_logger->alert("$e");
123            }
124        }
125        return $content;
126    }
127
128    /**
129     * @inheritdoc
130     */
131    public function getBlockType() : string
132    {
133        return self::BLOCK_TYPE;
134    }
135
136    /**
137     * @inheritdoc
138     */
139    protected function isRepositoryObject() : bool
140    {
141        return false;
142    }
143
144    public function fillDataSection()
145    {
146        assert($this->userHasReadableStudyProgrammes()); // We should not get here.
147        $this->tpl->setVariable("BLOCK_ROW", $this->getDataSectionContent());
148    }
149
150
151    protected function userHasVisibleStudyProgrammes()
152    {
153        if (count($this->users_assignments) == 0) {
154            return false;
155        }
156        foreach ($this->users_assignments as $assignment) {
157            if ($this->isVisible($assignment)) {
158                return true;
159            }
160        }
161        return false;
162    }
163
164    protected function userHasReadableStudyProgrammes()
165    {
166        if (count($this->users_assignments) == 0) {
167            return false;
168        }
169        foreach ($this->users_assignments as $assignment) {
170            if ($this->isReadable($assignment)) {
171                return true;
172            }
173        }
174        return false;
175    }
176
177    protected function readVisibleOnPDMode()
178    {
179        $this->visible_on_pd_mode = $this->il_setting->get(ilObjStudyProgrammeAdmin::SETTING_VISIBLE_ON_PD);
180    }
181
182    protected function hasPermission(ilStudyProgrammeUserAssignment $assignment, $permission)
183    {
184        $prg = $assignment->getStudyProgramme();
185        return $this->il_access->checkAccess($permission, "", $prg->getRefId(), "prg", $prg->getId());
186    }
187
188    protected function readToShowInfoMessage()
189    {
190        $viewSettings = new ilPDSelectedItemsBlockViewSettings($GLOBALS['DIC']->user(), (int) $_GET['view']);
191        $this->show_info_message = $viewSettings->isStudyProgrammeViewActive();
192    }
193
194    protected function isVisible(ilStudyProgrammeUserAssignment $assignment)
195    {
196        return $this->hasPermission($assignment, "visible");
197    }
198
199    protected function isReadable(ilStudyProgrammeUserAssignment $assignment)
200    {
201        if ($this->visible_on_pd_mode == ilObjStudyProgrammeAdmin::SETTING_VISIBLE_ON_PD_ALLWAYS) {
202            return true;
203        }
204
205        return $this->hasPermission($assignment, "read");
206    }
207
208    protected function shouldShowThisList()
209    {
210        global $DIC;
211        $ctrl = $DIC->ctrl();
212        return ($_GET["cmd"] == "jumpToSelectedItems" ||
213                ($ctrl->getCmdClass() == "ildashboardgui" && $ctrl->getCmd() == "show")
214            ) && !$_GET["expand"];
215    }
216
217    protected function readUsersAssignments()
218    {
219        $this->users_assignments = $this->sp_user_assignment_db->getInstancesOfUser($this->il_user->getId());
220    }
221
222    protected function new_ilStudyProgrammeAssignmentListGUI(ilStudyProgrammeUserAssignment $a_assignment)
223    {
224        $progress = $a_assignment->getStudyProgramme()->getProgressForAssignment($a_assignment->getId());
225        $progress_gui = new ilStudyProgrammeProgressListGUI($progress);
226        $progress_gui->setOnlyRelevant(true);
227        return $progress_gui;
228    }
229}
230