ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ScoreSettingsDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Test\Scoring\Settings\Settings as SettingsScoring;
24
26{
27 public const TABLE_NAME = 'tst_tests';
28 public const STORAGE_DATE_FORMAT = 'YmdHis';
29
30 protected \ilDBInterface $db;
31
32 public function __construct(\ilDBInterface $db)
33 {
34 $this->db = $db;
35 }
36
37 public function getForObjFi(int $obj_fi): ScoreSettings
38 {
39 $where_part = 'WHERE obj_fi = ' . $this->db->quote($obj_fi, 'integer');
40 return $this->doSelect($where_part);
41 }
42
43 public function getFor(int $test_id): ScoreSettings
44 {
45 $where_part = 'WHERE test_id = ' . $this->db->quote($test_id, 'integer');
46 return $this->doSelect($where_part);
47 }
48
49 protected function doSelect(string $where_part): ScoreSettings
50 {
51 $query = 'SELECT ' . PHP_EOL
52 . 'test_id,' . PHP_EOL
53 . 'count_system, score_cutting, pass_scoring,' . PHP_EOL
54 . 'score_reporting, reporting_date,' . PHP_EOL
55 . 'show_grading_status, show_grading_mark, pass_deletion_allowed,' . PHP_EOL
56 . 'print_bs_with_res,' . PHP_EOL //print_bs_with_res_sp
57 . 'examid_in_test_res,' . PHP_EOL
58 . 'results_presentation,' . PHP_EOL
59 . 'exportsettings,' . PHP_EOL
60 . 'highscore_enabled, highscore_anon, highscore_achieved_ts, highscore_score, highscore_percentage, highscore_wtime, highscore_own_table, highscore_top_table, highscore_top_num' . PHP_EOL
61 . 'FROM ' . self::TABLE_NAME . PHP_EOL
62 . $where_part;
63
64 $res = $this->db->query($query);
65
66 if ($this->db->numRows($res) == 0) {
67 throw new \Exception('no score settings: ' . $where_part);
68 }
69
70 $row = $this->db->fetchAssoc($res);
71
72 $test_id = (int) $row['test_id'];
73
74 $settings = new ScoreSettings(
75 $test_id,
76 (new SettingsScoring($test_id))
77 ->withCountSystem((int) $row['count_system'])
78 ->withScoreCutting((int) $row['score_cutting'])
79 ->withPassScoring((int) $row['pass_scoring']),
80 (new SettingsResultSummary($test_id))
81 ->withScoreReporting(ScoreReportingTypes::from($row['score_reporting']))
82 ->withReportingDate($this->buildDateFromString($row['reporting_date']))
83 ->withShowGradingStatusEnabled((bool) $row['show_grading_status'])
84 ->withShowGradingMarkEnabled((bool) $row['show_grading_mark'])
85 ->withPassDeletionAllowed((bool) $row['pass_deletion_allowed']),
86 //->withShowPassDetails derived from results_presentation with bit RESULTPRES_BIT_PASS_DETAILS
87 (new SettingsResultDetails($test_id))
88 ->withResultsPresentation((int) $row['results_presentation'])
89 ->withShowExamIdInTestResults((bool) $row['examid_in_test_res'])
90 ->withExportSettings((int) $row['exportsettings']),
91 (new SettingsGamification($test_id))
92 ->withHighscoreEnabled((bool) $row['highscore_enabled'])
93 ->withHighscoreAnon((bool) $row['highscore_anon'])
94 ->withHighscoreAchievedTS((bool) $row['highscore_achieved_ts'])
95 ->withHighscoreScore((bool) $row['highscore_score'])
96 ->withHighscorePercentage((bool) $row['highscore_percentage'])
97 ->withHighscoreWTime((bool) $row['highscore_wtime'])
98 ->withHighscoreOwnTable((bool) $row['highscore_own_table'])
99 ->withHighscoreTopTable((bool) $row['highscore_top_table'])
100 ->withHighscoreTopNum((int) $row['highscore_top_num'])
101 );
102
103 return $settings;
104 }
105
106 public function store(ScoreSettings $settings): void
107 {
108 $values = array_merge(
109 $settings->getScoringSettings()->toStorage(),
110 $settings->getResultSummarySettings()->toStorage(),
111 $settings->getResultDetailsSettings()
112 ->withShowPassDetails($settings->getResultSummarySettings()->getShowPassDetails())
113 ->toStorage(),
114 $settings->getGamificationSettings()->toStorage()
115 );
116
117 $this->db->update(
118 self::TABLE_NAME,
119 $values,
120 ['test_id' => ['integer', $settings->getTestId()]]
121 );
122 }
123
124
125 public function getSettingsResultSummaryByObjIds(array $obj_ids): array
126 {
127 $result = $this->db->query(
128 'SELECT ' . PHP_EOL
129 . 'test_id, obj_fi, score_reporting, reporting_date,' . PHP_EOL
130 . 'show_grading_status, show_grading_mark, pass_deletion_allowed' . PHP_EOL
131 . 'FROM ' . self::TABLE_NAME . 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($row['test_id']))
138 ->withScoreReporting(ScoreReportingTypes::from($row['score_reporting']))
139 ->withReportingDate($this->buildDateFromString($row['reporting_date']))
140 ->withShowGradingStatusEnabled((bool) $row['show_grading_status'])
141 ->withShowGradingMarkEnabled((bool) $row['show_grading_mark'])
142 ->withPassDeletionAllowed((bool) $row['pass_deletion_allowed']);
143 }
144 return $settings_summary;
145 }
146
147 private function buildDateFromString(?string $reporting_date): ?\DateTimeImmutable
148 {
149 if ($reporting_date === null
150 || $reporting_date === '') {
151 return null;
152 }
153
154 return \DateTimeImmutable::createFromFormat(
155 self::STORAGE_DATE_FORMAT,
156 $reporting_date,
157 new \DateTimeZone('UTC')
158 );
159 }
160}
Interface ilDBInterface.
$res
Definition: ltiservices.php:69