ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Setting.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ILIAS\Refinery\Custom\Constraint as CustomConstraint;
31use ILIAS\Refinery\Factory as Refinery;
32
33class Setting implements SettingDefinition
34{
36
37 public function __construct()
38 {
39 $this->starting_point_repository = LocalDIC::dic()[Repository::class];
40 }
41
42 public function getIdentifier(): string
43 {
44 return 'starting_point';
45 }
46
47 public function isAvailable(): bool
48 {
49 return true;
50 }
51
52 public function getLabel(Language $lng): string
53 {
54 return $lng->txt($this->getIdentifier());
55 }
56
58 {
59 return AvailablePages::MainSettings;
60 }
61
62 public function getSection(): AvailableSections
63 {
65 }
66
67 public function getInput(
68 FieldFactory $field_factory,
71 \ilSetting $settings,
72 ?\ilObjUser $user = null
73 ): Input {
74 $starting_point_id = null;
75 $object_ref_id = null;
76 if ($user !== null) {
77 [
78 'starting_point_id' => $starting_point_id ,
79 'object_id' => $object_ref_id
80 ] = $this->retrieveValueFromUser($user);
81 }
82 $possible_starting_points = $this->starting_point_repository->getPossibleStartingPoints();
83 return $field_factory->switchableGroup(
84 array_reduce(
85 array_keys($possible_starting_points),
86 static function (array $c, int $v) use (
87 $field_factory,
88 $lng,
89 $possible_starting_points,
90 $object_ref_id
91 ): array {
92 $c[$v] = $field_factory->group(
94 ? [
95 'usr_start_ref_id' => $field_factory->numeric(
96 $lng->txt('adm_user_starting_point_ref_id'),
97 $object_ref_id === null || ($start_obj_id = \ilObject::_lookupObjId($object_ref_id)) === 0
98 ? $lng->txt('adm_user_starting_point_ref_id_info')
99 : $lng->txt('obj_' . \ilObject::_lookupType($start_obj_id))
100 . ': ' . \ilObject::_lookupTitle($start_obj_id)
101 )->withRequired(true)
102 ] : [],
103 $lng->txt($possible_starting_points[$v])
104 );
105 return $c;
106 },
107 [
108 0 => $field_factory->group(
109 [],
110 $lng->txt('adm_user_starting_point_inherit'),
111 $lng->txt('adm_user_starting_point_inherit_info')
112 )
113 ]
114 ),
115 $lng->txt('adm_user_starting_point'),
116 $lng->txt('adm_user_starting_point_info')
117 )->withAdditionalTransformation(
118 $this->buildValidateObjectConstraint($refinery, $lng)
119 )->withAdditionalTransformation(
120 $refinery->custom()->transformation(
121 static fn(array $v): array => [
122 'starting_point_id' => $refinery->kindlyTo()->int()->transform($v[0]),
123 'object_id' => $v[1]['usr_start_ref_id'] ?? null
124 ]
125 )
126 )->withValue(
128 $starting_point_id,
129 $object_ref_id
130 )
131 );
132 }
133
134 public function getLegacyInput(
136 \ilSetting $settings,
137 ?\ilObjUser $user = null
139 $starting_point_id = null;
140 $object_ref_id = null;
141 if ($user !== null) {
142 ['starting_point_id' => $starting_point_id , 'object_id' => $object_ref_id] = $this->retrieveValueFromUser($user);
143 }
144 $input = new \ilRadioGroupInputGUI($lng->txt('adm_user_starting_point'));
145 $input->setInfo($lng->txt('adm_user_starting_point_info'));
146 $inherit_starting_point = new \ilRadioOption($lng->txt('adm_user_starting_point_inherit'), '0');
147 $inherit_starting_point->setInfo($lng->txt('adm_user_starting_point_inherit_info'));
148 $input->addOption($inherit_starting_point);
149 foreach ($this->starting_point_repository->getPossibleStartingPoints() as $value => $caption) {
150 if ($value === Repository::START_REPOSITORY_OBJ) {
151 continue;
152 }
153 $input->addOption(new \ilRadioOption($lng->txt($caption), (string) $value));
154 }
155 $input->setValue((string) $starting_point_id);
156
157 $starting_point_repository = new \ilRadioOption(
158 $lng->txt('adm_user_starting_point_object'),
160 );
161 $repository_object_id = new \ilTextInputGUI($lng->txt('adm_user_starting_point_ref_id'), 'usr_start_ref_id');
162 $repository_object_id->setInfo($lng->txt('adm_user_starting_point_ref_id_info'));
163 $repository_object_id->setRequired(true);
164 $repository_object_id->setSize(5);
165 if ($object_ref_id !== null) {
166 $repository_object_id->setValue($object_ref_id);
167 if (($start_obj_id = \ilObject::_lookupObjId($object_ref_id)) !== 0) {
168 $repository_object_id->setInfo(
169 $lng->txt('obj_' . \ilObject::_lookupType($start_obj_id)) .
170 ': ' . \ilObject::_lookupTitle($start_obj_id)
171 );
172 }
173 }
174 $starting_point_repository->addSubItem($repository_object_id);
175 $input->addOption($starting_point_repository);
176 return $input;
177 }
178
181 \ilSetting $settings
182 ): string {
183 $default_starting_point = $this->starting_point_repository->getSystemDefaultStartingPointType();
184 $starting_point = $this->starting_point_repository->getPossibleStartingPoints()[$default_starting_point];
185 if ($default_starting_point !== Repository::START_REPOSITORY_OBJ
186 || ($ref_id = $this->starting_point_repository->getSystemDefaultStartingObject()) === null
187 || ($obj_id = \ilObject::_lookupObjId($ref_id)) === 0) {
188 return $starting_point;
189 }
190 return $lng->txt('obj_' . \ilObject::_lookupType($obj_id)) . ' - ' . \ilObject::_lookupTitle($obj_id);
191 }
192
194 \ilSetting $settings,
195 \ilObjUser $user
196 ): bool {
197 return $this->starting_point_repository->isPersonalStartingPointEnabledForUser($user);
198 }
199
200 public function persistUserInput(
201 \ilObjUser $user,
202 mixed $input,
203 ?\ilPropertyFormGUI $form = null
204 ): \ilObjUser {
205 if ($input === null) {
206 $starting_point_id = 0;
207 $object_ref_id = null;
208 } elseif (is_array($input)) {
209 ['starting_point_id' => $starting_point_id , 'object_id' => $object_ref_id] = $input;
210 } else {
211 $starting_point_id = (int) $input;
212 $object_ref_id_input = $form->getInput('usr_start_ref_id');
213 $object_ref_id = $object_ref_id_input === '' ? null : (int) $object_ref_id_input;
214 }
215 $this->starting_point_repository->setPersonalStartingPointForUser(
216 $user,
217 $starting_point_id,
218 $object_ref_id
219 );
220 return $user;
221 }
222
223 public function validateUserChoice(
226 \ilPropertyFormGUI $form
227 ): bool {
228 if ($form->getInput($this->getIdentifier()) !== Repository::START_REPOSITORY_OBJ) {
229 return true;
230 }
231
232 $ref_id = $form->getInput('usr_start_ref_id');
233 if (!is_numeric($ref_id) || !\ilObject::_exists((int) $ref_id, true)) {
234 $tpl->setOnScreenMessage('failure', $lng->txt('obj_ref_id_not_exist'), true);
235 return false;
236 }
237
238 return true;
239 }
240
244 ): CustomConstraint {
245 return $refinery->custom()->constraint(
246 function (array $v): bool {
247 if ($v[0] !== Repository::START_REPOSITORY_OBJ) {
248 return true;
249 }
250 if (!is_int($v[1]['usr_start_ref_id']) || !\ilObject::_exists($v[1]['usr_start_ref_id'], true)) {
251 return false;
252 }
253 return true;
254 },
255 $lng->txt('obj_ref_id_not_exist')
256 );
257 }
258
259 public function retrieveValueFromUser(\ilObjUser $user): array
260 {
261 return [
262 'starting_point_id' => $this->starting_point_repository->getPersonalStartingPointForUser($user),
263 'object_id' => $this->starting_point_repository->getPersonalStartingObjectForUser($user)
264 ];
265 }
266
267 private function buildValueSetterArray(
268 int $starting_point_id,
269 ?int $object_ref_id
270 ): int|array {
271 if ($starting_point_id !== Repository::START_REPOSITORY_OBJ) {
272 return $starting_point_id;
273 }
274
275 return [
276 0 => $starting_point_id,
277 1 => [
278 'usr_start_ref_id' => $object_ref_id
279 ]
280 ];
281 }
282}
Builds data types.
Definition: Factory.php:36
buildValueSetterArray(int $starting_point_id, ?int $object_ref_id)
Definition: Setting.php:267
getLegacyInput(Language $lng, \ilSetting $settings, ?\ilObjUser $user=null)
You don't need to add a post_var to the input as the User will handle this for you,...
Definition: Setting.php:134
readonly Repository $starting_point_repository
Definition: Setting.php:35
buildValidateObjectConstraint(Refinery $refinery, Language $lng)
Definition: Setting.php:241
isAvailable()
If this function returns false the setting will not be shown, even if it's PropertyAttributes would a...
Definition: Setting.php:47
persistUserInput(\ilObjUser $user, mixed $input, ?\ilPropertyFormGUI $form=null)
Definition: Setting.php:200
getInput(FieldFactory $field_factory, Language $lng, Refinery $refinery, \ilSetting $settings, ?\ilObjUser $user=null)
Definition: Setting.php:67
hasUserPersonalizedSetting(\ilSetting $settings, \ilObjUser $user)
Definition: Setting.php:193
validateUserChoice(\ilGlobalTemplateInterface $tpl, Language $lng, \ilPropertyFormGUI $form)
Definition: Setting.php:223
getDefaultValueForDisplay(Language $lng, \ilSetting $settings)
Definition: Setting.php:179
This class represents a property in a property form.
User class.
static _lookupType(int $id, bool $reference=false)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
This class represents a property form user interface.
This class represents an option in a radio group.
ILIAS Setting Class.
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is what a factory for input fields looks like.
Definition: Factory.php:31
This describes commonalities between all inputs.
Definition: Input.php:47
$ref_id
Definition: ltiauth.php:66
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))