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