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

assessment/classes/class.assImagemapQuestion.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.assQuestion.php";
00025 require_once "./assessment/classes/class.assAnswerImagemap.php";
00026 
00027 define ("IMAGEMAP_QUESTION_IDENTIFIER", "IMAGE MAP QUESTION");
00028 
00039 class ASS_ImagemapQuestion extends ASS_Question {
00040 
00048   var $question;
00049 
00057   var $answers;
00058 
00068   var $points;
00069 
00077   var $imagemap_filename;
00078 
00086   var $image_filename;
00087 
00095   var $imagemap_contents;
00096         var $coords;
00097 
00112   function ASS_ImagemapQuestion(
00113     $title = "",
00114     $comment = "",
00115     $author = "",
00116     $owner = -1,
00117     $question = "",
00118     $imagemap_filename = "",
00119     $image_filename = ""
00120 
00121   )
00122   {
00123     $this->ASS_Question($title, $comment, $author, $owner);
00124     $this->question = $question;
00125     $this->imagemap_filename = $imagemap_filename;
00126     $this->image_filename = $image_filename;
00127     $this->answers = array();
00128                 $this->points = $points;
00129                 $this->coords = array();
00130   }
00131 
00140         function isComplete()
00141         {
00142                 if (($this->title) and ($this->author) and ($this->question) and ($this->image_filename) and (count($this->answers)))
00143                 {
00144                         return true;
00145                 }
00146                         else
00147                 {
00148                         return false;
00149                 }
00150         }
00151 
00160         function saveToDb($original_id = "")
00161         {
00162                 global $ilias;
00163 
00164                 $complete = 0;
00165                 if ($this->isComplete())
00166                 {
00167                         $complete = 1;
00168                 }
00169 
00170                 $db = & $ilias->db;
00171 
00172                 $estw_time = $this->getEstimatedWorkingTime();
00173                 $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
00174                 if ($original_id)
00175                 {
00176                         $original_id = $db->quote($original_id);
00177                 }
00178                 else
00179                 {
00180                         $original_id = "NULL";
00181                 }
00182 
00183                 if ($this->id == -1)
00184                 {
00185                         // Neuen Datensatz schreiben
00186                         $now = getdate();
00187                         $question_type = $this->getQuestionType();
00188                         $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00189                         $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, working_time, points, image_file, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00190                                 $db->quote($question_type),
00191                                 $db->quote($this->obj_id),
00192                                 $db->quote($this->title),
00193                                 $db->quote($this->comment),
00194                                 $db->quote($this->author),
00195                                 $db->quote($this->owner),
00196                                 $db->quote($this->question),
00197                                 $db->quote($estw_time),
00198                                 $db->quote($this->points),
00199                                 $db->quote($this->image_filename),
00200                                 $db->quote("$complete"),
00201                                 $db->quote($created),
00202                                 $original_id
00203                         );
00204                         $result = $db->query($query);
00205                         if ($result == DB_OK)
00206                         {
00207                                 $this->id = $db->getLastInsertId();
00208 
00209                                 // create page object of question
00210                                 $this->createPageObject();
00211 
00212                                 // Falls die Frage in einen Test eingef�gt werden soll, auch diese Verbindung erstellen
00213                                 if ($this->getTestId() > 0)
00214                                 {
00215                                         $this->insertIntoTest($this->getTestId());
00216                                 }
00217                         }
00218                 }
00219                 else
00220                 {
00221                         // Vorhandenen Datensatz aktualisieren
00222                         $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, working_time = %s, points = %s, image_file = %s, complete = %s WHERE question_id = %s",
00223                                 $db->quote($this->obj_id. ""),
00224                                 $db->quote($this->title),
00225                                 $db->quote($this->comment),
00226                                 $db->quote($this->author),
00227                                 $db->quote($this->question),
00228                                 $db->quote($estw_time),
00229                                 $db->quote($this->points),
00230                                 $db->quote($this->image_filename),
00231                                 $db->quote("$complete"),
00232                                 $db->quote($this->id)
00233                         );
00234                         $result = $db->query($query);
00235                 }
00236 
00237                 if ($result == DB_OK)
00238                 {
00239                         $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
00240                                 $db->quote($this->id)
00241                         );
00242                         $result = $db->query($query);
00243                         // Anworten wegschreiben
00244                         foreach ($this->answers as $key => $value)
00245                         {
00246                                 $answer_obj = $this->answers[$key];
00247                                 //print "id:".$this->id." answer tex:".$answer_obj->get_answertext()." answer_obj->get_order():".$answer_obj->get_order()." answer_obj->get_coords():".$answer_obj->get_coords()." answer_obj->get_area():".$answer_obj->get_area();
00248                                 $query = sprintf("INSERT INTO qpl_answers (answer_id, question_fi, answertext, points, aorder, correctness, coords, area, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, NULL)",
00249                                         $db->quote($this->id),
00250                                         $db->quote($answer_obj->get_answertext() . ""),
00251                                         $db->quote($answer_obj->get_points() . ""),
00252                                         $db->quote($answer_obj->get_order() . ""),
00253                                         $db->quote($answer_obj->getState() . ""),
00254                                         $db->quote($answer_obj->get_coords() . ""),
00255                                         $db->quote($answer_obj->get_area() . "")
00256                                         );
00257                                 $answer_result = $db->query($query);
00258                                 }
00259                 }
00260                 parent::saveToDb($original_id);
00261         }
00262 
00270         function duplicate($for_test = true, $title = "", $author = "", $owner = "")
00271         {
00272                 if ($this->id <= 0)
00273                 {
00274                         // The question has not been saved. It cannot be duplicated
00275                         return;
00276                 }
00277                 // duplicate the question in database
00278                 $clone = $this;
00279                 include_once ("./assessment/classes/class.assQuestion.php");
00280                 $original_id = ASS_Question::_getOriginalId($this->id);
00281                 $clone->id = -1;
00282                 if ($title)
00283                 {
00284                         $clone->setTitle($title);
00285                 }
00286                 if ($author)
00287                 {
00288                         $clone->setAuthor($author);
00289                 }
00290                 if ($owner)
00291                 {
00292                         $clone->setOwner($owner);
00293                 }
00294                 if ($for_test)
00295                 {
00296                         $clone->saveToDb($original_id);
00297                 }
00298                 else
00299                 {
00300                         $clone->saveToDb();
00301                 }
00302 
00303                 // copy question page content
00304                 $clone->copyPageOfQuestion($original_id);
00305 
00306                 // duplicate the image
00307                 $clone->duplicateImage($original_id);
00308                 return $clone->id;
00309         }
00310 
00311         function duplicateImage($question_id)
00312         {
00313                 $imagepath = $this->getImagePath();
00314                 $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
00315                 if (!file_exists($imagepath)) {
00316                         ilUtil::makeDirParents($imagepath);
00317                 }
00318                 $filename = $this->get_image_filename();
00319                 if (!copy($imagepath_original . $filename, $imagepath . $filename)) {
00320                         print "image could not be duplicated!!!! ";
00321                 }
00322         }
00323 
00333   function loadFromDb($question_id)
00334   {
00335     global $ilias;
00336 
00337     $db = & $ilias->db;
00338     $query = sprintf("SELECT * FROM qpl_questions WHERE question_id = %s",
00339       $db->quote($question_id)
00340     );
00341     $result = $db->query($query);
00342     if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00343       if ($result->numRows() == 1) {
00344         $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00345         $this->id = $question_id;
00346                                 $this->obj_id = $data->obj_fi;
00347         $this->title = $data->title;
00348         $this->comment = $data->comment;
00349         $this->author = $data->author;
00350                                 $this->original_id = $data->original_id;
00351                                 $this->solution_hint = $data->solution_hint;
00352         $this->owner = $data->owner;
00353         $this->question = $data->question_text;
00354         $this->image_filename = $data->image_file;
00355         $this->points = $data->points;
00356         $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
00357       }
00358       $query = sprintf("SELECT * FROM qpl_answers WHERE question_fi = %s ORDER BY aorder ASC",
00359         $db->quote($question_id)
00360       );
00361       $result = $db->query($query);
00362       if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00363         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00364                                         if ($data->correctness == 0)
00365                                         {
00366                                                 // fix for older single response answers where points could be given for unchecked answers
00367                                                 $data->correctness = 1;
00368                                                 $data->points = 0;
00369                                         }
00370           array_push($this->answers, new ASS_AnswerImagemap($data->answertext, $data->points, $data->aorder, $data->correctness, $data->coords, $data->area));
00371         }
00372       }
00373     }
00374                 parent::loadFromDb($question_id);
00375   }
00376 
00386         function from_xml($xml_text)
00387         {
00388                 $result = false;
00389                 if (!empty($this->domxml))
00390                 {
00391                         $this->domxml->free();
00392                 }
00393                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00394                 $this->domxml = domxml_open_mem($xml_text);
00395                 if (!empty($this->domxml))
00396                 {
00397                         $root = $this->domxml->document_element();
00398                         $item = $root->first_child();
00399                         $this->setTitle($item->get_attribute("title"));
00400                         $this->gaps = array();
00401                         $itemnodes = $item->child_nodes();
00402                         $materials = array();
00403                         $filename = "";
00404                         $image = "";
00405                         $shuffle = "";
00406                         foreach ($itemnodes as $index => $node)
00407                         {
00408                                 switch ($node->node_name())
00409                                 {
00410                                         case "qticomment":
00411                                                 $comment = $node->node_value();
00412                                                 if (strpos($comment, "ILIAS Version=") !== false)
00413                                                 {
00414                                                 }
00415                                                 elseif (strpos($comment, "Questiontype=") !== false)
00416                                                 {
00417                                                 }
00418                                                 elseif (strpos($comment, "Author=") !== false)
00419                                                 {
00420                                                         $comment = str_replace("Author=", "", $comment);
00421                                                         $this->setAuthor($comment);
00422                                                 }
00423                                                 else
00424                                                 {
00425                                                         $this->setComment($comment);
00426                                                 }
00427                                                 break;
00428                                         case "itemmetadata":
00429                                                 $md_array = array();
00430                                                 $metanodes = $node->child_nodes();
00431                                                 foreach ($metanodes as $metanode)
00432                                                 {
00433                                                         switch ($metanode->node_name())
00434                                                         {
00435                                                                 case "qtimetadata":
00436                                                                         $metafields = $metanode->child_nodes();
00437                                                                         foreach ($metafields as $metafield)
00438                                                                         {
00439                                                                                 switch ($metafield->node_name())
00440                                                                                 {
00441                                                                                         case "qtimetadatafield":
00442                                                                                                 $metafieldlist = $metafield->child_nodes();
00443                                                                                                 $md = array("label" => "", "entry" => "");
00444                                                                                                 foreach ($metafieldlist as $attr)
00445                                                                                                 {
00446                                                                                                         switch ($attr->node_name())
00447                                                                                                         {
00448                                                                                                                 case "fieldlabel":
00449                                                                                                                         $md["label"] = $attr->get_content();
00450                                                                                                                         break;
00451                                                                                                                 case "fieldentry":
00452                                                                                                                         $md["entry"] = $attr->get_content();
00453                                                                                                                         break;
00454                                                                                                         }
00455                                                                                                 }
00456                                                                                                 array_push($md_array, $md);
00457                                                                                                 break;
00458                                                                                 }
00459                                                                         }
00460                                                                         break;
00461                                                         }
00462                                                 }
00463                                                 foreach ($md_array as $md)
00464                                                 {
00465                                                         switch ($md["label"])
00466                                                         {
00467                                                                 case "ILIAS_VERSION":
00468                                                                         break;
00469                                                                 case "QUESTIONTYPE":
00470                                                                         break;
00471                                                                 case "AUTHOR":
00472                                                                         $this->setAuthor($md["entry"]);
00473                                                                         break;
00474                                                         }
00475                                                 }
00476                                                 break;
00477                                         case "duration":
00478                                                 $iso8601period = $node->node_value();
00479                                                 if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
00480                                                 {
00481                                                         $this->setEstimatedWorkingTime($matches[4], $matches[5], $matches[6]);
00482                                                 }
00483                                                 break;
00484                                         case "presentation":
00485                                                 $flow = $node->first_child();
00486                                                 $flownodes = $flow->child_nodes();
00487                                                 foreach ($flownodes as $idx => $flownode)
00488                                                 {
00489                                                         if (strcmp($flownode->node_name(), "material") == 0)
00490                                                         {
00491                                                                 $mattext = $flownode->first_child();
00492                                                                 $this->set_question($mattext->node_value());
00493                                                         }
00494                                                         elseif (strcmp($flownode->node_name(), "response_xy") == 0)
00495                                                         {
00496                                                                 $ident = $flownode->get_attribute("ident");
00497                                                                 $subnodes = $flownode->child_nodes();
00498                                                                 foreach ($subnodes as $node_type)
00499                                                                 {
00500                                                                         switch ($node_type->node_name())
00501                                                                         {
00502                                                                                 case "material":
00503                                                                                         $matlabel = $node_type->get_attribute("label");
00504                                                                                         if (strcmp($matlabel, "suggested_solution") == 0)
00505                                                                                         {
00506                                                                                                 $mattype = $node_type->first_child();
00507                                                                                                 if (strcmp($mattype->node_name(), "mattext") == 0)
00508                                                                                                 {
00509                                                                                                         $suggested_solution = $mattype->node_value();
00510                                                                                                         if ($suggested_solution)
00511                                                                                                         {
00512                                                                                                                 if ($this->getId() < 1)
00513                                                                                                                 {
00514                                                                                                                         $this->saveToDb();
00515                                                                                                                 }
00516                                                                                                                 $this->setSuggestedSolution($suggested_solution, 0, true);
00517                                                                                                         }
00518                                                                                                 }
00519                                                                                         }
00520                                                                                         break;
00521                                                                                 case "render_hotspot":
00522                                                                                         $render_hotspot = $node_type;
00523                                                                                         $labels = $render_hotspot->child_nodes();
00524                                                                                         foreach ($labels as $lidx => $response_label)
00525                                                                                         {
00526                                                                                                 if (strcmp($response_label->node_name(), "material") == 0)
00527                                                                                                 {
00528                                                                                                         // image map image
00529                                                                                                         $mattype = $response_label->first_child();
00530                                                                                                         if (strcmp($mattype->node_name(), "matimage") == 0)
00531                                                                                                         {
00532                                                                                                                 $filename = $mattype->get_attribute("label");
00533                                                                                                                 $image = base64_decode($mattype->node_value());
00534                                                                                                         }
00535                                                                                                 }
00536                                                                                                 else
00537                                                                                                 {
00538                                                                                                         $matident = $response_label->get_attribute("ident");
00539                                                                                                         switch ($response_label->get_attribute("rarea"))
00540                                                                                                         {
00541                                                                                                                 case "Ellipse":
00542                                                                                                                         $materials[$matident]["area"] = "circle";
00543                                                                                                                         break;
00544                                                                                                                 case "Bounded":
00545                                                                                                                         $materials[$matident]["area"] = "poly";
00546                                                                                                                         break;
00547                                                                                                                 case "Rectangle":
00548                                                                                                                         $materials[$matident]["area"] = "rect";
00549                                                                                                                         break;
00550                                                                                                         }
00551                                                                                                         $material_children = $response_label->child_nodes();
00552                                                                                                         foreach ($material_children as $midx => $childnode)
00553                                                                                                         {
00554                                                                                                                 if (strcmp($childnode->node_name(), "#text") == 0)
00555                                                                                                                 {
00556                                                                                                                         $materials[$matident]["coords"] = $childnode->node_value();
00557                                                                                                                 }
00558                                                                                                                 elseif (strcmp($childnode->node_name(), "material") == 0)
00559                                                                                                                 {
00560                                                                                                                         $materials[$matident]["answertext"] = $childnode->node_value();
00561                                                                                                                 }
00562                                                                                                         }
00563                                                                                                 }
00564                                                                                         }
00565                                                                                         break;
00566                                                                         }
00567                                                                 }
00568                                                         }
00569                                                 }
00570                                                 break;
00571                                         case "resprocessing":
00572                                                 $resproc_nodes = $node->child_nodes();
00573                                                 foreach ($resproc_nodes as $index => $respcondition)
00574                                                 {
00575                                                         if (strcmp($respcondition->node_name(), "respcondition") == 0)
00576                                                         {
00577                                                                 $respcondition_array =& ilQTIUtils::_getRespcondition($respcondition);
00578                                                                 foreach ($materials as $index => $material)
00579                                                                 {
00580                                                                         if (strcmp($material["coords"], $respcondition_array["conditionvar"]["value"]) == 0)
00581                                                                         {
00582                                                                                 $this->add_answer(
00583                                                                                         $material["answertext"], 
00584                                                                                         $respcondition_array["setvar"]["points"],
00585                                                                                         $respcondition_array["conditionvar"]["selected"],
00586                                                                                         $index,
00587                                                                                         $material["coords"],
00588                                                                                         $material["area"]
00589                                                                                 );
00590                                                                         }
00591                                                                 }
00592                                                         }
00593                                                 }
00594                                                 break;
00595                                 }
00596                         }
00597                         if ($filename)
00598                         {
00599                                 $this->saveToDb();
00600                                 $imagepath = $this->getImagePath();
00601                                 if (!file_exists($imagepath))
00602                                 {
00603                                         ilUtil::makeDirParents($imagepath);
00604                                 }
00605                                 $imagepath .=  $filename;
00606                                 $fh = fopen($imagepath, "wb");
00607                                 if ($fh == false)
00608                                 {
00609                                         global $ilErr;
00610                                         $ilErr->raiseError($this->lng->txt("error_save_image_file") . ": $php_errormsg", $ilErr->MESSAGE);
00611                                         return;
00612                                 }
00613                                 $imagefile = fwrite($fh, $image);
00614                                 fclose($fh);
00615                                 $this->image_filename = $filename;
00616                         }
00617                         $result = true;
00618                 }
00619                 return $result;
00620         }
00621 
00631         function to_xml($a_include_header = true, $a_include_binary = true, $a_shuffle = false, $test_output = false)
00632         {
00633                 if (!empty($this->domxml))
00634                 {
00635                         $this->domxml->free();
00636                 }
00637                 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<questestinterop></questestinterop>\n";
00638                 $this->domxml = domxml_open_mem($xml_header);
00639                 $root = $this->domxml->document_element();
00640                 // qti ident
00641                 $qtiIdent = $this->domxml->create_element("item");
00642                 $qtiIdent->set_attribute("ident", "il_".IL_INST_ID."_qst_".$this->getId());
00643                 $qtiIdent->set_attribute("title", $this->getTitle());
00644                 $root->append_child($qtiIdent);
00645         // add question description
00646                 $qtiComment = $this->domxml->create_element("qticomment");
00647                 $qtiCommentText = $this->domxml->create_text_node($this->getComment());
00648                 $qtiComment->append_child($qtiCommentText);
00649                 $qtiIdent->append_child($qtiComment);
00650         // add estimated working time
00651                 $qtiDuration = $this->domxml->create_element("duration");
00652                 $workingtime = $this->getEstimatedWorkingTime();
00653                 $qtiDurationText = $this->domxml->create_text_node(sprintf("P0Y0M0DT%dH%dM%dS", $workingtime["h"], $workingtime["m"], $workingtime["s"]));
00654                 $qtiDuration->append_child($qtiDurationText);
00655                 $qtiIdent->append_child($qtiDuration);
00656                 // add ILIAS specific metadata
00657                 $qtiItemmetadata = $this->domxml->create_element("itemmetadata");
00658                 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00659                 
00660                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00661                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00662                 $qtiFieldlabelText = $this->domxml->create_text_node("ILIAS_VERSION");
00663                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00664                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00665                 $qtiFieldentryText = $this->domxml->create_text_node($this->ilias->getSetting("ilias_version"));
00666                 $qtiFieldentry->append_child($qtiFieldentryText);
00667                 $qtiMetadatafield->append_child($qtiFieldlabel);
00668                 $qtiMetadatafield->append_child($qtiFieldentry);
00669                 $qtiMetadata->append_child($qtiMetadatafield);
00670 
00671                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00672                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00673                 $qtiFieldlabelText = $this->domxml->create_text_node("QUESTIONTYPE");
00674                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00675                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00676                 $qtiFieldentryText = $this->domxml->create_text_node(IMAGEMAP_QUESTION_IDENTIFIER);
00677                 $qtiFieldentry->append_child($qtiFieldentryText);
00678                 $qtiMetadatafield->append_child($qtiFieldlabel);
00679                 $qtiMetadatafield->append_child($qtiFieldentry);
00680                 $qtiMetadata->append_child($qtiMetadatafield);
00681                 
00682                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00683                 $qtiFieldlabel = $this->domxml->create_element("fieldlabel");
00684                 $qtiFieldlabelText = $this->domxml->create_text_node("AUTHOR");
00685                 $qtiFieldlabel->append_child($qtiFieldlabelText);
00686                 $qtiFieldentry = $this->domxml->create_element("fieldentry");
00687                 $qtiFieldentryText = $this->domxml->create_text_node($this->getAuthor());
00688                 $qtiFieldentry->append_child($qtiFieldentryText);
00689                 $qtiMetadatafield->append_child($qtiFieldlabel);
00690                 $qtiMetadatafield->append_child($qtiFieldentry);
00691                 $qtiMetadata->append_child($qtiMetadatafield);
00692                 
00693                 $qtiItemmetadata->append_child($qtiMetadata);
00694                 $qtiIdent->append_child($qtiItemmetadata);
00695                 
00696                 // PART I: qti presentation
00697                 $qtiPresentation = $this->domxml->create_element("presentation");
00698                 $qtiPresentation->set_attribute("label", $this->getTitle());
00699                 // add flow to presentation
00700                 $qtiFlow = $this->domxml->create_element("flow");
00701                 // add material with question text to presentation
00702                 $qtiMaterial = $this->domxml->create_element("material");
00703                 $qtiMatText = $this->domxml->create_element("mattext");
00704                 $qtiMatTextText = $this->domxml->create_text_node($this->get_question());
00705                 $qtiMatText->append_child($qtiMatTextText);
00706                 $qtiMaterial->append_child($qtiMatText);
00707                 $qtiFlow->append_child($qtiMaterial);
00708                 // add answers to presentation
00709                 $qtiResponseXy = $this->domxml->create_element("response_xy");
00710                 $qtiResponseXy->set_attribute("ident", "IM");
00711                 $qtiResponseXy->set_attribute("rcardinality", "Single");
00712                 $solution = $this->getSuggestedSolution(0);
00713                 if (count($solution))
00714                 {
00715                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
00716                         {
00717                                 $qtiMaterial = $this->domxml->create_element("material");
00718                                 $qtiMaterial->set_attribute("label", "suggested_solution");
00719                                 $qtiMatText = $this->domxml->create_element("mattext");
00720                                 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00721                                 if (strcmp($matches[1], "") != 0)
00722                                 {
00723                                         $intlink = $solution["internal_link"];
00724                                 }
00725                                 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00726                                 $qtiMatText->append_child($qtiMatTextText);
00727                                 $qtiMaterial->append_child($qtiMatText);
00728                                 $qtiResponseXy->append_child($qtiMaterial);
00729                         }
00730                 }
00731                 $qtiRenderHotspot = $this->domxml->create_element("render_hotspot");
00732                 $qtiMaterial = $this->domxml->create_element("material");
00733                 $qtiMatImage = $this->domxml->create_element("matimage");
00734                 $qtiMatImage->set_attribute("imagtype", "image/jpeg");
00735                 $qtiMatImage->set_attribute("label", $this->get_image_filename());
00736                 if ($a_include_binary)
00737                 {
00738                         $qtiMatImage->set_attribute("embedded", "base64");
00739                         $imagepath = $this->getImagePath() . $this->get_image_filename();
00740                         $fh = fopen($imagepath, "rb");
00741                         if ($fh == false)
00742                         {
00743                                 global $ilErr;
00744                                 $ilErr->raiseError($this->lng->txt("error_open_image_file"), $ilErr->MESSAGE);
00745                                 return;
00746                         }
00747                         $imagefile = fread($fh, filesize($imagepath));
00748                         fclose($fh);
00749                         $base64 = base64_encode($imagefile);
00750                         $qtiBase64Data = $this->domxml->create_text_node($base64);
00751                         $qtiMatImage->append_child($qtiBase64Data);
00752                 }
00753                 $qtiMaterial->append_child($qtiMatImage);
00754                 $qtiRenderHotspot->append_child($qtiMaterial);
00755                 // add answers
00756                 foreach ($this->answers as $index => $answer)
00757                 {
00758                         $qtiResponseLabel = $this->domxml->create_element("response_label");
00759                         $qtiResponseLabel->set_attribute("ident", $index);
00760                         switch ($answer->get_area())
00761                         {
00762                                 case "rect":
00763                                         $qtiResponseLabel->set_attribute("rarea", "Rectangle");
00764                                         break;
00765                                 case "circle":
00766                                         $qtiResponseLabel->set_attribute("rarea", "Ellipse");
00767                                         break;
00768                                 case "poly":
00769                                         $qtiResponseLabel->set_attribute("rarea", "Bounded");
00770                                         break;
00771                         }
00772                         $qtiResponseLabelCoords = $this->domxml->create_text_node($answer->get_coords());
00773                         $qtiMaterial = $this->domxml->create_element("material");
00774                         $qtiMatText = $this->domxml->create_element("mattext");
00775                         $qtiMatTextText = $this->domxml->create_text_node($answer->get_answertext());
00776                         $qtiMatText->append_child($qtiMatTextText);
00777                         $qtiMaterial->append_child($qtiMatText);
00778                         $qtiResponseLabel->append_child($qtiResponseLabelCoords);
00779                         $qtiResponseLabel->append_child($qtiMaterial);
00780                         $qtiRenderHotspot->append_child($qtiResponseLabel);
00781                 }
00782                 $qtiResponseXy->append_child($qtiRenderHotspot);
00783                 $qtiFlow->append_child($qtiResponseXy);
00784                 $qtiPresentation->append_child($qtiFlow);
00785                 $qtiIdent->append_child($qtiPresentation);
00786 
00787                 // PART II: qti resprocessing
00788                 $qtiResprocessing = $this->domxml->create_element("resprocessing");
00789                 $qtiOutcomes = $this->domxml->create_element("outcomes");
00790                 $qtiDecvar = $this->domxml->create_element("decvar");
00791                 $qtiOutcomes->append_child($qtiDecvar);
00792                 $qtiResprocessing->append_child($qtiOutcomes);
00793                 // add response conditions
00794                 foreach ($this->answers as $index => $answer)
00795                 {
00796                         $qtiRespcondition = $this->domxml->create_element("respcondition");
00797                         $qtiRespcondition->set_attribute("continue", "Yes");
00798                         // qti conditionvar
00799                         $qtiConditionvar = $this->domxml->create_element("conditionvar");
00800                         if (!$answer->isStateSet())
00801                         {
00802                                 $qtinot = $this->domxml->create_element("not");
00803                         }
00804                         $qtiVarinside = $this->domxml->create_element("varinside");
00805                         $qtiVarinside->set_attribute("respident", "IM");
00806                         switch ($answer->get_area())
00807                         {
00808                                 case "rect":
00809                                         $qtiVarinside->set_attribute("areatype", "Rectangle");
00810                                         break;
00811                                 case "circle":
00812                                         $qtiVarinside->set_attribute("areatype", "Ellipse");
00813                                         break;
00814                                 case "poly":
00815                                         $qtiVarinside->set_attribute("areatype", "Bounded");
00816                                         break;
00817                         }
00818                         $qtiVarinsideText = $this->domxml->create_text_node($answer->get_coords());
00819                         $qtiVarinside->append_child($qtiVarinsideText);
00820                         if (!$answer->isStateSet())
00821                         {
00822                                 $qtiConditionvar->append_child($qtinot);
00823                                 $qtinot->append_child($qtiVarinside);
00824                         }
00825                         else
00826                         {
00827                                 $qtiConditionvar->append_child($qtiVarinside);
00828                         }
00829                         // qti setvar
00830                         $qtiSetvar = $this->domxml->create_element("setvar");
00831                         $qtiSetvar->set_attribute("action", "Add");
00832                         $qtiSetvarText = $this->domxml->create_text_node($answer->get_points());
00833                         $qtiSetvar->append_child($qtiSetvarText);
00834                         // qti displayfeedback
00835                         $qtiDisplayfeedback = $this->domxml->create_element("displayfeedback");
00836                         $qtiDisplayfeedback->set_attribute("feedbacktype", "Response");
00837                         $linkrefid = "";
00838                         if ($answer->isStateSet())
00839                         {
00840                                 $linkrefid = "True";
00841                         }
00842                           else
00843                         {
00844                                 $linkrefid = "False_$index";
00845                         }
00846                         $qtiDisplayfeedback->set_attribute("linkrefid", $linkrefid);
00847                         $qtiRespcondition->append_child($qtiConditionvar);
00848                         $qtiRespcondition->append_child($qtiSetvar);
00849                         $qtiRespcondition->append_child($qtiDisplayfeedback);
00850                         $qtiResprocessing->append_child($qtiRespcondition);
00851                 }
00852                 $qtiIdent->append_child($qtiResprocessing);
00853 
00854                 // PART III: qti itemfeedback
00855                 foreach ($this->answers as $index => $answer)
00856                 {
00857                         $qtiItemfeedback = $this->domxml->create_element("itemfeedback");
00858                         $linkrefid = "";
00859                         if ($answer->isStateSet())
00860                         {
00861                                 $linkrefid = "True";
00862                         }
00863                           else
00864                         {
00865                                 $linkrefid = "False_$index";
00866                         }
00867                         $qtiItemfeedback->set_attribute("ident", $linkrefid);
00868                         $qtiItemfeedback->set_attribute("view", "All");
00869                         // qti flow_mat
00870                         $qtiFlowmat = $this->domxml->create_element("flow_mat");
00871                         $qtiMaterial = $this->domxml->create_element("material");
00872                         $qtiMattext = $this->domxml->create_element("mattext");
00873                         // Insert response text for right/wrong answers here!!!
00874                         $qtiMattextText = $this->domxml->create_text_node("");
00875                         $qtiMattext->append_child($qtiMattextText);
00876                         $qtiMaterial->append_child($qtiMattext);
00877                         $qtiFlowmat->append_child($qtiMaterial);
00878                         $qtiItemfeedback->append_child($qtiFlowmat);
00879                         $qtiIdent->append_child($qtiItemfeedback);
00880                 }
00881 
00882                 $xml = $this->domxml->dump_mem(true);
00883                 if (!$a_include_header)
00884                 {
00885                         $pos = strpos($xml, "?>");
00886                         $xml = substr($xml, $pos + 2);
00887                 }
00888 //echo htmlentities($xml);
00889                 return $xml;
00890 
00891         }
00892 
00902   function get_question() {
00903     return $this->question;
00904   }
00905 
00915   function set_question($question = "") {
00916     $this->question = $question;
00917   }
00918 
00928   function get_imagemap_filename() {
00929     return $this->imagemap_filename;
00930   }
00931 
00941   function set_imagemap_filename($imagemap_filename, $imagemap_tempfilename = "") {
00942     if (!empty($imagemap_filename)) {
00943       $this->imagemap_filename = $imagemap_filename;
00944     }
00945     if (!empty($imagemap_tempfilename)) {
00946             $fp = fopen($imagemap_tempfilename, "r");
00947             $contents = fread($fp, filesize($imagemap_tempfilename));
00948       fclose($fp);
00949                         if (preg_match_all("/<area(.+)>/siU", $contents, $matches)) {
00950                         for ($i=0; $i< count($matches[1]); $i++) {
00951                         preg_match("/alt\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $alt);
00952                         preg_match("/coords\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $coords);
00953                         preg_match("/shape\s*=\s*\"(.+)\"\s*/siU", $matches[1][$i], $shape);
00954                                         $this->add_answer($alt[1], 0.0, FALSE, count($this->answers), $coords[1], $shape[1]);
00955                         }
00956                         }
00957     }
00958         }
00959 
00969   function get_image_filename() {
00970     return $this->image_filename;
00971   }
00972 
00982   function set_image_filename($image_filename, $image_tempfilename = "") {
00983 
00984     if (!empty($image_filename)) 
00985                 {
00986                         $image_filename = str_replace(" ", "_", $image_filename);
00987       $this->image_filename = $image_filename;
00988     }
00989                 if (!empty($image_tempfilename)) {
00990                         $imagepath = $this->getImagePath();
00991                         if (!file_exists($imagepath)) {
00992                                 ilUtil::makeDirParents($imagepath);
00993                         }
00994                         
00995                         //if (!move_uploaded_file($image_tempfilename, $imagepath . $image_filename))
00996                         if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $imagepath.$image_filename))
00997                         {
00998                                 print "image not uploaded!!!! ";
00999                         } else {
01000                                 // create thumbnail file
01001                                 $size = 100;
01002                                 $thumbpath = $imagepath . $image_filename . "." . "thumb.jpg";
01003                                 $convert_cmd = ilUtil::getConvertCmd() . " $imagepath$image_filename -resize $sizex$size $thumbpath";
01004                                 system($convert_cmd);
01005                         }
01006                 }
01007   }
01008 
01018   function get_imagemap_contents($href = "#") {
01019                 $imagemap_contents = "<map name=\"".$this->title."\"> ";
01020                 for ($i = 0; $i < count($this->answers); $i++) {
01021                         $imagemap_contents .= "<area alt=\"".$this->answers[$i]->get_answertext()."\" ";
01022                         $imagemap_contents .= "shape=\"".$this->answers[$i]->get_area()."\" ";
01023                         $imagemap_contents .= "coords=\"".$this->answers[$i]->get_coords()."\" ";
01024                         $imagemap_contents .= "href=\"$href&selimage=" . $this->answers[$i]->get_order() . "\" /> ";
01025                 }
01026                 $imagemap_contents .= "</map>";
01027     return $imagemap_contents;
01028   }
01029 
01039   function get_points() {
01040     return $this->points;
01041   }
01042 
01052   function set_points($points = 0.0) {
01053     $this->points = $points;
01054   }
01055 
01070   function add_answer(
01071     $answertext = "",
01072     $points = 0.0,
01073     $status = 0,
01074     $order = 0,
01075     $coords="",
01076     $area=""
01077   )
01078   {
01079     if (array_key_exists($order, $this->answers)) {
01080       // Antwort einf�gen
01081       $answer = new ASS_AnswerImagemap($answertext, $points, $order, $status, $coords, $area);
01082                         for ($i = count($this->answers) - 1; $i >= $order; $i--) {
01083                                 $this->answers[$i+1] = $this->answers[$i];
01084                                 $this->answers[$i+1]->set_order($i+1);
01085                         }
01086                         $this->answers[$order] = $answer;
01087     } else {
01088       // Anwort anh�ngen
01089       $answer = new ASS_AnswerImagemap($answertext, $points, count($this->answers), $status, $coords, $area);
01090       array_push($this->answers, $answer);
01091     }
01092   }
01093 
01103   function get_answer_count() {
01104     return count($this->answers);
01105   }
01106 
01118   function get_answer($index = 0) {
01119     if ($index < 0) return NULL;
01120     if (count($this->answers) < 1) return NULL;
01121     if ($index >= count($this->answers)) return NULL;
01122     return $this->answers[$index];
01123   }
01124 
01135   function deleteArea($index = 0) {
01136     if ($index < 0) return;
01137     if (count($this->answers) < 1) return;
01138     if ($index >= count($this->answers)) return;
01139     unset($this->answers[$index]);
01140     $this->answers = array_values($this->answers);
01141     for ($i = 0; $i < count($this->answers); $i++) {
01142       if ($this->answers[$i]->get_order() > $index) {
01143         $this->answers[$i]->set_order($i);
01144       }
01145     }
01146   }
01147 
01156   function flush_answers() {
01157     $this->answers = array();
01158   }
01159 
01168   function getMaximumPoints() {
01169                 $points = array("set" => 0, "unset" => 0);
01170                 foreach ($this->answers as $key => $value) {
01171                         if ($value->get_points() > $points["set"])
01172                         {
01173                                 $points["set"] = $value->get_points();
01174                         }
01175                 }
01176                 return $points["set"];
01177   }
01178 
01188   function getReachedPoints($user_id, $test_id) {
01189     $found_values = array();
01190     $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01191       $this->ilias->db->quote($user_id),
01192       $this->ilias->db->quote($test_id),
01193       $this->ilias->db->quote($this->getId())
01194     );
01195     $result = $this->ilias->db->query($query);
01196                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01197                 {
01198                         if (strcmp($data->value1, "") != 0)
01199                         {
01200                                 array_push($found_values, $data->value1);
01201                         }
01202                 }
01203                 $points = 0;
01204                 if (count($found_values) > 0)
01205                 {
01206                         foreach ($this->answers as $key => $answer)
01207                         {
01208                                 if ($answer->isStateChecked())
01209                                 {
01210                                         if (in_array($key, $found_values))
01211                                         {
01212                                                 $points += $answer->get_points();
01213                                         }
01214                                 }
01215                         }
01216                 }
01217                 return $points;
01218   }
01219 
01231         function _getReachedPoints($user_id, $test_id)
01232         {
01233                 return 0;
01234         }
01235 
01245   function getReachedInformation($user_id, $test_id) {
01246     $found_values = array();
01247     $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01248       $this->ilias->db->quote($user_id),
01249       $this->ilias->db->quote($test_id),
01250       $this->ilias->db->quote($this->getId())
01251     );
01252     $result = $this->ilias->db->query($query);
01253                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT))
01254                 {
01255                         array_push($found_values, $data->value1);
01256                 }
01257                 $counter = 1;
01258                 $user_result = array();
01259                 foreach ($found_values as $key => $value)
01260                 {
01261                         $solution = array(
01262                                 "order" => "$counter",
01263                                 "points" => 0,
01264                                 "true" => 0,
01265                                 "value" => "",
01266                                 );
01267                         if (strlen($value) > 0)
01268                         {
01269                                 $solution["value"] = $value;
01270                                 $solution["points"] = $this->answers[$value]->get_points();
01271                                 if ($this->answers[$value]->isStateChecked())
01272                                 {
01273                                         $solution["true"] = 1;
01274                                 }
01275                         }
01276                         $counter++;
01277                         array_push($user_result, $solution);
01278                 }
01279                 return $user_result;
01280   }
01281 
01292   function saveWorkingData($test_id, $limit_to = LIMIT_NO_LIMIT) {
01293     global $ilDB;
01294                 global $ilUser;
01295     $db =& $ilDB->db;
01296 
01297     $query = sprintf("DELETE FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
01298       $db->quote($ilUser->id),
01299       $db->quote($test_id),
01300       $db->quote($this->getId())
01301     );
01302     $result = $db->query($query);
01303 
01304                 $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)",
01305                         $db->quote($ilUser->id),
01306                         $db->quote($test_id),
01307                         $db->quote($this->getId()),
01308                         $db->quote($_GET["selImage"])
01309                 );
01310                 $result = $db->query($query);
01311 //    parent::saveWorkingData($limit_to);
01312                 return true;
01313   }
01314 
01315         function syncWithOriginal()
01316         {
01317                 global $ilias;
01318                 if ($this->original_id)
01319                 {
01320                         $complete = 0;
01321                         if ($this->isComplete())
01322                         {
01323                                 $complete = 1;
01324                         }
01325                         $db = & $ilias->db;
01326         
01327                         $estw_time = $this->getEstimatedWorkingTime();
01328                         $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
01329         
01330                         $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, working_time = %s, points = %s, image_file = %s, complete = %s WHERE question_id = %s",
01331                                 $db->quote($this->obj_id. ""),
01332                                 $db->quote($this->title . ""),
01333                                 $db->quote($this->comment . ""),
01334                                 $db->quote($this->author . ""),
01335                                 $db->quote($this->question . ""),
01336                                 $db->quote($estw_time . ""),
01337                                 $db->quote($this->points . ""),
01338                                 $db->quote($this->image_filename . ""),
01339                                 $db->quote($complete . ""),
01340                                 $db->quote($this->original_id . "")
01341                         );
01342                         $result = $db->query($query);
01343 
01344                         if ($result == DB_OK)
01345                         {
01346                                 // write answers
01347                                 // delete old answers
01348                                 $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
01349                                         $db->quote($this->original_id)
01350                                 );
01351                                 $result = $db->query($query);
01352         
01353                                 foreach ($this->answers as $key => $value)
01354                                 {
01355                                         $answer_obj = $this->answers[$key];
01356                                         $query = sprintf("INSERT INTO qpl_answers (answer_id, question_fi, answertext, points, aorder, correctness, coords, area, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, NULL)",
01357                                                 $db->quote($this->original_id . ""),
01358                                                 $db->quote($answer_obj->get_answertext() . ""),
01359                                                 $db->quote($answer_obj->get_points() . ""),
01360                                                 $db->quote($answer_obj->get_order() . ""),
01361                                                 $db->quote($answer_obj->getState() . ""),
01362                                                 $db->quote($answer_obj->get_coords() . ""),
01363                                                 $db->quote($answer_obj->get_area() . "")
01364                                                 );
01365                                         $answer_result = $db->query($query);
01366                                 }
01367                         }
01368                         parent::syncWithOriginal();
01369                 }
01370         }
01371 
01380         function getQuestionType()
01381         {
01382                 return 6;
01383         }
01384 }
01385 
01386 ?>

Generated on Fri Dec 13 2013 09:06:32 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1