ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilPageQuestionProcessor.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
12{
19 public function __construct()
20 {
21 }
22
23
30 public static function saveQuestionAnswer($a_type, $a_id, $a_answer)
31 {
32 global $DIC;
33
34 $ilUser = $DIC->user();
35 $ilLog = $DIC["ilLog"];
36 $ilDB = $DIC->database();
37 //$a_type = "assOrderingQuestion";
38 //$a_id = 74;
39 //$a_answer = '{"tries":1,"wrong":2,"passed":false,"answer":[true,true,false,true,false],"interactionId":null,"choice":["1","2","5","4","3"]}';
40 $ilLog->write($a_type);
41 $ilLog->write($a_id);
42 $ilLog->write($a_answer);
43 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
44 $answer = ilJsonUtil::decode($a_answer);
45 $tries = $answer->tries;
46 $passed = $answer->passed;
47 $choice = $answer->choice;
48 $points = self::calculatePoints($a_type, $a_id, $choice);
49 $ilLog->write("Points: " . $points);
50
51 $set = $ilDB->query(
52 "SELECT * FROM page_qst_answer WHERE " .
53 " qst_id = " . $ilDB->quote($a_id, "integer") . " AND " .
54 " user_id = " . $ilDB->quote($ilUser->getId(), "integer")
55 );
56
57 /*
58 if ($rec = $ilDB->fetchAssoc($set))
59 {
60 $ilDB->manipulate("UPDATE page_qst_answer SET ".
61 " try = try + 1,".
62 " passed = ".$ilDB->quote($passed, "integer").",".
63 " points = ".$ilDB->quote($points, "float").
64 " WHERE qst_id = ".$ilDB->quote($a_id, "integer").
65 " AND user_id = ".$ilDB->quote($ilUser->getId(), "integer")
66 );
67 }
68 else
69 {
70 $ilDB->manipulate("INSERT INTO page_qst_answer ".
71 "(qst_id, user_id, try, passed, points) VALUES (".
72 $ilDB->quote($a_id, "integer").",".
73 $ilDB->quote($ilUser->getId(), "integer").",".
74 $ilDB->quote(1, "integer").",".
75 $ilDB->quote($passed, "integer").",".
76 $ilDB->quote($points, "float").
77 ")");
78 }
79 */
80
81 // #15146
82 if (!$ilDB->fetchAssoc($set)) {
83 $ilDB->replace(
84 "page_qst_answer",
85 array(
86 "qst_id" => array("integer", $a_id),
87 "user_id" => array("integer", $ilUser->getId())
88 ),
89 array(
90 "try" => array("integer", 1),
91 "passed" => array("integer", $passed),
92 "points" => array("float", $points)
93 )
94 );
95 } else {
96 $ilDB->manipulate(
97 "UPDATE page_qst_answer SET " .
98 " try = try + 1," .
99 " passed = " . $ilDB->quote($passed, "integer") . "," .
100 " points = " . $ilDB->quote($points, "float") .
101 " WHERE qst_id = " . $ilDB->quote($a_id, "integer") .
102 " AND user_id = " . $ilDB->quote($ilUser->getId(), "integer")
103 );
104 }
105 }
106
113 public static function getQuestionStatistics($a_q_id)
114 {
115 global $DIC;
116
117 $ilDB = $DIC->database();
118
119 $set = $ilDB->query(
120 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
121 " qst_id = " . $ilDB->quote($a_q_id, "integer")
122 );
123 $rec = $ilDB->fetchAssoc($set);
124 $all = $rec["usr_cnt"];
125
126 $first = false;
127 $second = false;
128 $third_or_more = false;
129
130 if ($all > 0) {
131 $set = $ilDB->query(
132 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
133 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
134 " passed = " . $ilDB->quote(1, "integer") . " AND " .
135 " try = " . $ilDB->quote(1, "integer")
136 );
137 $rec = $ilDB->fetchAssoc($set);
138 $first = $rec["usr_cnt"];
139
140 $set = $ilDB->query(
141 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
142 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
143 " passed = " . $ilDB->quote(1, "integer") . " AND " .
144 " try = " . $ilDB->quote(2, "integer")
145 );
146 $rec = $ilDB->fetchAssoc($set);
147 $second = $rec["usr_cnt"];
148
149 $set = $ilDB->query(
150 $q = "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
151 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
152 " passed = " . $ilDB->quote(1, "integer") . " AND " .
153 " try >= " . $ilDB->quote(3, "integer")
154 );
155 $rec = $ilDB->fetchAssoc($set);
156 $third_or_more = $rec["usr_cnt"];
157 }
158
159 return array("all" => $all, "first" => $first, "second" => $second, "third_or_more" => $third_or_more);
160 }
161
172 public static function calculatePoints($a_type, $a_id, $a_choice)
173 {
174 global $DIC;
175
176 $ilLog = $DIC["ilLog"];
177
178 switch ($a_type) {
179 case "assSingleChoice":
180 include_once("./Modules/TestQuestionPool/classes/class.assSingleChoice.php");
181 $q = new assSingleChoice();
182 $q->loadFromDb($a_id);
183 $points = 0;
184 foreach ($q->getAnswers() as $key => $answer) {
185 if (isset($a_choice[0]) && $key == $a_choice[0]) {
186 $points += $answer->getPoints();
187 }
188 }
189 break;
190
191 case "assMultipleChoice":
192 include_once("./Modules/TestQuestionPool/classes/class.assMultipleChoice.php");
193 $q = new assMultipleChoice();
194 $q->loadFromDb($a_id);
195 $points = 0;
196 foreach ($q->getAnswers() as $key => $answer) {
197 if (is_array($a_choice) && in_array($key, $a_choice)) {
198 $points += $answer->getPoints();
199 } else {
200 $points += $answer->getPointsUnchecked();
201 }
202 }
203 break;
204
205 case "assClozeTest":
206 include_once("./Modules/TestQuestionPool/classes/class.assClozeTest.php");
207 $q = new assClozeTest();
208 $q->loadFromDb($a_id);
209 $points = 0;
210 foreach ($q->getGaps() as $id => $gap) {
211 $choice = $a_choice[$id];
212 switch ($gap->getType()) {
213 case CLOZE_TEXT:
214 $gappoints = 0;
215 for ($order = 0; $order < $gap->getItemCount(); $order++) {
216 $answer = $gap->getItem($order);
217 $gotpoints = $q->getTextgapPoints(
218 $answer->getAnswertext(),
219 $choice,
220 $answer->getPoints()
221 );
222 if ($gotpoints > $gappoints) {
223 $gappoints = $gotpoints;
224 }
225 }
226 $points += $gappoints;
227//$ilLog->write("ct: ".$gappoints);
228 break;
229
230 case CLOZE_NUMERIC:
231 $gappoints = 0;
232 for ($order = 0; $order < $gap->getItemCount(); $order++) {
233 $answer = $gap->getItem($order);
234 $gotpoints = $q->getNumericgapPoints(
235 $answer->getAnswertext(),
236 $choice,
237 $answer->getPoints(),
238 $answer->getLowerBound(),
239 $answer->getUpperBound()
240 );
241 if ($gotpoints > $gappoints) {
242 $gappoints = $gotpoints;
243 }
244 }
245 $points += $gappoints;
246//$ilLog->write("cn: ".$gappoints);
247 break;
248
249 case CLOZE_SELECT:
250 for ($order = 0; $order < $gap->getItemCount(); $order++) {
251 $answer = $gap->getItem($order);
252 if ($choice == $answer->getOrder()) {
253 $answerpoints = $answer->getPoints();
254 $points += $answerpoints;
255 //$ilLog->write("cs: ".$answerpoints);
256 }
257 }
258 break;
259 }
260 }
261 break;
262
263 case "assMatchingQuestion":
264 include_once("./Modules/TestQuestionPool/classes/class.assMatchingQuestion.php");
265 $q = new assMatchingQuestion();
266 $q->loadFromDb($a_id);
267 $points = 0;
268 for ($i = 0; $i < $q->getMatchingPairCount(); $i++) {
269 $pair = $q->getMatchingPair($i);
270 if (is_array($a_choice) && in_array($pair->definition->identifier . "-" . $pair->term->identifier, $a_choice)) {
271 $points += $pair->points;
272 }
273 }
274 break;
275
276 case "assOrderingQuestion":
277
278 // TODO-LSD: change calculation strategy according to lsd cleanup changes
279
280 include_once("./Modules/TestQuestionPool/classes/class.assOrderingQuestion.php");
281 $q = new assOrderingQuestion();
282 $q->loadFromDb($a_id);
283 $points = 0;
284 $cnt = 1;
285 $right = true;
286 foreach ($q->getOrderElements() as $answer) {
287 if ($a_choice[$cnt - 1] != $cnt) {
288 $right = false;
289 }
290 $cnt++;
291 }
292 if ($right) {
293 $points = $q->getPoints();
294 }
295 break;
296
297 case "assImagemapQuestion":
298 include_once("./Modules/TestQuestionPool/classes/class.assImagemapQuestion.php");
299 $q = new assImagemapQuestion();
300 $q->loadFromDb($a_id);
301 $points = 0;
302
303 foreach ($q->getAnswers() as $key => $answer) {
304 if (is_array($a_choice) && in_array($key, $a_choice)) {
305 $points += $answer->getPoints();
306 }
307 }
308 break;
309
310 }
311
312 if ($points < 0) {
313 $points = 0;
314 }
315
316 return (int) $points;
317 }
318
325 public static function getAnswerStatus($a_q_id, $a_user_id = 0)
326 {
327 global $DIC;
328
329 $ilDB = $DIC->database();
330
331 $qst = (is_array($a_q_id))
332 ? $ilDB->in("qst_id", $a_q_id, false, "integer")
333 : " qst_id = " . $ilDB->quote($a_q_id, "integer");
334
335 $and = ($a_user_id > 0)
336 ? " AND user_id = " . $ilDB->quote($a_user_id, "integer")
337 : "";
338
339 $set = $ilDB->query(
340 "SELECT * FROM page_qst_answer WHERE " .
341 $qst .
342 $and
343 );
344
345 if (is_array($a_q_id) || $a_user_id == 0) {
346 $recs = array();
347 while ($rec = $ilDB->fetchAssoc($set)) {
348 $key = ($a_user_id == 0)
349 ? $rec["qst_id"] . ":" . $rec["user_id"]
350 : $rec["qst_id"];
351 $recs[$key] = $rec;
352 }
353 return $recs;
354 } else {
355 return $ilDB->fetchAssoc($set);
356 }
357 }
358
365 public static function resetTries($a_q_id, $a_user_id)
366 {
367 global $DIC;
368
369 $ilDB = $DIC->database();
370
371 $ilDB->manipulate(
372 $q = "UPDATE page_qst_answer SET " .
373 " try = " . $ilDB->quote(0, "integer") . "," .
374 " passed = " . $ilDB->quote(0, "integer") . "," .
375 " points = " . $ilDB->quote(0, "integer") . "," .
376 " unlocked = " . $ilDB->quote(0, "integer") .
377 " WHERE qst_id = " . $ilDB->quote($a_q_id, "integer") .
378 " AND user_id = " . $ilDB->quote($a_user_id, "integer")
379 );
380 }
381
388 public static function unlock($a_q_id, $a_user_id)
389 {
390 global $DIC;
391
392 $ilDB = $DIC->database();
393
394 $ilDB->manipulate(
395 $q = "UPDATE page_qst_answer SET " .
396 " unlocked = " . $ilDB->quote(1, "integer") .
397 " WHERE qst_id = " . $ilDB->quote($a_q_id, "integer") .
398 " AND user_id = " . $ilDB->quote($a_user_id, "integer")
399 );
400 }
401}
An exception for terminatinating execution or to throw for unit testing.
Class for cloze tests.
Class for image map questions.
Class for matching questions.
Class for multiple choice tests.
Class for ordering questions.
Class for single choice questions.
static decode($json_notated_string, $suppress_native=false)
static getQuestionStatistics($a_q_id)
Get statistics for question.
static resetTries($a_q_id, $a_user_id)
Reset tries.
static calculatePoints($a_type, $a_id, $a_choice)
Calculate points.
static getAnswerStatus($a_q_id, $a_user_id=0)
Get statistics for question.
static saveQuestionAnswer($a_type, $a_id, $a_answer)
Save question answer.
static unlock($a_q_id, $a_user_id)
Reset tries.
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
if(!array_key_exists('StateId', $_REQUEST)) $id
const CLOZE_NUMERIC
const CLOZE_SELECT
const CLOZE_TEXT
Cloze question constants.
global $DIC
Definition: saml.php:7
global $ilDB
$ilUser
Definition: imgupload.php:18
$a_type
Definition: workflow.php:92