ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPageQuestionProcessor.php
Go to the documentation of this file.
1<?php
2
25{
26 public function __construct()
27 {
28 }
29
30 public static function saveQuestionAnswer(
31 string $a_type,
32 int $a_id,
33 string $a_answer
34 ): void {
35 global $DIC;
36
37 $ilUser = $DIC->user();
38 $ilLog = $DIC["ilLog"];
39 $ilDB = $DIC->database();
40 $ilLog->write($a_type);
41 $ilLog->write($a_id);
42 $ilLog->write($a_answer);
43 $answer = json_decode($a_answer, false, 512, JSON_THROW_ON_ERROR);
44 $passed = $answer->passed;
45 $choice = $answer->choice ?? [];
46 $points = self::calculatePoints($a_type, $a_id, $choice);
47 $ilLog->write("Points: " . $points);
48
49 $set = $ilDB->query(
50 "SELECT * FROM page_qst_answer WHERE " .
51 " qst_id = " . $ilDB->quote($a_id, "integer") . " AND " .
52 " user_id = " . $ilDB->quote($ilUser->getId(), "integer")
53 );
54
55 // #15146
56 if (!$ilDB->fetchAssoc($set)) {
57 $ilDB->replace(
58 "page_qst_answer",
59 array(
60 "qst_id" => array("integer", $a_id),
61 "user_id" => array("integer", $ilUser->getId())
62 ),
63 array(
64 "try" => array("integer", 1),
65 "passed" => array("integer", $passed),
66 "points" => array("float", $points)
67 )
68 );
69 } else {
70 $ilDB->manipulate(
71 "UPDATE page_qst_answer SET " .
72 " try = try + 1," .
73 " passed = " . $ilDB->quote($passed, "integer") . "," .
74 " points = " . $ilDB->quote($points, "float") .
75 " WHERE qst_id = " . $ilDB->quote($a_id, "integer") .
76 " AND user_id = " . $ilDB->quote($ilUser->getId(), "integer")
77 );
78 }
79 }
80
81 public static function getQuestionStatistics(
82 int $a_q_id
83 ): array {
84 global $DIC;
85
86 $ilDB = $DIC->database();
87
88 $set = $ilDB->query(
89 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
90 " qst_id = " . $ilDB->quote($a_q_id, "integer")
91 );
92 $rec = $ilDB->fetchAssoc($set);
93 $all = $rec["usr_cnt"];
94
95 $first = false;
96 $second = false;
97 $third_or_more = false;
98
99 if ($all > 0) {
100 $set = $ilDB->query(
101 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
102 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
103 " passed = " . $ilDB->quote(1, "integer") . " AND " .
104 " try = " . $ilDB->quote(1, "integer")
105 );
106 $rec = $ilDB->fetchAssoc($set);
107 $first = $rec["usr_cnt"];
108
109 $set = $ilDB->query(
110 "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
111 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
112 " passed = " . $ilDB->quote(1, "integer") . " AND " .
113 " try = " . $ilDB->quote(2, "integer")
114 );
115 $rec = $ilDB->fetchAssoc($set);
116 $second = $rec["usr_cnt"];
117
118 $set = $ilDB->query(
119 $q = "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE " .
120 " qst_id = " . $ilDB->quote($a_q_id, "integer") . " AND " .
121 " passed = " . $ilDB->quote(1, "integer") . " AND " .
122 " try >= " . $ilDB->quote(3, "integer")
123 );
124 $rec = $ilDB->fetchAssoc($set);
125 $third_or_more = $rec["usr_cnt"];
126 }
127
128 return array("all" => $all, "first" => $first, "second" => $second, "third_or_more" => $third_or_more);
129 }
130
138 public static function calculatePoints(
139 string $a_type,
140 int $a_id,
141 array $a_choice
142 ): int {
143 $points = 0;
144
145 switch ($a_type) {
146 case "assSingleChoice":
147 $q = new assSingleChoice();
148 $q->loadFromDb($a_id);
149 $points = 0;
150 foreach ($q->getAnswers() as $key => $answer) {
151 if (isset($a_choice[0]) && $key == $a_choice[0]) {
152 $points += $answer->getPoints();
153 }
154 }
155 break;
156
157 case "assMultipleChoice":
158 $q = new assMultipleChoice();
159 $q->loadFromDb($a_id);
160 $points = 0;
161 foreach ($q->getAnswers() as $key => $answer) {
162 if (is_array($a_choice) && in_array($key, $a_choice)) {
163 $points += $answer->getPoints();
164 } else {
165 $points += $answer->getPointsUnchecked();
166 }
167 }
168 break;
169
170 case "assClozeTest":
171 $q = new assClozeTest();
172 $q->loadFromDb($a_id);
173 $points = 0;
174 foreach ($q->getGaps() as $id => $gap) {
175 $choice = $a_choice[$id];
176 switch ($gap->getType()) {
178 $gappoints = 0;
179 for ($order = 0; $order < $gap->getItemCount(); $order++) {
180 $answer = $gap->getItem($order);
181 $gotpoints = $q->getTextgapPoints(
182 $answer->getAnswertext(),
183 $choice,
184 $answer->getPoints()
185 );
186 if ($gotpoints > $gappoints) {
187 $gappoints = $gotpoints;
188 }
189 }
190 $points += $gappoints;
191 //$ilLog->write("ct: ".$gappoints);
192 break;
193
195 $gappoints = 0;
196 for ($order = 0; $order < $gap->getItemCount(); $order++) {
197 $answer = $gap->getItem($order);
198 $gotpoints = $q->getNumericgapPoints(
199 $answer->getAnswertext(),
200 $choice,
201 $answer->getPoints(),
202 $answer->getLowerBound(),
203 $answer->getUpperBound()
204 );
205 if ($gotpoints > $gappoints) {
206 $gappoints = $gotpoints;
207 }
208 }
209 $points += $gappoints;
210 //$ilLog->write("cn: ".$gappoints);
211 break;
212
214 for ($order = 0; $order < $gap->getItemCount(); $order++) {
215 $answer = $gap->getItem($order);
216 if ($choice == $answer->getOrder()) {
217 $answerpoints = $answer->getPoints();
218 $points += $answerpoints;
219 //$ilLog->write("cs: ".$answerpoints);
220 }
221 }
222 break;
223 }
224 }
225 break;
226
227 case "assMatchingQuestion":
228 $q = new assMatchingQuestion();
229 $q->loadFromDb($a_id);
230 $points = 0;
231 for ($i = 0; $i < $q->getMatchingPairCount(); $i++) {
232 $pair = $q->getMatchingPair($i);
233 if (is_array($a_choice) && in_array($pair->getDefinition()->getIdentifier() . "-" . $pair->getTerm()->getIdentifier(), $a_choice)) {
234 $points += $pair->points;
235 }
236 }
237 break;
238
239 case "assOrderingQuestion":
240
241 // TODO-LSD: change calculation strategy according to lsd cleanup changes
242
243 $q = new assOrderingQuestion();
244 $q->loadFromDb($a_id);
245 $points = 0;
246 $cnt = 1;
247 $right = true;
248 foreach ($q->getOrderElements() as $answer) {
249 if ($a_choice[$cnt - 1] != $cnt) {
250 $right = false;
251 }
252 $cnt++;
253 }
254 if ($right) {
255 $points = $q->getPoints();
256 }
257 break;
258
259 case "assImagemapQuestion":
260 $q = new assImagemapQuestion();
261 $q->loadFromDb($a_id);
262 $points = 0;
263
264 foreach ($q->getAnswers() as $key => $answer) {
265 if (is_array($a_choice) && in_array($key, $a_choice)) {
266 $points += $answer->getPoints();
267 }
268 }
269 break;
270 }
271
272 if ($points < 0) {
273 $points = 0;
274 }
275
276 return (int) $points;
277 }
278
282 public static function getAnswerStatus(
283 $a_q_id,
284 int $a_user_id = 0
285 ): array {
286 global $DIC;
287
288 $ilDB = $DIC->database();
289
290 $qst = (is_array($a_q_id))
291 ? $ilDB->in("qst_id", $a_q_id, false, "integer")
292 : " qst_id = " . $ilDB->quote($a_q_id, "integer");
293
294 $and = ($a_user_id > 0)
295 ? " AND user_id = " . $ilDB->quote($a_user_id, "integer")
296 : "";
297
298 $set = $ilDB->query(
299 "SELECT * FROM page_qst_answer WHERE " .
300 $qst .
301 $and
302 );
303
304 if (is_array($a_q_id) || $a_user_id == 0) {
305 $recs = array();
306 while ($rec = $ilDB->fetchAssoc($set)) {
307 $key = ($a_user_id == 0)
308 ? $rec["qst_id"] . ":" . $rec["user_id"]
309 : $rec["qst_id"];
310 $recs[$key] = $rec;
311 }
312 return $recs;
313 }
314
315 if ($rec = $ilDB->fetchAssoc($set)) {
316 return $rec;
317 }
318 return [
319 "try" => 0,
320 "passed" => false
321 ];
322 }
323
327 public static function resetTries(
328 int $a_q_id,
329 int $a_user_id
330 ): void {
331 global $DIC;
332
333 $ilDB = $DIC->database();
334
335 $ilDB->manipulate(
336 $q = "UPDATE page_qst_answer SET " .
337 " try = " . $ilDB->quote(0, "integer") . "," .
338 " passed = " . $ilDB->quote(0, "integer") . "," .
339 " points = " . $ilDB->quote(0, "integer") . "," .
340 " unlocked = " . $ilDB->quote(0, "integer") .
341 " WHERE qst_id = " . $ilDB->quote($a_q_id, "integer") .
342 " AND user_id = " . $ilDB->quote($a_user_id, "integer")
343 );
344 }
345
349 public static function unlock(
350 int $a_q_id,
351 int $a_user_id
352 ): void {
353 global $DIC;
354
355 $ilDB = $DIC->database();
356
357 $ilDB->manipulate(
358 $q = "UPDATE page_qst_answer SET " .
359 " unlocked = " . $ilDB->quote(1, "integer") .
360 " WHERE qst_id = " . $ilDB->quote($a_q_id, "integer") .
361 " AND user_id = " . $ilDB->quote($a_user_id, "integer")
362 );
363 }
364}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static saveQuestionAnswer(string $a_type, int $a_id, string $a_answer)
static unlock(int $a_q_id, int $a_user_id)
Unlock question for user.
static getAnswerStatus( $a_q_id, int $a_user_id=0)
static resetTries(int $a_q_id, int $a_user_id)
Reset tries for user and question.
static calculatePoints(string $a_type, int $a_id, array $a_choice)
Calculate points.
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23