ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SettingsGamification.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use ILIAS\Refinery\Factory as Refinery;
28
30{
31 public const HIGHSCORE_SHOW_OWN_TABLE = 1;
32 public const HIGHSCORE_SHOW_TOP_TABLE = 2;
33 public const HIGHSCORE_SHOW_ALL_TABLES = 3;
34
35 protected bool $highscore_enabled = false;
36 protected bool $highscore_anon = true;
37 protected bool $highscore_achieved_ts = true;
38 protected bool $highscore_score = true;
39 protected bool $highscore_percentage = true;
40 protected bool $highscore_wtime = true;
41 protected bool $highscore_own_table = true;
42 protected bool $highscore_top_table = true;
43 protected int $highscore_top_num = 10;
44
45
46 public function __construct(int $test_id)
47 {
49 }
50
51 public function toForm(
53 FieldFactory $f,
55 ?array $environment = null
56 ): FormInput {
57 $optional_group = $f->optionalGroup(
58 [
59 'highscore_mode' => $f->radio($lng->txt('tst_highscore_mode'), "")
60 ->withOption((string) self::HIGHSCORE_SHOW_OWN_TABLE, $lng->txt('tst_highscore_own_table'), $lng->txt('tst_highscore_own_table_description'))
61 ->withOption((string) self::HIGHSCORE_SHOW_TOP_TABLE, $lng->txt('tst_highscore_top_table'), $lng->txt('tst_highscore_top_table_description'))
62 ->withOption((string) self::HIGHSCORE_SHOW_ALL_TABLES, $lng->txt('tst_highscore_all_tables'), $lng->txt('tst_highscore_all_tables_description'))
63 ->withValue($this->getHighScoreMode() > 0 ? (string) $this->getHighScoreMode() : null)
64 ->withRequired(true)
65 ,
66 'highscore_top_num' => $f->numeric($lng->txt('tst_highscore_top_num'), $lng->txt('tst_highscore_top_num_description'))
67 ->withRequired(true)
68 ->withValue($this->getHighscoreTopNum()),
69 'highscore_anon' => $f->checkbox(
70 $lng->txt('tst_highscore_anon'),
71 $lng->txt('tst_highscore_anon_description')
72 )->withValue($this->getHighscoreAnon()),
73 'highscore_achieved_ts' => $f->checkbox(
74 $lng->txt('tst_highscore_achieved_ts'),
75 $lng->txt('tst_highscore_achieved_ts_description')
77 'highscore_score' => $f->checkbox(
78 $lng->txt('tst_highscore_score'),
79 $lng->txt('tst_highscore_score_description')
80 )->withValue($this->getHighscoreScore()),
81 'highscore_percentage' => $f->checkbox(
82 $lng->txt('tst_highscore_percentage'),
83 $lng->txt('tst_highscore_percentage_description')
85 'highscore_wtime' => $f->checkbox(
86 $lng->txt('tst_highscore_wtime'),
87 $lng->txt('tst_highscore_wtime_description')
88 )->withValue($this->getHighscoreWTime())
89
90 ],
91 $lng->txt('tst_highscore_enabled'),
92 $lng->txt('tst_highscore_description')
93 );
94
95 if (!$this->getHighscoreEnabled()) {
96 $optional_group = $optional_group->withValue(null);
97 }
98
99 $fields = ['highscore' => $optional_group];
100 return $f->section($fields, $lng->txt('tst_results_gamification'))
101 ->withAdditionalTransformation(
102 $refinery->custom()->transformation(
103 function ($v) {
104 $settings = clone $this;
105
106 if (! $v['highscore']) {
107 return $settings->withHighscoreEnabled(false);
108 }
109
110 return $settings
111 ->withHighscoreEnabled(true)
113 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_OWN_TABLE ||
114 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
115 )
117 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_TOP_TABLE ||
118 (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
119 )
120 ->withHighscoreTopNum($v['highscore']['highscore_top_num'])
121 ->withHighscoreAnon($v['highscore']['highscore_anon'])
122 ->withHighscoreAchievedTS($v['highscore']['highscore_achieved_ts'])
123 ->withHighscoreScore($v['highscore']['highscore_score'])
124 ->withHighscorePercentage($v['highscore']['highscore_percentage'])
125 ->withHighscoreWTime($v['highscore']['highscore_wtime']);
126 }
127 )
128 );
129 }
130
131 public function toStorage(): array
132 {
133 return [
134 'highscore_enabled' => ['integer', (int) $this->getHighscoreEnabled()],
135 'highscore_anon' => ['integer', (int) $this->getHighscoreAnon()],
136 'highscore_achieved_ts' => ['integer', (int) $this->getHighscoreAchievedTS()],
137 'highscore_score' => ['integer', (int) $this->getHighscoreScore()],
138 'highscore_percentage' => ['integer', (int) $this->getHighscorePercentage()],
139 'highscore_wtime' => ['integer', (int) $this->getHighscoreWTime()],
140 'highscore_own_table' => ['integer', (int) $this->getHighscoreOwnTable()],
141 'highscore_top_table' => ['integer', (int) $this->getHighscoreTopTable()],
142 'highscore_top_num' => ['integer', $this->getHighscoreTopNum()]
143 ];
144 }
145
146 public function toLog(AdditionalInformationGenerator $additional_info): array
147 {
148 if (!$this->getHighscoreEnabled()) {
149 return [
152 ];
153 }
154
155 switch ($this->getHighScoreMode()) {
156 case self::HIGHSCORE_SHOW_OWN_TABLE:
157 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_own_table');
158 break;
159 case self::HIGHSCORE_SHOW_TOP_TABLE:
160 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_top_table');
161 break;
162 case self::HIGHSCORE_SHOW_ALL_TABLES:
163 $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_all_tables');
164 break;
165 default:
166 $highscore_mode = $additional_info->getEnabledDisabledTagForBool(false);
167 }
168
169 return [
175 ->getEnabledDisabledTagForBool($this->getHighscoreAnon()),
177 ->getEnabledDisabledTagForBool($this->getHighscoreAchievedTS()),
179 ->getEnabledDisabledTagForBool($this->getHighscoreScore()),
181 ->getEnabledDisabledTagForBool($this->getHighscorePercentage()),
183 ->getEnabledDisabledTagForBool($this->getHighscoreWTime())
184 ];
185 }
186
187 public function getHighscoreEnabled(): bool
188 {
189 return $this->highscore_enabled;
190 }
191 public function withHighscoreEnabled(bool $highscore_enabled): self
192 {
193 $clone = clone $this;
194 $clone->highscore_enabled = $highscore_enabled;
195 return $clone;
196 }
197
198 public function getHighscoreOwnTable(): bool
199 {
200 return $this->highscore_own_table;
201 }
202 public function withHighscoreOwnTable(bool $highscore_own_table): self
203 {
204 $clone = clone $this;
205 $clone->highscore_own_table = $highscore_own_table;
206 return $clone;
207 }
208 public function getHighscoreTopTable(): bool
209 {
210 return $this->highscore_top_table;
211 }
212 public function withHighscoreTopTable(bool $highscore_top_table): self
213 {
214 $clone = clone $this;
215 $clone->highscore_top_table = $highscore_top_table;
216 return $clone;
217 }
218
219 public function getHighScoreMode(): int
220 {
221 if ($this->getHighscoreTopTable() && $this->getHighscoreOwnTable()) {
222 return self::HIGHSCORE_SHOW_ALL_TABLES;
223 }
224
225 if ($this->getHighscoreTopTable()) {
226 return self::HIGHSCORE_SHOW_TOP_TABLE;
227 }
228
229 if ($this->getHighscoreOwnTable()) {
230 return self::HIGHSCORE_SHOW_OWN_TABLE;
231 }
232
233 return 0;
234 }
235
236 public function getHighscoreTopNum(): int
237 {
238 return $this->highscore_top_num;
239 }
240 public function withHighscoreTopNum(int $highscore_top_num): self
241 {
242 $clone = clone $this;
243 $clone->highscore_top_num = $highscore_top_num;
244 return $clone;
245 }
246
247 public function getHighscoreAnon(): bool
248 {
249 return $this->highscore_anon;
250 }
251 public function withHighscoreAnon(bool $highscore_anon): self
252 {
253 $clone = clone $this;
254 $clone->highscore_anon = $highscore_anon;
255 return $clone;
256 }
257
258 public function getHighscoreAchievedTS(): bool
259 {
260 return $this->highscore_achieved_ts;
261 }
262 public function withHighscoreAchievedTS(bool $highscore_achieved_ts): self
263 {
264 $clone = clone $this;
265 $clone->highscore_achieved_ts = $highscore_achieved_ts;
266 return $clone;
267 }
268
269 public function getHighscoreScore(): bool
270 {
271 return $this->highscore_score;
272 }
273 public function withHighscoreScore(bool $highscore_score): self
274 {
275 $clone = clone $this;
276 $clone->highscore_score = $highscore_score;
277 return $clone;
278 }
279
280 public function getHighscorePercentage(): bool
281 {
282 return $this->highscore_percentage;
283 }
284 public function withHighscorePercentage(bool $highscore_percentage): self
285 {
286 $clone = clone $this;
287 $clone->highscore_percentage = $highscore_percentage;
288 return $clone;
289 }
290
291 public function getHighscoreWTime(): bool
292 {
293 return $this->highscore_wtime;
294 }
295 public function withHighscoreWTime(bool $highscore_wtime): self
296 {
297 $clone = clone $this;
298 $clone->highscore_wtime = $highscore_wtime;
299 return $clone;
300 }
301}
Builds data types.
Definition: Factory.php:36
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
toLog(AdditionalInformationGenerator $additional_info)
return true
language handling
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
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
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