ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 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  public function clearGapCombinationsFromDb($question_id)
156  {
157  global $ilDB;
158 
159  $ilDB->manipulateF( 'DELETE FROM qpl_a_cloze_combi_res WHERE question_fi = %s',
160  array( 'integer' ),
161  array( $question_id )
162  );
163  }
164 
165  public function combinationExistsForQid($question_id)
166  {
167  global $ilDB;
168 
169  $result = $ilDB->queryF('SELECT * FROM qpl_a_cloze_combi_res WHERE question_fi = %s ORDER BY gap_fi ASC',
170  array('integer'),
171  array($question_id)
172  );
173  if ($result->numRows() > 0)
174  {
175  return true;
176  }
177  else
178  {
179  return false;
180  }
181  }
182 
183  public function getGapsWhichAreUsedInCombination($question_id)
184  {
185  global $ilDB;
186 
187  $result = $ilDB->queryF('SELECT gap_fi, combination_id FROM `qpl_a_cloze_combi_res` WHERE question_fi = %s GROUP BY gap_fi, combination_id',
188  array('integer'),
189  array($question_id)
190  );
191  $gaps = array();
192  if ($result->numRows() > 0)
193  {
194  while ($data = $ilDB->fetchAssoc($result))
195  {
196  $gaps[$data['gap_fi']] = $data['combination_id'];
197  }
198  }
199  return $gaps;
200  }
201 
202  public function getMaxPointsForCombination($question_id, $combination_id = -1)
203  {
204  global $ilDB;
205 
206  if($combination_id == -1)
207  {
208  $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',
209  array('integer'),
210  array($question_id)
211  );
212  if ($result->numRows() > 0)
213  {
214  $points = 0;
215  while ($data = $ilDB->fetchAssoc($result))
216  {
217  $points += $data['points'];
218  }
219  return $points;
220  }
221  }
222  else
223  {
224  $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',
225  array('integer', 'integer'),
226  array($question_id, $combination_id)
227  );
228  if ($result->numRows() > 0)
229  {
230  $points = 0;
231  while ($data = $ilDB->fetchAssoc($result))
232  {
233  $points += $data['points'];
234  }
235  return $points;
236  }
237  }
238  return 0;
239  }
240 
241  public function getBestSolutionCombination($question_id)
242  {
243  global $ilDB, $lng;
244 
245  $result = $ilDB->queryF('SELECT * FROM qpl_a_cloze_combi_res WHERE question_fi = %s AND best_solution=1 ORDER BY gap_fi',
246  array('integer'),
247  array($question_id)
248  );
249  if ($result->numRows() > 0)
250  {
251  $return_string ='<br>';
252  $combination_id = 0;
253  $points = 0;
254  while ($data = $ilDB->fetchAssoc($result))
255  {
256  if($combination_id != $data['combination_id'])
257  {
258  $combination_id = $data['combination_id'];
259  $return_string .= $points;
260  $return_string .= '<br>';
261  $return_string .= $data['answer'].'|';
262  }
263  else
264  {
265  $return_string .= $data['answer'].'|';
266  }
267 
268  $points = ' (' . $data['points'] . ' '. $lng->txt('points') .')';
269  }
270  return rtrim($return_string , '|') . $points;
271  }
272  else
273  {
274  return 0;
275  }
276  }
277 }