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 "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
00025 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
00026
00038 class SurveyTextQuestion extends SurveyQuestion
00039 {
00040 var $maxchars;
00041 var $textwidth;
00042 var $textheight;
00043
00055 function SurveyTextQuestion(
00056 $title = "",
00057 $description = "",
00058 $author = "",
00059 $questiontext = "",
00060 $owner = -1
00061 )
00062
00063 {
00064 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00065 $this->maxchars = 0;
00066 $this->textwidth = 50;
00067 $this->textheight = 5;
00068 }
00069
00079 function _getQuestionDataArray($id)
00080 {
00081 global $ilDB;
00082
00083 $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",
00084 $ilDB->quote($id)
00085 );
00086 $result = $ilDB->query($query);
00087 if ($result->numRows() == 1)
00088 {
00089 return $result->fetchRow(DB_FETCHMODE_ASSOC);
00090 }
00091 else
00092 {
00093 return array();
00094 }
00095 }
00096
00105 function loadFromDb($id)
00106 {
00107 global $ilDB;
00108 $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",
00109 $ilDB->quote($id)
00110 );
00111 $result = $ilDB->query($query);
00112 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00113 {
00114 if ($result->numRows() == 1)
00115 {
00116 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00117 $this->id = $data->question_id;
00118 $this->title = $data->title;
00119 $this->description = $data->description;
00120 $this->obj_id = $data->obj_fi;
00121 $this->author = $data->author;
00122 $this->obligatory = $data->obligatory;
00123 $this->owner = $data->owner_fi;
00124 $this->original_id = $data->original_id;
00125 $this->maxchars = $data->maxchars;
00126 $this->textwidth = $data->width;
00127 $this->textheight = $data->height;
00128 include_once("./Services/RTE/classes/class.ilRTE.php");
00129 $this->questiontext = ilRTE::_replaceMediaObjectImageSrc($data->questiontext, 1);
00130 $this->complete = $data->complete;
00131 }
00132
00133 $this->loadMaterialFromDb($id);
00134 }
00135 parent::loadFromDb($id);
00136 }
00137
00146 function isComplete()
00147 {
00148 if ($this->title and $this->author and $this->questiontext)
00149 {
00150 return 1;
00151 }
00152 else
00153 {
00154 return 0;
00155 }
00156 }
00157
00165 function setMaxChars($maxchars = 0)
00166 {
00167 $this->maxchars = $maxchars;
00168 }
00169
00177 function getMaxChars()
00178 {
00179 return $this->maxchars;
00180 }
00181
00189 function saveToDb($original_id = "")
00190 {
00191 global $ilDB;
00192 $maxchars = "NULL";
00193 if ($this->maxchars)
00194 {
00195 $maxchars = $ilDB->quote($this->maxchars . "");
00196 }
00197 $complete = 0;
00198 if ($this->isComplete()) {
00199 $complete = 1;
00200 }
00201 if ($original_id)
00202 {
00203 $original_id = $ilDB->quote($original_id);
00204 }
00205 else
00206 {
00207 $original_id = "NULL";
00208 }
00209
00210
00211 include_once("./Services/RTE/classes/class.ilRTE.php");
00212 ilRTE::_cleanupMediaObjectUsage($this->questiontext, "spl:html",
00213 $this->getId());
00214
00215 if ($this->id == -1)
00216 {
00217
00218 $now = getdate();
00219 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00220 $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)",
00221 $ilDB->quote($this->getQuestionTypeID() . ""),
00222 $ilDB->quote($this->obj_id),
00223 $ilDB->quote($this->owner),
00224 $ilDB->quote($this->title),
00225 $ilDB->quote($this->description),
00226 $ilDB->quote($this->author),
00227 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00228 $ilDB->quote(sprintf("%d", $this->obligatory)),
00229 $ilDB->quote("$complete"),
00230 $ilDB->quote($created),
00231 $original_id
00232 );
00233 $result = $ilDB->query($query);
00234 if ($result == DB_OK)
00235 {
00236 $this->id = $ilDB->getLastInsertId();
00237 $query = sprintf("INSERT INTO survey_question_text (question_fi, maxchars, width, height) VALUES (%s, %s, %s, %s)",
00238 $ilDB->quote($this->id . ""),
00239 $maxchars,
00240 $ilDB->quote($this->getTextWidth() . ""),
00241 $ilDB->quote($this->getTextHeight() . ""),
00242 $maxchars
00243 );
00244 $ilDB->query($query);
00245 }
00246 }
00247 else
00248 {
00249
00250 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00251 $ilDB->quote($this->title),
00252 $ilDB->quote($this->description),
00253 $ilDB->quote($this->author),
00254 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->questiontext, 0)),
00255 $ilDB->quote(sprintf("%d", $this->obligatory)),
00256 $ilDB->quote("$complete"),
00257 $ilDB->quote($this->id)
00258 );
00259 $result = $ilDB->query($query);
00260 $query = sprintf("UPDATE survey_question_text SET maxchars = %s, width = %s, height = %s WHERE question_fi = %s",
00261 $maxchars,
00262 $ilDB->quote($this->getTextWidth() . ""),
00263 $ilDB->quote($this->getTextHeight() . ""),
00264 $ilDB->quote($this->id . "")
00265 );
00266 $result = $ilDB->query($query);
00267 }
00268 if ($result == DB_OK) {
00269
00270 $this->saveMaterialsToDb();
00271 }
00272 parent::saveToDb($original_id);
00273 }
00274
00283 function toXML($a_include_header = TRUE, $obligatory_state = "")
00284 {
00285 include_once("./classes/class.ilXmlWriter.php");
00286 $a_xml_writer = new ilXmlWriter;
00287 $a_xml_writer->xmlHeader();
00288 $this->insertXML($a_xml_writer, $a_include_header, $obligatory_state);
00289 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00290 if (!$a_include_header)
00291 {
00292 $pos = strpos($xml, "?>");
00293 $xml = substr($xml, $pos + 2);
00294 }
00295 return $xml;
00296 }
00297
00308 function insertXML(&$a_xml_writer, $a_include_header = TRUE, $obligatory_state = "")
00309 {
00310 $attrs = array(
00311 "id" => $this->getId(),
00312 "title" => $this->getTitle(),
00313 "type" => $this->getQuestiontype(),
00314 "obligatory" => $this->getObligatory()
00315 );
00316 $a_xml_writer->xmlStartTag("question", $attrs);
00317
00318 $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
00319 $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
00320 $a_xml_writer->xmlStartTag("questiontext");
00321 $this->addMaterialTag($a_xml_writer, $this->getQuestiontext());
00322 $a_xml_writer->xmlEndTag("questiontext");
00323
00324 $a_xml_writer->xmlStartTag("responses");
00325 $attrs = array(
00326 "id" => "0",
00327 "rows" => $this->getTextHeight(),
00328 "columns" => $this->getTextWidth()
00329 );
00330 if ($this->getMaxChars() > 0)
00331 {
00332 $attrs["maxlength"] = $this->getMaxChars();
00333 }
00334 $a_xml_writer->xmlElement("response_text", $attrs);
00335 $a_xml_writer->xmlEndTag("responses");
00336
00337 if (count($this->material))
00338 {
00339 if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00340 {
00341 $attrs = array(
00342 "label" => $this->material["title"]
00343 );
00344 $a_xml_writer->xmlStartTag("material", $attrs);
00345 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00346 if (strcmp($matches[1], "") != 0)
00347 {
00348 $intlink = $this->material["internal_link"];
00349 }
00350 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
00351 $a_xml_writer->xmlEndTag("material");
00352 }
00353 }
00354
00355 $a_xml_writer->xmlEndTag("question");
00356 }
00357
00358 function syncWithOriginal()
00359 {
00360 global $ilDB;
00361 if ($this->original_id)
00362 {
00363 $complete = 0;
00364 if ($this->isComplete())
00365 {
00366 $complete = 1;
00367 }
00368 $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00369 $ilDB->quote($this->title . ""),
00370 $ilDB->quote($this->description . ""),
00371 $ilDB->quote($this->author . ""),
00372 $ilDB->quote($this->questiontext . ""),
00373 $ilDB->quote(sprintf("%d", $this->obligatory) . ""),
00374 $ilDB->quote($complete . ""),
00375 $ilDB->quote($this->original_id . "")
00376 );
00377 $result = $ilDB->query($query);
00378 $query = sprintf("UPDATE survey_question_text SET maxchars = %s, width = %s, height = %s WHERE question_fi = %s",
00379 $ilDB->quote($this->getMaxChars() . ""),
00380 $ilDB->quote($this->getTextWidth() . ""),
00381 $ilDB->quote($this->getTextHeight() . ""),
00382 $ilDB->quote($this->original_id . "")
00383 );
00384 $result = $ilDB->query($query);
00385 }
00386 parent::syncWithOriginal();
00387 }
00388
00397 function _getMaxChars($question_id)
00398 {
00399 global $ilDB;
00400 $query = sprintf("SELECT maxchars FROM survey_question WHERE question_id = %s",
00401 $ilDB->quote($question_id . "")
00402 );
00403 $result = $ilDB->query($query);
00404 if ($result->numRows())
00405 {
00406 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00407 return $row["maxchars"];
00408 }
00409 return 0;
00410 }
00411
00420 function getQuestionType()
00421 {
00422 return "SurveyTextQuestion";
00423 }
00424
00433 function getAdditionalTableName()
00434 {
00435 return "survey_question_text";
00436 }
00437
00446 function &getWorkingDataFromUserInput($post_data)
00447 {
00448 $entered_value = $post_data[$this->getId() . "_text_question"];
00449 if (strlen($entered_value))
00450 {
00451 $data = array("textanswer" => $entered_value);
00452 }
00453 return $data;
00454 }
00455
00468 function checkUserInput($post_data, $survey_id)
00469 {
00470 $entered_value = $post_data[$this->getId() . "_text_question"];
00471
00472 if ((!$this->getObligatory($survey_id)) && (strlen($entered_value) == 0)) return "";
00473
00474 if (strlen($entered_value) == 0) return $this->lng->txt("text_question_not_filled_out");
00475
00476 return "";
00477 }
00478
00479 function saveUserInput($post_data, $active_id)
00480 {
00481 global $ilDB;
00482
00483 include_once "./Services/Utilities/classes/class.ilUtil.php";
00484 $entered_value = ilUtil::stripSlashes($post_data[$this->getId() . "_text_question"]);
00485 $maxchars = $this->getMaxChars();
00486 if ($maxchars > 0)
00487 {
00488 $entered_value = substr($entered_value, 0, $maxchars);
00489 }
00490 if (strlen($entered_value) == 0) return;
00491 $entered_value = $ilDB->quote($entered_value . "");
00492 $query = sprintf("INSERT INTO survey_answer (answer_id, question_fi, active_fi, value, textanswer, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00493 $ilDB->quote($this->getId() . ""),
00494 $ilDB->quote($active_id . ""),
00495 "NULL",
00496 $entered_value
00497 );
00498 $result = $ilDB->query($query);
00499 }
00500
00501 function &getCumulatedResults($survey_id, $nr_of_users)
00502 {
00503 global $ilDB;
00504
00505 $question_id = $this->getId();
00506
00507 $result_array = array();
00508 $cumulated = array();
00509 $textvalues = array();
00510
00511 $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_answer.question_fi = %s AND survey_finished.survey_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
00512 $ilDB->quote($question_id),
00513 $ilDB->quote($survey_id)
00514 );
00515 $result = $ilDB->query($query);
00516
00517 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00518 {
00519 $cumulated["$row->value"]++;
00520 array_push($textvalues, $row->textanswer);
00521 }
00522 asort($cumulated, SORT_NUMERIC);
00523 end($cumulated);
00524 $numrows = $result->numRows();
00525 $result_array["USERS_ANSWERED"] = $result->numRows();
00526 $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
00527 $result_array["QUESTION_TYPE"] = "SurveyTextQuestion";
00528 $result_array["textvalues"] = $textvalues;
00529 return $result_array;
00530 }
00531
00543 function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
00544 {
00545 include_once ("./classes/class.ilExcelUtils.php");
00546 $worksheet =& $workbook->addWorksheet();
00547 $worksheet->writeString(0, 0, ilExcelUtils::_convert_text($this->lng->txt("title")), $format_bold);
00548 $worksheet->writeString(0, 1, ilExcelUtils::_convert_text($this->getTitle()));
00549 $worksheet->writeString(1, 0, ilExcelUtils::_convert_text($this->lng->txt("question")), $format_bold);
00550 $worksheet->writeString(1, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
00551 $worksheet->writeString(2, 0, ilExcelUtils::_convert_text($this->lng->txt("question_type")), $format_bold);
00552 $worksheet->writeString(2, 1, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())));
00553 $worksheet->writeString(3, 0, ilExcelUtils::_convert_text($this->lng->txt("users_answered")), $format_bold);
00554 $worksheet->write(3, 1, $eval_data["USERS_ANSWERED"]);
00555 $worksheet->writeString(4, 0, ilExcelUtils::_convert_text($this->lng->txt("users_skipped")), $format_bold);
00556 $worksheet->write(4, 1, $eval_data["USERS_SKIPPED"]);
00557 $rowcounter = 5;
00558
00559 $worksheet->write($rowcounter, 0, ilExcelUtils::_convert_text($this->lng->txt("given_answers")), $format_bold);
00560 $textvalues = "";
00561 if (is_array($eval_data["textvalues"]))
00562 {
00563 foreach ($eval_data["textvalues"] as $textvalue)
00564 {
00565 $worksheet->write($rowcounter++, 1, ilExcelUtils::_convert_text($textvalue));
00566 }
00567 }
00568 }
00569
00579 function addUserSpecificResultsData(&$a_array, &$resultset)
00580 {
00581 if (count($resultset["answers"][$this->getId()]))
00582 {
00583 foreach ($resultset["answers"][$this->getId()] as $key => $answer)
00584 {
00585 array_push($a_array, $answer["textanswer"]);
00586 }
00587 }
00588 else
00589 {
00590 array_push($a_array, $this->lng->txt("skipped"));
00591 }
00592 }
00593
00603 function &getUserAnswers($survey_id)
00604 {
00605 global $ilDB;
00606
00607 $answers = array();
00608
00609 $query = sprintf("SELECT survey_answer.* FROM survey_answer, survey_finished WHERE survey_finished.survey_fi = %s AND survey_answer.question_fi = %s AND survey_finished.finished_id = survey_answer.active_fi",
00610 $ilDB->quote($survey_id),
00611 $ilDB->quote($this->getId())
00612 );
00613 $result = $ilDB->query($query);
00614 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00615 {
00616 $answers[$row["active_fi"]] = $row["textanswer"];
00617 }
00618 return $answers;
00619 }
00620
00629 function importResponses($a_data)
00630 {
00631 foreach ($a_data as $id => $data)
00632 {
00633 if ($data["maxlength"] > 0)
00634 {
00635 $this->setMaxChars($data["maxlength"]);
00636 }
00637 if ($data["rows"] > 0)
00638 {
00639 $this->setTextHeight($data["rows"]);
00640 }
00641 if ($data["columns"] > 0)
00642 {
00643 $this->setTextWidth($data["columns"]);
00644 }
00645 }
00646 }
00647
00656 function usableForPrecondition()
00657 {
00658 return FALSE;
00659 }
00660
00669 function getTextWidth()
00670 {
00671 return $this->textwidth;
00672 }
00673
00682 function getTextHeight()
00683 {
00684 return $this->textheight;
00685 }
00686
00695 function setTextWidth($a_textwidth)
00696 {
00697 if ($a_textwidth < 1)
00698 {
00699 $this->textwidth = 50;
00700 }
00701 else
00702 {
00703 $this->textwidth = $a_textwidth;
00704 }
00705 }
00706
00715 function setTextHeight($a_textheight)
00716 {
00717 if ($a_textheight < 1)
00718 {
00719 $this->textheight = 5;
00720 }
00721 else
00722 {
00723 $this->textheight = $a_textheight;
00724 }
00725 }
00726 }
00727 ?>