ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjTestSettingsScoringResultsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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;
63  protected Refinery $refinery;
64  protected ilTabsGUI $tabs;
66 
67 
68  public function __construct(
69  ilCtrlInterface $ctrl,
70  ilAccessHandler $access,
71  ilLanguage $lng,
72  ilTree $tree,
73  ilDBInterface $db,
74  ilComponentRepository $component_repository,
75  ilObjTestGUI $testGUI,
76  \ilGlobalTemplateInterface $main_template,
77  ilTabsGUI $tabs,
78  ScoreSettingsRepository $score_settings_repo,
79  int $test_id,
80  UIFactory $ui_factory,
81  UIRenderer $ui_renderer,
82  Refinery $refinery,
83  Request $request,
84  ilObjUser $active_user
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) {
149  case self::CMD_SHOW_FORM:
150  $this->showForm();
151  break;
152  case self::CMD_SAVE_FORM:
153  $this->saveForm();
154  break;
155  case self::CMD_CONFIRMED_RECALC:
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;
165  case self::CMD_CANCEL_RECALC:
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 
198  if ($this->isScoreRecalculationRequired(
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  }
219 
220  private function buildForm(): Form
221  {
222  $ui_pack = [
223  $this->lng,
224  $this->ui_factory->input()->field(),
226  ];
227 
228  $environment = [];
229  $df = (new \ILIAS\Data\Factory())->dateFormat();
230  switch ($this->active_user->getDateFormat()) {
232  $date_format = $df->germanShort();
233  break;
235  //americanShort
236  $date_format = $df->custom()->month()->slash()->day()->slash()->year()->get();
237  break;
239  default:
240  $date_format = $df->standard();
241  }
242  $environment['user_date_format'] = $date_format;
243  $environment['user_time_zone'] = $this->active_user->getTimeZone();
244 
245  $disabled_flag = ($this->areScoringSettingsWritable() === false);
246 
247  $settings = $this->loadScoreSettings();
248  $sections = [
249  'scoring' => $settings->getScoringSettings()->toForm(...$ui_pack)
250  ->withDisabled($disabled_flag),
251  'summary' => $settings->getResultSummarySettings()->toForm(...array_merge($ui_pack, [$environment])),
252  'details' => $settings->getResultDetailsSettings()->toForm(
253  ...array_merge($ui_pack, [['taxonomy_options' => $this->getTaxonomyOptions()]])
254  ),
255  'gameification' => $settings->getGamificationSettings()->toForm(...$ui_pack)
256  ];
257 
258  $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_FORM);
259  $form = $this->ui_factory->input()->container()->form()
260  ->standard($action, $sections)
261  ->withAdditionalTransformation(
262  $this->refinery->custom()->transformation(
263  function ($v) use ($settings) {
264  return $settings
265  ->withScoringSettings($v['scoring'])
266  ->withResultSummarySettings($v['summary'])
267  ->withResultDetailsSettings($v['details'])
268  ->withGamificationSettings($v['gameification'])
269  ;
270  }
271  )
272  );
273  return $form;
274  }
275 
276  private function isScoreReportingAvailable(): bool
277  {
278  if (!$this->testOBJ->getScoreReporting()) {
279  return false;
280  }
281 
282  if ($this->testOBJ->getScoreReporting() == ilObjTest::SCORE_REPORTING_DATE) {
284  $reporting_date = $this->testOBJ->getScoreSettings()->getResultSummarySettings()->getReportingDate();
285  return $reporting_date <= new DateTimeImmutable('now', new DateTimeZone('UTC'));
286  }
287 
288  return true;
289  }
290 
291  private function areScoringSettingsWritable(): bool
292  {
293  if (!$this->testOBJ->participantDataExist()) {
294  return true;
295  }
296 
297  if (!$this->isScoreReportingAvailable()) {
298  return true;
299  }
300 
301  return false;
302  }
303 
304  protected function getTaxonomyOptions(): array
305  {
306  $available_taxonomy_ids = ilObjTaxonomy::getUsageOfObject($this->testOBJ->getId());
307  $taxononmy_translator = new ilTestTaxonomyFilterLabelTranslater($this->db);
308  $taxononmy_translator->loadLabelsFromTaxonomyIds($available_taxonomy_ids);
309 
310  $taxonomy_options = [];
311  foreach ($available_taxonomy_ids as $tax_id) {
312  $taxonomy_options[$tax_id] = $taxononmy_translator->getTaxonomyTreeLabel($tax_id);
313  }
314  return $taxonomy_options;
315  }
316 
317  protected function isScoreRecalculationRequired(
318  ilObjTestSettingsScoring $new_settings,
319  ilObjTestSettingsScoring $old_settings
320  ): bool {
321  $settings_changed = (
322  $new_settings->getCountSystem() !== $old_settings->getCountSystem() ||
323  $new_settings->getScoreCutting() !== $old_settings->getScoreCutting() ||
324  $new_settings->getPassScoring() !== $old_settings->getPassScoring()
325  );
326 
327  return
328  $this->testOBJ->participantDataExist() &&
329  $this->areScoringSettingsWritable() &&
330  $settings_changed;
331  }
332 
333 
334  private function showConfirmation(Request $request)
335  {
336  $confirmation = new ilConfirmationGUI();
337  $confirmation->setHeaderText($this->lng->txt('tst_trigger_result_refreshing'));
338  $confirmation->setFormAction($this->ctrl->getFormAction($this));
339  $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_RECALC);
340  $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_CONFIRMED_RECALC);
341  $confirmation->addHiddenItem(self::F_CONFIRM_SETTINGS, base64_encode(serialize($request)));
342  $this->tpl->setContent($this->ctrl->getHTML($confirmation));
343  }
344 }
__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)
Readable part of repository interface to ilComponentDataDB.
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
Class ilObjTestGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const SCORE_REPORTING_DATE
storeScoreSettings(ilObjTestScoreSettings $score_settings)
isScoreRecalculationRequired(ilObjTestSettingsScoring $new_settings, ilObjTestSettingsScoring $old_settings)
Settings template application class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...