ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
SettingsGamification.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ILIAS\Refinery\Factory as Refinery;
29
31{
32 public const HIGHSCORE_SHOW_OWN_TABLE = 1;
33 public const HIGHSCORE_SHOW_TOP_TABLE = 2;
34 public const HIGHSCORE_SHOW_ALL_TABLES = 3;
35
36 protected bool $highscore_enabled = false;
37 protected bool $highscore_anon = true;
38 protected bool $highscore_achieved_ts = true;
39 protected bool $highscore_score = true;
40 protected bool $highscore_percentage = true;
41 protected bool $highscore_wtime = true;
42 protected bool $highscore_own_table = true;
43 protected bool $highscore_top_table = true;
44 protected int $highscore_top_num = 10;
45
46 public function toForm(
48 FieldFactory $f,
50 ?array $environment = null
51 ): FormInput {
52 $optional_group = $f->optionalGroup(
53 [
54 'highscore_mode' => $f->radio($lng->txt('tst_highscore_mode'), "")
55 ->withOption((string) self::HIGHSCORE_SHOW_OWN_TABLE, $lng->txt('tst_highscore_own_table'), $lng->txt('tst_highscore_own_table_description'))
56 ->withOption((string) self::HIGHSCORE_SHOW_TOP_TABLE, $lng->txt('tst_highscore_top_table'), $lng->txt('tst_highscore_top_table_description'))
57 ->withOption((string) self::HIGHSCORE_SHOW_ALL_TABLES, $lng->txt('tst_highscore_all_tables'), $lng->txt('tst_highscore_all_tables_description'))
58 ->withValue($this->getHighScoreMode() > 0 ? (string) $this->getHighScoreMode() : null)
59 ->withRequired(true)
60 ,
61 'highscore_top_num' => $f->numeric($lng->txt('tst_highscore_top_num'), $lng->txt('tst_highscore_top_num_description'))
62 ->withRequired(true)
63 ->withValue($this->getHighscoreTopNum()),
64 'highscore_anon' => $f->checkbox(
65 $lng->txt('tst_highscore_anon'),
66 $lng->txt('tst_highscore_anon_description')
67 )->withValue($this->getHighscoreAnon()),
68 'highscore_achieved_ts' => $f->checkbox(
69 $lng->txt('tst_highscore_achieved_ts'),
70 $lng->txt('tst_highscore_achieved_ts_description')
72 'highscore_score' => $f->checkbox(
73 $lng->txt('tst_highscore_score'),
74 $lng->txt('tst_highscore_score_description')
75 )->withValue($this->getHighscoreScore()),
76 'highscore_percentage' => $f->checkbox(
77 $lng->txt('tst_highscore_percentage'),
78 $lng->txt('tst_highscore_percentage_description')
80 'highscore_wtime' => $f->checkbox(
81 $lng->txt('tst_highscore_wtime'),
82 $lng->txt('tst_highscore_wtime_description')
83 )->withValue($this->getHighscoreWTime())
84
85 ],
86 $lng->txt('tst_highscore_enabled'),
87 $lng->txt('tst_highscore_description')
88 );
89
90 if (!$this->getHighscoreEnabled()) {
91 $optional_group = $optional_group->withValue(null);
92 }
93
94 $fields = ['highscore' => $optional_group];
95 return $f->section($fields, $lng->txt('tst_results_gamification'))
96 ->withAdditionalTransformation(
97 $refinery->custom()->transformation(
98 function ($v) {
99 $settings = clone $this;
100
101 if (! $v['highscore']) {
102 return $settings->withHighscoreEnabled(false);
103 }
104
105 return $settings
106 ->withHighscoreEnabled(true)
108 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_OWN_TABLE ||
109 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
110 )
112 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_TOP_TABLE ||
113 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
114 )
115 ->withHighscoreTopNum($v['highscore']['highscore_top_num'])
116 ->withHighscoreAnon($v['highscore']['highscore_anon'])
117 ->withHighscoreAchievedTS($v['highscore']['highscore_achieved_ts'])
118 ->withHighscoreScore($v['highscore']['highscore_score'])
119 ->withHighscorePercentage($v['highscore']['highscore_percentage'])
120 ->withHighscoreWTime($v['highscore']['highscore_wtime']);
121 }
122 )
123 );
124 }
125
126 public function toStorage(): array
127 {
128 return [
129 'highscore_enabled' => ['integer', (int) $this->getHighscoreEnabled()],
130 'highscore_anon' => ['integer', (int) $this->getHighscoreAnon()],
131 'highscore_achieved_ts' => ['integer', (int) $this->getHighscoreAchievedTS()],
132 'highscore_score' => ['integer', (int) $this->getHighscoreScore()],
133 'highscore_percentage' => ['integer', (int) $this->getHighscorePercentage()],
134 'highscore_wtime' => ['integer', (int) $this->getHighscoreWTime()],
135 'highscore_own_table' => ['integer', (int) $this->getHighscoreOwnTable()],
136 'highscore_top_table' => ['integer', (int) $this->getHighscoreTopTable()],
137 'highscore_top_num' => ['integer', $this->getHighscoreTopNum()]
138 ];
139 }
140
141 public function toLog(AdditionalInformationGenerator $additional_info): array
142 {
143 if (!$this->getHighscoreEnabled()) {
144 return [
147 ];
148 }
149
150 switch ($this->getHighScoreMode()) {
151 case self::HIGHSCORE_SHOW_OWN_TABLE:
152 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_own_table');
153 break;
154 case self::HIGHSCORE_SHOW_TOP_TABLE:
155 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_top_table');
156 break;
157 case self::HIGHSCORE_SHOW_ALL_TABLES:
158 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_all_tables');
159 break;
160 default:
161 $highscore_mode = $additional_info->getEnabledDisabledTagForBool(false);
162 }
163
164 return [
170 ->getEnabledDisabledTagForBool($this->getHighscoreAnon()),
172 ->getEnabledDisabledTagForBool($this->getHighscoreAchievedTS()),
174 ->getEnabledDisabledTagForBool($this->getHighscoreScore()),
176 ->getEnabledDisabledTagForBool($this->getHighscorePercentage()),
178 ->getEnabledDisabledTagForBool($this->getHighscoreWTime())
179 ];
180 }
181
182 public function getHighscoreEnabled(): bool
183 {
184 return $this->highscore_enabled;
185 }
186 public function withHighscoreEnabled(bool $highscore_enabled): self
187 {
188 $clone = clone $this;
189 $clone->highscore_enabled = $highscore_enabled;
190 return $clone;
191 }
192
193 public function getHighscoreOwnTable(): bool
194 {
195 return $this->highscore_own_table;
196 }
197 public function withHighscoreOwnTable(bool $highscore_own_table): self
198 {
199 $clone = clone $this;
200 $clone->highscore_own_table = $highscore_own_table;
201 return $clone;
202 }
203 public function getHighscoreTopTable(): bool
204 {
205 return $this->highscore_top_table;
206 }
207 public function withHighscoreTopTable(bool $highscore_top_table): self
208 {
209 $clone = clone $this;
210 $clone->highscore_top_table = $highscore_top_table;
211 return $clone;
212 }
213
214 public function getHighScoreMode(): int
215 {
216 if ($this->getHighscoreTopTable() && $this->getHighscoreOwnTable()) {
217 return self::HIGHSCORE_SHOW_ALL_TABLES;
218 }
219
220 if ($this->getHighscoreTopTable()) {
221 return self::HIGHSCORE_SHOW_TOP_TABLE;
222 }
223
224 if ($this->getHighscoreOwnTable()) {
225 return self::HIGHSCORE_SHOW_OWN_TABLE;
226 }
227
228 return 0;
229 }
230
231 public function getHighscoreTopNum(): int
232 {
233 return $this->highscore_top_num;
234 }
235 public function withHighscoreTopNum(int $highscore_top_num): self
236 {
237 $clone = clone $this;
238 $clone->highscore_top_num = $highscore_top_num;
239 return $clone;
240 }
241
242 public function getHighscoreAnon(): bool
243 {
244 return $this->highscore_anon;
245 }
246 public function withHighscoreAnon(bool $highscore_anon): self
247 {
248 $clone = clone $this;
249 $clone->highscore_anon = $highscore_anon;
250 return $clone;
251 }
252
253 public function getHighscoreAchievedTS(): bool
254 {
255 return $this->highscore_achieved_ts;
256 }
257 public function withHighscoreAchievedTS(bool $highscore_achieved_ts): self
258 {
259 $clone = clone $this;
260 $clone->highscore_achieved_ts = $highscore_achieved_ts;
261 return $clone;
262 }
263
264 public function getHighscoreScore(): bool
265 {
266 return $this->highscore_score;
267 }
268 public function withHighscoreScore(bool $highscore_score): self
269 {
270 $clone = clone $this;
271 $clone->highscore_score = $highscore_score;
272 return $clone;
273 }
274
275 public function getHighscorePercentage(): bool
276 {
277 return $this->highscore_percentage;
278 }
279 public function withHighscorePercentage(bool $highscore_percentage): self
280 {
281 $clone = clone $this;
282 $clone->highscore_percentage = $highscore_percentage;
283 return $clone;
284 }
285
286 public function getHighscoreWTime(): bool
287 {
288 return $this->highscore_wtime;
289 }
290 public function withHighscoreWTime(bool $highscore_wtime): self
291 {
292 $clone = clone $this;
293 $clone->highscore_wtime = $highscore_wtime;
294 return $clone;
295 }
296
297 public function toExport(): array
298 {
299 return [
300 'highscore_enabled' => $this->getHighscoreEnabled(),
301 'highscore_anon' => $this->getHighscoreAnon(),
302 'highscore_achieved_ts' => $this->getHighscoreAchievedTS(),
303 'highscore_score' => $this->getHighscoreScore(),
304 'highscore_percentage' => $this->getHighscorePercentage(),
305 'highscore_wtime' => $this->getHighscoreWTime(),
306 'highscore_own_table' => $this->getHighscoreOwnTable(),
307 'highscore_top_table' => $this->getHighscoreTopTable(),
308 'highscore_top_num' => $this->getHighscoreTopNum(),
309 ];
310 }
311
312 public static function fromExport(array $data): static
313 {
314 return (new self())
315 ->withHighscoreEnabled((bool) $data['highscore_enabled'])
316 ->withHighscoreAnon((bool) $data['highscore_anon'])
317 ->withHighscoreAchievedTS((bool) $data['highscore_achieved_ts'])
318 ->withHighscoreScore((bool) $data['highscore_score'])
319 ->withHighscorePercentage((bool) $data['highscore_percentage'])
320 ->withHighscoreWTime((bool) $data['highscore_wtime'])
321 ->withHighscoreOwnTable((bool) $data['highscore_own_table'])
322 ->withHighscoreTopTable((bool) $data['highscore_top_table'])
323 ->withHighscoreTopNum((int) $data['highscore_top_num']);
324 }
325}
Builds data types.
Definition: Factory.php:36
toExport()
Transform the object into a simple, associative array.
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
toLog(AdditionalInformationGenerator $additional_info)
static fromExport(array $data)
Creates an instance of the object from an array.
return true
language handling
This interface allows an object to define its own transformation into a language-neutral,...
Definition: Exportable.php:40
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is what a factory for input fields looks like.
Definition: Factory.php:31
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
global $lng
Definition: privfeed.php:31