ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 $settings = $this->buildForm()
116 ->withRequest($this->getRelayedRequest())
117 ->getData();
118 $this->storeScoreSettings($settings);
119 $this->test_object->recalculateScores(true);
120 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_score_settings_modified_and_recalc"), true);
121 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
122 break;
124 $this->tpl->setOnScreenMessage('info', $this->lng->txt("msg_score_settings_not_modified"), true);
125 $form = $this->buildForm()->withRequest($this->getRelayedRequest());
126 $this->showForm($form);
127 break;
128 default:
129 throw new \Exception('unknown command: ' . $cmd);
130 }
131 }
132 }
133
134 private function showForm(?Form $form = null): void
135 {
136 if ($form === null) {
137 $form = $this->buildForm();
138 }
139
140 $this->tpl->setContent($this->ui_renderer->render($form));
141 }
142
143 private function saveForm(): void
144 {
145 $form = $this->buildForm()
146 ->withRequest($this->request);
147
148 $settings = $form->getData();
149
150 if (is_null($settings)) {
151 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
152 $this->showForm($form);
153 return;
154 }
155
157 $settings->getScoringSettings(),
158 $this->loadScoreSettings()->getScoringSettings()
159 )) {
160 $this->showConfirmation($this->request);
161 return;
162 }
163
164 $this->storeScoreSettings($settings);
165 if ($this->logger->isLoggingEnabled()) {
166 $this->logger->logTestAdministrationInteraction(
167 $this->logger->getInteractionFactory()->buildTestAdministrationInteraction(
168 $this->test_gui->getRefId(),
169 $this->active_user->getId(),
170 TestAdministrationInteractionTypes::SCORING_SETTINGS_MODIFIED,
171 $settings->getArrayForLog($this->logger->getAdditionalInformationGenerator())
172 )
173 );
174 }
175 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
176 $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
177 }
178
179 private function getRelayedRequest(): Request
180 {
181 return unserialize(
182 base64_decode(
183 $this->request->getParsedBody()[self::F_CONFIRM_SETTINGS]
184 ),
185 [
186 'allowed_classes' => [
187 \GuzzleHttp\Psr7\ServerRequest::class,
188 \GuzzleHttp\Psr7\Uri::class,
189 \GuzzleHttp\Psr7\UploadedFile::class,
190 \GuzzleHttp\Psr7\Stream::class,
191 ]
192 ]
193 );
194 }
195
196 private function buildForm(): Form
197 {
198 $ui_pack = [
200 $this->ui_factory->input()->field(),
202 ];
203
204 $environment = [
205 'user_date_format' => $this->active_user->getDateTimeFormat(),
206 'user_time_zone' => $this->active_user->getTimeZone()
207 ];
208
209 $disabled_flag = ($this->areScoringSettingsWritable() === false);
210
211 $settings = $this->loadScoreSettings();
212 $sections = [
213 'scoring' => $settings->getScoringSettings()->toForm(...$ui_pack)
214 ->withDisabled($disabled_flag),
215 'summary' => $settings->getResultSummarySettings()->toForm(...array_merge($ui_pack, [$environment])),
216 'details' => $settings->getResultDetailsSettings()->toForm(
217 ...array_merge($ui_pack, [['taxonomy_options' => $this->getTaxonomyOptions()]])
218 ),
219 'gameification' => $settings->getGamificationSettings()->toForm(...$ui_pack)
220 ];
221
222 $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_FORM);
223 $form = $this->ui_factory->input()->container()->form()
224 ->standard($action, $sections)
225 ->withAdditionalTransformation(
226 $this->refinery->custom()->transformation(
227 function ($v) use ($settings) {
228 return $settings
229 ->withScoringSettings($v['scoring'])
230 ->withResultSummarySettings($v['summary'])
231 ->withResultDetailsSettings($v['details'])
232 ->withGamificationSettings($v['gameification'])
233 ;
234 }
235 )
236 );
237 return $form;
238 }
239
240 private function isScoreReportingAvailable(): bool
241 {
242 $result_summary_settings = $this->test_object->getScoreSettings()
243 ->getResultSummarySettings();
244 if (!$result_summary_settings->getScoreReporting()->isReportingEnabled()) {
245 return false;
246 }
247
248 if ($result_summary_settings->getScoreReporting() === ScoreReportingTypes::SCORE_REPORTING_DATE) {
249 return $result_summary_settings->getReportingDate()
250 <= new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
251 }
252
253 return true;
254 }
255
256 private function areScoringSettingsWritable(): bool
257 {
258 if (!$this->test_object->participantDataExist()) {
259 return true;
260 }
261
262 if (!$this->isScoreReportingAvailable()) {
263 return true;
264 }
265
266 return false;
267 }
268
269 protected function getTaxonomyOptions(): array
270 {
271 $available_taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->test_object->getId());
272 $taxononmy_translator = new \ilTestQuestionFilterLabelTranslator($this->db, $this->lng);
273 $taxononmy_translator->loadLabelsFromTaxonomyIds($available_taxonomy_ids);
274
275 $taxonomy_options = [];
276 foreach ($available_taxonomy_ids as $tax_id) {
277 $taxonomy_options[$tax_id] = $taxononmy_translator->getTaxonomyTreeLabel($tax_id);
278 }
279 return $taxonomy_options;
280 }
281
283 SettingsScoring $new_settings,
284 SettingsScoring $old_settings
285 ): bool {
286 $settings_changed = (
287 $new_settings->getCountSystem() !== $old_settings->getCountSystem() ||
288 $new_settings->getScoreCutting() !== $old_settings->getScoreCutting() ||
289 $new_settings->getPassScoring() !== $old_settings->getPassScoring()
290 );
291
292 return
293 $this->test_object->participantDataExist() &&
295 $settings_changed;
296 }
297
298
299 private function showConfirmation(Request $request)
300 {
301 $confirmation = new \ilConfirmationGUI();
302 $confirmation->setHeaderText($this->lng->txt('tst_trigger_result_refreshing'));
303 $confirmation->setFormAction($this->ctrl->getFormAction($this));
304 $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_RECALC);
305 $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_CONFIRMED_RECALC);
306 $confirmation->addHiddenItem(self::F_CONFIRM_SETTINGS, base64_encode(serialize($request)));
307 $this->tpl->setContent($this->ctrl->getHTML($confirmation));
308 }
309}
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:34
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