ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestEvaluationData.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 public const FILTER_BY_NONE = '';
29 public const FILTER_BY_NAME = 'name';
30 public const FILTER_BY_GROUP = 'group';
31 public const FILTER_BY_COURSE = 'course';
32 public const FILTER_BY_ACTIVE_ID = 'active_id';
33
34 public array $question_titles = [];
35
36 protected ?Statistics $statistics = null;
37 protected ?array $arr_filter = null;
38 protected int $datasets;
39
40 public function __sleep(): array
41 {
42 return ['question_titles', 'participants', 'statistics', 'arr_filter', 'datasets', 'test'];
43 }
44
48 public function __construct(
49 protected array $participants
50 ) {
51 }
52
53 public function setDatasets(int $datasets): void
54 {
55 $this->datasets = $datasets;
56 }
57
58 public function getDatasets(): int
59 {
60 return $this->datasets;
61 }
62
63 public function addQuestionTitle(int $question_id, string $question_title): void
64 {
65 $this->question_titles[$question_id] = $question_title;
66 }
67
71 public function getQuestionTitles(): array
72 {
74 }
75
76 public function getQuestionTitle(?int $question_id): string
77 {
78 if (array_key_exists($question_id, $this->question_titles)) {
79 return $this->question_titles[$question_id];
80 }
81
82 return '';
83 }
84
86 {
87 $finishedParticipants = 0;
88
89 foreach ($this->participants as $active_id => $participant) {
90 if (!$participant->isSubmitted()) {
91 continue;
92 }
93
94 $finishedParticipants++;
95 }
96
97 return $finishedParticipants;
98 }
99
103 public function getParticipants(): array
104 {
105 if (is_array($this->arr_filter) && count($this->arr_filter) > 0) {
106 $filtered_participants = [];
107 $courseids = [];
108 $groupids = [];
109
110 if (array_key_exists(self::FILTER_BY_GROUP, $this->arr_filter)) {
111 $ids = ilObject::_getIdsForTitle($this->arr_filter[self::FILTER_BY_GROUP], 'grp', true);
112 $groupids = array_merge($groupids, $ids);
113 }
114 if (array_key_exists(self::FILTER_BY_COURSE, $this->arr_filter)) {
115 $ids = ilObject::_getIdsForTitle($this->arr_filter[self::FILTER_BY_COURSE], 'crs', true);
116 $courseids = array_merge($courseids, $ids);
117 }
118 foreach ($this->participants as $active_id => $participant) {
119 $remove = false;
120 if (array_key_exists(self::FILTER_BY_NAME, $this->arr_filter)) {
121 if (!(strpos(strtolower($participant->getName()), strtolower((string) $this->arr_filter[self::FILTER_BY_NAME])) !== false)) {
122 $remove = true;
123 }
124 }
125 if (!$remove) {
126 if (array_key_exists(self::FILTER_BY_GROUP, $this->arr_filter)) {
127 $groups = ilParticipants::_getMembershipByType($participant->getUserID(), ['grp']);
128 $foundfilter = false;
129 if (count(array_intersect($groupids, $groups))) {
130 $foundfilter = true;
131 }
132 if (!$foundfilter) {
133 $remove = true;
134 }
135 }
136 }
137 if (!$remove) {
138 if (array_key_exists(self::FILTER_BY_COURSE, $this->arr_filter)) {
139 $courses = ilParticipants::_getMembershipByType($participant->getUserID(), ['crs']);
140 $foundfilter = false;
141 if (count(array_intersect($courseids, $courses))) {
142 $foundfilter = true;
143 }
144 if (!$foundfilter) {
145 $remove = true;
146 }
147 }
148 }
149 if (!$remove) {
150 if (array_key_exists(self::FILTER_BY_ACTIVE_ID, $this->arr_filter)) {
151 if ($active_id != $this->arr_filter[self::FILTER_BY_ACTIVE_ID]) {
152 $remove = true;
153 }
154 }
155 }
156 if (!$remove) {
157 $filtered_participants[$active_id] = $participant;
158 }
159 }
160 return $this->orderParticipants($filtered_participants);
161 }
162
163 return $this->orderParticipants($this->participants);
164 }
165
166 public function resetFilter(): void
167 {
168 $this->arr_filter = [];
169 }
170
171 public function setFilter(string $by, string $text): void
172 {
173 if (in_array(
174 $by,
175 [self::FILTER_BY_ACTIVE_ID, self::FILTER_BY_NAME, self::FILTER_BY_COURSE, self::FILTER_BY_GROUP],
176 true
177 )) {
178 $this->arr_filter = [$by => $text];
179 }
180 }
181
182 public function setFilterArray(array $arr_filter): void
183 {
184 $this->arr_filter = $arr_filter;
185 }
186
187 public function addParticipant(int $active_id, ilTestEvaluationUserData $participant): void
188 {
189 $this->participants[$active_id] = $participant;
190 }
191
192 public function getParticipant(int $active_id): ?ilTestEvaluationUserData
193 {
194 return $this->participants[$active_id] ?? null;
195 }
196
197 public function participantExists($active_id): bool
198 {
199 return array_key_exists($active_id, $this->participants);
200 }
201
202 public function removeParticipant($active_id)
203 {
204 unset($this->participants[$active_id]);
205 }
206
207 public function getStatistics(): Statistics
208 {
209 if ($this->statistics === null) {
210 $this->statistics = new Statistics($this);
211 }
212 return $this->statistics;
213 }
214
215 public function getParticipantIds(): array
216 {
217 return array_keys($this->participants);
218 }
219
220 private function orderParticipants(array $participants): array
221 {
222 uasort($participants, static fn($a, $b) => $a->getName() <=> $b->getName());
223 return $participants;
224 }
225}
static _getIdsForTitle(string $title, string $type='', bool $partial_match=false)
static _getMembershipByType(int $a_usr_id, array $a_type, bool $a_only_member_role=false)
get membership by type Get course or group membership
getQuestionTitle(?int $question_id)
addParticipant(int $active_id, ilTestEvaluationUserData $participant)
orderParticipants(array $participants)
setFilter(string $by, string $text)
addQuestionTitle(int $question_id, string $question_title)
__construct(protected array $participants)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples