• 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 require_once "./survey/classes/class.SurveyQuestion.php";
00025 
00026 define("SUBTYPE_MCSR", 1);
00027 define("SUBTYPE_MCMR", 2);
00028 
00029 define("NOMINAL_QUESTION_IDENTIFIER", "Nominal Question");
00030 
00042 class SurveyNominalQuestion extends SurveyQuestion {
00050   var $subtype;
00051 
00059   var $categories;
00060 
00072   function SurveyNominalQuestion(
00073     $title = "",
00074     $description = "",
00075     $author = "",
00076                 $questiontext = "",
00077     $owner = -1,
00078                 $subtype = 0
00079   )
00080 
00081   {
00082                 $this->SurveyQuestion($title, $description, $author, $questiontext, $owner);
00083                 $this->subtype = $subtype;
00084                 $this->categories = array();
00085         }
00086         
00096   function setSubtype($subtype = SUBTYPE_MCSR) 
00097         {
00098     $this->subtype = $subtype;
00099   }
00100 
00110   function getSubtype() 
00111         {
00112     return $this->subtype;
00113   }
00114         
00124         function getCategoryCount() 
00125         {
00126                 return count($this->categories);
00127         }
00128         
00139         function addCategoryWithIndex($categoryname, $index) 
00140         {
00141                 $this->categories[$index] = $categoryname;
00142         }
00143 
00153         function addCategory($categoryname) 
00154         {
00155                 array_push($this->categories, $categoryname);
00156         }
00157         
00167         function addCategoryArray($categories) 
00168         {
00169                 $this->categories = array_merge($this->categories, $categories);
00170         }
00171         
00181         function removeCategory($index)
00182         {
00183                 array_splice($this->categories, $index, 1);
00184         }
00185 
00195         function removeCategories($array)
00196         {
00197                 foreach ($array as $index)
00198                 {
00199                         unset($this->categories[$index]);
00200                 }
00201                 $this->categories = array_values($this->categories);
00202         }
00203 
00213         function removeCategoryWithName($name)
00214         {
00215                 $index = array_search($name, $this->categories);
00216                 $this->removeCategory($index);
00217         }
00218         
00229         function getCategory($index)
00230         {
00231                 return $this->categories[$index];
00232         }
00233         
00242         function flushCategories() {
00243                 $this->categories = array();
00244         }
00245         
00254   function loadFromDb($id) {
00255     $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
00256       $this->ilias->db->quote($id)
00257     );
00258     $result = $this->ilias->db->query($query);
00259     if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00260       if ($result->numRows() == 1) {
00261         $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00262         $this->id = $data->question_id;
00263         $this->title = $data->title;
00264         $this->description = $data->description;
00265         $this->obj_id = $data->obj_fi;
00266         $this->author = $data->author;
00267                                 $this->subtype = $data->subtype;
00268                                 $this->orientation = $data->orientation;
00269                                 $this->obligatory = $data->obligatory;
00270         $this->owner = $data->owner_fi;
00271         $this->questiontext = $data->questiontext;
00272         $this->complete = $data->complete;
00273                                 $this->original_id = $data->original_id;
00274       }
00275       // loads materials uris from database
00276       $this->loadMaterialFromDb($id);
00277 
00278                         $this->flushCategories();
00279       $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",
00280         $this->ilias->db->quote($id)
00281       );
00282       $result = $this->ilias->db->query($query);
00283       if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00284         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00285           array_push($this->categories, $data->title);
00286         }
00287       }
00288     }
00289                 parent::loadFromDb($id);
00290   }
00291 
00300         function isComplete()
00301         {
00302                 if ($this->title and $this->author and $this->questiontext and count($this->categories))
00303                 {
00304                         return 1;
00305                 }
00306                 else
00307                 {
00308                         return 0;
00309                 }
00310         }
00311         
00319   function saveToDb($original_id = "")
00320   {
00321                 $complete = 0;
00322                 if ($this->isComplete()) {
00323                         $complete = 1;
00324                 }
00325                 if ($original_id)
00326                 {
00327                         $original_id = $this->ilias->db->quote($original_id);
00328                 }
00329                 else
00330                 {
00331                         $original_id = "NULL";
00332                 }
00333     if ($this->id == -1) {
00334       // Write new dataset
00335       $now = getdate();
00336       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00337       $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)",
00338                                 $this->ilias->db->quote("$this->subtype"),
00339         $this->ilias->db->quote("1"),
00340         $this->ilias->db->quote($this->obj_id),
00341         $this->ilias->db->quote($this->owner),
00342         $this->ilias->db->quote($this->title),
00343         $this->ilias->db->quote($this->description),
00344         $this->ilias->db->quote($this->author),
00345         $this->ilias->db->quote($this->questiontext),
00346                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00347                                 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00348                                 $this->ilias->db->quote("$complete"),
00349         $this->ilias->db->quote($created),
00350                                 $original_id
00351       );
00352       $result = $this->ilias->db->query($query);
00353       if ($result == DB_OK) {
00354         $this->id = $this->ilias->db->getLastInsertId();
00355       }
00356     } else {
00357       // update existing dataset
00358       $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",
00359         $this->ilias->db->quote($this->title),
00360                                 $this->ilias->db->quote("$this->subtype"),
00361         $this->ilias->db->quote($this->description),
00362         $this->ilias->db->quote($this->author),
00363         $this->ilias->db->quote($this->questiontext),
00364                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory)),
00365                                 $this->ilias->db->quote(sprintf("%d", $this->orientation)),
00366                                 $this->ilias->db->quote("$complete"),
00367         $this->ilias->db->quote($this->id)
00368       );
00369       $result = $this->ilias->db->query($query);
00370     }
00371     if ($result == DB_OK) {
00372       // saving material uris in the database
00373       $this->saveMaterialsToDb();
00374 
00375       // save categories
00376                         
00377                         // delete existing category relations
00378       $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00379         $this->ilias->db->quote($this->id)
00380       );
00381       $result = $this->ilias->db->query($query);
00382       // create new category relations
00383       foreach ($this->categories as $key => $value) {
00384                                 $category_id = $this->saveCategoryToDb($value);
00385         $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00386                                         $this->ilias->db->quote($category_id),
00387           $this->ilias->db->quote($this->id),
00388           $this->ilias->db->quote(($key + 1)),
00389           $this->ilias->db->quote($key)
00390         );
00391         $answer_result = $this->ilias->db->query($query);
00392       }
00393     }
00394                 parent::saveToDb($original_id);
00395   }
00396 
00406         function from_xml($xml_text)
00407         {
00408                 $result = false;
00409                 if (!empty($this->domxml))
00410                 {
00411                         $this->domxml->free();
00412                 }
00413                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
00414                 $this->domxml = domxml_open_mem($xml_text);
00415                 if (!empty($this->domxml))
00416                 {
00417                         $root = $this->domxml->document_element();
00418                         $item = $root->first_child();
00419                         $this->setTitle($item->get_attribute("title"));
00420                         $this->gaps = array();
00421                         $itemnodes = $item->child_nodes();
00422                         foreach ($itemnodes as $index => $node)
00423                         {
00424                                 switch ($node->node_name())
00425                                 {
00426                                         case "qticomment":
00427                                                 $comment = $node->get_content();
00428                                                 if (strpos($comment, "ILIAS Version=") !== false)
00429                                                 {
00430                                                 }
00431                                                 elseif (strpos($comment, "Questiontype=") !== false)
00432                                                 {
00433                                                 }
00434                                                 elseif (strpos($comment, "Author=") !== false)
00435                                                 {
00436                                                         $comment = str_replace("Author=", "", $comment);
00437                                                         $this->setAuthor($comment);
00438                                                 }
00439                                                 else
00440                                                 {
00441                                                         $this->setDescription($comment);
00442                                                 }
00443                                                 break;
00444                                         case "itemmetadata":
00445                                                 $qtimetadata = $node->first_child();
00446                                                 $metadata_fields = $qtimetadata->child_nodes();
00447                                                 foreach ($metadata_fields as $index => $metadata_field)
00448                                                 {
00449                                                         $fieldlabel = $metadata_field->first_child();
00450                                                         $fieldentry = $fieldlabel->next_sibling();
00451                                                         switch ($fieldlabel->get_content())
00452                                                         {
00453                                                                 case "obligatory":
00454                                                                         $this->setObligatory($fieldentry->get_content());
00455                                                                         break;
00456                                                         }
00457                                                 }
00458                                                 break;
00459                                         case "presentation":
00460                                                 $flow = $node->first_child();
00461                                                 $flownodes = $flow->child_nodes();
00462                                                 foreach ($flownodes as $idx => $flownode)
00463                                                 {
00464                                                         if (strcmp($flownode->node_name(), "material") == 0)
00465                                                         {
00466                                                                 $mattext = $flownode->first_child();
00467                                                                 $this->setQuestiontext($mattext->get_content());
00468                                                         }
00469                                                         elseif (strcmp($flownode->node_name(), "response_lid") == 0)
00470                                                         {
00471                                                                 $ident = $flownode->get_attribute("ident");
00472                                                                 if (strcmp($ident, "MCSR") == 0)
00473                                                                 {
00474                                                                         $this->setSubtype(SUBTYPE_MCSR);
00475                                                                 }
00476                                                                 else
00477                                                                 {
00478                                                                         $this->setSubtype(SUBTYPE_MCMR);
00479                                                                 }
00480                                                                 $shuffle = "";
00481                                                                 $response_lid_nodes = $flownode->child_nodes();
00482                                                                 foreach ($response_lid_nodes as $resp_lid_id => $resp_lid_node)
00483                                                                 {
00484                                                                         switch ($resp_lid_node->node_name())
00485                                                                         {
00486                                                                                 case "render_choice":
00487                                                                                         $render_choice = $resp_lid_node;
00488                                                                                         $labels = $render_choice->child_nodes();
00489                                                                                         foreach ($labels as $lidx => $response_label)
00490                                                                                         {
00491                                                                                                 $material = $response_label->first_child();
00492                                                                                                 $mattext = $material->first_child();
00493                                                                                                 $shuf = 0;
00494                                                                                                 $this->addCategoryWithIndex($mattext->get_content(), $response_label->get_attribute("ident"));
00495                                                                                         }
00496                                                                                         break;
00497                                                                                 case "material":
00498                                                                                         $matlabel = $resp_lid_node->get_attribute("label");
00499                                                                                         $mattype = $resp_lid_node->first_child();
00500                                                                                         if (strcmp($mattype->node_name(), "mattext") == 0)
00501                                                                                         {
00502                                                                                                 $material = $mattype->get_content();
00503                                                                                                 if ($material)
00504                                                                                                 {
00505                                                                                                         if ($this->getId() < 1)
00506                                                                                                         {
00507                                                                                                                 $this->saveToDb();
00508                                                                                                         }
00509                                                                                                         $this->setMaterial($material, true, $matlabel);
00510                                                                                                 }
00511                                                                                         }
00512                                                                                         break;
00513                                                                         }
00514                                                                 }
00515                                                         }
00516                                                 }
00517                                                 break;
00518                                 }
00519                         }
00520                         $result = true;
00521                 }
00522                 return $result;
00523         }
00524 
00534         function to_xml($a_include_header = true, $obligatory_state = "")
00535         {
00536                 if (!empty($this->domxml))
00537                 {
00538                         $this->domxml->free();
00539                 }
00540                 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00541                 $xml_header .= "<questestinterop></questestinterop>\n";
00542                 $this->domxml = domxml_open_mem($xml_header);
00543                 $root = $this->domxml->document_element();
00544                 // qti ident
00545                 $qtiIdent = $this->domxml->create_element("item");
00546                 $qtiIdent->set_attribute("ident", $this->getId());
00547                 $qtiIdent->set_attribute("title", $this->getTitle());
00548                 $root->append_child($qtiIdent);
00549                 // add qti comment
00550                 $qtiComment = $this->domxml->create_element("qticomment");
00551                 $qtiCommentText = $this->domxml->create_text_node($this->getDescription());
00552                 $qtiComment->append_child($qtiCommentText);
00553                 $qtiIdent->append_child($qtiComment);
00554                 $qtiComment = $this->domxml->create_element("qticomment");
00555                 $qtiCommentText = $this->domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
00556                 $qtiComment->append_child($qtiCommentText);
00557                 $qtiIdent->append_child($qtiComment);
00558                 $qtiComment = $this->domxml->create_element("qticomment");
00559                 $qtiCommentText = $this->domxml->create_text_node("Questiontype=".NOMINAL_QUESTION_IDENTIFIER);
00560                 $qtiComment->append_child($qtiCommentText);
00561                 $qtiIdent->append_child($qtiComment);
00562                 $qtiComment = $this->domxml->create_element("qticomment");
00563                 $qtiCommentText = $this->domxml->create_text_node("Author=".$this->getAuthor());
00564                 $qtiComment->append_child($qtiCommentText);
00565                 $qtiIdent->append_child($qtiComment);
00566 
00567                 $qtiItemMetadata = $this->domxml->create_element("itemmetadata");
00568                 $qtiMetadata = $this->domxml->create_element("qtimetadata");
00569                 // obligatory state
00570                 $qtiMetadatafield = $this->domxml->create_element("qtimetadatafield");
00571                 $qtiFieldLabel = $this->domxml->create_element("fieldlabel");
00572                 $qtiFieldLabelText = $this->domxml->create_text_node("obligatory");
00573                 $qtiFieldLabel->append_child($qtiFieldLabelText);
00574                 $qtiFieldEntry = $this->domxml->create_element("fieldentry");
00575                 if (strcmp($obligatory_state, "") != 0)
00576                 {
00577                         $this->setObligatory($obligatory_state);
00578                 }
00579                 $qtiFieldEntryText = $this->domxml->create_text_node(sprintf("%d", $this->getObligatory()));
00580                 $qtiFieldEntry->append_child($qtiFieldEntryText);
00581                 $qtiMetadatafield->append_child($qtiFieldLabel);
00582                 $qtiMetadatafield->append_child($qtiFieldEntry);
00583                 $qtiMetadata->append_child($qtiMetadatafield);
00584                 $qtiItemMetadata->append_child($qtiMetadata);
00585                 $qtiIdent->append_child($qtiItemMetadata);
00586 
00587                 // PART I: qti presentation
00588                 $qtiPresentation = $this->domxml->create_element("presentation");
00589                 $qtiPresentation->set_attribute("label", $this->getTitle());
00590                 // add flow to presentation
00591                 $qtiFlow = $this->domxml->create_element("flow");
00592                 // add material with question text to presentation
00593                 $qtiMaterial = $this->domxml->create_element("material");
00594                 $qtiMatText = $this->domxml->create_element("mattext");
00595                 $qtiMatTextText = $this->domxml->create_text_node($this->getQuestiontext());
00596                 $qtiMatText->append_child($qtiMatTextText);
00597                 $qtiMaterial->append_child($qtiMatText);
00598                 $qtiFlow->append_child($qtiMaterial);
00599                 // add answers to presentation
00600                 $qtiResponseLid = $this->domxml->create_element("response_lid");
00601                 if ($this->getSubtype() == SUBTYPE_MCSR)
00602                 {
00603                         $qtiResponseLid->set_attribute("ident", "MCSR");
00604                         $qtiResponseLid->set_attribute("rcardinality", "Single");
00605                 }
00606                         else
00607                 {
00608                         $qtiResponseLid->set_attribute("ident", "MCMR");
00609                         $qtiResponseLid->set_attribute("rcardinality", "Multiple");
00610                 }
00611 
00612                 if (count($this->material))
00613                 {
00614                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
00615                         {
00616                                 $qtiMaterial = $this->domxml->create_element("material");
00617                                 $qtiMaterial->set_attribute("label", $this->material["title"]);
00618                                 $qtiMatText = $this->domxml->create_element("mattext");
00619                                 $intlink = "il_" . IL_INST_ID . "_" . $matches[2] . "_" . $matches[3];
00620                                 if (strcmp($matches[1], "") != 0)
00621                                 {
00622                                         $intlink = $this->material["internal_link"];
00623                                 }
00624                                 $qtiMatTextText = $this->domxml->create_text_node($intlink);
00625                                 $qtiMatText->append_child($qtiMatTextText);
00626                                 $qtiMaterial->append_child($qtiMatText);
00627                                 $qtiResponseLid->append_child($qtiMaterial);
00628                         }
00629                 }
00630 
00631                 $qtiRenderChoice = $this->domxml->create_element("render_choice");
00632                 $qtiRenderChoice->set_attribute("shuffle", "no");
00633 
00634                 // add categories
00635                 for ($index = 0; $index < $this->getCategoryCount(); $index++)
00636                 {
00637                         $category = $this->getCategory($index);
00638                         $qtiResponseLabel = $this->domxml->create_element("response_label");
00639                         $qtiResponseLabel->set_attribute("ident", $index);
00640                         $qtiMaterial = $this->domxml->create_element("material");
00641                         $qtiMatText = $this->domxml->create_element("mattext");
00642                         $qtiMatTextText = $this->domxml->create_text_node($category);
00643                         $qtiMatText->append_child($qtiMatTextText);
00644                         $qtiMaterial->append_child($qtiMatText);
00645                         $qtiResponseLabel->append_child($qtiMaterial);
00646                         $qtiRenderChoice->append_child($qtiResponseLabel);
00647                 }
00648                 $qtiResponseLid->append_child($qtiRenderChoice);
00649                 $qtiFlow->append_child($qtiResponseLid);
00650                 $qtiPresentation->append_child($qtiFlow);
00651                 $qtiIdent->append_child($qtiPresentation);
00652                 $xml = $this->domxml->dump_mem(true);
00653                 if (!$a_include_header)
00654                 {
00655                         $pos = strpos($xml, "?>");
00656                         $xml = substr($xml, $pos + 2);
00657                 }
00658 //echo htmlentities($xml);
00659                 return $xml;
00660         }
00661         
00662         function syncWithOriginal()
00663         {
00664                 if ($this->original_id)
00665                 {
00666                         $complete = 0;
00667                         if ($this->isComplete()) {
00668                                 $complete = 1;
00669                         }
00670                         $query = sprintf("UPDATE survey_question SET title = %s, subtype = %s, description = %s, author = %s, questiontext = %s, obligatory = %s, complete = %s WHERE question_id = %s",
00671                                 $this->ilias->db->quote($this->title . ""),
00672                                 $this->ilias->db->quote($this->subtype . ""),
00673                                 $this->ilias->db->quote($this->description . ""),
00674                                 $this->ilias->db->quote($this->author . ""),
00675                                 $this->ilias->db->quote($this->questiontext . ""),
00676                                 $this->ilias->db->quote(sprintf("%d", $this->obligatory) . ""),
00677                                 $this->ilias->db->quote($complete . ""),
00678                                 $this->ilias->db->quote($this->original_id . "")
00679                         );
00680                         $result = $this->ilias->db->query($query);
00681                         if ($result == DB_OK) {
00682                                 // save categories
00683                                 
00684                                 // delete existing category relations
00685                                 $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
00686                                         $this->ilias->db->quote($this->original_id . "")
00687                                 );
00688                                 $result = $this->ilias->db->query($query);
00689                                 // create new category relations
00690                                 foreach ($this->categories as $key => $value) {
00691                                         $category_id = $this->saveCategoryToDb($value);
00692                                         $query = sprintf("INSERT INTO survey_variable (variable_id, category_fi, question_fi, value1, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00693                                                 $this->ilias->db->quote($category_id . ""),
00694                                                 $this->ilias->db->quote($this->original_id . ""),
00695                                                 $this->ilias->db->quote(($key + 1) . ""),
00696                                                 $this->ilias->db->quote($key . "")
00697                                         );
00698                                         $answer_result = $this->ilias->db->query($query);
00699                                 }
00700                         }
00701                 }
00702                 parent::syncWithOriginal();
00703         }
00704         
00705 }
00706 ?>

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