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 require_once "./assessment/classes/class.assQuestion.php";
00025 require_once "./assessment/classes/class.assAnswerOrdering.php";
00026
00027 define ("OQ_PICTURES", 0);
00028 define ("OQ_TERMS", 1);
00029
00030 define ("ORDERING_QUESTION_IDENTIFIER", "ORDERING QUESTION");
00031
00042 class ASS_OrderingQuestion extends ASS_Question
00043 {
00051 var $question;
00052
00060 var $answers;
00061
00070 var $ordering_type;
00071
00081 var $points;
00082
00096 function ASS_OrderingQuestion (
00097 $title = "",
00098 $comment = "",
00099 $author = "",
00100 $owner = -1,
00101 $question = "",
00102 $points = 0.0,
00103 $ordering_type = OQ_TERMS
00104 )
00105 {
00106 $this->ASS_Question($title, $comment, $author, $owner);
00107 $this->answers = array();
00108 $this->question = $question;
00109 $this->points = $points;
00110 $this->ordering_type = $ordering_type;
00111 }
00112
00121 function isComplete()
00122 {
00123 if (($this->title) and ($this->author) and ($this->question) and (count($this->answers)))
00124 {
00125 return true;
00126 }
00127 else
00128 {
00129 return false;
00130 }
00131 }
00132
00142 function from_xml($xml_text)
00143 {
00144 $result = false;
00145 if (!empty($this->domxml))
00146 {
00147 $this->domxml->free();
00148 }
00149 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00150 $this->domxml = domxml_open_mem($xml_text);
00151 if (!empty($this->domxml))
00152 {
00153 $root = $this->domxml->document_element();
00154 $item = $root->first_child();
00155 $this->setTitle($item->get_attribute("title"));
00156 $this->gaps = array();
00157 $itemnodes = $item->child_nodes();
00158 $materials = array();
00159 $images = array();
00160 $shuffle = "";
00161 foreach ($itemnodes as $index => $node)
00162 {
00163 switch ($node->node_name())
00164 {
00165 case "qticomment":
00166 $comment = $node->get_content();
00167 if (strpos($comment, "ILIAS Version=") !== false)
00168 {
00169 }
00170 elseif (strpos($comment, "Questiontype=") !== false)
00171 {
00172 }
00173 elseif (strpos($comment, "Author=") !== false)
00174 {
00175 $comment = str_replace("Author=", "", $comment);
00176 $this->setAuthor($comment);
00177 }
00178 else
00179 {
00180 $this->setComment($comment);
00181 }
00182 break;
00183 case "duration":
00184 $iso8601period = $node->get_content();
00185 if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
00186 {
00187 $this->setEstimatedWorkingTime($matches[4], $matches[5], $matches[6]);
00188 }
00189 break;
00190 case "presentation":
00191 $flow = $node->first_child();
00192 $flownodes = $flow->child_nodes();
00193 foreach ($flownodes as $idx => $flownode)
00194 {
00195 if (strcmp($flownode->node_name(), "material") == 0)
00196 {
00197 $mattext = $flownode->first_child();
00198 $this->set_question($mattext->get_content());
00199 }
00200 elseif (strcmp($flownode->node_name(), "response_lid") == 0)
00201 {
00202 $ident = $flownode->get_attribute("ident");
00203 if (strcmp($ident, "OQT") == 0)
00204 {
00205 $this->set_ordering_type(OQ_TERMS);
00206 }
00207 elseif (strcmp($ident, "OQP") == 0)
00208 {
00209 $this->set_ordering_type(OQ_PICTURES);
00210 }
00211 $subnodes = $flownode->child_nodes();
00212 foreach ($subnodes as $node_type)
00213 {
00214 switch ($node_type->node_name())
00215 {
00216 case "material":
00217 $matlabel = $node_type->get_attribute("label");
00218 if (strcmp($matlabel, "suggested_solution") == 0)
00219 {
00220 $mattype = $node_type->first_child();
00221 if (strcmp($mattype->node_name(), "mattext") == 0)
00222 {
00223 $suggested_solution = $mattype->get_content();
00224 if ($suggested_solution)
00225 {
00226 if ($this->getId() < 1)
00227 {
00228 $this->saveToDb();
00229 }
00230 $this->setSuggestedSolution($suggested_solution, 0, true);
00231 }
00232 }
00233 }
00234 break;
00235 case "render_choice":
00236 $render_choice = $node_type;
00237 $shuffle = $render_choice->get_attribute("shuffle");
00238 $labels = $render_choice->child_nodes();
00239 foreach ($labels as $lidx => $response_label)
00240 {
00241 $material = $response_label->first_child();
00242 if ($this->get_ordering_type() == OQ_PICTURES)
00243 {
00244 $matimage = $material->first_child();
00245 $filename = $matimage->get_attribute("label");
00246 $image = base64_decode($matimage->get_content());
00247 $images["$filename"] = $image;
00248 $materials[$response_label->get_attribute("ident")] = $filename;
00249 }
00250 else
00251 {
00252 $mattext = $material->first_child();
00253 $materials[$response_label->get_attribute("ident")] = $mattext->get_content();
00254 }
00255 }
00256 }
00257 }
00258 }
00259 }
00260 break;
00261 case "resprocessing":
00262 $resproc_nodes = $node->child_nodes();
00263 foreach ($resproc_nodes as $index => $respcondition)
00264 {
00265 if (strcmp($respcondition->node_name(), "respcondition") == 0)
00266 {
00267 $respcondition_array =& ilQTIUtils::_getRespcondition($respcondition);
00268 $this->add_answer($materials[$respcondition_array["conditionvar"]["value"]], $respcondition_array["setvar"]["points"], count($this->answers), $respcondition_array["conditionvar"]["index"]);
00269 }
00270 }
00271 break;
00272 }
00273 }
00274 if (count($images))
00275 {
00276 $this->saveToDb();
00277 foreach ($images as $filename => $image)
00278 {
00279 if ($filename)
00280 {
00281 $imagepath = $this->getImagePath();
00282 if (!file_exists($imagepath))
00283 {
00284 ilUtil::makeDirParents($imagepath);
00285 }
00286 $imagepath .= $filename;
00287 $fh = fopen($imagepath, "wb");
00288 if ($fh == false)
00289 {
00290 global $ilErr;
00291 $ilErr->raiseError($this->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->WARNING);
00292 return;
00293 }
00294 $imagefile = fwrite($fh, $image);
00295 fclose($fh);
00296
00297 $thumbpath = $imagepath . "." . "thumb.jpg";
00298 ilUtil::convertImage($imagepath, $thumbpath, "JPEG", 100);
00299 }
00300 }
00301 }
00302 $result = true;
00303 }
00304 return $result;
00305 }
00306
00316 function to_xml($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false)
00317 {
00318 if (!empty($this->domxml))
00319 {
00320 $this->domxml->free();
00321 }
00322 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<questestinterop></questestinterop>\n";
00323 $this->domxml = domxml_open_mem($xml_header);
00324 $root = $this->domxml->document_element();
00325
00326 $qtiIdent = $this->domxml->create_element("item");
00327 $qtiIdent->set_attribute("ident", "il_".IL_INST_ID."_qst_".$this->getId());
00328 $qtiIdent->set_attribute("title", $this->getTitle());
00329 $root->append_child($qtiIdent);
00330
00331 $qtiComment = $this->domxml->create_element("qticomment");
00332 $qtiCommentText = $this->domxml->create_text_node($this->getComment());
00333 $qtiComment->append_child($qtiCommentText);
00334 $qtiIdent->append_child($qtiComment);
00335 $qtiComment = $this->domxml->create_element("qticomment");
00336 $qtiCommentText = $this->domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00337 $qtiComment->append_child($qtiCommentText);
00338 $qtiIdent->append_child($qtiComment);
00339 $qtiComment = $this->domxml->create_element("qticomment");
00340 $qtiCommentText = $this->domxml->create_text_node("Questiontype=".ORDERING_QUESTION_IDENTIFIER);
00341 $qtiComment->append_child($qtiCommentText);
00342 $qtiIdent->append_child($qtiComment);
00343 $qtiComment = $this->domxml->create_element("qticomment");
00344 $qtiCommentText = $this->domxml->create_text_node("Author=".$this->getAuthor());
00345 $qtiComment->append_child($qtiCommentText);
00346 $qtiIdent->append_child($qtiComment);
00347
00348 $qtiDuration = $this->domxml->create_element("duration");
00349 $workingtime = $this->getEstimatedWorkingTime();
00350 $qtiDurationText = $this->domxml->create_text_node(sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]));
00351 $qtiDuration->append_child($qtiDurationText);
00352 $qtiIdent->append_child($qtiDuration);
00353
00354 $qtiPresentation = $this->domxml->create_element("presentation");
00355 $qtiPresentation->set_attribute("label", $this->getTitle());
00356
00357 $qtiFlow = $this->domxml->create_element("flow");
00358
00359 $qtiMaterial = $this->domxml->create_element("material");
00360 $qtiMatText = $this->domxml->create_element("mattext");
00361 $qtiMatTextText = $this->domxml->create_text_node($this->get_question());
00362 $qtiMatText->append_child($qtiMatTextText);
00363 $qtiMaterial->append_child($qtiMatText);
00364 $qtiFlow->append_child($qtiMaterial);
00365
00366 $qtiResponseLid = $this->domxml->create_element("response_lid");
00367 if ($this->get_ordering_type() == OQ_PICTURES)
00368 {
00369 $qtiResponseLid->set_attribute("ident", "OQP");
00370 $qtiResponseLid->set_attribute("rcardinality", "Ordered");
00371 if ($this->getOutputType() == OUTPUT_JAVASCRIPT)
00372 {
00373 $qtiResponseLid->set_attribute("output", "javascript");
00374 }
00375 }
00376 else
00377 {
00378 $qtiResponseLid->set_attribute("ident", "OQT");
00379 $qtiResponseLid->set_attribute("rcardinality", "Ordered");
00380 if ($this->getOutputType() == OUTPUT_JAVASCRIPT)
00381 {
00382 $qtiResponseLid->set_attribute("output", "javascript");
00383 }
00384 }
00385 $solution = $this->getSuggestedSolution(0);
00386 if (count($solution))
00387 {
00388 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
00389 {
00390 $qtiMaterial = $this->domxml->create_element("material");
00391 $qtiMaterial->set_attribute("label", "suggested_solution");
00392 $qtiMatText = $this->domxml->create_element("mattext");
00393 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00394 if (strcmp($matches[1], "") != 0)
00395 {
00396 $intlink = $solution["internal_link"];
00397 }
00398 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00399 $qtiMatText->append_child($qtiMatTextText);
00400 $qtiMaterial->append_child($qtiMatText);
00401 $qtiResponseLid->append_child($qtiMaterial);
00402 }
00403 }
00404 $qtiRenderChoice = $this->domxml->create_element("render_choice");
00405
00406 if ($this->getShuffle())
00407 {
00408 $qtiRenderChoice->set_attribute("shuffle", "yes");
00409 }
00410 else
00411 {
00412 $qtiRenderChoice->set_attribute("shuffle", "no");
00413 }
00414
00415
00416 $akeys = array_keys($this->answers);
00417 if ($this->getshuffle() && $a_shuffle)
00418 {
00419 $akeys = $this->pcArrayShuffle($akeys);
00420 }
00421
00422
00423 foreach ($akeys as $index)
00424 {
00425 $answer = $this->answers[$index];
00426
00427 $qtiResponseLabel = $this->domxml->create_element("response_label");
00428 $qtiResponseLabel->set_attribute("ident", $index);
00429 $qtiMaterial = $this->domxml->create_element("material");
00430 if ($this->get_ordering_type() == OQ_PICTURES)
00431 {
00432 $qtiMatImage = $this->domxml->create_element("matimage");
00433 $qtiMatImage->set_attribute("imagtype", "image/jpeg");
00434 $qtiMatImage->set_attribute("label", $answer->get_answertext());
00435 $qtiMatImage->set_attribute("embedded", "base64");
00436 $imagepath = $this->getImagePath() . $answer->get_answertext();
00437 $fh = fopen($imagepath, "rb");
00438 if ($fh == false)
00439 {
00440
00441
00442
00443 }
00444 else
00445 {
00446 $imagefile = fread($fh, filesize($imagepath));
00447 fclose($fh);
00448 $base64 = base64_encode($imagefile);
00449 $qtiBase64Data = $this->domxml->create_text_node($base64);
00450 $qtiMatImage->append_child($qtiBase64Data);
00451 $qtiMaterial->append_child($qtiMatImage);
00452 }
00453 }
00454 else
00455 {
00456 $qtiMatText = $this->domxml->create_element("mattext");
00457 $qtiMatTextText = $this->domxml->create_text_node($answer->get_answertext());
00458 $qtiMatText->append_child($qtiMatTextText);
00459 $qtiMaterial->append_child($qtiMatText);
00460 }
00461 $qtiResponseLabel->append_child($qtiMaterial);
00462 $qtiRenderChoice->append_child($qtiResponseLabel);
00463 }
00464 $qtiResponseLid->append_child($qtiRenderChoice);
00465 $qtiFlow->append_child($qtiResponseLid);
00466 $qtiPresentation->append_child($qtiFlow);
00467 $qtiIdent->append_child($qtiPresentation);
00468
00469
00470 $qtiResprocessing = $this->domxml->create_element("resprocessing");
00471 $qtiOutcomes = $this->domxml->create_element("outcomes");
00472 $qtiDecvar = $this->domxml->create_element("decvar");
00473 $qtiOutcomes->append_child($qtiDecvar);
00474 $qtiResprocessing->append_child($qtiOutcomes);
00475
00476 foreach ($this->answers as $index => $answer)
00477 {
00478 $qtiRespcondition = $this->domxml->create_element("respcondition");
00479 $qtiRespcondition->set_attribute("continue", "Yes");
00480
00481 $qtiConditionvar = $this->domxml->create_element("conditionvar");
00482 $qtiVarequal = $this->domxml->create_element("varequal");
00483 if ($this->get_ordering_type() == OQ_PICTURES)
00484 {
00485 $qtiVarequal->set_attribute("respident", "OQP");
00486 }
00487 else
00488 {
00489 $qtiVarequal->set_attribute("respident", "OQT");
00490 }
00491 $qtiVarequal->set_attribute("index", $answer->get_solution_order());
00492 $qtiVarequalText = $this->domxml->create_text_node($index);
00493 $qtiVarequal->append_child($qtiVarequalText);
00494 $qtiConditionvar->append_child($qtiVarequal);
00495
00496 $qtiSetvar = $this->domxml->create_element("setvar");
00497 $qtiSetvar->set_attribute("action", "Add");
00498 $qtiSetvarText = $this->domxml->create_text_node($answer->get_points());
00499 $qtiSetvar->append_child($qtiSetvarText);
00500
00501 $qtiDisplayfeedback = $this->domxml->create_element("displayfeedback");
00502 $qtiDisplayfeedback->set_attribute("feedbacktype", "Response");
00503 $qtiDisplayfeedback->set_attribute("linkrefid", "link_$index");
00504 $qtiRespcondition->append_child($qtiConditionvar);
00505 $qtiRespcondition->append_child($qtiSetvar);
00506 $qtiRespcondition->append_child($qtiDisplayfeedback);
00507 $qtiResprocessing->append_child($qtiRespcondition);
00508 }
00509 $qtiIdent->append_child($qtiResprocessing);
00510
00511
00512 foreach ($this->answers as $index => $answer)
00513 {
00514 $qtiItemfeedback = $this->domxml->create_element("itemfeedback");
00515 $qtiItemfeedback->set_attribute("ident", "link_$index");
00516 $qtiItemfeedback->set_attribute("view", "All");
00517
00518 $qtiFlowmat = $this->domxml->create_element("flow_mat");
00519 $qtiMaterial = $this->domxml->create_element("material");
00520 $qtiMattext = $this->domxml->create_element("mattext");
00521
00522 $qtiMattextText = $this->domxml->create_text_node("");
00523 $qtiMattext->append_child($qtiMattextText);
00524 $qtiMaterial->append_child($qtiMattext);
00525 $qtiFlowmat->append_child($qtiMaterial);
00526 $qtiItemfeedback->append_child($qtiFlowmat);
00527 $qtiIdent->append_child($qtiItemfeedback);
00528 }
00529
00530 $xml = $this->domxml->dump_mem(true);
00531 if (!$a_include_header)
00532 {
00533 $pos = strpos($xml, "?>");
00534 $xml = substr($xml, $pos + 2);
00535 }
00536
00537 return $xml;
00538
00539 }
00540
00541
00550 function saveToDb($original_id = "")
00551 {
00552 global $ilias;
00553
00554 $db =& $ilias->db;
00555 $complete = 0;
00556 if ($this->isComplete())
00557 {
00558 $complete = 1;
00559 }
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 = $db->quote($original_id);
00567 }
00568 else
00569 {
00570 $original_id = "NULL";
00571 }
00572
00573 if ($this->id == -1)
00574 {
00575
00576 $now = getdate();
00577 $question_type = $this->getQuestionType();
00578 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00579 $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, working_time, ordering_type, points, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00580 $db->quote($question_type . ""),
00581 $db->quote($this->obj_id . ""),
00582 $db->quote($this->title . ""),
00583 $db->quote($this->comment . ""),
00584 $db->quote($this->author . ""),
00585 $db->quote($this->owner . ""),
00586 $db->quote($this->question . ""),
00587 $db->quote($estw_time . ""),
00588 $db->quote($this->ordering_type . ""),
00589 $db->quote($this->points . ""),
00590 $db->quote($complete . ""),
00591 $db->quote($created . ""),
00592 $original_id
00593 );
00594 $result = $db->query($query);
00595 if ($result == DB_OK)
00596 {
00597 $this->id = $this->ilias->db->getLastInsertId();
00598
00599
00600 $this->createPageObject();
00601
00602
00603 if ($this->getTestId() > 0)
00604 {
00605 $this->insertIntoTest($this->getTestId());
00606 }
00607 }
00608 }
00609 else
00610 {
00611
00612 $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, working_time = %s, ordering_type = %s, points = %s, complete = %s WHERE question_id = %s",
00613 $db->quote($this->obj_id. ""),
00614 $db->quote($this->title . ""),
00615 $db->quote($this->comment . ""),
00616 $db->quote($this->author . ""),
00617 $db->quote($this->question . ""),
00618 $db->quote($estw_time . ""),
00619 $db->quote($this->ordering_type . ""),
00620 $db->quote($this->points . ""),
00621 $db->quote($complete . ""),
00622 $db->quote($this->id . "")
00623 );
00624 $result = $db->query($query);
00625 }
00626 if ($result == DB_OK)
00627 {
00628
00629
00630 $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
00631 $db->quote($this->id)
00632 );
00633 $result = $db->query($query);
00634
00635
00636 foreach ($this->answers as $key => $value)
00637 {
00638 $answer_obj = $this->answers[$key];
00639 $query = sprintf("INSERT INTO qpl_answers (answer_id, question_fi, answertext, points, aorder, solution_order, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
00640 $db->quote($this->id),
00641 $db->quote($answer_obj->get_answertext() . ""),
00642 $db->quote($answer_obj->get_points() . ""),
00643 $db->quote($answer_obj->get_order() . ""),
00644 $db->quote($answer_obj->get_solution_order() . "")
00645 );
00646 $answer_result = $db->query($query);
00647 }
00648 }
00649 parent::saveToDb($original_id);
00650 }
00651
00661 function loadFromDb($question_id)
00662 {
00663 global $ilias;
00664 $db =& $ilias->db;
00665
00666 $query = sprintf("SELECT * FROM qpl_questions WHERE question_id = %s",
00667 $db->quote($question_id)
00668 );
00669 $result = $db->query($query);
00670 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00671 {
00672 if ($result->numRows() == 1)
00673 {
00674 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00675 $this->id = $question_id;
00676 $this->title = $data->title;
00677 $this->obj_id = $data->obj_fi;
00678 $this->comment = $data->comment;
00679 $this->original_id = $data->original_id;
00680 $this->author = $data->author;
00681 $this->owner = $data->owner;
00682 $this->question = $data->question_text;
00683 $this->solution_hint = $data->solution_hint;
00684 $this->ordering_type = $data->ordering_type;
00685 $this->points = $data->points;
00686 $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
00687 }
00688
00689 $query = sprintf("SELECT * FROM qpl_answers WHERE question_fi = %s ORDER BY aorder ASC",
00690 $db->quote($question_id)
00691 );
00692 $result = $db->query($query);
00693 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00694 {
00695 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00696 {
00697 array_push($this->answers, new ASS_AnswerOrdering($data->answertext, $data->points, $data->aorder, $data->solution_order));
00698 }
00699 }
00700 }
00701 parent::loadFromDb($question_id);
00702 }
00703
00711 function duplicate($for_test = true, $title = "", $author = "", $owner = "")
00712 {
00713 if ($this->id <= 0)
00714 {
00715
00716 return;
00717 }
00718
00719 $clone = $this;
00720 $original_id = $this->id;
00721 if ($original_id <= 0)
00722 {
00723 $original_id = "";
00724 }
00725 $clone->id = -1;
00726 if ($title)
00727 {
00728 $clone->setTitle($title);
00729 }
00730 if ($author)
00731 {
00732 $clone->setAuthor($author);
00733 }
00734 if ($owner)
00735 {
00736 $clone->setOwner($owner);
00737 }
00738 if ($for_test)
00739 {
00740 $clone->saveToDb($original_id);
00741 }
00742 else
00743 {
00744 $clone->saveToDb();
00745 }
00746
00747
00748 $clone->copyPageOfQuestion($original_id);
00749
00750
00751 $clone->duplicateImages($original_id);
00752 return $clone->id;
00753 }
00754
00755 function duplicateImages($question_id)
00756 {
00757 if ($this->get_ordering_type() == OQ_PICTURES)
00758 {
00759 $imagepath = $this->getImagePath();
00760 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
00761 if (!file_exists($imagepath)) {
00762 ilUtil::makeDirParents($imagepath);
00763 }
00764 foreach ($this->answers as $answer)
00765 {
00766 $filename = $answer->get_answertext();
00767 if (!copy($imagepath_original . $filename, $imagepath . $filename)) {
00768 print "image could not be duplicated!!!! ";
00769 }
00770 if (!copy($imagepath_original . $filename . ".thumb.jpg", $imagepath . $filename . ".thumb.jpg")) {
00771 print "image thumbnail could not be duplicated!!!! ";
00772 }
00773 }
00774 }
00775 }
00776
00786 function set_question($question = "")
00787 {
00788 $this->question = $question;
00789 }
00790
00800 function set_ordering_type($ordering_type = OQ_TERMS)
00801 {
00802 $this->ordering_type = $ordering_type;
00803 }
00804
00814 function get_question()
00815 {
00816 return $this->question;
00817 }
00818
00828 function get_ordering_type()
00829 {
00830 return $this->ordering_type;
00831 }
00832
00848 function add_answer(
00849 $answertext = "",
00850 $points = 0.0,
00851 $order = 0,
00852 $solution_order = 0
00853 )
00854 {
00855 $found = -1;
00856 foreach ($this->answers as $key => $value)
00857 {
00858 if ($value->get_order() == $order)
00859 {
00860 $found = $order;
00861 }
00862 }
00863 if ($found >= 0)
00864 {
00865
00866 $answer = new ASS_AnswerOrdering($answertext, $points, $found, $solution_order);
00867 array_push($this->answers, $answer);
00868 for ($i = $found + 1; $i < count($this->answers); $i++)
00869 {
00870 $this->answers[$i] = $this->answers[$i-1];
00871 }
00872 $this->answers[$found] = $answer;
00873 }
00874 else
00875 {
00876
00877 $answer = new ASS_AnswerOrdering($answertext, $points,
00878 count($this->answers), $solution_order);
00879 array_push($this->answers, $answer);
00880 }
00881 }
00882
00894 function get_answer($index = 0)
00895 {
00896 if ($index < 0) return NULL;
00897 if (count($this->answers) < 1) return NULL;
00898 if ($index >= count($this->answers)) return NULL;
00899 return $this->answers[$index];
00900 }
00901
00912 function delete_answer($index = 0)
00913 {
00914 if ($index < 0)
00915 {
00916 return;
00917 }
00918 if (count($this->answers) < 1)
00919 {
00920 return;
00921 }
00922 if ($index >= count($this->answers))
00923 {
00924 return;
00925 }
00926 unset($this->answers[$index]);
00927 $this->answers = array_values($this->answers);
00928 for ($i = 0; $i < count($this->answers); $i++)
00929 {
00930 if ($this->answers[$i]->get_order() > $index)
00931 {
00932 $this->answers[$i]->set_order($i);
00933 }
00934 }
00935 }
00936
00945 function flush_answers()
00946 {
00947 $this->answers = array();
00948 }
00949
00959 function get_answer_count()
00960 {
00961 return count($this->answers);
00962 }
00963
00973 function get_points()
00974 {
00975 return $this->points;
00976 }
00977
00987 function set_points($points = 0.0)
00988 {
00989 $this->points = $points;
00990 }
00991
01001 function get_max_solution_order()
01002 {
01003 if (count($this->answers) == 0)
01004 {
01005 $max = 0;
01006 }
01007 else
01008 {
01009 $max = $this->answers[0]->get_solution_order();
01010 }
01011 foreach ($this->answers as $key => $value)
01012 {
01013 if ($value->get_solution_order() > $max)
01014 {
01015 $max = $value->get_solution_order();
01016 }
01017 }
01018 return $max;
01019 }
01020
01030 function getReachedPoints($user_id, $test_id)
01031 {
01032 $found_value1 = array();
01033 $found_value2 = array();
01034 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01035 $this->ilias->db->quote($user_id),
01036 $this->ilias->db->quote($test_id),
01037 $this->ilias->db->quote($this->getId())
01038 );
01039 $result = $this->ilias->db->query($query);
01040 $user_order = array();
01041 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01042 {
01043 if ((strcmp($data->value1, "") != 0) && (strcmp($data->value2, "") != 0))
01044 {
01045 $user_order[$data->value2] = $data->value1;
01046 }
01047 }
01048 ksort($user_order);
01049 $user_order = array_values($user_order);
01050 $answer_order = array();
01051 foreach ($this->answers as $key => $answer)
01052 {
01053 $answer_order[$answer->get_solution_order()] = $key;
01054 }
01055 ksort($answer_order);
01056 $answer_order = array_values($answer_order);
01057 $points = 0;
01058 foreach ($answer_order as $index => $answer_id)
01059 {
01060 if (strcmp($user_order[$index], "") != 0)
01061 {
01062 if ($answer_id == $user_order[$index])
01063 {
01064 $points += $this->answers[$answer_id]->get_points();
01065 }
01066 }
01067 }
01068 return $points;
01069 }
01070
01080 function getReachedInformation($user_id, $test_id)
01081 {
01082 $found_value1 = array();
01083 $found_value2 = array();
01084 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01085 $this->ilias->db->quote($user_id),
01086 $this->ilias->db->quote($test_id),
01087 $this->ilias->db->quote($this->getId())
01088 );
01089 $result = $this->ilias->db->query($query);
01090 $user_result = array();
01091 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01092 {
01093 $solution = array(
01094 "answer_id" => $data->value1,
01095 "order" => $data->value2
01096 );
01097 $user_result[$data->value1] = $solution;
01098 }
01099 return $user_result;
01100 }
01101
01110 function getMaximumPoints()
01111 {
01112 $points = 0;
01113 foreach ($this->answers as $key => $value)
01114 {
01115 $points += $value->get_points();
01116 }
01117 return $points;
01118 }
01119
01130 function set_image_file($image_filename, $image_tempfilename = "")
01131 {
01132 $result = 0;
01133 if (!empty($image_tempfilename))
01134 {
01135 $imagepath = $this->getImagePath();
01136 if (!file_exists($imagepath))
01137 {
01138 ilUtil::makeDirParents($imagepath);
01139 }
01140
01141 if (!ilUtil::moveUploadedFile($image_tempfilename,$image_filename, $imagepath.$image_filename))
01142 {
01143 $result = 2;
01144 }
01145 else
01146 {
01147 require_once "./content/classes/Media/class.ilObjMediaObject.php";
01148 $mimetype = ilObjMediaObject::getMimeType($imagepath . $image_filename);
01149 if (!preg_match("/^image/", $mimetype))
01150 {
01151 unlink($imagepath . $image_filename);
01152 $result = 1;
01153 }
01154 else
01155 {
01156
01157 $thumbpath = $imagepath . $image_filename . "." . "thumb.jpg";
01158 ilUtil::convertImage($imagepath.$image_filename, $thumbpath, "JPEG", 100);
01159 }
01160 }
01161 }
01162 return $result;
01163 }
01164
01174 function checkSaveData()
01175 {
01176 $result = true;
01177 $order_values = array();
01178 foreach ($_POST as $key => $value)
01179 {
01180 if (preg_match("/^order_(\d+)/", $key, $matches))
01181 {
01182 if (strcmp($value, "") != 0)
01183 {
01184 array_push($order_values, $value);
01185 }
01186 }
01187 }
01188 $check_order = array_flip($order_values);
01189 if (count($check_order) != count($order_values))
01190 {
01191
01192 $result = false;
01193 sendInfo($this->lng->txt("duplicate_order_values_entered"));
01194 }
01195 return $result;
01196 }
01197
01208 function saveWorkingData($test_id, $limit_to = LIMIT_NO_LIMIT)
01209 {
01210 global $ilDB;
01211 global $ilUser;
01212
01213 $saveWorkingDataResult = $this->checkSaveData();
01214 if ($saveWorkingDataResult)
01215 {
01216 $db =& $ilDB->db;
01217
01218 $query = sprintf("DELETE FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01219 $db->quote($ilUser->id),
01220 $db->quote($test_id),
01221 $db->quote($this->getId())
01222 );
01223 $result = $db->query($query);
01224
01225 foreach ($_POST as $key => $value)
01226 {
01227 if (preg_match("/^order_(\d+)/", $key, $matches))
01228 {
01229 if (!(preg_match("/initial_value_\d+/", $value)))
01230 {
01231 $query = sprintf("INSERT INTO tst_solutions (solution_id, user_fi, test_fi, question_fi, value1, value2, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
01232 $db->quote($ilUser->id),
01233 $db->quote($test_id),
01234 $db->quote($this->getId()),
01235 $db->quote($matches[1]),
01236 $db->quote($value)
01237 );
01238 $result = $db->query($query);
01239 }
01240 }
01241 }
01242
01243 }
01244 return $saveWorkingDataResult;
01245 }
01246
01247 function syncWithOriginal()
01248 {
01249 global $ilias;
01250 if ($this->original_id)
01251 {
01252 $complete = 0;
01253 if ($this->isComplete())
01254 {
01255 $complete = 1;
01256 }
01257 $db = & $ilias->db;
01258
01259 $estw_time = $this->getEstimatedWorkingTime();
01260 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
01261
01262 $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, working_time = %s, ordering_type = %s, points = %s, complete = %s WHERE question_id = %s",
01263 $db->quote($this->obj_id. ""),
01264 $db->quote($this->title . ""),
01265 $db->quote($this->comment . ""),
01266 $db->quote($this->author . ""),
01267 $db->quote($this->question . ""),
01268 $db->quote($estw_time . ""),
01269 $db->quote($this->ordering_type . ""),
01270 $db->quote($this->points . ""),
01271 $db->quote($complete . ""),
01272 $db->quote($this->original_id . "")
01273 );
01274 $result = $db->query($query);
01275
01276 if ($result == DB_OK)
01277 {
01278
01279
01280 $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
01281 $db->quote($this->original_id)
01282 );
01283 $result = $db->query($query);
01284
01285 foreach ($this->answers as $key => $value)
01286 {
01287 $answer_obj = $this->answers[$key];
01288 $query = sprintf("INSERT INTO qpl_answers (answer_id, question_fi, answertext, points, aorder, solution_order, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
01289 $db->quote($this->original_id . ""),
01290 $db->quote($answer_obj->get_answertext() . ""),
01291 $db->quote($answer_obj->get_points() . ""),
01292 $db->quote($answer_obj->get_order() . ""),
01293 $db->quote($answer_obj->get_solution_order() . "")
01294 );
01295 $answer_result = $db->query($query);
01296 }
01297 }
01298 parent::syncWithOriginal();
01299 }
01300 }
01301
01302 function pc_array_shuffle($array) {
01303 mt_srand((double)microtime()*1000000);
01304 $i = count($array);
01305 while(--$i)
01306 {
01307 $j = mt_rand(0, $i);
01308 if ($i != $j)
01309 {
01310
01311 $tmp = $array[$j];
01312 $array[$j] = $array[$i];
01313 $array[$i] = $tmp;
01314 }
01315 }
01316 return $array;
01317 }
01318
01319 function createRandomSolution($test_id, $user_id)
01320 {
01321 global $ilDB;
01322 global $ilUser;
01323
01324 $db =& $ilDB->db;
01325
01326 $query = sprintf("DELETE FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01327 $db->quote($user_id),
01328 $db->quote($test_id),
01329 $db->quote($this->getId())
01330 );
01331 $result = $db->query($query);
01332
01333 $orders = range(1, count($this->answers));
01334 $orders = $this->pc_array_shuffle($orders);
01335 foreach ($this->answers as $key => $value)
01336 {
01337 $query = sprintf("INSERT INTO tst_solutions (solution_id, user_fi, test_fi, question_fi, value1, value2, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
01338 $db->quote($user_id),
01339 $db->quote($test_id),
01340 $db->quote($this->getId()),
01341 $db->quote($key),
01342 $db->quote(array_pop($orders))
01343 );
01344 $result = $db->query($query);
01345 }
01346 }
01347
01356 function getQuestionType()
01357 {
01358 return 5;
01359 }
01360 }
01361
01362 ?>