1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Class ilTestParticipantsTimeExtensionGUI
7 *
8 * @author    Björn Heyser <info@bjoernheyser.de>
9 * @version    $Id$
10 *
11 * @package    Modules/Test
12 *
13 * @ilCtrl_Calls ilTestParticipantsTimeExtensionGUI: ilTimingOverviewTableGUI
14 */
15class ilTestParticipantsTimeExtensionGUI
16{
17    /**
18     * Command Constants
19     */
20    const CMD_SHOW_LIST = 'showList';
21    const CMD_SHOW_FORM = 'showForm';
22    const CMD_SET_TIMING = 'setTiming';
23
24    /**
25     * @var ilObjTest
26     */
27    protected $testObj;
28
29    /**
30     * ilTestParticipantsTimeExtensionGUI constructor.
31     * @param ilObjTest $testObj
32     */
33    public function __construct(ilObjTest $testObj)
34    {
35        $this->testObj = $testObj;
36    }
37
38    /**
39     * @return ilObjTest
40     */
41    public function getTestObj()
42    {
43        return $this->testObj;
44    }
45
46    /**
47     * @param ilObjTest $testObj
48     */
49    public function setTestObj($testObj)
50    {
51        $this->testObj = $testObj;
52    }
53
54    /**
55     * @return bool
56     */
57    protected function isExtraTimeFeatureAvailable()
58    {
59        if (!($this->getTestObj()->getProcessingTimeInSeconds() > 0)) {
60            return false;
61        }
62
63        if ($this->getTestObj()->getNrOfTries() != 1) {
64            return false;
65        }
66
67        return true;
68    }
69
70    /**
71     * Execute Command
72     */
73    public function executeCommand()
74    {
75        global $DIC; /* @var ILIAS\DI\Container $DIC */
76
77        if (!$this->isExtraTimeFeatureAvailable()) {
78            ilObjTestGUI::accessViolationRedirect();
79        }
80
81        switch ($DIC->ctrl()->getNextClass($this)) {
82            default:
83
84                $command = $DIC->ctrl()->getCmd(self::CMD_SHOW_LIST) . 'Cmd';
85
86                $this->{$command}();
87        }
88    }
89
90    /**
91     * show list command
92     */
93    public function showListCmd()
94    {
95        global $DIC; /* @var ILIAS\DI\Container $DIC */
96
97        include_once "./Modules/Test/classes/tables/class.ilTimingOverviewTableGUI.php";
98        $tableGUI = new ilTimingOverviewTableGUI($this, self::CMD_SHOW_LIST);
99        $tableGUI->addCommandButton(self::CMD_SHOW_FORM, $DIC->language()->txt('timing'));
100
101
102        require_once 'Modules/Test/classes/class.ilTestParticipantList.php';
103        $participantList = new ilTestParticipantList($this->getTestObj());
104        $participantList->initializeFromDbRows($this->getTestObj()->getTestParticipants());
105
106        $participantList = $participantList->getAccessFilteredList(
107            ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId())
108        );
109
110        $addons = $this->getTestObj()->getTimeExtensionsOfParticipants();
111
112        $tableData = array();
113        foreach ($participantList as $participant) {
114            $tblRow = array();
115
116            $time = $this->getTestObj()->getStartingTimeOfUser($participant->getActiveId());
117            if ($time) {
118                $started = $DIC->language()->txt('tst_started') . ': ' . ilDatePresentation::formatDate(
119                    new ilDateTime($time, IL_CAL_UNIX)
120                );
121
122                $tblRow['started'] = $started;
123            } else {
124                $tblRow['started'] = '';
125            }
126
127            if ($addons[$participant->getActiveId()] > 0) {
128                $tblRow['extratime'] = $addons[$participant->getActiveId()];
129            }
130
131            $tblRow['login'] = $participant->getLogin();
132
133            if ($this->getTestObj()->getAnonymity()) {
134                $tblRow['name'] = $DIC->language()->txt("anonymous");
135            } else {
136                $tblRow['name'] = $participant->getLastname() . ', ' . $participant->getFirstname();
137            }
138
139            $tableData[] = $tblRow;
140        }
141
142        $tableGUI->setData($tableData);
143
144        $DIC->ui()->mainTemplate()->setContent($DIC->ctrl()->getHTML($tableGUI));
145    }
146
147    /**
148     * show form command
149     */
150    protected function showFormCmd()
151    {
152        global $DIC; /* @var ILIAS\DI\Container $DIC */
153
154        $DIC->ui()->mainTemplate()->setContent($this->buildTimingForm()->getHTML());
155    }
156
157    /**
158     * @return ilPropertyFormGUI
159     */
160    protected function buildTimingForm()
161    {
162        global $DIC; /* @var ILIAS\DI\Container $DIC */
163
164        include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
165        $form = new ilPropertyFormGUI();
166        $form->setFormAction($DIC->ctrl()->getFormAction($this));
167        $form->setTableWidth("100%");
168        $form->setId("tst_change_workingtime");
169        $form->setTitle($DIC->language()->txt("tst_change_workingtime"));
170
171        // test users
172        require_once 'Modules/Test/classes/class.ilTestParticipantList.php';
173        $participantList = new ilTestParticipantList($this->getTestObj());
174        $participantList->initializeFromDbRows($this->getTestObj()->getTestParticipants());
175
176        $participantList = $participantList->getAccessFilteredList(
177            ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId())
178        );
179
180        $addons = $this->getTestObj()->getTimeExtensionsOfParticipants();
181
182        $participantslist = new ilSelectInputGUI($DIC->language()->txt('participants'), "participant");
183
184        $options = array(
185            '' => $DIC->language()->txt('please_select'),
186            '0' => $DIC->language()->txt('all_participants')
187        );
188
189        foreach ($participantList as $participant) {
190            $started = "";
191
192            if ($this->getTestObj()->getAnonymity()) {
193                $name = $DIC->language()->txt("anonymous");
194            } else {
195                $name = $participant->getLastname() . ', ' . $participant->getFirstname();
196            }
197
198            $time = $this->getTestObj()->getStartingTimeOfUser($participant->getActiveId());
199            if ($time) {
200                $started = ", " . $DIC->language()->txt('tst_started') . ': ' . ilDatePresentation::formatDate(new ilDateTime($time, IL_CAL_UNIX));
201            }
202
203            if ($addons[$participant->getActiveId()] > 0) {
204                $started .= ", " . $DIC->language()->txt('extratime') . ': ' . $addons[$participant->getActiveId()] . ' ' . $DIC->language()->txt('minutes');
205            }
206
207            $options[$participant->getActiveId()] = $participant->getLogin() . ' (' . $name . ')' . $started;
208        }
209
210        $participantslist->setRequired(true);
211        $participantslist->setOptions($options);
212        $form->addItem($participantslist);
213
214        // extra time
215        $extratime = new ilNumberInputGUI($DIC->language()->txt("extratime"), "extratime");
216        $extratime->setInfo($DIC->language()->txt('tst_extratime_info'));
217        $extratime->setRequired(true);
218        $extratime->setMinValue(0);
219        $extratime->setMinvalueShouldBeGreater(false);
220        $extratime->setSuffix($DIC->language()->txt('minutes'));
221        $extratime->setSize(5);
222        $form->addItem($extratime);
223
224        if (is_array($_POST) && strlen($_POST['cmd']['timing'])) {
225            $form->setValuesByArray($_POST);
226        }
227
228        $form->addCommandButton(self::CMD_SET_TIMING, $DIC->language()->txt("save"));
229        $form->addCommandButton(self::CMD_SHOW_LIST, $DIC->language()->txt("cancel"));
230
231        return $form;
232    }
233
234    /**
235     * set timing command
236     */
237    protected function setTimingCmd()
238    {
239        global $DIC; /* @var ILIAS\DI\Container $DIC */
240
241        $form = $this->buildTimingForm();
242
243        if ($form->checkInput()) {
244            $this->getTestObj()->addExtraTime(
245                $form->getInput('participant'),
246                $form->getInput('extratime')
247            );
248
249            ilUtil::sendSuccess(sprintf($DIC->language()->txt('tst_extratime_added'), $form->getInput('extratime')), true);
250            $DIC->ctrl()->redirect($this, self::CMD_SHOW_LIST);
251        }
252
253        $DIC->ui()->mainTemplate()->setVariable("ADM_CONTENT", $form->getHTML());
254    }
255}
256