ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
PersonalSettingsCreateAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use ILIAS\UI\Factory as UIFactory;
29use Psr\Http\Message\ServerRequestInterface;
30
32{
33 public function __construct(
34 private readonly UIFactory $ui_factory,
35 private readonly Language $lng,
36 private readonly \ilObjUser $user,
37 private readonly PersonalSettingsRepository $repository,
38 private readonly MainSettingsRepository $main_settings_repository,
39 private readonly ScoreSettingsRepository $score_settings_repository,
40 private readonly MarksRepository $marks_repository,
41 ) {
42 }
43
44 public function buildModal(string $url): RoundTrip
45 {
46 $input_factory = $this->ui_factory->input();
47
48 $inputs = [
49 'name' => $input_factory->field()->text($this->lng->txt('title'))
50 ->withLabel($this->lng->txt('title'))
51 ->withRequired(true),
52 'author' => $input_factory->field()->text($this->lng->txt('author'))
53 ->withRequired(true)
54 ->withValue($this->user->getFullname()),
55 'description' => $input_factory->field()->textarea($this->lng->txt('description')),
56 ];
57
58 $explanation = $this->ui_factory->messageBox()->info(
59 $this->lng->txt('personal_settings_explanation')
60 );
61
62 return $this->ui_factory->modal()->roundtrip(
63 $this->lng->txt('personal_settings_create'),
64 [$explanation],
65 $inputs,
66 $url
67 )->withSubmitLabel($this->lng->txt('personal_settings_save'));
68 }
69
70 public function perform(int $test_id, ServerRequestInterface $request): void
71 {
72 $container = $this->buildModal('')->withRequest($request);
73 $data = $container->getData();
74
75 // 1. Resolve error messages on validation failure
76 if ($data === null) {
77 $inputs = $container->getInputs();
78
79 if ($inputs['name']->getValue() === '') {
80 throw new \InvalidArgumentException('personal_settings_required_title');
81 }
82
83 if ($inputs['author']->getValue() === '') {
84 throw new \InvalidArgumentException('personal_settings_required_author');
85 }
86 }
87
88 // 2. Create a new template
89 $template = $this->repository->create(
90 $data['name'],
91 $data['description'] ?? '',
92 $data['author']
93 );
94
95 // 3. Clone settings from test for the new template
96 $this->main_settings_repository->store(
97 $this->main_settings_repository->getFor($test_id)->withId($template->getSettingsId())
98 );
99 $this->score_settings_repository->store(
100 $this->score_settings_repository->getFor($test_id)->withId($template->getSettingsId())
101 );
102
103 // 4. Clone the mark schema of the test and create references for the template
104 $mark_schema = $this->marks_repository->getMarkSchemaFor($test_id);
105 $mark_ids = $this->marks_repository->storeMarkSchema($mark_schema->withTestId(-1));
106 $this->repository->associateMarkSteps($template->getId(), $mark_ids);
107 }
108}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
__construct(private readonly UIFactory $ui_factory, private readonly Language $lng, private readonly \ilObjUser $user, private readonly PersonalSettingsRepository $repository, private readonly MainSettingsRepository $main_settings_repository, private readonly ScoreSettingsRepository $score_settings_repository, private readonly MarksRepository $marks_repository,)
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
global $lng
Definition: privfeed.php:31
$url
Definition: shib_logout.php:68
$container
@noRector
Definition: wac.php:37