1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
5/**
6 * @author        Björn Heyser <bheyser@databay.de>
7 * @version        $Id$
8 *
9 * @package     Modules/Test
10 */
11class ilTestRandomQuestionSetConfigStateMessageHandler
12{
13    /**
14     * @var ILIAS\DI\Container
15     */
16    protected $DIC;
17    /**
18     * @var ilLanguage
19     */
20    protected $lng;
21
22    /**
23     * @var ilCtrl
24     */
25    protected $ctrl;
26
27    /**
28     * @var ilTestRandomQuestionSetConfigGUI
29     */
30    protected $targetGUI;
31
32    const CONTEXT_GENERAL_CONFIG = 'generalConfigContext';
33    const CONTEXT_POOL_SELECTION = 'poolSelectionContext';
34
35    /**
36     * @var string
37     */
38    protected $context;
39
40    /**
41     * @var bool
42     */
43    protected $participantDataExists;
44
45    /**
46     * @var ilTestRandomQuestionSetNonAvailablePool[]
47     */
48    protected $lostPools;
49
50    /**
51     * @var ilTestRandomQuestionSetConfig
52     */
53    protected $questionSetConfig;
54
55    /**
56     * @var bool
57     */
58    protected $validationFailed;
59
60    /**
61     * @var array
62     */
63    protected $validationReports;
64
65    /**
66     * @param ilLanguage $lng
67     */
68    public function __construct(ilLanguage $lng, ilCtrl $ctrl)
69    {
70        global $DIC; /* @var ILIAS\DI\Container $DIC */
71        $this->DIC = $DIC;
72
73        $this->lng = $lng;
74        $this->ctrl = $ctrl;
75        $this->validationFailed = false;
76        $this->validationReports = array();
77    }
78
79    /**
80     * @return ilTestRandomQuestionSetNonAvailablePool[]
81     */
82    public function getLostPools()
83    {
84        return $this->lostPools;
85    }
86
87    /**
88     * @param ilTestRandomQuestionSetNonAvailablePool[] $lostPools
89     */
90    public function setLostPools($lostPools)
91    {
92        $this->lostPools = $lostPools;
93    }
94
95    /**
96     * @return boolean
97     */
98    public function doesParticipantDataExists()
99    {
100        return $this->participantDataExists;
101    }
102
103    /**
104     * @param boolean $participantDataExists
105     */
106    public function setParticipantDataExists($participantDataExists)
107    {
108        $this->participantDataExists = $participantDataExists;
109    }
110
111    /**
112     * @return ilTestRandomQuestionSetConfigGUI
113     */
114    public function getTargetGUI()
115    {
116        return $this->targetGUI;
117    }
118
119    /**
120     * @param ilTestRandomQuestionSetConfigGUI $targetGUI
121     */
122    public function setTargetGUI($targetGUI)
123    {
124        $this->targetGUI = $targetGUI;
125    }
126
127    /**
128     * @return string
129     */
130    public function getContext()
131    {
132        return $this->context;
133    }
134
135    /**
136     * @param string $context
137     */
138    public function setContext($context)
139    {
140        $this->context = $context;
141    }
142
143    /**
144     * @return ilTestRandomQuestionSetConfig
145     */
146    public function getQuestionSetConfig()
147    {
148        return $this->questionSetConfig;
149    }
150
151    /**
152     * @param ilTestRandomQuestionSetConfig $questionSetConfig
153     */
154    public function setQuestionSetConfig($questionSetConfig)
155    {
156        $this->questionSetConfig = $questionSetConfig;
157    }
158
159    /**
160     * @return bool
161     */
162    public function isValidationFailed()
163    {
164        return $this->validationFailed;
165    }
166
167    /**
168     * @param bool $validationFailed
169     */
170    public function setValidationFailed($validationFailed)
171    {
172        $this->validationFailed = $validationFailed;
173    }
174
175    /**
176     * @return array
177     */
178    public function getValidationReportHtml()
179    {
180        return implode('<br />', $this->validationReports);
181    }
182
183    /**
184     * @return array
185     */
186    public function hasValidationReports()
187    {
188        return count($this->validationReports);
189    }
190
191    /**
192     * @param string $validationReport
193     */
194    public function addValidationReport($validationReport)
195    {
196        $this->validationReports[] = $validationReport;
197    }
198
199    public function handle()
200    {
201        if ($this->isNoAvailableQuestionPoolsHintRequired()) {
202            $this->addValidationReport($this->lng->txt('tst_msg_rand_quest_set_no_pools_available'));
203        } elseif ($this->getLostPools()) {
204            $this->populateMessage($this->buildLostPoolsReportMessage());
205        } elseif (!$this->questionSetConfig->isQuestionAmountConfigComplete()) {
206            $this->addValidationReport($this->lng->txt('tst_msg_rand_quest_set_incomplete_quest_amount_cfg'));
207
208            if ($this->isQuestionAmountConfigPerTestHintRequired()) {
209                $this->addValidationReport(
210                    sprintf(
211                        $this->lng->txt('tst_msg_rand_quest_set_change_quest_amount_here'),
212                        $this->buildGeneralConfigSubTabLink()
213                    )
214                );
215            } elseif ($this->isQuestionAmountConfigPerPoolHintRequired()) {
216                $this->addValidationReport(
217                    sprintf(
218                        $this->lng->txt('tst_msg_rand_quest_set_change_quest_amount_here'),
219                        $this->buildQuestionSelectionSubTabLink()
220                    )
221                );
222            }
223        } elseif (!$this->questionSetConfig->hasSourcePoolDefinitions()) {
224            $this->addValidationReport($this->lng->txt('tst_msg_rand_quest_set_no_src_pool_defs'));
225        }
226        // fau: delayCopyRandomQuestions - show info message if date of last synchronisation is empty
227        elseif ($this->questionSetConfig->getLastQuestionSyncTimestamp() == 0) {
228            $message = $this->DIC->language()->txt('tst_msg_rand_quest_set_not_sync');
229            $button = $this->buildQuestionStageRebuildButton();
230            $msgBox = $this->DIC->ui()->factory()->messageBox()->info($message)->withButtons(array($button));
231            $this->populateMessage($this->DIC->ui()->renderer()->render($msgBox));
232        }
233        // fau.
234
235        elseif (!$this->questionSetConfig->isQuestionSetBuildable()) {
236            $this->setValidationFailed(true);
237            $this->addValidationReport($this->lng->txt('tst_msg_rand_quest_set_pass_not_buildable'));
238
239            //fau: fixRandomTestBuildable - show the messages if set is not buildable
240            $this->addValidationReport(implode('<br />', $this->questionSetConfig->getBuildableMessages()));
241        //fau.
242        } elseif ($this->questionSetConfig->getLastQuestionSyncTimestamp()) {
243            $message = $this->lng->txt('tst_msg_rand_quest_set_pass_buildable');
244
245            $syncDate = new ilDateTime(
246                $this->questionSetConfig->getLastQuestionSyncTimestamp(),
247                IL_CAL_UNIX
248            );
249
250            $message .= sprintf(
251                $this->lng->txt('tst_msg_rand_quest_set_stage_pool_last_sync'),
252                ilDatePresentation::formatDate($syncDate)
253            );
254
255            if (!$this->doesParticipantDataExists() && !$this->getLostPools()) {
256                $msgBox = $this->DIC->ui()->factory()->messageBox()->info($message)->withButtons(
257                    array($this->buildQuestionStageRebuildButton())
258                );
259            } else {
260                $msgBox = $this->DIC->ui()->factory()->messageBox()->info($message);
261            }
262
263            $this->populateMessage($this->DIC->ui()->renderer()->render($msgBox));
264        }
265    }
266
267    private function buildLostQuestionPoolsString()
268    {
269        $titles = array();
270
271        foreach ($this->getLostPools() as $lostPool) {
272            $titles[] = $lostPool->getTitle();
273        }
274
275        return implode(', ', $titles);
276    }
277
278    private function getAfterRebuildQuestionStageCommand()
279    {
280        switch ($this->getContext()) {
281            case self::CONTEXT_POOL_SELECTION:
282
283                return ilTestRandomQuestionSetConfigGUI::CMD_SHOW_SRC_POOL_DEF_LIST;
284
285            case self::CONTEXT_GENERAL_CONFIG:
286            default:
287
288                return ilTestRandomQuestionSetConfigGUI::CMD_SHOW_GENERAL_CONFIG_FORM;
289        }
290    }
291
292    /**
293     * @return \ILIAS\UI\Component\Button\Standard
294     */
295    private function buildQuestionStageRebuildButton()
296    {
297        $this->ctrl->setParameter(
298            $this->getTargetGUI(),
299            ilTestRandomQuestionSetConfigGUI::HTTP_PARAM_AFTER_REBUILD_QUESTION_STAGE_CMD,
300            $this->getAfterRebuildQuestionStageCommand()
301        );
302
303        $href = $this->ctrl->getLinkTarget(
304            $this->getTargetGUI(),
305            ilTestRandomQuestionSetConfigGUI::CMD_BUILD_QUESTION_STAGE
306        );
307        $label = $this->lng->txt('tst_btn_rebuild_random_question_stage');
308
309        return $this->DIC->ui()->factory()->button()->standard($label, $href);
310    }
311
312    private function buildGeneralConfigSubTabLink()
313    {
314        $href = $this->ctrl->getLinkTarget(
315            $this->getTargetGUI(),
316            ilTestRandomQuestionSetConfigGUI::CMD_SHOW_GENERAL_CONFIG_FORM
317        );
318
319        $label = $this->lng->txt('tst_rnd_quest_cfg_tab_general');
320
321        return "<a href=\"{$href}\">{$label}</a>";
322    }
323
324    private function buildQuestionSelectionSubTabLink()
325    {
326        $href = $this->ctrl->getLinkTarget(
327            $this->getTargetGUI(),
328            ilTestRandomQuestionSetConfigGUI::CMD_SHOW_SRC_POOL_DEF_LIST
329        );
330
331        $label = $this->lng->txt('tst_rnd_quest_cfg_tab_pool');
332
333        return "<a href=\"{$href}\">{$label}</a>";
334    }
335
336    /**
337     * @param $currentRequestCmd
338     * @return bool
339     */
340    private function isNoAvailableQuestionPoolsHintRequired()
341    {
342        if ($this->getContext() != self::CONTEXT_POOL_SELECTION) {
343            return false;
344        }
345
346        if ($this->questionSetConfig->doesSelectableQuestionPoolsExist()) {
347            return false;
348        }
349
350        return true;
351    }
352
353    /**
354     * @param $currentRequestCmd
355     * @return bool
356     */
357    private function isQuestionAmountConfigPerPoolHintRequired()
358    {
359        if ($this->getContext() != self::CONTEXT_GENERAL_CONFIG) {
360            return false;
361        }
362
363        if (!$this->questionSetConfig->isQuestionAmountConfigurationModePerPool()) {
364            return false;
365        }
366
367        return true;
368    }
369
370    /**
371     * @param $currentRequestCmd
372     * @return bool
373     */
374    private function isQuestionAmountConfigPerTestHintRequired()
375    {
376        if ($this->getContext() != self::CONTEXT_POOL_SELECTION) {
377            return false;
378        }
379
380        if (!$this->questionSetConfig->isQuestionAmountConfigurationModePerTest()) {
381            return false;
382        }
383
384        return true;
385    }
386
387    /**
388     * @return string
389     */
390    protected function buildLostPoolsReportMessage()
391    {
392        $report = sprintf(
393            $this->lng->txt('tst_msg_rand_quest_set_lost_pools'),
394            $this->buildLostQuestionPoolsString()
395        );
396
397        if ($this->getContext() == self::CONTEXT_GENERAL_CONFIG) {
398            $action = $this->ctrl->getLinkTarget(
399                $this->getTargetGUI(),
400                ilTestRandomQuestionSetConfigGUI::CMD_SHOW_SRC_POOL_DEF_LIST
401            );
402
403            $link = $this->DIC->ui()->factory()->link()->standard(
404                $this->lng->txt('tst_msg_rand_quest_set_lost_pools_link'),
405                $action
406            );
407
408            $msgBox = $this->DIC->ui()->factory()->messageBox()->info($report)->withLinks(array($link));
409        } else {
410            $msgBox = $this->DIC->ui()->factory()->messageBox()->info($report);
411        }
412
413        return $this->DIC->ui()->renderer()->render($msgBox);
414    }
415
416    /**
417     * @param $message
418     */
419    protected function populateMessage($message)
420    {
421        $this->DIC->ui()->mainTemplate()->setCurrentBlock('mess');
422        $this->DIC->ui()->mainTemplate()->setVariable('MESSAGE', $message);
423        $this->DIC->ui()->mainTemplate()->parseCurrentBlock();
424    }
425}
426