• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

assessment/classes/class.assClozeTestGUI.php

Go to the documentation of this file.
00001 <?php
00002  /*
00003    +----------------------------------------------------------------------------+
00004    | ILIAS open source                                                          |
00005    +----------------------------------------------------------------------------+
00006    | Copyright (c) 1998-2001 ILIAS open source, University of Cologne           |
00007    |                                                                            |
00008    | This program is free software; you can redistribute it and/or              |
00009    | modify it under the terms of the GNU General Public License                |
00010    | as published by the Free Software Foundation; either version 2             |
00011    | of the License, or (at your option) any later version.                     |
00012    |                                                                            |
00013    | This program is distributed in the hope that it will be useful,            |
00014    | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
00015    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              |
00016    | GNU General Public License for more details.                               |
00017    |                                                                            |
00018    | You should have received a copy of the GNU General Public License          |
00019    | along with this program; if not, write to the Free Software                |
00020    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
00021    +----------------------------------------------------------------------------+
00022 */
00023 
00024 require_once "./assessment/classes/class.assQuestionGUI.php";
00025 require_once "./assessment/classes/class.assClozeTest.php";
00026 
00038 class ASS_ClozeTestGUI extends ASS_QuestionGUI
00039 {
00048         function ASS_ClozeTestGUI(
00049                         $id = -1
00050         )
00051         {
00052                 $this->ASS_QuestionGUI();
00053 //echo "<br>assClozeTestGUI_constructor";
00054                 $this->object = new ASS_ClozeTest();
00055                 if ($id >= 0)
00056                 {
00057                         $this->object->loadFromDb($id);
00058                 }
00059         }
00060 
00069         function getQuestionType()
00070         {
00071                 return "qt_cloze";
00072         }
00073 
00074 
00075         function getCommand($cmd)
00076         {
00077                 if (substr($cmd, 0, 6) == "delete")
00078                 {
00079                         $cmd = "delete";
00080                 }
00081                 if (substr($cmd, 0, 10) == "addTextGap")
00082                 {
00083                         $cmd = "addTextGap";
00084                 }
00085                 if (substr($cmd, 0, 12) == "addSelectGap")
00086                 {
00087                         $cmd = "addSelectGap";
00088                 }
00089                 if (substr($cmd, 0, 20) == "addSuggestedSolution")
00090                 {
00091                         $cmd = "addSuggestedSolution";
00092                 }
00093                 if (substr($cmd, 0, 23) == "removeSuggestedSolution")
00094                 {
00095                         $cmd = "removeSuggestedSolution";
00096                 }
00097 
00098                 return $cmd;
00099         }
00100 
00101 
00109         function editQuestion()
00110         {
00111                 $internallinks = array(
00112                         "lm" => $this->lng->txt("obj_lm"),
00113                         "st" => $this->lng->txt("obj_st"),
00114                         "pg" => $this->lng->txt("obj_pg"),
00115                         "glo" => $this->lng->txt("glossary_term")
00116                 );
00117                 $this->tpl->setVariable("HEADER", $this->object->getTitle());
00118                 $this->getQuestionTemplate("qt_cloze");
00119                 $this->tpl->addBlockFile("QUESTION_DATA", "question_data", "tpl.il_as_qpl_cloze_question.html", true);
00120                 for ($i = 0; $i < $this->object->get_gap_count(); $i++)
00121                 {
00122                         $gap = $this->object->get_gap($i);
00123                         if ($gap[0]->get_cloze_type() == CLOZE_TEXT)
00124                         {
00125                                 $this->tpl->setCurrentBlock("textgap_value");
00126                                 foreach ($gap    as $key => $value)
00127                                 {
00128                                         $this->tpl->setVariable("VALUE_TEXT_GAP", htmlspecialchars($value->get_answertext()));
00129                                         $this->tpl->setVariable("TEXT_VALUE", $this->lng->txt("value"));
00130                                         $this->tpl->setVariable("VALUE_GAP_COUNTER", "$i" . "_" . "$key");
00131                                         $this->tpl->setVariable("DELETE", $this->lng->txt("delete"));
00132                                         $this->tpl->parseCurrentBlock();
00133                                 }
00134 
00135                                 foreach ($internallinks as $key => $value)
00136                                 {
00137                                         $this->tpl->setCurrentBlock("textgap_internallink");
00138                                         $this->tpl->setVariable("TYPE_INTERNAL_LINK", $key);
00139                                         $this->tpl->setVariable("TEXT_INTERNAL_LINK", $value);
00140                                         $this->tpl->parseCurrentBlock();
00141                                 }
00142 
00143                                 $this->tpl->setCurrentBlock("textgap_suggested_solution");
00144                                 $this->tpl->setVariable("TEXT_SOLUTION_HINT", $this->lng->txt("solution_hint"));
00145                                 if (array_key_exists($i, $this->object->suggested_solutions))
00146                                 {
00147                                         $solution_array = $this->object->getSuggestedSolution($i);
00148                                         $href = ASS_Question::_getInternalLinkHref($solution_array["internal_link"]);
00149                                         $this->tpl->setVariable("TEXT_VALUE_SOLUTION_HINT", " <a href=\"$href\" target=\"content\">" . $this->lng->txt("solution_hint"). "</a> ");
00150                                         $this->tpl->setVariable("BUTTON_REMOVE_SOLUTION", $this->lng->txt("remove"));
00151                                         $this->tpl->setVariable("VALUE_GAP_COUNTER_REMOVE", $i);
00152                                         $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("change"));
00153                                         $this->tpl->setVariable("VALUE_SOLUTION_HINT", $solution_array["internal_link"]);
00154                                 }
00155                                 else
00156                                 {
00157                                         $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("add"));
00158                                 }
00159                                 $this->tpl->setVariable("VALUE_GAP_COUNTER", $i);
00160                                 $this->tpl->parseCurrentBlock();
00161                                 $this->tpl->setCurrentBlock("textgap");
00162                                 $answer_array = $this->object->get_gap($i);
00163                                 $answer_points = $answer_array[0]->get_points();
00164                                 $this->tpl->setVariable("VALUE_TEXT_GAP_POINTS", sprintf("%d", $answer_points));
00165                                 $this->tpl->setVariable("ADD_TEXT_GAP", $this->lng->txt("add_gap"));
00166                                 $this->tpl->setVariable("TEXT_POINTS", $this->lng->txt("points"));
00167                                 $this->tpl->setVariable("VALUE_GAP_COUNTER", $i);
00168                                 $this->tpl->parseCurrentBlock();
00169                                 
00170                         }
00171                         elseif ($gap[0]->get_cloze_type() == CLOZE_SELECT)
00172                         {
00173                                 $this->tpl->setCurrentBlock("selectgap_value");
00174                                 foreach ($gap as $key => $value)
00175                                 {
00176                                         $this->tpl->setVariable("TEXT_VALUE", $this->lng->txt("value"));
00177                                         $this->tpl->setVariable("VALUE_SELECT_GAP", htmlspecialchars($value->get_answertext()));
00178                                         $this->tpl->setVariable("VALUE_GAP_COUNTER", "$i" . "_" . "$key");
00179                                         $this->tpl->setVariable("VALUE_GAP", $i);
00180                                         $this->tpl->setVariable("VALUE_INDEX", $key);
00181                                         $this->tpl->setVariable("TEXT_TRUE", $this->lng->txt("true"));
00182                                         $this->tpl->setVariable("VALUE_STATUS_COUNTER", $key);
00183                                         $this->tpl->setVariable("VALUE_GAP", $i);
00184                                         $this->tpl->setVariable("TEXT_POINTS", $this->lng->txt("points"));
00185                                         $this->tpl->setVariable("VALUE_SELECT_GAP_POINTS", sprintf("%d", $value->get_points()));
00186                                         $this->tpl->setVariable("DELETE", $this->lng->txt("delete"));
00187                                         $this->tpl->parseCurrentBlock();
00188                                 }
00189 
00190                                 foreach ($internallinks as $key => $value)
00191                                 {
00192                                         $this->tpl->setCurrentBlock("selectgap_internallink");
00193                                         $this->tpl->setVariable("TYPE_INTERNAL_LINK", $key);
00194                                         $this->tpl->setVariable("TEXT_INTERNAL_LINK", $value);
00195                                         $this->tpl->parseCurrentBlock();
00196                                 }
00197 
00198                                 $this->tpl->setCurrentBlock("selectgap_suggested_solution");
00199                                 $this->tpl->setVariable("TEXT_SOLUTION_HINT", $this->lng->txt("solution_hint"));
00200                                 if (array_key_exists($i, $this->object->suggested_solutions))
00201                                 {
00202                                         $solution_array = $this->object->getSuggestedSolution($i);
00203                                         $href = ASS_Question::_getInternalLinkHref($solution_array["internal_link"]);
00204                                         $this->tpl->setVariable("TEXT_VALUE_SOLUTION_HINT", " <a href=\"$href\" target=\"content\">" . $this->lng->txt("solution_hint"). "</a> ");
00205                                         $this->tpl->setVariable("BUTTON_REMOVE_SOLUTION", $this->lng->txt("remove"));
00206                                         $this->tpl->setVariable("VALUE_GAP_COUNTER_REMOVE", $i);
00207                                         $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("change"));
00208                                         $this->tpl->setVariable("VALUE_SOLUTION_HINT", $solution_array["internal_link"]);
00209                                 }
00210                                 else
00211                                 {
00212                                         $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("add"));
00213                                 }
00214                                 $this->tpl->setVariable("VALUE_GAP_COUNTER", $i);
00215                                 $this->tpl->parseCurrentBlock();
00216                                 $this->tpl->setCurrentBlock("selectgap");
00217                                 $this->tpl->setVariable("ADD_SELECT_GAP", $this->lng->txt("add_gap"));
00218                                 $this->tpl->setVariable("TEXT_SHUFFLE_ANSWERS", $this->lng->txt("shuffle_answers"));
00219                                 $this->tpl->setVariable("VALUE_GAP_COUNTER", "$i");
00220                                 if ($gap[0]->get_shuffle())
00221                                 {
00222                                         $this->tpl->setVariable("SELECTED_YES", " selected=\"selected\"");
00223                                 }
00224                                 else
00225                                 {
00226                                         $this->tpl->setVariable("SELECTED_NO", " selected=\"selected\"");
00227                                 }
00228                                 $this->tpl->setVariable("TXT_YES", $this->lng->txt("yes"));
00229                                 $this->tpl->setVariable("TXT_NO", $this->lng->txt("no"));
00230                                 $this->tpl->parseCurrentBlock();
00231                         }
00232                         $this->tpl->setCurrentBlock("answer_row");
00233                         $name = $gap[0]->get_name();
00234                         if (!$name)
00235                         {
00236                                 $name = $this->lng->txt("gap") . " " . ($i+1);
00237                         }
00238                         $this->tpl->setVariable("TEXT_GAP_NAME", $name);
00239                         $this->tpl->setVariable("TEXT_TYPE", $this->lng->txt("type"));
00240                         if ($gap[0]->get_cloze_type() == CLOZE_SELECT)
00241                         {
00242                                 $this->tpl->setVariable("SELECTED_SELECT_GAP", " selected=\"selected\"");
00243                         }
00244                         else
00245                         {
00246                                 $this->tpl->setVariable("SELECTED_TEXT_GAP", " selected=\"selected\"");
00247                         }
00248                         $this->tpl->setVariable("TEXT_TEXT_GAP", $this->lng->txt("text_gap"));
00249                         $this->tpl->setVariable("TEXT_SELECT_GAP", $this->lng->txt("select_gap"));
00250                         $this->tpl->setVariable("VALUE_GAP_COUNTER", $i);
00251                         $this->tpl->parseCurrentBlock();
00252                 }
00253 
00254                 // call to other question data i.e. estimated working time block
00255                 $this->outOtherQuestionData();
00256 
00257                 $this->tpl->setCurrentBlock("HeadContent");
00258                 $javascript = "<script type=\"text/javascript\">function initialSelect() {\n%s\n}</script>";
00259                 if (preg_match("/addTextGap_(\d+)/", $this->ctrl->getCmd(), $matches))
00260                 {
00261                         $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_cloze_test.textgap_" . $matches[1] . "_" .(count($this->object->gaps[$matches[1]]) - 1).".focus(); document.frm_cloze_test.textgap_" . $matches[1] . "_" .(count($this->object->gaps[$matches[1]]) - 1).".scrollIntoView(\"true\");"));
00262                 }
00263                 else if (preg_match("/addSelectGap_(\d+)/", $this->ctrl->getCmd(), $matches))
00264                 {
00265                         $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_cloze_test.selectgap_" . $matches[1] . "_" .(count($this->object->gaps[$matches[1]]) - 1).".focus(); document.frm_cloze_test.selectgap_" . $matches[1] . "_" .(count($this->object->gaps[$matches[1]]) - 1).".scrollIntoView(\"true\");"));
00266                 }
00267                 else
00268                 {
00269                         $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_cloze_test.title.focus();"));
00270                 }
00271                 $this->tpl->parseCurrentBlock();
00272                 $this->tpl->setCurrentBlock("question_data");
00273                 $this->tpl->setVariable("VALUE_CLOZE_TITLE", htmlspecialchars($this->object->getTitle()));
00274                 $this->tpl->setVariable("VALUE_CLOZE_COMMENT", htmlspecialchars($this->object->getComment()));
00275                 $this->tpl->setVariable("VALUE_CLOZE_AUTHOR", htmlspecialchars($this->object->getAuthor()));
00276                 $cloze_text = $this->object->get_cloze_text();
00277                 $cloze_text = preg_replace("/<br \/>/", "\n", $cloze_text);
00278                 $this->tpl->setVariable("VALUE_CLOZE_TEXT", $cloze_text);
00279                 $this->tpl->setVariable("TEXT_CREATE_GAPS", $this->lng->txt("create_gaps"));
00280                 $this->tpl->setVariable("CLOZE_ID", $this->object->getId());
00281 
00282                 $this->tpl->setVariable("TEXT_TITLE", $this->lng->txt("title"));
00283                 $this->tpl->setVariable("TEXT_AUTHOR", $this->lng->txt("author"));
00284                 $this->tpl->setVariable("TEXT_COMMENT", $this->lng->txt("description"));
00285                 $this->tpl->setVariable("TEXT_CLOZE_TEXT", $this->lng->txt("cloze_text"));
00286                 $this->tpl->setVariable("TEXT_CLOSE_HINT", htmlspecialchars($this->lng->txt("close_text_hint")));
00287                 $this->tpl->setVariable("TEXT_GAP_DEFINITION", $this->lng->txt("gap_definition"));
00288                 $this->tpl->setVariable("SAVE",$this->lng->txt("save"));
00289                 $this->tpl->setVariable("SAVE_EDIT", $this->lng->txt("save_edit"));
00290                 $this->tpl->setVariable("CANCEL",$this->lng->txt("cancel"));
00291                 $this->ctrl->setParameter($this, "sel_question_types", "qt_cloze");
00292                 $this->tpl->setVariable("ACTION_CLOZE_TEST", $this->ctrl->getFormAction($this));
00293                 $this->tpl->setVariable("TXT_REQUIRED_FLD", $this->lng->txt("required_field"));
00294                 $this->tpl->parseCurrentBlock();
00295 
00296                 $this->tpl->setCurrentBlock("adm_content");
00297                 $this->tpl->setVariable("BODY_ATTRIBUTES", " onload=\"initialSelect();\""); 
00298                 $this->tpl->parseCurrentBlock();
00299         }
00300 
00308         function outOtherQuestionData()
00309         {
00310                 $colspan = " colspan=\"4\"";
00311 
00312                 $this->tpl->setCurrentBlock("other_question_data");
00313                 $est_working_time = $this->object->getEstimatedWorkingTime();
00314                 $this->tpl->setVariable("TEXT_WORKING_TIME", $this->lng->txt("working_time"));
00315                 $this->tpl->setVariable("TIME_FORMAT", $this->lng->txt("time_format"));
00316                 $this->tpl->setVariable("VALUE_WORKING_TIME", ilUtil::makeTimeSelect("Estimated", false, $est_working_time[h], $est_working_time[m], $est_working_time[s]));
00317                 $this->tpl->parseCurrentBlock();
00318         }
00319 
00328         function writePostData()
00329         {
00330                 $result = 0;
00331                 $saved = false;
00332 
00333                 // Delete all existing gaps and create new gaps from the form data
00334                 $this->object->flush_gaps();
00335 
00336                 if (!$this->checkInput())
00337                 {
00338                         $result = 1;
00339                 }
00340 
00341                 if (($result) and ($_POST["cmd"]["add"]))
00342                 {
00343                         // You cannot create gaps before you enter the required data
00344                         sendInfo($this->lng->txt("fill_out_all_required_fields_create_gaps"));
00345                         $_POST["cmd"]["add"] = "";
00346                 }
00347 
00348                 $this->object->setTitle(ilUtil::stripSlashes($_POST["title"]));
00349                 $this->object->setAuthor(ilUtil::stripSlashes($_POST["author"]));
00350                 $this->object->setComment(ilUtil::stripSlashes($_POST["comment"]));
00351                 $cloze_text = ilUtil::stripSlashes($_POST["clozetext"], true, "<strong><em><code><cite><gap>");
00352                 $cloze_text = preg_replace("/\n/", "<br />", $cloze_text);
00353                 $this->object->set_cloze_text($cloze_text);
00354                 // adding estimated working time
00355                 $saved = $saved | $this->writeOtherPostData($result);
00356                 $this->object->suggested_solutions = array();
00357                 foreach ($_POST as $key => $value)
00358                 {
00359                         if (preg_match("/^solution_hint_(\d+)/", $key, $matches))
00360                         {
00361                                 if ($value)
00362                                 {
00363                                         $this->object->setSuggestedSolution($value, $matches[1]);
00364                                 }
00365                         }
00366                 }
00367                 
00368                 if ($this->ctrl->getCmd() != "createGaps")
00369                 {
00370                         $this->setGapValues();
00371                         $this->setGapPoints();
00372                         $this->setShuffleState();
00373 
00374                         foreach ($_POST as $key => $value)
00375                         {
00376                                 // Set the cloze type of the gap
00377                                 if (preg_match("/clozetype_(\d+)/", $key, $matches))
00378                                 {
00379                                         $this->object->set_cloze_type($matches[1], $value);
00380                                 }
00381                         }
00382                 }
00383 
00384                 $this->object->update_all_gap_params();
00385                 if ($saved)
00386                 {
00387                         // If the question was saved automatically before an upload, we have to make
00388                         // sure, that the state after the upload is saved. Otherwise the user could be
00389                         // irritated, if he presses cancel, because he only has the question state before
00390                         // the upload process.
00391                         $this->object->saveToDb();
00392                         $_GET["q_id"] = $this->object->getId();
00393                 }
00394                 return $result;
00395         }
00396 
00400         function delete()
00401         {
00402                 $this->writePostData();
00403                 foreach ($_POST["cmd"] as $key => $value)
00404                 {
00405                         // Check, if one of the gap values was deleted
00406                         if (preg_match("/delete_(\d+)_(\d+)/", $key, $matches))
00407                         {
00408                                 $selectgap = "selectgap_" . $matches[1] . "_" . $matches[2];
00409                                 $this->object->delete_answertext_by_index($matches[1], $matches[2]);
00410                         }
00411                 }
00412                 $this->editQuestion();
00413         }
00414 
00415         function addSelectGap()
00416         {
00417                 $this->writePostData();
00418 
00419                 $len = strlen("addSelectGap_");
00420                 $i = substr($this->ctrl->getCmd(), $len);
00421                 $this->object->set_answertext(
00422                         ilUtil::stripSlashes($i),
00423                         ilUtil::stripSlashes($this->object->get_gap_text_count($i)),
00424                         "",
00425                         1
00426                 );
00427 
00428                 $this->editQuestion();
00429         }
00430 
00431         function addTextGap()
00432         {
00433                 $this->writePostData();
00434 
00435                 $len = strlen("addTextGap_");
00436                 $i = substr($this->ctrl->getCmd(), $len);
00437                 $this->object->set_answertext(
00438                         ilUtil::stripSlashes($i),
00439                         ilUtil::stripSlashes($this->object->get_gap_text_count($i)),
00440                         "",
00441                         1
00442                 );
00443 
00444                 $this->editQuestion();
00445         }
00446 
00447         function setGapValues($a_apply_text = true)
00448         {
00449 //echo "<br>SETGapValues:$a_apply_text:";
00450                 foreach ($_POST as $key => $value)
00451                 {
00452                         // Set gap values
00453                         if ($a_apply_text)
00454                         {
00455                                 if (preg_match("/textgap_(\d+)_(\d+)/", $key, $matches))
00456                                 {
00457                                         $answer_array = $this->object->get_gap($matches[1]);
00458                                         if (strlen($value) > 0)
00459                                         {
00460                                                 // Only change gap values <> empty string
00461                                                 if (array_key_exists($matches[2], $answer_array))
00462                                                 {
00463                                                         if (strcmp($value, $answer_array[$matches[2]]->get_answertext()) != 0)
00464                                                         {
00465                                                                 $this->object->set_answertext(
00466                                                                         ilUtil::stripSlashes($matches[1]),
00467                                                                         ilUtil::stripSlashes($matches[2]),
00468                                                                         ilUtil::stripSlashes($value)
00469                                                                 );
00470                                                         }
00471                                                 }
00472                                         }
00473                                         else
00474                                         {
00475                                                 // Display errormessage: You've tried to set an gap value to an empty string!
00476                                         }
00477                                 }
00478                         }
00479 
00480                         if (preg_match("/selectgap_(\d+)_(\d+)/", $key, $matches))
00481                         {
00482                                 $answer_array = $this->object->get_gap($matches[1]);
00483                                 if (strlen($value) > 0)
00484                                 {
00485                                         // Only change gap values <> empty string
00486                                         if (array_key_exists($matches[2], $answer_array))
00487                                         {
00488                                                 if (strcmp($value, $answer_array[$matches[2]]->get_answertext()) != 0)
00489                                                 {
00490                                                         if ($a_apply_text)
00491                                                         {
00492                                                                 $this->object->set_answertext(
00493                                                                         ilUtil::stripSlashes($matches[1]),
00494                                                                         ilUtil::stripSlashes($matches[2]),
00495                                                                         ilUtil::stripSlashes($value)
00496                                                                 );
00497                                                         }
00498                                                 }
00499                                                 if (preg_match("/\d+/", $_POST["points_$matches[1]_$matches[2]"]))
00500                                                 {
00501                                                         $points = $_POST["points_$matches[1]_$matches[2]"];
00502                                                         if ($points < 0)
00503                                                         {
00504                                                                 $points = 0.0;
00505                                                                 sendInfo($this->lng->txt("negative_points_not_allowed"), true);
00506                                                         }
00507                                                 }
00508                                                 else
00509                                                 {
00510                                                         $points = 0.0;
00511                                                 }
00512                                                 $this->object->set_single_answer_points($matches[1], $matches[2], $points);
00513                                                 $this->object->set_single_answer_state($matches[1], $matches[2], 1);
00514                                         }
00515                                 }
00516                                 else
00517                                 {
00518                                         // Display errormessage: You've tried to set an gap value to an empty string!
00519                                 }
00520                         }
00521                 }
00522         }
00523 
00524         function setGapPoints()
00525         {
00526                 foreach ($_POST as $key => $value)
00527                 {
00528                         // Set text gap points
00529                         if (preg_match("/^points_(\d+)$/", $key, $matches))
00530                         {
00531                                 if (preg_match("/\d+/", $value))
00532                                 {
00533                                         $points = $value;
00534                                         if ($points < 0)
00535                                         {
00536                                                 $points = 0.0;
00537                                                 sendInfo($this->lng->txt("negative_points_not_allowed"), true);
00538                                         }
00539                                 }
00540                                 else
00541                                 {
00542                                         $points = 0.0;
00543                                 }
00544                                 $this->object->set_gap_points($matches[1], $points);
00545                         }
00546                 }
00547         }
00548 
00549         function setShuffleState()
00550         {
00551                 foreach ($_POST as $key => $value)
00552                 {
00553                         // Set select gap shuffle state
00554                         if (preg_match("/^shuffle_(\d+)$/", $key, $matches))
00555                         {
00556                                 $this->object->set_gap_shuffle($matches[1], $value);
00557                         }
00558                 }
00559         }
00560 
00564         function createGaps()
00565         {
00566                 $this->writePostData();
00567 
00568                 $this->setGapValues(false);
00569                 $this->setGapPoints();
00570                 $this->setShuffleState();
00571                 $this->object->update_all_gap_params();
00572                 $this->editQuestion();
00573         }
00574 
00575 
00583         function outWorkingForm($test_id = "", $is_postponed = false, $showsolution = 0, $show_question_page=true, $show_solution_only = false, $ilUser = null)
00584         {
00585                 if (!is_object($ilUser)) {
00586                         global $ilUser;
00587                 }
00588                 
00589                 $output = $this->outQuestionPage(($show_solution_only)?"":"CLOZE_TEST", $is_postponed,"", !$show_question_page);
00590                 
00591                 if ($showsolution)
00592                 {
00593                         $solutionintroduction = "<p>" . $this->lng->txt("tst_your_answer_was") . "</p>";
00594                         $output = preg_replace("/(<div[^<]*?ilc_PageTitle.*?<\/div>)/", "\\1" . $solutionintroduction, $output);
00595                 }
00596                 $solutionoutput = preg_replace("/.*?(<div[^<]*?ilc_Question.*?<\/div>).*/", "\\1", $output);
00597                 $solutionoutput = preg_replace("/\"tgap/", "\"solution_tgap", $solutionoutput);
00598                 $solutionoutput = preg_replace("/\"sgap/", "\"solution_sgap", $solutionoutput);
00599                 $solutionoutput = preg_replace("/name=\"gap/", "name=\"solution_gap", $solutionoutput);
00600                 
00601                 // if wants question only then strip everything around question element
00602                 if (!$show_question_page) {              
00603                         $output = preg_replace("/.*?(<div[^<]*?ilc_Question.*?<\/div>).*/", "\\1", $output);
00604                 }
00605                 
00606                 // if wants solution only then strip the question element from output
00607                 if ($show_solution_only) {
00608                         $output = preg_replace("/(<div[^<]*?ilc_Question[^>]*>.*?<\/div>)/", "", $output);
00609                 }
00610                 //echo htmlentities ($output);
00611                 // set solutions
00612                 if ($test_id)
00613                 {
00614                         $solutions =& $this->object->getSolutionValues($test_id, $ilUser);
00615         
00616                         if (is_array($solutions)) 
00617                         {
00618                                 foreach ($solutions as $idx => $solution_value)
00619                                 {
00620                                         // text gaps
00621                                         
00622                                         $repl_str = "dummy=\"tgap_".$solution_value->value1."\"";
00623                                         //
00624                                         /* $output = preg_replace ("/(<input[^>]*?$repl_str.*?>)/" ,"[".$solution_value->value2."]", $output); */
00625                                         if (!$show_question_page)
00626                                         {
00627                                                 $output = $this->replaceInputElements($repl_str, $solution_value->value2, $output,"[","]");                                             
00628                                         }
00629                                         else 
00630                                                 $output = str_replace($repl_str, $repl_str." value=\"".ilUtil::prepareFormOutput($solution_value->value2)."\"", $output);
00631                                         
00632                                         // select gaps
00633                                         $repl_str = "dummy=\"sgap_".$solution_value->value1."_".$solution_value->value2."\"";
00634                                         
00635                                         if (!$show_question_page) 
00636                                         {
00637                                                 $output = $this->replaceSelectElements("gap_".$solution_value->value1, $repl_str, $output,"[","]"); 
00638                                         } else 
00639                                                 $output = str_replace($repl_str, $repl_str." selected=\"selected\"", $output);
00640                                         //echo "<br>".$repl_str;
00641                                 }
00642                         }
00643                         // now replace all empty inputs and selects with an []
00644                         if (!$show_question_page) 
00645                         {
00646                                 $output = $this->removeFormElements($output);
00647                         }
00648                 }
00649 
00650                 if($showsolution)
00651                 {                       
00652                                                 
00653                         foreach ($this->object->gaps as $idx => $gap)
00654                         {
00655                                 $solution_value = "";
00656                                 if (is_array($solutions)) 
00657                                 {
00658                                         foreach ($solutions as $solidx => $solvalue)
00659                                         {
00660                                                 if ($solvalue->value1 == $idx)
00661                                                 {
00662                                                         $solution_value = $solvalue->value2;
00663                                                 }
00664                                         }
00665                                 }
00666                                 if ($gap[0]->get_cloze_type() == CLOZE_SELECT)
00667                                 {
00668                                         $maxpoints = 0;
00669                                         $maxindex = -1;
00670                                         $sol_points = array();
00671                                         foreach ($gap as $answeridx => $answer)
00672                                         {
00673                                                 if ($answer->get_points() > $maxpoints)
00674                                                 {
00675                                                         $maxpoints = $answer->get_points();
00676                                                         $maxindex = $answeridx;
00677                                                 }
00678                                                 if ($show_solution_only && $answer->get_points()>0) 
00679                                                 {                                                       
00680                                                         $regexp = "/<select name=\"solution_gap_$idx\">.*?<option[^>]*dummy=\"solution_sgap_".$idx."_".$answeridx."\">(.*?)<\/option>.*?<\/select>/";
00681                                                         preg_match ($regexp, $solutionoutput, $matches);
00682                                                         $sol_points [] = $matches[1]." <em>(".$answer->get_points()." ".$this->lng->txt("points_short").")</em>";
00683                                                 }
00684                                         }
00685                                                                                                                         
00686                                         if ($this->object->suggested_solutions[$idx])
00687                                         {
00688                                                 if ($showsolution)
00689                                                 {
00690                                                         $href = $this->object->_getInternalLinkHref($this->object->suggested_solutions[$idx]["internal_link"]);
00691                                                         $output = preg_replace("/(<select name\=\"gap_$idx\">.*?<\/select>)/is", "\\1" . " [<a href=\"$href\" target=\"_blank\">".$this->lng->txt("solution_hint")."</a>] " , $output);
00692                                                 }
00693                                         }
00694                                         
00695                                         if (count ($sol_points)<=1)
00696                                                 $solutionoutput = preg_replace("/(<select name\=\"solution_gap_$idx\">.*?<\/select>)/is", "\\1" . " <em>(" . $maxpoints . " " . $this->lng->txt("points") . ")</em> " , $solutionoutput);
00697                                         
00698                                         if ($maxindex > -1)
00699                                         {
00700                                                 $repl_str = "dummy=\"solution_sgap_$idx" . "_$maxindex\"";
00701                                                 if (!$show_solution_only)
00702                                                         $solutionoutput = str_replace($repl_str, $repl_str." selected=\"selected\"", $solutionoutput);
00703                                                 else 
00704                                                 {
00705                                                         if (count ($sol_points)>1) 
00706                                                         {
00707                                                                 $solutionoutput = preg_replace ("/<select[^>]*name=\"solution_gap_$idx\">.*?<\/select>/i","<span class=\"textanswer\">[".join($sol_points,", ")."]</span>",$solutionoutput); 
00708                                                         } else 
00709                                                                 $solutionoutput = $this->replaceSelectElements("solution_gap_$idx",$repl_str, $solutionoutput,"[","]" );
00710                                                 }
00711                                         }
00712                                         
00713                                 }
00714                                 else
00715                                 {
00716                                         
00717                                         $repl_str = "dummy=\"solution_tgap_$idx\"";                                     
00718                                         $pvals = array();
00719                                         foreach ($gap as $answeridx => $answer)
00720                                         {
00721                                                 array_push($pvals, $answer->get_answertext());
00722                                         }
00723                                         $possible_values = join($pvals, " " . $this->lng->txt("or") . " ");
00724                                         
00725                                         $solutionoutput = preg_replace("/(<input[^<]*?dummy\=\"solution_tgap_$idx\"" . "[^>]*?>)/i", "\\1" . " <em>(" . $gap[0]->get_points() . " " . $this->lng->txt("points") . ")</em> ", $solutionoutput);
00726                                         
00727                                         if ($this->object->suggested_solutions[$idx])
00728                                         {
00729                                                 if ($showsolution)
00730                                                 {
00731                                                         $href = $this->object->_getInternalLinkHref($this->object->suggested_solutions[$idx]["internal_link"]);
00732                                                         $output = preg_replace("/(<input[^<]*?dummy\=\"tgap_$idx\"" . "[^>]*?>)/is", "\\1" . " [<a href=\"$href\" target=\"_blank\">".$this->lng->txt("solution_hint")."</a>] " , $output);
00733                                                 }
00734                                         }
00735                                          
00736                                         if (!$show_solution_only)
00737                                                 $solutionoutput = str_replace($repl_str, $repl_str." value=\"".ilUtil::prepareFormOutput($possible_values)."\"", $solutionoutput);
00738                                         else 
00739                                                 $solutionoutput = $this->replaceInputElements($repl_str, $possible_values, $solutionoutput,"[","]");                                    
00740                                 }
00741                         }
00742         
00743                         $solutionoutput = "<p>" . $this->lng->txt("correct_solution_is") . ":</p><p>$solutionoutput</p>";
00744 
00745                         if ($test_id) 
00746                         {
00747                                 $reached_points = $this->object->getReachedPoints($ilUser->id, $test_id);
00748                                 $received_points = "<p>" . sprintf($this->lng->txt("you_received_a_of_b_points"), $reached_points, $this->object->getMaximumPoints());
00749                                 $count_comment = "";
00750                                 if ($reached_points == 0)
00751                                 {
00752                                         $count_comment = $this->object->getSolutionCommentCountSystem($test_id);
00753                                         if (strlen($count_comment))
00754                                         {
00755                                                 if (strlen($mc_comment) == 0)
00756                                                 {
00757                                                         $count_comment = "<span class=\"asterisk\">*</span><br /><br /><span class=\"asterisk\">*</span>$count_comment";
00758                                                 }
00759                                                 else
00760                                                 {
00761                                                         $count_comment = "<br /><span class=\"asterisk\">*</span>$count_comment";
00762                                                 }
00763                                         }
00764                                 }
00765                                 $received_points .= $count_comment;
00766                                 $received_points .= "</p>";
00767                         }
00768                         if ($show_solution_only== true) 
00769                         {
00770                                 $received_points = "";
00771                         }
00772                 }
00773                 if (!$showsolution) {
00774                         $solutionoutput="";
00775                         $received_points = "";
00776                 }
00777                 
00778                 $this->tpl->setVariable("CLOZE_TEST", $output.$solutionoutput.$received_points);
00779                 return;
00780         }
00781 
00789         function outUserSolution($user_id, $test_id)
00790         {
00791                 $results = $this->object->getReachedInformation($user_id, $test_id);
00792                 foreach ($this->object->gaps as $key => $gap)
00793                 {
00794                         $this->tpl->setCurrentBlock("tablerow");
00795                         if ($gap[0]->get_cloze_type() == CLOZE_SELECT)
00796                         {
00797                                 if ($gap[$results[$key]["value"]]->isStateChecked())
00798                                 {
00799                                         $this->tpl->setVariable("ANSWER_IMAGE", ilUtil::getImagePath("right.png", true));
00800                                         $this->tpl->setVariable("ANSWER_IMAGE_TITLE", $this->lng->txt("answer_is_right"));
00801                                 }
00802                                 else
00803                                 {
00804                                         $this->tpl->setVariable("ANSWER_IMAGE", ilUtil::getImagePath("wrong.png", true));
00805                                         $this->tpl->setVariable("ANSWER_IMAGE_TITLE", $this->lng->txt("answer_is_wrong"));
00806                                 }
00807                                 $this->tpl->setVariable("ANSWER_DESCRIPTION", $this->lng->txt("gap") . " " . ($key+1) . ": " . "&quot;<em>" . $gap[$results[$key]["value"]]->get_answertext() . "</em>&quot;");
00808                         }
00809                         else
00810                         {
00811                                 $right = 0;
00812                                 foreach ($gap as $gapkey => $answer)
00813                                 {
00814                                         if (strcmp(strtolower($results[$key]["value"]), strtolower($answer->get_answertext())) == 0)
00815                                         {
00816                                                 $right = 1;
00817                                         }
00818                                 }
00819                                 if ($right)
00820                                 {
00821                                         $this->tpl->setVariable("ANSWER_IMAGE", ilUtil::getImagePath("right.png", true));
00822                                         $this->tpl->setVariable("ANSWER_IMAGE_TITLE", $this->lng->txt("answer_is_right"));
00823                                 }
00824                                 else
00825                                 {
00826                                         $this->tpl->setVariable("ANSWER_IMAGE", ilUtil::getImagePath("wrong.png", true));
00827                                         $this->tpl->setVariable("ANSWER_IMAGE_TITLE", $this->lng->txt("answer_is_wrong"));
00828                                 }
00829                                 $this->tpl->setVariable("ANSWER_DESCRIPTION", $this->lng->txt("gap") . " " . ($key+1) . ": " . "&quot;<em>" . $results[$key]["value"] . "</em>&quot;");
00830                         }
00831                         $this->tpl->parseCurrentBlock();
00832                 }
00833         }
00834 
00838         function checkInput()
00839         {
00840                 if ((!$_POST["title"]) or (!$_POST["author"]) or (!$_POST["clozetext"]))
00841                 {
00842                         return false;
00843                 }
00844                 return true;
00845         }
00846 
00847 
00848         function addSuggestedSolution()
00849         {
00850                 $addForGap = -1;
00851                 if (array_key_exists("cmd", $_POST))
00852                 {
00853                         foreach ($_POST["cmd"] as $key => $value)
00854                         {
00855                                 if (preg_match("/addSuggestedSolution_(\d+)/", $key, $matches))
00856                                 {
00857                                         $addForGap = $matches[1];
00858                                 }
00859                         }
00860                 }
00861                 if ($addForGap > -1)
00862                 {
00863                         $this->writePostData();
00864                         if (!$this->checkInput())
00865                         {
00866                                 sendInfo($this->lng->txt("fill_out_all_required_fields_add_answer"));
00867                                 $this->editQuestion();
00868                                 return;
00869                         }
00870                         $_POST["internalLinkType"] = $_POST["internalLinkType_$addForGap"];
00871                         $_SESSION["subquestion_index"] = $addForGap;
00872                 }
00873                 $this->object->saveToDb();
00874                 $_GET["q_id"] = $this->object->getId();
00875                 $this->tpl->setVariable("HEADER", $this->object->getTitle());
00876                 $this->getQuestionTemplate("qt_cloze");
00877                 parent::addSuggestedSolution();
00878         }
00879 
00880         function removeSuggestedSolution()
00881         {
00882                 $removeFromGap = -1;
00883                 foreach ($_POST["cmd"] as $key => $value)
00884                 {
00885                         if (preg_match("/removeSuggestedSolution_(\d+)/", $key, $matches))
00886                         {
00887                                 $removeFromGap = $matches[1];
00888                         }
00889                 }
00890                 if ($removeFromGap > -1)
00891                 {
00892                         unset($this->object->suggested_solutions[$removeFromGap]);
00893                 }
00894                 $this->object->saveToDb();
00895                 $this->editQuestion();
00896         }
00897                 
00898 
00899 
00900         
00901 }
00902 ?>

Generated on Fri Dec 13 2013 10:18:25 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1