ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestParticipantList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30class ilTestParticipantList implements Iterator
31{
35 protected array $participants = [];
36
37 public function __construct(
38 private ilObjTest $test_obj,
39 private ilObjUser $user,
40 private ilLanguage $lng,
41 private ilDBInterface $db
42 ) {
43 }
44
45 private function getTestObj(): ilObjTest
46 {
47 return $this->test_obj;
48 }
49
50 public function setTestObj(ilObjTest $test_obj): void
51 {
52 $this->test_obj = $test_obj;
53 }
54
55 public function addParticipant(ilTestParticipant $participant): void
56 {
57 $this->participants[] = $participant;
58 }
59
60 public function getParticipantByUsrId(int $usr_id): ?ilTestParticipant
61 {
62 foreach ($this as $participant) {
63 if ($participant->getUsrId() != $usr_id) {
64 continue;
65 }
66
67 return $participant;
68 }
69 return null;
70 }
71
72 public function getParticipantByActiveId($active_id): ?ilTestParticipant
73 {
74 foreach ($this as $participant) {
75 if ($participant->getActiveId() != $active_id) {
76 continue;
77 }
78
79 return $participant;
80 }
81 return null;
82 }
83
84 public function hasUnfinishedPasses(): bool
85 {
86 foreach ($this as $participant) {
87 if ($participant->hasUnfinishedPasses()) {
88 return true;
89 }
90 }
91
92 return false;
93 }
94
95 public function hasScorings(): bool
96 {
97 foreach ($this as $participant) {
98 if ($participant->hasScoring()) {
99 return true;
100 }
101 }
102
103 return false;
104 }
105
106 public function getAllUserIds(): array
107 {
108 $usrIds = [];
109
110 foreach ($this as $participant) {
111 $usrIds[] = $participant->getUsrId();
112 }
113
114 return $usrIds;
115 }
116
117 public function getAllActiveIds(): array
118 {
119 $activeIds = [];
120
121 foreach ($this as $participant) {
122 $activeIds[] = $participant->getActiveId();
123 }
124
125 return $activeIds;
126 }
127
128 public function isActiveIdInList(int $active_id): bool
129 {
130 foreach ($this as $participant) {
131 if ($participant->getActiveId() == $active_id) {
132 return true;
133 }
134 }
135
136 return false;
137 }
138
139 public function getAccessFilteredList(Closure $user_access_filter): ilTestParticipantList
140 {
141 $usr_ids = $user_access_filter($this->getAllUserIds());
142
143 $access_filtered_list = new self($this->getTestObj(), $this->user, $this->lng, $this->db);
144
145 foreach ($this as $participant) {
146 if (in_array($participant->getUsrId(), $usr_ids)) {
147 $participant = clone $participant;
148 $access_filtered_list->addParticipant($participant);
149 }
150 }
151
152 return $access_filtered_list;
153 }
154
155 public function current(): ilTestParticipant
156 {
157 return current($this->participants);
158 }
159 public function next(): void
160 {
161 next($this->participants);
162 }
163 public function key(): int
164 {
165 return key($this->participants);
166 }
167 public function valid(): bool
168 {
169 return key($this->participants) !== null;
170 }
171 public function rewind(): void
172 {
173 reset($this->participants);
174 }
175
176 public function initializeFromDbRows(array $db_rows): void
177 {
178 foreach ($db_rows as $row_data) {
179 $participant = new ilTestParticipant();
180
181 if ((int) $row_data['active_id']) {
182 $participant->setActiveId((int) $row_data['active_id']);
183 }
184
185 $participant->setUsrId((int) $row_data['usr_id']);
186
187 $participant->setLogin($row_data['login'] ?? '');
188 $participant->setLastname($row_data['lastname']);
189 $participant->setFirstname($row_data['firstname'] ?? '');
190 $participant->setMatriculation($row_data['matriculation'] ?? '');
191 $participant->setActiveStatus((bool) ($row_data['active'] ?? false));
192 $participant->setFinishedTries((int) $row_data['tries']);
193 $participant->setTestFinished((bool) $row_data['test_finished']);
194 $participant->setUnfinishedPasses((bool) $row_data['unfinished_passes']);
195
196 $this->addParticipant($participant);
197 }
198 }
199
201 {
202 $scored_participant_list = new self($this->getTestObj(), $this->user, $this->lng, $this->db);
203
204 $res = $this->db->query($this->buildScoringsQuery());
205
206 while ($row = $this->db->fetchAssoc($res)) {
207 $scoring = new ilTestParticipantScoring();
208
209 $scoring->setActiveId((int) $row['active_fi']);
210 $scoring->setScoredPass((int) $row['pass']);
211
212 $scoring->setAnsweredQuestions((int) $row['answeredquestions']);
213 $scoring->setTotalQuestions((int) $row['questioncount']);
214
215 $scoring->setReachedPoints((float) $row['reached_points']);
216 $scoring->setMaxPoints((float) $row['max_points']);
217
218 $scoring->setPassed((bool) $row['passed']);
219 $scoring->setFinalMark((string) $row['mark_short']);
220
221 $this->getParticipantByActiveId($row['active_fi'])->setScoring($scoring);
222
223 $scored_participant_list->addParticipant(
224 $this->getParticipantByActiveId($row['active_fi'])
225 );
226 }
227
228 return $scored_participant_list;
229 }
230
231 public function buildScoringsQuery(): string
232 {
233 $IN_activeIds = $this->db->in(
234 'tres.active_fi',
235 $this->getAllActiveIds(),
236 false,
237 'integer'
238 );
239
240 $query = "
241 SELECT * FROM tst_result_cache tres
242
243 INNER JOIN tst_pass_result pres
244 ON pres.active_fi = tres.active_fi
245 AND pres.pass = tres.pass
246 WHERE $IN_activeIds
247 ";
248
249 return $query;
250 }
251
252 public function getScoringTableRows(): array
253 {
254 $rows = [];
255
256 foreach ($this as $participant) {
257 $row = [
258 'usr_id' => $participant->getUsrId(),
259 'active_id' => $participant->getActiveId(),
260 'login' => $participant->getLogin(),
261 'firstname' => $participant->getFirstname(),
262 'lastname' => $participant->getLastname()
263 ];
264
265 $rows[] = $row;
266 }
267
268 return $rows;
269 }
270
271 public function getScoringsTableRows(): array
272 {
273 $rows = [];
274
275 foreach ($this as $participant) {
276 if (!$participant->hasScoring()) {
277 continue;
278 }
279
280 $row = [
281 'usr_id' => $participant->getUsrId(),
282 'active_id' => $participant->getActiveId(),
283 'login' => $participant->getLogin(),
284 'firstname' => $participant->getFirstname(),
285 'lastname' => $participant->getLastname(),
286 'name' => $this->buildFullname($participant)
287 ];
288
289 if ($participant->getScoring()) {
290 $row['scored_pass'] = $participant->getScoring()->getScoredPass();
291 $row['answered_questions'] = $participant->getScoring()->getAnsweredQuestions();
292 $row['total_questions'] = $participant->getScoring()->getTotalQuestions();
293 $row['reached_points'] = $participant->getScoring()->getReachedPoints();
294 $row['max_points'] = $participant->getScoring()->getMaxPoints();
295 $row['percent_result'] = $participant->getScoring()->getPercentResult();
296 $row['passed_status'] = $participant->getScoring()->isPassed();
297 $row['final_mark'] = $participant->getScoring()->getFinalMark();
298 $row['scored_pass_finished_timestamp'] = ilObjTest::lookupLastTestPassAccess(
299 $participant->getActiveId(),
300 $participant->getScoring()->getScoredPass()
301 );
302 $row['finished_passes'] = $participant->getFinishedTries();
303 $row['has_unfinished_passes'] = $participant->hasUnfinishedPasses();
304 }
305
306 $rows[] = $row;
307 }
308
309 return $rows;
310 }
311
312 public function lookupNrOfTries(?int $active_id): ?int
313 {
314 if ($active_id === null) {
315 return null;
316 }
317
318 $max_pass_index = ilObjTest::_getMaxPass($active_id);
319
320 if ($max_pass_index === null) {
321 return null;
322 }
323
324 return $max_pass_index + 1;
325 }
326
327 protected function lookupLastAccess(?int $active_id): string
328 {
329 if ($active_id === null) {
330 return '';
331 }
332
333 return $this->getTestObj()->_getLastAccess($active_id);
334 }
335
336 protected function buildFullname(ilTestParticipant $participant): string
337 {
338 if ($this->getTestObj()->getMainSettings()->getAccessSettings()->getFixedParticipants() && !$participant->getActiveId()) {
339 return $this->buildInviteeFullname($participant);
340 }
341
342 return $this->buildParticipantsFullname($participant);
343 }
344
345 protected function buildInviteeFullname(ilTestParticipant $participant): string
346 {
347 if (strlen($participant->getFirstname() . $participant->getLastname()) == 0) {
348 return $this->lng->txt("deleted_user");
349 }
350
351 if ($this->getTestObj()->getAnonymity()) {
352 return $this->lng->txt('anonymous');
353 }
354
355 return trim($participant->getLastname() . ", " . $participant->getFirstname());
356 }
357
358 protected function buildParticipantsFullname(ilTestParticipant $participant): string
359 {
361 }
362}
language handling
static _getParticipantData(int $active_id)
Retrieves a participant name from active id.
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...
User class.
buildParticipantsFullname(ilTestParticipant $participant)
buildInviteeFullname(ilTestParticipant $participant)
__construct(private ilObjTest $test_obj, private ilObjUser $user, private ilLanguage $lng, private ilDBInterface $db)
addParticipant(ilTestParticipant $participant)
buildFullname(ilTestParticipant $participant)
getAccessFilteredList(Closure $user_access_filter)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $lng
Definition: privfeed.php:31