ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMarkSchemaGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
35 {
37  private Request $request;
39 
40  protected $object;
41  protected ilLanguage $lng;
42  protected ilCtrl $ctrl;
45  protected ilTabsGUI $tabs;
48 
49  public function __construct($object)
50  {
52  global $DIC;
53 
54  $this->ctrl = $DIC['ilCtrl'];
55  $this->lng = $DIC['lng'];
56  $this->tpl = $DIC['tpl'];
57  $this->toolbar = $DIC['ilToolbar'];
58  $this->tabs = $DIC['ilTabs'];
59  $this->object = $object;
60  $this->post_wrapper = $DIC->http()->wrapper()->post();
61  $this->request = $DIC->http()->request();
62  $this->refinery = $DIC->refinery();
63  $this->ui_factory = $DIC['ui.factory'];
64  $this->ui_renderer = $DIC['ui.renderer'];
65  }
66 
67  public function executeCommand(): void
68  {
69  $this->tabs->activateTab(ilTestTabsManager::TAB_ID_SETTINGS);
70  $cmd = $this->ctrl->getCmd('showMarkSchema');
71  $this->$cmd();
72  }
73 
74  protected function ensureMarkSchemaCanBeEdited(): void
75  {
76  if (!$this->object->canEditMarks()) {
77  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
78  $this->ctrl->redirect($this, 'showMarkSchema');
79  }
80  }
81 
82  protected function addMarkStep(): void
83  {
85 
87  $this->object->getMarkSchema()->addMarkStep();
88  $this->showMarkSchema();
89  }
90 
91  protected function populateMarkSchemaFormData(): bool
92  {
93  $no_save_error = true;
94  $this->object->getMarkSchema()->flush();
95  $postdata = $this->request->getParsedBody();
96  foreach ($postdata as $key => $value) {
97  if (preg_match('/mark_short_(\d+)/', $key, $matches)) {
98  $passed = "0";
99  if (isset($postdata["passed_$matches[1]"])) {
100  $passed = "1";
101  }
102 
103  $percentage = str_replace(',', '.', ilUtil::stripSlashes($postdata["mark_percentage_$matches[1]"]));
104  if (!is_numeric($percentage)
105  || (float) $percentage < 0.0
106  || (float) $percentage > 100.0) {
107  $percentage = 0;
108  $no_save_error = false;
109  }
110 
111  $this->object->getMarkSchema()->addMarkStep(
112  ilUtil::stripSlashes($postdata["mark_short_$matches[1]"]),
113  ilUtil::stripSlashes($postdata["mark_official_$matches[1]"]),
114  (float) $percentage,
115  (int) ilUtil::stripSlashes($passed)
116  );
117  }
118  }
119 
120  return $no_save_error;
121  }
122 
123  protected function resetToSimpleMarkSchema(): void
124  {
126 
127  $this->object->getMarkSchema()->createSimpleSchema(
128  $this->lng->txt('failed_short'),
129  $this->lng->txt('failed_official'),
130  0,
131  0,
132  $this->lng->txt('passed_short'),
133  $this->lng->txt('passed_official'),
134  50,
135  1
136  );
137  $this->object->getMarkSchema()->saveToDb($this->object->getTestId());
138  $this->showMarkSchema();
139  }
140 
141  protected function deleteMarkSteps(): void
142  {
143  $marks_trafo = $this->refinery->custom()->transformation(
144  function ($vs): ?array {
145  if ($vs === null || !is_array($vs)) {
146  return null;
147  }
148  return $vs;
149  }
150  );
151  $deleted_mark_steps = null;
152  if ($this->post_wrapper->has('marks')) {
153  $deleted_mark_steps = $this->post_wrapper->retrieve(
154  'marks',
155  $marks_trafo
156  );
157  }
158 
160  if (!isset($deleted_mark_steps) || !is_array($deleted_mark_steps)) {
161  $this->showMarkSchema();
162  return;
163  }
164 
165  // test delete
166  $schema = clone $this->object->getMarkSchema();
167  $schema->deleteMarkSteps($deleted_mark_steps);
168  $check_result = $schema->checkMarks();
169  if (is_string($check_result)) {
170  $this->tpl->setOnScreenMessage('failure', $this->lng->txt($check_result), true);
171  $this->showMarkSchema();
172  return;
173  }
174 
175  // actual delete
176  if (!empty($deleted_mark_steps)) {
177  $this->object->getMarkSchema()->deleteMarkSteps($deleted_mark_steps);
178  } else {
179  $this->tpl->setOnScreenMessage('info', $this->lng->txt('tst_delete_missing_mark'));
180  }
181  $this->object->getMarkSchema()->saveToDb($this->object->getTestId());
182 
183  $this->showMarkSchema();
184  }
185 
186  protected function saveMarks(): void
187  {
189 
190  if ($this->populateMarkSchemaFormData()) {
191  $result = $this->object->checkMarks();
192  } else {
193  $result = 'mark_schema_invalid';
194  }
195 
196  if (is_string($result)) {
197  $this->tpl->setOnScreenMessage('failure', $this->lng->txt($result), true);
198  } else {
199  $this->object->getMarkSchema()->saveToDb($this->object->getMarkSchemaForeignId());
200  $this->object->onMarkSchemaSaved();
201  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
202  $this->object->getMarkSchema()->flush();
203  $this->object->getMarkSchema()->loadFromDb($this->object->getTestId());
204  }
205 
206  $this->showMarkSchema();
207  }
208 
209  protected function showMarkSchema(): void
210  {
211  if (!$this->object->canEditMarks()) {
212  $this->tpl->setOnScreenMessage('info', $this->lng->txt('cannot_edit_marks'));
213  }
214 
215  $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'showMarkSchema'));
216 
217  $mark_schema_table = new ilMarkSchemaTableGUI($this, 'showMarkSchema', $this->object);
218  $mark_schema_table->setShowRowsSelector(false);
219 
220  $rendered_modal = '';
221  if ($this->object->canEditMarks()) {
222  $confirmation_modal = $this->ui_factory->modal()->interruptive(
223  $this->lng->txt('tst_mark_reset_to_simple_mark_schema'),
224  $this->lng->txt('tst_mark_reset_to_simple_mark_schema_confirmation'),
225  $this->ctrl->getFormAction($this, 'resetToSimpleMarkSchema')
226  )->withActionButtonLabel($this->lng->txt('tst_mark_reset_to_simple_mark_schema'));
227  $this->populateToolbar($confirmation_modal, $mark_schema_table->getId());
228  $rendered_modal = $this->ui_renderer->render($confirmation_modal);
229  }
230 
231  $this->tpl->setContent(
232  $mark_schema_table->getHTML() . $rendered_modal
233  );
234  }
235 
236  private function populateToolbar(InterruptiveModal $confirmation_modal, string $mark_schema_id): void
237  {
238  $create_simple_schema_button = $this->ui_factory->button()->standard(
239  $this->lng->txt('tst_mark_reset_to_simple_mark_schema'),
240  $confirmation_modal->getShowSignal()
241  );
242  $this->toolbar->addComponent($create_simple_schema_button);
243 
244  $create_step_button = $this->buildCreateStepButton($mark_schema_id);
245  $this->toolbar->addComponent($create_step_button);
246  }
247 
248  private function buildCreateStepButton(string $mark_schema_id): StandardButton
249  {
250  return $this->ui_factory->button()->standard(
251  $this->lng->txt('tst_mark_create_new_mark_step'),
252  ''
254  fn(string $id): string =>
255  "{$id}.addEventListener('click', "
256  . ' (e) => {'
257  . ' e.preventDefault();'
258  . ' e.target.name = "cmd[addMarkStep]";'
259  . " let form = document.getElementById('form_{$mark_schema_id}');"
260  . ' let submitter = e.target.cloneNode();'
261  . ' submitter.style.visibility = "hidden";'
262  . ' form.appendChild(submitter);'
263  . ' form.requestSubmit(submitter);'
264  . ' }'
265  . ');'
266  );
267  }
268 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilGlobalPageTemplate $tpl
populateToolbar(InterruptiveModal $confirmation_modal, string $mark_schema_id)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
RequestWrapper $post_wrapper
Interface RequestWrapper.
string $key
Consumer key/client ID value.
Definition: System.php:193
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
buildCreateStepButton(string $mark_schema_id)