ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestParticipantList.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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->getScoring() instanceof ilTestParticipantScoring) {
99  return true;
100  }
101  }
102 
103  return false;
104  }
105 
106  public function getAllUserIds(): array
107  {
108  $usrIds = array();
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 = array();
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 
192  $participant->setActiveStatus((bool) ($row_data['active'] ?? false));
193 
194  if (isset($row_data['clientip'])) {
195  $participant->setClientIp($row_data['clientip']);
196  }
197 
198  $participant->setFinishedTries((int) $row_data['tries']);
199  $participant->setTestFinished((bool) $row_data['test_finished']);
200  $participant->setUnfinishedPasses((bool) $row_data['unfinished_passes']);
201 
202  $this->addParticipant($participant);
203  }
204  }
205 
207  {
208  $scored_participant_list = new self($this->getTestObj(), $this->user, $this->lng, $this->db);
209 
210  $res = $this->db->query($this->buildScoringsQuery());
211 
212  while ($row = $this->db->fetchAssoc($res)) {
213  $scoring = new ilTestParticipantScoring();
214 
215  $scoring->setActiveId((int) $row['active_fi']);
216  $scoring->setScoredPass((int) $row['pass']);
217 
218  $scoring->setAnsweredQuestions((int) $row['answeredquestions']);
219  $scoring->setTotalQuestions((int) $row['questioncount']);
220 
221  $scoring->setReachedPoints((float) $row['reached_points']);
222  $scoring->setMaxPoints((float) $row['max_points']);
223 
224  $scoring->setPassed((bool) $row['passed']);
225  $scoring->setFinalMark((string) $row['mark_short']);
226 
227  $this->getParticipantByActiveId($row['active_fi'])->setScoring($scoring);
228 
229  $scored_participant_list->addParticipant(
230  $this->getParticipantByActiveId($row['active_fi'])
231  );
232  }
233 
234  return $scored_participant_list;
235  }
236 
237  public function buildScoringsQuery(): string
238  {
239  $IN_activeIds = $this->db->in(
240  'tres.active_fi',
241  $this->getAllActiveIds(),
242  false,
243  'integer'
244  );
245 
246  $query = "
247  SELECT * FROM tst_result_cache tres
248 
249  INNER JOIN tst_pass_result pres
250  ON pres.active_fi = tres.active_fi
251  AND pres.pass = tres.pass
252  WHERE $IN_activeIds
253  ";
254 
255  return $query;
256  }
257 
258  public function getParticipantsTableRows(): array
259  {
260  $rows = [];
261 
262  foreach ($this as $participant) {
263  $row = [
264  'usr_id' => $participant->getUsrId(),
265  'active_id' => $participant->getActiveId(),
266  'login' => $participant->getLogin(),
267  'clientip' => $participant->getClientIp(),
268  'firstname' => $participant->getFirstname(),
269  'lastname' => $participant->getLastname(),
270  'name' => $this->buildFullname($participant),
271  'started' => ($participant->getActiveId() > 0) ? 1 : 0,
272  'unfinished' => $participant->hasUnfinishedPasses() ? 1 : 0,
273  'finished' => $participant->isTestFinished() ? 1 : 0,
274  'access' => $this->lookupLastAccess($participant->getActiveId()),
275  'tries' => $this->lookupNrOfTries($participant->getActiveId())
276  ];
277 
278  $rows[] = $row;
279  }
280 
281  return $rows;
282  }
283 
284  public function getScoringsTableRows(): array
285  {
286  $rows = [];
287 
288  foreach ($this as $participant) {
289  if (!$participant->hasScoring()) {
290  continue;
291  }
292 
293  $row = [
294  'usr_id' => $participant->getUsrId(),
295  'active_id' => $participant->getActiveId(),
296  'login' => $participant->getLogin(),
297  'firstname' => $participant->getFirstname(),
298  'lastname' => $participant->getLastname(),
299  'name' => $this->buildFullname($participant)
300  ];
301 
302  if ($participant->getScoring()) {
303  $row['scored_pass'] = $participant->getScoring()->getScoredPass();
304  $row['answered_questions'] = $participant->getScoring()->getAnsweredQuestions();
305  $row['total_questions'] = $participant->getScoring()->getTotalQuestions();
306  $row['reached_points'] = $participant->getScoring()->getReachedPoints();
307  $row['max_points'] = $participant->getScoring()->getMaxPoints();
308  $row['percent_result'] = $participant->getScoring()->getPercentResult();
309  $row['passed_status'] = $participant->getScoring()->isPassed();
310  $row['final_mark'] = $participant->getScoring()->getFinalMark();
311  $row['scored_pass_finished_timestamp'] = ilObjTest::lookupLastTestPassAccess(
312  $participant->getActiveId(),
313  $participant->getScoring()->getScoredPass()
314  );
315  $row['finished_passes'] = $participant->getFinishedTries();
316  $row['has_unfinished_passes'] = $participant->hasUnfinishedPasses();
317  }
318 
319  $rows[] = $row;
320  }
321 
322  return $rows;
323  }
324 
325  public function lookupNrOfTries(?int $active_id): ?int
326  {
327  if ($active_id === null) {
328  return null;
329  }
330 
331  $max_pass_index = ilObjTest::_getMaxPass($active_id);
332 
333  if ($max_pass_index === null) {
334  return null;
335  }
336 
337  return $max_pass_index + 1;
338  }
339 
340  protected function lookupLastAccess(?int $active_id): string
341  {
342  if ($active_id === null) {
343  return '';
344  }
345 
346  return $this->getTestObj()->_getLastAccess($active_id);
347  }
348 
349  protected function buildFullname(ilTestParticipant $participant): string
350  {
351  if ($this->getTestObj()->getMainSettings()->getAccessSettings()->getFixedParticipants() && !$participant->getActiveId()) {
352  return $this->buildInviteeFullname($participant);
353  }
354 
355  return $this->buildParticipantsFullname($participant);
356  }
357 
358  protected function buildInviteeFullname(ilTestParticipant $participant): string
359  {
360  if (strlen($participant->getFirstname() . $participant->getLastname()) == 0) {
361  return $this->lng->txt("deleted_user");
362  }
363 
364  if ($this->getTestObj()->getAnonymity()) {
365  return $this->lng->txt('anonymous');
366  }
367 
368  return trim($participant->getLastname() . ", " . $participant->getFirstname());
369  }
370 
371  protected function buildParticipantsFullname(ilTestParticipant $participant): string
372  {
373  return ilObjTestAccess::_getParticipantData($participant->getActiveId());
374  }
375 }
$res
Definition: ltiservices.php:69
static _getParticipantData($active_id)
Retrieves a participant name from active id.
buildInviteeFullname(ilTestParticipant $participant)
buildFullname(ilTestParticipant $participant)
addParticipant(ilTestParticipant $participant)
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...
__construct(private ilObjTest $test_obj, private ilObjUser $user, private ilLanguage $lng, private ilDBInterface $db)
$lng
buildParticipantsFullname(ilTestParticipant $participant)
getAccessFilteredList(Closure $user_access_filter)