ILIAS  Release_4_4_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  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
48 
49  if(!is_array($objectives))
50  {
51  return array();
52  }
53  $query = "SELECT objective_id FROM crs_objective_status ".
54  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
55  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
56  $res = $ilDB->query($query);
57  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
58  {
59  $accomplished[] = $row->objective_id;
60  }
61  return $accomplished ? $accomplished : array();
62  }
63 
64  function getSuggested($a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
65  {
66  return ilCourseObjectiveResult::_getSuggested($this->getUserId(),$a_crs_id,$a_status);
67  }
68 
69  function _getSuggested($a_user_id,$a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
70  {
71  global $ilDB;
72 
73  include_once './Modules/Course/classes/class.ilCourseObjective.php';
74  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
75 
76  $finished = array();
77  if($a_status == IL_OBJECTIVE_STATUS_FINAL or
78  $a_status == IL_OBJECTIVE_STATUS_FINISHED)
79  {
80  // check finished
81  $query = "SELECT objective_id FROM crs_objective_status ".
82  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer')." ".
83  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
84  $res = $ilDB->query($query);
85  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
86  {
87  $finished[] = $row->objective_id;
88  }
89  }
90  else
91  {
92  // Pretest
93  $query = "SELECT objective_id FROM crs_objective_status_p ".
94  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
95  "AND user_id = ".$ilDB->quote($a_user_id ,'integer');
96  $res = $ilDB->query($query);
97  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
98  {
99  $finished[] = $row->objective_id;
100  }
101  }
102  foreach($objectives as $objective_id)
103  {
104  if(!in_array($objective_id,$finished))
105  {
106  $suggested[] = $objective_id;
107  }
108  }
109  return $suggested ? $suggested : array();
110  }
111 
118  public static function getSuggestedQuestions($a_usr_id,$a_crs_id)
119  {
120  foreach(self::_getSuggested($a_usr_id,$a_crs_id) as $objective_id)
121  {
122  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
123  $obj = new ilCourseObjectiveQuestion($objective_id);
124  foreach($obj->getFinalTestQuestions() as $qst)
125  {
126  $qsts[] = $qst['question_id'];
127  }
128  }
129  return $qsts ? $qsts : array();
130  }
131 
132  function reset($a_course_id)
133  {
134  global $ilDB;
135 
136  include_once './Modules/Course/classes/class.ilCourseObjective.php';
137  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
138 
139 
140  foreach($objectives = ilCourseObjective::_getObjectiveIds($a_course_id) as $objective_id)
141  {
142  $tmp_obj_question =& new ilCourseObjectiveQuestion($objective_id);
143 
144  foreach($tmp_obj_question->getTests() as $test_data)
145  {
146  #$this->__deleteEntries($tmp_obj_question->getQuestionsByTest($test_data['ref_id']));
147 
148  if($tmp_test =& ilObjectFactory::getInstanceByRefId($test_data['ref_id']))
149  {
150  $tmp_test->removeTestResultsForUser($this->getUserId());
151  unset($tmp_test);
152  }
153  }
154  }
155 
156  if(count($objectives))
157  {
158  $query = "DELETE FROM crs_objective_status ".
159  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
160  "AND user_id = ".$ilDB->quote($this->getUserId() ,'integer')." ";
161  $res = $ilDB->manipulate($query);
162 
163  $query = "DELETE FROM crs_objective_status_p ".
164  "WHERE ".$ilDB->in('objective_id',$objectives,false,'integer').' '.
165  "AND user_id = ".$ilDB->quote($this->getUserId())."";
166  $res = $ilDB->manipulate($query);
167  }
168 
169  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
170  ilLPStatusWrapper::_updateStatus($a_course_id, $this->getUserId());
171 
172  return true;
173  }
174 
175  function getStatus($a_course_id)
176  {
177  include_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
178  include_once 'Modules/Course/classes/class.ilCourseObjective.php';
179  $objective_ids = ilCourseObjective::_getObjectiveIds($a_course_id);
180  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
181  $accomplished = $this->getAccomplished($a_course_id);
182  $suggested = $this->getSuggested($a_course_id);
183 
184  if(!count($objective_ids))
185  {
187  }
188 
189  if(count($accomplished) == count($objective_ids))
190  {
192  }
193 
194  $all_pretest_answered = false;
195  $all_final_answered = false;
196  foreach($objectives as $data)
197  {
198  if(assQuestion::_areAnswered($this->getUserId(),$data['questions']))
199  {
200  if($data['tst_status'])
201  {
202  $all_final_answered = true;
203  }
204  else
205  {
206  $all_pretest_answered = true;
207  }
208  }
209  }
210  if($all_final_answered)
211  {
213  }
214  if($all_pretest_answered and
215  !count($suggested))
216  {
218  }
219  elseif($all_pretest_answered)
220  {
222  }
224  }
225 
226  function hasAccomplishedObjective($a_objective_id)
227  {
228  global $ilDB;
229 
230  $query = "SELECT status FROM crs_objective_status ".
231  "WHERE objective_id = ".$ilDB->quote($a_objective_id ,'integer')." ".
232  "AND user_id = ".$ilDB->quote($this->getUserId() ,'integer')."";
233 
234  $res = $this->db->query($query);
235  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
236  {
237  return true;
238  }
239  return false;
240  }
241 
242  function readStatus($a_crs_id)
243  {
244  include_once './Modules/Course/classes/class.ilCourseObjective.php';
245 
246  $objective_ids = ilCourseObjective::_getObjectiveIds($a_crs_id);
247  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
249  return true;
250  }
251 
252 
253 
254 
255  // PRIVATE
256  function __deleteEntries($a_objective_ids)
257  {
258  global $ilLog;
259 
260  $ilLog->logStack();
261  #$ilLog(__METHOD__.': Call of deprecated method.');
262 
263  return true;
264  }
265 
266  public static function _deleteUser($user_id)
267  {
268  global $ilDB;
269 
270  $query = "DELETE FROM crs_objective_status ".
271  "WHERE user_id = ".$ilDB->quote($user_id ,'integer')." ";
272  $res = $ilDB->manipulate($query);
273 
274  $query = "DELETE FROM crs_objective_status_p ".
275  "WHERE user_id = ".$ilDB->quote($user_id ,'integer')." ";
276  $res = $ilDB->manipulate($query);
277  return true;
278  }
279 
280  function _updateObjectiveResult($a_user_id,$a_active_id,$a_question_id)
281  {
282  // find all objectives this question is assigned to
283  if(!$objectives = ilCourseObjectiveResult::_readAssignedObjectivesOfQuestion($a_question_id))
284  {
285  // no objectives found. TODO user has passed a test. After that questions of that test are assigned to an objective.
286  // => User has not passed
287  return true;
288  }
289  ilCourseObjectiveResult::_updateObjectiveStatus($a_user_id,$objectives);
290 
291  return true;
292  }
293 
294  function _readAssignedObjectivesOfQuestion($a_question_id)
295  {
296  global $ilDB;
297 
298  // get all objtives and questions this current question is assigned to
299  $query = "SELECT q2.question_id qid,q2.objective_id ob FROM crs_objective_qst q1, ".
300  "crs_objective_qst q2 ".
301  "WHERE q1.question_id = ".$ilDB->quote($a_question_id ,'integer')." ".
302  "AND q1.objective_id = q2.objective_id ";
303 
304  $res = $ilDB->query($query);
305  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
306  {
307  $objectives['all_objectives'][$row->ob] = $row->ob;
308  $objectives['all_questions'][$row->qid] = $row->qid;
309  }
310  if(!is_array($objectives))
311  {
312  return false;
313  }
314  $objectives['objectives'] = ilCourseObjectiveResult::_readAssignedObjectives($objectives['all_objectives']);
315  return $objectives ? $objectives : array();
316  }
317 
318 
319  function _readAssignedObjectives($a_all_objectives)
320  {
321  global $ilDB;
322 
323  // Read necessary points
324  $query = "SELECT t.objective_id obj,t.ref_id ref, question_id,tst_status,tst_limit ".
325  "FROM crs_objective_tst t JOIN crs_objective_qst q ".
326  "ON (t.objective_id = q.objective_id AND t.ref_id = q.ref_id) ".
327  "WHERE ".$ilDB->in('t.objective_id',$a_all_objectives,false,'integer');
328 
329  $res = $ilDB->query($query);
330  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
331  {
332  /*
333  $objectives[$row->obj."_".$row->ref]['questions'][$row->question_id] = $row->question_id;
334  $objectives[$row->obj."_".$row->ref]['tst_status'] = $row->tst_status;
335  $objectives[$row->obj."_".$row->ref]['tst_limit'] = $row->tst_limit;
336  $objectives[$row->obj."_".$row->ref]['objective_id'] = $row->obj;
337  */
338 
339  $objectives[$row->obj."_".$row->tst_status]['questions'][$row->question_id] = $row->question_id;
340  $objectives[$row->obj."_".$row->tst_status]['tst_status'] = $row->tst_status;
341  $objectives[$row->obj."_".$row->tst_status]['tst_limit'] = $row->tst_limit;
342  $objectives[$row->obj."_".$row->tst_status]['objective_id'] = $row->obj;
343 
344 
345  }
346  return $objectives ? $objectives : array();
347  }
348 
349  function _updateObjectiveStatus($a_user_id,$objectives)
350  {
351  global $ilDB,$ilUser;
352 
353  if(!count($objectives['all_questions']) or
354  !count($objectives['all_objectives']))
355  {
356  return false;
357  }
358  // Read reachable points
359  $query = "SELECT question_id,points FROM qpl_questions ".
360  "WHERE ".$ilDB->in('question_id',(array) $objectives['all_questions'],false,'integer');
361  $res = $ilDB->query($query);
362  while($row = $ilDB->fetchAssoc($res))
363  {
364  $objectives['all_question_points'][$row['question_id']]['max_points'] = $row['points'];
365  }
366  // Read reached points
367  $query = "SELECT question_fi, MAX(points) as reached FROM tst_test_result ".
368  "JOIN tst_active ON (active_id = active_fi) ".
369  "WHERE user_fi = ".$ilDB->quote($a_user_id,'integer')." ".
370  "AND ".$ilDB->in('question_fi',(array) $objectives['all_questions'],false,'integer')." ".
371  #"AND question_fi IN (".implode(",",ilUtil::quoteArray($objectives['all_questions'])).") ".
372  "GROUP BY question_fi,user_fi";
373  $res = $ilDB->query($query);
374  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
375  {
376  $objectives['all_question_points'][$row->question_fi]['reached_points'] = $row->reached;
377  }
378 
379  // Check accomplished
380  $fullfilled = array();
381  $pretest = array();
382  foreach($objectives['objectives'] as $kind => $data)
383  {
384  // objective does not allow to change status
385  if(ilCourseObjectiveResult::__isFullfilled($objectives['all_question_points'],$data))
386  {
387  // Status 0 means pretest fullfilled, status 1 means final test fullfilled
388  if($data['tst_status'])
389  {
390  $fullfilled[] = array($data['objective_id'],$ilUser->getId(),$data['tst_status']);
391  }
392  else
393  {
394  $pretest[] = array($data['objective_id'],$ilUser->getId());
395  }
396  }
397  }
398  if(count($fullfilled))
399  {
400  foreach($fullfilled as $fullfilled_arr)
401  {
402  $ilDB->replace(
403  'crs_objective_status',
404  array(
405  'objective_id' => array('integer',$fullfilled_arr[0]),
406  'user_id' => array('integer',$fullfilled_arr[1])
407  ),
408  array(
409  'status' => array('integer',$fullfilled_arr[2])
410  )
411  );
412  }
413  ilCourseObjectiveResult::__updatePassed($a_user_id,$objectives['all_objectives']);
414  }
415  if(count($pretest))
416  {
417  foreach($pretest as $pretest_arr)
418  {
419  $ilDB->replace(
420  'crs_objective_status_p',
421  array(
422  'objective_id' => array('integer',$pretest_arr[0]),
423  'user_id' => array('integer',$pretest_arr[1])
424  ),
425  array()
426  );
427  }
428  }
429  return true;
430  }
431 
432  function __isFullfilled($question_points,$objective_data)
433  {
434  if(!is_array($objective_data['questions']))
435  {
436  return false;
437  }
438  $max_points = 0;
439  $reached_points = 0;
440  foreach($objective_data['questions'] as $question_id)
441  {
442  $max_points += $question_points[$question_id]['max_points'];
443  $reached_points += $question_points[$question_id]['reached_points'];
444  }
445  if(!$max_points)
446  {
447  return false;
448  }
449 
450  return $reached_points >= $objective_data['tst_limit'] ? true : false;
451 
452  return (($reached_points / $max_points * 100) >= $objective_data['tst_limit']) ? true : false;
453  }
454 
455  function __updatePassed($a_user_id,$objective_ids)
456  {
457  global $ilDB;
458 
459  $passed = array();
460 
461  $query = "SELECT COUNT(t1.crs_id) num,t1.crs_id FROM crs_objectives t1 ".
462  "JOIN crs_objectives t2 WHERE t1.crs_id = t2.crs_id and ".
463  $ilDB->in('t1.objective_id',$objective_ids,false,'integer')." ".
464  "GROUP BY t1.crs_id";
465  $res = $ilDB->query($query);
466  $crs_ids = array();
467  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
468  {
469  $query = "SELECT COUNT(cs.objective_id) num_passed FROM crs_objective_status cs ".
470  "JOIN crs_objectives co ON cs.objective_id = co.objective_id ".
471  "WHERE crs_id = ".$ilDB->quote($row->crs_id ,'integer')." ".
472  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
473 
474  $user_res = $ilDB->query($query);
475  while($user_row = $user_res->fetchRow(DB_FETCHMODE_OBJECT))
476  {
477  if($user_row->num_passed == $row->num)
478  {
479  $passed[] = $row->crs_id;
480  }
481  }
482  $crs_ids[$row->crs_id] = $row->crs_id;
483  }
484  if(count($passed))
485  {
486  foreach($passed as $crs_id)
487  {
488  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
489  $members = ilCourseParticipants::_getInstanceByObjId($crs_id);
490  $members->updatePassed($a_user_id,true);
491  }
492  }
493 
494  // update tracking status
495  foreach ($crs_ids as $cid)
496  {
497  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
498  ilLPStatusWrapper::_updateStatus($cid, $a_user_id);
499  }
500  }
501 
502 }
503 ?>