ILIAS  Release_4_1_x_branch Revision 61804
 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  /*
360  $types = $ilDB->addTypesToArray(array(), 'integer', count($objectives['all_questions']));
361  $placeholders = $ilDB->addTypesToArray(array(), '%s', count($objectives['all_questions']));
362  $res = $ilDB->queryF("SELECT question_id,points FROM qpl_questions WHERE question_id IN(". implode(",", $placeholders) .")",
363  $types,
364  $objectives['all_questions']
365  );
366  */
367  $query = "SELECT question_id,points FROM qpl_questions ".
368  "WHERE ".$ilDB->in('question_id',(array) $objectives['all_questions'],false,'integer');
369  $res = $ilDB->query($query);
370  while($row = $ilDB->fetchAssoc($res))
371  {
372  $objectives['all_question_points'][$row['question_id']]['max_points'] = $row['points'];
373  }
374  // Read reached points
375  $query = "SELECT question_fi, MAX(points) as reached FROM tst_test_result ".
376  "JOIN tst_active ON (active_id = active_fi) ".
377  "WHERE user_fi = ".$ilDB->quote($a_user_id,'integer')." ".
378  "AND ".$ilDB->in('question_fi',(array) $objectives['all_questions'],false,'integer')." ".
379  #"AND question_fi IN (".implode(",",ilUtil::quoteArray($objectives['all_questions'])).") ".
380  "GROUP BY question_fi,user_fi";
381  $res = $ilDB->query($query);
382  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
383  {
384  $objectives['all_question_points'][$row->question_fi]['reached_points'] = $row->reached;
385  }
386 
387  // Check accomplished
388  $fullfilled = array();
389  $pretest = array();
390  foreach($objectives['objectives'] as $kind => $data)
391  {
392  // objective does not allow to change status
393  if(ilCourseObjectiveResult::__isFullfilled($objectives['all_question_points'],$data))
394  {
395  // Status 0 means pretest fullfilled, status 1 means final test fullfilled
396  if($data['tst_status'])
397  {
398  $fullfilled[] = array($data['objective_id'],$ilUser->getId(),$data['tst_status']);
399  }
400  else
401  {
402  $pretest[] = array($data['objective_id'],$ilUser->getId());
403  }
404  }
405  }
406  if(count($fullfilled))
407  {
408  $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status VALUES(?,?,?)"),
409  $fullfilled);
410  ilCourseObjectiveResult::__updatePassed($a_user_id,$objectives['all_objectives']);
411  }
412  if(count($pretest))
413  {
414  $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status_p VALUES(?,?)"),
415  $pretest);
416  }
417 
418  return true;
419  }
420 
421  function __isFullfilled($question_points,$objective_data)
422  {
423  if(!is_array($objective_data['questions']))
424  {
425  return false;
426  }
427  $max_points = 0;
428  $reached_points = 0;
429  foreach($objective_data['questions'] as $question_id)
430  {
431  $max_points += $question_points[$question_id]['max_points'];
432  $reached_points += $question_points[$question_id]['reached_points'];
433  }
434  if(!$max_points)
435  {
436  return false;
437  }
438 
439  return $reached_points >= $objective_data['tst_limit'] ? true : false;
440 
441  return (($reached_points / $max_points * 100) >= $objective_data['tst_limit']) ? true : false;
442  }
443 
444  function __updatePassed($a_user_id,$objective_ids)
445  {
446  global $ilDB;
447 
448  $passed = array();
449 
450  $query = "SELECT COUNT(t1.crs_id) num,t1.crs_id FROM crs_objectives t1 ".
451  "JOIN crs_objectives t2 WHERE t1.crs_id = t2.crs_id and ".
452  $ilDB->in('t1.objective_id',$objective_ids,false,'integer')." ".
453  "GROUP BY t1.crs_id";
454  $res = $ilDB->query($query);
455  $crs_ids = array();
456  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
457  {
458  $query = "SELECT COUNT(cs.objective_id) num_passed FROM crs_objective_status cs ".
459  "JOIN crs_objectives co ON cs.objective_id = co.objective_id ".
460  "WHERE crs_id = ".$ilDB->quote($row->crs_id ,'integer')." ".
461  "AND user_id = ".$ilDB->quote($a_user_id ,'integer')." ";
462 
463  $user_res = $ilDB->query($query);
464  while($user_row = $user_res->fetchRow(DB_FETCHMODE_OBJECT))
465  {
466  if($user_row->num_passed == $row->num)
467  {
468  $passed[] = $row->crs_id;
469  }
470  }
471  $crs_ids[$row->crs_id] = $row->crs_id;
472  }
473  if(count($passed))
474  {
475  foreach($passed as $crs_id)
476  {
477  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
478  $members = ilCourseParticipants::_getInstanceByObjId($crs_id);
479  $members->updatePassed($a_user_id,true);
480  }
481  }
482 
483  // update tracking status
484  foreach ($crs_ids as $cid)
485  {
486  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
487  ilLPStatusWrapper::_updateStatus($cid, $a_user_id);
488  }
489  }
490 
491 }
492 ?>