ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilUnitConfigurationGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
24 abstract class ilUnitConfigurationGUI
25 {
27  protected \ILIAS\TestQuestionPool\InternalRequestService $request;
28  protected ?ilPropertyFormGUI $unit_cat_form = null;
29  protected ?ilPropertyFormGUI $unit_form = null;
31  protected ilLanguage $lng;
33 
34  public function __construct(ilUnitConfigurationRepository $repository)
35  {
36  global $DIC;
37 
38  $lng = $DIC->language();
39  $ilCtrl = $DIC->ctrl();
40  $tpl = $DIC->ui()->mainTemplate();
41  $this->request = $DIC->testQuestionPool()->internal()->request();
42 
43  $this->repository = $repository;
44  $this->lng = $lng;
45  $this->ctrl = $ilCtrl;
46  $this->tpl = $tpl;
47 
48  $this->lng->loadLanguageModule('assessment');
49  }
50 
51  abstract protected function getDefaultCommand(): string;
52 
53  abstract public function getUnitCategoryOverviewCommand(): string;
54 
55  abstract public function isCRUDContext(): bool;
56 
57  abstract public function getUniqueId(): string;
58 
59  abstract protected function showUnitCategories(array $categories): void;
60 
61  protected function getCategoryById(int $id, bool $for_CRUD = true): assFormulaQuestionUnitCategory
62  {
63  $category = $this->repository->getUnitCategoryById($id);
64  if ($for_CRUD && $category->getQuestionFi() !== $this->repository->getConsumerId()) {
65  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('change_adm_categories_not_allowed'), true);
66  $this->ctrl->redirect($this, $this->getDefaultCommand());
67  }
68 
69  return $category;
70  }
71 
72  protected function handleSubtabs(): void
73  {
74  }
75 
76  protected function checkPermissions(string $cmd): void
77  {
78  }
79 
80  public function executeCommand(): void
81  {
82  $cmd = $this->ctrl->getCmd($this->getDefaultCommand());
83  $this->checkPermissions($cmd);
84  switch ($cmd) {
85  case 'confirmImportGlobalCategories':
86  $category_ids = $this->request->raw('category_ids');
87  $this->$cmd($category_ids);
88  break;
89  default:
90  $this->$cmd();
91  break;
92  }
93 
94  $this->handleSubtabs();
95  }
96 
97  protected function confirmDeleteUnit(): void
98  {
99  if (!$this->request->isset('unit_id')) {
100  $this->showUnitsOfCategory();
101  return;
102  }
103 
104  $this->confirmDeleteUnits([$this->request->int('unit_id')]);
105  }
106 
112  protected function confirmDeleteUnits(array $unit_ids = null): void
113  {
114  if (!$this->isCRUDContext()) {
115  $this->showUnitsOfCategory();
116  return;
117  }
118 
119  $unit_ids = $unit_ids ?? $this->request->getUnitIds();
120  if (count($unit_ids) === 0) {
121  $this->showUnitsOfCategory();
122  return;
123  }
124 
125  $this->ctrl->setParameter($this, 'category_id', $this->request->int('category_id'));
126  $confirmation = new ilConfirmationGUI();
127  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'deleteUnits'));
128  $confirmation->setConfirm($this->lng->txt('confirm'), 'deleteUnits');
129  $confirmation->setCancel($this->lng->txt('cancel'), 'showUnitsOfCategory');
130 
131  $errors = [];
132  $num_to_confirm = 0;
133  foreach ($unit_ids as $unit_id) {
134  try {
135  $unit = $this->repository->getUnit((int) $unit_id);
136  if (!$unit) {
137  continue;
138  }
139 
140  if ($check_result = $this->repository->checkDeleteUnit($unit->getId())) {
141  $errors[] = $unit->getDisplayString() . ' - ' . $check_result;
142  continue;
143  }
144 
145  $confirmation->addItem('unit_ids[]', (string) $unit->getId(), $unit->getDisplayString());
146  ++$num_to_confirm;
147  } catch (ilException $e) {
148  continue;
149  }
150  }
151 
152  if ($errors) {
153  $num_errors = count($errors);
154 
155  $error_message = array_map(static function (string $message): string {
156  return '<li>' . $message . '</li>';
157  }, $errors);
158  if ($num_errors === 1) {
159  $this->tpl->setOnScreenMessage(
160  'failure',
161  $this->lng->txt('un_unit_deletion_errors_f_s') . '<ul>' . implode('', $error_message) . '<ul>'
162  );
163  } else {
164  $this->tpl->setOnScreenMessage(
165  'failure',
166  $this->lng->txt('un_unit_deletion_errors_f') . '<ul>' . implode('', $error_message) . '<ul>'
167  );
168  }
169  }
170 
171  if ($num_to_confirm) {
172  if ($num_to_confirm === 1) {
173  $confirmation->setHeaderText($this->lng->txt('un_sure_delete_units_s'));
174  } else {
175  $confirmation->setHeaderText($this->lng->txt('un_sure_delete_units'));
176  }
177 
178  $this->tpl->setContent($confirmation->getHTML());
179  } else {
180  $this->showUnitsOfCategory();
181  }
182  }
183 
184  public function deleteUnits(): void
185  {
186  if (!$this->isCRUDContext()) {
187  $this->showUnitsOfCategory();
188  return;
189  }
190 
191  $unit_ids = $this->request->getUnitIds();
192  if (count($unit_ids) === 0) {
193  $this->showUnitsOfCategory();
194  return;
195  }
196 
197  $errors = [];
198  $num_deleted = 0;
199  foreach ($unit_ids as $unit_id) {
200  try {
201  $unit = $this->repository->getUnit($unit_id);
202  if (!$unit) {
203  continue;
204  }
205 
206  $check_result = $this->repository->deleteUnit($unit->getId());
207  if (!is_null($check_result)) {
208  $errors[] = $unit->getDisplayString() . ' - ' . $check_result;
209  continue;
210  }
211 
212  ++$num_deleted;
213  } catch (ilException $e) {
214  continue;
215  }
216  }
217 
218  if ($errors) {
219  $num_errors = count($errors);
220 
221  $error_message = array_map(static function (string $message): string {
222  return '<li>' . $message . '</li>';
223  }, $errors);
224  if ($num_errors === 1) {
225  $this->tpl->setOnScreenMessage(
226  'failure',
227  $this->lng->txt('un_unit_deletion_errors_p_s') . '<ul>' . implode('', $error_message) . '<ul>'
228  );
229  } else {
230  $this->tpl->setOnScreenMessage(
231  'failure',
232  $this->lng->txt('un_unit_deletion_errors_p') . '<ul>' . implode('', $error_message) . '<ul>'
233  );
234  }
235  }
236 
237  if ($num_deleted) {
238  if ($num_deleted === 1) {
239  $this->tpl->setOnScreenMessage('success', $this->lng->txt('un_deleted_units_s'));
240  } else {
241  $this->tpl->setOnScreenMessage('success', $this->lng->txt('un_deleted_units'));
242  }
243  }
244 
245  $this->showUnitsOfCategory();
246  }
247 
248  protected function saveOrder(): void
249  {
250  if (!$this->isCRUDContext()) {
251  $this->showUnitsOfCategory();
252  return;
253  }
254 
255  if (!$this->request->isset('sequence') || !is_array($this->request->raw('sequence'))) {
256  $this->showUnitsOfCategory();
257  return;
258  }
259 
260  $sequences = $this->request->raw('sequence');
261  foreach ($sequences as $id => $sequence) {
262  $sorting_value = str_replace(',', '.', $sequence);
263  $sorting_value = (int) $sorting_value * 100;
264  $this->repository->saveUnitOrder((int) $id, $sorting_value);
265  }
266 
267  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
268  $this->showUnitsOfCategory();
269  }
270 
271  protected function saveUnit(): void
272  {
273  if (!$this->isCRUDContext()) {
274  $this->showUnitsOfCategory();
275  return;
276  }
277 
278  $category = $this->getCategoryById($this->request->int('category_id'));
279  $unit = $this->repository->getUnit($this->request->int('unit_id'));
280 
281  if ($this->repository->isUnitInUse($unit->getId())) {
282  $this->showUnitModificationForm();
283  return;
284  }
285 
286  $this->initUnitForm($category, $unit);
287  if ($this->unit_form->checkInput()) {
288  $unit->setUnit($this->unit_form->getInput('unit_title'));
289  $unit->setFactor((float) $this->unit_form->getInput('factor'));
290  $unit->setBaseUnit((int) $this->unit_form->getInput('base_unit') !== $unit->getId() ? (int) $this->unit_form->getInput('base_unit') : 0);
291  $unit->setCategory($category->getId());
292  $this->repository->saveUnit($unit);
293 
294  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
295  $this->showUnitsOfCategory();
296  return;
297  }
298 
299  $this->unit_form->setValuesByPost();
300 
301  $this->tpl->setContent($this->unit_form->getHtml());
302  }
303 
304  protected function showUnitModificationForm(): void
305  {
306  if (!$this->isCRUDContext()) {
307  $this->showUnitsOfCategory();
308  return;
309  }
310 
311  $category = $this->getCategoryById($this->request->int('category_id'));
312  $unit = $this->repository->getUnit($this->request->int('unit_id'));
313 
314  $this->initUnitForm($category, $unit);
315  $this->unit_form->setValuesByArray([
316  'factor' => $unit->getFactor(),
317  'unit_title' => $unit->getUnit(),
318  'base_unit' => $unit->getBaseUnit() !== $unit->getId() ? $unit->getBaseUnit() : 0
319  ]);
320 
321  $this->tpl->setContent($this->unit_form->getHtml());
322  }
323 
324  protected function addUnit(): void
325  {
326  if (!$this->isCRUDContext()) {
327  $this->showUnitsOfCategory();
328  return;
329  }
330 
331  $category = $this->getCategoryById($this->request->int('category_id'));
332 
333  $this->initUnitForm($category);
334  if ($this->unit_form->checkInput()) {
335  $unit = new assFormulaQuestionUnit();
336  $unit->setUnit($this->unit_form->getInput('unit_title'));
337  $unit->setCategory($category->getId());
338 
339  $this->repository->createNewUnit($unit);
340 
341  $unit->setBaseUnit((int) $this->unit_form->getInput('base_unit'));
342  $unit->setFactor((float) $this->unit_form->getInput('factor'));
343 
344  $this->repository->saveUnit($unit);
345 
346  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
347  $this->showUnitsOfCategory();
348  return;
349  }
350 
351  $this->unit_form->setValuesByPost();
352 
353  $this->tpl->setContent($this->unit_form->getHtml());
354  }
355 
356  protected function showUnitCreationForm(): void
357  {
358  if (!$this->isCRUDContext()) {
359  $this->showUnitsOfCategory();
360  return;
361  }
362 
363  $category = $this->getCategoryById($this->request->int('category_id'));
364 
365  $this->initUnitForm($category);
366  $this->unit_form->setValuesByArray([
367  'factor' => 1,
368  'unit_title' => $this->lng->txt('unit_placeholder')
369  ]);
370 
371  $this->tpl->setContent($this->unit_form->getHtml());
372  }
373 
374  protected function initUnitForm(
375  assFormulaQuestionUnitCategory $category = null,
376  assFormulaQuestionUnit $unit = null
377  ): ilPropertyFormGUI {
378  if ($this->unit_form instanceof ilPropertyFormGUI) {
379  return $this->unit_form;
380  }
381 
382  $unit_in_use = false;
383  if ($unit instanceof assFormulaQuestionUnit && $this->repository->isUnitInUse($unit->getId())) {
384  $unit_in_use = true;
385  }
386 
387  $this->unit_form = new ilPropertyFormGUI();
388 
389  $title = new ilTextInputGUI($this->lng->txt('unit'), 'unit_title');
390  $title->setDisabled($unit_in_use);
391  $title->setRequired(true);
392  $this->unit_form->addItem($title);
393 
394  $baseunit = new ilSelectInputGUI($this->lng->txt('baseunit'), 'base_unit');
395  $items = $this->repository->getCategorizedUnits();
396  $options = [];
397  $category_name = '';
398  $new_category = false;
399  foreach ($items as $item) {
400  if (
401  $unit instanceof assFormulaQuestionUnit &&
402  $unit->getId() === $item->getId()
403  ) {
404  continue;
405  }
406 
407  if ($item instanceof assFormulaQuestionUnitCategory) {
408  if ($category_name !== $item->getDisplayString()) {
409  $new_category = true;
410  $category_name = $item->getDisplayString();
411  }
412  continue;
413  }
414 
415  $options[$item->getId()] = $item->getDisplayString() . ($new_category ? ' (' . $category_name . ')' : '');
416  $new_category = false;
417  }
418  $baseunit->setDisabled($unit_in_use);
419  $baseunit->setOptions([0 => $this->lng->txt('no_selection')] + $options);
420  $this->unit_form->addItem($baseunit);
421 
422  $factor = new ilNumberInputGUI($this->lng->txt('factor'), 'factor');
423  $factor->setRequired(true);
424  $factor->setSize(3);
425  $factor->setMinValue(0);
426  $factor->allowDecimals(true);
427  $factor->setDisabled($unit_in_use);
428  $this->unit_form->addItem($factor);
429 
430  $this->ctrl->setParameterByClass(get_class($this), 'category_id', $this->request->int('category_id'));
431  if (null === $unit) {
432  $this->unit_form->setTitle($this->lng->txt('new_unit'));
433  $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'addUnit'));
434  $this->unit_form->addCommandButton('addUnit', $this->lng->txt('save'));
435  } else {
436  $this->ctrl->setParameter($this, 'unit_id', $unit->getId());
437  if ($unit_in_use) {
438  $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'showUnitsOfCategory'));
439  } else {
440  $this->unit_form->addCommandButton('saveUnit', $this->lng->txt('save'));
441  $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'saveUnit'));
442  }
443  $this->unit_form->setTitle(sprintf(
444  $this->lng->txt('un_sel_cat_sel_unit'),
445  $category->getDisplayString(),
446  $unit->getDisplayString()
447  ));
448  }
449  $this->ctrl->clearParameterByClass(get_class($this), 'category_id');
450 
451  $this->unit_form->addCommandButton('showUnitsOfCategory', $this->lng->txt('cancel'));
452  return $this->unit_form;
453  }
454 
455  protected function showUnitsOfCategory(): void
456  {
457  global $DIC;
458 
459  $ilToolbar = $DIC->toolbar();
460 
461  $category = $this->getCategoryById($this->request->int('category_id'), false);
462 
463  $this->tpl->addJavaScript("./Services/JavaScript/js/Basic.js");
464  $this->tpl->addJavaScript("./Services/Form/js/Form.js");
465  $this->lng->loadLanguageModule('form');
466 
467  $ilToolbar->addButton(
468  $this->lng->txt('back'),
469  $this->ctrl->getLinkTarget($this, $this->getUnitCategoryOverviewCommand())
470  );
471  if ($this->isCRUDContext()) {
472  $this->ctrl->setParameterByClass(get_class($this), 'category_id', $category->getId());
473  $ilToolbar->addButton(
474  $this->lng->txt('un_add_unit'),
475  $this->ctrl->getLinkTarget($this, 'showUnitCreationForm')
476  );
477  $this->ctrl->clearParameterByClass(get_class($this), 'category_id');
478  }
479  $table = new ilUnitTableGUI($this, 'showUnitsOfCategory', $category);
480  $units = $this->repository->loadUnitsForCategory($category->getId());
481  $data = [];
482  foreach ($units as $unit) {
484  $data[] = [
485  'unit_id' => $unit->getId(),
486  'unit' => $unit->getUnit(),
487  'baseunit' => $unit->getBaseunitTitle(),
488  'baseunit_id' => $unit->getBaseUnit(),
489  'factor' => $unit->getFactor(),
490  'sequence' => $unit->getSequence(),
491  ];
492  }
493  $table->setData($data);
494 
495  $this->tpl->setContent($table->getHTML());
496  }
497 
498  protected function showGlobalUnitCategories(): void
499  {
500  $categories = array_filter(
501  $this->repository->getAllUnitCategories(),
502  static function (assFormulaQuestionUnitCategory $category): bool {
503  return !$category->getQuestionFi() ? true : false;
504  }
505  );
506  $data = [];
507  foreach ($categories as $category) {
509  $data[] = [
510  'category_id' => $category->getId(),
511  'category' => $category->getDisplayString()
512  ];
513  }
514 
515  $this->showUnitCategories($data);
516  }
517 
518  protected function confirmDeleteCategory(): void
519  {
520  if (!$this->request->isset('category_id')) {
521  $this->{$this->getUnitCategoryOverviewCommand()}();
522  return;
523  }
524 
525  $this->confirmDeleteCategories([$this->request->int('category_id')]);
526  }
527 
533  protected function confirmDeleteCategories(array $category_ids = null): void
534  {
535  if (!$this->isCRUDContext()) {
536  $this->{$this->getDefaultCommand()}();
537  return;
538  }
539 
540  $category_ids = $category_ids ?? $this->request->getUnitCategoryIds();
541  if (count($category_ids) === 0) {
542  $this->{$this->getUnitCategoryOverviewCommand()}();
543  return;
544  }
545 
546  $confirmation = new ilConfirmationGUI();
547  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'deleteCategories'));
548  $confirmation->setConfirm($this->lng->txt('confirm'), 'deleteCategories');
549  $confirmation->setCancel($this->lng->txt('cancel'), $this->getUnitCategoryOverviewCommand());
550 
551  $errors = [];
552  $num_to_confirm = 0;
553  foreach ($category_ids as $category_id) {
554  try {
555  $category = $this->repository->getUnitCategoryById($category_id);
556  } catch (ilException $e) {
557  continue;
558  }
559 
560  if (!$this->repository->isCRUDAllowed($category_id)) {
561  $errors[] = $category->getDisplayString() . ' - ' . $this->lng->txt('change_adm_categories_not_allowed');
562  continue;
563  }
564 
565  $possible_error = $this->repository->checkDeleteCategory($category_id);
566  if (is_string($possible_error) && $possible_error !== '') {
567  $errors[] = $category->getDisplayString() . ' - ' . $possible_error;
568  continue;
569  }
570 
571  $confirmation->addItem('category_ids[]', (string) $category->getId(), $category->getDisplayString());
572  ++$num_to_confirm;
573  }
574 
575  if ($errors) {
576  $num_errors = count($errors);
577 
578  $error_message = array_map(static function (string $message): string {
579  return '<li>' . $message . '</li>';
580  }, $errors);
581  if ($num_errors === 1) {
582  $this->tpl->setOnScreenMessage(
583  'failure',
584  $this->lng->txt('un_cat_deletion_errors_f_s') . '<ul>' . implode('', $error_message) . '<ul>'
585  );
586  } else {
587  $this->tpl->setOnScreenMessage(
588  'failure',
589  $this->lng->txt('un_cat_deletion_errors_f') . '<ul>' . implode('', $error_message) . '<ul>'
590  );
591  }
592  }
593 
594  if ($num_to_confirm) {
595  if ($num_to_confirm === 1) {
596  $confirmation->setHeaderText($this->lng->txt('un_sure_delete_categories_s'));
597  } else {
598  $confirmation->setHeaderText($this->lng->txt('un_sure_delete_categories'));
599  }
600 
601  $this->tpl->setContent($confirmation->getHTML());
602  } else {
603  $this->{$this->getUnitCategoryOverviewCommand()}();
604  }
605  }
606 
607  protected function deleteCategories(): void
608  {
609  if (!$this->isCRUDContext()) {
610  $this->{$this->getDefaultCommand()}();
611  return;
612  }
613 
614  $category_ids = $this->request->getUnitCategoryIds();
615  if (count($category_ids) === 0) {
616  $this->{$this->getUnitCategoryOverviewCommand()}();
617  return;
618  }
619 
620  $errors = [];
621  $num_deleted = 0;
622  foreach ($category_ids as $category_id) {
623  try {
624  $category = $this->repository->getUnitCategoryById($category_id);
625  } catch (ilException $e) {
626  continue;
627  }
628 
629  if (!$this->repository->isCRUDAllowed($category_id)) {
630  $errors[] = $category->getDisplayString() . ' - ' . $this->lng->txt('change_adm_categories_not_allowed');
631  continue;
632  }
633 
634  $possible_error = $this->repository->deleteCategory($category_id);
635  if (is_string($possible_error) && $possible_error !== '') {
636  $errors[] = $category->getDisplayString() . ' - ' . $possible_error;
637  continue;
638  }
639 
640  ++$num_deleted;
641  }
642 
643  if ($errors) {
644  $num_errors = count($errors);
645 
646  $error_message = array_map(static function (string $message): string {
647  return '<li>' . $message . '</li>';
648  }, $errors);
649  if ($num_errors === 1) {
650  $this->tpl->setOnScreenMessage(
651  'failure',
652  $this->lng->txt('un_cat_deletion_errors_p_s') . '<ul>' . implode('', $error_message) . '<ul>'
653  );
654  } else {
655  $this->tpl->setOnScreenMessage(
656  'failure',
657  $this->lng->txt('un_cat_deletion_errors_p') . '<ul>' . implode('', $error_message) . '<ul>'
658  );
659  }
660  }
661 
662  if ($num_deleted) {
663  if ($num_deleted === 1) {
664  $this->tpl->setOnScreenMessage('success', $this->lng->txt('un_deleted_categories_s'));
665  } else {
666  $this->tpl->setOnScreenMessage('success', $this->lng->txt('un_deleted_categories'));
667  }
668  }
669 
670  $this->{$this->getUnitCategoryOverviewCommand()}();
671  }
672 
674  {
675  if ($this->unit_cat_form instanceof ilPropertyFormGUI) {
676  return $this->unit_cat_form;
677  }
678 
679  $this->unit_cat_form = new ilPropertyFormGUI();
680 
681  $title = new ilTextInputGUI($this->lng->txt('title'), 'category_name');
682  $title->setRequired(true);
683  $this->unit_cat_form->addItem($title);
684 
685  if (null === $cat) {
686  $this->unit_cat_form->setTitle($this->lng->txt('new_category'));
687  $this->unit_cat_form->setFormAction($this->ctrl->getFormAction($this, 'addCategory'));
688  $this->unit_cat_form->addCommandButton('addCategory', $this->lng->txt('save'));
689  } else {
690  $this->ctrl->setParameter($this, 'category_id', $cat->getId());
691  $this->unit_cat_form->addCommandButton('saveCategory', $this->lng->txt('save'));
692  $this->unit_cat_form->setFormAction($this->ctrl->getFormAction($this, 'saveCategory'));
693  $this->unit_cat_form->setTitle(sprintf($this->lng->txt('selected_category'), $cat->getDisplayString()));
694  }
695 
696  $this->unit_cat_form->addCommandButton($this->getUnitCategoryOverviewCommand(), $this->lng->txt('cancel'));
697  return $this->unit_cat_form;
698  }
699 
700  protected function addCategory(): void
701  {
702  if (!$this->isCRUDContext()) {
703  $this->{$this->getDefaultCommand()}();
704  return;
705  }
706 
707  $this->initUnitCategoryForm();
708  if ($this->unit_cat_form->checkInput()) {
709  try {
710  $category = new assFormulaQuestionUnitCategory();
711  $category->setCategory($this->unit_cat_form->getInput('category_name'));
712  $this->repository->saveNewUnitCategory($category);
713  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
714 
715  $this->{$this->getUnitCategoryOverviewCommand()}();
716  return;
717  } catch (ilException $e) {
718  $this->unit_cat_form->getItemByPostVar('category_name')->setAlert($this->lng->txt($e->getMessage()));
719  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
720  }
721  }
722 
723  $this->unit_cat_form->setValuesByPost();
724 
725  $this->tpl->setContent($this->unit_cat_form->getHtml());
726  }
727 
728  protected function showUnitCategoryCreationForm(): void
729  {
730  if (!$this->isCRUDContext()) {
731  $this->{$this->getDefaultCommand()}();
732  return;
733  }
734 
735  $this->initUnitCategoryForm();
736 
737  $this->tpl->setContent($this->unit_cat_form->getHtml());
738  }
739 
740  protected function saveCategory(): void
741  {
742  if (!$this->isCRUDContext()) {
743  $this->{$this->getDefaultCommand()}();
744  return;
745  }
746 
747  $category = $this->getCategoryById($this->request->int('category_id'));
748 
749  $this->initUnitCategoryForm($category);
750  if ($this->unit_cat_form->checkInput()) {
751  try {
752  $category->setCategory($this->unit_cat_form->getInput('category_name'));
753  $this->repository->saveCategory($category);
754  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
755 
756  $this->{$this->getUnitCategoryOverviewCommand()}();
757  return;
758  } catch (ilException $e) {
759  $this->unit_cat_form->getItemByPostVar('category_name')->setAlert($this->lng->txt($e->getMessage()));
760  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
761  }
762  }
763 
764  $this->unit_cat_form->setValuesByPost();
765 
766  $this->tpl->setContent($this->unit_cat_form->getHtml());
767  }
768 
769  protected function showUnitCategoryModificationForm(): void
770  {
771  if (!$this->isCRUDContext()) {
772  $this->{$this->getDefaultCommand()}();
773  return;
774  }
775 
776  $category = $this->getCategoryById($this->request->int('category_id'));
777 
778  $this->initUnitCategoryForm($category);
779  $this->unit_cat_form->setValuesByArray([
780  'category_name' => $category->getCategory()
781  ]);
782 
783  $this->tpl->setContent($this->unit_cat_form->getHtml());
784  }
785 }
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...
$errors
Definition: imgupload.php:65
initUnitCategoryForm(assFormulaQuestionUnitCategory $cat=null)
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...
showUnitCategories(array $categories)
getCategoryById(int $id, bool $for_CRUD=true)
initUnitForm(assFormulaQuestionUnitCategory $category=null, assFormulaQuestionUnit $unit=null)
global $DIC
Definition: feed.php:28
ilUnitConfigurationRepository $repository
This class represents a number property in a property form.
confirmDeleteUnits(array $unit_ids=null)
setRequired(bool $a_required)
confirmDeleteCategories(array $category_ids=null)
__construct(ilUnitConfigurationRepository $repository)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$message
Definition: xapiexit.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS TestQuestionPool InternalRequestService $request