ILIAS  release_8 Revision v8.24
class.ilTestParticipantList.php
Go to the documentation of this file.
1<?php
2
27class ilTestParticipantList implements Iterator
28{
32 protected $participants = array();
33
37 protected $testObj;
38
43 {
44 $this->testObj = $testObj;
45 }
46
50 public function getTestObj(): ilObjTest
51 {
52 return $this->testObj;
53 }
54
58 public function setTestObj($testObj)
59 {
60 $this->testObj = $testObj;
61 }
62
66 public function addParticipant(ilTestParticipant $participant)
67 {
68 $this->participants[] = $participant;
69 }
70
71 public function getParticipantByUsrId($usrId)
72 {
73 foreach ($this as $participant) {
74 if ($participant->getUsrId() != $usrId) {
75 continue;
76 }
77
78 return $participant;
79 }
80 return null;
81 }
82
83 public function getParticipantByActiveId($activeId): ?ilTestParticipant
84 {
85 foreach ($this as $participant) {
86 if ($participant->getActiveId() != $activeId) {
87 continue;
88 }
89
90 return $participant;
91 }
92 return null;
93 }
94
98 public function hasUnfinishedPasses(): bool
99 {
100 foreach ($this as $participant) {
101 if ($participant->hasUnfinishedPasses()) {
102 return true;
103 }
104 }
105
106 return false;
107 }
108
112 public function hasScorings(): bool
113 {
114 foreach ($this as $participant) {
115 if ($participant->getScoring() instanceof ilTestParticipantScoring) {
116 return true;
117 }
118 }
119
120 return false;
121 }
122
123 public function getAllUserIds(): array
124 {
125 $usrIds = array();
126
127 foreach ($this as $participant) {
128 $usrIds[] = $participant->getUsrId();
129 }
130
131 return $usrIds;
132 }
133
134 public function getAllActiveIds(): array
135 {
136 $activeIds = array();
137
138 foreach ($this as $participant) {
139 $activeIds[] = $participant->getActiveId();
140 }
141
142 return $activeIds;
143 }
144
145 public function isActiveIdInList($activeId): bool
146 {
147 foreach ($this as $participant) {
148 if ($participant->getActiveId() == $activeId) {
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156 public function getAccessFilteredList(callable $userAccessFilter): ilTestParticipantList
157 {
158 $usrIds = call_user_func_array($userAccessFilter, [$this->getAllUserIds()]);
159
160 $accessFilteredList = new self($this->getTestObj());
161
162 foreach ($this as $participant) {
163 if (in_array($participant->getUsrId(), $usrIds)) {
164 $participant = clone $participant;
165 $accessFilteredList->addParticipant($participant);
166 }
167 }
168
169 return $accessFilteredList;
170 }
171
172 public function current()
173 {
174 return current($this->participants);
175 }
176 public function next()
177 {
178 return next($this->participants);
179 }
180 public function key()
181 {
182 return key($this->participants);
183 }
184 public function valid(): bool
185 {
186 return key($this->participants) !== null;
187 }
188 public function rewind()
189 {
190 return reset($this->participants);
191 }
192
196 public function initializeFromDbRows($dbRows)
197 {
198 foreach ($dbRows as $rowKey => $rowData) {
199 $participant = new ilTestParticipant();
200
201 if ((int) $rowData['active_id']) {
202 $participant->setActiveId((int) $rowData['active_id']);
203 }
204
205 $participant->setUsrId((int) $rowData['usr_id']);
206
207 $participant->setLogin($rowData['login']);
208 $participant->setLastname($rowData['lastname']);
209 $participant->setFirstname($rowData['firstname']);
210 $participant->setMatriculation($rowData['matriculation']);
211
212 $participant->setActiveStatus((bool) ($rowData['active'] ?? false));
213
214 if (isset($rowData['clientip'])) {
215 $participant->setClientIp($rowData['clientip']);
216 }
217
218 $participant->setFinishedTries((int) $rowData['tries']);
219 $participant->setTestFinished((bool) $rowData['test_finished']);
220 $participant->setUnfinishedPasses((bool) $rowData['unfinished_passes']);
221
222 $this->addParticipant($participant);
223 }
224 }
225
230 {
231 require_once 'Modules/Test/classes/class.ilTestParticipantScoring.php';
232
233 $scoredParticipantList = new self($this->getTestObj());
234
235 global $DIC; /* @var ILIAS\DI\Container $DIC */
236
237 $res = $DIC->database()->query($this->buildScoringsQuery());
238
239 while ($row = $DIC->database()->fetchAssoc($res)) {
240 $scoring = new ilTestParticipantScoring();
241
242 $scoring->setActiveId((int) $row['active_fi']);
243 $scoring->setScoredPass((int) $row['pass']);
244
245 $scoring->setAnsweredQuestions((int) $row['answeredquestions']);
246 $scoring->setTotalQuestions((int) $row['questioncount']);
247
248 $scoring->setReachedPoints((float) $row['reached_points']);
249 $scoring->setMaxPoints((float) $row['max_points']);
250
251 $scoring->setPassed((bool) $row['passed']);
252 $scoring->setFinalMark((string) $row['mark_short']);
253
254 $this->getParticipantByActiveId($row['active_fi'])->setScoring($scoring);
255
256 $scoredParticipantList->addParticipant(
257 $this->getParticipantByActiveId($row['active_fi'])
258 );
259 }
260
261 return $scoredParticipantList;
262 }
263
264 public function buildScoringsQuery(): string
265 {
266 global $DIC; /* @var ILIAS\DI\Container $DIC */
267
268 $IN_activeIds = $DIC->database()->in(
269 'tres.active_fi',
270 $this->getAllActiveIds(),
271 false,
272 'integer'
273 );
274
275 if (false && !$this->getTestObj()->isDynamicTest()) { // BH: keep for the moment
276 $closedScoringsOnly = "
277 INNER JOIN tst_active tact
278 ON tact.active_id = tres.active_fi
279 AND tact.last_finished_pass = tact.last_started_pass
280 ";
281 } else {
282 $closedScoringsOnly = '';
283 }
284
285 $query = "
286 SELECT * FROM tst_result_cache tres
287
288 INNER JOIN tst_pass_result pres
289 ON pres.active_fi = tres.active_fi
290 AND pres.pass = tres.pass
291
292 $closedScoringsOnly
293
294 WHERE $IN_activeIds
295 ";
296
297 return $query;
298 }
299
300 public function getParticipantsTableRows(): array
301 {
302 $rows = array();
303
304 foreach ($this as $participant) {
305 $row = array(
306 'usr_id' => $participant->getUsrId(),
307 'active_id' => $participant->getActiveId(),
308 'login' => $participant->getLogin(),
309 'clientip' => $participant->getClientIp(),
310 'firstname' => $participant->getFirstname(),
311 'lastname' => $participant->getLastname(),
312 'name' => $this->buildFullname($participant),
313 'started' => ($participant->getActiveId() > 0) ? 1 : 0,
314 'unfinished' => $participant->hasUnfinishedPasses() ? 1 : 0,
315 'finished' => $participant->isTestFinished() ? 1 : 0,
316 'access' => $this->lookupLastAccess($participant->getActiveId()),
317 'tries' => $this->lookupNrOfTries($participant->getActiveId())
318 );
319
320 $rows[] = $row;
321 }
322
323 return $rows;
324 }
325
326 public function getScoringsTableRows(): array
327 {
328 $rows = array();
329
330 foreach ($this as $participant) {
331 if (!$participant->hasScoring()) {
332 continue;
333 }
334
335 $row = array(
336 'usr_id' => $participant->getUsrId(),
337 'active_id' => $participant->getActiveId(),
338 'login' => $participant->getLogin(),
339 'firstname' => $participant->getFirstname(),
340 'lastname' => $participant->getLastname(),
341 'name' => $this->buildFullname($participant)
342 );
343
344 if ($participant->getScoring()) {
345 $row['scored_pass'] = $participant->getScoring()->getScoredPass();
346 $row['answered_questions'] = $participant->getScoring()->getAnsweredQuestions();
347 $row['total_questions'] = $participant->getScoring()->getTotalQuestions();
348 $row['reached_points'] = $participant->getScoring()->getReachedPoints();
349 $row['max_points'] = $participant->getScoring()->getMaxPoints();
350 $row['percent_result'] = $participant->getScoring()->getPercentResult();
351 $row['passed_status'] = $participant->getScoring()->isPassed();
352 $row['final_mark'] = $participant->getScoring()->getFinalMark();
353 $row['scored_pass_finished_timestamp'] = ilObjTest::lookupLastTestPassAccess(
354 $participant->getActiveId(),
355 $participant->getScoring()->getScoredPass()
356 );
357 $row['finished_passes'] = $participant->getFinishedTries();
358 $row['has_unfinished_passes'] = $participant->hasUnfinishedPasses();
359 }
360
361 $rows[] = $row;
362 }
363
364 return $rows;
365 }
366
371 public function lookupNrOfTries($activeId): ?int
372 {
373 $maxPassIndex = ilObjTest::_getMaxPass($activeId);
374
375 if ($maxPassIndex !== null) {
376 $nrOfTries = $maxPassIndex + 1;
377 return $nrOfTries;
378 }
379
380 return null;
381 }
382
387 protected function lookupLastAccess($activeId): string
388 {
389 if (!$activeId) {
390 return '';
391 }
392
393 return $this->getTestObj()->_getLastAccess($activeId);
394 }
395
400 protected function buildFullname(ilTestParticipant $participant): string
401 {
402 if ($this->getTestObj()->getFixedParticipants() && !$participant->getActiveId()) {
403 return $this->buildInviteeFullname($participant);
404 }
405
406 return $this->buildParticipantsFullname($participant);
407 }
408
413 protected function buildInviteeFullname(ilTestParticipant $participant): string
414 {
415 global $DIC; /* @var ILIAS\DI\Container $DIC */
416
417 if (strlen($participant->getFirstname() . $participant->getLastname()) == 0) {
418 return $DIC->language()->txt("deleted_user");
419 }
420
421 if ($this->getTestObj()->getAnonymity()) {
422 return $DIC->language()->txt('anonymous');
423 }
424
425 return trim($participant->getLastname() . ", " . $participant->getFirstname());
426 }
427
432 protected function buildParticipantsFullname(ilTestParticipant $participant): string
433 {
434 require_once 'Modules/Test/classes/class.ilObjTestAccess.php';
436 }
437}
static _getParticipantData($active_id)
Retrieves a participant name from active id.
static lookupLastTestPassAccess($activeId, $passIndex)
static _getMaxPass($active_id)
Retrieves the maximum pass of a given user for a given test in which the user answered at least one q...
buildParticipantsFullname(ilTestParticipant $participant)
buildInviteeFullname(ilTestParticipant $participant)
getAccessFilteredList(callable $userAccessFilter)
addParticipant(ilTestParticipant $participant)
buildFullname(ilTestParticipant $participant)
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query
$rows
Definition: xhr_table.php:10