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