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

survey/classes/class.SurveyNominalQuestion.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 SurveyNominalQuestion extends SurveyQuestion 
00039 {
00047   var $subtype;
00048 
00056   var $categories;
00057 
00069   function SurveyNominalQuestion(
00070     $title = "",
00071     $description = "",
00072     $author = "",
00073                 $questiontext = "",
00074     $owner = -1,
00075                 $subtype = 0
00076   )
00077 
00078   {
00079                 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00080                 $this->subtype = $subtype;
00081                 include_once "./survey/classes/class.SurveyCategories.php";
00082                 $this->categories = new SurveyCategories();
00083         }
00084         
00094   function setSubtype($subtype = SUBTYPE_MCSR) 
00095         {
00096     $this->subtype = $subtype;
00097   }
00098 
00108   function getSubtype() 
00109         {
00110     return $this->subtype;
00111   }
00112         
00121   function loadFromDb($id) 
00122         {
00123     $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00124       $this->ilias->db->quote($id)
00125     );
00126     $result = $this->ilias->db->query($query);
00127     if (strcmp(strtolower(get_class($result)), db_result) == 0) 
00128                 {
00129       if ($result->numRows() == 1) 
00130                         {
00131                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00132                                 $this->id = $data->question_id;
00133                                 $this->title = $data->title;
00134                                 $this->description = $data->description;
00135                                 $this->obj_id = $data->obj_fi;
00136                                 $this->author = $data->author;
00137                                 $this->subtype = $data->subtype;
00138                                 $this->orientation = $data->orientation;
00139                                 $this->obligatory = $data->obligatory;
00140                                 $this->owner = $data->owner_fi;
00141                                 $this->questiontext = $data->questiontext;
00142                                 $this->complete = $data->complete;
00143                                 $this->original_id = $data->original_id;
00144       }
00145       // loads materials uris from database
00146       $this->loadMaterialFromDb($id);
00147 
00148                         $this->categories->flushCategories();
00149       $query = sprintf("SELECT survey_variable.*, survey_category.title FROM survey_variable, survey_category WHERE survey_variable.question_fi = %s AND survey_variable.category_fi = survey_category.category_id ORDER BY sequence ASC",
00150         $this->ilias->db->quote($id)
00151       );
00152       $result = $this->ilias->db->query($query);
00153       if (strcmp(strtolower(get_class($result)), db_result) == 0) 
00154                         {
00155         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
00156                                 {
00157                                         $this->categories->addCategory($data->title);
00158         }
00159       }
00160     }
00161                 parent::loadFromDb($id);
00162   }
00163 
00172         function isComplete()
00173         {
00174                 if (strlen($this->title) && strlen($this->author) && strlen($this->questiontext) && $this->categories->getCategoryCount())
00175                 {
00176                         return 1;
00177                 }
00178                 else
00179                 {
00180                         return 0;
00181                 }
00182         }
00183 
00191   function saveToDb($original_id = "", $withanswers = true)
00192   {
00193                 $complete = 0;
00194                 if ($this->isComplete()) 
00195                 {
00196                         $complete = 1;
00197                 }
00198                 if ($original_id)
00199                 {
00200                         $original_id = $this->ilias->db->quote($original_id);
00201                 }
00202                 else
00203                 {
00204                         $original_id = "NULL";
00205                 }
00206     if ($this->id == -1) {
00207       // Write new dataset
00208       $now = getdate();
00209       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00210       $query = sprintf("INSERT INTO survey_question (question_id, subtype, questiontype_fi, obj_fi, owner_fi, title, description, author, questiontext, obligatory, orientation, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00211                                 $this->ilias->db->quote("$this->subtype"),
00212                                 $this->ilias->db->quote($this->getQuestionType()),
00213                                 $this->ilias->db->quote($this->obj_id),
00214                                 $this->ilias->db->quote($this->owner),
00215                                 $this->ilias->db->quote($this->title),
00216                                 $this->ilias->db->quote($this->description),
00217                                 $this->ilias->db->quote($this->author),
00218                                 $this->ilias->db->quote($this->questiontext),
00219                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00220                                 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00221                                 $this->ilias->db->quote("$complete"),
00222                                 $this->ilias->db->quote($created),
00223                                 $original_id
00224       );
00225       $result = $this->ilias->db->query($query);
00226       if ($result == DB_OK) 
00227                         {
00228         $this->id = $this->ilias->db->getLastInsertId();
00229       }
00230     } 
00231                 else 
00232                 {
00233       // update existing dataset
00234       $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, orientation = %s, complete = %s WHERE question_id = %s",
00235                                 $this->ilias->db->quote($this->title),
00236                                 $this->ilias->db->quote("$this->subtype"),
00237                                 $this->ilias->db->quote($this->description),
00238                                 $this->ilias->db->quote($this->author),
00239                                 $this->ilias->db->quote($this->questiontext),
00240                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00241                                 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00242                                 $this->ilias->db->quote("$complete"),
00243                                 $this->ilias->db->quote($this->id)
00244       );
00245       $result = $this->ilias->db->query($query);
00246     }
00247     if ($result == DB_OK) 
00248                 {
00249       // saving material uris in the database
00250       $this->saveMaterialsToDb();
00251                         if ($withanswers)
00252                         {
00253                                 $this->saveCategoriesToDb();
00254                         }
00255     }
00256                 parent::saveToDb($original_id);
00257   }
00258 
00268         function from_xml($xml_text)
00269         {
00270                 $result = false;
00271                 if (!empty($this->domxml))
00272                 {
00273                         $this->domxml->free();
00274                 }
00275                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00276                 $this->domxml = domxml_open_mem($xml_text);
00277                 if (!empty($this->domxml))
00278                 {
00279                         $root = $this->domxml->document_element();
00280                         $item = $root->first_child();
00281                         $this->setTitle($item->get_attribute("title"));
00282                         $this->gaps = array();
00283                         $itemnodes = $item->child_nodes();
00284                         foreach ($itemnodes as $index => $node)
00285                         {
00286                                 switch ($node->node_name())
00287                                 {
00288                                         case "qticomment":
00289                                                 $comment = $node->get_content();
00290                                                 if (strpos($comment, "ILIAS Version=") !== false)
00291                                                 {
00292                                                 }
00293                                                 elseif (strpos($comment, "Questiontype=") !== false)
00294                                                 {
00295                                                 }
00296                                                 elseif (strpos($comment, "Author=") !== false)
00297                                                 {
00298                                                         $comment = str_replace("Author=", "", $comment);
00299                                                         $this->setAuthor($comment);
00300                                                 }
00301                                                 else
00302                                                 {
00303                                                         $this->setDescription($comment);
00304                                                 }
00305                                                 break;
00306                                         case "itemmetadata":
00307                                                 $qtimetadata = $node->first_child();
00308                                                 $metadata_fields = $qtimetadata->child_nodes();
00309                                                 foreach ($metadata_fields as $index => $metadata_field)
00310                                                 {
00311                                                         $fieldlabel = $metadata_field->first_child();
00312                                                         $fieldentry = $fieldlabel->next_sibling();
00313                                                         switch ($fieldlabel->get_content())
00314                                                         {
00315                                                                 case "obligatory":
00316                                                                         $this->setObligatory($fieldentry->get_content());
00317                                                                         break;
00318                                                                 case "orientation":
00319                                                                         $this->setOrientation($fieldentry->get_content());
00320                                                                         break;
00321                                                         }
00322                                                 }
00323                                                 break;
00324                                         case "presentation":
00325                                                 $flow = $node->first_child();
00326                                                 $flownodes = $flow->child_nodes();
00327                                                 foreach ($flownodes as $idx => $flownode)
00328                                                 {
00329                                                         if (strcmp($flownode->node_name(), "material") == 0)
00330                                                         {
00331                                                                 $mattext = $flownode->first_child();
00332                                                                 $this->setQuestiontext($mattext->get_content());
00333                                                         }
00334                                                         elseif (strcmp($flownode->node_name(), "response_lid") == 0)
00335                                                         {
00336                                                                 $ident = $flownode->get_attribute("ident");
00337                                                                 if (strcmp($ident, "MCSR") == 0)
00338                                                                 {
00339                                                                         $this->setSubtype(SUBTYPE_MCSR);
00340                                                                 }
00341                                                                 else
00342                                                                 {
00343                                                                         $this->setSubtype(SUBTYPE_MCMR);
00344                                                                 }
00345                                                                 $shuffle = "";
00346                                                                 $response_lid_nodes = $flownode->child_nodes();
00347                                                                 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00348                                                                 {
00349                                                                         switch ($resp_lid_node->node_name())
00350                                                                         {
00351                                                                                 case "render_choice":
00352                                                                                         $render_choice = $resp_lid_node;
00353                                                                                         $labels = $render_choice->child_nodes();
00354                                                                                         foreach ($labels as $lidx => $response_label)
00355                                                                                         {
00356                                                                                                 $material = $response_label->first_child();
00357                                                                                                 $mattext = $material->first_child();
00358                                                                                                 $shuf = 0;
00359                                                                                                 $this->categories->addCategoryAtPosition($mattext->get_content(), $response_label->get_attribute("ident"));
00360                                                                                         }
00361                                                                                         break;
00362                                                                                 case "material":
00363                                                                                         $matlabel = $resp_lid_node->get_attribute("label");
00364                                                                                         $mattype = $resp_lid_node->first_child();
00365                                                                                         if (strcmp($mattype->node_name(), "mattext") == 0)
00366                                                                                         {
00367                                                                                                 $material = $mattype->get_content();
00368                                                                                                 if ($material)
00369                                                                                                 {
00370                                                                                                         if ($this->getId() < 1)
00371                                                                                                         {
00372                                                                                                                 $this->saveToDb();
00373                                                                                                         }
00374                                                                                                         $this->setMaterial($material, true, $matlabel);
00375                                                                                                 }
00376                                                                                         }
00377                                                                                         break;
00378                                                                         }
00379                                                                 }
00380                                                         }
00381                                                 }
00382                                                 break;
00383                                 }
00384                         }
00385                         $result = true;
00386                 }
00387                 return $result;
00388         }
00389 
00399         function to_xml($a_include_header = true, $obligatory_state = "")
00400         {
00401                 include_once("./classes/class.ilXmlWriter.php");
00402                 $a_xml_writer = new ilXmlWriter;
00403                 // set xml header
00404                 $a_xml_writer->xmlHeader();
00405                 $a_xml_writer->xmlStartTag("questestinterop");
00406                 $attrs = array(
00407                         "ident" => $this->getId(),
00408                         "title" => $this->getTitle()
00409                 );
00410                 $a_xml_writer->xmlStartTag("item", $attrs);
00411                 // add question description
00412                 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
00413                 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00414                 $a_xml_writer->xmlElement("qticomment", NULL, "Questiontype=".NOMINAL_QUESTION_IDENTIFIER);
00415                 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
00416                 // add ILIAS specific metadata
00417                 $a_xml_writer->xmlStartTag("itemmetadata");
00418                 $a_xml_writer->xmlStartTag("qtimetadata");
00419                 $a_xml_writer->xmlStartTag("qtimetadatafield");
00420                 $a_xml_writer->xmlElement("fieldlabel", NULL, "obligatory");
00421                 if (strcmp($obligatory_state, "") != 0)
00422                 {
00423                         $this->setObligatory($obligatory_state);
00424                 }
00425                 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getObligatory()));
00426                 $a_xml_writer->xmlEndTag("qtimetadatafield");
00427                 $a_xml_writer->xmlStartTag("qtimetadatafield");
00428                 $a_xml_writer->xmlElement("fieldlabel", NULL, "orientation");
00429                 $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("%d", $this->getOrientation()));
00430                 $a_xml_writer->xmlEndTag("qtimetadatafield");
00431                 $a_xml_writer->xmlEndTag("qtimetadata");
00432                 $a_xml_writer->xmlEndTag("itemmetadata");
00433 
00434                 // PART I: qti presentation
00435                 $attrs = array(
00436                         "label" => $this->getTitle()
00437                 );
00438                 $a_xml_writer->xmlStartTag("presentation", $attrs);
00439                 // add flow to presentation
00440                 $a_xml_writer->xmlStartTag("flow");
00441                 // add material with question text to presentation
00442                 $a_xml_writer->xmlStartTag("material");
00443                 $a_xml_writer->xmlElement("mattext", NULL, $this->getQuestiontext());
00444                 $a_xml_writer->xmlEndTag("material");
00445                 // add answers to presentation
00446                 $ident = "MCMR";
00447                 $rcardinality = "Multiple";
00448                 if ($this->getSubtype() == SUBTYPE_MCSR)
00449                 {
00450                         $ident = "MCSR";
00451                         $rcardinality = "Single";
00452                 }
00453                 $attrs = array(
00454                         "ident" => $ident,
00455                         "rcardinality" => $rcardinality
00456                 );
00457                 $a_xml_writer->xmlStartTag("response_lid", $attrs);
00458                 
00459                 if (count($this->material))
00460                 {
00461                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00462                         {
00463                                 $attrs = array(
00464                                         "label" => $this->material["title"]
00465                                 );
00466                                 $a_xml_writer->xmlStartTag("material", $attrs);
00467                                 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00468                                 if (strcmp($matches[1], "") != 0)
00469                                 {
00470                                         $intlink = $this->material["internal_link"];
00471                                 }
00472                                 $a_xml_writer->xmlElement("mattext", NULL, $intlink);
00473                                 $a_xml_writer->xmlEndTag("material");
00474                         }
00475                 }
00476 
00477                 $attrs = array(
00478                         "shuffle" => "no"
00479                 );
00480                 $a_xml_writer->xmlStartTag("render_choice", $attrs);
00481 
00482                 // add categories
00483                 for ($index = 0; $index < $this->categories->getCategoryCount(); $index++)
00484                 {
00485                         $category = $this->categories->getCategory($index);
00486                         $attrs = array(
00487                                 "ident" => "$index"
00488                         );
00489                         $a_xml_writer->xmlStartTag("response_label", $attrs);
00490                         $a_xml_writer->xmlStartTag("material");
00491                         $a_xml_writer->xmlElement("mattext", NULL, $category);
00492                         $a_xml_writer->xmlEndTag("material");
00493                         $a_xml_writer->xmlEndTag("response_label");
00494                 }
00495                 $a_xml_writer->xmlEndTag("render_choice");
00496                 $a_xml_writer->xmlEndTag("response_lid");
00497                 $a_xml_writer->xmlEndTag("flow");
00498                 $a_xml_writer->xmlEndTag("presentation");
00499                 $a_xml_writer->xmlEndTag("item");
00500                 $a_xml_writer->xmlEndTag("questestinterop");
00501 
00502                 $xml = $a_xml_writer->xmlDumpMem(FALSE);
00503                 if (!$a_include_header)
00504                 {
00505                         $pos = strpos($xml, "?>");
00506                         $xml = substr($xml, $pos + 2);
00507                 }
00508                 return $xml;
00509         }
00510         
00511         function syncWithOriginal()
00512         {
00513                 if ($this->original_id)
00514                 {
00515                         $complete = 0;
00516                         if ($this->isComplete()) 
00517                         {
00518                                 $complete = 1;
00519                         }
00520                         $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00521                                 $this->ilias->db->quote($this->title . ""),
00522                                 $this->ilias->db->quote($this->subtype . ""),
00523                                 $this->ilias->db->quote($this->description . ""),
00524                                 $this->ilias->db->quote($this->author . ""),
00525                                 $this->ilias->db->quote($this->questiontext . ""),
00526                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00527                                 $this->ilias->db->quote($complete . ""),
00528                                 $this->ilias->db->quote($this->original_id . "")
00529                         );
00530                         $result = $this->ilias->db->query($query);
00531                         if ($result == DB_OK) {
00532                                 // save categories
00533                                 
00534                                 // delete existing category relations
00535                                 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00536                                         $this->ilias->db->quote($this->original_id . "")
00537                                 );
00538                                 $result = $this->ilias->db->query($query);
00539 
00540                                 // create new category relations
00541                                 for ($i = 0; $i < $this->categories->getCategoryCount(); $i++)
00542                                 {
00543                                         $category_id = $this->saveCategoryToDb($this->categories->getCategory($i));
00544                                         $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00545                                                 $this->ilias->db->quote($category_id . ""),
00546                                                 $this->ilias->db->quote($this->original_id . ""),
00547                                                 $this->ilias->db->quote(($i + 1) . ""),
00548                                                 $this->ilias->db->quote($i . "")
00549                                         );
00550                                         $answer_result = $this->ilias->db->query($query);
00551                                 }
00552                         }
00553                 }
00554                 parent::syncWithOriginal();
00555         }
00556         
00565         function getQuestionType()
00566         {
00567                 return 1;
00568         }
00569 }
00570 ?>

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