ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjectiveResult.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
34 define('IL_OBJECTIVE_STATUS_EMPTY','empty');
35 define('IL_OBJECTIVE_STATUS_PRETEST','pretest');
36 define('IL_OBJECTIVE_STATUS_FINAL','final');
37 define('IL_OBJECTIVE_STATUS_NONE','none');
38 define('IL_OBJECTIVE_STATUS_FINISHED','finished');
39 define('IL_OBJECTIVE_STATUS_PRETEST_NON_SUGGEST','pretest_non_suggest');
40 
41 
43 {
44  var $db = null;
45  var $user_id = null;
46 
47 
48  function ilCourseObjectiveResult($a_usr_id)
49  {
50  global $ilDB;
51 
52  $this->db =& $ilDB;
53 
54  $this->user_id = $a_usr_id;
55  }
56  function getUserId()
57  {
58  return $this->user_id;
59  }
60 
61  function getAccomplished($a_crs_id)
62  {
63  return ilCourseObjectiveResult::_getAccomplished($this->getUserId(),$a_crs_id);
64  }
65  function _getAccomplished($a_user_id,$a_crs_id)
66  {
67  global $ilDB;
68 
69  include_once 'Modules/Course/classes/class.ilCourseObjective.php';
70  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
71 
72  if(!is_array($objectives))
73  {
74  return array();
75  }
76  $query = "SELECT objective_id FROM crs_objective_status ".
77  "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives))." ) ".
78  "AND user_id = ".$ilDB->quote($a_user_id)." ";
79  $res = $ilDB->query($query);
80  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
81  {
82  $accomplished[] = $row->objective_id;
83  }
84  return $accomplished ? $accomplished : array();
85  }
86 
87  function getSuggested($a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
88  {
89  return ilCourseObjectiveResult::_getSuggested($this->getUserId(),$a_crs_id,$a_status);
90  }
91 
92  function _getSuggested($a_user_id,$a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
93  {
94  global $ilDB;
95 
96  $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
97 
98  $finished = array();
99  if($a_status == IL_OBJECTIVE_STATUS_FINAL or
100  $a_status == IL_OBJECTIVE_STATUS_FINISHED)
101  {
102  // check finished
103  $query = "SELECT objective_id FROM crs_objective_status ".
104  "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
105  "AND user_id = ".$ilDB->quote($a_user_id)." ";
106  $res = $ilDB->query($query);
107  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
108  {
109  $finished[] = $row->objective_id;
110  }
111  }
112  else
113  {
114  // Pretest
115  $query = "SELECT objective_id FROM crs_objective_status_pretest ".
116  "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
117  "AND user_id = ".$ilDB->quote($a_user_id)."";
118  $res = $ilDB->query($query);
119  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
120  {
121  $finished[] = $row->objective_id;
122  }
123  }
124  foreach($objectives as $objective_id)
125  {
126  if(!in_array($objective_id,$finished))
127  {
128  $suggested[] = $objective_id;
129  }
130  }
131  return $suggested ? $suggested : array();
132  }
133 
134  function reset($a_course_id)
135  {
136  global $ilDB;
137 
138  include_once './Modules/Course/classes/class.ilCourseObjective.php';
139  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
140 
141 
142  foreach($objectives = ilCourseObjective::_getObjectiveIds($a_course_id) as $objective_id)
143  {
144  $tmp_obj_question =& new ilCourseObjectiveQuestion($objective_id);
145 
146  foreach($tmp_obj_question->getTests() as $test_data)
147  {
148  $this->__deleteEntries($tmp_obj_question->getQuestionsByTest($test_data['ref_id']));
149 
150  if($tmp_test =& ilObjectFactory::getInstanceByRefId($test_data['ref_id']))
151  {
152  $tmp_test->removeTestResultsForUser($this->getUserId());
153  unset($tmp_test);
154  }
155  }
156  }
157 
158  if(count($objectives))
159  {
160  $query = "DELETE FROM crs_objective_status ".
161  "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
162  "AND user_id = ".$ilDB->quote($this->getUserId())." ";
163  $this->db->query($query);
164 
165  $query = "DELETE FROM crs_objective_status_pretest ".
166  "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
167  "AND user_id = ".$ilDB->quote($this->getUserId())."";
168  $this->db->query($query);
169  }
170 
171  return true;
172  }
173 
174  function getStatus($a_course_id)
175  {
176  include_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
177  include_once 'Modules/Course/classes/class.ilCourseObjective.php';
178  $objective_ids = ilCourseObjective::_getObjectiveIds($a_course_id);
179  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
180  $accomplished = $this->getAccomplished($a_course_id);
181  $suggested = $this->getSuggested($a_course_id);
182 
183  if(!count($objective_ids))
184  {
186  }
187 
188  if(count($accomplished) == count($objective_ids))
189  {
191  }
192 
193  $all_pretest_answered = false;
194  $all_final_answered = false;
195  foreach($objectives as $data)
196  {
197  if(assQuestion::_areAnswered($this->getUserId(),$data['questions']))
198  {
199  if($data['tst_status'])
200  {
201  $all_final_answered = true;
202  }
203  else
204  {
205  $all_pretest_answered = true;
206  }
207  }
208  }
209  if($all_final_answered)
210  {
212  }
213  if($all_pretest_answered and
214  !count($suggested))
215  {
217  }
218  elseif($all_pretest_answered)
219  {
221  }
223  }
224 
225  function hasAccomplishedObjective($a_objective_id)
226  {
227  global $ilDB;
228 
229  $query = "SELECT status FROM crs_objective_status ".
230  "WHERE objective_id = ".$ilDB->quote($a_objective_id)." ".
231  "AND user_id = ".$ilDB->quote($this->getUserId())."";
232 
233  $res = $this->db->query($query);
234  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
235  {
236  return true;
237  }
238  return false;
239  }
240 
241  function readStatus($a_crs_id)
242  {
243  include_once './Modules/Course/classes/class.ilCourseObjective.php';
244 
245  $objective_ids = ilCourseObjective::_getObjectiveIds($a_crs_id);
246  $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
248  return true;
249  }
250 
251 
252 
253 
254  // PRIVATE
255  function __deleteEntries($a_objective_ids)
256  {
257  global $ilDB;
258 
259  if(!count($a_objective_ids))
260  {
261  return true;
262  }
263  $in = "IN (";
264  $in .= implode(",",ilUtil::quoteArray($a_objective_ids));
265  $in .= ")";
266 
267  $query = "DELETE FROM crs_objective_results ".
268  "WHERE usr_id = ".$ilDB->quote($this->getUserId())." ".
269  "AND question_id ".$in;
270  $this->db->query($query);
271  }
272 
274  {
275  global $ilDB;
276 
277  $query = "DELETE FROM crs_objective_results ".
278  "WHERE usr_id = ".$ilDB->quote($user_id)." ";
279  $ilDB->query($query);
280 
281  $query = "DELETE FROM crs_objective_status ".
282  "WHERE user_id = ".$ilDB->quote($user_id)." ";
283  $ilDB->query($query);
284 
285  $query = "DELETE FROM crs_objective_status_pretest ".
286  "WHERE user_id = ".$ilDB->quote($user_id)." ";
287  $ilDB->query($query);
288  return true;
289  }
290 
291  function _updateObjectiveResult($a_user_id,$a_active_id,$a_question_id)
292  {
293  // find all objectives this question is assigned to
294  if(!$objectives = ilCourseObjectiveResult::_readAssignedObjectivesOfQuestion($a_question_id))
295  {
296  // no objectives found. TODO user has passed a test. After that questions of that test are assigned to an objective.
297  // => User has not passed
298  return true;
299  }
300  ilCourseObjectiveResult::_updateObjectiveStatus($a_user_id,$objectives);
301 
302  return true;
303  }
304 
305  function _readAssignedObjectivesOfQuestion($a_question_id)
306  {
307  global $ilDB;
308 
309  // get all objtives and questions this current question is assigned to
310  $query = "SELECT q2.question_id as qid,q2.objective_id as ob FROM crs_objective_qst as q1, ".
311  "crs_objective_qst as q2 ".
312  "WHERE q1.question_id = ".$ilDB->quote($a_question_id)." ".
313  "AND q1.objective_id = q2.objective_id ";
314 
315  $res = $ilDB->query($query);
316  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
317  {
318  $objectives['all_objectives'][$row->ob] = $row->ob;
319  $objectives['all_questions'][$row->qid] = $row->qid;
320  }
321  if(!is_array($objectives))
322  {
323  return false;
324  }
325  $objectives['objectives'] = ilCourseObjectiveResult::_readAssignedObjectives($objectives['all_objectives']);
326  return $objectives ? $objectives : array();
327  }
328 
329 
330  function _readAssignedObjectives($a_all_objectives)
331  {
332  global $ilDB;
333 
334  // Read necessary points
335  $query = "SELECT t.objective_id as obj,t.ref_id as ref, question_id,tst_status,tst_limit ".
336  "FROM crs_objective_tst as t JOIN crs_objective_qst as q ".
337  "ON (t.objective_id = q.objective_id AND t.ref_id = q.ref_id) ".
338  "WHERE t.objective_id IN (".implode(",",ilUtil::quoteArray($a_all_objectives)).")";
339 
340  $res = $ilDB->query($query);
341  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
342  {
343  /*
344  $objectives[$row->obj."_".$row->ref]['questions'][$row->question_id] = $row->question_id;
345  $objectives[$row->obj."_".$row->ref]['tst_status'] = $row->tst_status;
346  $objectives[$row->obj."_".$row->ref]['tst_limit'] = $row->tst_limit;
347  $objectives[$row->obj."_".$row->ref]['objective_id'] = $row->obj;
348  */
349 
350  $objectives[$row->obj."_".$row->tst_status]['questions'][$row->question_id] = $row->question_id;
351  $objectives[$row->obj."_".$row->tst_status]['tst_status'] = $row->tst_status;
352  $objectives[$row->obj."_".$row->tst_status]['tst_limit'] = $row->tst_limit;
353  $objectives[$row->obj."_".$row->tst_status]['objective_id'] = $row->obj;
354 
355 
356  }
357  return $objectives ? $objectives : array();
358  }
359 
360  function _updateObjectiveStatus($a_user_id,$objectives)
361  {
362  global $ilDB,$ilUser;
363 
364  if(!count($objectives['all_questions']) or
365  !count($objectives['all_objectives']))
366  {
367  return false;
368  }
369 
370  // Read reachable points
371  $query = "SELECT question_id,points FROM qpl_questions ".
372  "WHERE question_id IN(".implode(",",ilUtil::quoteArray($objectives['all_questions'])).")";
373  $res = $ilDB->query($query);
374  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
375  {
376  $objectives['all_question_points'][$row->question_id]['max_points'] = $row->points;
377  }
378  // Read reached points
379  $query = "SELECT question_fi, MAX(points) as reached FROM tst_test_result JOIN tst_active ".
380  "ON (active_id = active_fi) ".
381  "WHERE user_fi = ".$ilDB->quote($a_user_id)." ".
382  "AND question_fi IN (".implode(",",ilUtil::quoteArray($objectives['all_questions'])).") ".
383  "GROUP BY question_fi,user_fi";
384  $res = $ilDB->query($query);
385  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
386  {
387  $objectives['all_question_points'][$row->question_fi]['reached_points'] = $row->reached;
388  }
389 
390  // Check accomplished
391  $fullfilled = array();
392  $pretest = array();
393  foreach($objectives['objectives'] as $kind => $data)
394  {
395  // objective does not allow to change status
396  if(ilCourseObjectiveResult::__isFullfilled($objectives['all_question_points'],$data))
397  {
398  // Status 0 means pretest fullfilled, status 1 means final test fullfilled
399  if($data['tst_status'])
400  {
401  $fullfilled[] = array($data['objective_id'],$ilUser->getId(),$data['tst_status']);
402  }
403  else
404  {
405  $pretest[] = array($data['objective_id'],$ilUser->getId());
406  }
407  }
408  }
409  if(count($fullfilled))
410  {
411  $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status VALUES(?,?,?)"),
412  $fullfilled);
413  ilCourseObjectiveResult::__updatePassed($a_user_id,$objectives['all_objectives']);
414  }
415  if(count($pretest))
416  {
417  $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status_pretest VALUES(?,?)"),
418  $pretest);
419  }
420 
421  return true;
422  }
423 
424  function __isFullfilled($question_points,$objective_data)
425  {
426  if(!is_array($objective_data['questions']))
427  {
428  return false;
429  }
430  $max_points = 0;
431  $reached_points = 0;
432  foreach($objective_data['questions'] as $question_id)
433  {
434  $max_points += $question_points[$question_id]['max_points'];
435  $reached_points += $question_points[$question_id]['reached_points'];
436  }
437  if(!$max_points)
438  {
439  return false;
440  }
441 
442  return $reached_points >= $objective_data['tst_limit'] ? true : false;
443 
444  return (($reached_points / $max_points * 100) >= $objective_data['tst_limit']) ? true : false;
445  }
446 
447  function __updatePassed($a_user_id,$objective_ids)
448  {
449  global $ilDB;
450 
451  $passed = array();
452 
453  $query = "SELECT COUNT(t1.crs_id) AS num,t1.crs_id FROM crs_objectives as t1 ".
454  "JOIN crs_objectives as t2 WHERE t1.crs_id = t2.crs_id and t1.objective_id ".
455  "IN (".implode(",",ilUtil::quoteArray($objective_ids)).") ".
456  "GROUP BY t1.crs_id";
457  $res = $ilDB->query($query);
458  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
459  {
460  $query = "SELECT COUNT(cs.objective_id) AS num_passed FROM crs_objective_status AS cs ".
461  "JOIN crs_objectives AS co ON cs.objective_id = co.objective_id ".
462  "WHERE crs_id = ".$ilDB->quote($row->crs_id)." ".
463  "AND user_id = ".$ilDB->quote($a_user_id)." ";
464 
465  $user_res = $ilDB->query($query);
466  while($user_row = $user_res->fetchRow(DB_FETCHMODE_OBJECT))
467  {
468  if($user_row->num_passed == $row->num)
469  {
470  $passed[] = $row->crs_id;
471  }
472  }
473  }
474  if(count($passed))
475  {
476  foreach($passed as $crs_id)
477  {
478  include_once('Modules/Course/classes/class.ilCourseParticipants.php');
479  $members = ilCourseParticipants::_getInstanceByObjId($crs_id);
480  $members->updatePassed($a_user_id,true);
481  }
482  }
483  }
484 
485 }
486 ?>