ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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  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 = ilPageQuestionProcessor::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  if ($rec = $ilDB->fetchAssoc($set))
53  {
54  $ilDB->manipulate("UPDATE page_qst_answer SET ".
55  " try = try + 1,".
56  " passed = ".$ilDB->quote($passed, "integer").",".
57  " points = ".$ilDB->quote($points, "float").
58  " WHERE qst_id = ".$ilDB->quote($a_id, "integer").
59  " AND user_id = ".
60  $ilDB->quote($ilUser->getId(), "integer")
61  );
62  }
63  else
64  {
65  $ilDB->manipulate("INSERT INTO page_qst_answer ".
66  "(qst_id, user_id, try, passed, points) VALUES (".
67  $ilDB->quote($a_id, "integer").",".
68  $ilDB->quote($ilUser->getId(), "integer").",".
69  $ilDB->quote(1, "integer").",".
70  $ilDB->quote($passed, "integer").",".
71  $ilDB->quote($points, "float").
72  ")");
73  }
74 
75  //$ilLog->write("tries: ".$tries);
76  //$ilLog->write("passed: ".$passed);
77  //$ilLog->write("points: ".$points);
78 
79  exit;
80  }
81 
88  static function getQuestionStatistics($a_q_id)
89  {
90  global $ilDB;
91 
92  $set = $ilDB->query("SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE ".
93  " qst_id = ".$ilDB->quote($a_q_id, "integer")
94  );
95  $rec = $ilDB->fetchAssoc($set);
96  $all = $rec["usr_cnt"];
97 
98  $first = false;
99  $second = false;
100  $third_or_more = false;
101 
102  if ($all > 0)
103  {
104  $set = $ilDB->query("SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE ".
105  " qst_id = ".$ilDB->quote($a_q_id, "integer")." AND ".
106  " passed = ".$ilDB->quote(1, "integer")." AND ".
107  " try = ".$ilDB->quote(1, "integer")
108  );
109  $rec = $ilDB->fetchAssoc($set);
110  $first = $rec["usr_cnt"];
111 
112  $set = $ilDB->query("SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE ".
113  " qst_id = ".$ilDB->quote($a_q_id, "integer")." AND ".
114  " passed = ".$ilDB->quote(1, "integer")." AND ".
115  " try = ".$ilDB->quote(2, "integer")
116  );
117  $rec = $ilDB->fetchAssoc($set);
118  $second = $rec["usr_cnt"];
119 
120  $set = $ilDB->query($q = "SELECT count(user_id) usr_cnt FROM page_qst_answer WHERE ".
121  " qst_id = ".$ilDB->quote($a_q_id, "integer")." AND ".
122  " passed = ".$ilDB->quote(1, "integer")." AND ".
123  " try >= ".$ilDB->quote(3, "integer")
124  );
125  $rec = $ilDB->fetchAssoc($set);
126  $third_or_more = $rec["usr_cnt"];
127  }
128 
129  return array("all" => $all, "first" => $first, "second" => $second, "third_or_more" => $third_or_more);
130  }
131 
142  function calculatePoints($a_type, $a_id, $a_choice)
143  {
144  global $ilLog;
145 
146  switch ($a_type)
147  {
148  case "assSingleChoice":
149  include_once("./Modules/TestQuestionPool/classes/class.assSingleChoice.php");
150  $q = new assSingleChoice();
151  $q->loadFromDb($a_id);
152  $points = 0;
153  foreach ($q->getAnswers() as $key => $answer)
154  {
155  if (isset($a_choice[0]) && $key == $a_choice[0])
156  {
157  $points += $answer->getPoints();
158  }
159  }
160  break;
161 
162  case "assMultipleChoice":
163  include_once("./Modules/TestQuestionPool/classes/class.assMultipleChoice.php");
164  $q = new assMultipleChoice();
165  $q->loadFromDb($a_id);
166  $points = 0;
167  foreach ($q->getAnswers() as $key => $answer)
168  {
169  if (is_array($a_choice) && in_array($key, $a_choice))
170  {
171  $points += $answer->getPoints();
172  }
173  else
174  {
175  $points += $answer->getPointsUnchecked();
176  }
177  }
178  break;
179 
180  case "assClozeTest":
181  include_once("./Modules/TestQuestionPool/classes/class.assClozeTest.php");
182  $q = new assClozeTest();
183  $q->loadFromDb($a_id);
184  $points = 0;
185  foreach ($q->getGaps() as $id => $gap)
186  {
187  $choice = $a_choice[$id];
188  switch ($gap->getType())
189  {
190  case CLOZE_TEXT:
191  $gappoints = 0;
192  for ($order = 0; $order < $gap->getItemCount(); $order++)
193  {
194  $answer = $gap->getItem($order);
195  $gotpoints = $q->getTextgapPoints($answer->getAnswertext(),
196  $choice, $answer->getPoints());
197  if ($gotpoints > $gappoints) $gappoints = $gotpoints;
198  }
199  $points += $gappoints;
200 //$ilLog->write("ct: ".$gappoints);
201  break;
202 
203  case CLOZE_NUMERIC:
204  $gappoints = 0;
205  for ($order = 0; $order < $gap->getItemCount(); $order++)
206  {
207  $answer = $gap->getItem($order);
208  $gotpoints = $q->getNumericgapPoints($answer->getAnswertext(),
209  $choice, $answer->getPoints(),
210  $answer->getLowerBound(), $answer->getUpperBound());
211  if ($gotpoints > $gappoints) $gappoints = $gotpoints;
212  }
213  $points += $gappoints;
214 //$ilLog->write("cn: ".$gappoints);
215  break;
216 
217  case CLOZE_SELECT:
218  for ($order = 0; $order < $gap->getItemCount(); $order++)
219  {
220  $answer = $gap->getItem($order);
221  if ($choice == $answer->getOrder())
222  {
223  $answerpoints = $answer->getPoints();
224  $points += $answerpoints;
225 //$ilLog->write("cs: ".$answerpoints);
226  }
227  }
228  break;
229  }
230  }
231  break;
232 
233  case "assMatchingQuestion":
234  include_once("./Modules/TestQuestionPool/classes/class.assMatchingQuestion.php");
235  $q = new assMatchingQuestion();
236  $q->loadFromDb($a_id);
237  $points = 0;
238  for ($i = 0; $i < $q->getMatchingPairCount(); $i++)
239  {
240  $pair = $q->getMatchingPair($i);
241  if (is_array($a_choice) && in_array($pair->definition->identifier."-".$pair->term->identifier, $a_choice))
242  {
243  $points += $pair->points;
244  }
245  }
246  break;
247 
248  case "assOrderingQuestion":
249  include_once("./Modules/TestQuestionPool/classes/class.assOrderingQuestion.php");
250  $q = new assOrderingQuestion();
251  $q->loadFromDb($a_id);
252  $points = 0;
253  $cnt = 1;
254  $right = true;
255  foreach ($q->getAnswers() as $answer)
256  {
257  if ($a_choice[$cnt - 1] != $cnt)
258  {
259  $right = false;
260  }
261  $cnt++;
262  }
263  if ($right)
264  {
265  $points = $q->getPoints();
266  }
267  break;
268 
269  case "assImagemapQuestion":
270  include_once("./Modules/TestQuestionPool/classes/class.assImagemapQuestion.php");
271  $q = new assImagemapQuestion();
272  $q->loadFromDb($a_id);
273  $points = 0;
274 
275  foreach ($q->getAnswers() as $key => $answer)
276  {
277  if (is_array($a_choice) && in_array($key, $a_choice))
278  {
279  $points += $answer->getPoints();
280  }
281  }
282  break;
283 
284  }
285 
286  if ($points < 0)
287  {
288  $points = 0;
289  }
290 
291  return (int) $points;
292  }
293 }
294 ?>