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

survey/classes/class.SurveyTextQuestion.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 include_once "./survey/classes/class.SurveyQuestion.php";
00025 include_once "./survey/classes/inc.SurveyConstants.php";
00026 
00038 class SurveyTextQuestion extends SurveyQuestion 
00039 {
00040         var $maxchars;
00052   function SurveyTextQuestion(
00053     $title = "",
00054     $description = "",
00055     $author = "",
00056                 $questiontext = "",
00057     $owner = -1
00058   )
00059 
00060   {
00061                 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00062                 $this->maxchars = 0;
00063         }
00064         
00073   function loadFromDb($id) 
00074         {
00075     $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00076       $this->ilias->db->quote($id)
00077     );
00078     $result = $this->ilias->db->query($query);
00079     if (strcmp(strtolower(get_class($result)), db_result) == 0) 
00080                 {
00081       if ($result->numRows() == 1) 
00082                         {
00083                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00084                                 $this->id = $data->question_id;
00085                                 $this->title = $data->title;
00086                                 $this->description = $data->description;
00087                                 $this->obj_id = $data->obj_fi;
00088                                 $this->author = $data->author;
00089                                 $this->obligatory = $data->obligatory;
00090                                 $this->owner = $data->owner_fi;
00091                                 $this->original_id = $data->original_id;
00092                                 $this->maxchars = $data->maxchars;
00093                                 $this->questiontext = $data->questiontext;
00094                                 $this->complete = $data->complete;
00095       }
00096       // loads materials uris from database
00097       $this->loadMaterialFromDb($id);
00098                 }
00099                 parent::loadFromDb($id);
00100   }
00101 
00110         function isComplete()
00111         {
00112                 if ($this->title and $this->author and $this->questiontext)
00113                 {
00114                         return 1;
00115                 }
00116                 else
00117                 {
00118                         return 0;
00119                 }
00120         }
00121         
00129         function setMaxChars($maxchars = 0)
00130         {
00131                 $this->maxchars = $maxchars;
00132         }
00133         
00141         function getMaxChars()
00142         {
00143                 return $this->maxchars;
00144         }
00145         
00153   function saveToDb($original_id = "")
00154   {
00155                 $maxchars = "NULL";
00156                 if ($this->maxchars)
00157                 {
00158                         $maxchars = $this->ilias->db->quote($this->maxchars . "");
00159                 }
00160                 $complete = 0;
00161                 if ($this->isComplete()) {
00162                         $complete = 1;
00163                 }
00164                 if ($original_id)
00165                 {
00166                         $original_id = $this->ilias->db->quote($original_id);
00167                 }
00168                 else
00169                 {
00170                         $original_id = "NULL";
00171                 }
00172     if ($this->id == -1) {
00173       // Write new dataset
00174       $now = getdate();
00175       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00176       $query = sprintf("INSERT INTO survey_question (question_id, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, maxchars, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00177                                 $this->ilias->db->quote($this->getQuestionType() . ""),
00178                                 $this->ilias->db->quote($this->obj_id),
00179                                 $this->ilias->db->quote($this->owner),
00180                                 $this->ilias->db->quote($this->title),
00181                                 $this->ilias->db->quote($this->description),
00182                                 $this->ilias->db->quote($this->author),
00183                                 $this->ilias->db->quote($this->questiontext),
00184                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00185                                 $maxchars,
00186                                 $this->ilias->db->quote("$complete"),
00187                                 $this->ilias->db->quote($created),
00188                                 $original_id
00189       );
00190       $result = $this->ilias->db->query($query);
00191       if ($result == DB_OK) 
00192                         {
00193         $this->id = $this->ilias->db->getLastInsertId();
00194       }
00195     } 
00196                 else 
00197                 {
00198       // update existing dataset
00199       $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, maxchars = %s, complete = %s WHERE question_id = %s",
00200                                 $this->ilias->db->quote($this->title),
00201                                 $this->ilias->db->quote($this->description),
00202                                 $this->ilias->db->quote($this->author),
00203                                 $this->ilias->db->quote($this->questiontext),
00204                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00205                                 $maxchars,
00206                                 $this->ilias->db->quote("$complete"),
00207                                 $this->ilias->db->quote($this->id)
00208       );
00209       $result = $this->ilias->db->query($query);
00210     }
00211     if ($result == DB_OK) {
00212       // saving material uris in the database
00213       $this->saveMaterialsToDb();
00214     }
00215                 parent::saveToDb($original_id);
00216   }
00217 
00227         function from_xml($xml_text)
00228         {
00229                 $result = false;
00230                 if (!empty($this->domxml))
00231                 {
00232                         $this->domxml->free();
00233                 }
00234                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00235                 $this->domxml = domxml_open_mem($xml_text);
00236                 if (!empty($this->domxml))
00237                 {
00238                         $root = $this->domxml->document_element();
00239                         $item = $root->first_child();
00240                         $this->setTitle($item->get_attribute("title"));
00241                         $this->gaps = array();
00242                         $itemnodes = $item->child_nodes();
00243                         foreach ($itemnodes as $index => $node)
00244                         {
00245                                 switch ($node->node_name())
00246                                 {
00247                                         case "qticomment":
00248                                                 $comment = $node->get_content();
00249                                                 if (strpos($comment, "ILIAS Version=") !== false)
00250                                                 {
00251                                                 }
00252                                                 elseif (strpos($comment, "Questiontype=") !== false)
00253                                                 {
00254                                                 }
00255                                                 elseif (strpos($comment, "Author=") !== false)
00256                                                 {
00257                                                         $comment = str_replace("Author=", "", $comment);
00258                                                         $this->setAuthor($comment);
00259                                                 }
00260                                                 else
00261                                                 {
00262                                                         $this->setDescription($comment);
00263                                                 }
00264                                                 break;
00265                                         case "itemmetadata":
00266                                                 $qtimetadata = $node->first_child();
00267                                                 $metadata_fields = $qtimetadata->child_nodes();
00268                                                 foreach ($metadata_fields as $index => $metadata_field)
00269                                                 {
00270                                                         $fieldlabel = $metadata_field->first_child();
00271                                                         $fieldentry = $fieldlabel->next_sibling();
00272                                                         switch ($fieldlabel->get_content())
00273                                                         {
00274                                                                 case "obligatory":
00275                                                                         $this->setObligatory($fieldentry->get_content());
00276                                                                         break;
00277                                                                 case "maxchars":
00278                                                                         $this->setMaxChars($fieldentry->get_content());
00279                                                                         break;
00280                                                         }
00281                                                 }
00282                                                 break;
00283                                         case "presentation":
00284                                                 $flow = $node->first_child();
00285                                                 $flownodes = $flow->child_nodes();
00286                                                 foreach ($flownodes as $idx => $flownode)
00287                                                 {
00288                                                         if (strcmp($flownode->node_name(), "material") == 0)
00289                                                         {
00290                                                                 $mattext = $flownode->first_child();
00291                                                                 $this->setQuestiontext($mattext->get_content());
00292                                                         }
00293                                                         elseif (strcmp($flownode->node_name(), "response_str") == 0)
00294                                                         {
00295                                                                 $ident = $flownode->get_attribute("ident");
00296                                                                 $response_lid_nodes = $flownode->child_nodes();
00297                                                                 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00298                                                                 {
00299                                                                         switch ($resp_lid_node->node_name())
00300                                                                         {
00301                                                                                 case "material":
00302                                                                                         $matlabel = $resp_lid_node->get_attribute("label");
00303                                                                                         $mattype = $resp_lid_node->first_child();
00304                                                                                         if (strcmp($mattype->node_name(), "mattext") == 0)
00305                                                                                         {
00306                                                                                                 $material = $mattype->get_content();
00307                                                                                                 if ($material)
00308                                                                                                 {
00309                                                                                                         if ($this->getId() < 1)
00310                                                                                                         {
00311                                                                                                                 $this->saveToDb();
00312                                                                                                         }
00313                                                                                                         $this->setMaterial($material, true, $matlabel);
00314                                                                                                 }
00315                                                                                         }
00316                                                                                         break;
00317                                                                         }
00318                                                                 }
00319                                                         }
00320                                                 }
00321                                                 break;
00322                                 }
00323                         }
00324                         $result = true;
00325                 }
00326                 return $result;
00327         }
00328 
00338         function to_xml($a_include_header = true, $obligatory_state = "")
00339         {
00340                 include_once("./classes/class.ilXmlWriter.php");
00341                 $a_xml_writer = new ilXmlWriter;
00342                 // set xml header
00343                 $a_xml_writer->xmlHeader();
00344                 $a_xml_writer->xmlStartTag("questestinterop");
00345                 $attrs = array(
00346                         "ident" => $this->getId(),
00347                         "title" => $this->getTitle()
00348                 );
00349                 $a_xml_writer->xmlStartTag("item", $attrs);
00350                 // add question description
00351                 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
00352                 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00353                 $a_xml_writer->xmlElement("qticomment", NULL, "Questiontype=".TEXT_QUESTION_IDENTIFIER);
00354                 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
00355                 // add ILIAS specific metadata
00356                 $a_xml_writer->xmlStartTag("itemmetadata");
00357                 $a_xml_writer->xmlStartTag("qtimetadata");
00358                 $a_xml_writer->xmlStartTag("qtimetadatafield");
00359                 $a_xml_writer->xmlElement("fieldlabel", NULL, "obligatory");
00360                 if (strcmp($obligatory_state, "") != 0)
00361                 {
00362                         $this->setObligatory($obligatory_state);
00363                 }
00364                 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getObligatory()));
00365                 $a_xml_writer->xmlEndTag("qtimetadatafield");
00366                 $a_xml_writer->xmlStartTag("qtimetadatafield");
00367                 $a_xml_writer->xmlElement("fieldlabel", NULL, "maxchars");
00368                 if (!$this->getMaxChars())
00369                 {
00370                         $a_xml_writer->xmlElement("fieldentry", NULL, "");
00371                 }
00372                 else
00373                 {
00374                         $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getMaxChars()));
00375                 }
00376                 $a_xml_writer->xmlEndTag("qtimetadatafield");
00377                 $a_xml_writer->xmlEndTag("qtimetadata");
00378                 $a_xml_writer->xmlEndTag("itemmetadata");
00379 
00380                 // PART I: qti presentation
00381                 $attrs = array(
00382                         "label" => $this->getTitle()
00383                 );
00384                 $a_xml_writer->xmlStartTag("presentation", $attrs);
00385                 // add flow to presentation
00386                 $a_xml_writer->xmlStartTag("flow");
00387                 // add material with question text to presentation
00388                 $a_xml_writer->xmlStartTag("material");
00389                 $a_xml_writer->xmlElement("mattext", NULL, $this->getQuestiontext());
00390                 $a_xml_writer->xmlEndTag("material");
00391                 // add answers to presentation
00392                 $attrs = array(
00393                         "ident" => "TEXT",
00394                         "rcardinality" => "Single"
00395                 );
00396                 $a_xml_writer->xmlStartTag("response_str", $attrs);
00397                 
00398                 if (count($this->material))
00399                 {
00400                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00401                         {
00402                                 $attrs = array(
00403                                         "label" => $this->material["title"]
00404                                 );
00405                                 $a_xml_writer->xmlStartTag("material", $attrs);
00406                                 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00407                                 if (strcmp($matches[1], "") != 0)
00408                                 {
00409                                         $intlink = $this->material["internal_link"];
00410                                 }
00411                                 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
00412                                 $a_xml_writer->xmlEndTag("material");
00413                         }
00414                 }
00415 
00416                 $a_xml_writer->xmlEndTag("response_str");
00417                 $a_xml_writer->xmlEndTag("flow");
00418                 $a_xml_writer->xmlEndTag("presentation");
00419                 $a_xml_writer->xmlEndTag("item");
00420                 $a_xml_writer->xmlEndTag("questestinterop");
00421 
00422                 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00423                 if (!$a_include_header)
00424                 {
00425                         $pos = strpos($xml, "?>");
00426                         $xml = substr($xml, $pos + 2);
00427                 }
00428                 return $xml;
00429         }
00430 
00431         function syncWithOriginal()
00432         {
00433                 if ($this->original_id)
00434                 {
00435                         $complete = 0;
00436                         if ($this->isComplete()) 
00437                         {
00438                                 $complete = 1;
00439                         }
00440       $query = sprintf("UPDATE survey_question SET title = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00441                                 $this->ilias->db->quote($this->title . ""),
00442                                 $this->ilias->db->quote($this->description . ""),
00443                                 $this->ilias->db->quote($this->author . ""),
00444                                 $this->ilias->db->quote($this->questiontext . ""),
00445                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00446                                 $this->ilias->db->quote($complete . ""),
00447                                 $this->ilias->db->quote($this->original_id . "")
00448       );
00449       $result = $this->ilias->db->query($query);
00450                 }
00451                 parent::syncWithOriginal();
00452         }
00453         
00462         function _getMaxChars($question_id)
00463         {
00464                 global $ilDB;
00465                 $query = sprintf("SELECT maxchars FROM survey_question WHERE question_id = %s",
00466                         $ilDB->quote($question_id . "")
00467                 );
00468                 $result = $ilDB->query($query);
00469                 if ($result->numRows())
00470                 {
00471                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00472                         return $row["maxchars"];
00473                 }
00474                 return 0;
00475         }
00476 
00485         function getQuestionType()
00486         {
00487                 return 4;
00488         }
00489 }
00490 ?>

Generated on Fri Dec 13 2013 11:58:02 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1