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("TEXT_QUESTION_IDENTIFIER", "Text Question");
00027
00039 class SurveyTextQuestion extends SurveyQuestion {
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
00073 function loadFromDb($id) {
00074 $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00075 $this->ilias->db->quote($id)
00076 );
00077 $result = $this->ilias->db->query($query);
00078 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00079 if ($result->numRows() == 1) {
00080 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00081 $this->id = $data->question_id;
00082 $this->title = $data->title;
00083 $this->description = $data->description;
00084 $this->obj_id = $data->obj_fi;
00085 $this->author = $data->author;
00086 $this->obligatory = $data->obligatory;
00087 $this->owner = $data->owner_fi;
00088 $this->original_id = $data->original_id;
00089 $this->maxchars = $data->maxchars;
00090 $this->questiontext = $data->questiontext;
00091 $this->complete = $data->complete;
00092 }
00093
00094 $this->loadMaterialFromDb($id);
00095 }
00096 parent::loadFromDb($id);
00097 }
00098
00107 function isComplete()
00108 {
00109 if ($this->title and $this->author and $this->questiontext)
00110 {
00111 return 1;
00112 }
00113 else
00114 {
00115 return 0;
00116 }
00117 }
00118
00126 function setMaxChars($maxchars = 0)
00127 {
00128 $this->maxchars = $maxchars;
00129 }
00130
00138 function getMaxChars()
00139 {
00140 return $this->maxchars;
00141 }
00142
00150 function saveToDb($original_id = "")
00151 {
00152 $maxchars = "NULL";
00153 if ($this->maxchars)
00154 {
00155 $maxchars = $this->ilias->db->quote($this->maxchars . "");
00156 }
00157 $complete = 0;
00158 if ($this->isComplete()) {
00159 $complete = 1;
00160 }
00161 if ($original_id)
00162 {
00163 $original_id = $this->ilias->db->quote($original_id);
00164 }
00165 else
00166 {
00167 $original_id = "NULL";
00168 }
00169 if ($this->id == -1) {
00170
00171 $now = getdate();
00172 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00173 $query = sprintf("INSERT INTO survey_question (question_id, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, maxchars, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00174 $this->ilias->db->quote("4"),
00175 $this->ilias->db->quote($this->obj_id),
00176 $this->ilias->db->quote($this->owner),
00177 $this->ilias->db->quote($this->title),
00178 $this->ilias->db->quote($this->description),
00179 $this->ilias->db->quote($this->author),
00180 $this->ilias->db->quote($this->questiontext),
00181 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00182 $maxchars,
00183 $this->ilias->db->quote("$complete"),
00184 $this->ilias->db->quote($created),
00185 $original_id
00186 );
00187 $result = $this->ilias->db->query($query);
00188 if ($result == DB_OK) {
00189 $this->id = $this->ilias->db->getLastInsertId();
00190 }
00191 } else {
00192
00193 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, maxchars = %s, complete = %s WHERE question_id = %s",
00194 $this->ilias->db->quote($this->title),
00195 $this->ilias->db->quote($this->description),
00196 $this->ilias->db->quote($this->author),
00197 $this->ilias->db->quote($this->questiontext),
00198 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00199 $maxchars,
00200 $this->ilias->db->quote("$complete"),
00201 $this->ilias->db->quote($this->id)
00202 );
00203 $result = $this->ilias->db->query($query);
00204 }
00205 if ($result == DB_OK) {
00206
00207 $this->saveMaterialsToDb();
00208 }
00209 parent::saveToDb($original_id);
00210 }
00211
00221 function from_xml($xml_text)
00222 {
00223 $result = false;
00224 if (!empty($this->domxml))
00225 {
00226 $this->domxml->free();
00227 }
00228 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00229 $this->domxml = domxml_open_mem($xml_text);
00230 if (!empty($this->domxml))
00231 {
00232 $root = $this->domxml->document_element();
00233 $item = $root->first_child();
00234 $this->setTitle($item->get_attribute("title"));
00235 $this->gaps = array();
00236 $itemnodes = $item->child_nodes();
00237 foreach ($itemnodes as $index => $node)
00238 {
00239 switch ($node->node_name())
00240 {
00241 case "qticomment":
00242 $comment = $node->get_content();
00243 if (strpos($comment, "ILIAS Version=") !== false)
00244 {
00245 }
00246 elseif (strpos($comment, "Questiontype=") !== false)
00247 {
00248 }
00249 elseif (strpos($comment, "Author=") !== false)
00250 {
00251 $comment = str_replace("Author=", "", $comment);
00252 $this->setAuthor($comment);
00253 }
00254 else
00255 {
00256 $this->setDescription($comment);
00257 }
00258 break;
00259 case "itemmetadata":
00260 $qtimetadata = $node->first_child();
00261 $metadata_fields = $qtimetadata->child_nodes();
00262 foreach ($metadata_fields as $index => $metadata_field)
00263 {
00264 $fieldlabel = $metadata_field->first_child();
00265 $fieldentry = $fieldlabel->next_sibling();
00266 switch ($fieldlabel->get_content())
00267 {
00268 case "obligatory":
00269 $this->setObligatory($fieldentry->get_content());
00270 break;
00271 case "maxchars":
00272 $this->setMaxChars($fieldentry->get_content());
00273 break;
00274 }
00275 }
00276 break;
00277 case "presentation":
00278 $flow = $node->first_child();
00279 $flownodes = $flow->child_nodes();
00280 foreach ($flownodes as $idx => $flownode)
00281 {
00282 if (strcmp($flownode->node_name(), "material") == 0)
00283 {
00284 $mattext = $flownode->first_child();
00285 $this->setQuestiontext($mattext->get_content());
00286 }
00287 elseif (strcmp($flownode->node_name(), "response_str") == 0)
00288 {
00289 $ident = $flownode->get_attribute("ident");
00290 $response_lid_nodes = $flownode->child_nodes();
00291 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00292 {
00293 switch ($resp_lid_node->node_name())
00294 {
00295 case "material":
00296 $matlabel = $resp_lid_node->get_attribute("label");
00297 $mattype = $resp_lid_node->first_child();
00298 if (strcmp($mattype->node_name(), "mattext") == 0)
00299 {
00300 $material = $mattype->get_content();
00301 if ($material)
00302 {
00303 if ($this->getId() < 1)
00304 {
00305 $this->saveToDb();
00306 }
00307 $this->setMaterial($material, true, $matlabel);
00308 }
00309 }
00310 break;
00311 }
00312 }
00313 }
00314 }
00315 break;
00316 }
00317 }
00318 $result = true;
00319 }
00320 return $result;
00321 }
00322
00332 function to_xml($a_include_header = true, $obligatory_state = "")
00333 {
00334 if (!empty($this->domxml))
00335 {
00336 $this->domxml->free();
00337 }
00338 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00339 $xml_header .= "<questestinterop></questestinterop>\n";
00340 $this->domxml = domxml_open_mem($xml_header);
00341 $root = $this->domxml->document_element();
00342
00343 $qtiIdent = $this->domxml->create_element("item");
00344 $qtiIdent->set_attribute("ident", $this->getId());
00345 $qtiIdent->set_attribute("title", $this->getTitle());
00346 $root->append_child($qtiIdent);
00347
00348 $qtiComment = $this->domxml->create_element("qticomment");
00349 $qtiCommentText = $this->domxml->create_text_node($this->getDescription());
00350 $qtiComment->append_child($qtiCommentText);
00351 $qtiIdent->append_child($qtiComment);
00352 $qtiComment = $this->domxml->create_element("qticomment");
00353 $qtiCommentText = $this->domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00354 $qtiComment->append_child($qtiCommentText);
00355 $qtiIdent->append_child($qtiComment);
00356 $qtiComment = $this->domxml->create_element("qticomment");
00357 $qtiCommentText = $this->domxml->create_text_node("Questiontype=".TEXT_QUESTION_IDENTIFIER);
00358 $qtiComment->append_child($qtiCommentText);
00359 $qtiIdent->append_child($qtiComment);
00360 $qtiComment = $this->domxml->create_element("qticomment");
00361 $qtiCommentText = $this->domxml->create_text_node("Author=".$this->getAuthor());
00362 $qtiComment->append_child($qtiCommentText);
00363 $qtiIdent->append_child($qtiComment);
00364
00365 $qtiItemMetadata = $this->domxml->create_element("itemmetadata");
00366 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00367
00368 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00369 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00370 $qtiFieldLabelText = $this->domxml->create_text_node("obligatory");
00371 $qtiFieldLabel->append_child($qtiFieldLabelText);
00372 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00373 if (strcmp($obligatory_state, "") != 0)
00374 {
00375 $this->setObligatory($obligatory_state);
00376 }
00377 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getObligatory()));
00378 $qtiFieldEntry->append_child($qtiFieldEntryText);
00379 $qtiMetadatafield->append_child($qtiFieldLabel);
00380 $qtiMetadatafield->append_child($qtiFieldEntry);
00381 $qtiMetadata->append_child($qtiMetadatafield);
00382
00383
00384 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00385 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00386 $qtiFieldLabelText = $this->domxml->create_text_node("maxchars");
00387 $qtiFieldLabel->append_child($qtiFieldLabelText);
00388 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00389 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getMaxChars()));
00390 $qtiFieldEntry->append_child($qtiFieldEntryText);
00391 $qtiMetadatafield->append_child($qtiFieldLabel);
00392 $qtiMetadatafield->append_child($qtiFieldEntry);
00393 $qtiMetadata->append_child($qtiMetadatafield);
00394
00395 $qtiItemMetadata->append_child($qtiMetadata);
00396 $qtiIdent->append_child($qtiItemMetadata);
00397
00398
00399 $qtiPresentation = $this->domxml->create_element("presentation");
00400 $qtiPresentation->set_attribute("label", $this->getTitle());
00401
00402 $qtiFlow = $this->domxml->create_element("flow");
00403
00404 $qtiMaterial = $this->domxml->create_element("material");
00405 $qtiMatText = $this->domxml->create_element("mattext");
00406 $qtiMatTextText = $this->domxml->create_text_node($this->getQuestiontext());
00407 $qtiMatText->append_child($qtiMatTextText);
00408 $qtiMaterial->append_child($qtiMatText);
00409 $qtiFlow->append_child($qtiMaterial);
00410
00411 $qtiResponseStr = $this->domxml->create_element("response_str");
00412 $qtiResponseStr->set_attribute("ident", "TEXT");
00413 $qtiResponseStr->set_attribute("rcardinality", "Single");
00414
00415 if (count($this->material))
00416 {
00417 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00418 {
00419 $qtiMaterial = $this->domxml->create_element("material");
00420 $qtiMaterial->set_attribute("label", $this->material["title"]);
00421 $qtiMatText = $this->domxml->create_element("mattext");
00422 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00423 if (strcmp($matches[1], "") != 0)
00424 {
00425 $intlink = $this->material["internal_link"];
00426 }
00427 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00428 $qtiMatText->append_child($qtiMatTextText);
00429 $qtiMaterial->append_child($qtiMatText);
00430 $qtiResponseStr->append_child($qtiMaterial);
00431 }
00432 }
00433
00434 $qtiFlow->append_child($qtiResponseStr);
00435 $qtiPresentation->append_child($qtiFlow);
00436 $qtiIdent->append_child($qtiPresentation);
00437 $xml = $this->domxml->dump_mem(true);
00438 if (!$a_include_header)
00439 {
00440 $pos = strpos($xml, "?>");
00441 $xml = substr($xml, $pos + 2);
00442 }
00443
00444 return $xml;
00445 }
00446
00447 function syncWithOriginal()
00448 {
00449 if ($this->original_id)
00450 {
00451 $complete = 0;
00452 if ($this->isComplete()) {
00453 $complete = 1;
00454 }
00455 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00456 $this->ilias->db->quote($this->title . ""),
00457 $this->ilias->db->quote($this->description . ""),
00458 $this->ilias->db->quote($this->author . ""),
00459 $this->ilias->db->quote($this->questiontext . ""),
00460 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00461 $this->ilias->db->quote($complete . ""),
00462 $this->ilias->db->quote($this->original_id . "")
00463 );
00464 $result = $this->ilias->db->query($query);
00465 }
00466 parent::syncWithOriginal();
00467 }
00468
00477 function _getMaxChars($question_id)
00478 {
00479 global $ilDB;
00480 $query = sprintf("SELECT maxchars FROM survey_question WHERE question_id = %s",
00481 $ilDB->quote($question_id . "")
00482 );
00483 $result = $ilDB->query($query);
00484 if ($result->numRows())
00485 {
00486 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00487 return $row["maxchars"];
00488 }
00489 return 0;
00490 }
00491 }
00492 ?>