1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Class ilTestParticipantsGUI
7 *
8 * @author    Björn Heyser <info@bjoernheyser.de>
9 * @version    $Id$
10 *
11 * @package    Modules/Test
12 *
13 * @ilCtrl_Calls ilTestParticipantsGUI: ilTestParticipantsTableGUI
14 * @ilCtrl_Calls ilTestParticipantsGUI: ilRepositorySearchGUI
15 * @ilCtrl_Calls ilTestParticipantsGUI: ilTestEvaluationGUI
16 */
17class ilTestParticipantsGUI
18{
19    /**
20     * Command/Callback Constants
21     */
22
23    const CMD_SHOW = 'show';
24    const CMD_SET_FILTER = 'setFilter';
25    const CMD_RESET_FILTER = 'resetFilter';
26    const CMD_SAVE_CLIENT_IP = 'saveClientIp';
27
28    const CALLBACK_ADD_PARTICIPANT = 'addParticipants';
29
30    /**
31     * @var ilObjTest
32     */
33    protected $testObj;
34
35    /**
36     * @var ilTestQuestionSetConfig
37     */
38    protected $questionSetConfig;
39
40    /**
41     * @var ilTestObjectiveOrientedContainer
42     */
43    protected $objectiveParent;
44
45    /**
46     * @var ilTestAccess
47     */
48    protected $testAccess;
49
50    /**
51     * ilTestParticipantsGUI constructor.
52     * @param ilObjTest $testObj
53     */
54    public function __construct(ilObjTest $testObj, ilTestQuestionSetConfig $questionSetConfig)
55    {
56        $this->testObj = $testObj;
57        $this->questionSetConfig = $questionSetConfig;
58    }
59
60    /**
61     * @return ilObjTest
62     */
63    public function getTestObj()
64    {
65        return $this->testObj;
66    }
67
68    /**
69     * @param ilObjTest $testObj
70     */
71    public function setTestObj($testObj)
72    {
73        $this->testObj = $testObj;
74    }
75
76    /**
77     * @return ilTestQuestionSetConfig
78     */
79    public function getQuestionSetConfig()
80    {
81        return $this->questionSetConfig;
82    }
83
84    /**
85     * @param ilTestQuestionSetConfig $questionSetConfig
86     */
87    public function setQuestionSetConfig($questionSetConfig)
88    {
89        $this->questionSetConfig = $questionSetConfig;
90    }
91
92    /**
93     * @return ilTestObjectiveOrientedContainer
94     */
95    public function getObjectiveParent() : ilTestObjectiveOrientedContainer
96    {
97        return $this->objectiveParent;
98    }
99
100    /**
101     * @param ilTestObjectiveOrientedContainer $objectiveParent
102     */
103    public function setObjectiveParent(ilTestObjectiveOrientedContainer $objectiveParent)
104    {
105        $this->objectiveParent = $objectiveParent;
106    }
107
108    /**
109     * @return ilTestAccess
110     */
111    public function getTestAccess() : ilTestAccess
112    {
113        return $this->testAccess;
114    }
115
116    /**
117     * @param ilTestAccess $testAccess
118     */
119    public function setTestAccess(ilTestAccess $testAccess)
120    {
121        $this->testAccess = $testAccess;
122    }
123
124    /**
125     * Execute Command
126     */
127    public function executeCommand()
128    {
129        global $DIC; /* @var ILIAS\DI\Container $DIC */
130
131
132        switch ($DIC->ctrl()->getNextClass($this)) {
133            case 'ilrepositorysearchgui':
134
135                require_once 'Modules/Test/classes/class.ilTestParticipantAccessFilter.php';
136                require_once 'Services/Search/classes/class.ilRepositorySearchGUI.php';
137
138                $gui = new ilRepositorySearchGUI();
139                $gui->setCallback($this, self::CALLBACK_ADD_PARTICIPANT, array());
140
141                $gui->addUserAccessFilterCallable(ilTestParticipantAccessFilter::getManageParticipantsUserFilter(
142                    $this->getTestObj()->getRefId()
143                ));
144
145
146                $DIC->ctrl()->setReturn($this, self::CMD_SHOW);
147                $DIC->ctrl()->forwardCommand($gui);
148
149                break;
150
151            case "iltestevaluationgui":
152
153                require_once 'Modules/Test/classes/class.ilTestEvaluationGUI.php';
154
155                $gui = new ilTestEvaluationGUI($this->getTestObj());
156                $gui->setObjectiveOrientedContainer($this->getObjectiveParent());
157                $gui->setTestAccess($this->getTestAccess());
158                $DIC->tabs()->clearTargets();
159                $DIC->tabs()->clearSubTabs();
160
161                $DIC->ctrl()->forwardCommand($gui);
162
163                break;
164
165            default:
166
167                $command = $DIC->ctrl()->getCmd(self::CMD_SHOW) . 'Cmd';
168                $this->{$command}();
169        }
170    }
171
172    /**
173     * @param array $a_user_ids
174     * @return bool
175     */
176    public function addParticipants($a_user_ids = array())
177    {
178        global $DIC; /* @var ILIAS\DI\Container $DIC */
179
180        require_once 'Modules/Test/classes/class.ilTestParticipantAccessFilter.php';
181        $filterCallback = ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId());
182        $a_user_ids = call_user_func_array($filterCallback, [$a_user_ids]);
183
184        $countusers = 0;
185        // add users
186        if (is_array($a_user_ids)) {
187            $i = 0;
188            foreach ($a_user_ids as $user_id) {
189                $client_ip = $_POST["client_ip"][$i];
190                $this->getTestObj()->inviteUser($user_id, $client_ip);
191                $countusers++;
192                $i++;
193            }
194        }
195        $message = "";
196        if ($countusers) {
197            $message = $DIC->language()->txt("tst_invited_selected_users");
198        }
199        if (strlen($message)) {
200            ilUtil::sendInfo($message, true);
201        } else {
202            ilUtil::sendInfo($DIC->language()->txt("tst_invited_nobody"), true);
203            return false;
204        }
205
206        $DIC->ctrl()->redirect($this, self::CMD_SHOW);
207    }
208
209    /**
210     * @return ilTestParticipantsTableGUI
211     */
212    protected function buildTableGUI()
213    {
214        global $DIC; /* @var ILIAS\DI\Container $DIC */
215
216        require_once 'Modules/Test/classes/tables/class.ilTestParticipantsTableGUI.php';
217        $tableGUI = new ilTestParticipantsTableGUI($this, self::CMD_SHOW);
218
219        $tableGUI->setParticipantHasSolutionsFilterEnabled(
220            $this->getTestObj()->getFixedParticipants()
221        );
222
223        if ($this->getTestObj()->getFixedParticipants()) {
224            $tableGUI->setTitle($DIC->language()->txt('tst_tbl_invited_users'));
225        } else {
226            $tableGUI->setTitle($DIC->language()->txt('tst_tbl_participants'));
227        }
228
229        return $tableGUI;
230    }
231
232    /**
233     * set table filter command
234     */
235    protected function setFilterCmd()
236    {
237        $tableGUI = $this->buildTableGUI();
238        $tableGUI->initFilter($this->getTestObj()->getFixedParticipants());
239        $tableGUI->writeFilterToSession();
240        $tableGUI->resetOffset();
241        $this->showCmd();
242    }
243
244    /**
245     * reset table filter command
246     */
247    protected function resetFilterCmd()
248    {
249        $tableGUI = $this->buildTableGUI();
250        $tableGUI->resetFilter();
251        $tableGUI->resetOffset();
252        $this->showCmd();
253    }
254
255    /**
256     * show command
257     */
258    public function showCmd()
259    {
260        global $DIC; /* @var ILIAS\DI\Container $DIC */
261
262        $tableGUI = $this->buildTableGUI();
263
264        if (!$this->getQuestionSetConfig()->areDepenciesBroken()) {
265            if ($this->getTestObj()->getFixedParticipants()) {
266                $participantList = $this->getTestObj()->getInvitedParticipantList()->getAccessFilteredList(
267                    ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId())
268                );
269
270                $tableGUI->setData($this->applyFilterCriteria($participantList->getParticipantsTableRows()));
271                $tableGUI->setRowKeyDataField('usr_id');
272                $tableGUI->setManageInviteesCommandsEnabled(true);
273                $tableGUI->setDescription($DIC->language()->txt("fixed_participants_hint"));
274            } else {
275                $participantList = $this->getTestObj()->getActiveParticipantList()->getAccessFilteredList(
276                    ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId())
277                );
278
279                $tableGUI->setData($participantList->getParticipantsTableRows());
280                $tableGUI->setRowKeyDataField('active_id');
281            }
282
283            $tableGUI->setManageResultsCommandsEnabled(true);
284
285            $this->initToolbarControls($participantList);
286        }
287
288        $tableGUI->setAnonymity($this->getTestObj()->getAnonymity());
289
290        $tableGUI->initColumns();
291        $tableGUI->initCommands();
292
293        $tableGUI->initFilter();
294        $tableGUI->setFilterCommand(self::CMD_SET_FILTER);
295        $tableGUI->setResetCommand(self::CMD_RESET_FILTER);
296
297        $DIC->ui()->mainTemplate()->setContent($DIC->ctrl()->getHTML($tableGUI));
298    }
299
300    /**
301     * @param array $in_rows
302     * @return array
303     */
304    protected function applyFilterCriteria($in_rows)
305    {
306        global $DIC; /* @var ILIAS\DI\Container $DIC */
307
308        $sess_filter = $_SESSION['form_tst_participants_' . $this->getTestObj()->getRefId()]['selection'];
309        $sess_filter = str_replace('"', '', $sess_filter);
310        $sess_filter = explode(':', $sess_filter);
311        $filter = substr($sess_filter[2], 0, strlen($sess_filter[2]) - 1);
312
313        if ($filter == 'all' || $filter == false) {
314            return $in_rows; #unchanged - no filter.
315        }
316
317        $with_result = array();
318        $without_result = array();
319        foreach ($in_rows as $row) {
320            $result = $DIC->database()->query(
321                'SELECT count(solution_id) count
322				FROM tst_solutions
323				WHERE active_fi = ' . $DIC->database()->quote($row['active_id'])
324            );
325            $count = $DIC->database()->fetchAssoc($result);
326            $count = $count['count'];
327
328            if ($count == 0) {
329                $without_result[] = $row;
330            } else {
331                $with_result[] = $row;
332            }
333        }
334
335        if ($filter == 'withSolutions') {
336            return $with_result;
337        }
338        return $without_result;
339    }
340
341    protected function initToolbarControls(ilTestParticipantList $participantList)
342    {
343        global $DIC; /* @var ILIAS\DI\Container $DIC */
344
345        if ($this->getTestObj()->getFixedParticipants()) {
346            $this->addUserSearchControls($DIC->toolbar());
347        }
348
349        if ($this->getTestObj()->getFixedParticipants() && $participantList->hasUnfinishedPasses()) {
350            $DIC->toolbar()->addSeparator();
351        }
352
353        if ($participantList->hasUnfinishedPasses()) {
354            $this->addFinishAllPassesButton($DIC->toolbar());
355        }
356    }
357
358    /**
359     * @param ilToolbarGUI $toolbar
360     * @param ilLanguage $lng
361     */
362    protected function addUserSearchControls(ilToolbarGUI $toolbar)
363    {
364        global $DIC; /* @var ILIAS\DI\Container $DIC */
365
366        // search button
367        include_once './Services/Search/classes/class.ilRepositorySearchGUI.php';
368        ilRepositorySearchGUI::fillAutoCompleteToolbar(
369            $this,
370            $toolbar,
371            array(
372                'auto_complete_name' => $DIC->language()->txt('user'),
373                'submit_name' => $DIC->language()->txt('add')
374            )
375        );
376
377        require_once  'Services/UIComponent/Button/classes/class.ilLinkButton.php';
378        $search_btn = ilLinkButton::getInstance();
379        $search_btn->setCaption('tst_search_users');
380        $search_btn->setUrl($DIC->ctrl()->getLinkTargetByClass('ilRepositorySearchGUI', 'start'));
381
382        $toolbar->addSeparator();
383        $toolbar->addButtonInstance($search_btn);
384    }
385
386    /**
387     * @param ilToolbarGUI $toolbar
388     */
389    protected function addFinishAllPassesButton(ilToolbarGUI $toolbar)
390    {
391        global $DIC; /* @var ILIAS\DI\Container $DIC */
392
393        $finish_all_user_passes_btn = ilLinkButton::getInstance();
394        $finish_all_user_passes_btn->setCaption('finish_all_user_passes');
395        $finish_all_user_passes_btn->setUrl($DIC->ctrl()->getLinkTargetByClass('iltestevaluationgui', 'finishAllUserPasses'));
396        $toolbar->addButtonInstance($finish_all_user_passes_btn);
397    }
398
399    /**
400     * save client ip command
401     */
402    protected function saveClientIpCmd()
403    {
404        global $DIC; /* @var ILIAS\DI\Container $DIC */
405
406        require_once 'Modules/Test/classes/class.ilTestParticipantAccessFilter.php';
407        $filterCallback = ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId());
408        $a_user_ids = call_user_func_array($filterCallback, [(array) $_POST["chbUser"]]);
409
410        if (is_array($a_user_ids)) {
411            foreach ($a_user_ids as $user_id) {
412                $this->getTestObj()->setClientIP($user_id, $_POST["clientip_" . $user_id]);
413            }
414        } else {
415            ilUtil::sendInfo($DIC->language()->txt("select_one_user"), true);
416        }
417        $DIC->ctrl()->redirect($this, self::CMD_SHOW);
418    }
419
420    /**
421     * remove participants command
422     */
423    protected function removeParticipantsCmd()
424    {
425        global $DIC; /* @var ILIAS\DI\Container $DIC */
426
427        require_once 'Modules/Test/classes/class.ilTestParticipantAccessFilter.php';
428        $filterCallback = ilTestParticipantAccessFilter::getManageParticipantsUserFilter($this->getTestObj()->getRefId());
429        $a_user_ids = call_user_func_array($filterCallback, [(array) $_POST["chbUser"]]);
430
431        if (is_array($a_user_ids)) {
432            foreach ($a_user_ids as $user_id) {
433                $this->getTestObj()->disinviteUser($user_id);
434            }
435        } else {
436            ilUtil::sendInfo($DIC->language()->txt("select_one_user"), true);
437        }
438
439        $DIC->ctrl()->redirect($this, self::CMD_SHOW);
440    }
441}
442