ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.assClozeGapCombination.php
Go to the documentation of this file.
1<?php
2
20{
21 public function __construct(
22 private readonly ilDBInterface $db
23 ) {
24
25 }
26
27 public function loadFromDb(int $question_id): array
28 {
29 $result = $this->db->queryF(
30 'SELECT combinations.combination_id,
31 combinations.gap_fi,
32 combinations.answer,
33 combinations.row_id,
34 combinations.points,
35 combinations.best_solution,
36 combinations.question_fi,
37 cloze.cloze_type
38 FROM qpl_a_cloze_combi_res AS combinations
39 INNER JOIN qpl_a_cloze AS cloze
40 WHERE combinations.question_fi = cloze.question_fi
41 AND combinations.gap_fi = cloze.gap_id
42 AND combinations.question_fi = %s
43 ORDER BY combination_id, row_id, gap_fi ASC
44 ',
45 ['integer'],
46 [$question_id]
47 );
48
49 $return_array = [];
50 while ($data = $this->db->fetchAssoc($result)) {
51 if (isset($return_array[$data['combination_id'] . '::' . $data['gap_fi']])) {
52 continue;
53 }
54
55 $return_array[$data['combination_id'] . '::' . $data['row_id'] . '::' . $data['gap_fi']] = [
56 'cid' => $data['combination_id'],
57 'gap_fi' => $data['gap_fi'],
58 'answer' => $data['answer'],
59 'points' => $data['points'],
60 'row_id' => $data['row_id'],
61 'type' => $data['cloze_type'],
62 'best_solution' => $data['best_solution']
63 ];
64 }
65
66 return array_values($return_array);
67 }
68
69 public function getCleanCombinationArray(int $question_id): array
70 {
71 $combination_from_db = $this->loadFromDb($question_id);
72 $clean_array = [];
73 foreach ($combination_from_db as $key => $value) {
74 $clean_array[$value['cid']][$value['row_id']][$value['gap_fi']]['answer'] = $value['answer'];
75 $clean_array[$value['cid']][$value['row_id']]['points'] = $value['points'];
76 $clean_array[$value['cid']][$value['row_id']][$value['gap_fi']]['type'] = $value['type'];
77 }
78 return $clean_array;
79 }
80
81 public function saveGapCombinationToDb(
82 int $question_id,
83 array $gap_combinations,
84 array $gap_values
85 ): void {
86 $best_solutions = [];
87 for ($i = 0; $i < count($gap_combinations['points']); $i++) {
88 $highest_points = 0;
89 for ($j = 0; $j < count($gap_combinations['points'][$i]); $j++) {
90 if ($highest_points < $gap_combinations['points'][$i][$j]) {
91 $highest_points = $gap_combinations['points'][$i][$j];
92 $best_solutions[$i] = $j;
93 }
94 }
95 }
96 for ($i = 0; $i < count($gap_values); $i++) {
97 for ($j = 0; $j < count($gap_values[$i]); $j++) {
98 for ($k = 0; $k < count($gap_values[$i][$j]); $k++) {
99 if ($best_solutions[$i] == $j) {
100 $best_solution = 1;
101 } else {
102 $best_solution = 0;
103 }
104 $this->db->manipulateF(
105 'INSERT INTO qpl_a_cloze_combi_res
106 (combination_id, question_fi, gap_fi, row_id, answer, points, best_solution) VALUES (%s, %s, %s, %s, %s, %s, %s)',
107 [
108 'integer',
109 'integer',
110 'integer',
111 'integer',
112 'text',
113 'float',
114 'integer'
115 ],
116 [
117 $i,
118 $question_id,
119 $gap_combinations['select'][$i][$k],
120 $j,
121 $gap_values[$i][$j][$k],
122 (float) str_replace(',', '.', $gap_combinations['points'][$i][$j]),
123 $best_solution
124 ]
125 );
126 }
127 }
128 }
129 }
130 public function importGapCombinationToDb(int $question_id, array $gap_combinations): void
131 {
132 foreach ($gap_combinations as $row) {
133 if (is_object($row)) {
134 $row = get_object_vars($row);
135 }
136 if ($question_id != -1) {
137 $this->db->manipulateF(
138 'INSERT INTO qpl_a_cloze_combi_res
139 (combination_id, question_fi, gap_fi, row_id, answer, points, best_solution)
140 VALUES (%s, %s, %s, %s, %s, %s, %s)',
141 [
142 'integer',
143 'integer',
144 'integer',
145 'integer',
146 'text',
147 'float',
148 'integer'
149 ],
150 [
151 $row['cid'],
152 $question_id,
153 $row['gap_fi'],
154 $row['row_id'],
155 $row['answer'],
156 $row['points'],
157 $row['best_solution']
158 ]
159 );
160 }
161 }
162 }
163
164 public function clearGapCombinationsFromDb($question_id): void
165 {
166 $this->db->manipulateF(
167 'DELETE FROM qpl_a_cloze_combi_res WHERE question_fi = %s',
168 [ 'integer' ],
169 [ $question_id ]
170 );
171 }
172
173 public function combinationExistsForQid($question_id): bool
174 {
175 $result = $this->db->queryF(
176 'SELECT * FROM qpl_a_cloze_combi_res WHERE question_fi = %s ORDER BY gap_fi ASC',
177 ['integer'],
178 [$question_id]
179 );
180 if ($result->numRows() > 0) {
181 return true;
182 }
183 return false;
184 }
185
186 public function getGapsWhichAreUsedInCombination($question_id): array
187 {
188 $result = $this->db->queryF(
189 'SELECT gap_fi, combination_id FROM '
190 . $this->db->quoteIdentifier('qpl_a_cloze_combi_res')
191 . ' WHERE question_fi = %s GROUP BY gap_fi, combination_id',
192 ['integer'],
193 [$question_id]
194 );
195 $gaps = [];
196 if ($result->numRows() > 0) {
197 while ($data = $this->db->fetchAssoc($result)) {
198 $gaps[$data['gap_fi']] = $data['combination_id'];
199 }
200 }
201 return $gaps;
202 }
203
205 int $question_id,
206 int $combination_id = -1
207 ): float {
208 $result = $this->fetchResult($question_id, $combination_id);
209
210 $points = 0.0;
211 while (($data = $this->db->fetchAssoc($result)) !== null) {
212 $points += $data['points'];
213 }
214 return $points;
215 }
216
217 private function fetchResult(
218 int $question_id,
219 int $combination_id
220 ): ilPDOStatement {
221 if ($combination_id === -1) {
222 return $this->db->queryF(
223 'SELECT combination_id, points' . PHP_EOL
224 . 'FROM qpl_a_cloze_combi_res' . PHP_EOL
225 . 'WHERE question_fi = %s' . PHP_EOL
226 . 'AND best_solution=1' . PHP_EOL
227 . 'GROUP BY combination_id, points',
228 ['integer'],
229 [$question_id]
230 );
231 }
232 return $this->db->queryF(
233 'SELECT combination_id, points' . PHP_EOL
234 . 'FROM qpl_a_cloze_combi_res' . PHP_EOL
235 . 'WHERE question_fi = %s' . PHP_EOL
236 . 'AND combination_id = %s' . PHP_EOL
237 . 'AND best_solution=1' . PHP_EOL
238 . 'GROUP BY combination_id, points',
239 ['integer', 'integer'],
240 [$question_id, $combination_id]
241 );
242 }
243}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fetchResult(int $question_id, int $combination_id)
saveGapCombinationToDb(int $question_id, array $gap_combinations, array $gap_values)
getMaxPointsForCombination(int $question_id, int $combination_id=-1)
__construct(private readonly ilDBInterface $db)
importGapCombinationToDb(int $question_id, array $gap_combinations)
Class ilPDOStatement is a Wrapper Class for PDOStatement.
Interface ilDBInterface.
if(!file_exists('../ilias.ini.php'))