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

assessment/classes/class.assJavaApplet.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 require_once "./assessment/classes/class.assQuestion.php";
00024 
00025 define ("JAVAAPPLET_QUESTION_IDENTIFIER", "JAVA APPLET QUESTION");
00026 
00037 class ASS_JavaApplet extends ASS_Question
00038 {
00046         var $question;
00047 
00055         var $javaapplet_filename;
00056 
00064         var $java_code;
00065 
00073         var $java_width;
00074 
00082         var $java_height;
00083 
00091         var $parameters;
00092 
00108         function ASS_JavaApplet(
00109                 $title = "",
00110                 $comment = "",
00111                 $author = "",
00112                 $owner = -1,
00113                 $question = "",
00114                 $javaapplet_filename = ""
00115         )
00116         {
00117                 $this->ASS_Question($title, $comment, $author, $owner);
00118                 $this->question = $question;
00119                 $this->javaapplet_filename = $javaapplet_filename;
00120                 $this->parameters = array();
00121         }
00122 
00123 
00133         function to_xml($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false)
00134         {
00135                 if (!empty($this->domxml))
00136                 {
00137                         $this->domxml->free();
00138                 }
00139                 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<questestinterop></questestinterop>\n";
00140                 $this->domxml = domxml_open_mem($xml_header);
00141                 $root = $this->domxml->document_element();
00142 
00143                 // qti comment with version information
00144                 $qtiComment = $this->domxml->create_element("qticomment");
00145 
00146                 // qti ident
00147                 $qtiIdent = $this->domxml->create_element("item");
00148                 $qtiIdent->set_attribute("ident", "il_".IL_INST_ID."_qst_".$this->getId());
00149                 $qtiIdent->set_attribute("title", $this->getTitle());
00150                 $root->append_child($qtiIdent);
00151 
00152                 // add question description
00153                 $qtiComment = $this->domxml->create_element("qticomment");
00154                 $qtiCommentText = $this->domxml->create_text_node($this->getComment());
00155                 $qtiComment->append_child($qtiCommentText);
00156                 $qtiIdent->append_child($qtiComment);
00157 
00158                 // add estimated working time
00159                 $qtiDuration = $this->domxml->create_element("duration");
00160                 $workingtime = $this->getEstimatedWorkingTime();
00161                 $qtiDurationText = $this->domxml->create_text_node(sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]));
00162                 $qtiDuration->append_child($qtiDurationText);
00163                 $qtiIdent->append_child($qtiDuration);
00164 
00165                 // add ILIAS specific metadata
00166                 $qtiItemmetadata = $this->domxml->create_element("itemmetadata");
00167                 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00168                 
00169                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00170                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00171                 $qtiFieldlabelText = $this->domxml->create_text_node("ILIAS_VERSION");
00172                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00173                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00174                 $qtiFieldentryText = $this->domxml->create_text_node($this->ilias->getSetting("ilias_version"));
00175                 $qtiFieldentry->append_child($qtiFieldentryText);
00176                 $qtiMetadatafield->append_child($qtiFieldlabel);
00177                 $qtiMetadatafield->append_child($qtiFieldentry);
00178                 $qtiMetadata->append_child($qtiMetadatafield);
00179 
00180                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00181                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00182                 $qtiFieldlabelText = $this->domxml->create_text_node("QUESTIONTYPE");
00183                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00184                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00185                 $qtiFieldentryText = $this->domxml->create_text_node(JAVAAPPLET_QUESTION_IDENTIFIER);
00186                 $qtiFieldentry->append_child($qtiFieldentryText);
00187                 $qtiMetadatafield->append_child($qtiFieldlabel);
00188                 $qtiMetadatafield->append_child($qtiFieldentry);
00189                 $qtiMetadata->append_child($qtiMetadatafield);
00190                 
00191                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00192                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00193                 $qtiFieldlabelText = $this->domxml->create_text_node("AUTHOR");
00194                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00195                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00196                 $qtiFieldentryText = $this->domxml->create_text_node($this->getAuthor());
00197                 $qtiFieldentry->append_child($qtiFieldentryText);
00198                 $qtiMetadatafield->append_child($qtiFieldlabel);
00199                 $qtiMetadatafield->append_child($qtiFieldentry);
00200                 $qtiMetadata->append_child($qtiMetadatafield);
00201                 
00202                 $qtiItemmetadata->append_child($qtiMetadata);
00203                 $qtiIdent->append_child($qtiItemmetadata);
00204                 
00205                 // PART I: qti presentation
00206                 $qtiPresentation = $this->domxml->create_element("presentation");
00207                 $qtiPresentation->set_attribute("label", $this->getTitle());
00208 
00209                 // add flow to presentation
00210                 $qtiFlow = $this->domxml->create_element("flow");
00211 
00212                 // add material with question text to presentation
00213                 $qtiMaterial = $this->domxml->create_element("material");
00214                 $qtiMatText = $this->domxml->create_element("mattext");
00215                 $qtiMatTextText = $this->domxml->create_text_node($this->getQuestion());
00216                 $qtiMatText->append_child($qtiMatTextText);
00217                 $qtiMaterial->append_child($qtiMatText);
00218                 $qtiFlow->append_child($qtiMaterial);
00219 
00220                 $solution = $this->getSuggestedSolution(0);
00221                 if (count($solution))
00222                 {
00223                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
00224                         {
00225                                 $qtiMaterial = $this->domxml->create_element("material");
00226                                 $qtiMaterial->set_attribute("label", "suggested_solution");
00227                                 $qtiMatText = $this->domxml->create_element("mattext");
00228                                 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00229                                 if (strcmp($matches[1], "") != 0)
00230                                 {
00231                                         $intlink = $solution["internal_link"];
00232                                 }
00233                                 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00234                                 $qtiMatText->append_child($qtiMatTextText);
00235                                 $qtiMaterial->append_child($qtiMatText);
00236                                 $qtiFlow->append_child($qtiMaterial);
00237                         }
00238                 }
00239                 
00240                 $qtiMaterial = $this->domxml->create_element("material");
00241                 $qtiMatApplet = $this->domxml->create_element("matapplet");
00242                 $qtiMatApplet->set_attribute("label", "applet data");
00243                 $qtiMatApplet->set_attribute("uri", $this->getJavaAppletFilename());
00244                 $qtiMatApplet->set_attribute("height", $this->getJavaHeight());
00245                 $qtiMatApplet->set_attribute("width", $this->getJavaWidth());
00246                 $qtiMatApplet->set_attribute("embedded", "base64");
00247                 $javapath = $this->getJavaPath() . $this->getJavaAppletFilename();
00248                 $fh = @fopen($javapath, "rb");
00249                 if ($fh == false)
00250                 {
00251                         //global $ilErr;
00252                         //$ilErr->raiseError($this->lng->txt("error_open_java_file"), $ilErr->MESSAGE);
00253                         return;
00254                 }
00255                 $javafile = fread($fh, filesize($javapath));
00256                 fclose($fh);
00257                 $base64 = base64_encode($javafile);
00258                 $qtiBase64Data = $this->domxml->create_text_node($base64);
00259                 $qtiMatApplet->append_child($qtiBase64Data);
00260                 $qtiMaterial->append_child($qtiMatApplet);
00261                 if ($this->buildParamsOnly())
00262                 {
00263                         if ($this->java_code)
00264                         {
00265                                 $qtiMatText = $this->domxml->create_element("mattext");
00266                                 $qtiMatText->set_attribute("label", "java_code");
00267                                 $qtiAppletParams = $this->domxml->create_text_node($this->java_code);
00268                                 $qtiMatText->append_child($qtiAppletParams);
00269                                 $qtiMaterial->append_child($qtiMatText);
00270                         }
00271                         foreach ($this->parameters as $key => $value)
00272                         {
00273                                 $qtiMatText = $this->domxml->create_element("mattext");
00274                                 $qtiMatText->set_attribute("label", $value["name"]);
00275                                 $qtiAppletParams = $this->domxml->create_text_node($value["value"]);
00276                                 $qtiMatText->append_child($qtiAppletParams);
00277                                 $qtiMaterial->append_child($qtiMatText);
00278                         }
00279                         if ($test_output)
00280                         {
00281                                 require_once "./assessment/classes/class.ilObjTest.php";
00282                                 $qtiMatText = $this->domxml->create_element("mattext");
00283                                 $qtiMatText->set_attribute("label", "test_type");
00284                                 $qtiAppletParams = $this->domxml->create_text_node(ilObjTest::_getTestType($test_output));
00285                                 $qtiMatText->append_child($qtiAppletParams);
00286                                 $qtiMaterial->append_child($qtiMatText);
00287                                 $qtiMatText = $this->domxml->create_element("mattext");
00288                                 $qtiMatText->set_attribute("label", "test_id");
00289                                 $qtiAppletParams = $this->domxml->create_text_node($test_output);
00290                                 $qtiMatText->append_child($qtiAppletParams);
00291                                 $qtiMaterial->append_child($qtiMatText);
00292                                 $qtiMatText = $this->domxml->create_element("mattext");
00293                                 $qtiMatText->set_attribute("label", "question_id");
00294                                 $qtiAppletParams = $this->domxml->create_text_node($this->getId());
00295                                 $qtiMatText->append_child($qtiAppletParams);
00296                                 $qtiMaterial->append_child($qtiMatText);
00297                                 $qtiMatText = $this->domxml->create_element("mattext");
00298                                 $qtiMatText->set_attribute("label", "user_id");
00299                                 global $ilUser;
00300                                 $qtiAppletParams = $this->domxml->create_text_node($ilUser->id);
00301                                 $qtiMatText->append_child($qtiAppletParams);
00302                                 $qtiMaterial->append_child($qtiMatText);
00303                                 $qtiMatText = $this->domxml->create_element("mattext");
00304                                 $qtiMatText->set_attribute("label", "points_max");
00305                                 $qtiAppletParams = $this->domxml->create_text_node($this->getPoints());
00306                                 $qtiMatText->append_child($qtiAppletParams);
00307                                 $qtiMaterial->append_child($qtiMatText);
00308                                 $qtiMatText = $this->domxml->create_element("mattext");
00309                                 $qtiMatText->set_attribute("label", "post_url");
00310                                 $qtiAppletParams = $this->domxml->create_text_node(ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) . "/assessment/save_java_question_result.php");
00311                                 $qtiMatText->append_child($qtiAppletParams);
00312                                 $qtiMaterial->append_child($qtiMatText);
00313                                 $info = $this->getReachedInformation($ilUser->id, $test_output);
00314                                 foreach ($info as $kk => $infodata)
00315                                 {
00316                                         $qtiMatText->append_child($qtiAppletParams);
00317                                         $qtiMaterial->append_child($qtiMatText);
00318                                         $qtiMatText = $this->domxml->create_element("mattext");
00319                                         $qtiMatText->set_attribute("label", "value_" . $infodata["order"] . "_1");
00320                                         $qtiAppletParams = $this->domxml->create_text_node($infodata["value1"]);
00321                                         $qtiMatText->append_child($qtiAppletParams);
00322                                         $qtiMaterial->append_child($qtiMatText);
00323                                         $qtiMatText->append_child($qtiAppletParams);
00324                                         $qtiMaterial->append_child($qtiMatText);
00325                                         $qtiMatText = $this->domxml->create_element("mattext");
00326                                         $qtiMatText->set_attribute("label", "value_" . $infodata["order"] . "_2");
00327                                         $qtiAppletParams = $this->domxml->create_text_node($infodata["value2"]);
00328                                         $qtiMatText->append_child($qtiAppletParams);
00329                                         $qtiMaterial->append_child($qtiMatText);
00330                                 }
00331                         }
00332                 }
00333 
00334                 $qtiFlow->append_child($qtiMaterial);
00335 
00336                 // add available points as material
00337                 $qtiMaterial = $this->domxml->create_element("material");
00338                 $qtiMatText = $this->domxml->create_element("mattext");
00339                 $qtiMatText->set_attribute("label", "points");
00340                 $qtiMatTextText = $this->domxml->create_text_node($this->getPoints());
00341                 $qtiMatText->append_child($qtiMatTextText);
00342                 $qtiMaterial->append_child($qtiMatText);
00343                 $qtiFlow->append_child($qtiMaterial);
00344 
00345                 $qtiPresentation->append_child($qtiFlow);
00346                 $qtiIdent->append_child($qtiPresentation);
00347 
00348                 $xml = $this->domxml->dump_mem(true);
00349                 if (!$a_include_header)
00350                 {
00351                         $pos = strpos($xml, "?>");
00352                         $xml = substr($xml, $pos + 2);
00353                 }
00354 //echo htmlentities($xml);
00355                 return $xml;
00356 
00357         }
00358 
00368         function from_xml($xml_text)
00369         {
00370                 $result = false;
00371                 if (!empty($this->domxml))
00372                 {
00373                         $this->domxml->free();
00374                 }
00375                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00376                 $this->domxml = domxml_open_mem($xml_text);
00377                 if (!empty($this->domxml))
00378                 {
00379                         $root = $this->domxml->document_element();
00380                         $item = $root->first_child();
00381                         $this->setTitle($item->get_attribute("title"));
00382                         $this->gaps = array();
00383                         $itemnodes = $item->child_nodes();
00384                         $materials = array();
00385                         $images = array();
00386                         $shuffle = "";
00387                         foreach ($itemnodes as $index => $node)
00388                         {
00389                                 switch ($node->node_name())
00390                                 {
00391                                         case "qticomment":
00392                                                 $comment = $node->get_content();
00393                                                 if (strpos($comment, "ILIAS Version=") !== false)
00394                                                 {
00395                                                 }
00396                                                 elseif (strpos($comment, "Questiontype=") !== false)
00397                                                 {
00398                                                 }
00399                                                 elseif (strpos($comment, "Author=") !== false)
00400                                                 {
00401                                                         $comment = str_replace("Author=", "", $comment);
00402                                                         $this->setAuthor($comment);
00403                                                 }
00404                                                 else
00405                                                 {
00406                                                         $this->setComment($comment);
00407                                                 }
00408                                                 break;
00409                                         case "itemmetadata":
00410                                                 $md_array = array();
00411                                                 $metanodes = $node->child_nodes();
00412                                                 foreach ($metanodes as $metanode)
00413                                                 {
00414                                                         switch ($metanode->node_name())
00415                                                         {
00416                                                                 case "qtimetadata":
00417                                                                         $metafields = $metanode->child_nodes();
00418                                                                         foreach ($metafields as $metafield)
00419                                                                         {
00420                                                                                 switch ($metafield->node_name())
00421                                                                                 {
00422                                                                                         case "qtimetadatafield":
00423                                                                                                 $metafieldlist = $metafield->child_nodes();
00424                                                                                                 $md = array("label" => "", "entry" => "");
00425                                                                                                 foreach ($metafieldlist as $attr)
00426                                                                                                 {
00427                                                                                                         switch ($attr->node_name())
00428                                                                                                         {
00429                                                                                                                 case "fieldlabel":
00430                                                                                                                         $md["label"] = $attr->get_content();
00431                                                                                                                         break;
00432                                                                                                                 case "fieldentry":
00433                                                                                                                         $md["entry"] = $attr->get_content();
00434                                                                                                                         break;
00435                                                                                                         }
00436                                                                                                 }
00437                                                                                                 array_push($md_array, $md);
00438                                                                                                 break;
00439                                                                                 }
00440                                                                         }
00441                                                                         break;
00442                                                         }
00443                                                 }
00444                                                 foreach ($md_array as $md)
00445                                                 {
00446                                                         switch ($md["label"])
00447                                                         {
00448                                                                 case "ILIAS_VERSION":
00449                                                                         break;
00450                                                                 case "QUESTIONTYPE":
00451                                                                         break;
00452                                                                 case "AUTHOR":
00453                                                                         $this->setAuthor($md["entry"]);
00454                                                                         break;
00455                                                         }
00456                                                 }
00457                                                 break;
00458                                         case "duration":
00459                                                 $iso8601period = $node->get_content();
00460                                                 if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
00461                                                 {
00462                                                         $this->setEstimatedWorkingTime($matches[4], $matches[5], $matches[6]);
00463                                                 }
00464                                                 break;
00465                                         case "presentation":
00466                                                 $flow = $node->first_child();
00467                                                 $flownodes = $flow->child_nodes();
00468                                                 foreach ($flownodes as $idx => $flownode)
00469                                                 {
00470                                                         if (strcmp($flownode->node_name(), "material") == 0)
00471                                                         {
00472                                                                 $childnodes = $flownode->child_nodes();
00473                                                                 foreach ($childnodes as $childnodeindex => $childnode)
00474                                                                 {
00475                                                                         if (strcmp($childnode->get_attribute("label"), "suggested_solution") == 0)
00476                                                                         {
00477                                                                                 $mattype = $childnode->first_child();
00478                                                                                 if (strcmp($mattype->node_name(), "mattext") == 0)
00479                                                                                 {
00480                                                                                         $suggested_solution = $mattype->get_content();
00481                                                                                         if ($suggested_solution)
00482                                                                                         {
00483                                                                                                 if ($this->getId() < 1)
00484                                                                                                 {
00485                                                                                                         $this->saveToDb();
00486                                                                                                 }
00487                                                                                                 $this->setSuggestedSolution($suggested_solution, 0, true);
00488                                                                                         }
00489                                                                                 }
00490                                                                         }
00491                                                                         elseif (strcmp($childnode->node_name(), "mattext") == 0)
00492                                                                         {
00493                                                                                 if (!$childnode->has_attribute("label"))
00494                                                                                 {
00495                                                                                         $this->setQuestion($childnode->get_content());
00496                                                                                 }
00497                                                                                 elseif (strcmp($childnode->get_attribute("label"), "points") == 0)
00498                                                                                 {
00499                                                                                         $this->setPoints($childnode->get_content());
00500                                                                                 }
00501                                                                                 elseif (strcmp($childnode->get_attribute("label"), "java_code") == 0)
00502                                                                                 {
00503                                                                                         $this->java_code = $childnode->get_content();
00504                                                                                 }
00505                                                                                 elseif (strcmp($childnode->get_attribute("label"), "") != 0)
00506                                                                                 {
00507                                                                                         $this->addParameter($childnode->get_attribute("label"), $childnode->get_content());
00508                                                                                 }
00509                                                                         }
00510                                                                         elseif (strcmp($childnode->node_name(), "matapplet") == 0)
00511                                                                         {
00512                                                                                 if (strcmp($childnode->get_attribute("label"), "applet data") == 0)
00513                                                                                 {
00514                                                                                         $this->javaapplet_filename = $childnode->get_attribute("uri");
00515                                                                                         $this->java_height = $childnode->get_attribute("height");
00516                                                                                         $this->java_width = $childnode->get_attribute("width");
00517                                                                                         $java = base64_decode($childnode->get_content());
00518                                                                                 }
00519                                                                         }
00520                                                                 }
00521                                                         }
00522                                                 }
00523                                                 break;
00524                                 }
00525                         }
00526                         if ($this->javaapplet_filename)
00527                         {
00528                                 $this->saveToDb();
00529                                 $javapath = $this->getJavaPath();
00530                                 if (!file_exists($javapath))
00531                                 {
00532                                         ilUtil::makeDirParents($javapath);
00533                                 }
00534                                 $javapath .=  $this->javaapplet_filename;
00535                                 $fh = fopen($javapath, "wb");
00536                                 if ($fh == false)
00537                                 {
00538                                         global $ilErr;
00539                                         $ilErr->raiseError($this->lng->txt("error_save_java_file") . ": $php_errormsg", $ilErr->MESSAGE);
00540                                         return;
00541                                 }
00542                                 $javafile = fwrite($fh, $java);
00543                                 fclose($fh);
00544                         }
00545                         $result = true;
00546                 }
00547                 return $result;
00548         }
00549 
00550 
00559         function splitParams($params = "")
00560         {
00561                 $params_array = split("<separator>", $params);
00562                 foreach ($params_array as $pair)
00563                 {
00564                         if (preg_match("/(.*?)\=(.*)/", $pair, $matches))
00565                         {
00566                                 switch ($matches[1])
00567                                 {
00568                                         case "java_code" :
00569                                                 $this->java_code = $matches[2];
00570                                                 break;
00571                                         case "java_width" :
00572                                                 $this->java_width = $matches[2];
00573                                                 break;
00574                                         case "java_height" :
00575                                                 $this->java_height = $matches[2];
00576                                                 break;
00577                                 }
00578                                 if (preg_match("/param_name_(\d+)/", $matches[1], $found_key))
00579                                 {
00580                                         $this->parameters[$found_key[1]]["name"] = $matches[2];
00581                                 }
00582                                 if (preg_match("/param_value_(\d+)/", $matches[1], $found_key))
00583                                 {
00584                                         $this->parameters[$found_key[1]]["value"] = $matches[2];
00585                                 }
00586                         }
00587                 }
00588         }
00589 
00598         function buildParams()
00599         {
00600                 $params_array = array();
00601                 if ($this->java_code)
00602                 {
00603                         array_push($params_array, "java_code=$this->java_code");
00604                 }
00605                 if ($this->java_width)
00606                 {
00607                         array_push($params_array, "java_width=$this->java_width");
00608                 }
00609                 if ($this->java_height)
00610                 {
00611                         array_push($params_array, "java_height=$this->java_height");
00612                 }
00613                 foreach ($this->parameters as $key => $value)
00614                 {
00615                         array_push($params_array, "param_name_$key=" . $value["name"]);
00616                         array_push($params_array, "param_value_$key=" . $value["value"]);
00617                 }
00618                 return join($params_array, "<separator>");
00619         }
00620 
00629         function buildParamsOnly()
00630         {
00631                 $params_array = array();
00632                 if ($this->java_code)
00633                 {
00634                         array_push($params_array, "java_code=$this->java_code");
00635                 }
00636                 foreach ($this->parameters as $key => $value)
00637                 {
00638                         array_push($params_array, "param_name_$key=" . $value["name"]);
00639                         array_push($params_array, "param_value_$key=" . $value["value"]);
00640                 }
00641                 return join($params_array, "<separator>");
00642         }
00643 
00652         function isComplete()
00653         {
00654                 if (($this->title) and ($this->author) and ($this->question) and ($this->javaapplet_filename) and ($this->java_width) and ($this->java_height) and ($this->points != ""))
00655                 {
00656                         return true;
00657                 }
00658                         else
00659                 {
00660                         return false;
00661                 }
00662         }
00663 
00664 
00673         function saveToDb($original_id = "")
00674         {
00675                 global $ilias;
00676 
00677                 $complete = 0;
00678                 if ($this->isComplete())
00679                 {
00680                         $complete = 1;
00681                 }
00682 
00683                 $db = & $ilias->db;
00684 
00685                 $params = $this->buildParams();
00686                 $estw_time = $this->getEstimatedWorkingTime();
00687                 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
00688 
00689                 if ($original_id)
00690                 {
00691                         $original_id = $db->quote($original_id);
00692                 }
00693                 else
00694                 {
00695                         $original_id = "NULL";
00696                 }
00697 
00698                 if ($this->id == -1)
00699                 {
00700                         // Neuen Datensatz schreiben
00701                         $now = getdate();
00702                         $question_type = $this->getQuestionType();
00703                         $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00704                         $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, points, working_time, shuffle, complete, image_file, params, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00705                                 $db->quote($question_type . ""),
00706                                 $db->quote($this->obj_id . ""),
00707                                 $db->quote($this->title . ""),
00708                                 $db->quote($this->comment . ""),
00709                                 $db->quote($this->author . ""),
00710                                 $db->quote($this->owner . ""),
00711                                 $db->quote($this->question . ""),
00712                                 $db->quote($this->points . ""),
00713                                 $db->quote($estw_time . ""),
00714                                 $db->quote($this->shuffle . ""),
00715                                 $db->quote($complete . ""),
00716                                 $db->quote($this->javaapplet_filename . ""),
00717                                 $db->quote($params . ""),
00718                                 $db->quote($created . ""),
00719                                 $original_id
00720                         );
00721 
00722                         $result = $db->query($query);
00723                         if ($result == DB_OK)
00724                         {
00725                                 $this->id = $this->ilias->db->getLastInsertId();
00726 
00727                                 // create page object of question
00728                                 $this->createPageObject();
00729 
00730                                 // Falls die Frage in einen Test eingef�gt werden soll, auch diese Verbindung erstellen
00731                                 if ($this->getTestId() > 0)
00732                                 {
00733                                         $this->insertIntoTest($this->getTestId());
00734                                 }
00735                         }
00736                 }
00737                 else
00738                 {
00739                         // Vorhandenen Datensatz aktualisieren
00740                         $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, points = %s, working_time=%s, shuffle = %s, complete = %s, image_file = %s, params = %s WHERE question_id = %s",
00741                                 $db->quote($this->obj_id. ""),
00742                                 $db->quote($this->title . ""),
00743                                 $db->quote($this->comment . ""),
00744                                 $db->quote($this->author . ""),
00745                                 $db->quote($this->question . ""),
00746                                 $db->quote($this->points . ""),
00747                                 $db->quote($estw_time . ""),
00748                                 $db->quote($this->shuffle . ""),
00749                                 $db->quote($complete . ""),
00750                                 $db->quote($this->javaapplet_filename . ""),
00751                                 $db->quote($params . ""),
00752                                 $db->quote($this->id . "")
00753                         );
00754                         $result = $db->query($query);
00755                 }
00756                 parent::saveToDb($original_id);
00757         }
00758 
00768         function loadFromDb($question_id)
00769         {
00770                 global $ilias;
00771 
00772                 $db = & $ilias->db;
00773                 $query = sprintf("SELECT * FROM qpl_questions WHERE question_id = %s",
00774                         $db->quote($question_id)
00775                 );
00776                 $result = $db->query($query);
00777 
00778                 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00779                 {
00780                         if ($result->numRows() == 1)
00781                         {
00782                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00783                                 $this->id = $question_id;
00784                                 $this->title = $data->title;
00785                                 $this->comment = $data->comment;
00786                                 $this->obj_id = $data->obj_fi;
00787                                 $this->author = $data->author;
00788                                 $this->points = $data->points;
00789                                 $this->owner = $data->owner;
00790                                 $this->original_id = $data->original_id;
00791                                 $this->javaapplet_filename = $data->image_file;
00792                                 $this->question = $data->question_text;
00793                                 $this->solution_hint = $data->solution_hint;
00794                                 $this->splitParams($data->params);
00795                                 $this->setShuffle($data->shuffle);
00796                                 $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
00797                         }
00798                 }
00799                 parent::loadFromDb($question_id);
00800         }
00801 
00809         function duplicate($for_test = true, $title = "", $author = "", $owner = "")
00810         {
00811                 if ($this->id <= 0)
00812                 {
00813                         // The question has not been saved. It cannot be duplicated
00814                         return;
00815                 }
00816                 // duplicate the question in database
00817                 $clone = $this;
00818                 include_once ("./assessment/classes/class.assQuestion.php");
00819                 $original_id = ASS_Question::_getOriginalId($this->id);
00820                 $clone->id = -1;
00821                 if ($title)
00822                 {
00823                         $clone->setTitle($title);
00824                 }
00825                 if ($author)
00826                 {
00827                         $clone->setAuthor($author);
00828                 }
00829                 if ($owner)
00830                 {
00831                         $clone->setOwner($owner);
00832                 }
00833                 if ($for_test)
00834                 {
00835                         $clone->saveToDb($original_id);
00836                 }
00837                 else
00838                 {
00839                         $clone->saveToDb();
00840                 }
00841 
00842                 // copy question page content
00843                 $clone->copyPageOfQuestion($original_id);
00844 
00845                 // duplicate the image
00846                 $clone->duplicateApplet($original_id);
00847                 return $clone->id;
00848         }
00849 
00850         function duplicateApplet($question_id)
00851         {
00852                 $javapath = $this->getJavaPath();
00853                 $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
00854                 if (!file_exists($javapath))
00855                 {
00856                         ilUtil::makeDirParents($javapath);
00857                 }
00858                 $filename = $this->getJavaAppletFilename();
00859                 if (!copy($javapath_original . $filename, $javapath . $filename)) {
00860                         print "java applet could not be duplicated!!!! ";
00861                 }
00862         }
00863 
00873         function getQuestion()
00874         {
00875                 return $this->question;
00876         }
00877 
00887         function setQuestion($question = "")
00888         {
00889                 $this->question = $question;
00890         }
00891 
00900         function getMaximumPoints()
00901         {
00902                 return $this->points;
00903         }
00904 
00913         function getJavaCode()
00914         {
00915                 return $this->java_code;
00916         }
00917 
00926         function setJavaCode($java_code = "")
00927         {
00928                 $this->java_code = $java_code;
00929         }
00930 
00939         function getJavaWidth()
00940         {
00941                 return $this->java_width;
00942         }
00943 
00952         function setJavaWidth($java_width = "")
00953         {
00954                 $this->java_width = $java_width;
00955         }
00956 
00965         function getJavaHeight()
00966         {
00967                 return $this->java_height;
00968         }
00969 
00978         function setJavaHeight($java_height = "")
00979         {
00980                 $this->java_height = $java_height;
00981         }
00982 
00994         function calculateReachedPoints($user_id, $test_id)
00995         {
00996                 global $ilDB;
00997                 
00998                 $found_values = array();
00999                 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01000                         $ilDB->quote($user_id),
01001                         $ilDB->quote($test_id),
01002                         $ilDB->quote($this->getId())
01003                 );
01004                 $result = $ilDB->query($query);
01005                 $points = 0;
01006                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01007                 {
01008                         $points += $data->points;
01009                 }
01010 
01011                 // check for special scoring options in test
01012                 $query = sprintf("SELECT * FROM tst_tests WHERE test_id = %s",
01013                         $ilDB->quote($test_id)
01014                 );
01015                 $result = $ilDB->query($query);
01016                 if ($result->numRows() == 1)
01017                 {
01018                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01019                         if ($row["count_system"] == 1)
01020                         {
01021                                 if ($points != $this->getMaximumPoints())
01022                                 {
01023                                         $points = 0;
01024                                 }
01025                         }
01026                 }
01027                 else
01028                 {
01029                         $points = 0;
01030                 }
01031                 return $points;
01032         }
01033 
01043         function getReachedInformation($user_id, $test_id)
01044         {
01045                 $found_values = array();
01046                 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01047                         $this->ilias->db->quote($user_id),
01048                         $this->ilias->db->quote($test_id),
01049                         $this->ilias->db->quote($this->getId())
01050                 );
01051                 $result = $this->ilias->db->query($query);
01052                 $counter = 1;
01053                 $user_result = array();
01054                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01055                 {
01056                         $true = 0;
01057                         if ($data->points > 0)
01058                         {
01059                                 $true = 1;
01060                         }
01061                         $solution = array(
01062                                 "order" => "$counter",
01063                                 "points" => "$data->points",
01064                                 "true" => "$true",
01065                                 "value1" => "$data->value1",
01066                                 "value2" => "$data->value2",
01067                         );
01068                         $counter++;
01069                         array_push($user_result, $solution);
01070                 }
01071                 return $user_result;
01072         }
01073 
01084         function addParameter($name = "", $value = "")
01085         {
01086                 $index = $this->getParameterIndex($name);
01087                 if ($index > -1)
01088                 {
01089                         $this->parameters[$index] = array("name" => $name, "value" => $value);
01090                 }
01091                 else
01092                 {
01093                         array_push($this->parameters, array("name" => $name, "value" => $value));
01094                 }
01095         }
01096 
01108         function addParameterAtIndex($index = 0, $name = "", $value = "")
01109         {
01110                 $this->parameters[$index] = array("name" => $name, "value" => $value);
01111         }
01112 
01122         function removeParameter($name)
01123         {
01124                 foreach ($this->parameters as $key => $value)
01125                 {
01126                         if (strcmp($name, $value["name"]) == 0)
01127                         {
01128                                 array_splice($this->parameters, $key, 1);
01129                                 return;
01130                         }
01131                 }
01132         }
01133 
01144         function getParameter($index)
01145         {
01146                 if (($index < 0) or ($index >= count($this->parameters)))
01147                 {
01148                         return undef;
01149                 }
01150                 return $this->parameters[$index];
01151         }
01152 
01163         function getParameterIndex($name)
01164         {
01165                 foreach ($this->parameters as $key => $value)
01166                 {
01167                         if (array_key_exists($name, $value))
01168                         {
01169                                 return $key;
01170                         }
01171                 }
01172                 return -1;
01173         }
01174 
01184         function getParameterCount()
01185         {
01186                 return count($this->parameters);
01187         }
01188 
01197         function flushParams()
01198         {
01199                 $this->parameters = array();
01200         }
01201 
01212         function saveWorkingData($test_id, $limit_to = LIMIT_NO_LIMIT)
01213         {
01214     parent::saveWorkingData($test_id);
01215                 return true;
01216                 /*    global $ilDB;
01217                         global $ilUser;
01218             $db =& $ilDB->db;
01219 
01220         if ($this->response == RESPONSE_SINGLE) {
01221                         $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01222                                 $db->quote($ilUser->id),
01223                                 $db->quote($test_id),
01224                                 $db->quote($this->getId())
01225                         );
01226                         $result = $db->query($query);
01227                         $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
01228                         $update = $row->solution_id;
01229                         if ($update) {
01230                                 $query = sprintf("UPDATE tst_solutions SET value1 = %s WHERE solution_id = %s",
01231                                         $db->quote($_POST["multiple_choice_result"]),
01232                                         $db->quote($update)
01233                                 );
01234                         } else {
01235                                 $query = sprintf("INSERT INTO tst_solutions (solution_id, user_fi, test_fi, question_fi, value1, value2, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL, NULL)",
01236                                         $db->quote($ilUser->id),
01237                                         $db->quote($test_id),
01238                                         $db->quote($this->getId()),
01239                                         $db->quote($_POST["multiple_choice_result"])
01240                                 );
01241                         }
01242       $result = $db->query($query);
01243     } else {
01244                         $query = sprintf("DELETE FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01245                                 $db->quote($ilUser->id),
01246                                 $db->quote($test_id),
01247                                 $db->quote($this->getId())
01248                         );
01249                         $result = $db->query($query);
01250       foreach ($_POST as $key => $value) {
01251         if (preg_match("/multiple_choice_result_(\d+)/", $key, $matches)) {
01252                                         $query = sprintf("INSERT INTO tst_solutions (solution_id, user_fi, test_fi, question_fi, value1, value2, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL, NULL)",
01253                                                 $db->quote($ilUser->id),
01254                                                 $db->quote($test_id),
01255                                                 $db->quote($this->getId()),
01256                                                 $db->quote($value)
01257                                         );
01258           $result = $db->query($query);
01259         }
01260       }
01261     }
01262     //parent::saveWorkingData($limit_to);
01263                 return true;
01264 */  }
01265 
01275         function getJavaAppletFilename()
01276         {
01277                 return $this->javaapplet_filename;
01278         }
01279 
01289         function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
01290         {
01291                 if (!empty($javaapplet_filename))
01292                 {
01293                         $this->javaapplet_filename = $javaapplet_filename;
01294                 }
01295                 if (!empty($javaapplet_tempfilename))
01296                 {
01297                         $javapath = $this->getJavaPath();
01298                         if (!file_exists($javapath))
01299                         {
01300                                 ilUtil::makeDirParents($javapath);
01301                         }
01302                         
01303                         //if (!move_uploaded_file($javaapplet_tempfilename, $javapath . $javaapplet_filename))
01304                         if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
01305                         {
01306                                 print "java applet not uploaded!!!! ";
01307                         }
01308                 }
01309         }
01310 
01311         function syncWithOriginal()
01312         {
01313                 global $ilias;
01314                 if ($this->original_id)
01315                 {
01316                         $complete = 0;
01317                         if ($this->isComplete())
01318                         {
01319                                 $complete = 1;
01320                         }
01321                         $db = & $ilias->db;
01322         
01323                         $estw_time = $this->getEstimatedWorkingTime();
01324                         $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
01325         
01326                         $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, points = %s, working_time=%s, shuffle = %s, complete = %s, image_file = %s, params = %s WHERE question_id = %s",
01327                                 $db->quote($this->obj_id. ""),
01328                                 $db->quote($this->title . ""),
01329                                 $db->quote($this->comment . ""),
01330                                 $db->quote($this->author . ""),
01331                                 $db->quote($this->question . ""),
01332                                 $db->quote($this->points . ""),
01333                                 $db->quote($estw_time . ""),
01334                                 $db->quote($this->shuffle . ""),
01335                                 $db->quote($complete . ""),
01336                                 $db->quote($this->javaapplet_filename . ""),
01337                                 $db->quote($params . ""),
01338                                 $db->quote($this->original_id . "")
01339                         );
01340                         $result = $db->query($query);
01341 
01342                         parent::syncWithOriginal();
01343                 }
01344         }
01345 
01354         function getQuestionType()
01355         {
01356                 return 7;
01357         }
01358 }
01359 
01360 ?>

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