ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.SettingsScoringGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Test\Scoring\Settings\Settings as SettingsScoring;
28use ILIAS\UI\Factory as UIFactory;
29use ILIAS\UI\Renderer as UIRenderer;
31use ILIAS\Data\Factory as DataFactory;
34use ilObjTestGUI;
35use Psr\Http\Message\ServerRequestInterface as Request;
36
49{
53 public const CMD_SHOW_FORM = 'showForm';
54 public const CMD_SAVE_FORM = 'saveForm';
55 public const CMD_CONFIRMED_RECALC = 'saveFormAndRecalc';
56 public const CMD_CANCEL_RECALC = 'cancelSaveForm';
57 private const F_CONFIRM_SETTINGS = 'f_settings';
58
59 public function __construct(
60 protected readonly \ilCtrlInterface $ctrl,
61 protected readonly \ilAccessHandler $access,
62 protected readonly \ilLanguage $lng,
63 protected readonly \ilTree $tree,
64 protected readonly \ilDBInterface $db,
65 protected readonly \ilComponentRepository $component_repository,
66 protected readonly \ilObjTestGUI $test_gui,
67 protected readonly \ilGlobalTemplateInterface $tpl,
68 protected readonly \ilTabsGUI $tabs,
69 protected readonly TestLogger $logger,
70 protected readonly ScoreSettingsRepository $score_settings_repo,
71 protected readonly int $test_id,
72 protected readonly UIFactory $ui_factory,
73 protected readonly UIRenderer $ui_renderer,
74 protected readonly Refinery $refinery,
75 protected readonly Request $request,
76 protected readonly \ilObjUser $active_user
77 ) {
78 parent::__construct($test_gui->getObject());
79 }
80
81 protected function loadScoreSettings(): ScoreSettings
82 {
83 return $this->score_settings_repo->getFor($this->test_id);
84 }
85 protected function storeScoreSettings(ScoreSettings $score_settings): void
86 {
87 $this->score_settings_repo->store($score_settings);
88 }
89
93 public function executeCommand()
94 {
95 if (!$this->access->checkAccess('write', '', $this->test_gui->getRefId())) {
96 $this->tpl->setOnScreenMessage('info', $this->lng->txt('cannot_edit_test'), true);
97 $this->ctrl->redirectByClass([\ilRepositoryGUI::class, \ilObjTestGUI::class, \ilInfoScreenGUI::class]);
98 }
99
100 $this->tabs->activateSubTab(TabsManager::SETTINGS_SUBTAB_ID_SCORING);
101
102 $nextClass = $this->ctrl->getNextClass();
103 switch ($nextClass) {
104 default:
105 $cmd = $this->ctrl->getCmd(self::CMD_SHOW_FORM);
106
107 switch ($cmd) {
109 $this->showForm();
110 break;
112 $this->saveForm();
113 break;
115 $this->saveForm();
116 $settings = $this->buildForm()
117 ->withRequest($this->getRelayedRequest())
118 ->getData();
119 $this->storeScoreSettings($settings);
120 $this->test_object->recalculateScores(true);
121 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_score_settings_modified_and_recalc"), true);
122 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
123 break;
125 $this->tpl->setOnScreenMessage('info', $this->lng->txt("msg_score_settings_not_modified"), true);
126 $form = $this->buildForm()->withRequest($this->getRelayedRequest());
127 $this->showForm($form);
128 break;
129 default:
130 throw new \Exception('unknown command: ' . $cmd);
131 }
132 }
133 }
134
135 private function showForm(?Form $form = null): void
136 {
137 if ($form === null) {
138 $form = $this->buildForm();
139 }
140
141 $this->tpl->setContent($this->ui_renderer->render($form));
142 }
143
144 private function saveForm(): void
145 {
146 $form = $this->buildForm()
147 ->withRequest($this->request);
148
149 $settings = $form->getData();
150
151 if (is_null($settings)) {
152 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
153 $this->showForm($form);
154 return;
155 }
156
158 $settings->getScoringSettings(),
159 $this->loadScoreSettings()->getScoringSettings()
160 )) {
161 $this->showConfirmation($this->request);
162 return;
163 }
164
165 $this->storeScoreSettings($settings);
166 if ($this->logger->isLoggingEnabled()) {
167 $this->logger->logTestAdministrationInteraction(
168 $this->logger->getInteractionFactory()->buildTestAdministrationInteraction(
169 $this->test_gui->getRefId(),
170 $this->active_user->getId(),
171 TestAdministrationInteractionTypes::SCORING_SETTINGS_MODIFIED,
172 $settings->getArrayForLog($this->logger->getAdditionalInformationGenerator())
173 )
174 );
175 }
176 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
177 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
178 }
179
180 private function getRelayedRequest(): Request
181 {
182 return unserialize(
183 base64_decode(
184 $this->request->getParsedBody()[self::F_CONFIRM_SETTINGS]
185 ),
186 [
187 'allowed_classes' => [
188 \GuzzleHttp\Psr7\ServerRequest::class,
189 \GuzzleHttp\Psr7\Uri::class,
190 \GuzzleHttp\Psr7\UploadedFile::class,
191 \GuzzleHttp\Psr7\Stream::class,
192 ]
193 ]
194 );
195 }
196
197 private function buildForm(): Form
198 {
199 $ui_pack = [
201 $this->ui_factory->input()->field(),
203 ];
204
205 $environment = [
206 'user_date_format' => $this->active_user->getDateTimeFormat(),
207 'user_time_zone' => $this->active_user->getTimeZone()
208 ];
209
210 $disabled_flag = ($this->areScoringSettingsWritable() === false);
211
212 $settings = $this->loadScoreSettings();
213 $sections = [
214 'scoring' => $settings->getScoringSettings()->toForm(...$ui_pack)
215 ->withDisabled($disabled_flag),
216 'summary' => $settings->getResultSummarySettings()->toForm(...array_merge($ui_pack, [$environment])),
217 'details' => $settings->getResultDetailsSettings()->toForm(
218 ...array_merge($ui_pack, [['taxonomy_options' => $this->getTaxonomyOptions()]])
219 ),
220 'gameification' => $settings->getGamificationSettings()->toForm(...$ui_pack)
221 ];
222
223 $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_FORM);
224 $form = $this->ui_factory->input()->container()->form()
225 ->standard($action, $sections)
226 ->withAdditionalTransformation(
227 $this->refinery->custom()->transformation(
228 function ($v) use ($settings) {
229 return $settings
230 ->withScoringSettings($v['scoring'])
231 ->withResultSummarySettings($v['summary'])
232 ->withResultDetailsSettings($v['details'])
233 ->withGamificationSettings($v['gameification'])
234 ;
235 }
236 )
237 );
238 return $form;
239 }
240
241 private function isScoreReportingAvailable(): bool
242 {
243 $result_summary_settings = $this->test_object->getScoreSettings()
244 ->getResultSummarySettings();
245 if (!$result_summary_settings->getScoreReporting()->isReportingEnabled()) {
246 return false;
247 }
248
249 if ($result_summary_settings->getScoreReporting() === ScoreReportingTypes::SCORE_REPORTING_DATE) {
250 return $result_summary_settings->getReportingDate()
251 <= new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
252 }
253
254 return true;
255 }
256
257 private function areScoringSettingsWritable(): bool
258 {
259 if (!$this->test_object->participantDataExist()) {
260 return true;
261 }
262
263 if (!$this->isScoreReportingAvailable()) {
264 return true;
265 }
266
267 return false;
268 }
269
270 protected function getTaxonomyOptions(): array
271 {
272 $available_taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->test_object->getId());
273 $taxononmy_translator = new \ilTestQuestionFilterLabelTranslator($this->db, $this->lng);
274 $taxononmy_translator->loadLabelsFromTaxonomyIds($available_taxonomy_ids);
275
276 $taxonomy_options = [];
277 foreach ($available_taxonomy_ids as $tax_id) {
278 $taxonomy_options[$tax_id] = $taxononmy_translator->getTaxonomyTreeLabel($tax_id);
279 }
280 return $taxonomy_options;
281 }
282
284 SettingsScoring $new_settings,
285 SettingsScoring $old_settings
286 ): bool {
287 $settings_changed = (
288 $new_settings->getCountSystem() !== $old_settings->getCountSystem() ||
289 $new_settings->getScoreCutting() !== $old_settings->getScoreCutting() ||
290 $new_settings->getPassScoring() !== $old_settings->getPassScoring()
291 );
292
293 return
294 $this->test_object->participantDataExist() &&
296 $settings_changed;
297 }
298
299
300 private function showConfirmation(Request $request)
301 {
302 $confirmation = new \ilConfirmationGUI();
303 $confirmation->setHeaderText($this->lng->txt('tst_trigger_result_refreshing'));
304 $confirmation->setFormAction($this->ctrl->getFormAction($this));
305 $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_RECALC);
306 $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_CONFIRMED_RECALC);
307 $confirmation->addHiddenItem(self::F_CONFIRM_SETTINGS, base64_encode(serialize($request)));
308 $this->tpl->setContent($this->ctrl->getHTML($confirmation));
309 }
310}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
isScoreRecalculationRequired(SettingsScoring $new_settings, SettingsScoring $old_settings)
__construct(protected readonly \ilCtrlInterface $ctrl, protected readonly \ilAccessHandler $access, protected readonly \ilLanguage $lng, protected readonly \ilTree $tree, protected readonly \ilDBInterface $db, protected readonly \ilComponentRepository $component_repository, protected readonly \ilObjTestGUI $test_gui, protected readonly \ilGlobalTemplateInterface $tpl, protected readonly \ilTabsGUI $tabs, protected readonly TestLogger $logger, protected readonly ScoreSettingsRepository $score_settings_repo, protected readonly int $test_id, protected readonly UIFactory $ui_factory, protected readonly UIRenderer $ui_renderer, protected readonly Refinery $refinery, protected readonly Request $request, protected readonly \ilObjUser $active_user)
Class ilInfoScreenGUI.
language handling
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
Class ilObjTestGUI.
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
This describes commonalities between all forms.
Definition: Form.php:33
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Readable part of repository interface to ilComponentDataDB.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $lng
Definition: privfeed.php:31