ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjectiveResult.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 define('IL_OBJECTIVE_STATUS_EMPTY','empty');
6 define('IL_OBJECTIVE_STATUS_PRETEST','pretest');
7 define('IL_OBJECTIVE_STATUS_FINAL','final');
8 define('IL_OBJECTIVE_STATUS_NONE','none');
9 define('IL_OBJECTIVE_STATUS_FINISHED','finished');
10 define('IL_OBJECTIVE_STATUS_PRETEST_NON_SUGGEST','pretest_non_suggest');
11 
20 {
21  var $db = null;
22  var $user_id = null;
23 
24 
25  function ilCourseObjectiveResult($a_usr_id)
26  {
27  global $ilDB;
28 
29  $this->db =& $ilDB;
30 
31  $this->user_id = $a_usr_id;
32  }
33  function getUserId()
34  {
35  return $this->user_id;
36  }
37 
38  function getAccomplished($a_crs_id)
39  {
40  return ilCourseObjectiveResult::_getAccomplished($this->getUserId(),$a_crs_id);
41  }
42  function _getAccomplished($a_user_id,$a_crs_id)
43  {
44  global $ilDB;
45 
46  include_once 'Modules/Course/classes/class.ilCourseObjective.php';
47  // begin-patch lok
48  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id,true);
49  // end-patch lok
50 
51  if(!is_array($objectives))
52  {
53  return array();
54  }
55  $query = "SELECT objective_id FROM crs_objective_status ".
56  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
57  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
58  $res = $ilDB->query($query);
59  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
60  {
61  $accomplished[] = $row->objective_id;
62  }
63  return $accomplished ? $accomplished : array();
64  }
65 
66  function getSuggested($a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
67  {
68  return ilCourseObjectiveResult::_getSuggested($this->getUserId(),$a_crs_id,$a_status);
69  }
70 
71  function _getSuggested($a_user_id,$a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
72  {
73  global $ilDB;
74 
75  include_once './Modules/Course/classes/class.ilCourseObjective.php';
76  // begin-patch lok
77  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id,true);
78  // end-patch lok
79 
80  $finished = array();
81  if($a_status == IL_OBJECTIVE_STATUS_FINAL or
82  $a_status == IL_OBJECTIVE_STATUS_FINISHED)
83  {
84  // check finished
85  $query = "SELECT objective_id FROM crs_objective_status ".
86  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer')." ".
87  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
88  $res = $ilDB->query($query);
89  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
90  {
91  $finished[] = $row->objective_id;
92  }
93  }
94  else
95  {
96  // Pretest
97  $query = "SELECT objective_id FROM crs_objective_status_p ".
98  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
99  "AND user_id = ".$ilDB->quote($a_user_id ,'integer');
100  $res = $ilDB->query($query);
101  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
102  {
103  $finished[] = $row->objective_id;
104  }
105  }
106  foreach($objectives as $objective_id)
107  {
108  if(!in_array($objective_id,$finished))
109  {
110  $suggested[] = $objective_id;
111  }
112  }
113  return $suggested ? $suggested : array();
114  }
115 
122  public static function getSuggestedQuestions($a_usr_id,$a_crs_id)
123  {
124  foreach(self::_getSuggested($a_usr_id,$a_crs_id) as $objective_id)
125  {
126  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
127  $obj = new ilCourseObjectiveQuestion($objective_id);
128  foreach($obj->getFinalTestQuestions() as $qst)
129  {
130  $qsts[] = $qst['question_id'];
131  }
132  }
133  return $qsts ? $qsts : array();
134  }
135 
136  protected function resetTestForUser(ilObjTest $a_test, $a_user_id)
137  {
138  // this is done in ilTestLP (see below)
139  // $a_test->removeTestResultsForUser($a_user_id);
140 
141  // #15038
142  include_once "Modules/Test/classes/class.ilTestLP.php";
143  $test_lp = ilTestLP::getInstance($a_test->getId());
144  $test_lp->resetLPDataForUserIds(array($a_user_id));
145 
146  // #15205 - see ilObjTestGUI::confirmDeleteSelectedUserDataObject()
147  $active_id = $a_test->getActiveIdOfUser($a_user_id);
148  if($active_id)
149  {
150  $a_test->removeTestActives(array($active_id));
151  }
152  }
153 
154  function reset($a_course_id)
155  {
156  global $ilDB;
157 
158  include_once './Modules/Course/classes/class.ilCourseObjective.php';
159  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
160 
161  include_once './Services/Object/classes/class.ilObjectFactory.php';
162  $factory = new ilObjectFactory();
163 
164  include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
165  $initial = ilLOSettings::getInstanceByObjId($a_course_id)->getInitialTest();
166  $initial_tst = $factory->getInstanceByRefId($initial, FALSE);
167  if($initial_tst instanceof ilObjTest)
168  {
169  $this->resetTestForUser($initial_tst, $this->getUserId());
170  }
171 
172  $qualified = ilLOSettings::getInstanceByObjId($a_course_id)->getQualifiedTest();
173  $qualified_tst = $factory->getInstanceByRefId($qualified, FALSE);
174  if($qualified_tst instanceof ilObjTest)
175  {
176  $this->resetTestForUser($qualified_tst, $this->getUserId());
177  }
178 
179  $objectives = ilCourseObjective::_getObjectiveIds($a_course_id,FALSE);
180 
181  if(count($objectives))
182  {
183  $query = "DELETE FROM crs_objective_status ".
184  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
185  "AND user_id = ".$ilDB->quote($this->getUserId() ,'integer')." ";
186  $res = $ilDB->manipulate($query);
187 
188  $query = "DELETE FROM crs_objective_status_p ".
189  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
190  "AND user_id = ".$ilDB->quote($this->getUserId())."";
191  $res = $ilDB->manipulate($query);
192 
193  $query = "DELETE FROM ilLOUserResults ".
194  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
195  "AND user_id = ".$ilDB->quote($this->getUserId())."";
196  }
197 
198  // update/reset LP for course
199  include_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
200  ilLPStatusWrapper::_updateStatus($a_course_id, $this->getUserId());
201 
202  return true;
203  }
204 
205  function getStatus($a_course_id)
206  {
207  include_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
208  include_once 'Modules/Course/classes/class.ilCourseObjective.php';
209  // begin-patch lok
210  $objective_ids = ilCourseObjective::_getObjectiveIds($a_course_id,true);
211  // end-patch lok
212  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
213  $accomplished = $this->getAccomplished($a_course_id);
214  $suggested = $this->getSuggested($a_course_id);
215 
216  if(!count($objective_ids))
217  {
219  }
220 
221  if(count($accomplished) == count($objective_ids))
222  {
224  }
225 
226  $all_pretest_answered = false;
227  $all_final_answered = false;
228  foreach($objectives as $data)
229  {
230  if(assQuestion::_areAnswered($this->getUserId(),$data['questions']))
231  {
232  if($data['tst_status'])
233  {
234  $all_final_answered = true;
235  }
236  else
237  {
238  $all_pretest_answered = true;
239  }
240  }
241  }
242  if($all_final_answered)
243  {
245  }
246  if($all_pretest_answered and
247  !count($suggested))
248  {
250  }
251  elseif($all_pretest_answered)
252  {
254  }
256  }
257 
258  function hasAccomplishedObjective($a_objective_id)
259  {
260  global $ilDB;
261 
262  $query = "SELECT status FROM crs_objective_status ".
263  "WHERE objective_id = ".$ilDB->quote($a_objective_id ,'integer')." ".
264  "AND user_id = ".$ilDB->quote($this->getUserId() ,'integer')."";
265 
266  $res = $this->db->query($query);
267  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
268  {
269  return true;
270  }
271  return false;
272  }
273 
274  function readStatus($a_crs_id)
275  {
276  include_once './Modules/Course/classes/class.ilCourseObjective.php';
277 
278  // begin-patch lok
279  $objective_ids = ilCourseObjective::_getObjectiveIds($a_crs_id,true);
280  // end-patch lok
281  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
283  return true;
284  }
285 
286 
287 
288 
289  // PRIVATE
290  function __deleteEntries($a_objective_ids)
291  {
292  global $ilLog;
293 
294  $ilLog->logStack();
295  #$ilLog(__METHOD__.': Call of deprecated method.');
296 
297  return true;
298  }
299 
300  public static function _deleteUser($user_id)
301  {
302  global $ilDB;
303 
304  $query = "DELETE FROM crs_objective_status ".
305  "WHERE user_id = ".$ilDB->quote($user_id ,'integer')." ";
306  $res = $ilDB->manipulate($query);
307 
308  $query = "DELETE FROM crs_objective_status_p ".
309  "WHERE user_id = ".$ilDB->quote($user_id ,'integer')." ";
310  $res = $ilDB->manipulate($query);
311  return true;
312  }
313 
314  function _updateObjectiveResult($a_user_id,$a_active_id,$a_question_id)
315  {
316  // find all objectives this question is assigned to
317  if(!$objectives = ilCourseObjectiveResult::_readAssignedObjectivesOfQuestion($a_question_id))
318  {
319  // no objectives found. TODO user has passed a test. After that questions of that test are assigned to an objective.
320  // => User has not passed
321  return true;
322  }
323  ilCourseObjectiveResult::_updateObjectiveStatus($a_user_id,$objectives);
324 
325  return true;
326  }
327 
328  function _readAssignedObjectivesOfQuestion($a_question_id)
329  {
330  global $ilDB;
331 
332  // get all objtives and questions this current question is assigned to
333  $query = "SELECT q2.question_id qid,q2.objective_id ob FROM crs_objective_qst q1, ".
334  "crs_objective_qst q2 ".
335  "WHERE q1.question_id = ".$ilDB->quote($a_question_id ,'integer')." ".
336  "AND q1.objective_id = q2.objective_id ";
337 
338  $res = $ilDB->query($query);
339  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
340  {
341  $objectives['all_objectives'][$row->ob] = $row->ob;
342  $objectives['all_questions'][$row->qid] = $row->qid;
343  }
344  if(!is_array($objectives))
345  {
346  return false;
347  }
348  $objectives['objectives'] = ilCourseObjectiveResult::_readAssignedObjectives($objectives['all_objectives']);
349  return $objectives ? $objectives : array();
350  }
351 
352 
353  function _readAssignedObjectives($a_all_objectives)
354  {
355  global $ilDB;
356 
357  // Read necessary points
358  $query = "SELECT t.objective_id obj,t.ref_id ref, question_id,tst_status,tst_limit ".
359  "FROM crs_objective_tst t JOIN crs_objective_qst q ".
360  "ON (t.objective_id = q.objective_id AND t.ref_id = q.ref_id) ".
361  "WHERE ".$ilDB->in('t.objective_id',$a_all_objectives,false,'integer');
362 
363  $res = $ilDB->query($query);
364  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
365  {
366  /*
367  $objectives[$row->obj."_".$row->ref]['questions'][$row->question_id] = $row->question_id;
368  $objectives[$row->obj."_".$row->ref]['tst_status'] = $row->tst_status;
369  $objectives[$row->obj."_".$row->ref]['tst_limit'] = $row->tst_limit;
370  $objectives[$row->obj."_".$row->ref]['objective_id'] = $row->obj;
371  */
372 
373  $objectives[$row->obj."_".$row->tst_status]['questions'][$row->question_id] = $row->question_id;
374  $objectives[$row->obj."_".$row->tst_status]['tst_status'] = $row->tst_status;
375  $objectives[$row->obj."_".$row->tst_status]['tst_limit'] = $row->tst_limit;
376  $objectives[$row->obj."_".$row->tst_status]['objective_id'] = $row->obj;
377 
378 
379  }
380  return $objectives ? $objectives : array();
381  }
382 
383  function _updateObjectiveStatus($a_user_id,$objectives)
384  {
385  global $ilDB,$ilUser;
386 
387  if(!count($objectives['all_questions']) or
388  !count($objectives['all_objectives']))
389  {
390  return false;
391  }
392  // Read reachable points
393  $query = "SELECT question_id,points FROM qpl_questions ".
394  "WHERE ".$ilDB->in('question_id',(array) $objectives['all_questions'],false,'integer');
395  $res = $ilDB->query($query);
396  while($row = $ilDB->fetchAssoc($res))
397  {
398  $objectives['all_question_points'][$row['question_id']]['max_points'] = $row['points'];
399  }
400  // Read reached points
401  $query = "SELECT question_fi, MAX(points) as reached FROM tst_test_result ".
402  "JOIN tst_active ON (active_id = active_fi) ".
403  "WHERE user_fi = ".$ilDB->quote($a_user_id,'integer')." ".
404  "AND ".$ilDB->in('question_fi',(array) $objectives['all_questions'],false,'integer')." ".
405  #"AND question_fi IN (".implode(",",ilUtil::quoteArray($objectives['all_questions'])).") ".
406  "GROUP BY question_fi,user_fi";
407  $res = $ilDB->query($query);
408  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
409  {
410  $objectives['all_question_points'][$row->question_fi]['reached_points'] = $row->reached;
411  }
412 
413  // Check accomplished
414  $fullfilled = array();
415  $pretest = array();
416  foreach($objectives['objectives'] as $kind => $data)
417  {
418  // objective does not allow to change status
419  if(ilCourseObjectiveResult::__isFullfilled($objectives['all_question_points'],$data))
420  {
421  // Status 0 means pretest fullfilled, status 1 means final test fullfilled
422  if($data['tst_status'])
423  {
424  $fullfilled[] = array($data['objective_id'],$ilUser->getId(),$data['tst_status']);
425  }
426  else
427  {
428  $pretest[] = array($data['objective_id'],$ilUser->getId());
429  }
430  }
431  }
432  if(count($fullfilled))
433  {
434  foreach($fullfilled as $fullfilled_arr)
435  {
436  $ilDB->replace(
437  'crs_objective_status',
438  array(
439  'objective_id' => array('integer',$fullfilled_arr[0]),
440  'user_id' => array('integer',$fullfilled_arr[1])
441  ),
442  array(
443  'status' => array('integer',$fullfilled_arr[2])
444  )
445  );
446  }
447  ilCourseObjectiveResult::__updatePassed($a_user_id,$objectives['all_objectives']);
448  }
449  if(count($pretest))
450  {
451  foreach($pretest as $pretest_arr)
452  {
453  $ilDB->replace(
454  'crs_objective_status_p',
455  array(
456  'objective_id' => array('integer',$pretest_arr[0]),
457  'user_id' => array('integer',$pretest_arr[1])
458  ),
459  array()
460  );
461  }
462  }
463  return true;
464  }
465 
466  function __isFullfilled($question_points,$objective_data)
467  {
468  if(!is_array($objective_data['questions']))
469  {
470  return false;
471  }
472  $max_points = 0;
473  $reached_points = 0;
474  foreach($objective_data['questions'] as $question_id)
475  {
476  $max_points += $question_points[$question_id]['max_points'];
477  $reached_points += $question_points[$question_id]['reached_points'];
478  }
479  if(!$max_points)
480  {
481  return false;
482  }
483 
484  return $reached_points >= $objective_data['tst_limit'] ? true : false;
485 
486  return (($reached_points / $max_points * 100) >= $objective_data['tst_limit']) ? true : false;
487  }
488 
489  function __updatePassed($a_user_id,$objective_ids)
490  {
491  global $ilDB;
492 
493  $passed = array();
494 
495  $query = "SELECT COUNT(t1.crs_id) num,t1.crs_id FROM crs_objectives t1 ".
496  "JOIN crs_objectives t2 WHERE t1.crs_id = t2.crs_id and ".
497  $ilDB->in('t1.objective_id',$objective_ids,false,'integer')." ".
498  "GROUP BY t1.crs_id";
499  $res = $ilDB->query($query);
500  $crs_ids = array();
501  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
502  {
503  $query = "SELECT COUNT(cs.objective_id) num_passed FROM crs_objective_status cs ".
504  "JOIN crs_objectives co ON cs.objective_id = co.objective_id ".
505  "WHERE crs_id = ".$ilDB->quote($row->crs_id ,'integer')." ".
506  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
507 
508  $user_res = $ilDB->query($query);
509  while($user_row = $user_res->fetchRow(DB_FETCHMODE_OBJECT))
510  {
511  if($user_row->num_passed == $row->num)
512  {
513  $passed[] = $row->crs_id;
514  }
515  }
516  $crs_ids[$row->crs_id] = $row->crs_id;
517  }
518  if(count($passed))
519  {
520  foreach($passed as $crs_id)
521  {
522  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
523  $members = ilCourseParticipants::_getInstanceByObjId($crs_id);
524  $members->updatePassed($a_user_id,true);
525  }
526  }
527 
528  // update tracking status
529  foreach ($crs_ids as $cid)
530  {
531  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
532  ilLPStatusWrapper::_updateStatus($cid, $a_user_id);
533  }
534  }
535 
536 }
537 ?>