ILIAS  release_8 Revision v8.24
ilObjTestSettingsGamification.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\Refinery\Factory as Refinery;
24
26{
27 public const HIGHSCORE_SHOW_OWN_TABLE = 1;
28 public const HIGHSCORE_SHOW_TOP_TABLE = 2;
29 public const HIGHSCORE_SHOW_ALL_TABLES = 3;
30
31 protected bool $highscore_enabled = false;
32 protected bool $highscore_anon = true;
33 protected bool $highscore_achieved_ts = true;
34 protected bool $highscore_score = true;
35 protected bool $highscore_percentage = true;
36 protected bool $highscore_hints = true;
37 protected bool $highscore_wtime = true;
38 protected bool $highscore_own_table = true;
39 protected bool $highscore_top_table = true;
40 protected int $highscore_top_num = 10;
41
42
43 public function __construct(int $test_id)
44 {
46 }
47
48 public function toForm(
50 FieldFactory $f,
51 Refinery $refinery,
52 array $environment = null
53 ): FormInput {
54 $optional_group = $f->optionalGroup(
55 [
56 'highscore_mode' => $f->radio($lng->txt('tst_highscore_mode'), "")
57 ->withOption((string) self::HIGHSCORE_SHOW_OWN_TABLE, $lng->txt('tst_highscore_own_table'), $lng->txt('tst_highscore_own_table_description'))
58 ->withOption((string) self::HIGHSCORE_SHOW_TOP_TABLE, $lng->txt('tst_highscore_top_table'), $lng->txt('tst_highscore_top_table_description'))
59 ->withOption((string) self::HIGHSCORE_SHOW_ALL_TABLES, $lng->txt('tst_highscore_all_tables'), $lng->txt('tst_highscore_all_tables_description'))
60 ->withValue($this->getHighScoreMode() > 0 ? (string) $this->getHighScoreMode() : '')
61 ->withRequired(true)
62 ,
63 'highscore_top_num' => $f->numeric($lng->txt('tst_highscore_top_num'), $lng->txt('tst_highscore_top_num_description'))
64 ->withRequired(true)
65 ->withValue($this->getHighscoreTopNum()),
66 'highscore_anon' => $f->checkbox(
67 $lng->txt('tst_highscore_anon'),
68 $lng->txt('tst_highscore_anon_description')
69 )->withValue($this->getHighscoreAnon()),
70 'highscore_achieved_ts' => $f->checkbox(
71 $lng->txt('tst_highscore_achieved_ts'),
72 $lng->txt('tst_highscore_achieved_ts_description')
74 'highscore_score' => $f->checkbox(
75 $lng->txt('tst_highscore_score'),
76 $lng->txt('tst_highscore_score_description')
77 )->withValue($this->getHighscoreScore()),
78 'highscore_percentage' => $f->checkbox(
79 $lng->txt('tst_highscore_percentage'),
80 $lng->txt('tst_highscore_percentage_description')
82 'highscore_hints' => $f->checkbox(
83 $lng->txt('tst_highscore_hints'),
84 $lng->txt('tst_highscore_hints_description')
85 )->withValue($this->getHighscoreHints()),
86 'highscore_wtime' => $f->checkbox(
87 $lng->txt('tst_highscore_wtime'),
88 $lng->txt('tst_highscore_wtime_description')
89 )->withValue($this->getHighscoreWTime())
90
91 ],
92 $lng->txt('tst_highscore_enabled'),
93 $lng->txt('tst_highscore_description')
94 );
95
96 if (!$this->getHighscoreEnabled()) {
97 $optional_group = $optional_group->withValue(null);
98 }
99
100 $fields = ['highscore' => $optional_group];
101 return $f->section($fields, $lng->txt('tst_results_gamification'))
102 ->withAdditionalTransformation(
103 $refinery->custom()->transformation(
104 function ($v) {
105 $settings = clone $this;
106
107 if (! $v['highscore']) {
108 return $settings->withHighscoreEnabled(false);
109 }
110
111 return $settings
112 ->withHighscoreEnabled(true)
114 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_OWN_TABLE ||
115 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
116 )
118 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_TOP_TABLE ||
119 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
120 )
121 ->withHighscoreTopNum($v['highscore']['highscore_top_num'])
122 ->withHighscoreAnon($v['highscore']['highscore_anon'])
123 ->withHighscoreAchievedTS($v['highscore']['highscore_achieved_ts'])
124 ->withHighscoreScore($v['highscore']['highscore_score'])
125 ->withHighscorePercentage($v['highscore']['highscore_percentage'])
126 ->withHighscoreHints($v['highscore']['highscore_hints'])
127 ->withHighscoreWTime($v['highscore']['highscore_wtime']);
128 }
129 )
130 );
131 }
132
133 public function toStorage(): array
134 {
135 return [
136 'highscore_enabled' => ['integer', (int) $this->getHighscoreEnabled()],
137 'highscore_anon' => ['integer', (int) $this->getHighscoreAnon()],
138 'highscore_achieved_ts' => ['integer', (int) $this->getHighscoreAchievedTS()],
139 'highscore_score' => ['integer', (int) $this->getHighscoreScore()],
140 'highscore_percentage' => ['integer', (int) $this->getHighscorePercentage()],
141 'highscore_hints' => ['integer', (int) $this->getHighscoreHints()],
142 'highscore_wtime' => ['integer', (int) $this->getHighscoreWTime()],
143 'highscore_own_table' => ['integer', (int) $this->getHighscoreOwnTable()],
144 'highscore_top_table' => ['integer', (int) $this->getHighscoreTopTable()],
145 'highscore_top_num' => ['integer', $this->getHighscoreTopNum()]
146 ];
147 }
148
149 public function getHighscoreEnabled(): bool
150 {
151 return $this->highscore_enabled;
152 }
153 public function withHighscoreEnabled(bool $highscore_enabled): self
154 {
155 $clone = clone $this;
156 $clone->highscore_enabled = $highscore_enabled;
157 return $clone;
158 }
159
160 public function getHighscoreOwnTable(): bool
161 {
162 return $this->highscore_own_table;
163 }
164 public function withHighscoreOwnTable(bool $highscore_own_table): self
165 {
166 $clone = clone $this;
167 $clone->highscore_own_table = $highscore_own_table;
168 return $clone;
169 }
170 public function getHighscoreTopTable(): bool
171 {
172 return $this->highscore_top_table;
173 }
174 public function withHighscoreTopTable(bool $highscore_top_table): self
175 {
176 $clone = clone $this;
177 $clone->highscore_top_table = $highscore_top_table;
178 return $clone;
179 }
180
181 public function getHighScoreMode(): int
182 {
183 if ($this->getHighscoreTopTable() && $this->getHighscoreOwnTable()) {
184 return self::HIGHSCORE_SHOW_ALL_TABLES;
185 }
186
187 if ($this->getHighscoreTopTable()) {
188 return self::HIGHSCORE_SHOW_TOP_TABLE;
189 }
190
191 if ($this->getHighscoreOwnTable()) {
192 return self::HIGHSCORE_SHOW_OWN_TABLE;
193 }
194
195 return 0;
196 }
197
198 public function getHighscoreTopNum(): int
199 {
200 return $this->highscore_top_num;
201 }
202 public function withHighscoreTopNum(int $highscore_top_num): self
203 {
204 $clone = clone $this;
205 $clone->highscore_top_num = $highscore_top_num;
206 return $clone;
207 }
208
209 public function getHighscoreAnon(): bool
210 {
211 return $this->highscore_anon;
212 }
213 public function withHighscoreAnon(bool $highscore_anon): self
214 {
215 $clone = clone $this;
216 $clone->highscore_anon = $highscore_anon;
217 return $clone;
218 }
219
220 public function getHighscoreAchievedTS(): bool
221 {
222 return $this->highscore_achieved_ts;
223 }
224 public function withHighscoreAchievedTS(bool $highscore_achieved_ts): self
225 {
226 $clone = clone $this;
227 $clone->highscore_achieved_ts = $highscore_achieved_ts;
228 return $clone;
229 }
230
231 public function getHighscoreScore(): bool
232 {
233 return $this->highscore_score;
234 }
235 public function withHighscoreScore(bool $highscore_score): self
236 {
237 $clone = clone $this;
238 $clone->highscore_score = $highscore_score;
239 return $clone;
240 }
241
242 public function getHighscorePercentage(): bool
243 {
244 return $this->highscore_percentage;
245 }
246 public function withHighscorePercentage(bool $highscore_percentage): self
247 {
248 $clone = clone $this;
249 $clone->highscore_percentage = $highscore_percentage;
250 return $clone;
251 }
252
253 public function getHighscoreHints(): bool
254 {
255 return $this->highscore_hints;
256 }
257 public function withHighscoreHints(bool $highscore_hints): self
258 {
259 $clone = clone $this;
260 $clone->highscore_hints = $highscore_hints;
261 return $clone;
262 }
263
264 public function getHighscoreWTime(): bool
265 {
266 return $this->highscore_wtime;
267 }
268 public function withHighscoreWTime(bool $highscore_wtime): self
269 {
270 $clone = clone $this;
271 $clone->highscore_wtime = $highscore_wtime;
272 return $clone;
273 }
274}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
Builds data types.
Definition: Factory.php:21
return true
language handling
withHighscoreOwnTable(bool $highscore_own_table)
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, array $environment=null)
withHighscoreTopTable(bool $highscore_top_table)
withHighscoreAchievedTS(bool $highscore_achieved_ts)
withHighscorePercentage(bool $highscore_percentage)
This describes inputs that can be used in forms.
Definition: FormInput.php:32
This is what a factory for input fields looks like.
Definition: Factory.php:29
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
Refinery Factory $refinery
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
$lng