00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 include_once "./assessment/classes/class.assQuestion.php";
00024 include_once "./assessment/classes/inc.AssessmentConstants.php";
00025
00036 class assSingleChoice extends assQuestion
00037 {
00045 var $question;
00046
00054 var $answers;
00055
00064 var $output_type;
00065
00080 function assSingleChoice(
00081 $title = "",
00082 $comment = "",
00083 $author = "",
00084 $owner = -1,
00085 $question = "",
00086 $output_type = OUTPUT_ORDER
00087 )
00088 {
00089 $this->assQuestion($title, $comment, $author, $owner);
00090 $this->question = $question;
00091 $this->output_type = $output_type;
00092 $this->answers = array();
00093 }
00094
00103 function isComplete()
00104 {
00105 if (($this->title) and ($this->author) and ($this->question) and (count($this->answers)) and ($this->getMaximumPoints() > 0))
00106 {
00107 foreach ($this->answers as $answer)
00108 {
00109 if ((strlen($answer->getAnswertext()) == 0) && (strlen($answer->getImage()) == 0)) return false;
00110 }
00111 return true;
00112 }
00113 else
00114 {
00115 return false;
00116 }
00117 }
00118
00132 function fromXML(&$item, $questionpool_id, &$tst_id, &$tst_object, &$question_counter, &$import_mapping)
00133 {
00134 global $ilUser;
00135
00136
00137 unset($_SESSION["import_mob_xhtml"]);
00138 $presentation = $item->getPresentation();
00139 $duration = $item->getDuration();
00140 $shuffle = 0;
00141 $now = getdate();
00142 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00143 $answers = array();
00144 foreach ($presentation->order as $entry)
00145 {
00146 switch ($entry["type"])
00147 {
00148 case "response":
00149 $response = $presentation->response[$entry["index"]];
00150 $rendertype = $response->getRenderType();
00151 switch (strtolower(get_class($response->getRenderType())))
00152 {
00153 case "ilqtirenderchoice":
00154 $shuffle = $rendertype->getShuffle();
00155 $answerorder = 0;
00156 $foundimage = FALSE;
00157 foreach ($rendertype->response_labels as $response_label)
00158 {
00159 $ident = $response_label->getIdent();
00160 $answertext = "";
00161 $answerimage = array();
00162 foreach ($response_label->material as $mat)
00163 {
00164 for ($m = 0; $m < $mat->getMaterialCount(); $m++)
00165 {
00166 $foundmat = $mat->getMaterial($m);
00167 if (strcmp($foundmat["type"], "mattext") == 0)
00168 {
00169 $answertext .= $foundmat["material"]->getContent();
00170 }
00171 if (strcmp($foundmat["type"], "matimage") == 0)
00172 {
00173 $foundimage = TRUE;
00174 $answerimage = array(
00175 "imagetype" => $foundmat["material"]->getImageType(),
00176 "label" => $foundmat["material"]->getLabel(),
00177 "content" => $foundmat["material"]->getContent()
00178 );
00179 }
00180 }
00181 }
00182 $answers[$ident] = array(
00183 "answertext" => $answertext,
00184 "imagefile" => $answerimage,
00185 "points" => 0,
00186 "answerorder" => $answerorder++,
00187 "points_unchecked" => 0,
00188 "action" => ""
00189 );
00190 }
00191 break;
00192 }
00193 break;
00194 }
00195 }
00196 $responses = array();
00197 foreach ($item->resprocessing as $resprocessing)
00198 {
00199 foreach ($resprocessing->respcondition as $respcondition)
00200 {
00201 $ident = "";
00202 $correctness = 1;
00203 $conditionvar = $respcondition->getConditionvar();
00204 foreach ($conditionvar->order as $order)
00205 {
00206 switch ($order["field"])
00207 {
00208 case "arr_not":
00209 $correctness = 0;
00210 break;
00211 case "varequal":
00212 $ident = $conditionvar->varequal[$order["index"]]->getContent();
00213 break;
00214 }
00215 }
00216 foreach ($respcondition->setvar as $setvar)
00217 {
00218 if (strcmp($ident, "") != 0)
00219 {
00220 if ($correctness)
00221 {
00222 $answers[$ident]["action"] = $setvar->getAction();
00223 $answers[$ident]["points"] = $setvar->getContent();
00224 }
00225 else
00226 {
00227 $answers[$ident]["action"] = $setvar->getAction();
00228 $answers[$ident]["points_unchecked"] = $setvar->getContent();
00229 }
00230 }
00231 }
00232 }
00233 }
00234 $this->setTitle($item->getTitle());
00235 $this->setComment($item->getComment());
00236 $this->setAuthor($item->getAuthor());
00237 $this->setOwner($ilUser->getId());
00238 $this->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
00239 $this->setObjId($questionpool_id);
00240 $this->setEstimatedWorkingTime($duration["h"], $duration["m"], $duration["s"]);
00241 $this->setShuffle($shuffle);
00242 foreach ($answers as $answer)
00243 {
00244 $this->addAnswer($answer["answertext"], $answer["points"], $answer["points_unchecked"], $answer["answerorder"], $answer["imagefile"]["label"]);
00245 }
00246 $this->saveToDb();
00247 foreach ($answers as $answer)
00248 {
00249 if (is_array($answer["imagefile"]) && (count($answer["imagefile"]) > 0))
00250 {
00251 $image =& base64_decode($answer["imagefile"]["content"]);
00252 $imagepath = $this->getImagePath();
00253 include_once "./classes/class.ilUtil.php";
00254 if (!file_exists($imagepath))
00255 {
00256 ilUtil::makeDirParents($imagepath);
00257 }
00258 $imagepath .= $answer["imagefile"]["label"];
00259 $fh = fopen($imagepath, "wb");
00260 if ($fh == false)
00261 {
00262
00263
00264
00265 }
00266 else
00267 {
00268 $imagefile = fwrite($fh, $image);
00269 fclose($fh);
00270 }
00271
00272 $thumbpath = $imagepath . "." . "thumb.jpg";
00273 ilUtil::convertImage($imagepath, $thumbpath, "JPEG", 100);
00274 }
00275 }
00276
00277 if (is_array($_SESSION["import_mob_xhtml"]))
00278 {
00279 include_once "./content/classes/Media/class.ilObjMediaObject.php";
00280 include_once "./Services/RTE/classes/class.ilRTE.php";
00281 foreach ($_SESSION["import_mob_xhtml"] as $mob)
00282 {
00283 if ($tst_id > 0)
00284 {
00285 include_once "./assessment/classes/class.ilObjTest.php";
00286 $importfile = ilObjTest::_getImportDirectory() . "/" . $_SESSION["tst_import_subdir"] . "/" . $mob["uri"];
00287 }
00288 else
00289 {
00290 include_once "./assessment/classes/class.ilObjQuestionPool.php";
00291 $importfile = ilObjQuestionPool::_getImportDirectory() . "/" . $_SESSION["qpl_import_subdir"] . "/" . $mob["uri"];
00292 }
00293 $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
00294 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->getId());
00295 $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getQuestion()), 1));
00296 foreach ($this->answers as $key => $value)
00297 {
00298 $answer_obj = $this->answers[$key];
00299 $answer_obj->setAnswertext(ilRTE::_replaceMediaObjectImageSrc(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $answer_obj->getAnswertext()), 1));
00300 }
00301 }
00302 $this->saveToDb();
00303 }
00304 if (count($item->suggested_solutions))
00305 {
00306 foreach ($item->suggested_solutions as $suggested_solution)
00307 {
00308 $this->setSuggestedSolution($suggested_solution["solution"]->getContent(), $suggested_solution["gap_index"], true);
00309 }
00310 $this->saveToDb();
00311 }
00312 if ($tst_id > 0)
00313 {
00314 $q_1_id = $this->getId();
00315 $question_id = $this->duplicate(true);
00316 $tst_object->questions[$question_counter++] = $question_id;
00317 $import_mapping[$item->getIdent()] = array("pool" => $q_1_id, "test" => $question_id);
00318 }
00319 else
00320 {
00321 $import_mapping[$item->getIdent()] = array("pool" => $this->getId(), "test" => 0);
00322 }
00323
00324 }
00325
00335 function to_xml($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false, $force_image_references = false)
00336 {
00337 include_once("./classes/class.ilXmlWriter.php");
00338 $a_xml_writer = new ilXmlWriter;
00339
00340 $a_xml_writer->xmlHeader();
00341 $a_xml_writer->xmlStartTag("questestinterop");
00342 $attrs = array(
00343 "ident" => "il_".IL_INST_ID."_qst_".$this->getId(),
00344 "title" => $this->getTitle()
00345 );
00346 $a_xml_writer->xmlStartTag("item", $attrs);
00347
00348 $a_xml_writer->xmlElement("qticomment", NULL, $this->getComment());
00349
00350 $workingtime = $this->getEstimatedWorkingTime();
00351 $duration = sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]);
00352 $a_xml_writer->xmlElement("duration", NULL, $duration);
00353
00354 $a_xml_writer->xmlStartTag("itemmetadata");
00355 $a_xml_writer->xmlStartTag("qtimetadata");
00356 $a_xml_writer->xmlStartTag("qtimetadatafield");
00357 $a_xml_writer->xmlElement("fieldlabel", NULL, "ILIAS_VERSION");
00358 $a_xml_writer->xmlElement("fieldentry", NULL, $this->ilias->getSetting("ilias_version"));
00359 $a_xml_writer->xmlEndTag("qtimetadatafield");
00360 $a_xml_writer->xmlStartTag("qtimetadatafield");
00361 $a_xml_writer->xmlElement("fieldlabel", NULL, "QUESTIONTYPE");
00362 $a_xml_writer->xmlElement("fieldentry", NULL, SINGLE_CHOICE_QUESTION_IDENTIFIER);
00363 $a_xml_writer->xmlEndTag("qtimetadatafield");
00364 $a_xml_writer->xmlStartTag("qtimetadatafield");
00365 $a_xml_writer->xmlElement("fieldlabel", NULL, "AUTHOR");
00366 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getAuthor());
00367 $a_xml_writer->xmlEndTag("qtimetadatafield");
00368 $a_xml_writer->xmlEndTag("qtimetadata");
00369 $a_xml_writer->xmlEndTag("itemmetadata");
00370
00371
00372 $attrs = array(
00373 "label" => $this->getTitle()
00374 );
00375 $a_xml_writer->xmlStartTag("presentation", $attrs);
00376
00377 $a_xml_writer->xmlStartTag("flow");
00378
00379 $this->addQTIMaterial($a_xml_writer, $this->getQuestion());
00380
00381 $attrs = array();
00382 $attrs = array(
00383 "ident" => "MCSR",
00384 "rcardinality" => "Single"
00385 );
00386 $a_xml_writer->xmlStartTag("response_lid", $attrs);
00387 $solution = $this->getSuggestedSolution(0);
00388 if (count($solution))
00389 {
00390 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
00391 {
00392 $a_xml_writer->xmlStartTag("material");
00393 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00394 if (strcmp($matches[1], "") != 0)
00395 {
00396 $intlink = $solution["internal_link"];
00397 }
00398 $attrs = array(
00399 "label" => "suggested_solution"
00400 );
00401 $a_xml_writer->xmlElement("mattext", $attrs, $intlink);
00402 $a_xml_writer->xmlEndTag("material");
00403 }
00404 }
00405
00406 $attrs = array();
00407 if ($this->getShuffle())
00408 {
00409 $attrs = array(
00410 "shuffle" => "Yes"
00411 );
00412 }
00413 else
00414 {
00415 $attrs = array(
00416 "shuffle" => "No"
00417 );
00418 }
00419 $a_xml_writer->xmlStartTag("render_choice", $attrs);
00420 $akeys = array_keys($this->answers);
00421 if ($this->getshuffle() && $a_shuffle)
00422 {
00423 $akeys = $this->pcArrayShuffle($akeys);
00424 }
00425
00426 foreach ($akeys as $index)
00427 {
00428 $answer = $this->answers[$index];
00429 $attrs = array(
00430 "ident" => $index
00431 );
00432 $a_xml_writer->xmlStartTag("response_label", $attrs);
00433 $this->addQTIMaterial($a_xml_writer, $answer->getAnswertext(), FALSE, FALSE);
00434
00435 if (strlen($answer->getImage()))
00436 {
00437 $imagetype = "image/jpeg";
00438 if (preg_match("/.*\.(png|gif)$/", $answer->getImage(), $matches))
00439 {
00440 $imagetype = "image/" . $matches[1];
00441 }
00442 if ($force_image_references)
00443 {
00444 $attrs = array(
00445 "imagtype" => $imagetype,
00446 "label" => $answer->getImage(),
00447 "uri" => $this->getImagePathWeb() . $answer->getImage()
00448 );
00449 $a_xml_writer->xmlElement("matimage", $attrs);
00450 }
00451 else
00452 {
00453 $imagepath = $this->getImagePath() . $answer->getImage();
00454 $fh = @fopen($imagepath, "rb");
00455 if ($fh != false)
00456 {
00457 $imagefile = fread($fh, filesize($imagepath));
00458 fclose($fh);
00459 $base64 = base64_encode($imagefile);
00460 $attrs = array(
00461 "imagtype" => $imagetype,
00462 "label" => $answer->getImage(),
00463 "embedded" => "base64"
00464 );
00465 $a_xml_writer->xmlElement("matimage", $attrs, $base64, FALSE, FALSE);
00466 }
00467 }
00468 }
00469 $a_xml_writer->xmlEndTag("material");
00470 $a_xml_writer->xmlEndTag("response_label");
00471 }
00472 $a_xml_writer->xmlEndTag("render_choice");
00473 $a_xml_writer->xmlEndTag("response_lid");
00474 $a_xml_writer->xmlEndTag("flow");
00475 $a_xml_writer->xmlEndTag("presentation");
00476
00477
00478 $a_xml_writer->xmlStartTag("resprocessing");
00479 $a_xml_writer->xmlStartTag("outcomes");
00480 $a_xml_writer->xmlStartTag("decvar");
00481 $a_xml_writer->xmlEndTag("decvar");
00482 $a_xml_writer->xmlEndTag("outcomes");
00483
00484 foreach ($this->answers as $index => $answer)
00485 {
00486 $attrs = array(
00487 "continue" => "Yes"
00488 );
00489 $a_xml_writer->xmlStartTag("respcondition", $attrs);
00490
00491 $a_xml_writer->xmlStartTag("conditionvar");
00492 $attrs = array();
00493 $attrs = array(
00494 "respident" => "MCSR"
00495 );
00496 $a_xml_writer->xmlElement("varequal", $attrs, $index);
00497 $a_xml_writer->xmlEndTag("conditionvar");
00498
00499 $attrs = array(
00500 "action" => "Add"
00501 );
00502 $a_xml_writer->xmlElement("setvar", $attrs, $answer->getPoints());
00503
00504 $linkrefid = "response_$index";
00505 $attrs = array(
00506 "feedbacktype" => "Response",
00507 "linkrefid" => $linkrefid
00508 );
00509 $a_xml_writer->xmlElement("displayfeedback", $attrs);
00510 $a_xml_writer->xmlEndTag("respcondition");
00511 }
00512 $a_xml_writer->xmlEndTag("resprocessing");
00513
00514
00515 foreach ($this->answers as $index => $answer)
00516 {
00517 $linkrefid = "response_$index";
00518 $attrs = array(
00519 "ident" => $linkrefid,
00520 "view" => "All"
00521 );
00522 $a_xml_writer->xmlStartTag("itemfeedback", $attrs);
00523
00524 $a_xml_writer->xmlStartTag("flow_mat");
00525 $a_xml_writer->xmlStartTag("material");
00526 $a_xml_writer->xmlElement("mattext");
00527 $a_xml_writer->xmlEndTag("material");
00528 $a_xml_writer->xmlEndTag("flow_mat");
00529 $a_xml_writer->xmlEndTag("itemfeedback");
00530 }
00531
00532 $a_xml_writer->xmlEndTag("item");
00533 $a_xml_writer->xmlEndTag("questestinterop");
00534
00535 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00536 if (!$a_include_header)
00537 {
00538 $pos = strpos($xml, "?>");
00539 $xml = substr($xml, $pos + 2);
00540 }
00541 return $xml;
00542 }
00543
00552 function saveToDb($original_id = "")
00553 {
00554 global $ilDB;
00555
00556 $complete = 0;
00557 if ($this->isComplete())
00558 {
00559 $complete = 1;
00560 }
00561 $estw_time = $this->getEstimatedWorkingTime();
00562 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
00563
00564 if ($original_id)
00565 {
00566 $original_id = $ilDB->quote($original_id);
00567 }
00568 else
00569 {
00570 $original_id = "NULL";
00571 }
00572
00573 include_once("./Services/RTE/classes/class.ilRTE.php");
00574 $combinedtext = $this->question;
00575
00576 if ($this->id == -1)
00577 {
00578
00579 $now = getdate();
00580 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00581 $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, points, working_time, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00582 $ilDB->quote("1"),
00583 $ilDB->quote($this->obj_id),
00584 $ilDB->quote($this->title),
00585 $ilDB->quote($this->comment),
00586 $ilDB->quote($this->author),
00587 $ilDB->quote($this->owner),
00588 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
00589 $ilDB->quote($this->getMaximumPoints() . ""),
00590 $ilDB->quote($estw_time),
00591 $ilDB->quote("$complete"),
00592 $ilDB->quote($created),
00593 $original_id
00594 );
00595 $result = $ilDB->query($query);
00596
00597 if ($result == DB_OK)
00598 {
00599 $this->id = $ilDB->getLastInsertId();
00600 $query = sprintf("INSERT INTO qpl_question_singlechoice (question_fi, shuffle) VALUES (%s, %s)",
00601 $ilDB->quote($this->id . ""),
00602 $ilDB->quote("$this->shuffle")
00603 );
00604 $ilDB->query($query);
00605
00606
00607 $this->createPageObject();
00608
00609 if ($this->getTestId() > 0)
00610 {
00611 $this->insertIntoTest($this->getTestId());
00612 }
00613 }
00614 }
00615 else
00616 {
00617
00618 $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, points = %s, working_time=%s, complete = %s WHERE question_id = %s",
00619 $ilDB->quote($this->obj_id. ""),
00620 $ilDB->quote($this->title),
00621 $ilDB->quote($this->comment),
00622 $ilDB->quote($this->author),
00623 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
00624 $ilDB->quote($this->getMaximumPoints() . ""),
00625 $ilDB->quote($estw_time),
00626 $ilDB->quote("$complete"),
00627 $ilDB->quote($this->id)
00628 );
00629 $result = $ilDB->query($query);
00630 $query = sprintf("UPDATE qpl_question_singlechoice SET shuffle = %s WHERE question_fi = %s",
00631 $ilDB->quote("$this->shuffle"),
00632 $ilDB->quote($this->id . "")
00633 );
00634 $result = $ilDB->query($query);
00635 }
00636 if ($result == DB_OK)
00637 {
00638
00639
00640 $query = sprintf("DELETE FROM qpl_answer_singlechoice WHERE question_fi = %s",
00641 $ilDB->quote($this->id)
00642 );
00643 $result = $ilDB->query($query);
00644
00645
00646 foreach ($this->answers as $key => $value)
00647 {
00648 $answer_obj = $this->answers[$key];
00649 $query = sprintf("INSERT INTO qpl_answer_singlechoice (answer_id, question_fi, answertext, points, aorder, imagefile) VALUES (NULL, %s, %s, %s, %s, %s)",
00650 $ilDB->quote($this->id),
00651 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($answer_obj->getAnswertext(), 0)),
00652 $ilDB->quote($answer_obj->getPoints() . ""),
00653 $ilDB->quote($answer_obj->getOrder() . ""),
00654 $ilDB->quote($answer_obj->getImage() . "")
00655 );
00656 $combinedtext .= $answer_obj->getAnswertext();
00657 $answer_result = $ilDB->query($query);
00658 }
00659 }
00660
00661 ilRTE::_cleanupMediaObjectUsage($combinedtext, "qpl:html",
00662 $this->getId());
00663
00664 parent::saveToDb($original_id);
00665 }
00666
00676 function loadFromDb($question_id)
00677 {
00678 global $ilDB;
00679
00680 $hasimages = 0;
00681 $query = sprintf("SELECT qpl_questions.*, qpl_question_singlechoice.* FROM qpl_questions, qpl_question_singlechoice WHERE question_id = %s AND qpl_questions.question_id = qpl_question_singlechoice.question_fi",
00682 $ilDB->quote($question_id));
00683 $result = $ilDB->query($query);
00684 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00685 {
00686 if ($result->numRows() == 1)
00687 {
00688 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00689 $this->id = $question_id;
00690 $this->title = $data->title;
00691 $this->comment = $data->comment;
00692 $this->solution_hint = $data->solution_hint;
00693 $this->original_id = $data->original_id;
00694 $this->obj_id = $data->obj_fi;
00695 $this->author = $data->author;
00696 $this->owner = $data->owner;
00697 $this->points = $data->points;
00698 include_once("./Services/RTE/classes/class.ilRTE.php");
00699 $this->question = ilRTE::_replaceMediaObjectImageSrc($data->question_text, 1);
00700 $this->setShuffle($data->shuffle);
00701 $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
00702 }
00703
00704 $query = sprintf("SELECT * FROM qpl_answer_singlechoice WHERE question_fi = %s ORDER BY aorder ASC",
00705 $ilDB->quote($question_id));
00706
00707 $result = $ilDB->query($query);
00708
00709 include_once "./assessment/classes/class.assAnswerBinaryStateImage.php";
00710 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00711 {
00712 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00713 {
00714 $imagefilename = $this->getImagePath() . $data->imagefile;
00715 if (!@file_exists($imagefilename))
00716 {
00717 $data->imagefile = "";
00718 }
00719 include_once("./Services/RTE/classes/class.ilRTE.php");
00720 $data->answertext = ilRTE::_replaceMediaObjectImageSrc($data->answertext, 1);
00721 if (strlen($data->imagefile)) $hasimages = 1;
00722 array_push($this->answers, new ASS_AnswerBinaryStateImage($data->answertext, $data->points, $data->aorder, 1, $data->imagefile));
00723 }
00724 }
00725 }
00726 $this->setGraphicalAnswerSetting($hasimages);
00727 parent::loadFromDb($question_id);
00728 }
00729
00737 function duplicate($for_test = true, $title = "", $author = "", $owner = "")
00738 {
00739 if ($this->id <= 0)
00740 {
00741
00742 return;
00743 }
00744
00745 $this_id = $this->getId();
00746 $clone = $this;
00747 include_once ("./assessment/classes/class.assQuestion.php");
00748 $original_id = assQuestion::_getOriginalId($this->id);
00749 $clone->id = -1;
00750 if ($title)
00751 {
00752 $clone->setTitle($title);
00753 }
00754
00755 if ($author)
00756 {
00757 $clone->setAuthor($author);
00758 }
00759 if ($owner)
00760 {
00761 $clone->setOwner($owner);
00762 }
00763
00764 if ($for_test)
00765 {
00766 $clone->saveToDb($original_id);
00767 }
00768 else
00769 {
00770 $clone->saveToDb();
00771 }
00772
00773
00774 $clone->copyPageOfQuestion($this_id);
00775
00776 $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
00777
00778 $clone->duplicateImages($this_id);
00779
00780 return $clone->id;
00781 }
00782
00790 function copyObject($target_questionpool, $title = "")
00791 {
00792 if ($this->id <= 0)
00793 {
00794
00795 return;
00796 }
00797
00798 $clone = $this;
00799 include_once ("./assessment/classes/class.assQuestion.php");
00800 $original_id = assQuestion::_getOriginalId($this->id);
00801 $clone->id = -1;
00802 $source_questionpool = $this->getObjId();
00803 $clone->setObjId($target_questionpool);
00804 if ($title)
00805 {
00806 $clone->setTitle($title);
00807 }
00808 $clone->saveToDb();
00809
00810
00811 $clone->copyPageOfQuestion($original_id);
00812
00813 $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
00814
00815 $clone->copyImages($original_id, $source_questionpool);
00816
00817 return $clone->id;
00818 }
00819
00829 function getQuestion()
00830 {
00831 return $this->question;
00832 }
00833
00843 function setQuestion($question = "")
00844 {
00845 $this->question = $question;
00846 }
00847
00857 function getOutputType()
00858 {
00859 return $this->output_type;
00860 }
00861
00871 function setOutputType($output_type = OUTPUT_ORDER)
00872 {
00873 $this->output_type = $output_type;
00874 }
00875
00891 function addAnswer(
00892 $answertext = "",
00893 $points = 0.0,
00894 $points_unchecked = 0.0,
00895 $order = 0,
00896 $answerimage = ""
00897 )
00898 {
00899 $found = -1;
00900 foreach ($this->answers as $key => $value)
00901 {
00902 if ($value->getOrder() == $order)
00903 {
00904 $found = $order;
00905 }
00906 }
00907 include_once "./assessment/classes/class.assAnswerBinaryStateImage.php";
00908 if ($found >= 0)
00909 {
00910
00911 $answer = new ASS_AnswerBinaryStateImage($answertext, $points, $found, 1, $answerimage);
00912 array_push($this->answers, $answer);
00913 for ($i = $found + 1; $i < count($this->answers); $i++)
00914 {
00915 $this->answers[$i] = $this->answers[$i-1];
00916 }
00917 $this->answers[$found] = $answer;
00918 }
00919 else
00920 {
00921
00922 $answer = new ASS_AnswerBinaryStateImage($answertext, $points, count($this->answers), 1, $answerimage);
00923 array_push($this->answers, $answer);
00924 }
00925 }
00926
00936 function getAnswerCount()
00937 {
00938 return count($this->answers);
00939 }
00940
00952 function getAnswer($index = 0)
00953 {
00954 if ($index < 0) return NULL;
00955 if (count($this->answers) < 1) return NULL;
00956 if ($index >= count($this->answers)) return NULL;
00957
00958 return $this->answers[$index];
00959 }
00960
00971 function deleteAnswer($index = 0)
00972 {
00973 if ($index < 0) return;
00974 if (count($this->answers) < 1) return;
00975 if ($index >= count($this->answers)) return;
00976 $answer = $this->answers[$index];
00977 if (strlen($answer->getImage())) $this->deleteImage($answer->getImage());
00978 unset($this->answers[$index]);
00979 $this->answers = array_values($this->answers);
00980 for ($i = 0; $i < count($this->answers); $i++)
00981 {
00982 if ($this->answers[$i]->getOrder() > $index)
00983 {
00984 $this->answers[$i]->setOrder($i);
00985 }
00986 }
00987 }
00988
00997 function flushAnswers()
00998 {
00999 $this->answers = array();
01000 }
01001
01010 function getMaximumPoints()
01011 {
01012 $points = 0;
01013 foreach ($this->answers as $key => $value)
01014 {
01015 if ($value->getPoints() > $points)
01016 {
01017 $points = $value->getPoints();
01018 }
01019 }
01020 return $points;
01021 }
01022
01034 function calculateReachedPoints($active_id, $pass = NULL)
01035 {
01036 global $ilDB;
01037
01038 $found_values = array();
01039 if (is_null($pass))
01040 {
01041 $pass = $this->getSolutionMaxPass($active_id);
01042 }
01043 $query = sprintf("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
01044 $ilDB->quote($active_id . ""),
01045 $ilDB->quote($this->getId() . ""),
01046 $ilDB->quote($pass . "")
01047 );
01048 $result = $ilDB->query($query);
01049 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01050 {
01051 if (strcmp($data->value1, "") != 0)
01052 {
01053 array_push($found_values, $data->value1);
01054 }
01055 }
01056 $points = 0;
01057 foreach ($this->answers as $key => $answer)
01058 {
01059 if (count($found_values) > 0)
01060 {
01061 if (in_array($key, $found_values))
01062 {
01063 $points += $answer->getPoints();
01064 }
01065 }
01066 }
01067
01068 $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
01069 return $points;
01070 }
01071
01082 function saveWorkingData($active_id, $pass = NULL)
01083 {
01084 global $ilDB;
01085 global $ilUser;
01086
01087 include_once "./assessment/classes/class.ilObjTest.php";
01088 $activepass = ilObjTest::_getPass($active_id);
01089 $entered_values = 0;
01090
01091 $query = sprintf("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
01092 $ilDB->quote($active_id . ""),
01093 $ilDB->quote($this->getId() . ""),
01094 $ilDB->quote($activepass . "")
01095 );
01096 $result = $ilDB->query($query);
01097 $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
01098 $update = $row->solution_id;
01099 if ($update)
01100 {
01101 if (strlen($_POST["multiple_choice_result"]))
01102 {
01103 $query = sprintf("UPDATE tst_solutions SET value1 = %s WHERE solution_id = %s",
01104 $ilDB->quote($_POST["multiple_choice_result"]),
01105 $ilDB->quote($update)
01106 );
01107 $result = $ilDB->query($query);
01108 $entered_values++;
01109 }
01110 else
01111 {
01112 $query = sprintf("DELETE FROM tst_solutions WHERE solution_id = %s",
01113 $ilDB->quote($update)
01114 );
01115 $result = $ilDB->query($query);
01116 }
01117 }
01118 else
01119 {
01120 if (strlen($_POST["multiple_choice_result"]))
01121 {
01122 $query = sprintf("INSERT INTO tst_solutions (solution_id, active_fi, question_fi, value1, value2, pass, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL, %s, NULL)",
01123 $ilDB->quote($active_id),
01124 $ilDB->quote($this->getId()),
01125 $ilDB->quote($_POST["multiple_choice_result"]),
01126 $ilDB->quote($activepass . "")
01127 );
01128 $result = $ilDB->query($query);
01129 $entered_values++;
01130 }
01131 }
01132 if ($entered_values)
01133 {
01134 include_once ("./classes/class.ilObjAssessmentFolder.php");
01135 if (ilObjAssessmentFolder::_enabledAssessmentLogging())
01136 {
01137 $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
01138 }
01139 }
01140 else
01141 {
01142 include_once ("./classes/class.ilObjAssessmentFolder.php");
01143 if (ilObjAssessmentFolder::_enabledAssessmentLogging())
01144 {
01145 $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
01146 }
01147 }
01148 parent::saveWorkingData($active_id, $pass);
01149 return true;
01150 }
01151
01152 function syncWithOriginal()
01153 {
01154 global $ilDB;
01155
01156 if ($this->original_id)
01157 {
01158 $complete = 0;
01159 if ($this->isComplete())
01160 {
01161 $complete = 1;
01162 }
01163 $estw_time = $this->getEstimatedWorkingTime();
01164 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
01165
01166 $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, points = %s, working_time=%s, complete = %s WHERE question_id = %s",
01167 $ilDB->quote($this->obj_id. ""),
01168 $ilDB->quote($this->title. ""),
01169 $ilDB->quote($this->comment. ""),
01170 $ilDB->quote($this->author. ""),
01171 $ilDB->quote($this->question. ""),
01172 $ilDB->quote($this->getMaximumPoints() . ""),
01173 $ilDB->quote($estw_time. ""),
01174 $ilDB->quote($complete. ""),
01175 $ilDB->quote($this->original_id. "")
01176 );
01177 $result = $ilDB->query($query);
01178 $query = sprintf("UPDATE qpl_question_singlechoice SET shuffle = %s WHERE question_fi = %s",
01179 $ilDB->quote($this->shuffle. ""),
01180 $ilDB->quote($this->original_id . "")
01181 );
01182 $result = $ilDB->query($query);
01183
01184 if ($result == DB_OK)
01185 {
01186
01187
01188 $query = sprintf("DELETE FROM qpl_answer_singlechoice WHERE question_fi = %s",
01189 $ilDB->quote($this->original_id)
01190 );
01191 $result = $ilDB->query($query);
01192 $points_unchecked = 0;
01193 foreach ($this->answers as $key => $value)
01194 {
01195 $answer_obj = $this->answers[$key];
01196 $query = sprintf("INSERT INTO qpl_answer_singlechoice (answer_id, question_fi, answertext, points, aorder) VALUES (NULL, %s, %s, %s, %s)",
01197 $ilDB->quote($this->original_id. ""),
01198 $ilDB->quote($answer_obj->getAnswertext(). ""),
01199 $ilDB->quote($answer_obj->getPoints() . ""),
01200 $ilDB->quote($answer_obj->getOrder() . "")
01201 );
01202 $answer_result = $ilDB->query($query);
01203 }
01204 }
01205 parent::syncWithOriginal();
01206 }
01207 }
01208
01217 function getQuestionType()
01218 {
01219 $question_type = 1;
01220 return $question_type;
01221 }
01222
01231 function getAdditionalTableName()
01232 {
01233 return "qpl_question_singlechoice";
01234 }
01235
01244 function getAnswerTableName()
01245 {
01246 return "qpl_answer_singlechoice";
01247 }
01248
01249 function getGraphicalAnswerSetting()
01250 {
01251 global $ilUser;
01252
01253 $graphicalAnswerSetting = $ilUser->getPref("graphicalAnswerSetting");
01254 if ($graphicalAnswerSetting != 1)
01255 {
01256 $graphicalAnswerSetting = 0;
01257 }
01258 return $graphicalAnswerSetting;
01259 }
01260
01261 function setGraphicalAnswerSetting($a_setting = 0)
01262 {
01263 global $ilUser;
01264 $ilUser->writePref("graphicalAnswerSetting", $a_setting);
01265 }
01266
01277 function setImageFile($image_filename, $image_tempfilename = "")
01278 {
01279 $result = 0;
01280 if (!empty($image_tempfilename))
01281 {
01282 $image_filename = str_replace(" ", "_", $image_filename);
01283 $imagepath = $this->getImagePath();
01284 if (!file_exists($imagepath))
01285 {
01286 ilUtil::makeDirParents($imagepath);
01287 }
01288
01289 if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath.$image_filename))
01290 {
01291 $result = 2;
01292 }
01293 else
01294 {
01295 include_once "./content/classes/Media/class.ilObjMediaObject.php";
01296 $mimetype = ilObjMediaObject::getMimeType($imagepath . $image_filename);
01297 if (!preg_match("/^image/", $mimetype))
01298 {
01299 unlink($imagepath . $image_filename);
01300 $result = 1;
01301 }
01302 else
01303 {
01304
01305 $thumbpath = $imagepath . $image_filename . "." . "thumb.jpg";
01306 ilUtil::convertImage($imagepath.$image_filename, $thumbpath, "JPEG", 100);
01307 }
01308 }
01309 }
01310 return $result;
01311 }
01312
01321 function deleteImage($image_filename)
01322 {
01323 $imagepath = $this->getImagePath();
01324 unlink($imagepath . $image_filename);
01325 $thumbpath = $imagepath . $image_filename . "." . "thumb.jpg";
01326 unlink($thumbpath);
01327 }
01328
01329 function duplicateImages($question_id)
01330 {
01331 global $ilLog;
01332 $imagepath = $this->getImagePath();
01333 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
01334 if (!file_exists($imagepath))
01335 {
01336 ilUtil::makeDirParents($imagepath);
01337 }
01338 foreach ($this->answers as $answer)
01339 {
01340 $filename = $answer->getImage();
01341 if (strlen($filename))
01342 {
01343 if (!copy($imagepath_original . $filename, $imagepath . $filename))
01344 {
01345 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
01346 $ilLog->write("object: " . print_r($this), $ilLog->ERROR);
01347 }
01348 if (!copy($imagepath_original . $filename . ".thumb.jpg", $imagepath . $filename . ".thumb.jpg"))
01349 {
01350 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
01351 $ilLog->write("object: " . print_r($this), $ilLog->ERROR);
01352 }
01353 }
01354 }
01355 }
01356
01357 function copyImages($question_id, $source_questionpool)
01358 {
01359 global $ilLog;
01360 $imagepath = $this->getImagePath();
01361 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
01362 $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
01363 if (!file_exists($imagepath))
01364 {
01365 ilUtil::makeDirParents($imagepath);
01366 }
01367 foreach ($this->answers as $answer)
01368 {
01369 $filename = $answer->getImage();
01370 if (strlen($filename))
01371 {
01372 if (!copy($imagepath_original . $filename, $imagepath . $filename))
01373 {
01374 $ilLog->write("image could not be duplicated!!!!", $ilLog->ERROR);
01375 $ilLog->write("object: " . print_r($this), $ilLog->ERROR);
01376 }
01377 if (!copy($imagepath_original . $filename . ".thumb.jpg", $imagepath . $filename . ".thumb.jpg"))
01378 {
01379 $ilLog->write("image thumbnail could not be duplicated!!!!", $ilLog->ERROR);
01380 $ilLog->write("object: " . print_r($this), $ilLog->ERROR);
01381 }
01382 }
01383 }
01384 }
01385
01386 }
01387
01388 ?>