ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
PersonalSettingsTableApplyAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30use ILIAS\UI\Factory as UIFactory;
34use Psr\Http\Message\ServerRequestInterface;
35
37{
38 public const string ACTION_ID = 'apply_template';
39
40 public function __construct(
41 private readonly Language $lng,
42 private readonly UIFactory $ui_factory,
43 private readonly \ilTestQuestionSetConfigFactory $question_set_config_factory,
44 private readonly GlobalTemplate $tpl,
45 private readonly \ilObjTest $test_obj,
46 private readonly PersonalSettingsRepository $repository,
47 private readonly MainSettingsRepository $main_settings_repository,
48 private readonly ScoreSettingsRepository $score_settings_repository,
49 private readonly MarksRepository $marks_repository,
50 ) {
51 }
52
53 public function getActionId(): string
54 {
55 return self::ACTION_ID;
56 }
57
58 public function buildTableAction(
59 URLBuilder $url_builder,
60 URLBuilderToken $row_id_token,
61 URLBuilderToken $action_token,
62 URLBuilderToken $action_type_token
63 ): Action {
64 return $this->ui_factory->table()->action()->single(
65 $this->lng->txt('personal_settings_apply'),
66 $url_builder
67 ->withParameter($action_token, self::ACTION_ID)
68 ->withParameter($action_type_token, ParticipantTableActions::SHOW_ACTION),
69 $row_id_token
70 )->withAsync();
71 }
72
73 public function buildModal(
74 URLBuilder $url_builder,
75 array $selected_templates
76 ): ?Modal {
77 $template = $this->checkSelectedTemplate($selected_templates);
78 $question_set_type_changed = $this->hasDifferentQuestionSetType($template->getSettingsId());
79
80 return $this->ui_factory->modal()->interruptive(
81 $this->lng->txt('confirm'),
82 $this->lng->txt($question_set_type_changed ? 'personal_settings_apply_changed_confirmation' : 'personal_settings_apply_confirmation'),
83 $url_builder->buildURI()->__toString()
84 )->withAffectedItems(
85 [
86 $this->ui_factory->modal()->interruptiveItem()->standard(
87 (string) $template->getId(),
88 $template->getName(),
89 null,
90 sprintf($this->lng->txt('personal_settings_apply_description'), $this->test_obj->getTitle()),
91 ),
92 ]
93 )->withActionButtonLabel($this->lng->txt('apply'));
94 }
95
96 public function onSubmit(
97 URLBuilder $url_builder,
98 ServerRequestInterface $request,
99 array $selected_templates,
100 ): ?Modal {
101 $template = $this->checkSelectedTemplate($selected_templates);
102
103 $old_question_set_config = $this->hasDifferentQuestionSetType($template->getSettingsId()) ?
104 $this->question_set_config_factory->getQuestionSetConfig() :
105 null;
106
107 $test_settings_id = $this->test_obj->getMainSettings()->getId();
108 $main_settings = $this->main_settings_repository->getById($template->getSettingsId());
109 $score_settings = $this->score_settings_repository->getById($template->getSettingsId());
110
111 $mark_schema = $this->marks_repository->getMarkSchemaBySteps(
112 $this->repository->lookupMarkSteps($template->getId())
113 );
114
115 $this->main_settings_repository->store($main_settings->withId($test_settings_id));
116 $this->score_settings_repository->store($score_settings->withId($test_settings_id));
117 $this->marks_repository->storeMarkSchema($mark_schema->withTestId($this->test_obj->getTestId()));
118
119 if ($old_question_set_config && $old_question_set_config->doesQuestionSetRelatedDataExist()) {
120 $old_question_set_config->removeQuestionSetRelatedData();
121 }
122
123 $this->tpl->setOnScreenMessage(
124 GlobalTemplate::MESSAGE_TYPE_SUCCESS,
125 $this->lng->txt('personal_settings_apply_success'),
126 true
127 );
128 return null;
129 }
130
131 private function checkSelectedTemplate(array $selected_templates): PersonalSettingsTemplate
132 {
133 if (count($selected_templates) !== 1) {
134 throw new \InvalidArgumentException('personal_settings_invalid_selection');
135 }
136
137 // Do not apply if user datasets exist
138 if ($this->test_obj->evalTotalPersons() > 0) {
139 throw new \InvalidArgumentException('personal_settings_apply_not_possible');
140 }
141
142 return reset($selected_templates);
143 }
144
145 private function hasDifferentQuestionSetType(int $template_settings_id): bool
146 {
147 $template_main_settings = $this->main_settings_repository->getById($template_settings_id);
148 return $template_main_settings->getGeneralSettings()->getQuestionSetType() !== $this->test_obj->getQuestionSetType();
149 }
150}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
__construct(private readonly Language $lng, private readonly UIFactory $ui_factory, private readonly \ilTestQuestionSetConfigFactory $question_set_config_factory, private readonly GlobalTemplate $tpl, private readonly \ilObjTest $test_obj, private readonly PersonalSettingsRepository $repository, private readonly MainSettingsRepository $main_settings_repository, private readonly ScoreSettingsRepository $score_settings_repository, private readonly MarksRepository $marks_repository,)
onSubmit(URLBuilder $url_builder, ServerRequestInterface $request, array $selected_templates,)
buildTableAction(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:214
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes commonalities between the different modals.
Definition: Modal.php:35
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:26