1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Test/classes/class.ilObjAssessmentFolder.php';
5
6/**
7 * @author		Björn Heyser <bheyser@databay.de>
8 * @version		$Id$
9 *
10 * @package     Modules/Test
11 */
12class ilTestPasswordChecker
13{
14    /**
15     * @var ilRbacSystem
16     */
17    protected $rbacsystem;
18
19    /**
20     * @var ilObjUser
21     */
22    protected $user;
23
24    /**
25     * @var ilObjTest
26     */
27    protected $testOBJ;
28
29    /**
30     * @var ilLanguage
31     */
32    protected $lng;
33
34    public function __construct(ilRbacSystem $rbacsystem, ilObjUser $user, ilObjTest $testOBJ, ilLanguage $lng)
35    {
36        $this->rbacsystem = $rbacsystem;
37        $this->user = $user;
38        $this->testOBJ = $testOBJ;
39        $this->lng = $lng;
40
41        $this->initSession();
42    }
43
44    public function isPasswordProtectionPageRedirectRequired()
45    {
46        if (!$this->isTestPasswordEnabled()) {
47            return false;
48        }
49
50        if ($this->isPrivilegedParticipant()) {
51            return false;
52        }
53
54        if ($this->isUserEnteredPasswordCorrect()) {
55            return false;
56        }
57
58        return true;
59    }
60
61    protected function isTestPasswordEnabled()
62    {
63        return strlen($this->testOBJ->getPassword());
64    }
65
66    protected function isPrivilegedParticipant()
67    {
68        return $this->rbacsystem->checkAccess('write', $this->testOBJ->getRefId());
69    }
70
71    public function wrongUserEnteredPasswordExist()
72    {
73        if (!strlen($this->getUserEnteredPassword())) {
74            return false;
75        }
76
77        return !$this->isUserEnteredPasswordCorrect();
78    }
79
80    public function isUserEnteredPasswordCorrect()
81    {
82        return $this->getUserEnteredPassword() == $this->testOBJ->getPassword();
83    }
84
85    public function setUserEnteredPassword($enteredPassword)
86    {
87        $_SESSION[$this->buildSessionKey()] = $enteredPassword;
88    }
89
90    protected function getUserEnteredPassword()
91    {
92        return $_SESSION[$this->buildSessionKey()];
93    }
94
95    protected function initSession()
96    {
97        if (!isset($_SESSION[$this->buildSessionKey()])) {
98            $_SESSION[$this->buildSessionKey()] = null;
99        }
100    }
101
102    protected function buildSessionKey()
103    {
104        return 'tst_password_' . $this->testOBJ->getTestId();
105    }
106
107    public function logWrongEnteredPassword()
108    {
109        if (!ilObjAssessmentFolder::_enabledAssessmentLogging()) {
110            return;
111        }
112
113        ilObjAssessmentFolder::_addLog(
114            $this->user->getId(),
115            $this->testOBJ->getId(),
116            $this->getWrongEnteredPasswordLogMsg(),
117            null,
118            null,
119            true,
120            $this->testOBJ->getRefId()
121        );
122    }
123
124    protected function getWrongEnteredPasswordLogMsg()
125    {
126        return $this->lng->txtlng(
127            'assessment',
128            'log_wrong_test_password_entered',
129            ilObjAssessmentFolder::_getLogLanguage()
130        );
131    }
132}
133