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("SUBTYPE_NON_RATIO", 3);
00027 define("SUBTYPE_RATIO_NON_ABSOLUTE", 4);
00028 define("SUBTYPE_RATIO_ABSOLUTE", 5);
00029
00030 define("METRIC_QUESTION_IDENTIFIER", "Metric Question");
00031
00043 class SurveyMetricQuestion extends SurveyQuestion {
00051 var $subtype;
00052
00060 var $minimum;
00061
00069 var $maximum;
00070
00082 function SurveyMetricQuestion(
00083 $title = "",
00084 $description = "",
00085 $author = "",
00086 $questiontext = "",
00087 $owner = -1,
00088 $subtype = 0
00089 )
00090
00091 {
00092 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00093 $this->subtype = $subtype;
00094 $this->minimum = 0;
00095 $this->maximum = "∞";
00096 }
00097
00107 function setSubtype($subtype = SUBTYPE_NON_RATIO)
00108 {
00109 $this->subtype = $subtype;
00110 }
00111
00121 function setMinimum($minimum = 0)
00122 {
00123 $this->minimum = $minimum;
00124 }
00125
00135 function setMaximum($maximum = "∞")
00136 {
00137 $this->maximum = $maximum;
00138 }
00139
00149 function getSubtype()
00150 {
00151 return $this->subtype;
00152 }
00153
00163 function getMinimum()
00164 {
00165 return $this->minimum;
00166 }
00167
00177 function getMaximum()
00178 {
00179 return $this->maximum;
00180 }
00181
00190 function loadFromDb($id) {
00191 $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00192 $this->ilias->db->quote($id)
00193 );
00194 $result = $this->ilias->db->query($query);
00195 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00196 if ($result->numRows() == 1) {
00197 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00198 $this->id = $data->question_id;
00199 $this->title = $data->title;
00200 $this->description = $data->description;
00201 $this->obj_id = $data->obj_fi;
00202 $this->obligatory = $data->obligatory;
00203 $this->author = $data->author;
00204 $this->subtype = $data->subtype;
00205 $this->original_id = $data->original_id;
00206 $this->owner = $data->owner_fi;
00207 $this->questiontext = $data->questiontext;
00208 $this->complete = $data->complete;
00209 }
00210
00211 $this->loadMaterialFromDb($id);
00212
00213 $query = sprintf("SELECT survey_variable.* FROM survey_variable WHERE survey_variable.question_fi = %s",
00214 $this->ilias->db->quote($id)
00215 );
00216 $result = $this->ilias->db->query($query);
00217 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00218 if ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00219 $this->minimum = $data->value1;
00220 if (($data->value2 < 0) or (strcmp($data->value2, "") == 0))
00221 {
00222 $this->maximum = "∞";
00223 }
00224 else
00225 {
00226 $this->maximum = $data->value2;
00227 }
00228 }
00229 }
00230 }
00231 parent::loadFromDb($id);
00232 }
00233
00242 function isComplete()
00243 {
00244 if ($this->title and $this->author and $this->questiontext)
00245 {
00246 return 1;
00247 }
00248 else
00249 {
00250 return 0;
00251 }
00252 }
00253
00261 function saveToDb($original_id = "")
00262 {
00263 $complete = 0;
00264 if ($this->isComplete()) {
00265 $complete = 1;
00266 }
00267 if ($original_id)
00268 {
00269 $original_id = $this->ilias->db->quote($original_id);
00270 }
00271 else
00272 {
00273 $original_id = "NULL";
00274 }
00275 if ($this->id == -1) {
00276
00277 $now = getdate();
00278 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00279 $query = sprintf("INSERT INTO survey_question (question_id, subtype, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00280 $this->ilias->db->quote("$this->subtype"),
00281 $this->ilias->db->quote("3"),
00282 $this->ilias->db->quote($this->obj_id),
00283 $this->ilias->db->quote($this->owner),
00284 $this->ilias->db->quote($this->title),
00285 $this->ilias->db->quote($this->description),
00286 $this->ilias->db->quote($this->author),
00287 $this->ilias->db->quote($this->questiontext),
00288 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00289 $this->ilias->db->quote("$complete"),
00290 $this->ilias->db->quote($created),
00291 $original_id
00292 );
00293 $result = $this->ilias->db->query($query);
00294 if ($result == DB_OK) {
00295 $this->id = $this->ilias->db->getLastInsertId();
00296 }
00297 } else {
00298
00299 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00300 $this->ilias->db->quote($this->title),
00301 $this->ilias->db->quote("$this->subtype"),
00302 $this->ilias->db->quote($this->description),
00303 $this->ilias->db->quote($this->author),
00304 $this->ilias->db->quote($this->questiontext),
00305 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00306 $this->ilias->db->quote("$complete"),
00307 $this->ilias->db->quote($this->id)
00308 );
00309 $result = $this->ilias->db->query($query);
00310 }
00311 if ($result == DB_OK) {
00312
00313 $this->saveMaterialsToDb();
00314
00315
00316
00317
00318 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00319 $this->ilias->db->quote($this->id)
00320 );
00321 $result = $this->ilias->db->query($query);
00322
00323 if (strcmp($this->minimum, "") == 0)
00324 {
00325 $min = "NULL";
00326 }
00327 else
00328 {
00329 $min = $this->ilias->db->quote($this->minimum);
00330 }
00331 if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "∞") == 0))
00332 {
00333 $max = -1;
00334 }
00335 else
00336 {
00337 if (strcmp($this->maximum, "") == 0)
00338 {
00339 $max = "NULL";
00340 }
00341 else
00342 {
00343 $max = $this->ilias->db->quote($this->maximum);
00344 }
00345 }
00346 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, value2, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
00347 $this->ilias->db->quote(0),
00348 $this->ilias->db->quote($this->id),
00349 $min,
00350 $max,
00351 $this->ilias->db->quote(0)
00352 );
00353 $answer_result = $this->ilias->db->query($query);
00354 }
00355 parent::saveToDb($original_id);
00356 }
00357
00367 function from_xml($xml_text)
00368 {
00369 $result = false;
00370 if (!empty($this->domxml))
00371 {
00372 $this->domxml->free();
00373 }
00374 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00375 $this->domxml = domxml_open_mem($xml_text);
00376 if (!empty($this->domxml))
00377 {
00378 $root = $this->domxml->document_element();
00379 $item = $root->first_child();
00380 $this->setTitle($item->get_attribute("title"));
00381 $this->gaps = array();
00382 $itemnodes = $item->child_nodes();
00383 foreach ($itemnodes as $index => $node)
00384 {
00385 switch ($node->node_name())
00386 {
00387 case "qticomment":
00388 $comment = $node->get_content();
00389 if (strpos($comment, "ILIAS Version=") !== false)
00390 {
00391 }
00392 elseif (strpos($comment, "Questiontype=") !== false)
00393 {
00394 }
00395 elseif (strpos($comment, "Author=") !== false)
00396 {
00397 $comment = str_replace("Author=", "", $comment);
00398 $this->setAuthor($comment);
00399 }
00400 else
00401 {
00402 $this->setDescription($comment);
00403 }
00404 break;
00405 case "itemmetadata":
00406 $qtimetadata = $node->first_child();
00407 $metadata_fields = $qtimetadata->child_nodes();
00408 foreach ($metadata_fields as $index => $metadata_field)
00409 {
00410 $fieldlabel = $metadata_field->first_child();
00411 $fieldentry = $fieldlabel->next_sibling();
00412 switch ($fieldlabel->get_content())
00413 {
00414 case "obligatory":
00415 $this->setObligatory($fieldentry->get_content());
00416 break;
00417 case "subtype":
00418 $this->setSubtype($fieldentry->get_content());
00419 break;
00420 }
00421 }
00422 break;
00423 case "presentation":
00424 $flow = $node->first_child();
00425 $flownodes = $flow->child_nodes();
00426 foreach ($flownodes as $idx => $flownode)
00427 {
00428 if (strcmp($flownode->node_name(), "material") == 0)
00429 {
00430 $mattext = $flownode->first_child();
00431 $this->setQuestiontext($mattext->get_content());
00432 }
00433 elseif (strcmp($flownode->node_name(), "response_num") == 0)
00434 {
00435 $ident = $flownode->get_attribute("ident");
00436 $shuffle = "";
00437
00438 $response_lid_nodes = $flownode->child_nodes();
00439 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00440 {
00441 switch ($resp_lid_node->node_name())
00442 {
00443 case "render_fib":
00444 $render_choice = $resp_lid_node;
00445 $minnumber = $render_choice->get_attribute("minnumber");
00446 $this->setMinimum($minnumber);
00447 $maxnumber = $render_choice->get_attribute("maxnumber");
00448 $this->setMaximum($maxnumber);
00449 break;
00450 case "material":
00451 $matlabel = $resp_lid_node->get_attribute("label");
00452 $mattype = $resp_lid_node->first_child();
00453 if (strcmp($mattype->node_name(), "mattext") == 0)
00454 {
00455 $material = $mattype->get_content();
00456 if ($material)
00457 {
00458 if ($this->getId() < 1)
00459 {
00460 $this->saveToDb();
00461 }
00462 $this->setMaterial($material, true, $matlabel);
00463 }
00464 }
00465 break;
00466 }
00467 }
00468 }
00469 }
00470 break;
00471 }
00472 }
00473 $result = true;
00474 }
00475 return $result;
00476 }
00477
00487 function to_xml($a_include_header = true, $obligatory_state = "")
00488 {
00489 if (!empty($this->domxml))
00490 {
00491 $this->domxml->free();
00492 }
00493 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00494 $xml_header .= "<questestinterop></questestinterop>\n";
00495 $this->domxml = domxml_open_mem($xml_header);
00496 $root = $this->domxml->document_element();
00497
00498 $qtiIdent = $this->domxml->create_element("item");
00499 $qtiIdent->set_attribute("ident", $this->getId());
00500 $qtiIdent->set_attribute("title", $this->getTitle());
00501 $root->append_child($qtiIdent);
00502
00503 $qtiComment = $this->domxml->create_element("qticomment");
00504 $qtiCommentText = $this->domxml->create_text_node($this->getDescription());
00505 $qtiComment->append_child($qtiCommentText);
00506 $qtiIdent->append_child($qtiComment);
00507 $qtiComment = $this->domxml->create_element("qticomment");
00508 $qtiCommentText = $this->domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00509 $qtiComment->append_child($qtiCommentText);
00510 $qtiIdent->append_child($qtiComment);
00511 $qtiComment = $this->domxml->create_element("qticomment");
00512 $qtiCommentText = $this->domxml->create_text_node("Questiontype=".METRIC_QUESTION_IDENTIFIER);
00513 $qtiComment->append_child($qtiCommentText);
00514 $qtiIdent->append_child($qtiComment);
00515 $qtiComment = $this->domxml->create_element("qticomment");
00516 $qtiCommentText = $this->domxml->create_text_node("Author=".$this->getAuthor());
00517 $qtiComment->append_child($qtiCommentText);
00518 $qtiIdent->append_child($qtiComment);
00519
00520 $qtiItemMetadata = $this->domxml->create_element("itemmetadata");
00521 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00522
00523 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00524 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00525 $qtiFieldLabelText = $this->domxml->create_text_node("obligatory");
00526 $qtiFieldLabel->append_child($qtiFieldLabelText);
00527 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00528 if (strcmp($obligatory_state, "") != 0)
00529 {
00530 $this->setObligatory($obligatory_state);
00531 }
00532 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getObligatory()));
00533 $qtiFieldEntry->append_child($qtiFieldEntryText);
00534 $qtiMetadatafield->append_child($qtiFieldLabel);
00535 $qtiMetadatafield->append_child($qtiFieldEntry);
00536 $qtiMetadata->append_child($qtiMetadatafield);
00537 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00538 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00539 $qtiFieldLabelText = $this->domxml->create_text_node("subtype");
00540 $qtiFieldLabel->append_child($qtiFieldLabelText);
00541 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00542 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getSubtype()));
00543 $qtiFieldEntry->append_child($qtiFieldEntryText);
00544 $qtiMetadatafield->append_child($qtiFieldLabel);
00545 $qtiMetadatafield->append_child($qtiFieldEntry);
00546 $qtiMetadata->append_child($qtiMetadatafield);
00547 $qtiItemMetadata->append_child($qtiMetadata);
00548 $qtiIdent->append_child($qtiItemMetadata);
00549
00550
00551 $qtiPresentation = $this->domxml->create_element("presentation");
00552 $qtiPresentation->set_attribute("label", $this->getTitle());
00553
00554 $qtiFlow = $this->domxml->create_element("flow");
00555
00556 $qtiMaterial = $this->domxml->create_element("material");
00557 $qtiMatText = $this->domxml->create_element("mattext");
00558 $qtiMatTextText = $this->domxml->create_text_node($this->getQuestiontext());
00559 $qtiMatText->append_child($qtiMatTextText);
00560 $qtiMaterial->append_child($qtiMatText);
00561 $qtiFlow->append_child($qtiMaterial);
00562
00563 $qtiResponseNum = $this->domxml->create_element("response_num");
00564 $qtiResponseNum->set_attribute("ident", "METRIC");
00565 $qtiResponseNum->set_attribute("rcardinality", "Single");
00566
00567 if (count($this->material))
00568 {
00569 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00570 {
00571 $qtiMaterial = $this->domxml->create_element("material");
00572 $qtiMaterial->set_attribute("label", $this->material["title"]);
00573 $qtiMatText = $this->domxml->create_element("mattext");
00574 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00575 if (strcmp($matches[1], "") != 0)
00576 {
00577 $intlink = $this->material["internal_link"];
00578 }
00579 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00580 $qtiMatText->append_child($qtiMatTextText);
00581 $qtiMaterial->append_child($qtiMatText);
00582 $qtiResponseNum->append_child($qtiMaterial);
00583 }
00584 }
00585
00586 $qtiRenderChoice = $this->domxml->create_element("render_fib");
00587 $qtiRenderChoice->set_attribute("minnumber", $this->getMinimum());
00588 $qtiRenderChoice->set_attribute("maxnumber", $this->getMaximum());
00589
00590 $qtiResponseNum->append_child($qtiRenderChoice);
00591 $qtiFlow->append_child($qtiResponseNum);
00592 $qtiPresentation->append_child($qtiFlow);
00593 $qtiIdent->append_child($qtiPresentation);
00594 $xml = $this->domxml->dump_mem(true);
00595 if (!$a_include_header)
00596 {
00597 $pos = strpos($xml, "?>");
00598 $xml = substr($xml, $pos + 2);
00599 }
00600
00601 return $xml;
00602 }
00603
00604 function syncWithOriginal()
00605 {
00606 if ($this->original_id)
00607 {
00608 $complete = 0;
00609 if ($this->isComplete()) {
00610 $complete = 1;
00611 }
00612 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00613 $this->ilias->db->quote($this->title . ""),
00614 $this->ilias->db->quote($this->subtype . ""),
00615 $this->ilias->db->quote($this->description . ""),
00616 $this->ilias->db->quote($this->author . ""),
00617 $this->ilias->db->quote($this->questiontext . ""),
00618 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00619 $this->ilias->db->quote($complete . ""),
00620 $this->ilias->db->quote($this->original_id . "")
00621 );
00622 $result = $this->ilias->db->query($query);
00623 if ($result == DB_OK)
00624 {
00625
00626
00627
00628 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00629 $this->ilias->db->quote($this->original_id)
00630 );
00631 $result = $this->ilias->db->query($query);
00632
00633 if (strcmp($this->minimum, "") == 0)
00634 {
00635 $min = "NULL";
00636 }
00637 else
00638 {
00639 $min = $this->ilias->db->quote($this->minimum . "");
00640 }
00641 if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "∞") == 0))
00642 {
00643 $max = -1;
00644 }
00645 else
00646 {
00647 if (strcmp($this->maximum, "") == 0)
00648 {
00649 $max = "NULL";
00650 }
00651 else
00652 {
00653 $max = $this->ilias->db->quote($this->maximum . "");
00654 }
00655 }
00656 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, value2, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
00657 $this->ilias->db->quote("0"),
00658 $this->ilias->db->quote($this->original_id . ""),
00659 $min,
00660 $max,
00661 $this->ilias->db->quote("0")
00662 );
00663 $answer_result = $this->ilias->db->query($query);
00664 }
00665 }
00666 parent::syncWithOriginal();
00667 }
00668
00669 }
00670 ?>