ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
ScoreSettingsDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
27{
29 private array $settings_by_test_fi = [];
30
32 private array $settings_instances = [];
33
34 public function __construct(
35 protected \ilDBInterface $db,
36 protected SettingsFactory $factory
37 ) {
38 }
39
40 public function getFor(int $test_id): ScoreSettings
41 {
42 return isset($this->settings_by_test_fi[$test_id])
43 ? $this->settings_instances[$this->settings_by_test_fi[$test_id]]
44 : $this->doSelect("WHERE test_id = {$this->db->quote($test_id, \ilDBConstants::T_INTEGER)}");
45 }
46
47 public function getById(int $settings_id): ScoreSettings
48 {
49 if (isset($this->settings_instances[$settings_id])) {
50 return $this->settings_instances[$settings_id];
51 }
52
53 $res = $this->db->queryF(
54 "SELECT * FROM tst_test_settings WHERE id = %s",
56 [$settings_id]
57 );
58
59 if ($this->db->numRows($res) === 0) {
60 throw new SettingsNotFoundException("No score settings with id: {$settings_id}");
61 }
62
63 $settings = $this->factory->createScoreSettingsFromDBRow($this->db->fetchAssoc($res));
64 $this->settings_instances[$settings->getId()] = $settings;
65
66 return $settings;
67 }
68
69 protected function doSelect(string $where_part): ScoreSettings
70 {
71 $query = 'SELECT ' . PHP_EOL
72 . 'tst_set.id,' . PHP_EOL
73 . 'tst_set.count_system, tst_set.score_cutting, tst_set.pass_scoring,' . PHP_EOL
74 . 'tst_set.score_reporting, tst_set.reporting_date,' . PHP_EOL
75 . 'tst_set.show_grading_status, tst_set.show_grading_mark, tst_set.pass_deletion_allowed,' . PHP_EOL
76 . 'tst_set.print_bs_with_res,' . PHP_EOL //print_bs_with_res_sp
77 . 'tst_set.examid_in_test_res,' . PHP_EOL
78 . 'tst_set.results_presentation,' . PHP_EOL
79 . 'tst_set.exportsettings,' . PHP_EOL
80 . 'tst_set.highscore_enabled, tst_set.highscore_anon, tst_set.highscore_achieved_ts, tst_set.highscore_score, tst_set.highscore_percentage, tst_set.highscore_wtime, tst_set.highscore_own_table, tst_set.highscore_top_table, tst_set.highscore_top_num,' . PHP_EOL
81 . 'tst.test_id AS test_id' . PHP_EOL
82 . 'FROM tst_test_settings AS tst_set' . PHP_EOL
83 . 'INNER JOIN tst_tests AS tst ON tst.settings_id = tst_set.id' . PHP_EOL
84 . $where_part;
85
86 $res = $this->db->query($query);
87
88 if ($this->db->numRows($res) === 0) {
89 throw new SettingsNotFoundException("No score settings for: {$where_part}");
90 }
91
92 $row = $this->db->fetchAssoc($res);
93 $settings = $this->factory->createScoreSettingsFromDBRow($row);
94
95 $this->settings_instances[$row['test_id']] = $settings;
96
97 return $settings;
98 }
99
100 public function store(ScoreSettings $settings): void
101 {
102 $values = array_merge(
103 $settings->getScoringSettings()->toStorage(),
104 $settings->getResultSummarySettings()->toStorage(),
105 $settings->getResultDetailsSettings()
106 ->withShowPassDetails($settings->getResultSummarySettings()->getShowPassDetails())
107 ->toStorage(),
108 $settings->getGamificationSettings()->toStorage()
109 );
110
111 $this->db->update(
112 'tst_test_settings',
113 $values,
114 ['id' => [\ilDBConstants::T_INTEGER, $settings->getId()]]
115 );
116
117 $this->settings_instances = array_filter(
118 $this->settings_instances,
119 static fn(ScoreSettings $value): bool => $value->getId() !== $settings->getId(),
120 );
121 }
122
123 public function getSettingsResultSummaryByObjIds(array $obj_ids): array
124 {
125 $result = $this->db->query(
126 'SELECT ' . PHP_EOL
127 . 'tst_set.score_reporting, tst_set.reporting_date,' . PHP_EOL
128 . 'tst_set.show_grading_status, tst_set.show_grading_mark, tst_set.pass_deletion_allowed,' . PHP_EOL
129 . 'tst_tests.obj_fi AS obj_fi' . PHP_EOL
130 . 'FROM tst_test_settings AS tst_set' . PHP_EOL
131 . 'INNER JOIN tst_tests ON tst_tests.settings_id = tst_set.id' . PHP_EOL
132 . 'WHERE ' . $this->db->in('obj_fi', $obj_ids, false, \ilDBConstants::T_INTEGER)
133 );
134
135 $settings_summary = [];
136 while (($row = $this->db->fetchAssoc($result)) !== null) {
137 $settings_summary[$row['obj_fi']] = (new SettingsResultSummary())
138 ->withScoreReporting(ScoreReportingTypes::from($row['score_reporting']))
139 ->withReportingDate($row['reporting_date'] !== 0
140 ? \DateTimeImmutable::createFromFormat('U', (string) $row['reporting_date'])
141 : null)
142 ->withShowGradingStatusEnabled((bool) $row['show_grading_status'])
143 ->withShowGradingMarkEnabled((bool) $row['show_grading_mark'])
144 ->withPassDeletionAllowed((bool) $row['pass_deletion_allowed']);
145 }
146 return $settings_summary;
147 }
148}
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