1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5require_once 'Modules/OrgUnit/classes/Positions/Operation/class.ilOrgUnitOperation.php';
6
7/**
8 * Class ilTestParticipantAccessFilter
9 *
10 * @author    Björn Heyser <info@bjoernheyser.de>
11 * @version    $Id$
12 *
13 * @package    Modules/Test
14 */
15class ilTestParticipantAccessFilter
16{
17    const FILTER_MANAGE_PARTICIPANTS = 'manageParticipantsUserFilter';
18    const FILTER_SCORE_PARTICIPANTS = 'scoreParticipantsUserFilter';
19    const FILTER_ACCESS_RESULTS = 'accessResultsUserFilter';
20    const FILTER_ACCESS_STATISTICS = 'accessStatisticsUserFilter';
21
22    const CALLBACK_METHOD = 'filterCallback';
23
24    /**
25     * @var integer
26     */
27    protected $refId;
28
29    /**
30     * @var string
31     */
32    protected $filter;
33
34    /**
35     * @return int
36     */
37    public function getRefId()
38    {
39        return $this->refId;
40    }
41
42    /**
43     * @param int $refId
44     */
45    public function setRefId($refId)
46    {
47        $this->refId = $refId;
48    }
49
50    /**
51     * @return string
52     */
53    public function getFilter()
54    {
55        return $this->filter;
56    }
57
58    /**
59     * @param string $filter
60     */
61    public function setFilter($filter)
62    {
63        $this->filter = $filter;
64    }
65
66    /**
67     * @param int[] $userIds
68     * @return int[]
69     */
70    public function filterCallback($userIds)
71    {
72        switch ($this->getFilter()) {
73            case self::FILTER_MANAGE_PARTICIPANTS:
74                return $this->manageParticipantsUserFilter($userIds);
75
76            case self::FILTER_SCORE_PARTICIPANTS:
77                return $this->scoreParticipantsUserFilter($userIds);
78
79            case self::FILTER_ACCESS_RESULTS:
80                return $this->accessResultsUserFilter($userIds);
81
82            case self::FILTER_ACCESS_STATISTICS:
83                return $this->accessStatisticsUserFilter($userIds);
84        }
85
86        require_once 'Modules/Test/exceptions/class.ilTestException.php';
87        throw new ilTestException('invalid user access filter mode chosen: ' . $this->getFilter());
88    }
89
90    /**
91     * @param int[] $userIds
92     * @return int[]
93     */
94    public function manageParticipantsUserFilter($userIds)
95    {
96        global $DIC; /* @var ILIAS\DI\Container $DIC */
97
98        $userIds = $DIC->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
99            'write',
100            ilOrgUnitOperation::OP_MANAGE_PARTICIPANTS,
101            $this->getRefId(),
102            $userIds
103        );
104
105        return $userIds;
106    }
107
108    /**
109     * @param int[] $userIds
110     * @return int[]
111     */
112    public function scoreParticipantsUserFilter($userIds)
113    {
114        global $DIC; /* @var ILIAS\DI\Container $DIC */
115
116        $userIds = $DIC->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
117            'write',
118            ilOrgUnitOperation::OP_SCORE_PARTICIPANTS,
119            $this->getRefId(),
120            $userIds
121        );
122
123        return $userIds;
124    }
125
126    /**
127     * @param int[] $userIds
128     * @return int[]
129     */
130    public function accessResultsUserFilter($userIds)
131    {
132        global $DIC; /* @var ILIAS\DI\Container $DIC */
133
134        $userIds = $DIC->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
135            'write',
136            ilOrgUnitOperation::OP_ACCESS_RESULTS,
137            $this->getRefId(),
138            $userIds
139        );
140
141        return $userIds;
142    }
143
144    /**
145     * @param int[] $userIds
146     * @return int[]
147     */
148    public function accessStatisticsUserFilter($userIds)
149    {
150        global $DIC; /* @var ILIAS\DI\Container $DIC */
151
152        if ($DIC->access()->checkAccess('tst_statistics', '', $this->getRefId())) {
153            return $userIds;
154        }
155
156        return $this->accessResultsUserFilter($userIds);
157    }
158
159    /**
160     * @param integer $refId
161     * @return callable
162     */
163    public static function getManageParticipantsUserFilter($refId)
164    {
165        $filter = new self();
166        $filter->setFilter(self::FILTER_MANAGE_PARTICIPANTS);
167        $filter->setRefId($refId);
168        return [$filter, self::CALLBACK_METHOD];
169    }
170
171    /**
172     * @param integer $refId
173     * @return callable
174     */
175    public static function getScoreParticipantsUserFilter($refId)
176    {
177        $filter = new self();
178        $filter->setFilter(self::FILTER_SCORE_PARTICIPANTS);
179        $filter->setRefId($refId);
180        return [$filter, self::CALLBACK_METHOD];
181    }
182
183    /**
184     * @param integer $refId
185     * @return callable
186     */
187    public static function getAccessResultsUserFilter($refId)
188    {
189        $filter = new self();
190        $filter->setFilter(self::FILTER_ACCESS_RESULTS);
191        $filter->setRefId($refId);
192        return [$filter, self::CALLBACK_METHOD];
193    }
194
195    /**
196     * @param integer $refId
197     * @return callable
198     */
199    public static function getAccessStatisticsUserFilter($refId)
200    {
201        $filter = new self();
202        $filter->setFilter(self::FILTER_ACCESS_STATISTICS);
203        $filter->setRefId($refId);
204        return [$filter, self::CALLBACK_METHOD];
205    }
206}
207