ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
MainSettingsDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
27{
29 private array $settings_by_obj_fi = [];
30
32 private array $settings_by_test_fi = [];
33
35 private array $settings_instances = [];
36
37 public function __construct(
38 protected \ilDBInterface $db,
39 protected SettingsFactory $factory
40 ) {
41 }
42
43 public function getForObjFi(int $obj_fi): MainSettings
44 {
45 return isset($this->settings_by_obj_fi[$obj_fi])
46 ? $this->settings_instances[$this->settings_by_obj_fi[$obj_fi]]
47 : $this->doSelect("WHERE obj_fi = {$this->db->quote($obj_fi, \ilDBConstants::T_INTEGER)}");
48 }
49
50 public function getFor(int $test_id): MainSettings
51 {
52 return isset($this->settings_by_test_fi[$test_id])
53 ? $this->settings_instances[$this->settings_by_test_fi[$test_id]]
54 : $this->doSelect("WHERE test_id = {$this->db->quote($test_id, \ilDBConstants::T_INTEGER)}");
55 }
56
57 public function getById(int $settings_id): MainSettings
58 {
59 if (isset($this->settings_instances[$settings_id])) {
60 return $this->settings_instances[$settings_id];
61 }
62
63 $res = $this->db->queryF(
64 "SELECT * FROM tst_test_settings WHERE id = %s",
66 [$settings_id]
67 );
68
69 if ($this->db->numRows($res) === 0) {
70 throw new SettingsNotFoundException("No main settings with id: {$settings_id}");
71 }
72
73 $settings = $this->factory->createMainSettingsFromDBRow($this->db->fetchAssoc($res));
74 $this->settings_instances[$settings->getId()] = $settings;
75
76 return $settings;
77 }
78
79 protected function doSelect(string $where_part): MainSettings
80 {
81 $query = 'SELECT ' . PHP_EOL
82 . 'tst_set.id,' . PHP_EOL
83 . 'tst_set.question_set_type,' . PHP_EOL
84 . 'tst_set.anonymity,' . PHP_EOL
85 . 'tst_set.intro_enabled,' . PHP_EOL
86 . 'tst_set.hide_info_tab,' . PHP_EOL
87 . 'tst_set.conditions_checkbox_enabled,' . PHP_EOL
88 . 'tst_set.introduction,' . PHP_EOL
89 . 'tst_set.introduction_page_id,' . PHP_EOL
90 . 'tst_set.starting_time_enabled,' . PHP_EOL
91 . 'tst_set.starting_time,' . PHP_EOL
92 . 'tst_set.ending_time_enabled,' . PHP_EOL
93 . 'tst_set.ending_time,' . PHP_EOL
94 . 'tst_set.password_enabled,' . PHP_EOL
95 . 'tst_set.password,' . PHP_EOL
96 . 'tst_set.ip_range_from,' . PHP_EOL
97 . 'tst_set.ip_range_to,' . PHP_EOL
98 . 'tst_set.fixed_participants,' . PHP_EOL
99 . 'tst_set.nr_of_tries,' . PHP_EOL
100 . 'tst_set.block_after_passed,' . PHP_EOL
101 . 'tst_set.pass_waiting,' . PHP_EOL
102 . 'tst_set.enable_processing_time,' . PHP_EOL
103 . 'tst_set.processing_time,' . PHP_EOL
104 . 'tst_set.reset_processing_time,' . PHP_EOL
105 . 'tst_set.kiosk,' . PHP_EOL
106 . 'tst_set.examid_in_test_pass,' . PHP_EOL
107 . 'tst_set.title_output,' . PHP_EOL
108 . 'tst_set.autosave,' . PHP_EOL
109 . 'tst_set.autosave_ival,' . PHP_EOL
110 . 'tst_set.shuffle_questions,' . PHP_EOL
111 . 'tst_set.answer_feedback_points,' . PHP_EOL
112 . 'tst_set.answer_feedback,' . PHP_EOL
113 . 'tst_set.specific_feedback,' . PHP_EOL
114 . 'tst_set.instant_verification,' . PHP_EOL
115 . 'tst_set.force_inst_fb,' . PHP_EOL
116 . 'tst_set.inst_fb_answer_fixation,' . PHP_EOL
117 . 'tst_set.follow_qst_answer_fixation,' . PHP_EOL
118 . 'tst_set.use_previous_answers,' . PHP_EOL
119 . 'tst_set.suspend_test_allowed,' . PHP_EOL
120 . 'tst_set.sequence_settings,' . PHP_EOL
121 . 'tst_set.usr_pass_overview_mode,' . PHP_EOL
122 . 'tst_set.show_marker,' . PHP_EOL
123 . 'tst_set.show_questionlist,' . PHP_EOL
124 . 'tst_set.enable_examview,' . PHP_EOL
125 . 'tst_set.showfinalstatement,' . PHP_EOL
126 . 'tst_set.finalstatement,' . PHP_EOL
127 . 'tst_set.concluding_remarks_page_id,' . PHP_EOL
128 . 'tst_set.redirection_mode,' . PHP_EOL
129 . 'tst_set.redirection_url,' . PHP_EOL
130 . 'tst_set.skill_service,' . PHP_EOL
131 . 'tst.test_id AS test_id,' . PHP_EOL
132 . 'tst.obj_fi AS obj_fi' . PHP_EOL
133 . 'FROM tst_test_settings AS tst_set' . PHP_EOL
134 . 'INNER JOIN tst_tests AS tst ON tst.settings_id = tst_set.id' . PHP_EOL
135 . $where_part;
136
137 $res = $this->db->query($query);
138
139 if ($this->db->numRows($res) === 0) {
140 throw new SettingsNotFoundException("No main settings for: {$where_part}");
141 }
142
143 $row = $this->db->fetchAssoc($res);
144 $settings = $this->factory->createMainSettingsFromDBRow($row);
145
146 $this->settings_by_obj_fi[$row['obj_fi']] = $settings->getId();
147 $this->settings_by_test_fi[$row['test_id']] = $settings->getId();
148 $this->settings_instances[$settings->getId()] = $settings;
149
150 return $settings;
151 }
152
153 public function store(MainSettings $settings, ?int $test_id = null): MainSettings
154 {
155 $values = array_merge(
156 $settings->getGeneralSettings()->toStorage(),
157 $settings->getIntroductionSettings()->toStorage(),
158 $settings->getAccessSettings()->toStorage(),
159 $settings->getTestBehaviourSettings()->toStorage(),
160 $settings->getQuestionBehaviourSettings()->toStorage(),
161 $settings->getParticipantFunctionalitySettings()->toStorage(),
162 $settings->getFinishingSettings()->toStorage(),
163 $settings->getAdditionalSettings()->toStorage()
164 );
165
166 if ($settings->getId() === 0) {
167 $settings = $settings->withId($this->db->nextId('tst_test_settings'));
168 $values['id'] = [\ilDBConstants::T_INTEGER, $settings->getId()];
169
170 $this->db->insert('tst_test_settings', $values);
171 $this->db->update(
172 'tst_tests',
173 ['settings_id' => [\ilDBConstants::T_INTEGER, $settings->getId()]],
174 ['test_id' => [\ilDBConstants::T_INTEGER, $test_id]]
175 );
176 } else {
177 $this->db->update(
178 'tst_test_settings',
179 $values,
180 ['id' => [\ilDBConstants::T_INTEGER, $settings->getId()]]
181 );
182 }
183
184 unset($this->settings_instances[$settings->getId()]);
185 $this->settings_by_obj_fi = array_filter(
186 $this->settings_by_obj_fi,
187 static fn(int $value): bool => $value !== $settings->getId(),
188 );
189 $this->settings_by_test_fi = array_filter(
190 $this->settings_by_test_fi,
191 static fn(int $value): bool => $value !== $settings->getId(),
192 );
193
194 return $settings;
195 }
196}
factory()
__construct(protected \ilDBInterface $db, protected SettingsFactory $factory)
@depracated This is only a temporary exception to identify missing migrations and will be removed in ...
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...