ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.assClozeGapCombination.php
Go to the documentation of this file.
1<?php
2
4{
5 public function loadFromDb($question_id)
6 {
7 global $ilDB;
8 $result = $ilDB->queryF(
9 '
10 SELECT combinations.combination_id,
11 combinations.gap_fi,
12 combinations.answer,
13 combinations.row_id,
14 combinations.points,
15 combinations.best_solution,
16 combinations.question_fi,
17 cloze.cloze_type
18 FROM qpl_a_cloze_combi_res AS combinations
19 INNER JOIN qpl_a_cloze AS cloze
20 WHERE combinations.question_fi = cloze.question_fi
21 AND combinations.gap_fi = cloze.gap_id
22 AND combinations.question_fi = %s
23 ORDER BY combination_id, row_id, gap_fi ASC
24 ',
25 array('integer'),
26 array($question_id)
27 );
28
29 $return_array = array();
30 while ($data = $ilDB->fetchAssoc($result)) {
31 if (isset($return_array[$data['combination_id'] . '::' . $data['gap_fi']])) {
32 continue;
33 }
34
35 $return_array[$data['combination_id'] . '::' . $data['row_id'] . '::' . $data['gap_fi']]=array(
36 'cid' => $data['combination_id'],
37 'gap_fi' => $data['gap_fi'],
38 'answer' => $data['answer'],
39 'points' => $data['points'],
40 'row_id' => $data['row_id'],
41 'type' => $data['cloze_type'],
42 'best_solution' => $data['best_solution']
43 );
44 }
45
46 return array_values($return_array);
47 }
48
49 public function getCleanCombinationArray($question_id)
50 {
51 $assClozeGapCombinationObj = new assClozeGapCombination();
52 $combination_from_db = $assClozeGapCombinationObj->loadFromDb($question_id);
53 $clean_array = array();
54 foreach ($combination_from_db as $key => $value) {
55 $clean_array[$value['cid']][$value['row_id']][$value['gap_fi']]['answer'] = $value['answer'];
56 $clean_array[$value['cid']][$value['row_id']]['points'] = $value['points'];
57 $clean_array[$value['cid']][$value['row_id']][$value['gap_fi']]['type'] = $value['type'];
58 }
59 return $clean_array;
60 }
61
62 public function saveGapCombinationToDb($question_id, $gap_combinations, $gap_values)
63 {
64 global $ilDB;
65 $best_solutions = array();
66 for ($i = 0; $i < count($gap_combinations['points']); $i++) {
67 $highest_points = 0;
68 for ($j = 0; $j < count($gap_combinations['points'][$i]); $j++) {
69 if ($highest_points < $gap_combinations['points'][$i][$j]) {
70 $highest_points = $gap_combinations['points'][$i][$j];
71 $best_solutions[$i] = $j;
72 }
73 }
74 }
75 for ($i = 0; $i < count($gap_values); $i++) {
76 for ($j = 0; $j < count($gap_values[$i]); $j++) {
77 for ($k = 0; $k < count($gap_values[$i][$j]); $k++) {
78 if ($best_solutions[$i] == $j) {
79 $best_solution = 1;
80 } else {
81 $best_solution = 0;
82 }
83 $ilDB->manipulateF(
84 'INSERT INTO qpl_a_cloze_combi_res
85 (combination_id, question_fi, gap_fi, row_id, answer, points, best_solution) VALUES (%s, %s, %s, %s, %s, %s, %s)',
86 array(
87 'integer',
88 'integer',
89 'integer',
90 'integer',
91 'text',
92 'float',
93 'integer'
94 ),
95 array(
96 $i,
97 $question_id,
98 $gap_combinations['select'][$i][$k],
99 $j,
100 $gap_values[$i][$j][$k],
101 $gap_combinations['points'][$i][$j],
102 $best_solution
103 )
104 );
105 }
106 }
107 }
108 }
109 public static function importGapCombinationToDb($question_id, $gap_combinations)
110 {
111 global $ilDB;
112
113 foreach ($gap_combinations as $key => $row) {
114 if (is_object($row)) {
115 $row = get_object_vars($row);
116 }
117 if ($question_id != -1) {
118 $ilDB->manipulateF(
119 'INSERT INTO qpl_a_cloze_combi_res
120 (combination_id, question_fi, gap_fi, row_id, answer, points, best_solution) VALUES (%s, %s, %s, %s, %s, %s, %s)',
121 array(
122 'integer',
123 'integer',
124 'integer',
125 'integer',
126 'text',
127 'float',
128 'integer'
129 ),
130 array(
131 $row['cid'],
132 $question_id,
133 $row['gap_fi'],
134 $row['row_id'],
135 $row['answer'],
136 $row['points'],
137 $row['best_solution']
138 )
139 );
140 }
141 }
142 }
143
144 public static function clearGapCombinationsFromDb($question_id)
145 {
146 global $ilDB;
147
148 $ilDB->manipulateF(
149 'DELETE FROM qpl_a_cloze_combi_res WHERE question_fi = %s',
150 array( 'integer' ),
151 array( $question_id )
152 );
153 }
154
155 public function combinationExistsForQid($question_id)
156 {
157 global $ilDB;
158
159 $result = $ilDB->queryF(
160 'SELECT * FROM qpl_a_cloze_combi_res WHERE question_fi = %s ORDER BY gap_fi ASC',
161 array('integer'),
162 array($question_id)
163 );
164 if ($result->numRows() > 0) {
165 return true;
166 } else {
167 return false;
168 }
169 }
170
171 public function getGapsWhichAreUsedInCombination($question_id)
172 {
173 global $ilDB;
174
175 $result = $ilDB->queryF(
176 'SELECT gap_fi, combination_id FROM ' . $ilDB->quoteIdentifier('qpl_a_cloze_combi_res') . ' WHERE question_fi = %s GROUP BY gap_fi, combination_id',
177 array('integer'),
178 array($question_id)
179 );
180 $gaps = array();
181 if ($result->numRows() > 0) {
182 while ($data = $ilDB->fetchAssoc($result)) {
183 $gaps[$data['gap_fi']] = $data['combination_id'];
184 }
185 }
186 return $gaps;
187 }
188
189 public function getMaxPointsForCombination($question_id, $combination_id = -1)
190 {
191 global $ilDB;
192
193 if ($combination_id == -1) {
194 $result = $ilDB->queryF(
195 'SELECT combination_id, points FROM qpl_a_cloze_combi_res WHERE question_fi = %s AND best_solution=1 GROUP BY combination_id, points',
196 array('integer'),
197 array($question_id)
198 );
199 if ($result->numRows() > 0) {
200 $points = 0;
201 while ($data = $ilDB->fetchAssoc($result)) {
202 $points += $data['points'];
203 }
204 return $points;
205 }
206 } else {
207 $result = $ilDB->queryF(
208 'SELECT combination_id, points FROM qpl_a_cloze_combi_res WHERE question_fi = %s AND combination_id = %s AND best_solution=1 GROUP BY combination_id, points',
209 array('integer', 'integer'),
210 array($question_id, $combination_id)
211 );
212 if ($result->numRows() > 0) {
213 $points = 0;
214 while ($data = $ilDB->fetchAssoc($result)) {
215 $points += $data['points'];
216 }
217 return $points;
218 }
219 }
220 return 0;
221 }
222
223 public function getBestSolutionCombination($question_id)
224 {
225 global $ilDB, $lng;
226
227 $result = $ilDB->queryF(
228 'SELECT * FROM qpl_a_cloze_combi_res WHERE question_fi = %s AND best_solution=1 ORDER BY gap_fi',
229 array('integer'),
230 array($question_id)
231 );
232 if ($result->numRows() > 0) {
233 $return_string ='<br>';
234 $combination_id = 0;
235 $points = 0;
236 while ($data = $ilDB->fetchAssoc($result)) {
237 if ($combination_id != $data['combination_id']) {
238 $combination_id = $data['combination_id'];
239 $return_string .= $points;
240 $return_string .= '<br>';
241 $return_string .= $data['answer'] . '|';
242 } else {
243 $return_string .= $data['answer'] . '|';
244 }
245
246 $points = ' (' . $data['points'] . ' ' . $lng->txt('points') . ')';
247 }
248 return rtrim($return_string, '|') . $points;
249 } else {
250 return 0;
251 }
252 }
253}
$result
An exception for terminatinating execution or to throw for unit testing.
saveGapCombinationToDb($question_id, $gap_combinations, $gap_values)
getMaxPointsForCombination($question_id, $combination_id=-1)
static clearGapCombinationsFromDb($question_id)
static importGapCombinationToDb($question_id, $gap_combinations)
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
global $lng
Definition: privfeed.php:17
global $ilDB