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 include_once "./assessment/classes/inc.AssessmentConstants.php";
00025
00037 class ASS_Question
00038 {
00046 var $id;
00047
00055 var $title;
00056
00064 var $comment;
00065
00074 var $owner;
00075
00084 var $author;
00085
00095 var $points;
00096
00104 var $est_working_time;
00105
00113 var $shuffle;
00114
00122 var $test_id;
00123
00131 var $obj_id;
00132
00140 var $ilias;
00141
00149 var $tpl;
00150
00158 var $lng;
00159
00167 var $domxml;
00168
00176 var $outputType;
00177
00178 var $suggested_solutions;
00190 function ASS_Question(
00191 $title = "",
00192 $comment = "",
00193 $author = "",
00194 $owner = -1
00195 )
00196 {
00197 global $ilias;
00198 global $lng;
00199 global $tpl;
00200
00201 $this->ilias =& $ilias;
00202 $this->lng =& $lng;
00203 $this->tpl =& $tpl;
00204
00205 $this->title = $title;
00206 $this->comment = $comment;
00207 $this->author = $author;
00208 if (!$this->author)
00209 {
00210 $this->author = $this->ilias->account->fullname;
00211 }
00212 $this->owner = $owner;
00213 if ($this->owner == -1)
00214 {
00215 $this->owner = $this->ilias->account->id;
00216 }
00217 $this->id = -1;
00218 $this->test_id = -1;
00219 $this->suggested_solutions = array();
00220 $this->shuffle = 1;
00221 $this->setEstimatedWorkingTime(0,1,0);
00222 $this->outputType = OUTPUT_HTML;
00223 register_shutdown_function(array(&$this, '_ASS_Question'));
00224 }
00225
00226 function _ASS_Question()
00227 {
00228 if (!empty($this->domxml))
00229 {
00230 $this->domxml->free();
00231 }
00232 }
00233
00243 function to_xml()
00244 {
00245
00246 }
00247
00256 function isComplete()
00257 {
00258 return false;
00259 }
00260
00270 function questionTitleExists($questionpool_id, $title)
00271 {
00272 $query = sprintf("SELECT * FROM qpl_questions WHERE obj_fi = %s AND title = %s",
00273 $this->ilias->db->quote($questionpool_id . ""),
00274 $this->ilias->db->quote($title)
00275 );
00276 $result = $this->ilias->db->query($query);
00277 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00278 {
00279 if ($result->numRows() == 1)
00280 {
00281 return TRUE;
00282 }
00283 }
00284 return FALSE;
00285 }
00286
00296 function setTitle($title = "")
00297 {
00298 $this->title = $title;
00299 }
00300
00310 function setId($id = -1)
00311 {
00312 $this->id = $id;
00313 }
00314
00324 function setTestId($id = -1)
00325 {
00326 $this->test_id = $id;
00327 }
00328
00338 function setComment($comment = "")
00339 {
00340 $this->comment = $comment;
00341 }
00342
00352 function setOutputType($outputType = OUTPUT_HTML)
00353 {
00354 $this->outputType = $outputType;
00355 }
00356
00357
00367 function setShuffle($shuffle = true)
00368 {
00369 if ($shuffle)
00370 {
00371 $this->shuffle = 1;
00372 }
00373 else
00374 {
00375 $this->shuffle = 0;
00376 }
00377 }
00378
00390 function setEstimatedWorkingTime($hour=0, $min=0, $sec=0)
00391 {
00392 $this->est_working_time = array("h" => (int)$hour, "m" => (int)$min, "s" => (int)$sec);
00393 }
00394
00404 function keyInArray($searchkey, $array)
00405 {
00406 if ($searchKey)
00407 {
00408 foreach ($array as $key => $value)
00409 {
00410 if (strcmp($key, $searchkey)==0)
00411 {
00412 return true;
00413 }
00414 }
00415 }
00416 return false;
00417 }
00418
00428 function setAuthor($author = "")
00429 {
00430 if (!$author)
00431 {
00432 $author = $this->ilias->account->fullname;
00433 }
00434 $this->author = $author;
00435 }
00436
00446 function setOwner($owner = "")
00447 {
00448 $this->owner = $owner;
00449 }
00450
00460 function getTitle()
00461 {
00462 return $this->title;
00463 }
00464
00474 function getId()
00475 {
00476 return $this->id;
00477 }
00478
00488 function getShuffle()
00489 {
00490 return $this->shuffle;
00491 }
00492
00502 function getTestId()
00503 {
00504 return $this->test_id;
00505 }
00506
00516 function getComment()
00517 {
00518 return $this->comment;
00519 }
00520
00530 function getOutputType()
00531 {
00532 return $this->outputType;
00533 }
00534
00544 function getEstimatedWorkingTime()
00545 {
00546 if (!$this->est_working_time)
00547 {
00548 $this->est_working_time = array("h" => 0, "m" => 0, "s" => 0);
00549 }
00550 return $this->est_working_time;
00551 }
00552
00562 function getAuthor()
00563 {
00564 return $this->author;
00565 }
00566
00576 function getOwner()
00577 {
00578 return $this->owner;
00579 }
00580
00590 function getObjId()
00591 {
00592 return $this->obj_id;
00593 }
00594
00604 function setObjId($obj_id = 0)
00605 {
00606 $this->obj_id = $obj_id;
00607 }
00608
00612 function createPageObject()
00613 {
00614 $qpl_id = $this->getObjId();
00615
00616 include_once "./content/classes/Pages/class.ilPageObject.php";
00617 $this->page = new ilPageObject("qpl", 0);
00618 $this->page->setId($this->getId());
00619 $this->page->setParentId($qpl_id);
00620 $this->page->setXMLContent("<PageObject><PageContent>".
00621 "<Question QRef=\"il__qst_".$this->getId()."\"/>".
00622 "</PageContent></PageObject>");
00623 $this->page->create();
00624 }
00625
00634 function insertIntoTest($test_id)
00635 {
00636
00637 $query = sprintf("SELECT MAX(sequence) AS seq FROM dum_test_question WHERE test_fi=%s",
00638 $this->ilias->db->quote($test_id)
00639 );
00640 $result = $this->ilias->db->query($query);
00641 $sequence = 1;
00642 if ($result->numRows() == 1)
00643 {
00644 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00645 $sequence = $data->seq + 1;
00646 }
00647 $query = sprintf("INSERT INTO dum_test_question (test_question_id, test_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00648 $this->ilias->db->quote($test_id),
00649 $this->ilias->db->quote($this->getId()),
00650 $this->ilias->db->quote($sequence)
00651 );
00652 $result = $this->ilias->db->query($query);
00653 if ($result != DB_OK)
00654 {
00655
00656 }
00657 }
00658
00668 function _getMaximumPoints($question_id)
00669 {
00670 global $ilDB;
00671
00672 $points = 0;
00673 $query = sprintf("SELECT points FROM qpl_questions WHERE question_id = %s",
00674 $ilDB->quote($question_id . "")
00675 );
00676 $result = $ilDB->query($query);
00677 if ($result->numRows() == 1)
00678 {
00679 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00680 $points = $row["points"];
00681 }
00682 return $points;
00683 }
00684
00694 function &_getQuestionInfo($question_id)
00695 {
00696 global $ilDB;
00697
00698 $query = sprintf("SELECT qpl_questions.*, qpl_question_type.type_tag FROM qpl_question_type, qpl_questions WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
00699 $ilDB->quote($question_id . "")
00700 );
00701 $result = $ilDB->query($query);
00702 if ($result->numRows())
00703 {
00704 return $result->fetchRow(DB_FETCHMODE_ASSOC);
00705 }
00706 else return array();
00707 }
00708
00718 function _getSuggestedSolutionCount($question_id)
00719 {
00720 global $ilDB;
00721
00722 $query = sprintf("SELECT suggested_solution_id FROM qpl_suggested_solutions WHERE question_fi = %s",
00723 $ilDB->quote($question_id . "")
00724 );
00725 $result = $ilDB->query($query);
00726 return $result->numRows();
00727 }
00728
00739 function &_getSuggestedSolution($question_id, $subquestion_index = 0)
00740 {
00741 global $ilDB;
00742
00743 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s AND subquestion_index = %s",
00744 $ilDB->quote($question_id . ""),
00745 $ilDB->quote($subquestion_index . "")
00746 );
00747 $result = $ilDB->query($query);
00748 if ($result->numRows() == 1)
00749 {
00750 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00751 return array(
00752 "internal_link" => $row["internal_link"],
00753 "import_id" => $row["import_id"]
00754 );
00755 }
00756 else
00757 {
00758 return array();
00759 }
00760 }
00761
00772 function _getReachedPoints($user_id, $test_id, $question_id, $pass = NULL)
00773 {
00774 global $ilDB;
00775
00776 $points = 0;
00777 if (is_null($pass))
00778 {
00779 include_once "./assessment/classes/class.assQuestion.php";
00780 $pass = ASS_Question::_getSolutionMaxPass($question_id, $user_id, $test_id);
00781 }
00782 $query = sprintf("SELECT * FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s AND pass = %s",
00783 $ilDB->quote($user_id . ""),
00784 $ilDB->quote($test_id . ""),
00785 $ilDB->quote($question_id . ""),
00786 $ilDB->quote($pass . "")
00787 );
00788 $result = $ilDB->query($query);
00789 if ($result->numRows() == 1)
00790 {
00791 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00792 $points = $row["points"];
00793 }
00794 return $points;
00795 }
00796
00807 function getReachedPoints($user_id, $test_id, $pass = NULL)
00808 {
00809 global $ilDB;
00810
00811 $points = 0;
00812 if (is_null($pass))
00813 {
00814 $pass = $this->getSolutionMaxPass($user_id, $test_id);
00815 }
00816 $query = sprintf("SELECT * FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s AND pass = %s",
00817 $ilDB->quote($user_id . ""),
00818 $ilDB->quote($test_id . ""),
00819 $ilDB->quote($this->getId() . ""),
00820 $ilDB->quote($pass . "")
00821 );
00822 $result = $ilDB->query($query);
00823 if ($result->numRows() == 1)
00824 {
00825 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00826 $points = $row["points"];
00827 }
00828 return $points;
00829 }
00830
00839 function getMaximumPoints()
00840 {
00841 return 0;
00842 }
00843
00853 function saveWorkingData($test_id, $pass = NULL)
00854 {
00855 global $ilDB;
00856 global $ilUser;
00857 include_once "./assessment/classes/class.ilObjTest.php";
00858 $pass = ilObjTest::_getPass($ilUser->id, $test_id);
00859 $reached_points = $this->calculateReachedPoints($ilUser->id, $test_id, $pass);
00860 $query = sprintf("REPLACE INTO tst_test_result (user_fi, test_fi, question_fi, pass, points) VALUES (%s, %s, %s, %s, %s)",
00861 $ilDB->quote($ilUser->id . ""),
00862 $ilDB->quote($test_id . ""),
00863 $ilDB->quote($this->getId() . ""),
00864 $ilDB->quote($pass . ""),
00865 $ilDB->quote($reached_points . "")
00866 );
00867 $result = $ilDB->query($query);
00868 include_once ("./classes/class.ilObjAssessmentFolder.php");
00869 if (ilObjAssessmentFolder::_enabledAssessmentLogging())
00870 {
00871 $this->logAction(sprintf($this->lng->txtlng("assessment", "log_user_answered_question", ilObjAssessmentFolder::_getLogLanguage()), $reached_points), $test_id, $this->getId());
00872 }
00873 }
00874
00884 function logAction($logtext = "", $test_id = "", $question_id = "")
00885 {
00886 global $ilUser;
00887
00888 $original_id = "";
00889 if (strcmp($question_id, "") != 0)
00890 {
00891 include_once "./assessment/classes/class.assQuestion.php";
00892 $original_id = ASS_Question::_getOriginalId($question_id);
00893 }
00894 include_once "./classes/class.ilObjAssessmentFolder.php";
00895 include_once "./assessment/classes/class.ilObjTest.php";
00896 ilObjAssessmentFolder::_addLog($ilUser->id, ilObjTest::_getObjectIDFromTestID($test_id), $logtext, $question_id, $original_id);
00897 }
00898
00907 function getJavaPath() {
00908 return CLIENT_WEB_DIR . "/assessment/$this->obj_id/$this->id/java/";
00909 }
00910
00919 function getImagePath()
00920 {
00921 return CLIENT_WEB_DIR . "/assessment/$this->obj_id/$this->id/images/";
00922 }
00923
00932 function getJavaPathWeb()
00933 {
00934 include_once "./classes/class.ilUtil.php";
00935 $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/assessment/$this->obj_id/$this->id/java/";
00936 return str_replace(ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH), ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH), $webdir);
00937 }
00938
00947 function getImagePathWeb()
00948 {
00949 include_once "./classes/class.ilUtil.php";
00950 $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/assessment/$this->obj_id/$this->id/images/";
00951 return str_replace(ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH), ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH), $webdir);
00952 }
00953
00963 function &getSolutionValues($test_id, $ilUser, $pass = NULL)
00964 {
00965 global $ilDB;
00966
00967 $values = array();
00968
00969 if (is_null($pass))
00970 {
00971 $query = sprintf("SELECT MAX(pass) AS maxpass FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
00972 $ilDB->quote($ilUser->id . ""),
00973 $ilDB->quote($test_id . ""),
00974 $ilDB->quote($this->getId() . "")
00975 );
00976 $result = $ilDB->query($query);
00977 if ($result->numRows())
00978 {
00979 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00980 $pass = $row["maxpass"];
00981 }
00982 else
00983 {
00984 return $values;
00985 }
00986 }
00987
00988 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s AND pass = %s",
00989 $ilDB->quote($ilUser->id . ""),
00990 $ilDB->quote($test_id . ""),
00991 $ilDB->quote($this->getId() . ""),
00992 $ilDB->quote($pass . "")
00993 );
00994 $result = $ilDB->query($query);
00995 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00996 {
00997 array_push($values, $row);
00998 }
00999
01000 return $values;
01001 }
01002
01011 function isInUse($question_id = "")
01012 {
01013 if ($question_id < 1) $question_id = $this->id;
01014 $query = sprintf("SELECT COUNT(question_id) AS question_count FROM qpl_questions WHERE original_id = %s",
01015 $this->ilias->db->quote($question_id . "")
01016 );
01017 $result = $this->ilias->db->query($query);
01018 $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
01019 return $row->question_count;
01020 }
01021
01030 function isClone($question_id = "")
01031 {
01032 if ($question_id < 1) $question_id = $this->id;
01033 $query = sprintf("SELECT original_id FROM qpl_questions WHERE question_id = %s",
01034 $this->ilias->db->quote($question_id . "")
01035 );
01036 $result = $this->ilias->db->query($query);
01037 $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
01038 if ($row->original_id > 0)
01039 {
01040 return TRUE;
01041 }
01042 else
01043 {
01044 return FALSE;
01045 }
01046 }
01047
01056 function pcArrayShuffle($array)
01057 {
01058 mt_srand((double)microtime()*1000000);
01059 $i = count($array);
01060 if ($i > 0)
01061 {
01062 while(--$i)
01063 {
01064 $j = mt_rand(0, $i);
01065 if ($i != $j)
01066 {
01067
01068 $tmp = $array[$j];
01069 $array[$j] = $array[$i];
01070 $array[$i] = $tmp;
01071 }
01072 }
01073 }
01074 return $array;
01075 }
01076
01082 function getQuestionTypeFromDb($question_id)
01083 {
01084 global $ilDB;
01085
01086 $query = sprintf("SELECT qpl_question_type.type_tag FROM qpl_question_type, qpl_questions WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
01087 $ilDB->quote($question_id));
01088
01089 $result = $ilDB->query($query);
01090 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
01091
01092 return $data->type_tag;
01093 }
01094
01103 function delete($question_id)
01104 {
01105 if ($question_id < 1)
01106 return;
01107
01108 $query = sprintf("SELECT obj_fi FROM qpl_questions WHERE question_id = %s",
01109 $this->ilias->db->quote($question_id)
01110 );
01111 $result = $this->ilias->db->query($query);
01112 if ($result->numRows() == 1)
01113 {
01114 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01115 $obj_id = $row["obj_fi"];
01116 }
01117 else
01118 {
01119 return;
01120 }
01121
01122 include_once "./content/classes/Pages/class.ilPageObject.php";
01123 $page = new ilPageObject("qpl", $question_id);
01124 $page->delete();
01125
01126 $query = sprintf("DELETE FROM qpl_questions WHERE question_id = %s",
01127 $this->ilias->db->quote($question_id)
01128 );
01129 $result = $this->ilias->db->query($query);
01130 $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
01131 $this->ilias->db->quote($question_id)
01132 );
01133 $result = $this->ilias->db->query($query);
01134
01135
01136 $querydelete = sprintf("DELETE FROM tst_test_question WHERE question_fi = %s", $this->ilias->db->quote($question_id));
01137 $deleteresult = $this->ilias->db->query($querydelete);
01138
01139
01140 $querydelete = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s", $this->ilias->db->quote($question_id));
01141 $deleteresult = $this->ilias->db->query($querydelete);
01142
01143 $directory = CLIENT_WEB_DIR . "/assessment/" . $obj_id . "/$question_id";
01144 if (preg_match("/\d+/", $obj_id) and preg_match("/\d+/", $question_id) and is_dir($directory))
01145 {
01146 include_once "./classes/class.ilUtil.php";
01147 ilUtil::delDir($directory);
01148 }
01149 }
01150
01154 function getTotalAnswers()
01155 {
01156 return $this->_getTotalAnswers($this->id);
01157 }
01158
01165 function _getTotalAnswers($a_q_id)
01166 {
01167 global $ilDB;
01168
01169
01170 $query = sprintf("SELECT question_id FROM qpl_questions WHERE original_id = %s OR question_id = %s",
01171 $ilDB->quote($a_q_id),
01172 $ilDB->quote($a_q_id)
01173 );
01174
01175 $result = $ilDB->query($query);
01176
01177 if ($result->numRows() == 0)
01178 {
01179 return 0;
01180 }
01181 $found_id = array();
01182 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01183 {
01184 array_push($found_id, $row->question_id);
01185 }
01186
01187 $query = sprintf("SELECT * FROM tst_test_result WHERE question_fi IN (%s)",
01188 join($found_id, ","));
01189 $result = $ilDB->query($query);
01190
01191 return $result->numRows();
01192 }
01193
01194
01201 function _getTotalRightAnswers($a_q_id)
01202 {
01203 global $ilDB;
01204 $query = sprintf("SELECT question_id FROM qpl_questions WHERE original_id = %s OR question_id = %s",
01205 $ilDB->quote($a_q_id),
01206 $ilDB->quote($a_q_id)
01207 );
01208 $result = $ilDB->query($query);
01209 if ($result->numRows() == 0)
01210 {
01211 return 0;
01212 }
01213 $found_id = array();
01214 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01215 {
01216 array_push($found_id, $row->question_id);
01217 }
01218 $query = sprintf("SELECT * FROM tst_test_result WHERE question_fi IN (%s)",
01219 join($found_id, ","));
01220 $result = $ilDB->query($query);
01221 $answers = array();
01222 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01223 {
01224 $reached = $row->points;
01225 include_once "./assessment/classes/class.assQuestion.php";
01226 $max = ASS_Question::_getMaximumPoints($row->question_fi);
01227 array_push($answers, array("reached" => $reached, "max" => $max));
01228 }
01229 $max = 0.0;
01230 $reached = 0.0;
01231 foreach ($answers as $key => $value)
01232 {
01233 $max += $value["max"];
01234 $reached += $value["reached"];
01235 }
01236 if ($max > 0)
01237 {
01238 return $reached / $max;
01239 }
01240 else
01241 {
01242 return 0;
01243 }
01244 }
01245
01251 function _getTitle($a_q_id)
01252 {
01253 global $ilDB;
01254 $query = sprintf("SELECT title FROM qpl_questions WHERE question_id = %s",
01255 $ilDB->quote($a_q_id)
01256 );
01257 $result = $ilDB->query($query);
01258 if ($result->numRows() == 1)
01259 {
01260 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01261 return $row["title"];
01262 }
01263 else
01264 {
01265 return "";
01266 }
01267 }
01268
01269 function copyPageOfQuestion($a_q_id)
01270 {
01271 if ($a_q_id > 0)
01272 {
01273 include_once "./content/classes/Pages/class.ilPageObject.php";
01274 $page = new ilPageObject("qpl", $a_q_id);
01275
01276 $xml = str_replace("il__qst_".$a_q_id, "il__qst_".$this->id,
01277 $page->getXMLContent());
01278 $this->page->setXMLContent($xml);
01279 $this->page->saveMobUsage($xml);
01280 $this->page->updateFromXML();
01281 }
01282 }
01283
01284 function getPageOfQuestion()
01285 {
01286 include_once "./content/classes/Pages/class.ilPageObject.php";
01287 $page = new ilPageObject("qpl", $this->id);
01288 return $page->getXMLContent();
01289 }
01290
01300 function _getQuestionType($question_id) {
01301 global $ilDB;
01302
01303 if ($question_id < 1)
01304 return "";
01305
01306 $query = sprintf("SELECT type_tag FROM qpl_questions, qpl_question_type WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
01307 $ilDB->quote($question_id)
01308 );
01309 $result = $ilDB->query($query);
01310 if ($result->numRows() == 1) {
01311 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
01312 return $data->type_tag;
01313 } else {
01314 return "";
01315 }
01316 }
01317
01327 function _getQuestionTitle($question_id) {
01328 global $ilDB;
01329
01330 if ($question_id < 1)
01331 return "";
01332
01333 $query = sprintf("SELECT title FROM qpl_questions WHERE qpl_questions.question_id = %s",
01334 $ilDB->quote($question_id)
01335 );
01336 $result = $ilDB->query($query);
01337 if ($result->numRows() == 1) {
01338 $data = $result->fetchRow(DB_FETCHMODE_ASSOC);
01339 return $data["title"];
01340 } else {
01341 return "";
01342 }
01343 }
01344
01353 function loadFromDb($question_id)
01354 {
01355 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01356 $this->ilias->db->quote($this->getId() . "")
01357 );
01358 $result = $this->ilias->db->query($query);
01359 $this->suggested_solutions = array();
01360 if ($result->numRows())
01361 {
01362 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01363 {
01364 $this->suggested_solutions[$row["subquestion_index"]] = array(
01365 "internal_link" => $row["internal_link"],
01366 "import_id" => $row["import_id"]
01367 );
01368 }
01369 }
01370 }
01371
01380 function saveToDb($original_id = "")
01381 {
01382 include_once "./content/classes/Pages/class.ilInternalLink.php";
01383 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01384 $this->ilias->db->quote($this->getId() . "")
01385 );
01386 $result = $this->ilias->db->query($query);
01387 ilInternalLink::_deleteAllLinksOfSource("qst", $this->getId());
01388 foreach ($this->suggested_solutions as $index => $solution)
01389 {
01390 $query = sprintf("INSERT INTO qpl_suggested_solutions (suggested_solution_id, question_fi, internal_link, import_id, subquestion_index, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
01391 $this->ilias->db->quote($this->getId() . ""),
01392 $this->ilias->db->quote($solution["internal_link"] . ""),
01393 $this->ilias->db->quote($solution["import_id"] . ""),
01394 $this->ilias->db->quote($index . "")
01395 );
01396 $this->ilias->db->query($query);
01397 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
01398 {
01399 ilInternalLink::_saveLink("qst", $this->getId(), $matches[2], $matches[3], $matches[1]);
01400 }
01401 }
01402 }
01403
01411 function deleteSuggestedSolutions()
01412 {
01413
01414 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01415 $this->ilias->db->quote($this->getId() . "")
01416 );
01417 $result = $this->ilias->db->query($query);
01418
01419 include_once "./content/classes/Pages/class.ilInternalLink.php";
01420 ilInternalLink::_deleteAllLinksOfSource("qst", $this->getId());
01421 }
01422
01432 function getSuggestedSolution($subquestion_index = 0)
01433 {
01434 if (array_key_exists($subquestion_index, $this->suggested_solutions))
01435 {
01436 return $this->suggested_solutions[$subquestion_index];
01437 }
01438 else
01439 {
01440 return array();
01441 }
01442 }
01443
01454 function getSuggestedSolutionTitle($subquestion_index = 0)
01455 {
01456 if (array_key_exists($subquestion_index, $this->suggested_solutions))
01457 {
01458 $title = $this->suggested_solutions[$subquestion_index]["internal_link"];
01459
01460 }
01461 else
01462 {
01463 $title = "";
01464 }
01465 return $title;
01466 }
01467
01479 function setSuggestedSolution($solution_id = "", $subquestion_index = 0, $is_import = false)
01480 {
01481 if (strcmp($solution_id, "") != 0)
01482 {
01483 $import_id = "";
01484 if ($is_import)
01485 {
01486 $import_id = $solution_id;
01487 $solution_id = $this->_resolveInternalLink($import_id);
01488 }
01489 $this->suggested_solutions[$subquestion_index] = array(
01490 "internal_link" => $solution_id,
01491 "import_id" => $import_id
01492 );
01493 }
01494 }
01495
01496 function _resolveInternalLink($internal_link)
01497 {
01498 if (preg_match("/il_(\d+)_(\w+)_(\d+)/", $internal_link, $matches))
01499 {
01500 include_once "./content/classes/Pages/class.ilInternalLink.php";
01501 include_once "./content/classes/class.ilLMObject.php";
01502 include_once "./content/classes/class.ilGlossaryTerm.php";
01503 switch ($matches[2])
01504 {
01505 case "lm":
01506 $resolved_link = ilLMObject::_getIdForImportId($internal_link);
01507 break;
01508 case "pg":
01509 $resolved_link = ilInternalLink::_getIdForImportId("PageObject", $internal_link);
01510 break;
01511 case "st":
01512 $resolved_link = ilInternalLink::_getIdForImportId("StructureObject", $internal_link);
01513 break;
01514 case "git":
01515 $resolved_link = ilInternalLink::_getIdForImportId("GlossaryItem", $internal_link);
01516 break;
01517 case "mob":
01518 $resolved_link = ilInternalLink::_getIdForImportId("MediaObject", $internal_link);
01519 break;
01520 }
01521 if (strcmp($resolved_link, "") == 0)
01522 {
01523 $resolved_link = $internal_link;
01524 }
01525 }
01526 else
01527 {
01528 $resolved_link = $internal_link;
01529 }
01530 return $resolved_link;
01531 }
01532
01533 function _resolveIntLinks($question_id)
01534 {
01535 global $ilDB;
01536 $resolvedlinks = 0;
01537 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01538 $ilDB->quote($question_id . "")
01539 );
01540 $result = $ilDB->query($query);
01541 if ($result->numRows())
01542 {
01543 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01544 {
01545 $internal_link = $row["internal_link"];
01546 include_once "./assessment/classes/class.assQuestion.php";
01547 $resolved_link = ASS_Question::_resolveInternalLink($internal_link);
01548 if (strcmp($internal_link, $resolved_link) != 0)
01549 {
01550
01551 $queryupdate = sprintf("UPDATE qpl_suggested_solutions SET internal_link = %s WHERE suggested_solution_id = %s",
01552 $ilDB->quote($resolved_link),
01553 $ilDB->quote($row["suggested_solution_id"] . "")
01554 );
01555 $updateresult = $ilDB->query($queryupdate);
01556 $resolvedlinks++;
01557 }
01558 }
01559 }
01560 if ($resolvedlinks)
01561 {
01562
01563
01564
01565 include_once "./content/classes/Pages/class.ilInternalLink.php";
01566 ilInternalLink::_deleteAllLinksOfSource("qst", $question_id);
01567
01568 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01569 $ilDB->quote($question_id . "")
01570 );
01571 $result = $ilDB->query($query);
01572 if ($result->numRows())
01573 {
01574 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01575 {
01576 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $row["internal_link"], $matches))
01577 {
01578 ilInternalLink::_saveLink("qst", $question_id, $matches[2], $matches[3], $matches[1]);
01579 }
01580 }
01581 }
01582 }
01583 }
01584
01585 function _getInternalLinkHref($target = "")
01586 {
01587 global $ilDB;
01588 $linktypes = array(
01589 "lm" => "LearningModule",
01590 "pg" => "PageObject",
01591 "st" => "StructureObject",
01592 "git" => "GlossaryItem",
01593 "mob" => "MediaObject"
01594 );
01595 $href = "";
01596 if (preg_match("/il__(\w+)_(\d+)/", $target, $matches))
01597 {
01598 $type = $matches[1];
01599 $target_id = $matches[2];
01600 include_once "./classes/class.ilUtil.php";
01601 switch($linktypes[$matches[1]])
01602 {
01603 case "LearningModule":
01604 $href = "./goto.php?target=" . $type . "_" . $target_id;
01605 break;
01606 case "PageObject":
01607 case "StructureObject":
01608 $href = "./goto.php?target=" . $type . "_" . $target_id;
01609 break;
01610 case "GlossaryItem":
01611 $href = "./goto.php?target=" . $type . "_" . $target_id;
01612 break;
01613 case "MediaObject":
01614 $href = "./content/lm_presentation.php?obj_type=" . $linktypes[$type] . "&cmd=media&ref_id=".$_GET["ref_id"]."&mob_id=".$target_id;
01615 break;
01616 }
01617 }
01618 return $href;
01619 }
01620
01630 function _getOriginalId($question_id)
01631 {
01632 global $ilDB;
01633 $query = sprintf("SELECT * FROM qpl_questions WHERE question_id = %s",
01634 $ilDB->quote($question_id . "")
01635 );
01636 $result = $ilDB->query($query);
01637 if ($result->numRows() > 0)
01638 {
01639 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01640 if ($row["original_id"] > 0)
01641 {
01642 return $row["original_id"];
01643 }
01644 else
01645 {
01646 return $row["question_id"];
01647 }
01648 }
01649 else
01650 {
01651 return "";
01652 }
01653 }
01654
01655 function syncWithOriginal()
01656 {
01657 include_once "./content/classes/Pages/class.ilInternalLink.php";
01658 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01659 $this->ilias->db->quote($this->original_id . "")
01660 );
01661 $result = $this->ilias->db->query($query);
01662 ilInternalLink::_deleteAllLinksOfSource("qst", $this->original_id);
01663 foreach ($this->suggested_solutions as $index => $solution)
01664 {
01665 $query = sprintf("INSERT INTO qpl_suggested_solutions (suggested_solution_id, question_fi, internal_link, import_id, subquestion_index, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
01666 $this->ilias->db->quote($this->original_id . ""),
01667 $this->ilias->db->quote($solution["internal_link"] . ""),
01668 $this->ilias->db->quote($solution["import_id"] . ""),
01669 $this->ilias->db->quote($index . "")
01670 );
01671 $this->ilias->db->query($query);
01672 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
01673 {
01674 ilInternalLink::_saveLink("qst", $this->original_id, $matches[2], $matches[3], $matches[1]);
01675 }
01676 }
01677 }
01678
01679 function createRandomSolution($test_id, $user_id)
01680 {
01681 }
01682
01692 function _questionExists($question_id)
01693 {
01694 global $ilDB;
01695
01696 if ($question_id < 1)
01697 {
01698 return false;
01699 }
01700
01701 $query = sprintf("SELECT question_id FROM qpl_questions WHERE question_id = %s",
01702 $ilDB->quote($question_id)
01703 );
01704 $result = $ilDB->query($query);
01705 if ($result->numRows() == 1)
01706 {
01707 return true;
01708 }
01709 else
01710 {
01711 return false;
01712 }
01713 }
01714
01724 function &_instanciateQuestion($question_id)
01725 {
01726 if (strcmp($question_id, "") != 0)
01727 {
01728 include_once "./assessment/classes/class.assQuestion.php";
01729 $question_type = ASS_Question::_getQuestionType($question_id);
01730 switch ($question_type) {
01731 case "qt_cloze":
01732 include_once "./assessment/classes/class.assClozeTest.php";
01733 $question = new ASS_ClozeTest();
01734 break;
01735 case "qt_matching":
01736 include_once "./assessment/classes/class.assMatchingQuestion.php";
01737 $question = new ASS_MatchingQuestion();
01738 break;
01739 case "qt_ordering":
01740 include_once "./assessment/classes/class.assOrderingQuestion.php";
01741 $question = new ASS_OrderingQuestion();
01742 break;
01743 case "qt_imagemap":
01744 include_once "./assessment/classes/class.assImagemapQuestion.php";
01745 $question = new ASS_ImagemapQuestion();
01746 break;
01747 case "qt_multiple_choice_sr":
01748 case "qt_multiple_choice_mr":
01749 include_once "./assessment/classes/class.assMultipleChoice.php";
01750 $question = new ASS_MultipleChoice();
01751 break;
01752 case "qt_javaapplet":
01753 include_once "./assessment/classes/class.assJavaApplet.php";
01754 $question = new ASS_JavaApplet();
01755 break;
01756 case "qt_text":
01757 include_once "./assessment/classes/class.assTextQuestion.php";
01758 $question = new ASS_TextQuestion();
01759 break;
01760 }
01761 $question->loadFromDb($question_id);
01762 return $question;
01763 }
01764 }
01765
01774 function getPoints()
01775 {
01776 if (strcmp($this->points, "") == 0)
01777 {
01778 return 0;
01779 }
01780 else
01781 {
01782 return $this->points;
01783 }
01784 }
01785
01786
01795 function setPoints($a_points)
01796 {
01797 $this->points = $a_points;
01798 }
01799
01800 function getSolutionCommentMCScoring($test_id)
01801 {
01802 $result = "";
01803 include_once "./assessment/classes/class.ilObjTest.php";
01804 if (ilObjTest::_getMCScoring($test_id) == SCORE_ZERO_POINTS_WHEN_UNANSWERED)
01805 {
01806 $result = $this->lng->txt("solution_comment_mc_scoring");
01807 }
01808 return $result;
01809 }
01810
01811 function getSolutionCommentCountSystem($test_id)
01812 {
01813 $result = "";
01814 include_once "./assessment/classes/class.ilObjTest.php";
01815 if (ilObjTest::_getCountSystem($test_id) == COUNT_CORRECT_SOLUTIONS )
01816 {
01817 $result = $this->lng->txt("solution_comment_count_system");
01818 }
01819 return $result;
01820 }
01821
01830 function getSolutionMaxPass($user_id, $test_id)
01831 {
01832 return $this->_getSolutionMaxPass($this->getId(), $user_id, $test_id);
01833 }
01834
01843 function _getSolutionMaxPass($question_id, $user_id, $test_id)
01844 {
01845 global $ilDB;
01846
01847 $query = sprintf("SELECT MAX(pass) as maxpass FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01848 $ilDB->quote($user_id . ""),
01849 $ilDB->quote($test_id . ""),
01850 $ilDB->quote($question_id . "")
01851 );
01852 $result = $ilDB->query($query);
01853 if ($result->numRows() == 1)
01854 {
01855 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01856 return $row["maxpass"];
01857 }
01858 else
01859 {
01860 return 0;
01861 }
01862 }
01863
01874 function _isWriteable($question_id, $user_id)
01875 {
01876 global $ilDB;
01877
01878 if (($question_id < 1) || ($user_id < 1))
01879 {
01880 return false;
01881 }
01882
01883 $query = sprintf("SELECT obj_fi FROM qpl_questions WHERE question_id = %s",
01884 $ilDB->quote($question_id . "")
01885 );
01886 $result = $ilDB->query($query);
01887 if ($result->numRows() == 1)
01888 {
01889 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01890 $qpl_object_id = $row["obj_fi"];
01891 include_once "./assessment/classes/class.ilObjQuestionPool.php";
01892 return ilObjQuestionPool::_isWriteable($qpl_object_id, $user_id);
01893 }
01894 else
01895 {
01896 return false;
01897 }
01898 }
01899
01908 function _isUsedInRandomTest($question_id = "")
01909 {
01910 global $ilDB;
01911
01912 if ($question_id < 1) return 0;
01913 $query = sprintf("SELECT test_random_question_id FROM tst_test_random_question WHERE question_fi = %s",
01914 $ilDB->quote($question_id . "")
01915 );
01916 $result = $ilDB->query($query);
01917 return $result->numRows();
01918 }
01919
01931 function _isWorkedThrough($user_id, $test_id, $question_id, $pass = NULL)
01932 {
01933 global $ilDB;
01934
01935 $points = 0;
01936 if (is_null($pass))
01937 {
01938 include_once "./assessment/classes/class.assQuestion.php";
01939 $pass = ASS_Question::_getSolutionMaxPass($question_id, $user_id, $test_id);
01940 }
01941 $query = sprintf("SELECT solution_id FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s AND pass = %s",
01942 $ilDB->quote($user_id . ""),
01943 $ilDB->quote($test_id . ""),
01944 $ilDB->quote($question_id . ""),
01945 $ilDB->quote($pass . "")
01946 );
01947 $result = $ilDB->query($query);
01948 if ($result->numRows())
01949 {
01950 return TRUE;
01951 }
01952 else
01953 {
01954 return FALSE;
01955 }
01956 }
01957
01958 }
01959
01960 ?>