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 *
11 * @ilCtrl_Calls ilTestPasswordProtectionGUI: ilPropertyFormGUI
12 */
13class ilTestPasswordProtectionGUI
14{
15    const CMD_SHOW_PASSWORD_FORM = 'showPasswordForm';
16    const CMD_SAVE_ENTERED_PASSWORD = 'saveEnteredPassword';
17    const CMD_BACK_TO_INFO_SCREEN = 'backToInfoScreen';
18
19    /**
20     * @var ilCtrl
21     */
22    protected $ctrl;
23
24    /**
25     * @var ilGlobalTemplateInterface
26     */
27    protected $tpl;
28
29    /**
30     * @var ilLanguage
31     */
32    protected $lng;
33
34    /**
35     * @var ilTestPlayerAbstractGUI
36     */
37    protected $parentGUI;
38
39    /**
40     * @var ilTestPasswordChecker
41     */
42    protected $passwordChecker;
43
44    /**
45     * @var string
46     */
47    private $nextCommandClass;
48
49    /**
50     * @var string
51     */
52    private $nextCommandCmd;
53
54    public function __construct(ilCtrl $ctrl, ilGlobalTemplateInterface $tpl, ilLanguage $lng, ilTestPlayerAbstractGUI $parentGUI, ilTestPasswordChecker $passwordChecker)
55    {
56        $this->ctrl = $ctrl;
57        $this->tpl = $tpl;
58        $this->lng = $lng;
59        $this->parentGUI = $parentGUI;
60        $this->passwordChecker = $passwordChecker;
61    }
62
63    public function executeCommand()
64    {
65        $this->ctrl->saveParameter($this, 'nextCommand');
66        $nextCommand = explode('::', $_GET['nextCommand']);
67        $this->setNextCommandClass($nextCommand[0]);
68        $this->setNextCommandCmd($nextCommand[1]);
69
70        $this->ctrl->saveParameter($this->parentGUI, 'lock');
71
72        switch ($this->ctrl->getNextClass()) {
73            default:
74
75                $cmd = $this->ctrl->getCmd() . 'Cmd';
76                $this->$cmd();
77        }
78    }
79
80    protected function buildPasswordMsg()
81    {
82        if (!$this->passwordChecker->wrongUserEnteredPasswordExist()) {
83            return '';
84        }
85
86        return ilUtil::getSystemMessageHTML(
87            $this->lng->txt('tst_password_entered_wrong_password'),
88            'failure'
89        );
90    }
91
92    /**
93     * @return ilPropertyFormGUI
94     */
95    protected function buildPasswordForm()
96    {
97        $form = new ilPropertyFormGUI();
98        $form->setTitle($this->lng->txt("tst_password_form"));
99        $form->setDescription($this->lng->txt("tst_password_introduction"));
100
101        $form->setFormAction($this->ctrl->getFormAction($this));
102        $form->addCommandButton(self::CMD_SAVE_ENTERED_PASSWORD, $this->lng->txt("submit"));
103        $form->addCommandButton(self::CMD_BACK_TO_INFO_SCREEN, $this->lng->txt("cancel"));
104
105        $inp = new ilPasswordInputGUI($this->lng->txt("tst_password"), 'password');
106        $inp->setRequired(true);
107        $inp->setRetype(false);
108        $form->addItem($inp);
109        return $form;
110    }
111
112    private function showPasswordFormCmd()
113    {
114        require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
115        require_once 'Services/Form/classes/class.ilPasswordInputGUI.php';
116
117        $msg = $this->buildPasswordMsg();
118        $form = $this->buildPasswordForm();
119
120        $this->tpl->setVariable(
121            $this->parentGUI->getContentBlockName(),
122            $msg . $this->ctrl->getHTML($form)
123        );
124    }
125
126    private function saveEnteredPasswordCmd()
127    {
128        $this->passwordChecker->setUserEnteredPassword($_POST["password"]);
129
130        if (!$this->passwordChecker->isUserEnteredPasswordCorrect()) {
131            $this->passwordChecker->logWrongEnteredPassword();
132        }
133
134        $this->ctrl->redirectByClass($this->getNextCommandClass(), $this->getNextCommandCmd());
135    }
136
137    private function backToInfoScreenCmd()
138    {
139        $this->ctrl->redirectByClass('ilObjTestGUI', 'infoScreen');
140    }
141
142    private function setNextCommandClass($nextCommandClass)
143    {
144        $this->nextCommandClass = $nextCommandClass;
145    }
146
147    private function getNextCommandClass()
148    {
149        return $this->nextCommandClass;
150    }
151
152    private function setNextCommandCmd($nextCommandCmd)
153    {
154        $this->nextCommandCmd = $nextCommandCmd;
155    }
156
157    private function getNextCommandCmd()
158    {
159        return $this->nextCommandCmd;
160    }
161}
162