ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 
49  public function __construct(
50  protected ilCtrlInterface $ctrl,
51  protected ilAccessHandler $access,
52  protected ilLanguage $lng,
53  protected ilTree $tree,
54  protected ilDBInterface $db,
55  protected ilComponentRepository $component_repository,
56  protected ilObjTestGUI $test_gui,
57  protected \ilGlobalTemplateInterface $tpl,
58  protected ilTabsGUI $tabs,
59  protected ScoreSettingsRepository $score_settings_repo,
60  protected int $test_id,
61  protected UIFactory $ui_factory,
62  protected UIRenderer $ui_renderer,
63  protected Refinery $refinery,
64  protected Request $request,
65  protected ilObjUser $active_user
66  ) {
67  parent::__construct($test_gui->getObject());
68 
69  $template_id = $this->test_object->getTemplate();
70 
71  if ($template_id) {
72  $this->settingsTemplate = new ilSettingsTemplate(
73  (int) $template_id,
74  ilObjAssessmentFolderGUI::getSettingsTemplateConfig()
75  );
76  }
77  }
78 
80  {
81  return $this->score_settings_repo->getFor($this->test_id);
82  }
83  protected function storeScoreSettings(ilObjTestScoreSettings $score_settings): void
84  {
85  $this->score_settings_repo->store($score_settings);
86  }
87 
91  public function executeCommand()
92  {
93  if (!$this->access->checkAccess('write', '', $this->test_gui->getRefId())) {
94  $this->tpl->setOnScreenMessage('info', $this->lng->txt('cannot_edit_test'), true);
95  $this->ctrl->redirect($this->test_gui, 'infoScreen');
96  }
97 
98  $this->tabs->activateTab(ilTestTabsManager::TAB_ID_SETTINGS);
99 
100  $nextClass = $this->ctrl->getNextClass();
101  switch ($nextClass) {
102  default:
103  $cmd = $this->ctrl->getCmd(self::CMD_SHOW_FORM);
104 
105  switch ($cmd) {
106  case self::CMD_SHOW_FORM:
107  $this->showForm();
108  break;
109  case self::CMD_SAVE_FORM:
110  $this->saveForm();
111  break;
112  case self::CMD_CONFIRMED_RECALC:
113  $this->saveForm();
114  $settings = $this->buildForm()
115  ->withRequest($this->getRelayedRequest())
116  ->getData();
118  $this->test_object->recalculateScores(true);
119  $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_score_settings_modified_and_recalc"), true);
120  $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
121  break;
122  case self::CMD_CANCEL_RECALC:
123  $this->tpl->setOnScreenMessage('info', $this->lng->txt("msg_score_settings_not_modified"), true);
124  $form = $this->buildForm()->withRequest($this->getRelayedRequest());
125  $this->showForm($form);
126  break;
127  default:
128  throw new Exception('unknown command: ' . $cmd);
129  }
130  }
131  }
132 
133  private function showForm(Form $form = null): void
134  {
135  if ($form === null) {
136  $form = $this->buildForm();
137  }
138 
139  $this->tpl->setContent($this->ui_renderer->render($form));
140  }
141 
142  private function saveForm(): void
143  {
144  $form = $this->buildForm()
145  ->withRequest($this->request);
146 
147  $settings = $form->getData();
148 
149  if (is_null($settings)) {
150  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
151  $this->showForm($form);
152  return;
153  }
154 
155  if ($this->isScoreRecalculationRequired(
156  $settings->getScoringSettings(),
157  $this->loadScoreSettings()->getScoringSettings()
158  )) {
159  $this->showConfirmation($this->request);
160  return;
161  }
162 
164  $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
165  $this->ctrl->redirect($this, self::CMD_SHOW_FORM);
166  }
167 
168  private function getRelayedRequest(): Request
169  {
170  return unserialize(
171  base64_decode(
172  $this->request->getParsedBody()[self::F_CONFIRM_SETTINGS]
173  )
174  );
175  }
176 
177  private function buildForm(): Form
178  {
179  $ui_pack = [
180  $this->lng,
181  $this->ui_factory->input()->field(),
183  ];
184 
185 
186  $environment = [];
187  $environment['user_date_format'] = (new \ILIAS\Data\Factory())->dateFormat()->withTime24(
188  $this->active_user->getDateFormat()
189  );
190  $environment['user_time_zone'] = $this->active_user->getTimeZone();
191 
192  $anonymity_flag = (bool) $this->test_object->getAnonymity();
193  $disabled_flag = ($this->areScoringSettingsWritable() === false);
194 
195  $settings = $this->loadScoreSettings();
196  $sections = [
197  'scoring' => $settings->getScoringSettings()->toForm(...$ui_pack)
198  ->withDisabled($disabled_flag),
199  'summary' => $settings->getResultSummarySettings()->toForm(...array_merge($ui_pack, [$environment])),
200  'details' => $settings->getResultDetailsSettings()->toForm(
201  ...array_merge($ui_pack, [['taxonomy_options' => $this->getTaxonomyOptions()]])
202  ),
203  'gameification' => $settings->getGamificationSettings()->toForm(...$ui_pack)
204  ];
205 
206  $action = $this->ctrl->getFormAction($this, self::CMD_SAVE_FORM);
207  $form = $this->ui_factory->input()->container()->form()
208  ->standard($action, $sections)
209  ->withAdditionalTransformation(
210  $this->refinery->custom()->transformation(
211  function ($v) use ($settings) {
212  return $settings
213  ->withScoringSettings($v['scoring'])
214  ->withResultSummarySettings($v['summary'])
215  ->withResultDetailsSettings($v['details'])
216  ->withGamificationSettings($v['gameification'])
217  ;
218  }
219  )
220  );
221  return $form;
222  }
223 
224  private function isScoreReportingAvailable(): bool
225  {
226  if (!$this->test_object->getScoreReporting()) {
227  return false;
228  }
229 
230  if ($this->test_object->getScoreReporting() == ilObjTestSettingsResultSummary::SCORE_REPORTING_DATE) {
231  $reporting_date = $this->test_object->getScoreSettings()->getResultSummarySettings()->getReportingDate();
232  return $reporting_date <= new DateTimeImmutable('now', new DateTimeZone('UTC'));
233  }
234 
235  return true;
236  }
237 
238  private function areScoringSettingsWritable(): bool
239  {
240  if (!$this->test_object->participantDataExist()) {
241  return true;
242  }
243 
244  if (!$this->isScoreReportingAvailable()) {
245  return true;
246  }
247 
248  return false;
249  }
250 
251  protected function getTaxonomyOptions(): array
252  {
253  $available_taxonomy_ids = ilObjTaxonomy::getUsageOfObject($this->test_object->getId());
254  $taxononmy_translator = new ilTestQuestionFilterLabelTranslater($this->db, $this->lng);
255  $taxononmy_translator->loadLabelsFromTaxonomyIds($available_taxonomy_ids);
256 
257  $taxonomy_options = [];
258  foreach ($available_taxonomy_ids as $tax_id) {
259  $taxonomy_options[$tax_id] = $taxononmy_translator->getTaxonomyTreeLabel($tax_id);
260  }
261  return $taxonomy_options;
262  }
263 
264  protected function isScoreRecalculationRequired(
265  ilObjTestSettingsScoring $new_settings,
266  ilObjTestSettingsScoring $old_settings
267  ): bool {
268  $settings_changed = (
269  $new_settings->getCountSystem() !== $old_settings->getCountSystem() ||
270  $new_settings->getScoreCutting() !== $old_settings->getScoreCutting() ||
271  $new_settings->getPassScoring() !== $old_settings->getPassScoring()
272  );
273 
274  return
275  $this->test_object->participantDataExist() &&
276  $this->areScoringSettingsWritable() &&
277  $settings_changed;
278  }
279 
280 
281  private function showConfirmation(Request $request)
282  {
283  $confirmation = new ilConfirmationGUI();
284  $confirmation->setHeaderText($this->lng->txt('tst_trigger_result_refreshing'));
285  $confirmation->setFormAction($this->ctrl->getFormAction($this));
286  $confirmation->setCancel($this->lng->txt('cancel'), self::CMD_CANCEL_RECALC);
287  $confirmation->setConfirm($this->lng->txt('confirm'), self::CMD_CONFIRMED_RECALC);
288  $confirmation->addHiddenItem(self::F_CONFIRM_SETTINGS, base64_encode(serialize($request)));
289  $this->tpl->setContent($this->ctrl->getHTML($confirmation));
290  }
291 }
Readable part of repository interface to ilComponentDataDB.
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
This describes commonalities between all forms.
Definition: Form.php:32
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)
__construct(VocabulariesInterface $vocabularies)
$lng
storeScoreSettings(ilObjTestScoreSettings $score_settings)
__construct(protected ilCtrlInterface $ctrl, protected ilAccessHandler $access, protected ilLanguage $lng, protected ilTree $tree, protected ilDBInterface $db, protected ilComponentRepository $component_repository, protected ilObjTestGUI $test_gui, protected \ilGlobalTemplateInterface $tpl, protected ilTabsGUI $tabs, protected ScoreSettingsRepository $score_settings_repo, protected int $test_id, protected UIFactory $ui_factory, protected UIRenderer $ui_renderer, protected Refinery $refinery, protected Request $request, protected ilObjUser $active_user)
isScoreRecalculationRequired(ilObjTestSettingsScoring $new_settings, ilObjTestSettingsScoring $old_settings)
Refinery Factory $refinery