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 "./Modules/TestQuestionPool/classes/class.assQuestion.php";
00025 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
00026
00036 class assImagemapQuestion extends assQuestion
00037 {
00038
00046 var $answers;
00047
00055 var $imagemap_filename;
00056
00064 var $image_filename;
00065
00073 var $imagemap_contents;
00074 var $coords;
00075
00090 function assImagemapQuestion(
00091 $title = "",
00092 $comment = "",
00093 $author = "",
00094 $owner = -1,
00095 $question = "",
00096 $imagemap_filename = "",
00097 $image_filename = ""
00098
00099 )
00100 {
00101 $this->assQuestion($title, $comment, $author, $owner, $question);
00102 $this->imagemap_filename = $imagemap_filename;
00103 $this->image_filename = $image_filename;
00104 $this->answers = array();
00105 $this->coords = array();
00106 }
00107
00116 function isComplete()
00117 {
00118 if (($this->title) and ($this->author) and ($this->question) and ($this->image_filename) and (count($this->answers)) and ($this->getMaximumPoints() > 0))
00119 {
00120 return true;
00121 }
00122 else
00123 {
00124 return false;
00125 }
00126 }
00127
00136 function saveToDb($original_id = "")
00137 {
00138 global $ilDB;
00139
00140 $complete = 0;
00141 if ($this->isComplete())
00142 {
00143 $complete = 1;
00144 }
00145
00146 $estw_time = $this->getEstimatedWorkingTime();
00147 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
00148 if ($original_id)
00149 {
00150 $original_id = $ilDB->quote($original_id);
00151 }
00152 else
00153 {
00154 $original_id = "NULL";
00155 }
00156
00157
00158 include_once("./Services/RTE/classes/class.ilRTE.php");
00159 if ($this->id == -1)
00160 {
00161
00162 $now = getdate();
00163 $question_type = $this->getQuestionTypeID();
00164 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00165 $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, working_time, points, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00166 $ilDB->quote($question_type),
00167 $ilDB->quote($this->obj_id),
00168 $ilDB->quote($this->title),
00169 $ilDB->quote($this->comment),
00170 $ilDB->quote($this->author),
00171 $ilDB->quote($this->owner),
00172 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
00173 $ilDB->quote($estw_time),
00174 $ilDB->quote($this->getMaximumPoints() . ""),
00175 $ilDB->quote("$complete"),
00176 $ilDB->quote($created),
00177 $original_id
00178 );
00179 $result = $ilDB->query($query);
00180 if ($result == DB_OK)
00181 {
00182 $this->id = $ilDB->getLastInsertId();
00183 $insertquery = sprintf("INSERT INTO qpl_question_imagemap (question_fi, image_file) VALUES (%s, %s)",
00184 $ilDB->quote($this->id . ""),
00185 $ilDB->quote($this->image_filename)
00186 );
00187 $ilDB->query($insertquery);
00188
00189 $this->createPageObject();
00190
00191 if ($this->getTestId() > 0)
00192 {
00193 $this->insertIntoTest($this->getTestId());
00194 }
00195 }
00196 }
00197 else
00198 {
00199
00200 $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, working_time = %s, points = %s, complete = %s WHERE question_id = %s",
00201 $ilDB->quote($this->obj_id. ""),
00202 $ilDB->quote($this->title),
00203 $ilDB->quote($this->comment),
00204 $ilDB->quote($this->author),
00205 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
00206 $ilDB->quote($estw_time),
00207 $ilDB->quote($this->getMaximumPoints() . ""),
00208 $ilDB->quote("$complete"),
00209 $ilDB->quote($this->id)
00210 );
00211 $result = $ilDB->query($query);
00212 $query = sprintf("UPDATE qpl_question_imagemap SET image_file = %s WHERE question_fi = %s",
00213 $ilDB->quote($this->image_filename),
00214 $ilDB->quote($this->id)
00215 );
00216 $result = $ilDB->query($query);
00217
00218 }
00219
00220 if ($result == DB_OK)
00221 {
00222 $query = sprintf("DELETE FROM qpl_answer_imagemap WHERE question_fi = %s",
00223 $ilDB->quote($this->id)
00224 );
00225 $result = $ilDB->query($query);
00226
00227 foreach ($this->answers as $key => $value)
00228 {
00229 $answer_obj = $this->answers[$key];
00230
00231 $query = sprintf("INSERT INTO qpl_answer_imagemap (answer_id, question_fi, answertext, points, aorder, correctness, coords, area) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s)",
00232 $ilDB->quote($this->id),
00233 $ilDB->quote($answer_obj->getAnswertext() . ""),
00234 $ilDB->quote($answer_obj->getPoints() . ""),
00235 $ilDB->quote($answer_obj->getOrder() . ""),
00236 $ilDB->quote($answer_obj->getState() . ""),
00237 $ilDB->quote($answer_obj->getCoords() . ""),
00238 $ilDB->quote($answer_obj->getArea() . "")
00239 );
00240 $answer_result = $ilDB->query($query);
00241 }
00242 }
00243 parent::saveToDb($original_id);
00244 }
00245
00253 function duplicate($for_test = true, $title = "", $author = "", $owner = "")
00254 {
00255 if ($this->id <= 0)
00256 {
00257
00258 return;
00259 }
00260
00261 $this_id = $this->getId();
00262 $clone = $this;
00263 include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
00264 $original_id = assQuestion::_getOriginalId($this->id);
00265 $clone->id = -1;
00266 if ($title)
00267 {
00268 $clone->setTitle($title);
00269 }
00270 if ($author)
00271 {
00272 $clone->setAuthor($author);
00273 }
00274 if ($owner)
00275 {
00276 $clone->setOwner($owner);
00277 }
00278 if ($for_test)
00279 {
00280 $clone->saveToDb($original_id);
00281 }
00282 else
00283 {
00284 $clone->saveToDb();
00285 }
00286
00287
00288 $clone->copyPageOfQuestion($this_id);
00289
00290 $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
00291
00292 $clone->duplicateFeedbackGeneric($this_id);
00293
00294 $clone->duplicateFeedbackAnswer($this_id);
00295
00296
00297 $clone->duplicateImage($this_id);
00298 return $clone->id;
00299 }
00300
00308 function copyObject($target_questionpool, $title = "")
00309 {
00310 if ($this->id <= 0)
00311 {
00312
00313 return;
00314 }
00315
00316 $clone = $this;
00317 include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
00318 $original_id = assQuestion::_getOriginalId($this->id);
00319 $clone->id = -1;
00320 $source_questionpool = $this->getObjId();
00321 $clone->setObjId($target_questionpool);
00322 if ($title)
00323 {
00324 $clone->setTitle($title);
00325 }
00326 $clone->saveToDb();
00327
00328
00329 $clone->copyPageOfQuestion($original_id);
00330
00331 $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
00332
00333 $clone->duplicateFeedbackGeneric($original_id);
00334
00335 $clone->duplicateFeedbackAnswer($original_id);
00336
00337
00338 $clone->copyImage($original_id, $source_questionpool);
00339 return $clone->id;
00340 }
00341
00342 function duplicateImage($question_id)
00343 {
00344 $imagepath = $this->getImagePath();
00345 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
00346 if (!file_exists($imagepath)) {
00347 ilUtil::makeDirParents($imagepath);
00348 }
00349 $filename = $this->get_image_filename();
00350 if (!copy($imagepath_original . $filename, $imagepath . $filename)) {
00351 print "image could not be duplicated!!!! ";
00352 }
00353 }
00354
00355 function copyImage($question_id, $source_questionpool)
00356 {
00357 $imagepath = $this->getImagePath();
00358 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
00359 $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
00360 if (!file_exists($imagepath))
00361 {
00362 ilUtil::makeDirParents($imagepath);
00363 }
00364 $filename = $this->get_image_filename();
00365 if (!copy($imagepath_original . $filename, $imagepath . $filename))
00366 {
00367 print "image could not be copied!!!! ";
00368 }
00369 }
00370
00380 function loadFromDb($question_id)
00381 {
00382 global $ilDB;
00383
00384 $query = sprintf("SELECT qpl_questions.*, qpl_question_imagemap.* FROM qpl_questions, qpl_question_imagemap WHERE question_id = %s AND qpl_questions.question_id = qpl_question_imagemap.question_fi",
00385 $ilDB->quote($question_id)
00386 );
00387 $result = $ilDB->query($query);
00388 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00389 if ($result->numRows() == 1) {
00390 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00391 $this->id = $question_id;
00392 $this->obj_id = $data->obj_fi;
00393 $this->title = $data->title;
00394 $this->comment = $data->comment;
00395 $this->author = $data->author;
00396 $this->original_id = $data->original_id;
00397 $this->solution_hint = $data->solution_hint;
00398 $this->owner = $data->owner;
00399 include_once("./Services/RTE/classes/class.ilRTE.php");
00400 $this->question = ilRTE::_replaceMediaObjectImageSrc($data->question_text, 1);
00401 $this->image_filename = $data->image_file;
00402 $this->points = $data->points;
00403 $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
00404 }
00405 $query = sprintf("SELECT * FROM qpl_answer_imagemap WHERE question_fi = %s ORDER BY aorder ASC",
00406 $ilDB->quote($question_id)
00407 );
00408 $result = $ilDB->query($query);
00409 include_once "./Modules/TestQuestionPool/classes/class.assAnswerImagemap.php";
00410 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00411 {
00412 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00413 {
00414 array_push($this->answers, new ASS_AnswerImagemap($data->answertext, $data->points, $data->aorder, $data->coords, $data->area));
00415 }
00416 }
00417 }
00418 parent::loadFromDb($question_id);
00419 }
00420
00430 function get_imagemap_filename() {
00431 return $this->imagemap_filename;
00432 }
00433
00443 function setImagemapFilename($imagemap_filename, $imagemap_tempfilename = "") {
00444 if (!empty($imagemap_filename)) {
00445 $this->imagemap_filename = $imagemap_filename;
00446 }
00447 if (!empty($imagemap_tempfilename)) {
00448 $fp = fopen($imagemap_tempfilename, "r");
00449 $contents = fread($fp, filesize($imagemap_tempfilename));
00450 fclose($fp);
00451 if (preg_match_all("/<area(.+)>/siU", $contents, $matches)) {
00452 for ($i=0; $i< count($matches[1]); $i++) {
00453 preg_match("/alt\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $alt);
00454 preg_match("/coords\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $coords);
00455 preg_match("/shape\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $shape);
00456 $this->addAnswer($alt[1], 0.0, count($this->answers), $coords[1], $shape[1]);
00457 }
00458 }
00459 }
00460 }
00461
00471 function get_image_filename() {
00472 return $this->image_filename;
00473 }
00474
00475 function getImageFilename()
00476 {
00477 return $this->image_filename;
00478 }
00479
00489 function setImageFilename($image_filename, $image_tempfilename = "")
00490 {
00491 if (!empty($image_filename))
00492 {
00493 $image_filename = str_replace(" ", "_", $image_filename);
00494 $this->image_filename = $image_filename;
00495 }
00496 if (!empty($image_tempfilename))
00497 {
00498 $imagepath = $this->getImagePath();
00499 if (!file_exists($imagepath))
00500 {
00501 ilUtil::makeDirParents($imagepath);
00502 }
00503 if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath.$image_filename))
00504 {
00505 $this->ilias->raiseError("The image could not be uploaded!", $this->ilias->error_obj->MESSAGE);
00506 }
00507 global $ilLog; $ilLog->write("gespeichert: " . $imagepath.$image_filename);
00508 }
00509 }
00510
00520 function get_imagemap_contents($href = "#") {
00521 $imagemap_contents = "<map name=\"".$this->title."\"> ";
00522 for ($i = 0; $i < count($this->answers); $i++) {
00523 $imagemap_contents .= "<area alt=\"".$this->answers[$i]->getAnswertext()."\" ";
00524 $imagemap_contents .= "shape=\"".$this->answers[$i]->getArea()."\" ";
00525 $imagemap_contents .= "coords=\"".$this->answers[$i]->getCoords()."\" ";
00526 $imagemap_contents .= "href=\"$href&selimage=" . $this->answers[$i]->getOrder() . "\" /> ";
00527 }
00528 $imagemap_contents .= "</map>";
00529 return $imagemap_contents;
00530 }
00531
00546 function addAnswer(
00547 $answertext = "",
00548 $points = 0.0,
00549 $order = 0,
00550 $coords="",
00551 $area=""
00552 )
00553 {
00554 include_once "./Modules/TestQuestionPool/classes/class.assAnswerImagemap.php";
00555 if (array_key_exists($order, $this->answers))
00556 {
00557
00558 $answer = new ASS_AnswerImagemap($answertext, $points, $order, $coords, $area);
00559 for ($i = count($this->answers) - 1; $i >= $order; $i--)
00560 {
00561 $this->answers[$i+1] = $this->answers[$i];
00562 $this->answers[$i+1]->setOrder($i+1);
00563 }
00564 $this->answers[$order] = $answer;
00565 }
00566 else
00567 {
00568
00569 $answer = new ASS_AnswerImagemap($answertext, $points, count($this->answers), $coords, $area);
00570 array_push($this->answers, $answer);
00571 }
00572 }
00573
00583 function getAnswerCount() {
00584 return count($this->answers);
00585 }
00586
00598 function getAnswer($index = 0) {
00599 if ($index < 0) return NULL;
00600 if (count($this->answers) < 1) return NULL;
00601 if ($index >= count($this->answers)) return NULL;
00602 return $this->answers[$index];
00603 }
00604
00614 function &getAnswers()
00615 {
00616 return $this->answers;
00617 }
00618
00629 function deleteArea($index = 0) {
00630 if ($index < 0) return;
00631 if (count($this->answers) < 1) return;
00632 if ($index >= count($this->answers)) return;
00633 unset($this->answers[$index]);
00634 $this->answers = array_values($this->answers);
00635 for ($i = 0; $i < count($this->answers); $i++) {
00636 if ($this->answers[$i]->getOrder() > $index) {
00637 $this->answers[$i]->setOrder($i);
00638 }
00639 }
00640 }
00641
00650 function flushAnswers() {
00651 $this->answers = array();
00652 }
00653
00662 function getMaximumPoints() {
00663 $points = 0;
00664 foreach ($this->answers as $key => $value) {
00665 if ($value->getPoints() > $points)
00666 {
00667 $points = $value->getPoints();
00668 }
00669 }
00670 return $points;
00671 }
00672
00684 function calculateReachedPoints($active_id, $pass = NULL)
00685 {
00686 global $ilDB;
00687
00688 $found_values = array();
00689 if (is_null($pass))
00690 {
00691 $pass = $this->getSolutionMaxPass($active_id);
00692 }
00693 $query = sprintf("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
00694 $ilDB->quote($active_id . ""),
00695 $ilDB->quote($this->getId() . ""),
00696 $ilDB->quote($pass . "")
00697 );
00698 $result = $ilDB->query($query);
00699 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00700 {
00701 if (strcmp($data->value1, "") != 0)
00702 {
00703 array_push($found_values, $data->value1);
00704 }
00705 }
00706 $points = 0;
00707 if (count($found_values) > 0)
00708 {
00709 foreach ($this->answers as $key => $answer)
00710 {
00711 if (in_array($key, $found_values))
00712 {
00713 $points += $answer->getPoints();
00714 }
00715 }
00716 }
00717
00718 $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
00719 return $points;
00720 }
00721
00732 function saveWorkingData($active_id, $pass = NULL)
00733 {
00734 global $ilDB;
00735 global $ilUser;
00736
00737 if (is_null($pass))
00738 {
00739 include_once "./Modules/Test/classes/class.ilObjTest.php";
00740 $pass = ilObjTest::_getPass($active_id);
00741 }
00742
00743 $query = sprintf("DELETE FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
00744 $ilDB->quote($active_id . ""),
00745 $ilDB->quote($this->getId() . ""),
00746 $ilDB->quote($pass . "")
00747 );
00748 $result = $ilDB->query($query);
00749
00750 if (strlen($_GET["selImage"]))
00751 {
00752 $query = sprintf("INSERT INTO tst_solutions (solution_id, active_fi, question_fi, value1, value2, pass, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL, %s, NULL)",
00753 $ilDB->quote($active_id),
00754 $ilDB->quote($this->getId()),
00755 $ilDB->quote($_GET["selImage"]),
00756 $ilDB->quote($pass . "")
00757 );
00758
00759 $result = $ilDB->query($query);
00760 include_once ("./classes/class.ilObjAssessmentFolder.php");
00761 if (ilObjAssessmentFolder::_enabledAssessmentLogging())
00762 {
00763 $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
00764 }
00765 }
00766 else
00767 {
00768 include_once ("./classes/class.ilObjAssessmentFolder.php");
00769 if (ilObjAssessmentFolder::_enabledAssessmentLogging())
00770 {
00771 $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
00772 }
00773 }
00774
00775 parent::saveWorkingData($active_id, $pass);
00776 return true;
00777 }
00778
00779 function syncWithOriginal()
00780 {
00781 if ($this->getOriginalId())
00782 {
00783 parent::syncWithOriginal();
00784 $this->syncFeedbackSingleAnswers();
00785 }
00786 }
00787
00796 function getQuestionType()
00797 {
00798 return "assImagemapQuestion";
00799 }
00800
00809 function getAdditionalTableName()
00810 {
00811 return "qpl_question_imagemap";
00812 }
00813
00822 function getAnswerTableName()
00823 {
00824 return "qpl_answer_imagemap";
00825 }
00826
00836 function saveFeedbackSingleAnswer($answer_index, $feedback)
00837 {
00838 global $ilDB;
00839
00840 $query = sprintf("DELETE FROM qpl_feedback_imagemap WHERE question_fi = %s AND answer = %s",
00841 $ilDB->quote($this->getId() . ""),
00842 $ilDB->quote($answer_index . "")
00843 );
00844 $result = $ilDB->query($query);
00845 if (strlen($feedback))
00846 {
00847 include_once("./Services/RTE/classes/class.ilRTE.php");
00848 $query = sprintf("INSERT INTO qpl_feedback_imagemap VALUES (NULL, %s, %s, %s, NULL)",
00849 $ilDB->quote($this->getId() . ""),
00850 $ilDB->quote($answer_index . ""),
00851 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($feedback, 0))
00852 );
00853 $result = $ilDB->query($query);
00854 }
00855 }
00856
00864 function syncFeedbackSingleAnswers()
00865 {
00866 global $ilDB;
00867
00868 $feedback = "";
00869
00870
00871 $deletequery = sprintf("DELETE FROM qpl_feedback_imagemap WHERE question_fi = %s",
00872 $ilDB->quote($this->original_id . "")
00873 );
00874 $result = $ilDB->query($deletequery);
00875
00876
00877 $query = sprintf("SELECT * FROM qpl_feedback_imagemap WHERE question_fi = %s",
00878 $ilDB->quote($this->getId() . "")
00879 );
00880 $result = $ilDB->query($query);
00881
00882
00883 if ($result->numRows())
00884 {
00885 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00886 {
00887 $duplicatequery = sprintf("INSERT INTO qpl_feedback_imagemap VALUES (NULL, %s, %s, %s, NULL)",
00888 $ilDB->quote($this->original_id . ""),
00889 $ilDB->quote($row["answer"] . ""),
00890 $ilDB->quote($row["feedback"] . "")
00891 );
00892 $duplicateresult = $ilDB->query($duplicatequery);
00893 }
00894 }
00895 }
00896
00906 function getFeedbackSingleAnswer($answer_index)
00907 {
00908 global $ilDB;
00909
00910 $feedback = "";
00911 $query = sprintf("SELECT * FROM qpl_feedback_imagemap WHERE question_fi = %s AND answer = %s",
00912 $ilDB->quote($this->getId() . ""),
00913 $ilDB->quote($answer_index . "")
00914 );
00915 $result = $ilDB->query($query);
00916 if ($result->numRows())
00917 {
00918 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00919 include_once("./Services/RTE/classes/class.ilRTE.php");
00920 $feedback = ilRTE::_replaceMediaObjectImageSrc($row["feedback"], 1);
00921 }
00922 return $feedback;
00923 }
00924
00933 function duplicateFeedbackAnswer($original_id)
00934 {
00935 global $ilDB;
00936
00937 $feedback = "";
00938 $query = sprintf("SELECT * FROM qpl_feedback_imagemap WHERE question_fi = %s",
00939 $ilDB->quote($original_id . "")
00940 );
00941 $result = $ilDB->query($query);
00942 if ($result->numRows())
00943 {
00944 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00945 {
00946 $duplicatequery = sprintf("INSERT INTO qpl_feedback_imagemap VALUES (NULL, %s, %s, %s, NULL)",
00947 $ilDB->quote($this->getId() . ""),
00948 $ilDB->quote($row["answer"] . ""),
00949 $ilDB->quote($row["feedback"] . "")
00950 );
00951 $duplicateresult = $ilDB->query($duplicatequery);
00952 }
00953 }
00954 }
00955
00960 function getRTETextWithMediaObjects()
00961 {
00962 $text = parent::getRTETextWithMediaObjects();
00963 foreach ($this->answers as $index => $answer)
00964 {
00965 $text .= $this->getFeedbackSingleAnswer($index);
00966 }
00967 return $text;
00968 }
00969
00970 }
00971
00972 ?>