00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00034 define('IL_OBJECTIVE_STATUS_PRETEST','pretest');
00035 define('IL_OBJECTIVE_STATUS_FINAL','final');
00036 define('IL_OBJECTIVE_STATUS_NONE','none');
00037 define('IL_OBJECTIVE_STATUS_FINISHED','finished');
00038 define('IL_OBJECTIVE_STATUS_PRETEST_NON_SUGGEST','pretest_non_suggest');
00039
00040
00041 class ilCourseObjectiveResult
00042 {
00043 var $db = null;
00044 var $user_id = null;
00045
00046
00047 function ilCourseObjectiveResult($a_usr_id)
00048 {
00049 global $ilDB;
00050
00051 $this->db =& $ilDB;
00052
00053 $this->user_id = $a_usr_id;
00054 }
00055 function getUserId()
00056 {
00057 return $this->user_id;
00058 }
00059
00060 function getAccomplished($a_crs_id)
00061 {
00062 return ilCourseObjectiveResult::_getAccomplished($this->getUserId(),$a_crs_id);
00063 }
00064 function _getAccomplished($a_user_id,$a_crs_id)
00065 {
00066 global $ilDB;
00067
00068 include_once 'Modules/Course/classes/class.ilCourseObjective.php';
00069 $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
00070
00071 if(!is_array($objectives))
00072 {
00073 return array();
00074 }
00075 $query = "SELECT objective_id FROM crs_objective_status ".
00076 "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives))." ) ".
00077 "AND user_id = ".$ilDB->quote($a_user_id)." ";
00078 $res = $ilDB->query($query);
00079 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00080 {
00081 $accomplished[] = $row->objective_id;
00082 }
00083 return $accomplished ? $accomplished : array();
00084 }
00085
00086 function getSuggested($a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
00087 {
00088 return ilCourseObjectiveResult::_getSuggested($this->getUserId(),$a_crs_id,$a_status);
00089 }
00090 function _getSuggested($a_user_id,$a_crs_id,$a_status = IL_OBJECTIVE_STATUS_FINAL)
00091 {
00092 global $ilDB;
00093
00094 $objectives = ilCourseObjective::_getObjectiveIds($a_crs_id);
00095
00096 $finished = array();
00097 if($a_status == IL_OBJECTIVE_STATUS_FINAL or
00098 $a_status == IL_OBJECTIVE_STATUS_FINISHED)
00099 {
00100
00101 $query = "SELECT objective_id FROM crs_objective_status ".
00102 "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
00103 "AND user_id = ".$ilDB->quote($a_user_id)." ";
00104 $res = $ilDB->query($query);
00105 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00106 {
00107 $finished[] = $row->objective_id;
00108 }
00109 }
00110 else
00111 {
00112
00113 $query = "SELECT objective_id FROM crs_objective_status_pretest ".
00114 "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
00115 "AND user_id = ".$ilDB->quote($a_user_id)."";
00116 $res = $ilDB->query($query);
00117 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00118 {
00119 $finished[] = $row->objective_id;
00120 }
00121 }
00122 foreach($objectives as $objective_id)
00123 {
00124 if(!in_array($objective_id,$finished))
00125 {
00126 $suggested[] = $objective_id;
00127 }
00128 }
00129 return $suggested ? $suggested : array();
00130 }
00131
00132 function reset($a_course_id)
00133 {
00134 global $ilDB;
00135
00136 include_once './Modules/Course/classes/class.ilCourseObjective.php';
00137 include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
00138
00139
00140 foreach($objectives = ilCourseObjective::_getObjectiveIds($a_course_id) as $objective_id)
00141 {
00142 $tmp_obj_question =& new ilCourseObjectiveQuestion($objective_id);
00143
00144 foreach($tmp_obj_question->getTests() as $test_data)
00145 {
00146 $this->__deleteEntries($tmp_obj_question->getQuestionsByTest($test_data['ref_id']));
00147
00148 if($tmp_test =& ilObjectFactory::getInstanceByRefId($test_data['ref_id']))
00149 {
00150 $tmp_test->removeTestResultsForUser($this->getUserId());
00151 unset($tmp_test);
00152 }
00153 }
00154 }
00155
00156 if(count($objectives))
00157 {
00158 $query = "DELETE FROM crs_objective_status ".
00159 "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
00160 "AND user_id = ".$ilDB->quote($this->getUserId())." ";
00161 $this->db->query($query);
00162
00163 $query = "DELETE FROM crs_objective_status_pretest ".
00164 "WHERE objective_id IN (".implode(",",ilUtil::quoteArray($objectives)).") ".
00165 "AND user_id = ".$ilDB->quote($this->getUserId())."";
00166 $this->db->query($query);
00167 }
00168
00169 return true;
00170 }
00171
00172 function getStatus($a_course_id)
00173 {
00174 include_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
00175 include_once 'Modules/Course/classes/class.ilCourseObjective.php';
00176 $objective_ids = ilCourseObjective::_getObjectiveIds($a_course_id);
00177 $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
00178 $accomplished = $this->getAccomplished($a_course_id);
00179 $suggested = $this->getSuggested($a_course_id);
00180
00181 if(count($accomplished) == count($objective_ids))
00182 {
00183 return IL_OBJECTIVE_STATUS_FINISHED;
00184 }
00185
00186 $all_pretest_answered = false;
00187 $all_final_answered = false;
00188 foreach($objectives as $data)
00189 {
00190 if(assQuestion::_areAnswered($this->getUserId(),$data['questions']))
00191 {
00192 if($data['tst_status'])
00193 {
00194 $all_final_answered = true;
00195 }
00196 else
00197 {
00198 $all_pretest_answered = true;
00199 }
00200 }
00201 }
00202 if($all_final_answered)
00203 {
00204 return IL_OBJECTIVE_STATUS_FINAL;
00205 }
00206 if($all_pretest_answered and
00207 !count($suggested))
00208 {
00209 return IL_OBJECTIVE_STATUS_PRETEST_NON_SUGGEST;
00210 }
00211 elseif($all_pretest_answered)
00212 {
00213 return IL_OBJECTIVE_STATUS_PRETEST;
00214 }
00215 return IL_OBJECTIVE_STATUS_NONE;
00216 }
00217
00218 function hasAccomplishedObjective($a_objective_id)
00219 {
00220 global $ilDB;
00221
00222 $query = "SELECT status FROM crs_objective_status ".
00223 "WHERE objective_id = ".$ilDB->quote($a_objective_id)." ".
00224 "AND user_id = ".$ilDB->quote($this->getUserId())."";
00225
00226 $res = $this->db->query($query);
00227 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00228 {
00229 return true;
00230 }
00231 return false;
00232 }
00233
00234 function readStatus($a_crs_id)
00235 {
00236 include_once './Modules/Course/classes/class.ilCourseObjective.php';
00237
00238 $objective_ids = ilCourseObjective::_getObjectiveIds($a_crs_id);
00239 $objectives = ilCourseObjectiveResult::_readAssignedObjectives($objective_ids);
00240 ilCourseObjectiveResult::_updateObjectiveStatus($this->getUserId(),$objectives);
00241 return true;
00242 }
00243
00244
00245
00246
00247
00248 function __deleteEntries($a_objective_ids)
00249 {
00250 global $ilDB;
00251
00252 if(!count($a_objective_ids))
00253 {
00254 return true;
00255 }
00256 $in = "IN (";
00257 $in .= implode(",",ilUtil::quoteArray($a_objective_ids));
00258 $in .= ")";
00259
00260 $query = "DELETE FROM crs_objective_results ".
00261 "WHERE usr_id = ".$ilDB->quote($this->getUserId())." ".
00262 "AND question_id ".$in;
00263 $this->db->query($query);
00264 }
00265
00266 function _deleteUser($user_id)
00267 {
00268 global $ilDB;
00269
00270 $query = "DELETE FROM crs_objective_results ".
00271 "WHERE usr_id = ".$ilDB->quote($user_id)." ";
00272 $ilDB->query($query);
00273
00274 $query = "DELETE FROM crs_objective_status ".
00275 "WHERE user_id = ".$ilDB->quote($user_id)." ";
00276 $ilDB->query($query);
00277
00278 $query = "DELETE FROM crs_objective_status_pretest ".
00279 "WHERE user_id = ".$ilDB->quote($user_id)." ";
00280 $ilDB->query($query);
00281 return true;
00282 }
00283
00284 function _updateObjectiveResult($a_user_id,$a_active_id,$a_question_id)
00285 {
00286
00287 if(!$objectives = ilCourseObjectiveResult::_readAssignedObjectivesOfQuestion($a_question_id))
00288 {
00289
00290
00291 return true;
00292 }
00293 ilCourseObjectiveResult::_updateObjectiveStatus($a_user_id,$objectives);
00294
00295 return true;
00296 }
00297
00298 function _readAssignedObjectivesOfQuestion($a_question_id)
00299 {
00300 global $ilDB;
00301
00302
00303 $query = "SELECT q2.question_id as qid,q2.objective_id as ob FROM crs_objective_qst as q1, ".
00304 "crs_objective_qst as q2 ".
00305 "WHERE q1.question_id = ".$ilDB->quote($a_question_id)." ".
00306 "AND q1.objective_id = q2.objective_id ";
00307
00308 $res = $ilDB->query($query);
00309 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00310 {
00311 $objectives['all_objectives'][$row->ob] = $row->ob;
00312 $objectives['all_questions'][$row->qid] = $row->qid;
00313 }
00314 if(!is_array($objectives))
00315 {
00316 return false;
00317 }
00318 $objectives['objectives'] = ilCourseObjectiveResult::_readAssignedObjectives($objectives['all_objectives']);
00319 return $objectives ? $objectives : array();
00320 }
00321
00322
00323 function _readAssignedObjectives($a_all_objectives)
00324 {
00325 global $ilDB;
00326
00327
00328 $query = "SELECT t.objective_id as obj,t.ref_id as ref, question_id,tst_status,tst_limit ".
00329 "FROM crs_objective_tst as t JOIN crs_objective_qst as q ".
00330 "ON (t.objective_id = q.objective_id AND t.ref_id = q.ref_id) ".
00331 "WHERE t.objective_id IN (".implode(",",ilUtil::quoteArray($a_all_objectives)).")";
00332
00333 $res = $ilDB->query($query);
00334 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00335 {
00336 $objectives[$row->obj."_".$row->ref]['questions'][$row->question_id] = $row->question_id;
00337 $objectives[$row->obj."_".$row->ref]['tst_status'] = $row->tst_status;
00338 $objectives[$row->obj."_".$row->ref]['tst_limit'] = $row->tst_limit;
00339 $objectives[$row->obj."_".$row->ref]['objective_id'] = $row->obj;
00340 }
00341 return $objectives ? $objectives : array();
00342 }
00343
00344 function _updateObjectiveStatus($a_user_id,$objectives)
00345 {
00346 global $ilDB,$ilUser;
00347
00348 if(!count($objectives['all_questions']) or
00349 !count($objectives['all_objectives']))
00350 {
00351 return false;
00352 }
00353
00354
00355 $query = "SELECT question_id,points FROM qpl_questions ".
00356 "WHERE question_id IN(".implode(",",ilUtil::quoteArray($objectives['all_questions'])).")";
00357 $res = $ilDB->query($query);
00358 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00359 {
00360 $objectives['all_question_points'][$row->question_id]['max_points'] = $row->points;
00361 }
00362
00363 $query = "SELECT question_fi, MAX(points) as reached FROM tst_test_result JOIN tst_active ".
00364 "ON (active_id = active_fi) ".
00365 "WHERE user_fi = ".$ilDB->quote($a_user_id)." ".
00366 "AND question_fi IN (".implode(",",ilUtil::quoteArray($objectives['all_questions'])).") ".
00367 "GROUP BY question_fi,user_fi";
00368 $res = $ilDB->query($query);
00369 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00370 {
00371 $objectives['all_question_points'][$row->question_fi]['reached_points'] = $row->reached;
00372 }
00373
00374
00375 $fullfilled = array();
00376 $pretest = array();
00377 foreach($objectives['objectives'] as $kind => $data)
00378 {
00379
00380 if(ilCourseObjectiveResult::__isFullfilled($objectives['all_question_points'],$data))
00381 {
00382
00383 if($data['tst_status'])
00384 {
00385 $fullfilled[] = array($data['objective_id'],$ilUser->getId(),$data['tst_status']);
00386 }
00387 else
00388 {
00389 $pretest[] = array($data['objective_id'],$ilUser->getId());
00390 }
00391 }
00392 }
00393 if(count($fullfilled))
00394 {
00395 $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status VALUES(?,?,?)"),
00396 $fullfilled);
00397 ilCourseObjectiveResult::__updatePassed($a_user_id,$objectives['all_objectives']);
00398 }
00399 if(count($pretest))
00400 {
00401 $ilDB->executeMultiple($ilDB->prepare("REPLACE INTO crs_objective_status_pretest VALUES(?,?)"),
00402 $pretest);
00403 }
00404
00405 return true;
00406 }
00407
00408 function __isFullfilled($question_points,$objective_data)
00409 {
00410 if(!is_array($objective_data['questions']))
00411 {
00412 return false;
00413 }
00414 $max_points = 0;
00415 $reached_points = 0;
00416 foreach($objective_data['questions'] as $question_id)
00417 {
00418 $max_points += $question_points[$question_id]['max_points'];
00419 $reached_points += $question_points[$question_id]['reached_points'];
00420 }
00421 if(!$max_points)
00422 {
00423 return false;
00424 }
00425 return (($reached_points / $max_points * 100) >= $objective_data['tst_limit']) ? true : false;
00426 }
00427
00428 function __updatePassed($a_user_id,$objective_ids)
00429 {
00430 global $ilDB;
00431
00432 $passed = array();
00433
00434 $query = "SELECT COUNT(t1.crs_id) AS num,t1.crs_id FROM crs_objectives as t1 ".
00435 "JOIN crs_objectives as t2 WHERE t1.crs_id = t2.crs_id and t1.objective_id ".
00436 "IN (".implode(",",ilUtil::quoteArray($objective_ids)).") ".
00437 "GROUP BY t1.crs_id";
00438 $res = $ilDB->query($query);
00439 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00440 {
00441 $query = "SELECT COUNT(cs.objective_id) AS num_passed FROM crs_objective_status AS cs ".
00442 "JOIN crs_objectives AS co ON cs.objective_id = co.objective_id ".
00443 "WHERE crs_id = ".$ilDB->quote($row->crs_id)." ".
00444 "AND user_id = ".$ilDB->quote($a_user_id)." ";
00445
00446 $user_res = $ilDB->query($query);
00447 while($user_row = $user_res->fetchRow(DB_FETCHMODE_OBJECT))
00448 {
00449 if($user_row->num_passed == $row->num)
00450 {
00451 $passed[] = $row->crs_id;
00452 }
00453 }
00454 }
00455 if(count($passed))
00456 {
00457 foreach($passed as $crs_id)
00458 {
00459 include_once('Modules/Course/classes/class.ilCourseParticipants.php');
00460 $members = ilCourseParticipants::_getInstanceByObjId($crs_id);
00461 $members->updatePassed($a_user_id,true);
00462 }
00463 }
00464 }
00465
00466 }
00467 ?>