ILIAS  release_8 Revision v8.24
class.ilObjTestSettingsScoringResultsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\UI\Factory as UIFactory;
22use ILIAS\UI\Renderer as UIRenderer;
23use ILIAS\Refinery\Factory as Refinery;
25use Psr\Http\Message\ServerRequestInterface as Request;
26
39{
43 public const CMD_SHOW_FORM = 'showForm';
44 public const CMD_SAVE_FORM = 'saveForm';
45 public const CMD_CONFIRMED_RECALC = 'saveFormAndRecalc';
46 public const CMD_CANCEL_RECALC = 'cancelSaveForm';
47 private const F_CONFIRM_SETTINGS = 'f_settings';
48
51 protected ilLanguage $lng;
53 protected ilTree $tree;
54 protected ilDBInterface $db;
58
60 protected int $test_id;
61 protected UIFactory $ui_factory;
62 protected UIRenderer $ui_renderer;
63 protected Refinery $refinery;
64 protected ilTabsGUI $tabs;
66
67
68 public function __construct(
76 \ilGlobalTemplateInterface $main_template,
79 int $test_id,
80 UIFactory $ui_factory,
81 UIRenderer $ui_renderer,
82 Refinery $refinery,
83 Request $request,
85 ) {
86 $this->ctrl = $ctrl;
87 $this->access = $access;
88 $this->lng = $lng;
89 $this->tree = $tree;
90 $this->db = $db;
91 $this->component_repository = $component_repository;
92 $this->testGUI = $testGUI;
93 $this->testOBJ = $testGUI->getObject();
94 $this->tpl = $main_template;
95 $this->tabs = $tabs;
96 $this->active_user = $active_user;
97
98 $this->testQuestionSetConfigFactory = new ilTestQuestionSetConfigFactory(
99 $this->tree,
100 $this->db,
101 $this->component_repository,
102 $this->testOBJ
103 );
104
105 $templateId = $this->testOBJ->getTemplate();
106
107 if ($templateId) {
108 $this->settingsTemplate = new ilSettingsTemplate(
109 (int) $templateId,
111 );
112 }
113
114 $this->score_settings_repo = $score_settings_repo;
115 $this->test_id = $test_id;
116 $this->ui_factory = $ui_factory;
117 $this->ui_renderer = $ui_renderer;
118 $this->refinery = $refinery;
119 $this->request = $request;
120 }
121
123 {
124 return $this->score_settings_repo->getFor($this->test_id);
125 }
126 protected function storeScoreSettings(ilObjTestScoreSettings $score_settings): void
127 {
128 $this->score_settings_repo->store($score_settings);
129 }
130
134 public function executeCommand()
135 {
136 if (!$this->access->checkAccess('write', '', $this->testGUI->getRefId())) {
137 $this->tpl->setOnScreenMessage('info', $this->lng->txt('cannot_edit_test'), true);
138 $this->ctrl->redirect($this->testGUI, 'infoScreen');
139 }
140
141 $this->tabs->activateTab(ilTestTabsManager::TAB_ID_SETTINGS);
142
143 $nextClass = $this->ctrl->getNextClass();
144 switch ($nextClass) {
145 default:
146 $cmd = $this->ctrl->getCmd(self::CMD_SHOW_FORM);
147
148 switch ($cmd) {
150 $this->showForm();
151 break;
153 $this->saveForm();
154 break;
156 $this->saveForm();
157 $settings = $this->buildForm()
158 ->withRequest($this->getRelayedRequest())
159 ->getData();
161 $this->testOBJ->recalculateScores(true);
162 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_score_settings_modified_and_recalc"), true);
163 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
164 break;
166 $this->tpl->setOnScreenMessage('info', $this->lng->txt("msg_score_settings_not_modified"), true);
167 $form = $this->buildForm()->withRequest($this->getRelayedRequest());
168 $this->showForm($form);
169 break;
170 default:
171 throw new Exception('unknown command: ' . $cmd);
172 }
173 }
174 }
175
176 private function showForm(Form $form = null): void
177 {
178 if ($form === null) {
179 $form = $this->buildForm();
180 }
181
182 $this->tpl->setContent($this->ui_renderer->render($form));
183 }
184
185 private function saveForm(): void
186 {
187 $form = $this->buildForm()
188 ->withRequest($this->request);
189
190 $settings = $form->getData();
191
192 if (is_null($settings)) {
193 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
194 $this->showForm($form);
195 return;
196 }
197
199 $settings->getScoringSettings(),
200 $this->loadScoreSettings()->getScoringSettings()
201 )) {
202 $this->showConfirmation($this->request);
203 return;
204 }
205
207 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
208 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
209 }
210
211 private function getRelayedRequest(): Request
212 {
213 return unserialize(
214 base64_decode(
215 $this->request->getParsedBody()[self::F_CONFIRM_SETTINGS]
216 ),
217 [
218 'allowed_classes' => [
219 GuzzleHttp\Psr7\ServerRequest::class,
220 GuzzleHttp\Psr7\Uri::class,
221 GuzzleHttp\Psr7\UploadedFile::class,
222 GuzzleHttp\Psr7\Stream::class,
223 ]
224 ]
225 );
226 }
227
228 private function buildForm(): Form
229 {
230 $ui_pack = [
232 $this->ui_factory->input()->field(),
234 ];
235
236 $environment = [];
237 $df = (new \ILIAS\Data\Factory())->dateFormat();
238 switch ($this->active_user->getDateFormat()) {
240 $date_format = $df->germanShort();
241 break;
243 //americanShort
244 $date_format = $df->custom()->month()->slash()->day()->slash()->year()->get();
245 break;
247 default:
248 $date_format = $df->standard();
249 }
250 $environment['user_date_format'] = $date_format;
251 $environment['user_time_zone'] = $this->active_user->getTimeZone();
252
253 $disabled_flag = ($this->areScoringSettingsWritable() === false);
254
255 $settings = $this->loadScoreSettings();
256 $sections = [
257 'scoring' => $settings->getScoringSettings()->toForm(...$ui_pack)
258 ->withDisabled($disabled_flag),
259 'summary' => $settings->getResultSummarySettings()->toForm(...array_merge($ui_pack, [$environment])),
260 'details' => $settings->getResultDetailsSettings()->toForm(
261 ...array_merge($ui_pack, [['taxonomy_options' => $this->getTaxonomyOptions()]])
262 ),
263 'gameification' => $settings->getGamificationSettings()->toForm(...$ui_pack)
264 ];
265
266 $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_FORM);
267 $form = $this->ui_factory->input()->container()->form()
268 ->standard($action, $sections)
269 ->withAdditionalTransformation(
270 $this->refinery->custom()->transformation(
271 function ($v) use ($settings) {
272 return $settings
273 ->withScoringSettings($v['scoring'])
274 ->withResultSummarySettings($v['summary'])
275 ->withResultDetailsSettings($v['details'])
276 ->withGamificationSettings($v['gameification'])
277 ;
278 }
279 )
280 );
281 return $form;
282 }
283
284 private function isScoreReportingAvailable(): bool
285 {
286 if (!$this->testOBJ->getScoreReporting()) {
287 return false;
288 }
289
290 if ($this->testOBJ->getScoreReporting() == ilObjTest::SCORE_REPORTING_DATE) {
292 $reporting_date = $this->testOBJ->getScoreSettings()->getResultSummarySettings()->getReportingDate();
293 return $reporting_date <= new DateTimeImmutable('now', new DateTimeZone('UTC'));
294 }
295
296 return true;
297 }
298
299 private function areScoringSettingsWritable(): bool
300 {
301 if (!$this->testOBJ->participantDataExist()) {
302 return true;
303 }
304
305 if (!$this->isScoreReportingAvailable()) {
306 return true;
307 }
308
309 return false;
310 }
311
312 protected function getTaxonomyOptions(): array
313 {
314 $available_taxonomy_ids = ilObjTaxonomy::getUsageOfObject($this->testOBJ->getId());
315 $taxononmy_translator = new ilTestTaxonomyFilterLabelTranslater($this->db);
316 $taxononmy_translator->loadLabelsFromTaxonomyIds($available_taxonomy_ids);
317
318 $taxonomy_options = [];
319 foreach ($available_taxonomy_ids as $tax_id) {
320 $taxonomy_options[$tax_id] = $taxononmy_translator->getTaxonomyTreeLabel($tax_id);
321 }
322 return $taxonomy_options;
323 }
324
326 ilObjTestSettingsScoring $new_settings,
327 ilObjTestSettingsScoring $old_settings
328 ): bool {
329 $settings_changed = (
330 $new_settings->getCountSystem() !== $old_settings->getCountSystem() ||
331 $new_settings->getScoreCutting() !== $old_settings->getScoreCutting() ||
332 $new_settings->getPassScoring() !== $old_settings->getPassScoring()
333 );
334
335 return
336 $this->testOBJ->participantDataExist() &&
338 $settings_changed;
339 }
340
341
342 private function showConfirmation(Request $request)
343 {
344 $confirmation = new ilConfirmationGUI();
345 $confirmation->setHeaderText($this->lng->txt('tst_trigger_result_refreshing'));
346 $confirmation->setFormAction($this->ctrl->getFormAction($this));
347 $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_RECALC);
348 $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_CONFIRMED_RECALC);
349 $confirmation->addHiddenItem(self::F_CONFIRM_SETTINGS, base64_encode(serialize($request)));
350 $this->tpl->setContent($this->ctrl->getHTML($confirmation));
351 }
352}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
Builds data types.
Definition: Factory.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
Class ilObjTestGUI.
storeScoreSettings(ilObjTestScoreSettings $score_settings)
isScoreRecalculationRequired(ilObjTestSettingsScoring $new_settings, ilObjTestSettingsScoring $old_settings)
__construct(ilCtrlInterface $ctrl, ilAccessHandler $access, ilLanguage $lng, ilTree $tree, ilDBInterface $db, ilComponentRepository $component_repository, ilObjTestGUI $testGUI, \ilGlobalTemplateInterface $main_template, ilTabsGUI $tabs, ScoreSettingsRepository $score_settings_repo, int $test_id, UIFactory $ui_factory, UIRenderer $ui_renderer, Refinery $refinery, Request $request, ilObjUser $active_user)
const SCORE_REPORTING_DATE
User class.
Settings template application class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes commonalities between all forms.
Definition: Form.php:33
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200