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