ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
Positions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
30 private array $positions = [];
31
37 public function __construct(
38 array $user_questions,
39 private array $user_attempts,
40 private array $question_properties
41 ) {
42 foreach ($user_questions as $uid => $qids) {
43 $this->positions[] = [[$uid], $qids];
44 }
45 }
46
47 public function get(ConsecutiveScoringMode $mode): array
48 {
49 return $this->applyMode($mode, $this->positions);
50 }
51
52 public function getAllQuestionProperties(): array
53 {
54 return $this->question_properties;
55 }
56
57 public function getAllAttempts(): array
58 {
59 return $this->user_attempts;
60 }
61
62 public function applyFilters(\Closure ...$filters): self
63 {
64 $clone = clone $this;
65 $positions = $clone->positions;
66 foreach ($filters as $filter) {
67 $positions = array_map(
68 fn($set) => $filter(...$set),
70 );
71 $positions = array_values(
72 array_filter(
74 fn($set) => $set[0] !== [] && $set[1] !== []
75 )
76 );
77 }
78 $clone->positions = $positions === []
79 ? [null]
80 : $positions;
81
82 return $clone;
83 }
84
85 private function applyMode(ConsecutiveScoringMode $mode, array $positions): array
86 {
87 if ($mode->isSingle()) {
88 return $this->buildSingleModePositionsList($mode, $positions);
89 }
90
91 return $this->buildAllPositionsList($mode, $positions);
92 }
93
95 {
96 $reordered_positions = array_reduce(
98 static function (array $c, ?array $v): array {
99 [$uids, $qids] = $v;
100 if ($qids === null) {
101 return $c;
102 }
103 foreach ($qids as $qid) {
104 $c[] = [$uids, [$qid]];
105 }
106 return $c;
107 },
108 []
109 );
110
111 if ($reordered_positions === []) {
112 return [null];
113 }
114
115 if (!$mode->isUserCentric()) {
116 usort($reordered_positions, static fn($a, $b) => $a[1][0] <=> $b[1][0]);
117 }
118
119 return $reordered_positions;
120 }
121
122 private function buildAllPositionsList(ConsecutiveScoringMode $mode, array $positions): array
123 {
124 if ($mode->isUserCentric()) {
125 return $positions;
126 }
127
128 $questions2users = array_reduce(
130 static function (array $c, ?array $v): array {
131 [$uids, $qids] = $v;
132 if ($qids === null) {
133 return $c;
134 }
135 foreach ($qids as $qid) {
136 if (!array_key_exists($qid, $c)) {
137 $c[$qid] = [];
138 }
139 $c[$qid] = array_unique(array_merge($c[$qid], $uids));
140 }
141 return $c;
142 },
143 []
144 );
145
146 $reorderd_positions = [];
147 foreach ($questions2users as $qid => $uids) {
148 $reorderd_positions[] = [$uids, [$qid]];
149 }
150
151 if ($reorderd_positions === []) {
152 return [null];
153 }
154
155 return $reorderd_positions;
156 }
157}
applyMode(ConsecutiveScoringMode $mode, array $positions)
Definition: Positions.php:85
buildAllPositionsList(ConsecutiveScoringMode $mode, array $positions)
Definition: Positions.php:122
applyFilters(\Closure ... $filters)
Definition: Positions.php:62
array $positions
"position" is a set of two arrays [[$user_id(s)], [$question_id(s)]], where - according to transposit...
Definition: Positions.php:30
__construct(array $user_questions, private array $user_attempts, private array $question_properties)
Definition: Positions.php:37
buildSingleModePositionsList(ConsecutiveScoringMode $mode, array $positions)
Definition: Positions.php:94
$c
Definition: deliver.php:25
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples