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 "./survey/classes/class.SurveyQuestion.php";
00025
00026 define("ORDINAL_QUESTION_IDENTIFIER", "Ordinal Question");
00027
00039 class SurveyOrdinalQuestion extends SurveyQuestion {
00047 var $categories;
00048
00060 function SurveyOrdinalQuestion(
00061 $title = "",
00062 $description = "",
00063 $author = "",
00064 $questiontext = "",
00065 $owner = -1
00066 )
00067
00068 {
00069 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00070 $this->categories = array();
00071 }
00072
00082 function getCategoryCount()
00083 {
00084 return count($this->categories);
00085 }
00086
00097 function addCategoryWithIndex($categoryname, $index)
00098 {
00099 $this->categories[$index] = $categoryname;
00100 }
00101
00111 function addCategory($categoryname)
00112 {
00113 array_push($this->categories, $categoryname);
00114 }
00115
00125 function addCategoryArray($categories)
00126 {
00127 $this->categories = array_merge($this->categories, $categories);
00128 }
00129
00139 function removeCategory($index)
00140 {
00141 array_splice($this->categories, $index, 1);
00142 }
00143
00153 function removeCategories($array)
00154 {
00155 foreach ($array as $index)
00156 {
00157 unset($this->categories[$index]);
00158 }
00159 $this->categories = array_values($this->categories);
00160 }
00161
00171 function removeCategoryWithName($name)
00172 {
00173 $index = array_search($name, $this->categories);
00174 $this->removeCategory($index);
00175 }
00176
00187 function getCategory($index)
00188 {
00189 return $this->categories[$index];
00190 }
00191
00200 function flushCategories()
00201 {
00202 $this->categories = array();
00203 }
00204
00214 function &getAvailablePhrases($useronly = 0)
00215 {
00216 global $ilUser;
00217
00218 $phrases = array();
00219 $query = sprintf("SELECT * FROM survey_phrase WHERE defaultvalue = '1' OR owner_fi = %s ORDER BY title",
00220 $this->ilias->db->quote($ilUser->id)
00221 );
00222 $result = $this->ilias->db->query($query);
00223 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00224 {
00225 if (($row->defaultvalue == 1) and ($row->owner_fi == 0))
00226 {
00227 if (!$useronly)
00228 {
00229 $phrases[$row->phrase_id] = array(
00230 "title" => $this->lng->txt($row->title),
00231 "owner" => $row->owner_fi
00232 );
00233 }
00234 }
00235 else
00236 {
00237 $phrases[$row->phrase_id] = array(
00238 "title" => $row->title,
00239 "owner" => $row->owner_fi
00240 );
00241 }
00242 }
00243 return $phrases;
00244 }
00245
00255 function &getCategoriesForPhrase($phrase_id)
00256 {
00257 $categories = array();
00258 $query = sprintf("SELECT survey_category.* FROM survey_category, survey_phrase_category WHERE survey_phrase_category.category_fi = survey_category.category_id AND survey_phrase_category.phrase_fi = %s ORDER BY survey_phrase_category.sequence",
00259 $this->ilias->db->quote($phrase_id)
00260 );
00261 $result = $this->ilias->db->query($query);
00262 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00263 {
00264 if (($row->defaultvalue == 1) and ($row->owner_fi == 0))
00265 {
00266 $categories[$row->category_id] = $this->lng->txt($row->title);
00267 }
00268 else
00269 {
00270 $categories[$row->category_id] = $row->title;
00271 }
00272 }
00273 return $categories;
00274 }
00275
00284 function addPhrase($phrase_id)
00285 {
00286 global $ilUser;
00287
00288 $query = sprintf("SELECT survey_category.* FROM survey_category, survey_phrase_category WHERE survey_phrase_category.category_fi = survey_category.category_id AND survey_phrase_category.phrase_fi = %s AND (survey_category.owner_fi = 0 OR survey_category.owner_fi = %s) ORDER BY survey_phrase_category.sequence",
00289 $this->ilias->db->quote($phrase_id),
00290 $this->ilias->db->quote($ilUser->id)
00291 );
00292 $result = $this->ilias->db->query($query);
00293 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00294 {
00295 if (($row->defaultvalue == 1) and ($row->owner_fi == 0))
00296 {
00297 array_push($this->categories, $this->lng->txt($row->title));
00298 }
00299 else
00300 {
00301 array_push($this->categories, $row->title);;
00302 }
00303 }
00304 }
00305
00314 function loadFromDb($id) {
00315 $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00316 $this->ilias->db->quote($id)
00317 );
00318 $result = $this->ilias->db->query($query);
00319 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00320 if ($result->numRows() == 1) {
00321 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00322 $this->id = $data->question_id;
00323 $this->title = $data->title;
00324 $this->description = $data->description;
00325 $this->obj_id = $data->obj_fi;
00326 $this->orientation = $data->orientation;
00327 $this->author = $data->author;
00328 $this->owner = $data->owner_fi;
00329 $this->questiontext = $data->questiontext;
00330 $this->obligatory = $data->obligatory;
00331 $this->complete = $data->complete;
00332 $this->original_id = $data->original_id;
00333 }
00334
00335 $this->loadMaterialFromDb($id);
00336
00337 $this->flushCategories();
00338 $query = sprintf("SELECT survey_variable.*, survey_category.title FROM survey_variable, survey_category WHERE survey_variable.question_fi = %s AND survey_variable.category_fi = survey_category.category_id ORDER BY sequence ASC",
00339 $this->ilias->db->quote($id)
00340 );
00341 $result = $this->ilias->db->query($query);
00342 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00343 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00344 array_push($this->categories, $data->title);
00345 }
00346 }
00347 }
00348 parent::loadFromDb($id);
00349 }
00350
00359 function isComplete()
00360 {
00361 if ($this->title and $this->author and $this->questiontext and count($this->categories))
00362 {
00363 return 1;
00364 }
00365 else
00366 {
00367 return 0;
00368 }
00369 }
00370
00378 function saveToDb($original_id = "")
00379 {
00380 $complete = 0;
00381 if ($this->isComplete()) {
00382 $complete = 1;
00383 }
00384 if ($original_id)
00385 {
00386 $original_id = $this->ilias->db->quote($original_id);
00387 }
00388 else
00389 {
00390 $original_id = "NULL";
00391 }
00392 if ($this->id == -1) {
00393
00394 $now = getdate();
00395 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00396 $query = sprintf("INSERT INTO survey_question (question_id, subtype, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, orientation, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00397 $this->ilias->db->quote("0"),
00398 $this->ilias->db->quote("2"),
00399 $this->ilias->db->quote($this->obj_id),
00400 $this->ilias->db->quote($this->owner),
00401 $this->ilias->db->quote($this->title),
00402 $this->ilias->db->quote($this->description),
00403 $this->ilias->db->quote($this->author),
00404 $this->ilias->db->quote($this->questiontext),
00405 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00406 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00407 $this->ilias->db->quote("$complete"),
00408 $this->ilias->db->quote($created),
00409 $original_id
00410 );
00411 $result = $this->ilias->db->query($query);
00412 if ($result == DB_OK) {
00413 $this->id = $this->ilias->db->getLastInsertId();
00414 }
00415 } else {
00416
00417 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, orientation = %s, complete = %s WHERE question_id = %s",
00418 $this->ilias->db->quote($this->title),
00419 $this->ilias->db->quote("0"),
00420 $this->ilias->db->quote($this->description),
00421 $this->ilias->db->quote($this->author),
00422 $this->ilias->db->quote($this->questiontext),
00423 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00424 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00425 $this->ilias->db->quote("$complete"),
00426 $this->ilias->db->quote($this->id)
00427 );
00428 $result = $this->ilias->db->query($query);
00429 }
00430 if ($result == DB_OK) {
00431
00432 $this->saveMaterialsToDb();
00433
00434
00435
00436
00437 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00438 $this->ilias->db->quote($this->id)
00439 );
00440 $result = $this->ilias->db->query($query);
00441
00442 foreach ($this->categories as $key => $value) {
00443 $category_id = $this->saveCategoryToDb($value);
00444 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00445 $this->ilias->db->quote($category_id),
00446 $this->ilias->db->quote($this->id),
00447 $this->ilias->db->quote(($key + 1)),
00448 $this->ilias->db->quote($key)
00449 );
00450 $answer_result = $this->ilias->db->query($query);
00451 }
00452 }
00453 parent::saveToDb($original_id);
00454 }
00455
00465 function from_xml($xml_text)
00466 {
00467 $result = false;
00468 if (!empty($this->domxml))
00469 {
00470 $this->domxml->free();
00471 }
00472 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00473 $this->domxml = domxml_open_mem($xml_text);
00474 if (!empty($this->domxml))
00475 {
00476 $root = $this->domxml->document_element();
00477 $item = $root->first_child();
00478 $this->setTitle($item->get_attribute("title"));
00479 $this->gaps = array();
00480 $itemnodes = $item->child_nodes();
00481 foreach ($itemnodes as $index => $node)
00482 {
00483 switch ($node->node_name())
00484 {
00485 case "qticomment":
00486 $comment = $node->get_content();
00487 if (strpos($comment, "ILIAS Version=") !== false)
00488 {
00489 }
00490 elseif (strpos($comment, "Questiontype=") !== false)
00491 {
00492 }
00493 elseif (strpos($comment, "Author=") !== false)
00494 {
00495 $comment = str_replace("Author=", "", $comment);
00496 $this->setAuthor($comment);
00497 }
00498 else
00499 {
00500 $this->setDescription($comment);
00501 }
00502 break;
00503 case "itemmetadata":
00504 $qtimetadata = $node->first_child();
00505 $metadata_fields = $qtimetadata->child_nodes();
00506 foreach ($metadata_fields as $index => $metadata_field)
00507 {
00508 $fieldlabel = $metadata_field->first_child();
00509 $fieldentry = $fieldlabel->next_sibling();
00510 switch ($fieldlabel->get_content())
00511 {
00512 case "obligatory":
00513 $this->setObligatory($fieldentry->get_content());
00514 break;
00515 }
00516 }
00517 break;
00518 case "presentation":
00519 $flow = $node->first_child();
00520 $flownodes = $flow->child_nodes();
00521 foreach ($flownodes as $idx => $flownode)
00522 {
00523 if (strcmp($flownode->node_name(), "material") == 0)
00524 {
00525 $mattext = $flownode->first_child();
00526 $this->setQuestiontext($mattext->get_content());
00527 }
00528 elseif (strcmp($flownode->node_name(), "response_lid") == 0)
00529 {
00530 $ident = $flownode->get_attribute("ident");
00531 $shuffle = "";
00532
00533 $response_lid_nodes = $flownode->child_nodes();
00534 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00535 {
00536 switch ($resp_lid_node->node_name())
00537 {
00538 case "render_choice":
00539 $render_choice = $resp_lid_node;
00540 $labels = $render_choice->child_nodes();
00541 foreach ($labels as $lidx => $response_label)
00542 {
00543 $material = $response_label->first_child();
00544 $mattext = $material->first_child();
00545 $shuf = 0;
00546 $this->addCategoryWithIndex($mattext->get_content(), $response_label->get_attribute("ident"));
00547 }
00548 break;
00549 case "material":
00550 $matlabel = $resp_lid_node->get_attribute("label");
00551 $mattype = $resp_lid_node->first_child();
00552 if (strcmp($mattype->node_name(), "mattext") == 0)
00553 {
00554 $material = $mattype->get_content();
00555 if ($material)
00556 {
00557 if ($this->getId() < 1)
00558 {
00559 $this->saveToDb();
00560 }
00561 $this->setMaterial($material, true, $matlabel);
00562 }
00563 }
00564 break;
00565 }
00566 }
00567 }
00568 }
00569 break;
00570 }
00571 }
00572 $result = true;
00573 }
00574 return $result;
00575 }
00576
00586 function to_xml($a_include_header = true, $obligatory_state = "")
00587 {
00588 if (!empty($this->domxml))
00589 {
00590 $this->domxml->free();
00591 }
00592 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00593 $xml_header .= "<questestinterop></questestinterop>\n";
00594 $this->domxml = domxml_open_mem($xml_header);
00595 $root = $this->domxml->document_element();
00596
00597 $qtiIdent = $this->domxml->create_element("item");
00598 $qtiIdent->set_attribute("ident", $this->getId());
00599 $qtiIdent->set_attribute("title", $this->getTitle());
00600 $root->append_child($qtiIdent);
00601
00602 $qtiComment = $this->domxml->create_element("qticomment");
00603 $qtiCommentText = $this->domxml->create_text_node($this->getDescription());
00604 $qtiComment->append_child($qtiCommentText);
00605 $qtiIdent->append_child($qtiComment);
00606 $qtiComment = $this->domxml->create_element("qticomment");
00607 $qtiCommentText = $this->domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00608 $qtiComment->append_child($qtiCommentText);
00609 $qtiIdent->append_child($qtiComment);
00610 $qtiComment = $this->domxml->create_element("qticomment");
00611 $qtiCommentText = $this->domxml->create_text_node("Questiontype=".ORDINAL_QUESTION_IDENTIFIER);
00612 $qtiComment->append_child($qtiCommentText);
00613 $qtiIdent->append_child($qtiComment);
00614 $qtiComment = $this->domxml->create_element("qticomment");
00615 $qtiCommentText = $this->domxml->create_text_node("Author=".$this->getAuthor());
00616 $qtiComment->append_child($qtiCommentText);
00617 $qtiIdent->append_child($qtiComment);
00618
00619 $qtiItemMetadata = $this->domxml->create_element("itemmetadata");
00620 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00621
00622 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00623 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00624 $qtiFieldLabelText = $this->domxml->create_text_node("obligatory");
00625 $qtiFieldLabel->append_child($qtiFieldLabelText);
00626 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00627 if (strcmp($obligatory_state, "") != 0)
00628 {
00629 $this->setObligatory($obligatory_state);
00630 }
00631 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getObligatory()));
00632 $qtiFieldEntry->append_child($qtiFieldEntryText);
00633 $qtiMetadatafield->append_child($qtiFieldLabel);
00634 $qtiMetadatafield->append_child($qtiFieldEntry);
00635 $qtiMetadata->append_child($qtiMetadatafield);
00636 $qtiItemMetadata->append_child($qtiMetadata);
00637 $qtiIdent->append_child($qtiItemMetadata);
00638
00639
00640 $qtiPresentation = $this->domxml->create_element("presentation");
00641 $qtiPresentation->set_attribute("label", $this->getTitle());
00642
00643 $qtiFlow = $this->domxml->create_element("flow");
00644
00645 $qtiMaterial = $this->domxml->create_element("material");
00646 $qtiMatText = $this->domxml->create_element("mattext");
00647 $qtiMatTextText = $this->domxml->create_text_node($this->getQuestiontext());
00648 $qtiMatText->append_child($qtiMatTextText);
00649 $qtiMaterial->append_child($qtiMatText);
00650 $qtiFlow->append_child($qtiMaterial);
00651
00652 $qtiResponseLid = $this->domxml->create_element("response_lid");
00653 $qtiResponseLid->set_attribute("ident", "MCSR");
00654 $qtiResponseLid->set_attribute("rcardinality", "Single");
00655
00656 if (count($this->material))
00657 {
00658 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00659 {
00660 $qtiMaterial = $this->domxml->create_element("material");
00661 $qtiMaterial->set_attribute("label", $this->material["title"]);
00662 $qtiMatText = $this->domxml->create_element("mattext");
00663 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00664 if (strcmp($matches[1], "") != 0)
00665 {
00666 $intlink = $this->material["internal_link"];
00667 }
00668 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00669 $qtiMatText->append_child($qtiMatTextText);
00670 $qtiMaterial->append_child($qtiMatText);
00671 $qtiResponseLid->append_child($qtiMaterial);
00672 }
00673 }
00674
00675 $qtiRenderChoice = $this->domxml->create_element("render_choice");
00676 $qtiRenderChoice->set_attribute("shuffle", "no");
00677
00678
00679 for ($index = 0; $index < $this->getCategoryCount(); $index++)
00680 {
00681 $category = $this->getCategory($index);
00682 $qtiResponseLabel = $this->domxml->create_element("response_label");
00683 $qtiResponseLabel->set_attribute("ident", $index);
00684 $qtiMaterial = $this->domxml->create_element("material");
00685 $qtiMatText = $this->domxml->create_element("mattext");
00686 $qtiMatTextText = $this->domxml->create_text_node($category);
00687 $qtiMatText->append_child($qtiMatTextText);
00688 $qtiMaterial->append_child($qtiMatText);
00689 $qtiResponseLabel->append_child($qtiMaterial);
00690 $qtiRenderChoice->append_child($qtiResponseLabel);
00691 }
00692 $qtiResponseLid->append_child($qtiRenderChoice);
00693 $qtiFlow->append_child($qtiResponseLid);
00694 $qtiPresentation->append_child($qtiFlow);
00695 $qtiIdent->append_child($qtiPresentation);
00696 $xml = $this->domxml->dump_mem(true);
00697 if (!$a_include_header)
00698 {
00699 $pos = strpos($xml, "?>");
00700 $xml = substr($xml, $pos + 2);
00701 }
00702
00703 return $xml;
00704 }
00705
00706 function syncWithOriginal()
00707 {
00708 if ($this->original_id)
00709 {
00710 $complete = 0;
00711 if ($this->isComplete()) {
00712 $complete = 1;
00713 }
00714 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00715 $this->ilias->db->quote($this->title . ""),
00716 $this->ilias->db->quote("0"),
00717 $this->ilias->db->quote($this->description . ""),
00718 $this->ilias->db->quote($this->author . ""),
00719 $this->ilias->db->quote($this->questiontext . ""),
00720 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00721 $this->ilias->db->quote($complete . ""),
00722 $this->ilias->db->quote($this->original_id . "")
00723 );
00724 $result = $this->ilias->db->query($query);
00725 if ($result == DB_OK) {
00726
00727
00728
00729 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00730 $this->ilias->db->quote($this->original_id . "")
00731 );
00732 $result = $this->ilias->db->query($query);
00733
00734 foreach ($this->categories as $key => $value) {
00735 $category_id = $this->saveCategoryToDb($value);
00736 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00737 $this->ilias->db->quote($category_id . ""),
00738 $this->ilias->db->quote($this->original_id . ""),
00739 $this->ilias->db->quote(($key + 1) . ""),
00740 $this->ilias->db->quote($key . "")
00741 );
00742 $answer_result = $this->ilias->db->query($query);
00743 }
00744 }
00745 }
00746 parent::syncWithOriginal();
00747 }
00748
00749 }
00750 ?>