ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
SettingsGeneral.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ILIAS\Refinery\Factory as Refinery;
29
30class SettingsGeneral extends TestSettings implements Exportable
31{
32 public function __construct(
33 protected string $question_set_type = \ilObjTest::QUESTION_SET_TYPE_FIXED,
34 protected bool $anonymous_test = false
35 ) {
37 }
38
43 public function toForm(
45 FieldFactory $f,
47 ?array $environment = null
48 ): FormInput | array {
49 $inputs['question_set_type'] = $f->radio(
50 $lng->txt('test_question_set_type')
51 )->withOption(
52 \ilObjTest::QUESTION_SET_TYPE_FIXED,
53 $lng->txt('test_question_set_type_fixed'),
54 $lng->txt('test_question_set_type_fixed_info')
55 )->withOption(
56 \ilObjTest::QUESTION_SET_TYPE_RANDOM,
57 $lng->txt('test_question_set_type_random'),
58 $lng->txt('test_question_set_type_random_info')
59 )->withValue($this->getQuestionSetType());
60
61 $trafo = $refinery->custom()->transformation(
62 static function (string $v): bool {
63 if ($v === '1') {
64 return true;
65 }
66
67 return false;
68 }
69 );
70
71 $inputs['anonymity'] = $f->radio(
72 $lng->txt('tst_anonymity')
73 )->withOption(
74 '0',
75 $lng->txt('tst_anonymity_no_anonymization')
76 )->withOption(
77 '1',
78 $lng->txt('tst_anonymity_anonymous_test')
79 )->withValue($this->getAnonymity() ? '1' : '0')
80 ->withAdditionalTransformation($trafo);
81
82 if ($environment['participant_data_exists']) {
83 $inputs['question_set_type'] = $inputs['question_set_type']->withDisabled(true);
84 $inputs['anonymity'] = $inputs['anonymity']->withDisabled(true);
85 }
86
87 return $inputs;
88 }
89
90 public function toStorage(): array
91 {
92 return [
93 'question_set_type' => ['text', $this->getQuestionSetType()],
94 'anonymity' => ['integer', (int) $this->getAnonymity()]
95 ];
96 }
97
98 public function toLog(AdditionalInformationGenerator $additional_info): array
99 {
100 switch ($this->getQuestionSetType()) {
101 case \ilObjTest::QUESTION_SET_TYPE_FIXED:
103 ->getTagForLangVar('test_question_set_type_fixed');
104 break;
105 case \ilObjTest::QUESTION_SET_TYPE_RANDOM:
107 ->getTagForLangVar('test_question_set_type_random') ;
108 break;
109 }
110
111 $log_array[AdditionalInformationGenerator::KEY_TEST_ANONYMITY] = $additional_info
112 ->getEnabledDisabledTagForBool($this->getAnonymity());
113 return $log_array;
114 }
115
116 public function getQuestionSetType(): string
117 {
118 return $this->question_set_type;
119 }
120
121 public function withQuestionSetType(string $question_set_type): self
122 {
123 $clone = clone $this;
124 $clone->question_set_type = $question_set_type;
125 return $clone;
126 }
127
128 public function getAnonymity(): bool
129 {
130 return $this->anonymous_test;
131 }
132
133 public function withAnonymity(bool $anonymous_test): self
134 {
135 $clone = clone $this;
136 $clone->anonymous_test = $anonymous_test;
137 return $clone;
138 }
139
140 public function toExport(): array
141 {
142 return [
143 'question_set_type' => $this->getQuestionSetType(),
144 'anonymity' => $this->getAnonymity()
145 ];
146 }
147
148 public static function fromExport(array $data): static
149 {
150 return new self(
151 (string) $data['question_set_type'],
152 (bool) $data['anonymity']
153 );
154 }
155}
Builds data types.
Definition: Factory.php:36
static fromExport(array $data)
Creates an instance of the object from an array.
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
toExport()
Transform the object into a simple, associative array.
toLog(AdditionalInformationGenerator $additional_info)
__construct(protected string $question_set_type=\ilObjTest::QUESTION_SET_TYPE_FIXED, protected bool $anonymous_test=false)
language handling
const QUESTION_SET_TYPE_FIXED
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
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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