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