ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLOTestQuestionAdapter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
5 
13 {
14  protected $settings = NULL;
15 
16  protected $user_id = 0;
17  protected $container_id = 0;
18 
19 
20 
26  public function __construct($a_user_id, $a_course_id)
27  {
28  $this->user_id = $a_user_id;
29  $this->container_id = $a_course_id;
30 
31  $this->settings = ilLOSettings::getInstanceByObjId($this->container_id);
32  }
33 
34 
35 
36 
42  public static function filterQuestions(ilTestSession $a_test_session, ilTestSequence $a_test_sequence)
43  {
44 
45  $adapter = new self(
46  $a_test_session->getUserId(),
47  $a_test_session->getObjectiveOrientedContainerId()
48  );
49  $adapter->initTestRun($a_test_session);
50  $adapter->updateQuestions($a_test_session, $a_test_sequence);
51  $adapter->hideQuestions($a_test_sequence);
52  $adapter->storeTestRun();
53  $adapter->initUserResult($a_test_session);
54 
55  // Save test sequence
56  $a_test_sequence->saveToDb();
57 
58  $GLOBALS['ilLog']->write(__METHOD__.': '.print_r($a_test_sequence,true));
59  return true;
60  }
61 
68  public static function updateObjectiveStatus(ilTestSession $a_test_session, ilTestSequence $a_test_sequence, assQuestion $a_question)
69  {
70  $adapter = new self(
71  $a_test_session->getUserId(),
72  $a_test_session->getObjectiveOrientedContainerId()
73  );
74  $adapter->initTestRun($a_test_session);
75  $adapter->updateQuestionResult($a_test_session,$a_question);
76  return true;
77 
78  /*
79  $usr_id = $a_test_session->getUserId();
80  $crs_id = $a_test_session->getObjectiveOrientedContainerId();
81 
82  $question_id = $a_question->getId();
83 
84  $points_reached = $a_question->getReachedPoints($a_test_session->getActiveId(), $a_test_session->getPass());
85  //$points_max = $a_question->getMaxPoints();
86 
87  if( $a_test_sequence instanceof ilTestSequenceFixedQuestionSet )
88  {
89  // make some noise (with question id only)
90  }
91  elseif( $a_test_sequence instanceof ilTestSequenceRandomQuestionSet )
92  {
93  $respSrcPoolDefId = $a_test_sequence->getResponsibleSourcePoolDefinitionId($question_id);
94 
95  // make some noise (with question id and responsible source pool definition)
96  }
97  */
98  }
99 
100  protected function getUserId()
101  {
102  return $this->user_id;
103  }
104 
105  protected function getContainerId()
106  {
107  return $this->container_id;
108  }
109 
114  protected function getSettings()
115  {
116  return $this->settings;
117  }
118 
122  protected function initUserResult(ilTestSession $session)
123  {
124  // check if current test is start object and fullfilled
125  // if yes => do not increase tries.
126  $is_qualified_run = false;
127  if($this->isQualifiedStartRun($session))
128  {
129  $is_qualified_run = true;
130  }
131 
132  foreach($this->run as $run)
133  {
134  include_once './Modules/Course/classes/Objectives/class.ilLOUserResults.php';
135  include_once './Modules/Course/classes/Objectives/class.ilLOUtils.php';
136 
137  $old_result = ilLOUserResults::lookupResult(
138  $this->container_id,
139  $this->user_id,
140  $run->getObjectiveId(),
141  ($this->getSettings()->getQualifiedTest() == $session->getRefId()) ?
144  );
145 
146  include_once './Modules/Course/classes/Objectives/class.ilLOUtils.php';
147 
149  $this->container_id,
150  $run->getObjectiveId(),
151  ($this->getSettings()->getQualifiedTest() == $session->getRefId()) ?
154  $run->getMaxPoints()
155  );
156 
157  $max_attempts = ilLOUtils::lookupMaxAttempts($this->container_id, $run->getObjectiveId());
158  if($max_attempts)
159  {
160  // check if current test is start object and fullfilled
161  // if yes => do not increase tries.
162  $GLOBALS['ilLog']->write(__METHOD__.': check for qualified...');
163  if(!$is_qualified_run)
164  {
165  $GLOBALS['ilLog']->write(__METHOD__.': and increase attempts');
166  ++$old_result['tries'];
167  }
168  $old_result['is_final'] = ($old_result['tries'] >= $max_attempts);
169  }
170 
171  $ur = new ilLOUserResults($this->container_id,$this->user_id);
172  $ur->saveObjectiveResult(
173  $run->getObjectiveId(),
174  ($this->getSettings()->getQualifiedTest() == $session->getRefId()) ?
176  ilLOUserResults::TYPE_INITIAL,
177  $old_result['status'],
178  $old_result['result_perc'],
179  $limit,
180  $old_result['tries'],
181  $old_result['is_final']
182  );
183  }
184  }
185 
191  protected function isQualifiedStartRun(ilTestSession $session)
192  {
193  if($session->getRefId() == $this->getSettings()->getInitialTest())
194  {
195  $GLOBALS['ilLog']->write(__METHOD__.': is intial');
196  return false;
197  }
198  if($session->getRefId() != $this->getSettings()->getQualifiedTest())
199  {
200  $GLOBALS['ilLog']->write(__METHOD__.': is not qualified');
201  return false;
202  }
203  include_once './Services/Container/classes/class.ilContainerStartObjects.php';
205  {
206  $GLOBALS['ilLog']->write(__METHOD__.': no start object');
207  return false;
208  }
209  // Check if start object is fullfilled
210 
211  $container_ref_ids = ilObject::_getAllReferences($this->getContainerId());
212  $container_ref_id = end($container_ref_ids);
213 
214  $start = new ilContainerStartObjects(
215  $container_ref_id,
216  $this->getContainerId()
217  );
218  if($start->isFullfilled($this->getUserId(),$session->getRefId()))
219  {
220  $GLOBALS['ilLog']->write(__METHOD__.': is fullfilled');
221 
222  return false;
223  }
224  $GLOBALS['ilLog']->write(__METHOD__.': is not fullfilled');
225  return true;
226  }
227 
233  protected function updateQuestionResult(ilTestSession $session, assQuestion $qst)
234  {
235  foreach($this->run as $run)
236  {
237  if($run->questionExists($qst->getId()))
238  {
239  $GLOBALS['ilLog']->write(__METHOD__.': reached points are '.$qst->getReachedPoints($session->getActiveId(),$session->getPass()));
240  $run->setQuestionResult(
241  $qst->getId(),
242  $qst->getReachedPoints($session->getActiveId(),$session->getPass())
243  );
244  $run->update();
245 
246  $res = $run->getResult();
247 
248  include_once './Modules/Course/classes/Objectives/class.ilLOUserResults.php';
249  include_once './Modules/Course/classes/Objectives/class.ilLOUtils.php';
250 
251  $old_result = ilLOUserResults::lookupResult(
252  $this->container_id,
253  $this->user_id,
254  $run->getObjectiveId(),
255  ($this->getSettings()->getQualifiedTest() == $session->getRefId()) ?
258  );
259 
260  $ur = new ilLOUserResults($this->container_id,$this->user_id);
261  $ur->saveObjectiveResult(
262  $run->getObjectiveId(),
263  ($this->getSettings()->getQualifiedTest() == $session->getRefId()) ?
265  ilLOUserResults::TYPE_INITIAL,
267  $this->container_id,
268  $session->getRefId(),
269  $run->getObjectiveId(),
270  $res['max'],$res['reached'],$old_result['limit_perc']) ?
273  (int) $res['percentage'],
274  $old_result['limit_perc'],
275  $old_result['tries'],
276  $old_result['is_final']
277  );
278  $GLOBALS['ilLog']->write(__METHOD__.': '.print_r($run->getResult(),true));
279 
280  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
281  ilLPStatusWrapper::_updateStatus($this->container_id,$this->user_id);
282  }
283  }
284  return false;
285  }
286 
287 
292  protected function hideQuestions(ilTestSequence $seq)
293  {
294  // first unhide all questions
295  $seq->clearHiddenQuestions();
296  foreach($seq->getQuestionIds() as $qid)
297  {
298  if(!$this->isInRun($qid))
299  {
300  $seq->hideQuestion($qid);
301  }
302  }
303  }
304 
305 
306  protected function initTestRun(ilTestSession $session)
307  {
308  include_once './Modules/Course/classes/Objectives/class.ilLOTestRun.php';
309  $this->run = ilLOTestRun::getRun(
310  $this->container_id,
311  $this->user_id,
312  ilObject::_lookupObjId($session->getRefId())
313  );
314  #$GLOBALS['ilLog']->write(__METHOD__.': '.print_r($this->run,true));
315  }
316 
320  protected function storeTestRun()
321  {
322  foreach ($this->run as $tst_run)
323  {
324  $tst_run->update();
325  }
326  }
327 
328 
329  protected function updateQuestions(ilTestSession $session, ilTestSequence $seq)
330  {
331  if($seq instanceof ilTestSequenceFixedQuestionSet)
332  {
333  $this->updateFixedQuestions($session, $seq);
334  }
335  if($seq instanceof ilTestSequenceRandomQuestionSet)
336  {
337  $this->updateRandomQuestions($session, $seq);
338  }
339 
340  }
341 
342  protected function updateFixedQuestions(ilTestSession $session, ilTestSequence $seq)
343  {
344  foreach ($this->run as $tst_run)
345  {
346  $tst_run->clearQuestions();
347  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
349  ilObject::_lookupObjId($session->getRefId()),
350  $tst_run->getObjectiveId()
351  );
352  $GLOBALS['ilLog']->write(__METHOD__.': '.print_r($qst,true));
353  $points = 0;
354  foreach($qst as $id)
355  {
356  $tst_run->addQuestion($id);
358  }
359  $tst_run->setMaxPoints($points);
360  }
361  }
362 
364  {
365  include_once './Modules/Course/classes/Objectives/class.ilLORandomTestQuestionPools.php';
366  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
367 
368  foreach($this->run as $tst_run)
369  {
370  // Clear questions of previous run
371  $tst_run->clearQuestions();
372  $rnd = new ilLORandomTestQuestionPools(
373  $this->container_id,
374  $tst_run->getObjectiveId(),
375  ($this->getSettings()->getQualifiedTest() == $session->getRefId() ?
378  );
379  $stored_sequence_id = $rnd->getQplSequence();
380  $points = 0;
381  foreach($seq->getQuestionIds() as $qst)
382  {
383  if($stored_sequence_id == $seq->getResponsibleSourcePoolDefinitionId($qst))
384  {
385  $tst_run->addQuestion($qst);
387  }
388  }
389  $tst_run->setMaxPoints($points);
390  }
391  }
392 
393  protected function isInRun($a_qid)
394  {
395  foreach($this->run as $run)
396  {
397  if($run->questionExists($a_qid))
398  {
399  return true;
400  }
401  }
402  return false;
403  }
404 
405 
406  private static function getQuestionData($testObjId, $questionIds)
407  {
408  global $ilDB, $lng, $ilPluginAdmin;
409 
410  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionList.php';
411  $questionList = new ilAssQuestionList($ilDB, $lng, $ilPluginAdmin, $testObjId);
412 
413  $questionList->setQuestionInstanceTypeFilter(ilAssQuestionList::QUESTION_INSTANCE_TYPE_DUPLICATES);
414  $questionList->setQuestionIdsFilter($questionIds);
415 
416  $questionList->load();
417 
418  return $questionList->getQuestionDataArray();
419  }
420 }
421 ?>