ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
SettingsGamification.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 use ILIAS\UI\Component\Input\Field\Factory as FieldFactory;
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_hints = 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 
47  public function __construct(int $test_id)
48  {
49  parent::__construct($test_id);
50  }
51 
52  public function toForm(
54  FieldFactory $f,
56  array $environment = null
57  ): FormInput {
58  $optional_group = $f->optionalGroup(
59  [
60  'highscore_mode' => $f->radio($lng->txt('tst_highscore_mode'), "")
61  ->withOption((string) self::HIGHSCORE_SHOW_OWN_TABLE, $lng->txt('tst_highscore_own_table'), $lng->txt('tst_highscore_own_table_description'))
62  ->withOption((string) self::HIGHSCORE_SHOW_TOP_TABLE, $lng->txt('tst_highscore_top_table'), $lng->txt('tst_highscore_top_table_description'))
63  ->withOption((string) self::HIGHSCORE_SHOW_ALL_TABLES, $lng->txt('tst_highscore_all_tables'), $lng->txt('tst_highscore_all_tables_description'))
64  ->withValue($this->getHighScoreMode() > 0 ? (string) $this->getHighScoreMode() : null)
65  ->withRequired(true)
66  ,
67  'highscore_top_num' => $f->numeric($lng->txt('tst_highscore_top_num'), $lng->txt('tst_highscore_top_num_description'))
68  ->withRequired(true)
69  ->withValue($this->getHighscoreTopNum()),
70  'highscore_anon' => $f->checkbox(
71  $lng->txt('tst_highscore_anon'),
72  $lng->txt('tst_highscore_anon_description')
73  )->withValue($this->getHighscoreAnon()),
74  'highscore_achieved_ts' => $f->checkbox(
75  $lng->txt('tst_highscore_achieved_ts'),
76  $lng->txt('tst_highscore_achieved_ts_description')
77  )->withValue($this->getHighscoreAchievedTS()),
78  'highscore_score' => $f->checkbox(
79  $lng->txt('tst_highscore_score'),
80  $lng->txt('tst_highscore_score_description')
81  )->withValue($this->getHighscoreScore()),
82  'highscore_percentage' => $f->checkbox(
83  $lng->txt('tst_highscore_percentage'),
84  $lng->txt('tst_highscore_percentage_description')
85  )->withValue($this->getHighscorePercentage()),
86  'highscore_hints' => $f->checkbox(
87  $lng->txt('tst_highscore_hints'),
88  $lng->txt('tst_highscore_hints_description')
89  )->withValue($this->getHighscoreHints()),
90  'highscore_wtime' => $f->checkbox(
91  $lng->txt('tst_highscore_wtime'),
92  $lng->txt('tst_highscore_wtime_description')
93  )->withValue($this->getHighscoreWTime())
94 
95  ],
96  $lng->txt('tst_highscore_enabled'),
97  $lng->txt('tst_highscore_description')
98  );
99 
100  if (!$this->getHighscoreEnabled()) {
101  $optional_group = $optional_group->withValue(null);
102  }
103 
104  $fields = ['highscore' => $optional_group];
105  return $f->section($fields, $lng->txt('tst_results_gamification'))
107  $refinery->custom()->transformation(
108  function ($v) {
109  $settings = clone $this;
110 
111  if (! $v['highscore']) {
112  return $settings->withHighscoreEnabled(false);
113  }
114 
115  return $settings
116  ->withHighscoreEnabled(true)
117  ->withHighscoreOwnTable(
118  (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_OWN_TABLE ||
119  (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
120  )
121  ->withHighscoreTopTable(
122  (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_TOP_TABLE ||
123  (int) $v['highscore']['highscore_mode'] == self::HIGHSCORE_SHOW_ALL_TABLES
124  )
125  ->withHighscoreTopNum($v['highscore']['highscore_top_num'])
126  ->withHighscoreAnon($v['highscore']['highscore_anon'])
127  ->withHighscoreAchievedTS($v['highscore']['highscore_achieved_ts'])
128  ->withHighscoreScore($v['highscore']['highscore_score'])
129  ->withHighscorePercentage($v['highscore']['highscore_percentage'])
130  ->withHighscoreHints($v['highscore']['highscore_hints'])
131  ->withHighscoreWTime($v['highscore']['highscore_wtime']);
132  }
133  )
134  );
135  }
136 
137  public function toStorage(): array
138  {
139  return [
140  'highscore_enabled' => ['integer', (int) $this->getHighscoreEnabled()],
141  'highscore_anon' => ['integer', (int) $this->getHighscoreAnon()],
142  'highscore_achieved_ts' => ['integer', (int) $this->getHighscoreAchievedTS()],
143  'highscore_score' => ['integer', (int) $this->getHighscoreScore()],
144  'highscore_percentage' => ['integer', (int) $this->getHighscorePercentage()],
145  'highscore_hints' => ['integer', (int) $this->getHighscoreHints()],
146  'highscore_wtime' => ['integer', (int) $this->getHighscoreWTime()],
147  'highscore_own_table' => ['integer', (int) $this->getHighscoreOwnTable()],
148  'highscore_top_table' => ['integer', (int) $this->getHighscoreTopTable()],
149  'highscore_top_num' => ['integer', $this->getHighscoreTopNum()]
150  ];
151  }
152 
153  public function toLog(AdditionalInformationGenerator $additional_info): array
154  {
155  if (!$this->getHighscoreEnabled()) {
156  return [
159  ];
160  }
161 
162  switch ($this->getHighScoreMode()) {
163  case self::HIGHSCORE_SHOW_OWN_TABLE:
164  $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_own_table');
165  break;
166  case self::HIGHSCORE_SHOW_TOP_TABLE:
167  $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_top_table');
168  break;
169  case self::HIGHSCORE_SHOW_ALL_TABLES:
170  $highscore_mode = $additional_info->getTagForLangVar('tst_highscore_all_tables');
171  break;
172  default:
173  $highscore_mode = $additional_info->getEnabledDisabledTagForBool(false);
174  }
175 
176  return [
193  ];
194  }
195 
196  public function getHighscoreEnabled(): bool
197  {
199  }
200  public function withHighscoreEnabled(bool $highscore_enabled): self
201  {
202  $clone = clone $this;
203  $clone->highscore_enabled = $highscore_enabled;
204  return $clone;
205  }
206 
207  public function getHighscoreOwnTable(): bool
208  {
210  }
211  public function withHighscoreOwnTable(bool $highscore_own_table): self
212  {
213  $clone = clone $this;
214  $clone->highscore_own_table = $highscore_own_table;
215  return $clone;
216  }
217  public function getHighscoreTopTable(): bool
218  {
220  }
221  public function withHighscoreTopTable(bool $highscore_top_table): self
222  {
223  $clone = clone $this;
224  $clone->highscore_top_table = $highscore_top_table;
225  return $clone;
226  }
227 
228  public function getHighScoreMode(): int
229  {
230  if ($this->getHighscoreTopTable() && $this->getHighscoreOwnTable()) {
231  return self::HIGHSCORE_SHOW_ALL_TABLES;
232  }
233 
234  if ($this->getHighscoreTopTable()) {
235  return self::HIGHSCORE_SHOW_TOP_TABLE;
236  }
237 
238  if ($this->getHighscoreOwnTable()) {
239  return self::HIGHSCORE_SHOW_OWN_TABLE;
240  }
241 
242  return 0;
243  }
244 
245  public function getHighscoreTopNum(): int
246  {
248  }
249  public function withHighscoreTopNum(int $highscore_top_num): self
250  {
251  $clone = clone $this;
252  $clone->highscore_top_num = $highscore_top_num;
253  return $clone;
254  }
255 
256  public function getHighscoreAnon(): bool
257  {
258  return $this->highscore_anon;
259  }
260  public function withHighscoreAnon(bool $highscore_anon): self
261  {
262  $clone = clone $this;
263  $clone->highscore_anon = $highscore_anon;
264  return $clone;
265  }
266 
267  public function getHighscoreAchievedTS(): bool
268  {
270  }
271  public function withHighscoreAchievedTS(bool $highscore_achieved_ts): self
272  {
273  $clone = clone $this;
274  $clone->highscore_achieved_ts = $highscore_achieved_ts;
275  return $clone;
276  }
277 
278  public function getHighscoreScore(): bool
279  {
280  return $this->highscore_score;
281  }
282  public function withHighscoreScore(bool $highscore_score): self
283  {
284  $clone = clone $this;
285  $clone->highscore_score = $highscore_score;
286  return $clone;
287  }
288 
289  public function getHighscorePercentage(): bool
290  {
292  }
293  public function withHighscorePercentage(bool $highscore_percentage): self
294  {
295  $clone = clone $this;
296  $clone->highscore_percentage = $highscore_percentage;
297  return $clone;
298  }
299 
300  public function getHighscoreHints(): bool
301  {
302  return $this->highscore_hints;
303  }
304  public function withHighscoreHints(bool $highscore_hints): self
305  {
306  $clone = clone $this;
307  $clone->highscore_hints = $highscore_hints;
308  return $clone;
309  }
310 
311  public function getHighscoreWTime(): bool
312  {
313  return $this->highscore_wtime;
314  }
315  public function withHighscoreWTime(bool $highscore_wtime): self
316  {
317  $clone = clone $this;
318  $clone->highscore_wtime = $highscore_wtime;
319  return $clone;
320  }
321 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
toLog(AdditionalInformationGenerator $additional_info)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, array $environment=null)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
withValue($value)
Get an input like this with another value displayed on the client side.
__construct(Container $dic, ilPlugin $plugin)
global $lng
Definition: privfeed.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:31