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 SurveyTextQuestion extends SurveyQuestion
00039 {
00040 var $maxchars;
00052 function SurveyTextQuestion(
00053 $title = "",
00054 $description = "",
00055 $author = "",
00056 $questiontext = "",
00057 $owner = -1
00058 )
00059
00060 {
00061 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00062 $this->maxchars = 0;
00063 }
00064
00074 function _getQuestionDataArray($id)
00075 {
00076 global $ilDB;
00077
00078 $query = sprintf("SELECT survey_question.*, survey_question_text.* FROM survey_question, survey_question_text WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_text.question_fi",
00079 $ilDB->quote($id)
00080 );
00081 $result = $ilDB->query($query);
00082 if ($result->numRows() == 1)
00083 {
00084 return $result->fetchRow(DB_FETCHMODE_ASSOC);
00085 }
00086 else
00087 {
00088 return array();
00089 }
00090 }
00091
00100 function loadFromDb($id)
00101 {
00102 global $ilDB;
00103 $query = sprintf("SELECT survey_question.*, survey_question_text.* FROM survey_question, survey_question_text WHERE survey_question.question_id = %s AND survey_question.question_id = survey_question_text.question_fi",
00104 $ilDB->quote($id)
00105 );
00106 $result = $ilDB->query($query);
00107 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00108 {
00109 if ($result->numRows() == 1)
00110 {
00111 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00112 $this->id = $data->question_id;
00113 $this->title = $data->title;
00114 $this->description = $data->description;
00115 $this->obj_id = $data->obj_fi;
00116 $this->author = $data->author;
00117 $this->obligatory = $data->obligatory;
00118 $this->owner = $data->owner_fi;
00119 $this->original_id = $data->original_id;
00120 $this->maxchars = $data->maxchars;
00121 include_once("./Services/RTE/classes/class.ilRTE.php");
00122 $this->questiontext = ilRTE::_replaceMediaObjectImageSrc($data->questiontext, 1);
00123 $this->complete = $data->complete;
00124 }
00125
00126 $this->loadMaterialFromDb($id);
00127 }
00128 parent::loadFromDb($id);
00129 }
00130
00139 function isComplete()
00140 {
00141 if ($this->title and $this->author and $this->questiontext)
00142 {
00143 return 1;
00144 }
00145 else
00146 {
00147 return 0;
00148 }
00149 }
00150
00158 function setMaxChars($maxchars = 0)
00159 {
00160 $this->maxchars = $maxchars;
00161 }
00162
00170 function getMaxChars()
00171 {
00172 return $this->maxchars;
00173 }
00174
00182 function saveToDb($original_id = "")
00183 {
00184 global $ilDB;
00185 $maxchars = "NULL";
00186 if ($this->maxchars)
00187 {
00188 $maxchars = $ilDB->quote($this->maxchars . "");
00189 }
00190 $complete = 0;
00191 if ($this->isComplete()) {
00192 $complete = 1;
00193 }
00194 if ($original_id)
00195 {
00196 $original_id = $ilDB->quote($original_id);
00197 }
00198 else
00199 {
00200 $original_id = "NULL";
00201 }
00202
00203
00204 include_once("./Services/RTE/classes/class.ilRTE.php");
00205 ilRTE::_cleanupMediaObjectUsage($this->questiontext, "spl:html",
00206 $this->getId());
00207
00208 if ($this->id == -1)
00209 {
00210
00211 $now = getdate();
00212 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00213 $query = sprintf("INSERT INTO survey_question (question_id, 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, NULL)",
00214 $ilDB->quote($this->getQuestionType() . ""),
00215 $ilDB->quote($this->obj_id),
00216 $ilDB->quote($this->owner),
00217 $ilDB->quote($this->title),
00218 $ilDB->quote($this->description),
00219 $ilDB->quote($this->author),
00220 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00221 $ilDB->quote(sprintf("%d", $this->obligatory)),
00222 $ilDB->quote("$complete"),
00223 $ilDB->quote($created),
00224 $original_id
00225 );
00226 $result = $ilDB->query($query);
00227 if ($result == DB_OK)
00228 {
00229 $this->id = $ilDB->getLastInsertId();
00230 $query = sprintf("INSERT INTO survey_question_text (question_fi, maxchars) VALUES (%s, %s)",
00231 $ilDB->quote($this->id . ""),
00232 $maxchars
00233 );
00234 $ilDB->query($query);
00235 }
00236 }
00237 else
00238 {
00239
00240 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00241 $ilDB->quote($this->title),
00242 $ilDB->quote($this->description),
00243 $ilDB->quote($this->author),
00244 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00245 $ilDB->quote(sprintf("%d", $this->obligatory)),
00246 $ilDB->quote("$complete"),
00247 $ilDB->quote($this->id)
00248 );
00249 $result = $ilDB->query($query);
00250 $query = sprintf("UPDATE survey_question_text SET maxchars = %s WHERE question_fi = %s",
00251 $maxchars,
00252 $ilDB->quote($this->id . "")
00253 );
00254 $result = $ilDB->query($query);
00255 }
00256 if ($result == DB_OK) {
00257
00258 $this->saveMaterialsToDb();
00259 }
00260 parent::saveToDb($original_id);
00261 }
00262
00272 function from_xml($xml_text)
00273 {
00274 $result = false;
00275 if (!empty($this->domxml))
00276 {
00277 $this->domxml->free();
00278 }
00279 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00280 $this->domxml = domxml_open_mem($xml_text);
00281 if (!empty($this->domxml))
00282 {
00283 $root = $this->domxml->document_element();
00284 $item = $root->first_child();
00285 $this->setTitle($item->get_attribute("title"));
00286 $this->gaps = array();
00287 $itemnodes = $item->child_nodes();
00288 foreach ($itemnodes as $index => $node)
00289 {
00290 switch ($node->node_name())
00291 {
00292 case "qticomment":
00293 $comment = $node->get_content();
00294 if (strpos($comment, "ILIAS Version=") !== false)
00295 {
00296 }
00297 elseif (strpos($comment, "Questiontype=") !== false)
00298 {
00299 }
00300 elseif (strpos($comment, "Author=") !== false)
00301 {
00302 $comment = str_replace("Author=", "", $comment);
00303 $this->setAuthor($comment);
00304 }
00305 else
00306 {
00307 $this->setDescription($comment);
00308 }
00309 break;
00310 case "itemmetadata":
00311 $qtimetadata = $node->first_child();
00312 $metadata_fields = $qtimetadata->child_nodes();
00313 foreach ($metadata_fields as $index => $metadata_field)
00314 {
00315 $fieldlabel = $metadata_field->first_child();
00316 $fieldentry = $fieldlabel->next_sibling();
00317 switch ($fieldlabel->get_content())
00318 {
00319 case "obligatory":
00320 $this->setObligatory($fieldentry->get_content());
00321 break;
00322 case "maxchars":
00323 $this->setMaxChars($fieldentry->get_content());
00324 break;
00325 }
00326 }
00327 break;
00328 case "presentation":
00329 $flow = $node->first_child();
00330 $flownodes = $flow->child_nodes();
00331 foreach ($flownodes as $idx => $flownode)
00332 {
00333 if (strcmp($flownode->node_name(), "material") == 0)
00334 {
00335 $mattext = $flownode->first_child();
00336 $this->setQuestiontext($mattext->get_content());
00337 }
00338 elseif (strcmp($flownode->node_name(), "response_str") == 0)
00339 {
00340 $ident = $flownode->get_attribute("ident");
00341 $response_lid_nodes = $flownode->child_nodes();
00342 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00343 {
00344 switch ($resp_lid_node->node_name())
00345 {
00346 case "material":
00347 $matlabel = $resp_lid_node->get_attribute("label");
00348 $mattype = $resp_lid_node->first_child();
00349 if (strcmp($mattype->node_name(), "mattext") == 0)
00350 {
00351 $material = $mattype->get_content();
00352 if ($material)
00353 {
00354 if ($this->getId() < 1)
00355 {
00356 $this->saveToDb();
00357 }
00358 $this->setMaterial($material, true, $matlabel);
00359 }
00360 }
00361 break;
00362 }
00363 }
00364 }
00365 }
00366 break;
00367 }
00368 }
00369 $result = true;
00370 }
00371 return $result;
00372 }
00373
00383 function to_xml($a_include_header = true, $obligatory_state = "")
00384 {
00385 include_once("./classes/class.ilXmlWriter.php");
00386 $a_xml_writer = new ilXmlWriter;
00387
00388 $a_xml_writer->xmlHeader();
00389 $a_xml_writer->xmlStartTag("questestinterop");
00390 $attrs = array(
00391 "ident" => $this->getId(),
00392 "title" => $this->getTitle()
00393 );
00394 $a_xml_writer->xmlStartTag("item", $attrs);
00395
00396 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
00397 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00398 $a_xml_writer->xmlElement("qticomment", NULL, "Questiontype=".TEXT_QUESTION_IDENTIFIER);
00399 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
00400
00401 $a_xml_writer->xmlStartTag("itemmetadata");
00402 $a_xml_writer->xmlStartTag("qtimetadata");
00403 $a_xml_writer->xmlStartTag("qtimetadatafield");
00404 $a_xml_writer->xmlElement("fieldlabel", NULL, "obligatory");
00405 if (strcmp($obligatory_state, "") != 0)
00406 {
00407 $this->setObligatory($obligatory_state);
00408 }
00409 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getObligatory()));
00410 $a_xml_writer->xmlEndTag("qtimetadatafield");
00411 $a_xml_writer->xmlStartTag("qtimetadatafield");
00412 $a_xml_writer->xmlElement("fieldlabel", NULL, "maxchars");
00413 if (!$this->getMaxChars())
00414 {
00415 $a_xml_writer->xmlElement("fieldentry", NULL, "");
00416 }
00417 else
00418 {
00419 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getMaxChars()));
00420 }
00421 $a_xml_writer->xmlEndTag("qtimetadatafield");
00422 $a_xml_writer->xmlEndTag("qtimetadata");
00423 $a_xml_writer->xmlEndTag("itemmetadata");
00424
00425
00426 $attrs = array(
00427 "label" => $this->getTitle()
00428 );
00429 $a_xml_writer->xmlStartTag("presentation", $attrs);
00430
00431 $a_xml_writer->xmlStartTag("flow");
00432
00433 $this->addQTIMaterial($a_xml_writer, $this->getQuestiontext());
00434
00435 $attrs = array(
00436 "ident" => "TEXT",
00437 "rcardinality" => "Single"
00438 );
00439 $a_xml_writer->xmlStartTag("response_str", $attrs);
00440
00441 if (count($this->material))
00442 {
00443 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00444 {
00445 $attrs = array(
00446 "label" => $this->material["title"]
00447 );
00448 $a_xml_writer->xmlStartTag("material", $attrs);
00449 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00450 if (strcmp($matches[1], "") != 0)
00451 {
00452 $intlink = $this->material["internal_link"];
00453 }
00454 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
00455 $a_xml_writer->xmlEndTag("material");
00456 }
00457 }
00458
00459 $a_xml_writer->xmlEndTag("response_str");
00460 $a_xml_writer->xmlEndTag("flow");
00461 $a_xml_writer->xmlEndTag("presentation");
00462 $a_xml_writer->xmlEndTag("item");
00463 $a_xml_writer->xmlEndTag("questestinterop");
00464
00465 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00466 if (!$a_include_header)
00467 {
00468 $pos = strpos($xml, "?>");
00469 $xml = substr($xml, $pos + 2);
00470 }
00471 return $xml;
00472 }
00473
00474 function syncWithOriginal()
00475 {
00476 global $ilDB;
00477 if ($this->original_id)
00478 {
00479 $complete = 0;
00480 if ($this->isComplete())
00481 {
00482 $complete = 1;
00483 }
00484 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00485 $ilDB->quote($this->title . ""),
00486 $ilDB->quote($this->description . ""),
00487 $ilDB->quote($this->author . ""),
00488 $ilDB->quote($this->questiontext . ""),
00489 $ilDB->quote(sprintf("%d", $this->obligatory) . ""),
00490 $ilDB->quote($complete . ""),
00491 $ilDB->quote($this->original_id . "")
00492 );
00493 $result = $ilDB->query($query);
00494 $query = sprintf("UPDATE survey_question_text SET maxchars = %s WHERE question_fi = %s",
00495 $ilDB->quote($this->getMaxChars() . ""),
00496 $ilDB->quote($this->original_id . "")
00497 );
00498 $result = $ilDB->query($query);
00499 }
00500 parent::syncWithOriginal();
00501 }
00502
00511 function _getMaxChars($question_id)
00512 {
00513 global $ilDB;
00514 $query = sprintf("SELECT maxchars FROM survey_question WHERE question_id = %s",
00515 $ilDB->quote($question_id . "")
00516 );
00517 $result = $ilDB->query($query);
00518 if ($result->numRows())
00519 {
00520 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00521 return $row["maxchars"];
00522 }
00523 return 0;
00524 }
00525
00534 function getQuestionType()
00535 {
00536 return 4;
00537 }
00538
00547 function getAdditionalTableName()
00548 {
00549 return "survey_question_text";
00550 }
00551
00552 function checkUserInput($post_data)
00553 {
00554 $entered_value = $post_data[$this->getId() . "_text_question"];
00555
00556 if ((!$this->getObligatory()) && (strlen($entered_value) == 0)) return "";
00557
00558 if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
00559
00560 return "";
00561 }
00562
00563 function saveUserInput($post_data, $survey_id, $user_id, $anonymous_id)
00564 {
00565 global $ilDB;
00566
00567 include_once "./classes/class.ilUtil.php";
00568 $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
00569 $maxchars = $this->getMaxChars();
00570 if ($maxchars > 0)
00571 {
00572 $entered_value = substr($entered_value, 0, $maxchars);
00573 }
00574 if (strlen($entered_value) == 0) return;
00575 $entered_value = $ilDB->quote($entered_value . "");
00576 $query = sprintf("INSERT INTO survey_answer (answer_id, survey_fi, question_fi, user_fi, anonymous_id, value, textanswer, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, NULL)",
00577 $ilDB->quote($survey_id . ""),
00578 $ilDB->quote($this->getId() . ""),
00579 $ilDB->quote($user_id . ""),
00580 $ilDB->quote($anonymous_id . ""),
00581 "NULL",
00582 $entered_value
00583 );
00584 $result = $ilDB->query($query);
00585 }
00586
00587 function &getCumulatedResults($survey_id, $nr_of_users)
00588 {
00589 global $ilDB;
00590
00591 $question_id = $this->getId();
00592
00593 $result_array = array();
00594 $cumulated = array();
00595 $textvalues = array();
00596
00597 $query = sprintf("SELECT * FROM survey_answer WHERE question_fi = %s AND survey_fi = %s",
00598 $ilDB->quote($question_id),
00599 $ilDB->quote($survey_id)
00600 );
00601 $result = $ilDB->query($query);
00602
00603 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00604 {
00605 $cumulated["$row->value"]++;
00606 array_push($textvalues, $row->textanswer);
00607 }
00608 asort($cumulated, SORT_NUMERIC);
00609 end($cumulated);
00610 $numrows = $result->numRows();
00611 $result_array["USERS_ANSWERED"] = $result->numRows();
00612 $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
00613 $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
00614 $result_array["textvalues"] = $textvalues;
00615 return $result_array;
00616 }
00617
00618 }
00619 ?>