ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
54 public function getActionId(): string
55 {
56 return self::ACTION_ID;
57 }
58
59 public function buildTableAction(
60 URLBuilder $url_builder,
61 URLBuilderToken $row_id_token,
62 URLBuilderToken $action_token,
63 URLBuilderToken $action_type_token
64 ): Action {
65 return $this->ui_factory->table()->action()->single(
66 $this->lng->txt('personal_settings_apply'),
67 $url_builder
68 ->withParameter($action_token, self::ACTION_ID)
69 ->withParameter($action_type_token, ParticipantTableActions::SHOW_ACTION),
70 $row_id_token
71 )->withAsync();
72 }
73
74 public function buildModal(
75 URLBuilder $url_builder,
76 array $selected_templates
77 ): ?Modal {
78 $template = $this->checkSelectedTemplate($selected_templates);
79 $question_set_type_changed = $this->hasDifferentQuestionSetType($template->getSettingsId());
80
81 return $this->ui_factory->modal()->interruptive(
82 $this->lng->txt('confirm'),
83 $this->lng->txt($question_set_type_changed ? 'personal_settings_apply_changed_confirmation' : 'personal_settings_apply_confirmation'),
84 $url_builder->buildURI()->__toString()
85 )->withAffectedItems(
86 [
87 $this->ui_factory->modal()->interruptiveItem()->standard(
88 (string) $template->getId(),
89 $template->getName(),
90 null,
91 sprintf($this->lng->txt('personal_settings_apply_description'), $this->test_obj->getTitle()),
92 ),
93 ]
94 )->withActionButtonLabel($this->lng->txt('apply'));
95 }
96
97 public function onSubmit(
98 URLBuilder $url_builder,
99 ServerRequestInterface $request,
100 array $selected_templates,
101 ): ?Modal {
102 $template = $this->checkSelectedTemplate($selected_templates);
103
104 $old_question_set_config = $this->hasDifferentQuestionSetType($template->getSettingsId()) ?
105 $this->question_set_config_factory->getQuestionSetConfig() :
106 null;
107
108 $test_settings_id = $this->test_obj->getMainSettings()->getId();
109 $main_settings = $this->main_settings_repository->getById($template->getSettingsId());
110 $score_settings = $this->score_settings_repository->getById($template->getSettingsId());
111
112 $mark_schema = $this->marks_repository->getMarkSchemaBySteps(
113 $this->repository->lookupMarkSteps($template->getId())
114 );
115
116 $this->main_settings_repository->store($main_settings->withId($test_settings_id));
117 $this->score_settings_repository->store($score_settings->withId($test_settings_id));
118 $this->marks_repository->storeMarkSchema($mark_schema->withTestId($this->test_obj->getTestId()));
119
120 if ($old_question_set_config && $old_question_set_config->doesQuestionSetRelatedDataExist()) {
121 $old_question_set_config->removeQuestionSetRelatedData();
122 }
123
124 $this->tpl->setOnScreenMessage(
125 GlobalTemplate::MESSAGE_TYPE_SUCCESS,
126 $this->lng->txt('personal_settings_apply_success'),
127 true
128 );
129 return null;
130 }
131
132 private function checkSelectedTemplate(array $selected_templates): PersonalSettingsTemplate
133 {
134 if (count($selected_templates) !== 1) {
135 throw new \InvalidArgumentException('personal_settings_invalid_selection');
136 }
137
138 // Do not apply if user datasets exist
139 if ($this->test_obj->evalTotalPersons() > 0) {
140 throw new \InvalidArgumentException('personal_settings_apply_not_possible');
141 }
142
143 return reset($selected_templates);
144 }
145
146 private function hasDifferentQuestionSetType(int $template_settings_id): bool
147 {
148 $template_main_settings = $this->main_settings_repository->getById($template_settings_id);
149 return $template_main_settings->getGeneralSettings()->getQuestionSetType() !== $this->test_obj->getQuestionSetType();
150 }
151}
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:31