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