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 include_once "./survey/classes/class.SurveyQuestion.php";
00025 include_once "./survey/classes/inc.SurveyConstants.php";
00026
00038 class SurveyMetricQuestion extends SurveyQuestion
00039 {
00047 var $subtype;
00048
00056 var $minimum;
00057
00065 var $maximum;
00066
00078 function SurveyMetricQuestion(
00079 $title = "",
00080 $description = "",
00081 $author = "",
00082 $questiontext = "",
00083 $owner = -1,
00084 $subtype = 0
00085 )
00086
00087 {
00088 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00089 $this->subtype = $subtype;
00090 $this->minimum = "";
00091 $this->maximum = "";
00092 }
00093
00103 function setSubtype($subtype = SUBTYPE_NON_RATIO)
00104 {
00105 $this->subtype = $subtype;
00106 }
00107
00117 function setMinimum($minimum = 0)
00118 {
00119 $this->minimum = $minimum;
00120 }
00121
00131 function setMaximum($maximum = "")
00132 {
00133 $this->maximum = $maximum;
00134 }
00135
00145 function getSubtype()
00146 {
00147 return $this->subtype;
00148 }
00149
00159 function getMinimum()
00160 {
00161 if ((strlen($this->minimum) == 0) && ($this->getSubtype() > 3))
00162 {
00163 $this->minimum = 0;
00164 }
00165 return $this->minimum;
00166 }
00167
00177 function getMaximum()
00178 {
00179 return $this->maximum;
00180 }
00181
00190 function loadFromDb($id)
00191 {
00192 $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00193 $this->ilias->db->quote($id)
00194 );
00195 $result = $this->ilias->db->query($query);
00196 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00197 {
00198 if ($result->numRows() == 1)
00199 {
00200 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00201 $this->id = $data->question_id;
00202 $this->title = $data->title;
00203 $this->description = $data->description;
00204 $this->obj_id = $data->obj_fi;
00205 $this->obligatory = $data->obligatory;
00206 $this->author = $data->author;
00207 $this->subtype = $data->subtype;
00208 $this->original_id = $data->original_id;
00209 $this->owner = $data->owner_fi;
00210 $this->questiontext = $data->questiontext;
00211 $this->complete = $data->complete;
00212 }
00213
00214 $this->loadMaterialFromDb($id);
00215
00216 $query = sprintf("SELECT survey_variable.* FROM survey_variable WHERE survey_variable.question_fi = %s",
00217 $this->ilias->db->quote($id)
00218 );
00219 $result = $this->ilias->db->query($query);
00220 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00221 {
00222 if ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
00223 {
00224 $this->minimum = $data->value1;
00225 if (($data->value2 < 0) or (strcmp($data->value2, "") == 0))
00226 {
00227 $this->maximum = "";
00228 }
00229 else
00230 {
00231 $this->maximum = $data->value2;
00232 }
00233 }
00234 }
00235 }
00236 parent::loadFromDb($id);
00237 }
00238
00247 function isComplete()
00248 {
00249 if ($this->title and $this->author and $this->questiontext)
00250 {
00251 return 1;
00252 }
00253 else
00254 {
00255 return 0;
00256 }
00257 }
00258
00266 function saveToDb($original_id = "")
00267 {
00268 $complete = 0;
00269 if ($this->isComplete())
00270 {
00271 $complete = 1;
00272 }
00273 if ($original_id)
00274 {
00275 $original_id = $this->ilias->db->quote($original_id);
00276 }
00277 else
00278 {
00279 $original_id = "NULL";
00280 }
00281 if ($this->id == -1)
00282 {
00283
00284 $now = getdate();
00285 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00286 $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)",
00287 $this->ilias->db->quote("$this->subtype"),
00288 $this->ilias->db->quote($this->getQuestionType()),
00289 $this->ilias->db->quote($this->obj_id),
00290 $this->ilias->db->quote($this->owner),
00291 $this->ilias->db->quote($this->title),
00292 $this->ilias->db->quote($this->description),
00293 $this->ilias->db->quote($this->author),
00294 $this->ilias->db->quote($this->questiontext),
00295 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00296 $this->ilias->db->quote("$complete"),
00297 $this->ilias->db->quote($created),
00298 $original_id
00299 );
00300 $result = $this->ilias->db->query($query);
00301 if ($result == DB_OK)
00302 {
00303 $this->id = $this->ilias->db->getLastInsertId();
00304 }
00305 }
00306 else
00307 {
00308
00309 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00310 $this->ilias->db->quote($this->title),
00311 $this->ilias->db->quote("$this->subtype"),
00312 $this->ilias->db->quote($this->description),
00313 $this->ilias->db->quote($this->author),
00314 $this->ilias->db->quote($this->questiontext),
00315 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00316 $this->ilias->db->quote("$complete"),
00317 $this->ilias->db->quote($this->id)
00318 );
00319 $result = $this->ilias->db->query($query);
00320 }
00321 if ($result == DB_OK)
00322 {
00323
00324 $this->saveMaterialsToDb();
00325
00326
00327
00328
00329 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00330 $this->ilias->db->quote($this->id)
00331 );
00332 $result = $this->ilias->db->query($query);
00333
00334 if (strcmp($this->minimum, "") == 0)
00335 {
00336 $min = "NULL";
00337 }
00338 else
00339 {
00340 $min = $this->ilias->db->quote($this->minimum);
00341 }
00342 if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "∞") == 0))
00343 {
00344 $max = -1;
00345 }
00346 else
00347 {
00348 if (strcmp($this->maximum, "") == 0)
00349 {
00350 $max = "NULL";
00351 }
00352 else
00353 {
00354 $max = $this->ilias->db->quote($this->maximum);
00355 }
00356 }
00357 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, value2, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
00358 $this->ilias->db->quote(0),
00359 $this->ilias->db->quote($this->id),
00360 $min,
00361 $max,
00362 $this->ilias->db->quote(0)
00363 );
00364 $answer_result = $this->ilias->db->query($query);
00365 }
00366 parent::saveToDb($original_id);
00367 }
00368
00378 function from_xml($xml_text)
00379 {
00380 $result = false;
00381 if (!empty($this->domxml))
00382 {
00383 $this->domxml->free();
00384 }
00385 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00386 $this->domxml = domxml_open_mem($xml_text);
00387 if (!empty($this->domxml))
00388 {
00389 $root = $this->domxml->document_element();
00390 $item = $root->first_child();
00391 $this->setTitle($item->get_attribute("title"));
00392 $this->gaps = array();
00393 $itemnodes = $item->child_nodes();
00394 foreach ($itemnodes as $index => $node)
00395 {
00396 switch ($node->node_name())
00397 {
00398 case "qticomment":
00399 $comment = $node->get_content();
00400 if (strpos($comment, "ILIAS Version=") !== false)
00401 {
00402 }
00403 elseif (strpos($comment, "Questiontype=") !== false)
00404 {
00405 }
00406 elseif (strpos($comment, "Author=") !== false)
00407 {
00408 $comment = str_replace("Author=", "", $comment);
00409 $this->setAuthor($comment);
00410 }
00411 else
00412 {
00413 $this->setDescription($comment);
00414 }
00415 break;
00416 case "itemmetadata":
00417 $qtimetadata = $node->first_child();
00418 $metadata_fields = $qtimetadata->child_nodes();
00419 foreach ($metadata_fields as $index => $metadata_field)
00420 {
00421 $fieldlabel = $metadata_field->first_child();
00422 $fieldentry = $fieldlabel->next_sibling();
00423 switch ($fieldlabel->get_content())
00424 {
00425 case "obligatory":
00426 $this->setObligatory($fieldentry->get_content());
00427 break;
00428 case "subtype":
00429 $this->setSubtype($fieldentry->get_content());
00430 break;
00431 }
00432 }
00433 break;
00434 case "presentation":
00435 $flow = $node->first_child();
00436 $flownodes = $flow->child_nodes();
00437 foreach ($flownodes as $idx => $flownode)
00438 {
00439 if (strcmp($flownode->node_name(), "material") == 0)
00440 {
00441 $mattext = $flownode->first_child();
00442 $this->setQuestiontext($mattext->get_content());
00443 }
00444 elseif (strcmp($flownode->node_name(), "response_num") == 0)
00445 {
00446 $ident = $flownode->get_attribute("ident");
00447 $shuffle = "";
00448
00449 $response_lid_nodes = $flownode->child_nodes();
00450 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00451 {
00452 switch ($resp_lid_node->node_name())
00453 {
00454 case "render_fib":
00455 $render_choice = $resp_lid_node;
00456 $minnumber = $render_choice->get_attribute("minnumber");
00457 $this->setMinimum($minnumber);
00458 $maxnumber = $render_choice->get_attribute("maxnumber");
00459 $this->setMaximum($maxnumber);
00460 break;
00461 case "material":
00462 $matlabel = $resp_lid_node->get_attribute("label");
00463 $mattype = $resp_lid_node->first_child();
00464 if (strcmp($mattype->node_name(), "mattext") == 0)
00465 {
00466 $material = $mattype->get_content();
00467 if ($material)
00468 {
00469 if ($this->getId() < 1)
00470 {
00471 $this->saveToDb();
00472 }
00473 $this->setMaterial($material, true, $matlabel);
00474 }
00475 }
00476 break;
00477 }
00478 }
00479 }
00480 }
00481 break;
00482 }
00483 }
00484 $result = true;
00485 }
00486 return $result;
00487 }
00488
00498 function to_xml($a_include_header = true, $obligatory_state = "")
00499 {
00500 include_once("./classes/class.ilXmlWriter.php");
00501 $a_xml_writer = new ilXmlWriter;
00502
00503 $a_xml_writer->xmlHeader();
00504 $a_xml_writer->xmlStartTag("questestinterop");
00505 $attrs = array(
00506 "ident" => $this->getId(),
00507 "title" => $this->getTitle()
00508 );
00509 $a_xml_writer->xmlStartTag("item", $attrs);
00510
00511 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
00512 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00513 $a_xml_writer->xmlElement("qticomment", NULL, "Questiontype=".METRIC_QUESTION_IDENTIFIER);
00514 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
00515
00516 $a_xml_writer->xmlStartTag("itemmetadata");
00517 $a_xml_writer->xmlStartTag("qtimetadata");
00518 $a_xml_writer->xmlStartTag("qtimetadatafield");
00519 $a_xml_writer->xmlElement("fieldlabel", NULL, "obligatory");
00520 if (strcmp($obligatory_state, "") != 0)
00521 {
00522 $this->setObligatory($obligatory_state);
00523 }
00524 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getObligatory()));
00525 $a_xml_writer->xmlEndTag("qtimetadatafield");
00526 $a_xml_writer->xmlStartTag("qtimetadatafield");
00527 $a_xml_writer->xmlElement("fieldlabel", NULL, "subtype");
00528 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getSubtype()));
00529 $a_xml_writer->xmlEndTag("qtimetadatafield");
00530 $a_xml_writer->xmlEndTag("qtimetadata");
00531 $a_xml_writer->xmlEndTag("itemmetadata");
00532
00533
00534 $attrs = array(
00535 "label" => $this->getTitle()
00536 );
00537 $a_xml_writer->xmlStartTag("presentation", $attrs);
00538
00539 $a_xml_writer->xmlStartTag("flow");
00540
00541 $a_xml_writer->xmlStartTag("material");
00542 $a_xml_writer->xmlElement("mattext", NULL, $this->getQuestiontext());
00543 $a_xml_writer->xmlEndTag("material");
00544
00545 $attrs = array(
00546 "ident" => "METRIC",
00547 "rcardinality" => "Single"
00548 );
00549 $a_xml_writer->xmlStartTag("response_num", $attrs);
00550
00551 if (count($this->material))
00552 {
00553 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00554 {
00555 $attrs = array(
00556 "label" => $this->material["title"]
00557 );
00558 $a_xml_writer->xmlStartTag("material", $attrs);
00559 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00560 if (strcmp($matches[1], "") != 0)
00561 {
00562 $intlink = $this->material["internal_link"];
00563 }
00564 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
00565 $a_xml_writer->xmlEndTag("material");
00566 }
00567 }
00568
00569 $attrs = array(
00570 "minnumber" => $this->getMinimum(),
00571 "maxnumber" => $this->getMaximum()
00572 );
00573 $a_xml_writer->xmlStartTag("render_fib", $attrs);
00574 $a_xml_writer->xmlEndTag("render_fib");
00575 $a_xml_writer->xmlEndTag("response_num");
00576 $a_xml_writer->xmlEndTag("flow");
00577 $a_xml_writer->xmlEndTag("presentation");
00578 $a_xml_writer->xmlEndTag("item");
00579 $a_xml_writer->xmlEndTag("questestinterop");
00580
00581 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00582 if (!$a_include_header)
00583 {
00584 $pos = strpos($xml, "?>");
00585 $xml = substr($xml, $pos + 2);
00586 }
00587 return $xml;
00588 }
00589
00590 function syncWithOriginal()
00591 {
00592 if ($this->original_id)
00593 {
00594 $complete = 0;
00595 if ($this->isComplete())
00596 {
00597 $complete = 1;
00598 }
00599 $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00600 $this->ilias->db->quote($this->title . ""),
00601 $this->ilias->db->quote($this->subtype . ""),
00602 $this->ilias->db->quote($this->description . ""),
00603 $this->ilias->db->quote($this->author . ""),
00604 $this->ilias->db->quote($this->questiontext . ""),
00605 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00606 $this->ilias->db->quote($complete . ""),
00607 $this->ilias->db->quote($this->original_id . "")
00608 );
00609 $result = $this->ilias->db->query($query);
00610 if ($result == DB_OK)
00611 {
00612
00613
00614
00615 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00616 $this->ilias->db->quote($this->original_id)
00617 );
00618 $result = $this->ilias->db->query($query);
00619
00620 if (strcmp($this->minimum, "") == 0)
00621 {
00622 $min = "NULL";
00623 }
00624 else
00625 {
00626 $min = $this->ilias->db->quote($this->minimum . "");
00627 }
00628 if (preg_match("/[\D]/", $this->maximum) or (strcmp($this->maximum, "∞") == 0))
00629 {
00630 $max = -1;
00631 }
00632 else
00633 {
00634 if (strcmp($this->maximum, "") == 0)
00635 {
00636 $max = "NULL";
00637 }
00638 else
00639 {
00640 $max = $this->ilias->db->quote($this->maximum . "");
00641 }
00642 }
00643 $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, value2, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
00644 $this->ilias->db->quote("0"),
00645 $this->ilias->db->quote($this->original_id . ""),
00646 $min,
00647 $max,
00648 $this->ilias->db->quote("0")
00649 );
00650 $answer_result = $this->ilias->db->query($query);
00651 }
00652 }
00653 parent::syncWithOriginal();
00654 }
00655
00664 function getQuestionType()
00665 {
00666 return 3;
00667 }
00668
00669 }
00670 ?>