ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestParticipantData.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
32 protected $db;
33
37 protected $lng;
38
39 private array $active_ids_filter;
40
41 private array $userIdsFilter;
42
43 private array $anonymousIdsFilter;
44
45 private array $byActiveId;
46
47 private array $byUserId;
48
49 private array $byAnonymousId;
50
55
57
59 {
60 $this->db = $db;
61 $this->lng = $lng;
62
63 $this->active_ids_filter = [];
64 $this->userIdsFilter = [];
65 $this->anonymousIdsFilter = [];
66
67 $this->byActiveId = [];
68 $this->byUserId = [];
69 $this->byAnonymousId = [];
70
71 $this->scoredParticipantsFilterEnabled = false;
72 }
73
77 public function getParticipantAccessFilter(): ?Closure
78 {
80 }
81
83 {
84 $this->participantAccessFilter = $participantAccessFilter;
85 }
86
90 public function isScoredParticipantsFilterEnabled(): bool
91 {
93 }
94
99 {
100 $this->scoredParticipantsFilterEnabled = $scoredParticipantsFilterEnabled;
101 }
102
103 public function load($testId): void
104 {
105 $this->byActiveId = [];
106 $this->byUserId = [];
107
108 $query = "
109 SELECT ta.active_id,
110 ta.user_fi user_id,
111 ta.anonymous_id,
112 ud.firstname,
113 ud.lastname,
114 ud.login,
115 ud.matriculation
116 FROM tst_active ta
117 LEFT JOIN usr_data ud
118 ON ud.usr_id = ta.user_fi
119 WHERE test_fi = %s
120 AND {$this->getConditionalExpression()}
121 AND {$this->getScoredParticipantsFilterExpression()}
122 ";
123
124 $res = $this->db->queryF($query, ['integer'], [$testId]);
125
126 $rows = [];
127 $accessFilteredUsrIds = [];
128
129 while ($row = $this->db->fetchAssoc($res)) {
130 $accessFilteredUsrIds[] = $row['user_id'];
131 $rows[] = $row;
132 }
133
134 if (is_callable($this->getParticipantAccessFilter(), true)) {
135 $accessFilteredUsrIds = call_user_func_array($this->getParticipantAccessFilter(), [$accessFilteredUsrIds]);
136 }
137
138 foreach ($rows as $row) {
139 if (!in_array($row['user_id'], $accessFilteredUsrIds)) {
140 continue;
141 }
142
143 $this->byActiveId[ $row['active_id'] ] = $row;
144
145 if ($row['user_id'] == ANONYMOUS_USER_ID) {
146 $this->byAnonymousId[ $row['anonymous_id'] ] = $row;
147 } else {
148 $this->byUserId[ $row['user_id'] ] = $row;
149 }
150 }
151 }
152
154 {
156 return "ta.last_finished_pass = ta.last_started_pass";
157 }
158
159 return '1 = 1';
160 }
161
162 public function getConditionalExpression(): string
163 {
164 $conditions = [];
165
166 if (count($this->getActiveIdsFilter())) {
167 $conditions[] = $this->db->in('active_id', $this->getActiveIdsFilter(), false, 'integer');
168 }
169
170 if (count($this->getUserIdsFilter())) {
171 $conditions[] = $this->db->in('user_fi', $this->getUserIdsFilter(), false, 'integer');
172 }
173
174 if (count($this->getAnonymousIdsFilter())) {
175 $conditions[] = $this->db->in('anonymous_id', $this->getAnonymousIdsFilter(), false, 'integer');
176 }
177
178 if (count($conditions)) {
179 return '(' . implode(' OR ', $conditions) . ')';
180 }
181
182 return '1 = 1';
183 }
184
185 public function setActiveIdsFilter(array $active_ids_filter): void
186 {
187 $this->active_ids_filter = $active_ids_filter;
188 }
189
190 public function getActiveIdsFilter(): array
191 {
193 }
194
195 public function setUserIdsFilter($userIdsFilter): void
196 {
197 $this->userIdsFilter = $userIdsFilter;
198 }
199
200 public function getUserIdsFilter(): array
201 {
203 }
204
206 {
207 $this->anonymousIdsFilter = $anonymousIdsFilter;
208 }
209
210 public function getAnonymousIdsFilter(): array
211 {
213 }
214
215 public function getActiveIds(): array
216 {
217 return array_keys($this->byActiveId);
218 }
219
220 public function getUserIds(): array
221 {
222 return array_keys($this->byUserId);
223 }
224
225 public function getAnonymousIds(): array
226 {
227 return array_keys($this->byAnonymousId);
228 }
229
230 public function getUserIdByActiveId($activeId)
231 {
232 return $this->byActiveId[$activeId]['user_id'];
233 }
234
235 public function getActiveIdByUserId($userId)
236 {
237 return $this->byUserId[$userId]['active_id'] ?? null;
238 }
239
240 public function getFormatedFullnameByActiveId($activeId): string
241 {
242 return ilObjTestAccess::_getParticipantData($activeId);
243 }
244
245 public function getFileSystemCompliantFullnameByActiveId($activeId): string
246 {
247 $fullname = str_replace(' ', '', $this->byActiveId[$activeId]['lastname']);
248 $fullname .= '_' . str_replace(' ', '', $this->byActiveId[$activeId]['firstname']);
249 $fullname .= '_' . $this->byActiveId[$activeId]['login'];
250
251 return ilFileUtils::getASCIIFilename($fullname);
252 }
253
254 public function getOptionArray(): array
255 {
256 $options = [];
257
258 foreach ($this->byActiveId as $activeId => $usrData) {
259 $options[$activeId] = ilObjTestAccess::_getParticipantData($activeId);
260 }
261
262 asort($options);
263
264 return $options;
265 }
266
267 public function getAnonymousActiveIds(): array
268 {
269 $anonymousActiveIds = [];
270
271 foreach ($this->byActiveId as $activeId => $active) {
272 if ($active['user_id'] == ANONYMOUS_USER_ID) {
273 $anonymousActiveIds[] = $activeId;
274 }
275 }
276
277 return $anonymousActiveIds;
278 }
279
280 public function getUserDataByActiveId(int $active_Id): array
281 {
282 if (isset($this->byActiveId[$active_Id])) {
283 return $this->byActiveId[$active_Id];
284 }
285
286 return null;
287 }
288}
static getASCIIFilename(string $a_filename)
language handling
static _getParticipantData(int $active_id)
Retrieves a participant name from active id.
setParticipantAccessFilter(Closure $participantAccessFilter)
setScoredParticipantsFilterEnabled($scoredParticipantsFilterEnabled)
__construct(ilDBInterface $db, ilLanguage $lng)
setAnonymousIdsFilter($anonymousIdsFilter)
setActiveIdsFilter(array $active_ids_filter)
const ANONYMOUS_USER_ID
Definition: constants.php:27
Interface ilDBInterface.
$res
Definition: ltiservices.php:69