ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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  include_once("./Modules/TestQuestionPool/classes/class.assOrderingQuestion.php");
272  $q = new assOrderingQuestion();
273  $q->loadFromDb($a_id);
274  $points = 0;
275  $cnt = 1;
276  $right = true;
277  foreach ($q->getAnswers() as $answer)
278  {
279  if ($a_choice[$cnt - 1] != $cnt)
280  {
281  $right = false;
282  }
283  $cnt++;
284  }
285  if ($right)
286  {
287  $points = $q->getPoints();
288  }
289  break;
290 
291  case "assImagemapQuestion":
292  include_once("./Modules/TestQuestionPool/classes/class.assImagemapQuestion.php");
293  $q = new assImagemapQuestion();
294  $q->loadFromDb($a_id);
295  $points = 0;
296 
297  foreach ($q->getAnswers() as $key => $answer)
298  {
299  if (is_array($a_choice) && in_array($key, $a_choice))
300  {
301  $points += $answer->getPoints();
302  }
303  }
304  break;
305 
306  }
307 
308  if ($points < 0)
309  {
310  $points = 0;
311  }
312 
313  return (int) $points;
314  }
315 
322  static function getAnswerStatus($a_q_id, $a_user_id = 0)
323  {
324  global $ilDB;
325 
326  $qst = (is_array($a_q_id))
327  ? $ilDB->in("qst_id", $a_q_id, false, "integer")
328  : " qst_id = ".$ilDB->quote($a_q_id, "integer");
329 
330  $and = ($a_user_id > 0)
331  ? " AND user_id = ".$ilDB->quote($a_user_id, "integer")
332  : "";
333 
334  $set = $ilDB->query("SELECT * FROM page_qst_answer WHERE ".
335  $qst.
336  $and
337  );
338 
339  if (is_array($a_q_id))
340  {
341  $recs = array();
342  while ($rec = $ilDB->fetchAssoc($set))
343  {
344  $recs[$rec["qst_id"]] = $rec;
345  }
346  return $recs;
347  }
348  else
349  {
350  return $ilDB->fetchAssoc($set);
351  }
352  }
353 
360  static function resetTries($a_q_id, $a_user_id)
361  {
362  global $ilDB;
363 
364  $ilDB->manipulate($q = "UPDATE page_qst_answer SET ".
365  " try = ".$ilDB->quote(0, "integer").",".
366  " passed = ".$ilDB->quote(0, "integer").",".
367  " points = ".$ilDB->quote(0, "integer").",".
368  " unlocked = ".$ilDB->quote(0, "integer").
369  " WHERE qst_id = ".$ilDB->quote($a_q_id, "integer").
370  " AND user_id = ".$ilDB->quote($a_user_id, "integer")
371  );
372  }
373 
380  static function unlock($a_q_id, $a_user_id)
381  {
382  global $ilDB;
383 
384  $ilDB->manipulate($q = "UPDATE page_qst_answer SET ".
385  " unlocked = ".$ilDB->quote(1, "integer").
386  " WHERE qst_id = ".$ilDB->quote($a_q_id, "integer").
387  " AND user_id = ".$ilDB->quote($a_user_id, "integer")
388  );
389  }
390 
391 
392 }
393 ?>
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.
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)
Class for image map questions.
global $ilUser
Definition: imgupload.php:15
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.