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

Modules/Survey/classes/class.ilObjSurvey.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 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 
00034 include_once "./classes/class.ilObject.php";
00035 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
00036 
00037 class ilObjSurvey extends ilObject
00038 {
00047   var $survey_id;
00048 
00057   var $author;
00058 
00066   var $introduction;
00067 
00075   var $outro;
00076 
00084   var $status;
00085 
00093   var $evaluation_access;
00094 
00102   var $start_date;
00103 
00111         var $startdate_enabled;
00112 
00120   var $end_date;
00121 
00129         var $enddate_enabled;
00130 
00138         var $questions;
00139 
00147         var $invitation;
00148 
00156         var $invitation_mode;
00157         
00164         var $anonymize;
00165 
00172         var $display_question_titles;
00173 
00179         var $surveyCodeSecurity;
00180 
00187         function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
00188         {
00189                 global $ilUser;
00190                 $this->type = "svy";
00191                 $this->ilObject($a_id,$a_call_by_reference);
00192 
00193                 $this->survey_id = -1;
00194                 $this->introduction = "";
00195                 $this->outro = $this->lng->txt("survey_finished");
00196                 $this->author = $ilUser->fullname;
00197                 $this->status = STATUS_OFFLINE;
00198                 $this->evaluation_access = EVALUATION_ACCESS_OFF;
00199                 $this->startdate_enabled = 0;
00200                 $this->enddate_enabled = 0;
00201                 $this->questions = array();
00202                 $this->invitation = INVITATION_OFF;
00203                 $this->invitation_mode = MODE_PREDEFINED_USERS;
00204                 $this->anonymize = ANONYMIZE_OFF;
00205                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
00206                 $this->surveyCodeSecurity = TRUE;
00207         }
00208 
00212         function create($a_upload = false)
00213         {
00214                 parent::create();
00215                 if(!$a_upload)
00216                 {
00217                         $this->createMetaData();
00218                 }
00219         }
00220 
00228         function createMetaData()
00229         {
00230                 parent::createMetaData();
00231                 $this->saveAuthorToMetadata();
00232         }
00233 
00240         function update()
00241         {
00242                 $this->updateMetaData();
00243 
00244                 if (!parent::update())
00245                 {
00246                         return false;
00247                 }
00248 
00249                 // put here object specific stuff
00250 
00251                 return true;
00252         }
00253 
00254         function createReference() 
00255         {
00256                 $result = parent::createReference();
00257                 $this->saveToDb();
00258                 return $result;
00259         }
00260 
00266         function read($a_force_db = false)
00267         {
00268                 parent::read($a_force_db);
00269                 $this->loadFromDb();
00270         }
00271         
00278         function addQuestion($question_id)
00279         {
00280                 array_push($this->questions, $question_id);
00281         }
00282 
00283 
00290         function delete()
00291         {
00292                 $remove = parent::delete();
00293                 // always call parent delete function first!!
00294                 if (!$remove)
00295                 {
00296                         return false;
00297                 }
00298 
00299                 $this->deleteMetaData();
00300 
00301                 // Delete all survey questions, constraints and materials
00302                 foreach ($this->questions as $question_id)
00303                 {
00304                         $this->removeQuestion($question_id);
00305                 }
00306                 $this->deleteSurveyRecord();
00307                 
00308                 ilUtil::delDir($this->getImportDirectory());
00309                 return true;
00310         }
00311         
00319         function deleteSurveyRecord()
00320         {
00321                 global $ilDB;
00322                 
00323                 $query = sprintf("DELETE FROM survey_survey WHERE survey_id = %s",
00324                         $ilDB->quote($this->getSurveyId())
00325                 );
00326                 $result = $ilDB->query($query);
00327 
00328                 $query = sprintf("SELECT questionblock_fi FROM survey_questionblock_question WHERE survey_fi = %s",
00329                         $ilDB->quote($this->getSurveyId())
00330                 );
00331                 $result = $ilDB->query($query);
00332                 $questionblocks = array();
00333                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00334                 {
00335                         array_push($questionblocks, $row["questionblock_fi"]);
00336                 }
00337                 if (count($questionblocks))
00338                 {
00339                         $query = "DELETE FROM survey_questionblock WHERE questionblock_id IN ('" . join($questionblocks, "','") . "')";
00340                         $result = $ilDB->query($query);
00341                 }
00342                 $query = sprintf("DELETE FROM survey_questionblock_question WHERE survey_fi = %s",
00343                         $ilDB->quote($this->getSurveyId())
00344                 );
00345                 $result = $ilDB->query($query);
00346                 
00347                 $this->deleteAllUserData();
00348 
00349                 $query = sprintf("DELETE FROM survey_anonymous WHERE survey_fi = %s",
00350                         $ilDB->quote($this->getSurveyId())
00351                 );
00352                 $result = $ilDB->query($query);
00353                 
00354                 // delete export files
00355                 include_once "./Services/Utilities/classes/class.ilUtil.php";
00356                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
00357                 $directory = $svy_data_dir."/svy_".$this->getId();
00358                 if (is_dir($directory))
00359                 {
00360                         include_once "./Services/Utilities/classes/class.ilUtil.php";
00361                         ilUtil::delDir($directory);
00362                 }
00363 
00364                 include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
00365                 $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
00366                 // remaining usages are not in text anymore -> delete them
00367                 // and media objects (note: delete method of ilObjMediaObject
00368                 // checks whether object is used in another context; if yes,
00369                 // the object is not deleted!)
00370                 foreach($mobs as $mob)
00371                 {
00372                         ilObjMediaObject::_removeUsage($mob, "svy:html", $this->getId());
00373                         $mob_obj =& new ilObjMediaObject($mob);
00374                         $mob_obj->delete();
00375                 }
00376         }
00377         
00385         function deleteAllUserData()
00386         {
00387                 global $ilDB;
00388                 
00389                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
00390                         $ilDB->quote($this->getSurveyId())
00391                 );
00392                 $result = $ilDB->query($query);
00393                 $active_array = array();
00394                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00395                 {
00396                         array_push($active_array, $row["finished_id"]);
00397                 }
00398 
00399                 $query = sprintf("DELETE FROM survey_finished WHERE survey_fi = %s",
00400                         $ilDB->quote($this->getSurveyId())
00401                 );
00402                 $result = $ilDB->query($query);
00403 
00404                 foreach ($active_array as $active_fi)
00405                 {
00406                         $query = sprintf("DELETE FROM survey_answer WHERE active_fi = %s",
00407                                 $ilDB->quote($active_fi)
00408                         );
00409                         $result = $ilDB->query($query);
00410                 }
00411         }
00412         
00420         function removeSelectedSurveyResults($finished_ids)
00421         {
00422                 global $ilDB;
00423                 
00424                 foreach ($finished_ids as $finished_id)
00425                 {
00426                         $query = sprintf("SELECT finished_id FROM survey_finished WHERE finished_id = %s",
00427                                 $ilDB->quote($finished_id . "")
00428                         );
00429                         $result = $ilDB->query($query);
00430                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00431 
00432                         $query = sprintf("DELETE FROM survey_answer WHERE active_fi = %s",
00433                                 $ilDB->quote($row["finished_id"] . "")
00434                         );
00435                         $result = $ilDB->query($query);
00436 
00437                         $query = sprintf("DELETE FROM survey_finished WHERE finished_id = %s",
00438                                 $ilDB->quote($finished_id . "")
00439                         );
00440                         $result = $ilDB->query($query);
00441                 }
00442         }
00443         
00444         function &getSurveyParticipants()
00445         {
00446                 global $ilDB;
00447                 
00448                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
00449                         $ilDB->quote($this->getSurveyId() . "")
00450                 );
00451                 $result = $ilDB->query($query);
00452                 $participants = array();
00453                 if ($result->numRows() > 0)
00454                 {
00455                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00456                         {
00457                                 $userdata = $this->getUserDataFromActiveId($row["finished_id"]);
00458                                 $participants[$userdata["sortname"] . $userdata["active_id"]] = $userdata;
00459                         }
00460                 }
00461                 return $participants;
00462         }
00463 
00477         function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
00478         {
00479                 global $tree;
00480                 
00481                 switch ($a_event)
00482                 {
00483                         case "link":
00484                                 
00485                                 //var_dump("<pre>",$a_params,"</pre>");
00486                                 //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
00487                                 //exit;
00488                                 break;
00489                         
00490                         case "cut":
00491                                 
00492                                 //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
00493                                 //exit;
00494                                 break;
00495                                 
00496                         case "copy":
00497                         
00498                                 //var_dump("<pre>",$a_params,"</pre>");
00499                                 //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
00500                                 //exit;
00501                                 break;
00502 
00503                         case "paste":
00504                                 
00505                                 //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
00506                                 //exit;
00507                                 break;
00508                         
00509                         case "new":
00510                                 
00511                                 //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
00512                                 //exit;
00513                                 break;
00514                 }
00515                 
00516                 // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
00517                 if ($a_node_id==$_GET["ref_id"])
00518                 {       
00519                         $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
00520                         $parent_type = $parent_obj->getType();
00521                         if($parent_type == $this->getType())
00522                         {
00523                                 $a_node_id = (int) $tree->getParentId($a_node_id);
00524                         }
00525                 }
00526                 
00527                 parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
00528         }
00529 
00538         function isComplete()
00539         {
00540                 if (($this->getTitle()) and ($this->getAuthor()) and (count($this->questions)))
00541                 {
00542                         return true;
00543                 } 
00544                         else 
00545                 {
00546                         return false;
00547                 }
00548         }
00549 
00558         function _isComplete($obj_id)
00559         {
00560                 $survey = new ilObjSurvey($obj_id, false);
00561                 $survey->loadFromDb();
00562                 if (($survey->getTitle()) and ($survey->getAuthor()) and (count($survey->questions)))
00563                 {
00564                         return true;
00565                 } 
00566                         else 
00567                 {
00568                         return false;
00569                 }
00570         }
00571 
00580         function &_getGlobalSurveyData($obj_id)
00581         {
00582                 $survey = new ilObjSurvey($obj_id, false);
00583                 $survey->loadFromDb();
00584                 $result = array();
00585                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00586                 {
00587                         $result["complete"] = true;
00588                 } 
00589                         else 
00590                 {
00591                         $result["complete"] = false;
00592                 }
00593                 $result["evaluation_access"] = $survey->getEvaluationAccess();
00594                 return $result;
00595         }
00596 
00604         function saveCompletionStatus() 
00605         {
00606                 global $ilDB;
00607                 
00608                 $complete = 0;
00609                 if ($this->isComplete()) 
00610                 {
00611                         $complete = 1;
00612                 }
00613     if ($this->survey_id > 0) 
00614                 {
00615                         $query = sprintf("UPDATE survey_survey SET complete = %s WHERE survey_id = %s",
00616                                 $ilDB->quote("$complete"),
00617                                 $ilDB->quote($this->survey_id) 
00618                         );
00619       $result = $ilDB->query($query);
00620                 }
00621         }
00622 
00632         function duplicateQuestionForSurvey($question_id)
00633         {
00634                 global $ilUser;
00635                 
00636                 $questiontype = $this->getQuestionType($question_id);
00637                 $question_gui = $this->getQuestionGUI($questiontype, $question_id);
00638                 $duplicate_id = $question_gui->object->duplicate(true);
00639                 return $duplicate_id;
00640         }
00641 
00649         function insertQuestion($question_id) 
00650         {
00651                 global $ilDB;
00652                 
00653                 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
00654                 if (!SurveyQuestion::_isComplete($question_id))
00655                 {
00656                         return FALSE;
00657                 }
00658                 else
00659                 {
00660                         // get maximum sequence index in test
00661                         $query = sprintf("SELECT survey_question_id FROM survey_survey_question WHERE survey_fi = %s",
00662                                 $ilDB->quote($this->getSurveyId())
00663                         );
00664                         $result = $ilDB->query($query);
00665                         $sequence = $result->numRows();
00666                         $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
00667                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00668                                 $ilDB->quote($this->getSurveyId()),
00669                                 $ilDB->quote($duplicate_id),
00670                                 $ilDB->quote($sequence)
00671                         );
00672                         $result = $ilDB->query($query);
00673                         if ($result != DB_OK) 
00674                         {
00675                                 // Error
00676                         }
00677                         $this->loadQuestionsFromDb();
00678                         return TRUE;
00679                 }
00680         }
00681 
00682 
00683         
00691         function insertQuestionblock($questionblock_id) 
00692         {
00693                 global $ilDB;
00694                 $query = sprintf("SELECT survey_questionblock.title, survey_questionblock.show_questiontext, survey_questionblock_question.question_fi FROM survey_questionblock, survey_questionblock_question, survey_survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_survey_question.question_fi = survey_questionblock_question.question_fi AND survey_questionblock.questionblock_id = %s ORDER BY survey_survey_question.sequence",
00695                         $ilDB->quote($questionblock_id)
00696                 );
00697                 $result = $ilDB->query($query);
00698                 $questions = array();
00699                 $show_questiontext = 0;
00700                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00701                 {
00702                         $duplicate_id = $this->duplicateQuestionForSurvey($row["question_fi"]);
00703                         array_push($questions, $duplicate_id);
00704                         $title = $row["title"];
00705                         $show_questiontext = $row["show_questiontext"];
00706                 }
00707                 $this->createQuestionblock($title, $show_questiontext, $questions);
00708         }
00709         
00717         function getAllRTEContent()
00718         {
00719                 $result = array();
00720                 array_push($result, $this->getIntroduction());
00721                 array_push($result, $this->getOutro());
00722                 return $result;
00723         }
00724         
00732         function cleanupMediaobjectUsage()
00733         {
00734                 include_once("./Services/RTE/classes/class.ilRTE.php");
00735                 $completecontent = "";
00736                 foreach ($this->getAllRTEContent() as $content)
00737                 {
00738                         $completecontent .= $content;
00739                 }
00740                 ilRTE::_cleanupMediaObjectUsage($completecontent, $this->getType() . ":html",
00741                         $this->getId());
00742         }
00743 
00751   function saveToDb()
00752   {
00753                 global $ilDB;
00754                 $complete = 0;
00755                 if ($this->isComplete()) 
00756                 {
00757                         $complete = 1;
00758                 }
00759                 $startdate = $this->getStartDate();
00760                 if (!$startdate or !$this->startdate_enabled)
00761                 {
00762                         $startdate = "NULL";
00763                 }
00764                 else
00765                 {
00766                         $startdate = $ilDB->quote($startdate);
00767                 }
00768                 $enddate = $this->getEndDate();
00769                 if (!$enddate or !$this->enddate_enabled)
00770                 {
00771                         $enddate = "NULL";
00772                 }
00773                 else
00774                 {
00775                         $enddate = $ilDB->quote($enddate);
00776                 }
00777 
00778                 // cleanup RTE images
00779                 $this->cleanupMediaobjectUsage();
00780 
00781     if ($this->survey_id == -1) 
00782                 {
00783       // Write new dataset
00784       $now = getdate();
00785       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00786       $query = sprintf("INSERT INTO survey_survey (survey_id, obj_fi, author, introduction, outro, status, startdate, enddate, evaluation_access, invitation, invitation_mode, complete, created, anonymize, show_question_titles, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00787                                 $ilDB->quote($this->getId()),
00788                                 $ilDB->quote($this->author . ""),
00789                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->introduction, 0)),
00790                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
00791                                 $ilDB->quote($this->status . ""),
00792                                 $startdate,
00793                                 $enddate,
00794                                 $ilDB->quote($this->evaluation_access . ""),
00795                                 $ilDB->quote($this->invitation . ""),
00796                                 $ilDB->quote($this->invitation_mode . ""),
00797                                 $ilDB->quote($complete . ""),
00798                                 $ilDB->quote($this->getAnonymize() . ""),
00799                                 $ilDB->quote($this->getShowQuestionTitles() . ""),
00800                                 $ilDB->quote($created)
00801       );
00802       $result = $ilDB->query($query);
00803       if ($result == DB_OK) 
00804                         {
00805         $this->survey_id = $ilDB->getLastInsertId();
00806       }
00807     } 
00808                 else 
00809                 {
00810       // update existing dataset
00811                         $query = sprintf("UPDATE survey_survey SET author = %s, introduction = %s, outro = %s, status = %s, startdate = %s, enddate = %s, evaluation_access = %s, invitation = %s, invitation_mode = %s, complete = %s, anonymize = %s, show_question_titles = %s WHERE survey_id = %s",
00812                                 $ilDB->quote($this->author . ""),
00813                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->introduction, 0)),
00814                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
00815                                 $ilDB->quote($this->status . ""),
00816                                 $startdate,
00817                                 $enddate,
00818                                 $ilDB->quote($this->evaluation_access . ""),
00819                                 $ilDB->quote($this->invitation . ""),
00820                                 $ilDB->quote($this->invitation_mode . ""),
00821                                 $ilDB->quote($complete . ""),
00822                                 $ilDB->quote($this->getAnonymize() . ""),
00823                                 $ilDB->quote($this->getShowQuestionTitles() . ""),
00824                                 $ilDB->quote($this->survey_id)
00825       );
00826       $result = $ilDB->query($query);
00827     }
00828     if ($result == DB_OK) 
00829                 {
00830                         // save questions to db
00831                         $this->saveQuestionsToDb();
00832     }
00833   }
00834 
00843         function saveQuestionsToDb() 
00844         {
00845                 global $ilDB;
00846                 // save old questions state
00847                 $old_questions = array();
00848                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
00849                         $ilDB->quote($this->getSurveyId())
00850                 );
00851                 $result = $ilDB->query($query);
00852                 if ($result->numRows())
00853                 {
00854                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00855                         {
00856                                 $old_questions[$row["question_fi"]] = $row;
00857                         }
00858                 }
00859                 
00860                 // delete existing question relations
00861     $query = sprintf("DELETE FROM survey_survey_question WHERE survey_fi = %s",
00862                         $ilDB->quote($this->getSurveyId())
00863                 );
00864                 $result = $ilDB->query($query);
00865                 // create new question relations
00866                 foreach ($this->questions as $key => $value) 
00867                 {
00868                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, heading, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00869                                 $ilDB->quote($this->getSurveyId() . ""),
00870                                 $ilDB->quote($value . ""),
00871                                 $ilDB->quote($old_questions[$value]["heading"]),
00872                                 $ilDB->quote($key . "")
00873                         );
00874                         $result = $ilDB->query($query);
00875                 }
00876         }
00877 
00887         function getAnonymousId($id)
00888         {
00889                 global $ilDB;
00890                 $query = sprintf("SELECT anonymous_id FROM survey_finished WHERE anonymous_id = %s",
00891                         $ilDB->quote($id)
00892                 );
00893                 $result = $ilDB->query($query);
00894                 if ($result->numRows())
00895                 {
00896                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00897                         return $row["anonymous_id"];
00898                 }
00899                 else
00900                 {
00901                         return "";
00902                 }
00903         }
00904 
00913         function getQuestionGUI($questiontype, $question_id)
00914         {
00915                 $questiontypegui = $questiontype . "GUI";
00916                 include_once "./Modules/SurveyQuestionPool/classes/class.$questiontypegui.php";
00917                 $question = new $questiontypegui();
00918                 $question->object->loadFromDb($question_id);
00919                 return $question;
00920         }
00921         
00931   function getQuestionType($question_id) 
00932         {
00933                 global $ilDB;
00934     if ($question_id < 1) return -1;
00935     $query = sprintf("SELECT type_tag FROM survey_question, survey_questiontype WHERE survey_question.question_id = %s AND survey_question.questiontype_fi = survey_questiontype.questiontype_id",
00936       $ilDB->quote($question_id)
00937     );
00938     $result = $ilDB->query($query);
00939     if ($result->numRows() == 1) 
00940                 {
00941       $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00942       return $data->type_tag;
00943     } 
00944                 else 
00945                 {
00946       return "";
00947     }
00948   }
00949         
00958         function getSurveyId()
00959         {
00960                 return $this->survey_id;
00961         }
00962         
00966         function setAnonymize($a_anonymize)
00967         {
00968                 $this->anonymize = $a_anonymize;
00969         }
00970 
00976         function getAnonymize()
00977         {
00978                 return $this->anonymize;
00979         }
00980         
00986         function isAccessibleWithoutCode()
00987         {
00988                 if ($this->getAnonymize() == ANONYMIZE_FREEACCESS)
00989                 {
00990                         return true;
00991                 }
00992                 else
00993                 {
00994                         return false;
00995                 }
00996         }
00997 
01005   function loadFromDb()
01006   {
01007                 global $ilDB;
01008     $query = sprintf("SELECT * FROM survey_survey WHERE obj_fi = %s",
01009       $ilDB->quote($this->getId())
01010     );
01011     $result = $ilDB->query($query);
01012     if (strcmp(strtolower(get_class($result)), db_result) == 0) 
01013                 {
01014       if ($result->numRows() == 1) 
01015                         {
01016                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
01017                                 $this->survey_id = $data->survey_id;
01018                                 if (strlen($this->getAuthor()) == 0)
01019                                 {
01020                                         $this->saveAuthorToMetadata($data->author);
01021                                 }
01022                                 $this->author = $this->getAuthor();
01023                                 include_once("./Services/RTE/classes/class.ilRTE.php");
01024                                 $this->introduction = ilRTE::_replaceMediaObjectImageSrc($data->introduction, 1);
01025                                 if (strcmp($data->outro, "survey_finished") == 0)
01026                                 {
01027                                         $this->setOutro($this->lng->txt("survey_finished"));
01028                                 }
01029                                 else
01030                                 {
01031                                         $this->setOutro(ilRTE::_replaceMediaObjectImageSrc($data->outro, 1));
01032                                 }
01033                                 $this->status = $data->status;
01034                                 $this->invitation = $data->invitation;
01035                                 $this->invitation_mode = $data->invitation_mode;
01036                                 $this->display_question_titles = $data->show_question_titles;
01037                                 $this->start_date = $data->startdate;
01038                                 if (!$data->startdate)
01039                                 {
01040                                         $this->startdate_enabled = 0;
01041                                 }
01042                                 else
01043                                 {
01044                                         $this->startdate_enabled = 1;
01045                                 }
01046         $this->end_date = $data->enddate;
01047                                 if (!$data->enddate)
01048                                 {
01049                                         $this->enddate_enabled = 0;
01050                                 }
01051                                 else
01052                                 {
01053                                         $this->enddate_enabled = 1;
01054                                 }
01055                                 switch ($data->anonymize)
01056                                 {
01057                                         case ANONYMIZE_OFF:
01058                                         case ANONYMIZE_ON:
01059                                         case ANONYMIZE_FREEACCESS:
01060                                                 $this->setAnonymize($data->anonymize);
01061                                                 break;
01062                                         default:
01063                                                 $this->setAnonymize(ANONYMIZE_OFF);
01064                                                 break;
01065                                 }
01066         $this->evaluation_access = $data->evaluation_access;
01067                                 $this->loadQuestionsFromDb();
01068       }
01069     }
01070         }
01071 
01080         function loadQuestionsFromDb() 
01081         {
01082                 global $ilDB;
01083                 $this->questions = array();
01084                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
01085                         $ilDB->quote($this->survey_id)
01086                 );
01087                 $result = $ilDB->query($query);
01088                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
01089                 {
01090                         $this->questions[$data->sequence] = $data->question_fi;
01091                 }
01092         }
01093 
01103         function setStartDateEnabled($enabled = false)
01104         {
01105                 if ($enabled)
01106                 {
01107                         $this->startdate_enabled = 1;
01108                 }
01109                 else
01110                 {
01111                         $this->startdate_enabled = 0;
01112                 }
01113         }
01114         
01124         function getStartDateEnabled()
01125         {
01126                 return $this->startdate_enabled;
01127         }
01128 
01138         function setEndDateEnabled($enabled = false)
01139         {
01140                 if ($enabled)
01141                 {
01142                         $this->enddate_enabled = 1;
01143                 }
01144                 else
01145                 {
01146                         $this->enddate_enabled = 0;
01147                 }
01148         }
01149         
01159         function getEndDateEnabled()
01160         {
01161                 return $this->enddate_enabled;
01162         }
01163 
01173   function setAuthor($author = "") 
01174         {
01175     $this->author = $author;
01176   }
01177 
01189         function saveAuthorToMetadata($a_author = "")
01190         {
01191                 $md =& new ilMD($this->getId(), 0, $this->getType());
01192                 $md_life =& $md->getLifecycle();
01193                 if (!$md_life)
01194                 {
01195                         if (strlen($a_author) == 0)
01196                         {
01197                                 global $ilUser;
01198                                 $a_author = $ilUser->getFullname();
01199                         }
01200                         
01201                         $md_life =& $md->addLifecycle();
01202                         $md_life->save();
01203                         $con =& $md_life->addContribute();
01204                         $con->setRole("Author");
01205                         $con->save();
01206                         $ent =& $con->addEntity();
01207                         $ent->setEntity($a_author);
01208                         $ent->save();
01209                 }
01210         }
01211         
01221   function getAuthor() 
01222         {
01223                 $author = array();
01224                 include_once "./Services/MetaData/classes/class.ilMD.php";
01225                 $md =& new ilMD($this->getId(), 0, $this->getType());
01226                 $md_life =& $md->getLifecycle();
01227                 if ($md_life)
01228                 {
01229                         $ids =& $md_life->getContributeIds();
01230                         foreach ($ids as $id)
01231                         {
01232                                 $md_cont =& $md_life->getContribute($id);
01233                                 if (strcmp($md_cont->getRole(), "Author") == 0)
01234                                 {
01235                                         $entids =& $md_cont->getEntityIds();
01236                                         foreach ($entids as $entid)
01237                                         {
01238                                                 $md_ent =& $md_cont->getEntity($entid);
01239                                                 array_push($author, $md_ent->getEntity());
01240                                         }
01241                                 }
01242                         }
01243                 }
01244                 return join($author, ",");
01245   }
01246 
01256   function getShowQuestionTitles() 
01257         {
01258                 return $this->display_question_titles;
01259   }
01260 
01269   function showQuestionTitles() 
01270         {
01271                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
01272   }
01273 
01282   function hideQuestionTitles() 
01283         {
01284                 $this->display_question_titles = QUESTIONTITLES_HIDDEN;
01285   }
01286         
01296   function setInvitation($invitation = 0) 
01297         {
01298                 global $ilDB;
01299                 global $ilAccess;
01300     $this->invitation = $invitation;
01301                 // remove the survey from the personal desktops
01302                 $query = sprintf("DELETE FROM desktop_item WHERE type = %s AND item_id = %s",
01303                         $ilDB->quote("svy"),
01304                         $ilDB->quote($this->getRefId())
01305                 );
01306                 $result = $ilDB->query($query);
01307                 if ($invitation == INVITATION_OFF)
01308                 {
01309                         // already removed prior
01310                 }
01311                 else if ($invitation == INVITATION_ON)
01312                 {
01313                         if ($this->getInvitationMode() == MODE_UNLIMITED)
01314                         {
01315                                 $query = "SELECT usr_id FROM usr_data";
01316                                 $result = $ilDB->query($query);
01317                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01318                                 {
01319                                         if ($ilAccess->checkAccessOfUser($row["usr_id"], "read", "", $this->getRefId(), "svy", $this->getId()))
01320                                         {
01321                                                 $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01322                                                         $ilDB->quote($row["usr_id"]),
01323                                                         $ilDB->quote($this->getRefId()),
01324                                                         $ilDB->quote("svy")
01325                                                 );
01326                                                 $insertresult = $ilDB->query($query);
01327                                         }
01328                                 }
01329                         }
01330                         else
01331                         {
01332                                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
01333                                         $ilDB->quote($this->getSurveyId())
01334                                 );
01335                                 $result = $ilDB->query($query);
01336                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01337                                 {
01338                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01339                                                 $ilDB->quote($row["user_fi"]),
01340                                                 $ilDB->quote($this->getRefId()),
01341                                                 $ilDB->quote("svy")
01342                                         );
01343                                         $insertresult = $ilDB->query($query);
01344                                 }
01345                                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
01346                                         $ilDB->quote($this->getSurveyId())
01347                                 );
01348                                 $result = $ilDB->query($query);
01349                                 include_once "./classes/class.ilObjGroup.php";
01350                                 include_once './Services/User/classes/class.ilObjUser.php';
01351                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01352                                 {
01353                                         $group = new ilObjGroup($row["group_fi"]);
01354                                         $members = $group->getGroupMemberIds();
01355                                         foreach ($members as $user_id)
01356                                         {
01357                                                 if (ilObjUser::_lookupLogin($user_id))
01358                                                 {
01359                                                         $user = new ilObjUser($user_id);
01360                                                         $user->addDesktopItem($this->getRefId(), "svy");
01361                                                 }
01362                                         }
01363                                 }
01364                         }
01365                 }
01366   }
01367 
01377   function setInvitationMode($invitation_mode = 0) 
01378         {
01379                 global $ilDB;
01380     $this->invitation_mode = $invitation_mode;
01381                 if ($invitation_mode == MODE_UNLIMITED)
01382                 {
01383                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01384                                 $ilDB->quote($this->getSurveyId())
01385                         );
01386                         $result = $ilDB->query($query);
01387                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01388                                 $ilDB->quote($this->getSurveyId())
01389                         );
01390                         $result = $ilDB->query($query);
01391                 }
01392                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01393                 $this->setInvitation($this->getInvitation());
01394   }
01395         
01406         function setInvitationAndMode($invitation = 0, $invitation_mode = 0)
01407         {
01408                 global $ilDB;
01409     $this->invitation_mode = $invitation_mode;
01410                 if ($invitation_mode == MODE_UNLIMITED)
01411                 {
01412                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01413                                 $ilDB->quote($this->getSurveyId())
01414                         );
01415                         $result = $ilDB->query($query);
01416                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01417                                 $ilDB->quote($this->getSurveyId())
01418                         );
01419                         $result = $ilDB->query($query);
01420                 }
01421                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01422                 $this->setInvitation($invitation);
01423         }
01424 
01434   function setIntroduction($introduction = "") 
01435         {
01436     $this->introduction = $introduction;
01437   }
01438 
01448   function setOutro($outro = "") 
01449         {
01450     $this->outro = $outro;
01451   }
01452 
01462   function getInvitation() 
01463         {
01464     return $this->invitation;
01465   }
01466 
01476   function getInvitationMode() 
01477         {
01478     return $this->invitation_mode;
01479   }
01480 
01490   function getStatus() 
01491         {
01492     return $this->status;
01493   }
01494 
01504   function isOnline() 
01505         {
01506     if ($this->status == STATUS_ONLINE)
01507                 {
01508                         return true;
01509                 }
01510                 else
01511                 {
01512                         return false;
01513                 }
01514   }
01515 
01525   function isOffline() 
01526         {
01527     if ($this->status == STATUS_OFFLINE)
01528                 {
01529                         return true;
01530                 }
01531                 else
01532                 {
01533                         return false;
01534                 }
01535   }
01536 
01547   function setStatus($status = STATUS_OFFLINE) 
01548         {
01549                 $result = "";
01550                 if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
01551                 {
01552         $this->status = STATUS_OFFLINE;
01553                         $result = $this->lng->txt("cannot_switch_to_online_no_questions");
01554                 }
01555                 else
01556                 {
01557         $this->status = $status;
01558                 }
01559                 return $result;
01560   }
01561 
01571   function getStartDate() 
01572         {
01573     return $this->start_date;
01574   }
01575 
01584         function canStartSurvey($anonymous_id = NULL)
01585         {
01586                 global $ilAccess;
01587                 
01588                 $result = TRUE;
01589                 $messages = array();
01590                 // check start date
01591                 if ($this->getStartDateEnabled())
01592                 {
01593                         $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
01594                         $now = mktime();
01595                         if ($now < $epoch_time) 
01596                         {
01597                                 array_push($messages, $this->lng->txt("start_date_not_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getStartYear().$this->getStartMonth().$this->getStartDay()."000000"), "date") . ")");
01598                                 $result = FALSE;
01599                         }
01600                 }
01601                 // check end date
01602                 if ($this->getEndDateEnabled())
01603                 {
01604                         $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
01605                         $now = mktime();
01606                         if ($now > $epoch_time) 
01607                         {
01608                                 array_push($messages, $this->lng->txt("end_date_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getEndYear().$this->getEndMonth().$this->getEndDay()."000000"), "date") . ")");
01609                                 $result = FALSE;
01610                         }
01611                 }
01612                 // check online status
01613                 if ($this->getStatus() == STATUS_OFFLINE)
01614                 {
01615                         array_push($messages, $this->lng->txt("survey_is_offline"));
01616                         $result = FALSE;
01617                 }
01618                 // check rbac permissions
01619                 if (!$ilAccess->checkAccess("read", "", $this->ref_id))
01620                 {
01621                         array_push($messages, $this->lng->txt("cannot_participate_survey"));
01622                         $result = FALSE;
01623                 }
01624                 // 2. check previous access
01625                 if (!$result["error"])
01626                 {
01627                         global $ilUser;
01628                         $survey_started = $this->isSurveyStarted($ilUser->getId(), $anonymous_id);
01629                         if ($survey_started === 1)
01630                         {
01631                                 array_push($messages, $this->lng->txt("already_completed_survey"));
01632                                 $result = FALSE;
01633                         }
01634                 }
01635                 return array(
01636                         "result" => $result,
01637                         "messages" => $messages
01638                 );
01639         }
01640 
01650   function setStartDate($start_date = "") 
01651         {
01652     $this->start_date = $start_date;
01653   }
01654 
01664   function getStartMonth() 
01665         {
01666                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01667                 {
01668                         return $matches[2];
01669                 }
01670                 else
01671                 {
01672                         return "";
01673                 }
01674   }
01675 
01685   function getStartDay() 
01686         {
01687                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01688                 {
01689                         return $matches[3];
01690                 }
01691                 else
01692                 {
01693                         return "";
01694                 }
01695   }
01696 
01706   function getStartYear() 
01707         {
01708                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01709                 {
01710                         return $matches[1];
01711                 }
01712                 else
01713                 {
01714                         return "";
01715                 }
01716   }
01717 
01727   function getEndDate() 
01728         {
01729     return $this->end_date;
01730   }
01731 
01741   function setEndDate($end_date = "") 
01742         {
01743     $this->end_date = $end_date;
01744   }
01745 
01755   function getEndMonth() 
01756         {
01757                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01758                 {
01759                         return $matches[2];
01760                 }
01761                 else
01762                 {
01763                         return "";
01764                 }
01765   }
01766 
01776   function getEndDay() 
01777         {
01778                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01779                 {
01780                         return $matches[3];
01781                 }
01782                 else
01783                 {
01784                         return "";
01785                 }
01786   }
01787 
01797   function getEndYear() 
01798         {
01799                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01800                 {
01801                         return $matches[1];
01802                 }
01803                 else
01804                 {
01805                         return "";
01806                 }
01807   }
01808 
01818   function getEvaluationAccess() 
01819         {
01820     return $this->evaluation_access;
01821   }
01822 
01832   function setEvaluationAccess($evaluation_access = EVALUATION_ACCESS_OFF) 
01833         {
01834     $this->evaluation_access = $evaluation_access;
01835   }
01836 
01846   function getIntroduction() 
01847         {
01848     return $this->introduction;
01849   }
01850 
01860   function getOutro() 
01861         {
01862     return $this->outro;
01863   }
01864 
01873         function &getExistingQuestions() 
01874         {
01875                 global $ilDB;
01876                 $existing_questions = array();
01877                 $query = sprintf("SELECT survey_question.original_id FROM survey_question, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id",
01878                         $ilDB->quote($this->getSurveyId())
01879                 );
01880                 $result = $ilDB->query($query);
01881                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
01882                 {
01883                         array_push($existing_questions, $data->original_id);
01884                 }
01885                 return $existing_questions;
01886         }
01887 
01896         function &getQuestionpoolTitles($could_be_offline = FALSE, $showPath = FALSE) 
01897         {
01898                 include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
01899                 return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_object_id = TRUE, $could_be_offline, $showPath);
01900         }
01901         
01910         function moveUpQuestion($question_id)
01911         {
01912                 $move_questions = array($question_id);
01913                 $pages =& $this->getSurveyPages();
01914                 $pageindex = -1;
01915                 foreach ($pages as $idx => $page)
01916                 {
01917                         if ($page[0]["question_id"] == $question_id)
01918                         {
01919                                 $pageindex = $idx;
01920                         }
01921                 }
01922                 if ($pageindex > 0)
01923                 {
01924                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01925                 }
01926                 else
01927                 {
01928                         // move up a question in a questionblock
01929                         $questions = $this->getSurveyQuestions();
01930                         $questions = array_keys($questions);
01931                         $index = array_search($question_id, $questions);
01932                         if (($index !== FALSE) && ($index > 0))
01933                         {
01934                                 $this->moveQuestions($move_questions, $questions[$index-1], 0);
01935                         }
01936                 }
01937         }
01938         
01947         function moveDownQuestion($question_id)
01948         {
01949                 $move_questions = array($question_id);
01950                 $pages =& $this->getSurveyPages();
01951                 $pageindex = -1;
01952                 foreach ($pages as $idx => $page)
01953                 {
01954                         if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
01955                         {
01956                                 $pageindex = $idx;
01957                         }
01958                 }
01959                 if (($pageindex < count($pages)-1) && ($pageindex >= 0))
01960                 {
01961                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01962                 }
01963                 else
01964                 {
01965                         // move down a question in a questionblock
01966                         $questions = $this->getSurveyQuestions();
01967                         $questions = array_keys($questions);
01968                         $index = array_search($question_id, $questions);
01969                         if (($index !== FALSE) && ($index < count($questions)-1))
01970                         {
01971                                 $this->moveQuestions($move_questions, $questions[$index+1], 1);
01972                         }
01973                 }
01974         }
01975         
01984         function moveUpQuestionblock($questionblock_id)
01985         {
01986                 $pages =& $this->getSurveyPages();
01987                 $move_questions = array();
01988                 $pageindex = -1;
01989                 foreach ($pages as $idx => $page)
01990                 {
01991                         if ($page[0]["questionblock_id"] == $questionblock_id)
01992                         {
01993                                 foreach ($page as $pageidx => $question)
01994                                 {
01995                                         array_push($move_questions, $question["question_id"]);
01996                                 }
01997                                 $pageindex = $idx;
01998                         }
01999                 }
02000                 if ($pageindex > 0)
02001                 {
02002                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
02003                 }
02004         }
02005         
02014         function moveDownQuestionblock($questionblock_id)
02015         {
02016                 $pages =& $this->getSurveyPages();
02017                 $move_questions = array();
02018                 $pageindex = -1;
02019                 foreach ($pages as $idx => $page)
02020                 {
02021                         if ($page[0]["questionblock_id"] == $questionblock_id)
02022                         {
02023                                 foreach ($page as $pageidx => $question)
02024                                 {
02025                                         array_push($move_questions, $question["question_id"]);
02026                                 }
02027                                 $pageindex = $idx;
02028                         }
02029                 }
02030                 if ($pageindex < count($pages)-1)
02031                 {
02032                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
02033                 }
02034         }
02035         
02046         function moveQuestions($move_questions, $target_index, $insert_mode)
02047         {
02048                 $array_pos = array_search($target_index, $this->questions);
02049                 if ($insert_mode == 0)
02050                 {
02051                         $part1 = array_slice($this->questions, 0, $array_pos);
02052                         $part2 = array_slice($this->questions, $array_pos);
02053                 }
02054                 else if ($insert_mode == 1)
02055                 {
02056                         $part1 = array_slice($this->questions, 0, $array_pos + 1);
02057                         $part2 = array_slice($this->questions, $array_pos + 1);
02058                 }
02059                 foreach ($move_questions as $question_id)
02060                 {
02061                         if (!(array_search($question_id, $part1) === FALSE))
02062                         {
02063                                 unset($part1[array_search($question_id, $part1)]);
02064                         }
02065                         if (!(array_search($question_id, $part2) === FALSE))
02066                         {
02067                                 unset($part2[array_search($question_id, $part2)]);
02068                         }
02069                 }
02070                 $part1 = array_values($part1);
02071                 $part2 = array_values($part2);
02072                 $this->questions = array_values(array_merge($part1, $move_questions, $part2));
02073                 foreach ($move_questions as $question_id)
02074                 {
02075                         $constraints = $this->getConstraints($question_id);
02076                         foreach ($constraints as $idx => $constraint)
02077                         {
02078                                 foreach ($part2 as $next_question_id)
02079                                 {
02080                                         if ($constraint["question"] == $next_question_id)
02081                                         {
02082                                                 // constraint concerning a question that follows -> delete constraint
02083                                                 $this->deleteConstraint($constraint["id"], $question_id);
02084                                         }
02085                                 }
02086                         }
02087                 }
02088                 $this->saveQuestionsToDb();
02089         }
02090         
02099         function removeQuestion($question_id)
02100         {
02101                 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
02102                 $question =& $this->_instanciateQuestion($question_id);
02103                 $question->delete($question_id);
02104                 $this->removeConstraintsConcerningQuestion($question_id);
02105         }
02106         
02115         function removeConstraintsConcerningQuestion($question_id)
02116         {
02117                 global $ilDB;
02118                 $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02119                         $ilDB->quote($question_id . ""),
02120                         $ilDB->quote($this->getSurveyId() . "")
02121                 );
02122                 $result = $ilDB->query($query);
02123                 if ($result->numRows() > 0)
02124                 {
02125                         $remove_constraints = array();
02126                         while ($row = $result->fetchRow(DB_FETCHMODE_HASHREF))
02127                         {
02128                                 array_push($remove_constraints, $row["constraint_fi"]);
02129                         }
02130                         $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02131                                 $ilDB->quote($question_id . ""),
02132                                 $ilDB->quote($this->getSurveyId() . "")
02133                         );
02134                         $result = $ilDB->query($query);
02135                         foreach ($remove_constraints as $key => $constraint_id)
02136                         {
02137                                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02138                                         $ilDB->quote($constraint_id . "")
02139                                 );
02140                                 $result = $ilDB->query($query);
02141                         }
02142                 }
02143         }
02144                 
02154         function removeQuestions($remove_questions, $remove_questionblocks)
02155         {
02156                 global $ilDB;
02157                 $questions =& $this->getSurveyQuestions();
02158                 foreach ($questions as $question_id => $data)
02159                 {
02160                         if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
02161                         {
02162                                 unset($this->questions[array_search($question_id, $this->questions)]);
02163                                 $this->removeQuestion($question_id);
02164                         }
02165                 }
02166                 foreach ($remove_questionblocks as $questionblock_id)
02167                 {
02168                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
02169                                 $ilDB->quote($questionblock_id)
02170                         );
02171                         $result = $ilDB->query($query);
02172                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
02173                                 $ilDB->quote($questionblock_id),
02174                                 $ilDB->quote($this->getSurveyId())
02175                         );
02176                         $result = $ilDB->query($query);
02177                 }
02178                 $this->questions = array_values($this->questions);
02179                 $this->saveQuestionsToDb();
02180         }
02181                 
02190         function unfoldQuestionblocks($questionblocks)
02191         {
02192                 global $ilDB;
02193                 foreach ($questionblocks as $index)
02194                 {
02195                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
02196                                 $ilDB->quote($index)
02197                         );
02198                         $result = $ilDB->query($query);
02199                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
02200                                 $ilDB->quote($index),
02201                                 $ilDB->quote($this->getSurveyId())
02202                         );
02203                         $result = $ilDB->query($query);
02204                 }
02205         }
02206         
02215         function &getQuestionblockTitles()
02216         {
02217                 global $ilDB;
02218                 $titles = array();
02219                 $query = sprintf("SELECT survey_questionblock.* FROM survey_questionblock, survey_question, survey_questionblock_question WHERE survey_questionblock_question.question_fi = survey_question.question_id AND survey_question.obj_fi = %s",
02220                         $ilDB->quote($this->getId())
02221                 );
02222                 $result = $ilDB->query($query);
02223                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02224                 {
02225                         $titles[$row->questionblock_id] = $row->title;
02226                 }
02227                 return $titles;
02228         }
02229         
02238         function &getQuestionblockQuestions($questionblock_id)
02239         {
02240                 global $ilDB;
02241                 $titles = array();
02242                 $query = sprintf("SELECT survey_question.title, survey_questionblock_question.question_fi, survey_questionblock_question.survey_fi FROM survey_questionblock, survey_questionblock_question, survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_question.question_id = survey_questionblock_question.question_fi AND survey_questionblock.questionblock_id = %s",
02243                         $ilDB->quote($questionblock_id)
02244                 );
02245                 $result = $ilDB->query($query);
02246                 $survey_id = "";
02247                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02248                 {
02249                         $titles[$row["question_fi"]] = $row["title"];
02250                         $survey_id = $row["survey_fi"];
02251                 }
02252                 $query = sprintf("SELECT question_fi, sequence FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
02253                         $ilDB->quote($survey_id . "")
02254                 );
02255                 $result = $ilDB->query($query);
02256                 $resultarray = array();
02257                 $counter = 1;
02258                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02259                 {
02260                         if (array_key_exists($row["question_fi"], $titles))
02261                         {
02262                                 $resultarray[$counter++] = $titles[$row["question_fi"]];
02263                         }
02264                 }
02265                 return $resultarray;
02266         }
02267         
02276         function &getQuestionblockQuestionIds($questionblock_id)
02277         {
02278                 global $ilDB;
02279                 $ids = array();
02280                 $query = sprintf("SELECT survey_questionblock.*, survey_survey.obj_fi, survey_question.question_id AS questiontitle, survey_survey_question.sequence, object_data.title as surveytitle, survey_question.question_id FROM object_reference, object_data, survey_questionblock, survey_questionblock_question, survey_survey, survey_question, survey_survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_survey.survey_id = survey_questionblock_question.survey_fi AND survey_questionblock_question.question_fi = survey_question.question_id AND survey_survey.obj_fi = object_reference.obj_id AND object_reference.obj_id = object_data.obj_id AND survey_survey_question.survey_fi = survey_survey.survey_id AND survey_survey_question.question_fi = survey_question.question_id AND survey_survey.obj_fi = %s AND survey_questionblock.questionblock_id = %s ORDER BY survey_survey_question.sequence ASC",
02281                         $ilDB->quote($this->getId()),
02282                         $ilDB->quote($questionblock_id)
02283                 );
02284                 $result = $ilDB->query($query);
02285                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02286                 {
02287                         array_push($ids, $row->question_id);
02288                 }
02289                 return $ids;
02290         }
02291         
02301         function getQuestionblock($questionblock_id)
02302         {
02303                 global $ilDB;
02304                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02305                         $ilDB->quote($questionblock_id)
02306                 );
02307                 $result = $ilDB->query($query);
02308                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02309                 return $row;
02310         }
02311         
02321         function _getQuestionblock($questionblock_id)
02322         {
02323                 global $ilDB;
02324                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02325                         $ilDB->quote($questionblock_id)
02326                 );
02327                 $result = $ilDB->query($query);
02328                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02329                 return $row;
02330         }
02331 
02342         function _addQuestionblock($title = "", $owner = 0)
02343         {
02344                 global $ilDB;
02345                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02346                         $ilDB->quote($title . ""),
02347                         $ilDB->quote($owner . "")
02348                 );
02349                 $result = $ilDB->query($query);
02350                 return $ilDB->getLastInsertId();
02351         }
02352         
02362         function createQuestionblock($title, $show_questiontext, $questions)
02363         {
02364                 global $ilDB;
02365                 // if the selected questions are not in a continous selection, move all questions of the
02366                 // questionblock at the position of the first selected question
02367                 $this->moveQuestions($questions, $questions[0], 0);
02368                 
02369                 // now save the question block
02370                 global $ilUser;
02371                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, show_questiontext, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02372                         $ilDB->quote($title),
02373                         $ilDB->quote($show_questiontext . ""),
02374                         $ilDB->quote($ilUser->getId())
02375                 );
02376                 $result = $ilDB->query($query);
02377                 if ($result == DB_OK) {
02378                         $questionblock_id = $ilDB->getLastInsertId();
02379                         foreach ($questions as $index)
02380                         {
02381                                 $query = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
02382                                         $ilDB->quote($this->getSurveyId()),
02383                                         $ilDB->quote($questionblock_id),
02384                                         $ilDB->quote($index)
02385                                 );
02386                                 $result = $ilDB->query($query);
02387                                 $this->deleteConstraints($index);
02388                         }
02389                 }
02390         }
02391         
02401         function modifyQuestionblock($questionblock_id, $title, $show_questiontext)
02402         {
02403                 global $ilDB;
02404                 $query = sprintf("UPDATE survey_questionblock SET title = %s, show_questiontext = %s WHERE questionblock_id = %s",
02405                         $ilDB->quote($title),
02406                         $ilDB->quote($show_questiontext . ""),
02407                         $ilDB->quote($questionblock_id)
02408                 );
02409                 $result = $ilDB->query($query);
02410         }
02411         
02420         function deleteConstraints($question_id)
02421         {
02422                 global $ilDB;
02423                 $query = sprintf("SELECT * FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02424                         $ilDB->quote($question_id),
02425                         $ilDB->quote($this->getSurveyId())
02426                 );
02427                 $result = $ilDB->query($query);
02428                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02429                 {
02430                         $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02431                                 $ilDB->quote($row->constraint_fi)
02432                         );
02433                         $delresult = $ilDB->query($query);
02434                 }
02435                 $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02436                         $ilDB->quote($question_id),
02437                         $ilDB->quote($this->getSurveyId())
02438                 );
02439                 $delresult = $ilDB->query($query);
02440         }
02441 
02451         function deleteConstraint($constraint_id, $question_id)
02452         {
02453                 global $ilDB;
02454                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02455                         $ilDB->quote($constraint_id)
02456                 );
02457                 $delresult = $ilDB->query($query);
02458                 $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s AND question_fi = %s AND survey_fi = %s",
02459                         $ilDB->quote($constraint_id),
02460                         $ilDB->quote($question_id),
02461                         $ilDB->quote($this->getSurveyId())
02462                 );
02463                 $delresult = $ilDB->query($query);
02464         }
02465 
02473         function &getSurveyQuestions($with_answers = false)
02474         {
02475                 global $ilDB;
02476                 $obligatory_states =& $this->getObligatoryStates();
02477                 // get questionblocks
02478                 $all_questions = array();
02479                 $query = sprintf("SELECT survey_questiontype.type_tag, survey_question.question_id, survey_survey_question.heading FROM survey_questiontype, survey_question, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id AND survey_question.questiontype_fi = survey_questiontype.questiontype_id ORDER BY survey_survey_question.sequence",
02480                         $ilDB->quote($this->getSurveyId())
02481                 );
02482                 $result = $ilDB->query($query);
02483                 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
02484                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02485                 {
02486                         $question =& $this->_instanciateQuestion($row["question_id"]);
02487                         $questionrow = $question->_getQuestionDataArray($row["question_id"]);
02488                         foreach ($row as $key => $value)
02489                         {
02490                                 $questionrow[$key] = $value;
02491                         }
02492                         $all_questions[$row["question_id"]] = $questionrow;
02493                         $all_questions[$row["question_id"]]["usableForPrecondition"] = $question->usableForPrecondition();
02494                         $all_questions[$row["question_id"]]["availableRelations"] = $question->getAvailableRelations();
02495                         if (array_key_exists($row["question_id"], $obligatory_states))
02496                         {
02497                                 $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
02498                         }
02499                 }
02500                 // get all questionblocks
02501                 $questionblocks = array();
02502                 $in = join(array_keys($all_questions), "','");
02503                 if ($in)
02504                 {
02505                         $query = sprintf("SELECT survey_questionblock.*, survey_questionblock_question.question_fi FROM survey_questionblock, survey_questionblock_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_questionblock_question.survey_fi = %s AND survey_questionblock_question.question_fi IN ('$in')",
02506                                 $ilDB->quote($this->getSurveyId())
02507                         );
02508                         $result = $ilDB->query($query);
02509                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02510                         {
02511                                 $questionblocks[$row->question_fi] = $row;
02512                         }                       
02513                 }
02514                 
02515                 foreach ($all_questions as $question_id => $row)
02516                 {
02517                         $constraints = $this->getConstraints($question_id);
02518                         if (isset($questionblocks[$question_id]))
02519                         {
02520                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02521                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02522                                 $all_questions[$question_id]["constraints"] = $constraints;
02523                         }
02524                         else
02525                         {
02526                                 $all_questions[$question_id]["questionblock_title"] = "";
02527                                 $all_questions[$question_id]["questionblock_id"] = "";
02528                                 $all_questions[$question_id]["constraints"] = $constraints;
02529                         }
02530                         if ($with_answers)
02531                         {
02532                                 $answers = array();
02533                                 $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",
02534                                         $ilDB->quote($question_id . "")
02535                                 );
02536                                 $result = $ilDB->query($query);
02537                                 if (strcmp(strtolower(get_class($result)), db_result) == 0) 
02538                                 {
02539                                         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
02540                                         {
02541                                                 array_push($answers, $data->title);
02542                                         }
02543                                 }
02544                                 $all_questions[$question_id]["answers"] = $answers;                             
02545                         }
02546                 }
02547                 return $all_questions;
02548         }
02549         
02558         function &getQuestiontypes()
02559         {
02560                 global $ilDB;
02561                 $query = "SELECT type_tag FROM survey_questiontype";
02562                 $result = $ilDB->query($query);
02563                 $result_array = array();
02564                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02565                 {
02566                         array_push($result_array, $row->type_tag);
02567                 }
02568                 return $result_array;
02569         }
02570 
02579         function setObligatoryStates($obligatory_questions)
02580         {
02581                 global $ilDB;
02582                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
02583                         $ilDB->quote($this->getSurveyId() . "")
02584                 );
02585                 $result = $ilDB->query($query);
02586                 if ($result->numRows())
02587                 {
02588                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02589                         {
02590                                 if (!array_key_exists($row["question_fi"], $obligatory_questions))
02591                                 {
02592                                         $obligatory_questions[$row["question_fi"]] = 0;
02593                                 }
02594                         }
02595                 }
02596 
02597           // set the obligatory states in the database
02598                 $query = sprintf("DELETE FROM survey_question_obligatory WHERE survey_fi = %s",
02599                         $ilDB->quote($this->getSurveyId() . "")
02600                 );
02601                 $result = $ilDB->query($query);
02602 
02603           // set the obligatory states in the database
02604                 foreach ($obligatory_questions as $question_fi => $obligatory)
02605                 {
02606                         $query = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02607                                 $ilDB->quote($this->getSurveyId() . ""),
02608                                 $ilDB->quote($question_fi . ""),
02609                                 $ilDB->quote($obligatory . "")
02610                         );
02611                         $result = $ilDB->query($query);
02612                 }
02613         }
02614         
02623         function &getObligatoryStates()
02624         {
02625                 global $ilDB;
02626                 $obligatory_states = array();
02627                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
02628                         $ilDB->quote($this->getSurveyId() . "")
02629                 );
02630                 $result = $ilDB->query($query);
02631                 if ($result->numRows())
02632                 {
02633                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02634                         {
02635                                 $obligatory_states[$row["question_fi"]] = $row["obligatory"];
02636                         }
02637                 }
02638                 return $obligatory_states;
02639         }
02640         
02648         function &getSurveyPages()
02649         {
02650                 global $ilDB;
02651                 $obligatory_states =& $this->getObligatoryStates();
02652                 // get questionblocks
02653                 $all_questions = array();
02654                 $query = sprintf("SELECT survey_question.*, survey_questiontype.type_tag, survey_survey_question.heading FROM survey_question, survey_questiontype, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id AND survey_question.questiontype_fi = survey_questiontype.questiontype_id ORDER BY survey_survey_question.sequence",
02655                         $ilDB->quote($this->getSurveyId())
02656                 );
02657                 $result = $ilDB->query($query);
02658                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02659                 {
02660                         $all_questions[$row["question_id"]] = $row;
02661                 }
02662                 // get all questionblocks
02663                 $questionblocks = array();
02664                 $in = join(array_keys($all_questions), "','");
02665                 if ($in)
02666                 {
02667                         $query = sprintf("SELECT survey_questionblock.*, survey_questionblock_question.question_fi FROM survey_questionblock, survey_questionblock_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_questionblock_question.survey_fi = %s AND survey_questionblock_question.question_fi IN ('$in')",
02668                                 $ilDB->quote($this->getSurveyId())
02669                         );
02670                         $result = $ilDB->query($query);
02671                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02672                         {
02673                                 $questionblocks["$row->question_fi"] = $row;
02674                         }                       
02675                 }
02676                 
02677                 $all_pages = array();
02678                 $pageindex = -1;
02679                 $currentblock = "";
02680                 foreach ($all_questions as $question_id => $row)
02681                 {
02682                         if (array_key_exists($question_id, $obligatory_states))
02683                         {
02684                                 $all_questions["$question_id"]["obligatory"] = $obligatory_states["$question_id"];
02685                         }
02686                         $constraints = array();
02687                         if (isset($questionblocks[$question_id]))
02688                         {
02689                                 if (!$currentblock or ($currentblock != $questionblocks[$question_id]->questionblock_id))
02690                                 {
02691                                         $pageindex++;
02692                                 }
02693                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02694                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02695                                 $all_questions[$question_id]["questionblock_show_questiontext"] = $questionblocks[$question_id]->show_questiontext;
02696                                 $currentblock = $questionblocks[$question_id]->questionblock_id;
02697                                 $constraints = $this->getConstraints($question_id);
02698                                 $all_questions[$question_id]["constraints"] = $constraints;
02699                         }
02700                         else
02701                         {
02702                                 $pageindex++;
02703                                 $all_questions[$question_id]["questionblock_title"] = "";
02704                                 $all_questions[$question_id]["questionblock_id"] = "";
02705                                 $all_questions[$question_id]["questionblock_show_questiontext"] = 1;
02706                                 $currentblock = "";
02707                                 $constraints = $this->getConstraints($question_id);
02708                                 $all_questions[$question_id]["constraints"] = $constraints;
02709                         }
02710                         if (!isset($all_pages[$pageindex]))
02711                         {
02712                                 $all_pages[$pageindex] = array();
02713                         }
02714                         array_push($all_pages[$pageindex], $all_questions[$question_id]);
02715                 }
02716                 // calculate position percentage for every page
02717                 $max = count($all_pages);
02718                 $counter = 1;
02719                 foreach ($all_pages as $index => $block)
02720                 {
02721                         foreach ($block as $blockindex => $question)
02722                         {
02723                                 $all_pages[$index][$blockindex][position] = $counter / $max;
02724                         }
02725                         $counter++;
02726                 }
02727                 return $all_pages;
02728         }
02729         
02740         function getNextPage($active_page_question_id, $direction)
02741         {
02742                 $foundpage = -1;
02743                 $pages =& $this->getSurveyPages();
02744                 if (strcmp($active_page_question_id, "") == 0)
02745                 {
02746                         return $pages[0];
02747                 }
02748                 
02749                 foreach ($pages as $key => $question_array)
02750                 {
02751                         foreach ($question_array as $question)
02752                         {
02753                                 if ($active_page_question_id == $question["question_id"])
02754                                 {
02755                                         $foundpage = $key;
02756                                 }
02757                         }
02758                 }
02759                 if ($foundpage == -1)
02760                 {
02761                         // error: page not found
02762                 }
02763                 else
02764                 {
02765                         $foundpage += $direction;
02766                         if ($foundpage < 0)
02767                         {
02768                                 return 0;
02769                         }
02770                         if ($foundpage >= count($pages))
02771                         {
02772                                 return 1;
02773                         }
02774                         return $pages[$foundpage];
02775                 }
02776         }
02777                 
02786         function &getAvailableQuestionpools($use_obj_id = false, $could_be_offline = false, $showPath = FALSE, $permission = "read")
02787         {
02788                 include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
02789                 return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_obj_id, $could_be_offline, $showPath, $permission);
02790         }
02791         
02799         function getPrecondition($id)
02800         {
02801                 global $ilDB;
02802                 
02803                 $result_array = array();
02804                 $query = sprintf("SELECT survey_constraint.*, survey_relation.* FROM survey_question_constraint, survey_constraint, survey_relation WHERE survey_constraint.relation_fi = survey_relation.relation_id AND survey_question_constraint.constraint_fi = survey_constraint.constraint_id AND survey_constraint.constraint_id = %s",
02805                         $ilDB->quote($id . "")
02806                 );
02807                 $result = $ilDB->query($query);
02808                 $pc = array();
02809                 if ($result->numRows())
02810                 {
02811                         $pc = $result->fetchRow(DB_FETCHMODE_ASSOC);
02812                 }
02813                 return $pc;
02814         }
02815         
02823         function getConstraints($question_id)
02824         {
02825                 global $ilDB;
02826                 
02827                 $result_array = array();
02828                 $query = sprintf("SELECT survey_constraint.*, survey_relation.* FROM survey_question_constraint, survey_constraint, survey_relation WHERE survey_constraint.relation_fi = survey_relation.relation_id AND survey_question_constraint.constraint_fi = survey_constraint.constraint_id AND survey_question_constraint.question_fi = %s AND survey_question_constraint.survey_fi = %s",
02829                         $ilDB->quote($question_id),
02830                         $ilDB->quote($this->getSurveyId())
02831                 );
02832                 $result = $ilDB->query($query);
02833                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02834                 {       
02835                         include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
02836                         $question_type = SurveyQuestion::_getQuestionType($row->question_fi);
02837                         include_once "./Modules/SurveyQuestionPool/classes/class.$question_type.php";
02838                         $question = new $question_type();
02839                         $question->loadFromDb($row->question_fi);
02840                         $valueoutput = $question->getPreconditionValueOutput($row->value);
02841                         array_push($result_array, array("id" => $row->constraint_id, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "value" => $row->value, "valueoutput" => $valueoutput));
02842                 }
02843                 return $result_array;
02844         }
02845 
02853         function _getConstraints($survey_id)
02854         {
02855                 global $ilDB;
02856                 $result_array = array();
02857                 $query = sprintf("SELECT survey_question_constraint.question_fi as for_question, survey_constraint.*, survey_relation.* FROM survey_question_constraint, survey_constraint, survey_relation WHERE survey_constraint.relation_fi = survey_relation.relation_id AND survey_question_constraint.constraint_fi = survey_constraint.constraint_id AND survey_question_constraint.survey_fi = %s",
02858                         $ilDB->quote($survey_id . "")
02859                 );
02860                 $result = $ilDB->query($query);
02861                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02862                 {               
02863                         array_push($result_array, array("id" => $row->constraint_id, "for_question" => $row->for_question, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "relation_id" => $row->relation_id, "value" => $row->value));
02864                 }
02865                 return $result_array;
02866         }
02867 
02868 
02876         function &getVariables($question_id)
02877         {
02878                 global $ilDB;
02879                 
02880                 $result_array = array();
02881                 $query = sprintf("SELECT survey_variable.*, survey_category.title FROM survey_variable LEFT JOIN survey_category ON survey_variable.category_fi = survey_category.category_id WHERE survey_variable.question_fi = %s ORDER BY survey_variable.sequence",
02882                         $ilDB->quote($question_id)
02883                 );
02884                 $result = $ilDB->query($query);
02885                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02886                 {
02887                         $result_array[$row->sequence] = $row;
02888                 }
02889                 return $result_array;
02890         }
02891         
02903         function addConstraint($to_question_id, $if_question_id, $relation, $value)
02904         {
02905                 global $ilDB;
02906                 
02907                 $query = sprintf("INSERT INTO survey_constraint (constraint_id, question_fi, relation_fi, value) VALUES (NULL, %s, %s, %s)",
02908                         $ilDB->quote($if_question_id),
02909                         $ilDB->quote($relation),
02910                         $ilDB->quote($value)
02911                 );
02912                 $result = $ilDB->query($query);
02913                 if ($result == DB_OK) {
02914                         $constraint_id = $ilDB->getLastInsertId();
02915                         $query = sprintf("INSERT INTO survey_question_constraint (question_constraint_id, survey_fi, question_fi, constraint_fi) VALUES (NULL, %s, %s, %s)",
02916                                 $ilDB->quote($this->getSurveyId()),
02917                                 $ilDB->quote($to_question_id),
02918                                 $ilDB->quote($constraint_id)
02919                         );
02920                         $result = $ilDB->query($query);
02921                 }
02922         }
02923         
02936                 function updateConstraint($to_question_id, $if_question_id, $relation, $value)
02937                 {
02938                         global $ilDB;
02939                         $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02940                                 $ilDB->quote($to_question_id . ""),
02941                                 $ilDB->quote($this->getSurveyId() . "")
02942                         );
02943                         $result = $ilDB->query($query);
02944                         if ($result->numRows())
02945                         {
02946                                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02947                                 $query = sprintf("UPDATE survey_constraint SET question_fi = %s, relation_fi = %s, value = %s WHERE constraint_id = %s",
02948                                         $ilDB->quote($if_question_id),
02949                                         $ilDB->quote($relation),
02950                                         $ilDB->quote($value),
02951                                         $ilDB->quote($row["constraint_fi"])
02952                                 );
02953                                 $result = $ilDB->query($query);
02954                         }
02955                 }
02956 
02964         function getAllRelations($short_as_key = false)
02965         {
02966                 global $ilDB;
02967                 
02968                 $result_array = array();
02969                 $query = "SELECT * FROM survey_relation";
02970                 $result = $ilDB->query($query);
02971                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02972                 {
02973                         if ($short_as_key)
02974                         {
02975                                 $result_array[$row->shortname] = array("short" => $row->shortname, "long" => $row->longname, "id" => $row->relation_id);
02976                         }
02977                         else
02978                         {
02979                                 $result_array[$row->relation_id] = array("short" => $row->shortname, "long" => $row->longname);
02980                         }
02981                 }
02982                 return $result_array;
02983         }
02984 
02993         function disinviteUser($user_id)
02994         {
02995                 global $ilDB;
02996                 
02997                 $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s AND user_fi = %s",
02998                         $ilDB->quote($this->getSurveyId()),
02999                         $ilDB->quote($user_id)
03000                 );
03001                 $result = $ilDB->query($query);
03002                 if ($this->getInvitation() == INVITATION_ON)
03003                 {
03004                         include_once './Services/User/classes/class.ilObjUser.php';
03005                         if (ilObjUser::_lookupLogin($user_id))
03006                         {
03007                                 $userObj = new ilObjUser($user_id);
03008                                 $userObj->dropDesktopItem($this->getRefId(), "svy");
03009                         }
03010                 }
03011         }
03012 
03021         function inviteUser($user_id)
03022         {
03023                 global $ilDB;
03024                 
03025                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE user_fi = %s AND survey_fi = %s",
03026                         $ilDB->quote($user_id),
03027                         $ilDB->quote($this->getSurveyId())
03028                 );
03029                 $result = $ilDB->query($query);
03030                 if ($result->numRows() < 1)
03031                 {
03032                         $query = sprintf("INSERT INTO survey_invited_user (invited_user_id, survey_fi, user_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
03033                                 $ilDB->quote($this->getSurveyId()),
03034                                 $ilDB->quote($user_id)
03035                         );
03036                         $result = $ilDB->query($query);
03037                 }
03038                 if ($this->getInvitation() == INVITATION_ON)
03039                 {
03040                         include_once './Services/User/classes/class.ilObjUser.php';
03041                         if (ilObjUser::_lookupLogin($user_id))
03042                         {
03043                                 $userObj = new ilObjUser($user_id);
03044                                 $userObj->addDesktopItem($this->getRefId(), "svy");
03045                         }
03046                 }
03047         }
03048 
03057         function inviteGroup($group_id)
03058         {
03059                 global $ilAccess;
03060                 $invited = 0;
03061                 include_once "./classes/class.ilObjGroup.php";
03062                 $group = new ilObjGroup($group_id);
03063                 $members = $group->getGroupMemberIds();
03064                 foreach ($members as $user_id)
03065                 {
03066                         if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
03067                         {
03068                                 $this->inviteUser($user_id);
03069                                 if ($this->getInvitation() == INVITATION_ON)
03070                                 {
03071                                         if (ilObjUser::_lookupLogin($user_id))
03072                                         {
03073                                                 $userObj = new ilObjUser($user_id);
03074                                                 $userObj->addDesktopItem($this->getRefId(), "svy");
03075                                                 $invited++;
03076                                         }
03077                                 }
03078                         }
03079                 }
03080                 return $invited;
03081         }
03082         
03091         function inviteRole($role_id)
03092         {
03093                 global $rbacreview;
03094                 global $ilAccess;
03095                 $invited = 0;
03096                 $members = $rbacreview->assignedUsers($role_id);
03097                 foreach ($members as $user_id)
03098                 {
03099                         if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
03100                         {
03101                                 $this->inviteUser($user_id);
03102                                 if ($this->getInvitation() == INVITATION_ON)
03103                                 {
03104                                         if (ilObjUser::_lookupLogin($user_id))
03105                                         {
03106                                                 $userObj = new ilObjUser($user_id);
03107                                                 $userObj->addDesktopItem($this->getRefId(), "svy");
03108                                                 $invited++;
03109                                         }
03110                                 }
03111                         }
03112                 }
03113                 return $invited;
03114         }
03115         
03124         function &getInvitedUsers()
03125         {
03126                 global $ilDB;
03127                 
03128                 $result_array = array();
03129                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
03130                         $ilDB->quote($this->getSurveyId())
03131                 );
03132                 $result = $ilDB->query($query);
03133                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03134                 {
03135                         array_push($result_array, $row->user_fi);
03136                 }
03137                 return $result_array;
03138         }
03139 
03148         function &getInvitedGroups()
03149         {
03150                 global $ilDB;
03151                 
03152                 $result_array = array();
03153                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
03154                         $ilDB->quote($this->getSurveyId())
03155                 );
03156                 $result = $ilDB->query($query);
03157                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03158                 {
03159                         array_push($result_array, $row->group_fi);
03160                 }
03161                 return $result_array;
03162         }
03163 
03173         function deleteWorkingData($question_id, $active_id)
03174         {
03175                 global $ilDB;
03176                 
03177                 $query = "";
03178                 $query = sprintf("DELETE FROM survey_answer WHERE question_fi = %s AND active_fi = %s",
03179                         $ilDB->quote($question_id),
03180                         $ilDB->quote($active_id)
03181                 );
03182                 $result = $ilDB->query($query);
03183         }
03184         
03195         function loadWorkingData($question_id, $active_id)
03196         {
03197                 global $ilDB;
03198                 $result_array = array();
03199                 $query = sprintf("SELECT * FROM survey_answer WHERE question_fi = %s AND active_fi = %s",
03200                         $ilDB->quote($question_id. ""),
03201                         $ilDB->quote($active_id)
03202                 );
03203                 $result = $ilDB->query($query);
03204                 if ($result->numRows() >= 1)
03205                 {
03206                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03207                         {
03208                                 array_push($result_array, $row);
03209                         }
03210                         return $result_array;
03211                 }
03212                 else
03213                 {
03214                         return $result_array;
03215                 }
03216         }
03217 
03226         function startSurvey($user_id, $anonymous_id)
03227         {
03228                 global $ilUser;
03229                 global $ilDB;
03230                 
03231                 if ($this->getAnonymize() && (strlen($anonymous_id) == 0)) return;
03232 
03233                 if (strcmp($user_id, "") == 0)
03234                 {
03235                         if ($user_id == ANONYMOUS_USER_ID)
03236                         {
03237                                 $user_id = 0;
03238                         }
03239                 }
03240                 $query = sprintf("INSERT INTO survey_finished (finished_id, survey_fi, user_fi, anonymous_id, state, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
03241                         $ilDB->quote($this->getSurveyId() . ""),
03242                         $ilDB->quote($user_id . ""),
03243                         $ilDB->quote($anonymous_id . ""),
03244                         $ilDB->quote(0 . "")
03245                 );
03246                 $result = $ilDB->query($query);
03247                 
03248                 // get the insert id, but don't trust last_insert_id since we don't have transactions
03249                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s AND user_fi = %s AND anonymous_id = %s",
03250                         $ilDB->quote($this->getSurveyId() . ""),
03251                         $ilDB->quote($user_id . ""),
03252                         $ilDB->quote($anonymous_id . "")
03253                 );
03254                 $result = $ilDB->query($query);
03255                 $insert_id = 0;
03256                 if ($result->numRows())
03257                 {
03258                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03259                         $insert_id = $row["finished_id"];
03260                 }
03261                 return $insert_id;
03262         }
03263                         
03272         function finishSurvey($user_id, $anonymize_id)
03273         {
03274                 global $ilDB;
03275                 
03276                 if ($this->getAnonymize())
03277                 {
03278                         $query = sprintf("UPDATE survey_finished SET state = %s, user_fi = %s WHERE survey_fi = %s AND anonymous_id = %s",
03279                                 $ilDB->quote("1"),
03280                                 $ilDB->quote($user_id . ""),
03281                                 $ilDB->quote($this->getSurveyId() . ""),
03282                                 $ilDB->quote($anonymize_id . "")
03283                         );
03284                 }
03285                 else
03286                 {
03287                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND user_fi = %s",
03288                                 $ilDB->quote("1"),
03289                                 $ilDB->quote($this->getSurveyId() . ""),
03290                                 $ilDB->quote($user_id . "")
03291                         );
03292                 }
03293                 $result = $ilDB->query($query);
03294         }
03295         
03305         function isAllowedToTakeMultipleSurveys($userid = "")
03306         {
03307                 $result = FALSE;
03308                 if ($this->getAnonymize())
03309                 {
03310                         if ($this->isAccessibleWithoutCode())
03311                         {
03312                                 if (strlen($username) == 0)
03313                                 {
03314                                         global $ilUser;
03315                                         $userid = $ilUser->getId();
03316                                 }
03317                                 global $ilSetting;
03318                                 $surveysetting = new ilSetting("survey");
03319                                 $allowedUsers = strlen($surveysetting->get("multiple_survey_users")) ? explode(",",$surveysetting->get("multiple_survey_users")) : array();
03320                                 if (in_array($userid, $allowedUsers))
03321                                 {
03322                                         $result = TRUE;
03323                                 }
03324                         }
03325                 }
03326                 return $result;
03327         }
03328         
03338         function isSurveyStarted($user_id, $anonymize_id)
03339         {
03340                 global $ilDB;
03341 
03342                 if ($this->getAnonymize())
03343                 {
03344                         if ((($user_id != ANONYMOUS_USER_ID) && (strlen($anonymize_id) == 0)) && (!($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys())))
03345                         {
03346                                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03347                                         $ilDB->quote($this->getSurveyId()),
03348                                         $ilDB->quote($user_id)
03349                                 );
03350                         }
03351                         else
03352                         {
03353                                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
03354                                         $ilDB->quote($this->getSurveyId()),
03355                                         $ilDB->quote($anonymize_id)
03356                                 );
03357                         }
03358                 }
03359                 else
03360                 {
03361                         $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03362                                 $ilDB->quote($this->getSurveyId()),
03363                                 $ilDB->quote($user_id)
03364                         );
03365                 }
03366                 $result = $ilDB->query($query);
03367                 if ($result->numRows() == 0)
03368                 {
03369                         return false;
03370                 }                       
03371                 else
03372                 {
03373                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03374                         $_SESSION["finished_id"] = $row["finished_id"];
03375                         return (int)$row["state"];
03376                 }
03377         }
03378         
03388         function getLastActivePage($active_id)
03389         {
03390                 global $ilDB;
03391                 $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE active_fi = %s ORDER BY TIMESTAMP14 DESC",
03392                         $ilDB->quote($active_id)
03393                 );
03394                 $result = $ilDB->query($query);
03395                 if ($result->numRows() == 0)
03396                 {
03397                         return "";
03398                 }
03399                 else
03400                 {
03401                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03402                         return $row["question_fi"];
03403                 }
03404         }
03405 
03416         function checkConstraint($constraint_data, $working_data)
03417         {
03418                 if (count($working_data) == 0)
03419                 {
03420                         return 0;
03421                 }
03422                 
03423                 if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
03424                 {
03425                         return 0;
03426                 }
03427                 
03428                 foreach ($working_data as $data)
03429                 {
03430                         switch ($constraint_data["short"])
03431                         {
03432                                 case "<":
03433                                         if ($data["value"] < $constraint_data["value"])
03434                                         {
03435                                                 return 1;
03436                                         }
03437                                         break;
03438                                 case "<=":
03439                                         if ($data["value"] <= $constraint_data["value"])
03440                                         {
03441                                                 return 1;
03442                                         }
03443                                         break;
03444                                 case "=":
03445                                         if ($data["value"] == $constraint_data["value"])
03446                                         {
03447                                                 return 1;
03448                                         }
03449                                         break;
03450                                 case "<>":
03451                                         if ($data["value"] != $constraint_data["value"])
03452                                         {
03453                                                 return 1;
03454                                         }
03455                                         break;
03456                                 case ">=":
03457                                         if ($data["value"] >= $constraint_data["value"])
03458                                         {
03459                                                 return 1;
03460                                         }
03461                                         break;
03462                                 case ">":
03463                                         if ($data["value"] > $constraint_data["value"])
03464                                         {
03465                                                 return 1;
03466                                         }
03467                                         break;
03468                         }
03469                 }
03470                 return 0;
03471         }
03472         
03473         function _hasDatasets($survey_id)
03474         {
03475                 global $ilDB;
03476                 
03477                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03478                         $ilDB->quote($survey_id . "")
03479                 );
03480                 $result = $ilDB->query($query);
03481                 if ($result->numRows())
03482                 {
03483                         return true;
03484                 }
03485                 else
03486                 {
03487                         return false;
03488                 }
03489         }
03490 
03491         function &getEvaluationForAllUsers()
03492         {
03493                 global $ilDB;
03494                 
03495                 $users = array();
03496                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03497                         $ilDB->quote($this->getSurveyId() . "")
03498                 );
03499                 $result = $ilDB->query($query);
03500                 if ($result->numRows())
03501                 {
03502                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03503                         {
03504                                 array_push($users, $row);
03505                         }
03506                 }
03507                 $evaluation = array();
03508                 $questions =& $this->getSurveyQuestions();
03509                 foreach ($users as $row)
03510                 {
03511                         $evaluation[$row["finished_id"]] = $this->getEvaluationByUser($questions, $row["finished_id"]);
03512                 }
03513                 return $evaluation;
03514         }
03515         
03524         function &getUserSpecificResults()
03525         {
03526                 global $ilDB;
03527                 
03528                 $users = array();
03529                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03530                         $ilDB->quote($this->getSurveyId() . "")
03531                 );
03532                 $result = $ilDB->query($query);
03533                 if ($result->numRows())
03534                 {
03535                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03536                         {
03537                                 array_push($users, $row);
03538                         }
03539                 }
03540                 $evaluation = array();
03541                 $questions =& $this->getSurveyQuestions();
03542                 foreach ($questions as $question_id => $question_data)
03543                 {
03544                         include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
03545                         $question_type = SurveyQuestion::_getQuestionType($question_id);
03546                         include_once "./Modules/SurveyQuestionPool/classes/class.$question_type.php";
03547                         $question = new $question_type();
03548                         $question->loadFromDb($question_id);
03549                         $data =& $question->getUserAnswers($this->getSurveyId());
03550                         $evaluation[$question_id] = $data;
03551                 }
03552                 return $evaluation;
03553         }
03554         
03562         function getUserDataFromActiveId($active_id)
03563         {
03564                 global $ilDB;
03565                 
03566                 $query = sprintf("SELECT * FROM survey_finished WHERE finished_id = %s",
03567                         $ilDB->quote($active_id)
03568                 );
03569                 $result = $ilDB->query($query);
03570                 $userdata = array(
03571                         "fullname" => $this->lng->txt("anonymous"),
03572                         "sortname" => $this->lng->txt("anonymous"),
03573                         "firstname" => "",
03574                         "lastname" => "",
03575                         "login" => "",
03576                         "gender" => "",
03577                         "active_id" => "$active_id"
03578                 );
03579                 if ($result->numRows())
03580                 {
03581                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03582                         if (($row["user_fi"] > 0) && ($row["user_fi"] != ANONYMOUS_USER_ID) && ($this->getAnonymize() == 0))
03583                         {
03584                                 include_once './Services/User/classes/class.ilObjUser.php';
03585                                 if (strlen(ilObjUser::_lookupLogin($row["user_fi"])) == 0)
03586                                 {
03587                                         $userdata["fullname"] = $this->lng->txt("deleted_user");
03588                                 }
03589                                 else
03590                                 {
03591                                         $user = new ilObjUser($row["user_fi"]);
03592                                         $userdata["fullname"] = $user->getFullname();
03593                                         $gender = $user->getGender();
03594                                         if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
03595                                         $userdata["gender"] = $gender;
03596                                         $userdata["firstname"] = $user->getFirstname();
03597                                         $userdata["lastname"] = $user->getLastname();
03598                                         $userdata["sortname"] = $user->getLastname() . ", " . $user->getFirstname();
03599                                         $userdata["login"] = $user->getLogin();
03600                                 }
03601                         }
03602                 }
03603                 return $userdata;
03604         }
03605         
03617         function &getEvaluationByUser($questions, $active_id)
03618         {
03619                 global $ilDB;
03620                 
03621                 // collect all answers
03622                 $answers = array();
03623                 $query = sprintf("SELECT * FROM survey_answer WHERE active_fi = %s",
03624                         $ilDB->quote($active_id)
03625                 );
03626                 $result = $ilDB->query($query);
03627                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03628                 {
03629                         if (!is_array($answers[$row["question_fi"]]))
03630                         {
03631                                 $answers[$row["question_fi"]] = array();
03632                         }
03633                         array_push($answers[$row["question_fi"]], $row);
03634                 }
03635                 $userdata = $this->getUserDataFromActiveId($active_id);
03636                 $resultset = array(
03637                         "name" => $userdata["fullname"],
03638                         "gender" => $userdata["gender"],
03639                         "answers" => array()
03640                 );
03641                 foreach ($questions as $key => $question)
03642                 {
03643                         if (array_key_exists($key, $answers))
03644                         {
03645                                 $resultset["answers"][$key] = $answers[$key];
03646                         }
03647                         else
03648                         {
03649                                 $resultset["answers"][$key] = array();
03650                         }
03651                         sort($resultset["answers"][$key]);
03652                 }
03653                 return $resultset;
03654         }
03655         
03666         function getCumulatedResults(&$question)
03667         {
03668                 global $ilDB;
03669                 
03670                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03671                         $ilDB->quote($this->getSurveyId())
03672                 );
03673                 $result = $ilDB->query($query);
03674                 $nr_of_users = $result->numRows();
03675                 
03676                 $result_array =& $question->getCumulatedResults($this->getSurveyId(), $nr_of_users);
03677                 return $result_array;
03678         }
03679 
03689         function _getNrOfParticipants($survey_id)
03690         {
03691                 global $ilDB;
03692                 
03693                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03694                         $ilDB->quote($survey_id . "")
03695                 );
03696                 $result = $ilDB->query($query);
03697                 return $result->numRows();
03698         }
03699 
03700         function &getQuestions($question_ids)
03701         {
03702                 $result_array = array();
03703                 $query = "SELECT survey_question.*, survey_questiontype.type_tag FROM survey_question, survey_questiontype WHERE survey_question.questiontype_fi = survey_questiontype.questiontype_id AND survey_question.question_id IN ('" . join($question_ids, "','") . "')";
03704                 $result = $ilDB->query($query);
03705                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03706                 {
03707                         array_push($result_array, $row);
03708                 }
03709                 return $result_array;
03710         }
03711         
03712         function &getQuestionblocks($questionblock_ids)
03713         {
03714                 global $ilDB;
03715                 
03716                 $result_array = array();
03717     $query = "SELECT survey_questionblock.*, survey_survey.obj_fi, survey_question.title AS questiontitle, survey_survey_question.sequence, object_data.title as surveytitle, survey_question.question_id FROM object_reference, object_data, survey_questionblock, survey_questionblock_question, survey_survey, survey_question, survey_survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_survey.survey_id = survey_questionblock_question.survey_fi AND survey_questionblock_question.question_fi = survey_question.question_id AND survey_survey.obj_fi = object_reference.obj_id AND object_reference.obj_id = object_data.obj_id AND survey_survey_question.survey_fi = survey_survey.survey_id AND survey_survey_question.question_fi = survey_question.question_id AND survey_questionblock.questionblock_id IN ('" . join($questionblock_ids, "','") . "') ORDER BY survey_survey.survey_id, survey_survey_question.sequence";
03718                 $result = $ilDB->query($query);
03719                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03720                 {
03721                         if ($row["questionblock_id"] != $qbid)
03722                         {
03723                                 $sequence = 1;
03724                         }
03725                         $row["sequence"] = $sequence++;
03726                         $result_array[$row["questionblock_id"]][$row["question_id"]] = $row;
03727                         $qbid = $row["questionblock_id"];
03728                 }
03729                 return $result_array;
03730         }
03731 
03739         function getQuestionsTable($sort, $sortorder, $filter_text, $sel_filter_type, $startrow = 0, $completeonly = 0, $filter_question_type = "", $filter_questionpool = "")
03740         {
03741                 global $ilUser;
03742                 global $ilDB;
03743                 
03744                 $where = "";
03745                 if (strlen($filter_text) > 0) 
03746                 {
03747                         switch($sel_filter_type) 
03748                         {
03749                                 case "title":
03750                                         $where = " AND survey_question.title LIKE " . $ilDB->quote("%" . $filter_text . "%");
03751                                         break;
03752                                 case "description":
03753                                         $where = " AND survey_question.description LIKE " . $ilDB->quote("%" . $filter_text . "%");
03754                                         break;
03755                                 case "author":
03756                                         $where = " AND survey_question.author LIKE " . $ilDB->quote("%" . $filter_text . "%");
03757                                         break;
03758                         }
03759                 }
03760   
03761                 if ($filter_question_type && (strcmp($filter_question_type, "all") != 0))
03762                 {
03763                         $where .= " AND survey_questiontype.type_tag = " . $ilDB->quote($filter_question_type);
03764                 }
03765                 
03766                 if ($filter_questionpool && (strcmp($filter_questionpool, "all") != 0))
03767                 {
03768                         $where .= " AND survey_question.obj_fi = $filter_questionpool";
03769                 }
03770   
03771     // build sort order for sql query
03772                 $order = "";
03773                 $images = array();
03774                 include_once "./Services/Utilities/classes/class.ilUtil.php";
03775                 switch($sort) 
03776                 {
03777                         case "title":
03778                                 $order = " ORDER BY title $sortorder";
03779                                 $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03780                                 break;
03781                         case "description":
03782                                 $order = " ORDER BY description $sortorder";
03783                                 $images["description"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03784                                 break;
03785                         case "type":
03786                                 $order = " ORDER BY questiontype_id $sortorder";
03787                                 $images["type"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03788                                 break;
03789                         case "author":
03790                                 $order = " ORDER BY author $sortorder";
03791                                 $images["author"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03792                                 break;
03793                         case "created":
03794                                 $order = " ORDER BY created $sortorder";
03795                                 $images["created"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03796                                 break;
03797                         case "updated":
03798                                 $order = " ORDER BY TIMESTAMP14 $sortorder";
03799                                 $images["updated"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03800                                 break;
03801                         case "qpl":
03802                                 $order = " ORDER BY obj_fi $sortorder";
03803                                 $images["qpl"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03804                                 break;
03805                 }
03806                 $maxentries = $ilUser->prefs["hits_per_page"];
03807                 if ($maxentries < 1)
03808                 {
03809                         $maxentries = 9999;
03810                 }
03811 
03812                 $spls =& $this->getAvailableQuestionpools($use_obj_id = TRUE, $could_be_offline = FALSE, $showPath = FALSE);
03813                 $forbidden = "";
03814                 $forbidden = " AND survey_question.obj_fi IN ('" . join(array_keys($spls), "','") . "')";
03815                 if ($completeonly)
03816                 {
03817                         $forbidden .= " AND survey_question.complete = " . $ilDB->quote("1");
03818                 }
03819                 $existing = "";
03820                 $existing_questions =& $this->getExistingQuestions();
03821                 if (count($existing_questions))
03822                 {
03823                         $existing = " AND survey_question.question_id NOT IN ('" . join($existing_questions, "','") . "')";
03824                 }
03825                 $limit = " LIMIT $startrow, $maxentries";
03826                 $query = "SELECT survey_question.*, survey_question.TIMESTAMP + 0 AS TIMESTAMP14, survey_questiontype.type_tag, object_reference.ref_id FROM survey_question, survey_questiontype, object_reference WHERE survey_question.questiontype_fi = survey_questiontype.questiontype_id$forbidden$existing AND survey_question.obj_fi = object_reference.obj_id AND ISNULL(survey_question.original_id) " . " $where$order";
03827                 $query_result = $ilDB->query($query);
03828                 $max = $query_result->numRows();
03829                 $query = "SELECT survey_question.*, survey_question.TIMESTAMP + 0 AS TIMESTAMP14, survey_questiontype.type_tag, object_reference.ref_id FROM survey_question, survey_questiontype, object_reference WHERE survey_question.questiontype_fi = survey_questiontype.questiontype_id$forbidden$existing AND survey_question.obj_fi = object_reference.obj_id AND ISNULL(survey_question.original_id) " . " $where$order$limit";
03830                 $query_result = $ilDB->query($query);
03831                 if ($startrow > $max -1)
03832                 {
03833                         $startrow = $max - ($max % $maxentries);
03834                 }
03835                 else if ($startrow < 0)
03836                 {
03837                         $startrow = 0;
03838                 }
03839                 $rows = array();
03840                 if ($query_result->numRows())
03841                 {
03842                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03843                         {
03844                                 array_push($rows, $row);
03845                         }
03846                 }
03847                 $nextrow = $startrow + $maxentries;
03848                 if ($nextrow > $max - 1)
03849                 {
03850                         $nextrow = $startrow;
03851                 }
03852                 $prevrow = $startrow - $maxentries;
03853                 if ($prevrow < 0)
03854                 {
03855                         $prevrow = 0;
03856                 }
03857                 return array(
03858                         "rows" => $rows,
03859                         "images" => $images,
03860                         "startrow" => $startrow,
03861                         "nextrow" => $nextrow,
03862                         "prevrow" => $prevrow,
03863                         "step" => $maxentries,
03864                         "rowcount" => $max
03865                 );
03866         }
03867 
03875         function getQuestionblocksTable($sort, $sortorder, $filter_text, $sel_filter_type, $startrow = 0)
03876         {
03877                 global $ilDB;
03878                 global $ilUser;
03879                 $where = "";
03880                 if (strlen($filter_text) > 0) {
03881                         switch($sel_filter_type) {
03882                                 case "title":
03883                                         $where = " AND survey_questionblock.title LIKE " . $ilDB->quote("%" . $filter_text . "%");
03884                                         break;
03885                         }
03886                 }
03887   
03888     // build sort order for sql query
03889                 $order = "";
03890                 $images = array();
03891                 include_once "./Services/Utilities/classes/class.ilUtil.php";
03892                 switch($sort) 
03893                 {
03894                         case "title":
03895                                 $order = " ORDER BY survey_questionblock.title $sortorder";
03896                                 $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03897                                 break;
03898                         case "svy":
03899                                 $order = " ORDER BY survey_survey_question.survey_fi $sortorder";
03900                                 $images["svy"] = " <img src=\"" . ilUtil::getImagePath(strtolower($sortorder) . "_order.gif") . "\" alt=\"" . strtolower($sortorder) . "ending order\" />";
03901                                 break;
03902                 }
03903                 $maxentries = $ilUser->prefs["hits_per_page"];
03904                 if ($order)
03905                 {
03906                         $order .=  ",survey_survey_question.sequence ASC";
03907                 }
03908                 else
03909                 {
03910                         $order = " ORDER BY survey_survey_question.sequence ASC";
03911                 }
03912                 $query = "SELECT survey_questionblock.questionblock_id FROM object_reference, object_data, survey_questionblock, survey_questionblock_question, survey_survey, survey_question, survey_survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_survey.survey_id = survey_questionblock_question.survey_fi AND survey_questionblock_question.question_fi = survey_question.question_id AND survey_survey.obj_fi = object_reference.obj_id AND object_reference.obj_id = object_data.obj_id AND survey_survey_question.survey_fi = survey_survey.survey_id AND survey_survey_question.question_fi = survey_question.question_id$where GROUP BY survey_questionblock.questionblock_id$order$limit";
03913     $query_result = $ilDB->query($query);
03914                 $questionblock_ids = array();
03915                 if ($query_result->numRows())
03916                 {
03917                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03918                         {
03919                                 array_push($questionblock_ids, $row["questionblock_id"]);
03920                         }
03921                 }
03922                 
03923                 $max = $query_result->numRows();
03924                 if ($startrow > $max -1)
03925                 {
03926                         $startrow = $max - ($max % $maxentries);
03927                 }
03928                 else if ($startrow < 0)
03929                 {
03930                         $startrow = 0;
03931                 }
03932                 $limit = " LIMIT $startrow, $maxentries";
03933                 $query = "SELECT survey_questionblock.*, object_data.title as surveytitle FROM object_reference, object_data, survey_questionblock, survey_questionblock_question, survey_survey, survey_question, survey_survey_question WHERE survey_questionblock.questionblock_id = survey_questionblock_question.questionblock_fi AND survey_survey.survey_id = survey_questionblock_question.survey_fi AND survey_questionblock_question.question_fi = survey_question.question_id AND survey_survey.obj_fi = object_reference.obj_id AND object_reference.obj_id = object_data.obj_id AND survey_survey_question.survey_fi = survey_survey.survey_id AND survey_survey_question.question_fi = survey_question.question_id$where GROUP BY survey_questionblock.questionblock_id$order$limit";
03934     $query_result = $ilDB->query($query);
03935                 $rows = array();
03936                 if ($query_result->numRows())
03937                 {
03938                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03939                         {
03940                                 $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
03941                                 $counter = 1;
03942                                 foreach ($questions_array as $key => $value)
03943                                 {
03944                                         $questions_array[$key] = "$counter. $value";
03945                                         $counter++;
03946                                 }
03947                                 $rows[$row["questionblock_id"]] = array(
03948                                         "questionblock_id" => $row["questionblock_id"],
03949                                         "title" => $row["title"], 
03950                                         "surveytitle" => $row["surveytitle"], 
03951                                         "questions" => join($questions_array, ", "),
03952                                         "owner" => $row["owner_fi"]
03953                                 );
03954                         }
03955                 }
03956                 $nextrow = $startrow + $maxentries;
03957                 if ($nextrow > $max - 1)
03958                 {
03959                         $nextrow = $startrow;
03960                 }
03961                 $prevrow = $startrow - $maxentries;
03962                 if ($prevrow < 0)
03963                 {
03964                         $prevrow = 0;
03965                 }
03966                 return array(
03967                         "rows" => $rows,
03968                         "images" => $images,
03969                         "startrow" => $startrow,
03970                         "nextrow" => $nextrow,
03971                         "prevrow" => $prevrow,
03972                         "step" => $maxentries,
03973                         "rowcount" => $max
03974                 );
03975         }
03976 
03985         function &_getQuestiontypes()
03986         {
03987                 global $ilDB;
03988                 
03989                 $questiontypes = array();
03990                 $query = "SELECT * FROM survey_questiontype ORDER BY type_tag";
03991                 $query_result = $ilDB->query($query);
03992                 while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03993                 {
03994                         array_push($questiontypes, $row["type_tag"]);
03995                 }
03996                 return $questiontypes;
03997         }
03998                 
04007         function toXML()
04008         {
04009                 include_once("./classes/class.ilXmlWriter.php");
04010                 $a_xml_writer = new ilXmlWriter;
04011                 // set xml header
04012                 $a_xml_writer->xmlHeader();
04013                 $attrs = array(
04014                         "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
04015                         "xsi:noNamespaceSchemaLocation" => "http://www.ilias.de/download/xsd/ilias_survey_3_8.xsd"
04016                 );
04017                 $a_xml_writer->xmlStartTag("surveyobject", $attrs);
04018                 $attrs = array(
04019                         "id" => $this->getSurveyId(),
04020                         "title" => $this->getTitle()
04021                 );
04022                 $a_xml_writer->xmlStartTag("survey", $attrs);
04023                 
04024                 $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
04025                 $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
04026                 $a_xml_writer->xmlStartTag("objectives");
04027                 $attrs = array(
04028                         "label" => "introduction"
04029                 );
04030                 $this->addMaterialTag($a_xml_writer, $this->getIntroduction(), TRUE, TRUE, $attrs);
04031                 $attrs = array(
04032                         "label" => "outro"
04033                 );
04034                 $this->addMaterialTag($a_xml_writer, $this->getOutro(), TRUE, TRUE, $attrs);
04035                 $a_xml_writer->xmlEndTag("objectives");
04036 
04037                 if ($this->getAnonymize())
04038                 {
04039                         $attribs = array("enabled" => "1");
04040                 }
04041                 else
04042                 {
04043                         $attribs = array("enabled" => "0");
04044                 }
04045                 $a_xml_writer->xmlElement("anonymisation", $attribs);
04046                 $a_xml_writer->xmlStartTag("restrictions");
04047                 if ($this->getAnonymize() == 2)
04048                 {
04049                         $attribs = array("type" => "free");
04050                 }
04051                 else
04052                 {
04053                         $attribs = array("type" => "restricted");
04054                 }
04055                 $a_xml_writer->xmlElement("access", $attribs);
04056                 if ($this->getStartDateEnabled())
04057                 {
04058                         $attrs = array("type" => "date");
04059                         $a_xml_writer->xmlElement("startingtime", $attrs, sprintf("%04d-%02d-%02dT00:00:00", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
04060                 }
04061                 if ($this->getEndDateEnabled())
04062                 {
04063                         $attrs = array("type" => "date");
04064                         $a_xml_writer->xmlElement("endingtime", $attrs, sprintf("%04d-%02d-%02dT00:00:00", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
04065                 }
04066                 $a_xml_writer->xmlEndTag("restrictions");
04067                 
04068                 // constraints
04069                 $pages =& $this->getSurveyPages();
04070                 $hasconstraints = FALSE;
04071                 foreach ($pages as $question_array)
04072                 {
04073                         foreach ($question_array as $question)
04074                         {
04075                                 if (count($question["constraints"]))
04076                                 {
04077                                         $hasconstraints = TRUE;
04078                                 }
04079                         }
04080                 }
04081                 
04082                 if ($hasconstraints)
04083                 {
04084                         $a_xml_writer->xmlStartTag("constraints");
04085                         foreach ($pages as $question_array)
04086                         {
04087                                 foreach ($question_array as $question)
04088                                 {
04089                                         if (count($question["constraints"]))
04090                                         {
04091                                                 // found constraints
04092                                                 foreach ($question["constraints"] as $constraint)
04093                                                 {
04094                                                         $attribs = array(
04095                                                                 "sourceref" => $question["question_id"],
04096                                                                 "destref" => $constraint["question"],
04097                                                                 "relation" => $constraint["short"],
04098                                                                 "value" => $constraint["value"]
04099                                                         );
04100                                                         $a_xml_writer->xmlElement("constraint", $attribs);
04101                                                 }
04102                                         }
04103                                 }
04104                         }
04105                         $a_xml_writer->xmlEndTag("constraints");
04106                 }
04107                 
04108                 // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
04109                 $a_xml_writer->xmlStartTag("metadata");
04110 
04111                 $a_xml_writer->xmlStartTag("metadatafield");
04112                 $a_xml_writer->xmlElement("fieldlabel", NULL, "evaluation_access");
04113                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getEvaluationAccess());
04114                 $a_xml_writer->xmlEndTag("metadatafield");
04115 
04116                 $a_xml_writer->xmlStartTag("metadatafield");
04117                 $a_xml_writer->xmlElement("fieldlabel", NULL, "status");
04118                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getStatus());
04119                 $a_xml_writer->xmlEndTag("metadatafield");
04120 
04121                 $a_xml_writer->xmlStartTag("metadatafield");
04122                 $a_xml_writer->xmlElement("fieldlabel", NULL, "display_question_titles");
04123                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getShowQuestionTitles());
04124                 $a_xml_writer->xmlEndTag("metadatafield");
04125 
04126                 $a_xml_writer->xmlStartTag("metadatafield");
04127                 $a_xml_writer->xmlElement("fieldlabel", NULL, "SCORM");
04128                 include_once "./Services/MetaData/classes/class.ilMD.php";
04129                 $md = new ilMD($this->getId(),0, $this->getType());
04130                 $writer = new ilXmlWriter();
04131                 $md->toXml($writer);
04132                 $metadata = $writer->xmlDumpMem();
04133                 $a_xml_writer->xmlElement("fieldentry", NULL, $metadata);
04134                 $a_xml_writer->xmlEndTag("metadatafield");
04135 
04136                 $a_xml_writer->xmlEndTag("metadata");
04137                 $a_xml_writer->xmlEndTag("survey");
04138 
04139                 $attribs = array("id" => $this->getId());
04140                 $a_xml_writer->xmlStartTag("surveyquestions", $attribs);
04141                 // add questionblock descriptions
04142                 $obligatory_states =& $this->getObligatoryStates();
04143                 foreach ($pages as $question_array)
04144                 {
04145                         if (count($question_array) > 1)
04146                         {
04147                                 $attribs = array("id" => $question_array[0]["question_id"]);
04148                                 $attribs = array("showQuestiontext" => $question_array[0]["questionblock_show_questiontext"]);
04149                                 $a_xml_writer->xmlStartTag("questionblock", $attribs);
04150                                 if (strlen($question_array[0]["questionblock_title"]))
04151                                 {
04152                                         $a_xml_writer->xmlElement("questionblocktitle", NULL, $question_array[0]["questionblock_title"]);
04153                                 }
04154                         }
04155                         foreach ($question_array as $question)
04156                         {
04157                                 if (strlen($question["heading"]))
04158                                 {
04159                                         $a_xml_writer->xmlElement("textblock", NULL, $question["heading"]);
04160                                 }
04161                                 $questionObject =& $this->_instanciateQuestion($question["question_id"]);
04162                                 if ($questionObject !== FALSE) $questionObject->insertXML($a_xml_writer, FALSE, $obligatory_states[$question["question_id"]]);
04163                         }
04164                         if (count($question_array) > 1)
04165                         {
04166                                 $a_xml_writer->xmlEndTag("questionblock");
04167                         }
04168                 }
04169 
04170                 $a_xml_writer->xmlEndTag("surveyquestions");
04171                 $a_xml_writer->xmlEndTag("surveyobject");
04172                 $xml = $a_xml_writer->xmlDumpMem(FALSE);
04173                 return $xml;
04174         }
04175         
04185   function &_instanciateQuestion($question_id) 
04186         {
04187                 if ($question_id < 1) return FALSE;
04188                 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
04189                 $question_type = SurveyQuestion::_getQuestionType($question_id);
04190                 if (strlen($question_type) == 0) return FALSE;
04191                 include_once "./Modules/SurveyQuestionPool/classes/class.$question_type.php";
04192                 $question = new $question_type();
04193                 $question->loadFromDb($question_id);
04194                 return $question;
04195   }
04196 
04205         function locateImportFiles($a_dir)
04206         {
04207                 if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
04208                 {
04209                         return;
04210                 }
04211                 $importDirectory = "";
04212                 $xmlFile = "";
04213 
04214                 $current_dir = opendir($a_dir);
04215                 $files = array();
04216                 while($entryname = readdir($current_dir))
04217                 {
04218                         $files[] = $entryname;
04219                 }
04220 
04221                 foreach($files as $file)
04222                 {
04223                         if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
04224                         {
04225                                 // found directory created by zip
04226                                 $importDirectory = $a_dir."/".$file;
04227                         }
04228                 }
04229                 closedir($current_dir);
04230                 if (strlen($importDirectory))
04231                 {
04232                         // find the xml file
04233                         $current_dir = opendir($importDirectory);
04234                         $files = array();
04235                         while($entryname = readdir($current_dir))
04236                         {
04237                                 $files[] = $entryname;
04238                         }
04239                         foreach($files as $file)
04240                         { 
04241                                 if(@is_file($importDirectory."/".$file) && ($file != "." && $file!="..") && (strcmp(strtolower(substr($file, -4, 4)), ".xml") == 0))
04242                                 {
04243                                         // found xml file
04244                                         $xmlFile = $importDirectory."/".$file;
04245                                 }
04246                         }
04247                 }
04248                 return array("dir" => $importDirectory, "xml" => $xmlFile);
04249         }
04250 
04259         function importObject($file_info, $survey_questionpool_id)
04260         {
04261                 // check if file was uploaded
04262                 $source = $file_info["tmp_name"];
04263                 $error = "";
04264                 if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
04265                 {
04266                         $error = $this->lng->txt("import_no_file_selected");
04267                 }
04268                 // check correct file type
04269                 $isXml = FALSE;
04270                 $isZip = FALSE;
04271                 if ((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0))
04272                 {
04273                         $isXml = TRUE;
04274                 }
04275                 if (strpos($file_info["type"], "zip") !== FALSE)
04276                 {
04277                         $isZip = TRUE;
04278                 }
04279                 if (!$isXml && !$isZip)
04280                 {
04281                         $error = $this->lng->txt("import_wrong_file_type");
04282                         global $ilLog;
04283                         $ilLog->write("Survey: Import error. Filetype was \"" . $file_info["type"] ."\"");
04284                 }
04285                 if (strlen($error) == 0)
04286                 {
04287                         // import file as a survey
04288                         $import_dir = $this->getImportDirectory();
04289                         $import_subdir = "";
04290                         $importfile = "";
04291                         include_once "./Services/Utilities/classes/class.ilUtil.php";
04292                         if ($isZip)
04293                         {
04294                                 $importfile = $import_dir."/".$file_info["name"];
04295                                 ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
04296                                 ilUtil::unzip($importfile);
04297                                 $found = $this->locateImportFiles($import_dir);
04298                                 if (!((strlen($found["dir"]) > 0) && (strlen($found["xml"]) > 0)))
04299                                 {
04300                                         $error = $this->lng->txt("wrong_import_file_structure");
04301                                         return $error;
04302                                 }
04303                                 $importfile = $found["xml"];
04304                                 $import_subdir = $found["dir"];
04305                         }
04306                         else
04307                         {
04308                                 $importfile = tempnam($import_dir, "survey_import");
04309                                 ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
04310                         }
04311                         $fh = fopen($importfile, "r");
04312                         if (!$fh)
04313                         {
04314                                 $error = $this->lng->txt("import_error_opening_file");
04315                                 return $error;
04316                         }
04317                         $xml = fread($fh, filesize($importfile));
04318                         $result = fclose($fh);
04319                         if (!$result)
04320                         {
04321                                 $error = $this->lng->txt("import_error_closing_file");
04322                                 return $error;
04323                         }
04324 
04325                         unset($_SESSION["import_mob_xhtml"]);
04326                         if (strpos($xml, "questestinterop"))
04327                         {
04328                                 include_once "./Services/Survey/classes/class.SurveyImportParserPre38.php";
04329                                 include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
04330                                 $spl = new ilObjSurveyQuestionPool($survey_questionpool_id, FALSE);
04331                                 $import = new SurveyImportParserPre38($spl, "", TRUE);
04332                                 $import->setSurveyObject($this);
04333                                 $import->setXMLContent($xml);
04334                                 $import->startParsing();
04335                         }
04336                         else
04337                         {
04338                                 include_once "./Services/Survey/classes/class.SurveyImportParser.php";
04339                                 include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
04340                                 $spl = new ilObjSurveyQuestionPool($survey_questionpool_id, FALSE);
04341                                 $import = new SurveyImportParser($spl, "", TRUE);
04342                                 $import->setSurveyObject($this);
04343                                 $import->setXMLContent($xml);
04344                                 $import->startParsing();
04345                         }
04346 
04347                         if (is_array($_SESSION["import_mob_xhtml"]))
04348                         {
04349                                 include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
04350                                 include_once "./Services/RTE/classes/class.ilRTE.php";
04351                                 include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
04352                                 foreach ($_SESSION["import_mob_xhtml"] as $mob)
04353                                 {
04354                                         $importfile = $import_subdir . "/" . $mob["uri"];
04355                                         if (file_exists($importfile))
04356                                         {
04357                                                 $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
04358                                                 ilObjMediaObject::_saveUsage($media_object->getId(), "svy:html", $this->getId());
04359                                                 $this->setIntroduction(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getIntroduction()));
04360                                                 $this->setOutro(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getOutro()));
04361                                         }
04362                                         else
04363                                         {
04364                                                 global $ilLog;
04365                                                 $ilLog->write("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
04366                                         }
04367                                 }
04368                                 $this->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($this->getIntroduction(), 1));
04369                                 $this->setOutro(ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 1));
04370                                 $this->saveToDb();
04371                         }
04372 
04373                         // delete import directory
04374                         ilUtil::delDir($this->getImportDirectory());
04375                 }
04376                 return $error;
04377         }
04378 
04387         function &_getAvailableSurveys($use_object_id = false)
04388         {
04389                 global $ilUser;
04390                 global $ilDB;
04391 
04392                 $result_array = array();
04393                 $surveys = ilUtil::_getObjectsByOperations("svy","write", $ilUser->getId(), -1);
04394                 if (count($surveys))
04395                 {
04396                         $titles = ilObject::_prepareCloneSelection($surveys, "svy");
04397                         $query = sprintf("SELECT object_data.*, object_reference.ref_id FROM object_data, object_reference WHERE object_data.obj_id = object_reference.obj_id AND object_reference.ref_id IN ('%s') ORDER BY object_data.title",
04398                                 implode("','", $surveys)
04399                         );
04400                         $result = $ilDB->query($query);
04401                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04402                         {
04403                                 if ($use_object_id)
04404                                 {
04405                                         $result_array[$row["obj_id"]] = $titles[$row["ref_id"]];
04406                                 }
04407                                 else
04408                                 {
04409                                         $result_array[$row["ref_id"]] = $titles[$row["ref_id"]];
04410                                 }
04411                         }
04412                 }
04413                 return $result_array;
04414         }       
04415         
04424         public function cloneObject($a_target_id,$a_copy_id = 0)
04425         {
04426                 global $ilDB;
04427                 
04428                 $this->loadFromDb();
04429                 
04430                 // Copy settings
04431                 $newObj = parent::cloneObject($a_target_id,$a_copy_id);
04432                 $this->cloneMetaData($newObj);
04433                 
04434                 $newObj->author = $this->getAuthor();
04435                 $newObj->introduction = $this->getIntroduction();
04436                 $newObj->outro = $this->getOutro();
04437                 $newObj->status = $this->getStatus();
04438                 $newObj->evaluation_access = $this->getEvaluationAccess();
04439                 $newObj->start_date = $this->getStartDate();
04440                 $newObj->startdate_enabled = $this->getStartDateEnabled();
04441                 $newObj->end_date = $this->getEndDate();
04442                 $newObj->enddate_enabled = $this->getEndDateEnabled();
04443                 $newObj->invitation = $this->getInvitation();
04444                 $newObj->invitation_mode = $this->getInvitationMode();
04445                 $newObj->anonymize = $this->getAnonymize();
04446 
04447                 $question_pointer = array();
04448                 // clone the questions
04449                 $mapping = array();
04450                 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
04451                 foreach ($this->questions as $key => $question_id)
04452                 {
04453                         $question = ilObjSurvey::_instanciateQuestion($question_id);
04454                         $question->id = -1;
04455                         $original_id = SurveyQuestion::_getOriginalId($question_id);
04456                         $question->saveToDb($original_id);
04457                         $newObj->questions[$key] = $question->getId();
04458                         $question_pointer[$question_id] = $question->getId();
04459                         $mapping[$question_id] = $question->getId();
04460                 }
04461 
04462                 $newObj->saveToDb();            
04463                 $newObj->cloneTextblocks($mapping);
04464 
04465                 // clone the questionblocks
04466                 $questionblocks = array();
04467                 $questionblock_questions = array();
04468                 $query = sprintf("SELECT * FROM survey_questionblock_question WHERE survey_fi = %s",
04469                         $ilDB->quote($this->getSurveyId() . "")
04470                 );
04471                 $result = $ilDB->query($query);
04472                 if ($result->numRows() > 0)
04473                 {
04474                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04475                         {
04476                                 array_push($questionblock_questions, $row);
04477                                 $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
04478                         }
04479                 }
04480                 // create new questionblocks
04481                 foreach ($questionblocks as $key => $value)
04482                 {
04483                         $questionblock = ilObjSurvey::_getQuestionblock($key);
04484                         $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
04485                         $questionblocks[$key] = $questionblock_id;
04486                 }
04487                 // create new questionblock questions
04488                 foreach ($questionblock_questions as $key => $value)
04489                 {
04490                         $clonequery = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
04491                                 $ilDB->quote($newObj->getSurveyId() . ""),
04492                                 $ilDB->quote($questionblocks[$value["questionblock_fi"]] . ""),
04493                                 $ilDB->quote($question_pointer[$value["question_fi"]] . "")
04494                         );
04495                         $cloneresult = $ilDB->query($clonequery);
04496                 }
04497                 
04498                 // clone the constraints
04499                 $constraints = ilObjSurvey::_getConstraints($this->getSurveyId());
04500                 foreach ($constraints as $key => $constraint)
04501                 {
04502                         $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
04503                 }
04504                 
04505                 // clone the obligatory states
04506                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
04507                         $ilDB->quote($this->getSurveyId() . "")
04508                 );
04509                 $result = $ilDB->query($query);
04510                 if ($result->numRows() > 0)
04511                 {
04512                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04513                         {
04514                                 $clonequery = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
04515                                         $ilDB->quote($newObj->getSurveyId() . ""),
04516                                         $ilDB->quote($question_pointer[$row["question_fi"]] . ""),
04517                                         $ilDB->quote($row["obligatory"])
04518                                 );
04519                                 $cloneresult = $ilDB->query($clonequery);
04520                         }
04521                 }
04522                 return $newObj;
04523         }
04524         
04525         function getTextblock($question_id)
04526         {
04527                 global $ilDB;
04528                 $query = sprintf("SELECT * FROM survey_survey_question WHERE question_fi = %s",
04529                         $ilDB->quote($question_id . "")
04530                 );
04531                 $result = $ilDB->query($query);
04532                 if ($result->numRows())
04533                 {
04534                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04535                         return $row["heading"];
04536                 }
04537                 else
04538                 {
04539                         return "";
04540                 }
04541         }
04542 
04550         function cloneTextblocks($mapping)
04551         {
04552                 foreach ($mapping as $original_id => $new_id)
04553                 {
04554                         $textblock = $this->getTextblock($original_id);
04555                         include_once "./classes/class.ilObjAdvancedEditing.php";
04556                         $this->saveHeading(ilUtil::stripSlashes($textblock, TRUE, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("survey")), $new_id);
04557                 }
04558         }
04559 
04565         function createExportDirectory()
04566         {
04567                 include_once "./Services/Utilities/classes/class.ilUtil.php";
04568                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04569                 ilUtil::makeDir($svy_data_dir);
04570                 if(!is_writable($svy_data_dir))
04571                 {
04572                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04573                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04574                 }
04575                 
04576                 // create learning module directory (data_dir/lm_data/lm_<id>)
04577                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04578                 ilUtil::makeDir($svy_dir);
04579                 if(!@is_dir($svy_dir))
04580                 {
04581                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04582                 }
04583                 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
04584                 $export_dir = $svy_dir."/export";
04585                 ilUtil::makeDir($export_dir);
04586                 if(!@is_dir($export_dir))
04587                 {
04588                         $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
04589                 }
04590         }
04591 
04595         function getExportDirectory()
04596         {
04597                 include_once "./Services/Utilities/classes/class.ilUtil.php";
04598                 $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
04599 
04600                 return $export_dir;
04601         }
04602         
04606         function getExportFiles($dir)
04607         {
04608                 // quit if import dir not available
04609                 if (!@is_dir($dir) or
04610                         !is_writeable($dir))
04611                 {
04612                         return array();
04613                 }
04614 
04615                 // open directory
04616                 $dir = dir($dir);
04617 
04618                 // initialize array
04619                 $file = array();
04620 
04621                 // get files and save the in the array
04622                 while ($entry = $dir->read())
04623                 {
04624                         if ($entry != "." and
04625                                 $entry != ".." and
04626                                 ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml|zip\$", $entry))
04627                         {
04628                                 $file[] = $entry;
04629                         }
04630                 }
04631 
04632                 // close import directory
04633                 $dir->close();
04634                 // sort files
04635                 sort ($file);
04636                 reset ($file);
04637 
04638                 return $file;
04639         }
04640 
04646         function createImportDirectory()
04647         {
04648                 include_once "./Services/Utilities/classes/class.ilUtil.php";
04649                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04650                 ilUtil::makeDir($svy_data_dir);
04651                 
04652                 if(!is_writable($svy_data_dir))
04653                 {
04654                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04655                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04656                 }
04657 
04658                 // create test directory (data_dir/svy_data/svy_<id>)
04659                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04660                 ilUtil::makeDir($svy_dir);
04661                 if(!@is_dir($svy_dir))
04662                 {
04663                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04664                 }
04665 
04666                 // create import subdirectory (data_dir/svy_data/svy_<id>/import)
04667                 $import_dir = $svy_dir."/import";
04668                 ilUtil::makeDir($import_dir);
04669                 if(!@is_dir($import_dir))
04670                 {
04671                         $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
04672                 }
04673         }
04674 
04678         function getImportDirectory()
04679         {
04680                 include_once "./Services/Utilities/classes/class.ilUtil.php";
04681                 $import_dir = ilUtil::getDataDir()."/svy_data".
04682                         "/svy_".$this->getId()."/import";
04683                 if (!is_dir($import_dir))
04684                 {
04685                         ilUtil::makeDirParents($import_dir);
04686                 }
04687                 if(@is_dir($import_dir))
04688                 {
04689                         return $import_dir;
04690                 }
04691                 else
04692                 {
04693                         return false;
04694                 }
04695         }
04696         
04697         function saveHeading($heading = "", $insertbefore)
04698         {
04699                 global $ilDB;
04700                 if ($heading)
04701                 {
04702                         $query = sprintf("UPDATE survey_survey_question SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
04703                                 $ilDB->quote($heading),
04704                                 $ilDB->quote($this->getSurveyId() . ""),
04705                                 $ilDB->quote($insertbefore)
04706                         );
04707                 }
04708                 else
04709                 {
04710                         $query = sprintf("UPDATE survey_survey_question SET heading=NULL WHERE survey_fi=%s AND question_fi=%s",
04711                                 $ilDB->quote($this->getSurveyId() . ""),
04712                                 $ilDB->quote($insertbefore)
04713                         );
04714                 }
04715                 $ilDB->query($query);
04716         }
04717 
04718         function _getRefIdFromObjId($obj_id)
04719         {
04720                 global $ilDB;
04721                 
04722                 $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
04723                         $ilDB->quote($obj_id)
04724                         
04725                 );
04726                 $result = $ilDB->query($query);
04727                 if ($result->numRows())
04728                 {
04729                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04730                         return $row["ref_id"];
04731                 }
04732                 return 0;
04733         }
04734         
04735         function isAnonymousKey($key)
04736         {
04737                 global $ilDB;
04738                 
04739                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_key = %s AND survey_fi = %s",
04740                         $ilDB->quote($key . ""),
04741                         $ilDB->quote($this->getSurveyId() . "")
04742                 );
04743                 $result = $ilDB->query($query);
04744                 if ($result->numRows() == 1)
04745                 {
04746                         return true;
04747                 }
04748                 else
04749                 {
04750                         return false;
04751                 }
04752         }
04753         
04754         function getUserSurveyCode($user_id)
04755         {
04756                 global $ilDB;
04757                 
04758                 if (($user_id == ANONYMOUS_USER_ID) || (($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys()))) return "";
04759                 $query = sprintf("SELECT anonymous_id FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
04760                         $ilDB->quote($this->getSurveyId() . ""),
04761                         $ilDB->quote($user_id . "")
04762                 );
04763                 $result = $ilDB->query($query);
04764                 if ($result->numRows() == 1)
04765                 {
04766                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04767                         return $row["anonymous_id"];
04768                 }
04769                 else
04770                 {
04771                         return "";
04772                 }
04773         }
04774         
04775         function isAnonymizedParticipant($key)
04776         {
04777                 global $ilDB;
04778                 
04779                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE anonymous_id = %s AND survey_fi = %s",
04780                         $ilDB->quote($key . ""),
04781                         $ilDB->quote($this->getSurveyId() . "")
04782                 );
04783                 $result = $ilDB->query($query);
04784                 if ($result->numRows() == 1)
04785                 {
04786                         return true;
04787                 }
04788                 else
04789                 {
04790                         return false;
04791                 }
04792         }
04793         
04794         function checkSurveyCode($code)
04795         {
04796                 if ($this->isAnonymousKey($code))
04797                 {
04798                         if ($this->isSurveyStarted("", $code) == 1)
04799                         {
04800                                 return false;
04801                         }
04802                         else
04803                         {
04804                                 return true;
04805                         }
04806                 }
04807                 else
04808                 {
04809                         return false;
04810                 }
04811         }
04812 
04821         function getSurveyCodesCount()
04822         {
04823                 global $ilDB;
04824 
04825                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_fi = %s AND ISNULL(user_key)",
04826                         $ilDB->quote($this->getSurveyId() . "")
04827                 );
04828                 $result = $ilDB->query($query);
04829                 return $result->numRows();
04830         }
04831         
04841         function getSurveyCodesForExport($a_array)
04842         {
04843                 global $ilDB;
04844 
04845 /*              $query = sprintf("SELECT * FROM survey_anonymous WHERE survey_fi = %s AND ISNULL(user_key)",
04846                         $ilDB->quote($this->getSurveyId() . "")
04847                 );
04848 */
04849                 $query = sprintf("SELECT survey_anonymous.*, survey_anonymous.TIMESTAMP + 0 AS TIMESTAMP14, survey_finished.state FROM survey_anonymous LEFT JOIN survey_finished ON survey_anonymous.survey_key = survey_finished.anonymous_id WHERE survey_anonymous.survey_fi = %s AND ISNULL(survey_anonymous.user_key)",
04850                         $ilDB->quote($this->getSurveyId() . "")
04851                 );
04852                 $result = $ilDB->query($query);
04853 
04854 
04855                 $result = $ilDB->query($query);
04856                 $export = "";
04857                 $lang = ($_POST["lang"] != 1) ? "&amp;lang=" . $_POST["lang"] : "";
04858                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04859                 {
04860                         if (in_array($row["survey_key"], $a_array) || (count($a_array) == 0))
04861                         {
04862                                 $export .= $row["survey_key"] . ",";
04863                                 $created = ilFormat::formatDate(ilFormat::ftimestamp2dateDB($row["TIMESTAMP14"]), "date");
04864                                 $export .= "$created,";
04865                                 if ($this->isSurveyCodeUsed($row["survey_key"]))
04866                                 {
04867                                         $export .= "1,";
04868                                 }
04869                                 else
04870                                 {
04871                                         $export .= "0,";
04872                                 }
04873                                 $url = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$row["survey_key"].$lang;
04874                                 $export .= $url . "\n";
04875                         }
04876                 }
04877                 return $export;
04878         }
04879         
04891         function &getSurveyCodesTableData($lang = "en", $offset = 0, $limit = 10)
04892         {
04893                 global $ilDB;
04894 
04895                 include_once "./classes/class.ilFormat.php";
04896                 if (strlen($lang) == 0) $lang = "en";
04897                 if (strlen($offset) == 0) $offset = 0;
04898                 if (strlen($limit) == 0) $limit = 10;
04899                 
04900                 $order = "ORDER BY TIMESTAMP14, survey_key ASC";
04901                 $codes = array();
04902                 $query = sprintf("SELECT survey_anonymous.anonymous_id, survey_anonymous.survey_key, survey_anonymous.survey_fi, survey_anonymous.TIMESTAMP + 0 AS TIMESTAMP14, survey_finished.state FROM survey_anonymous LEFT JOIN survey_finished ON survey_anonymous.survey_key = survey_finished.anonymous_id WHERE survey_anonymous.survey_fi = %s AND ISNULL(survey_anonymous.user_key) $order LIMIT $offset,$limit",
04903                         $ilDB->quote($this->getSurveyId() . "")
04904                 );
04905                 $result = $ilDB->query($query);
04906                 $counter = $offset+1;
04907                 if ($result->numRows() > 0)
04908                 {
04909                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04910                         {
04911                                 $created = ilFormat::formatDate(ilFormat::ftimestamp2datetimeDB($row["TIMESTAMP14"]));
04912 
04913                                 $url = "";
04914                                 
04915                                 $state = "<span class=\"smallred\">" . $this->lng->txt("not_used") . "</span>";
04916                                 if ($this->isSurveyCodeUsed($row["survey_key"]))
04917                                 {
04918                                         $state = "<span class=\"smallgreen\">" . $this->lng->txt("used") . "</span>";
04919                                 }
04920                                 else
04921                                 {
04922                                         $addlang = "";
04923                                         if (strlen($lang))
04924                                         {
04925                                                 $addlang = "&amp;lang=$lang";
04926                                         }
04927                                         $url = "<a href=\"" . ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$row["survey_key"].$addlang . "\">";
04928                                         $url .= $this->lng->txt("survey_code_url_name");
04929                                         $url .= "</a>";
04930                                 }
04931                                 $counter = "<input type=\"checkbox\" name=\"chb_code[]\" value=\"" . $row["survey_key"] . "\"/>";
04932                                 array_push($codes, array($counter, $row["survey_key"], $created, $state, $url));
04933                                 $counter++;
04934                         }
04935                 }
04936                 return $codes;
04937         }
04938 
04939         function isSurveyCodeUsed($code)
04940         {
04941                 global $ilDB;
04942                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
04943                         $ilDB->quote($this->getSurveyId() . ""),
04944                         $ilDB->quote($code)
04945                 );
04946                 $result = $ilDB->query($query);
04947                 if ($result->numRows() > 0)
04948                 {
04949                         return TRUE;
04950                 }
04951                 else
04952                 {
04953                         return FALSE;
04954                 }
04955         }
04956         
04957         function createSurveyCodes($nrOfCodes)
04958         {
04959                 global $ilDB;
04960                 for ($i = 0; $i < $nrOfCodes; $i++)
04961                 {
04962                         $anonymize_key = $this->createNewAccessCode();
04963                         $query = sprintf("INSERT INTO survey_anonymous (anonymous_id, survey_key, survey_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
04964                                 $ilDB->quote($anonymize_key . ""),
04965                                 $ilDB->quote($this->getSurveyId() . "")
04966                         );
04967                         $result = $ilDB->query($query);
04968                 }
04969         }
04970 
04978         function deleteSurveyCode($survey_code)
04979         {
04980                 global $ilDB;
04981                 
04982                 if (strlen($survey_code) > 0)
04983                 {
04984                         $query = sprintf("DELETE FROM survey_anonymous WHERE survey_fi = %s AND survey_key = %s",
04985                                 $ilDB->quote($this->getSurveyId() . ""),
04986                                 $ilDB->quote($survey_code)
04987                         );
04988                         $ilDB->query($query);
04989                 }
04990         }
04991         
05000         function getUserAccessCode($user_id)
05001         {
05002                 global $ilDB;
05003                 $access_code = "";
05004                 $query = sprintf("SELECT survey_key FROM survey_anonymous WHERE survey_fi = %s AND user_key = %s",
05005                         $ilDB->quote($this->getSurveyId() . ""),
05006                         $ilDB->quote(md5($user_id))
05007                 );
05008                 $result = $ilDB->query($query);
05009                 if ($result->numRows())
05010                 {
05011                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
05012                         $access_code = $row["survey_key"];
05013                 }
05014                 return $access_code;
05015         }
05016         
05025         function saveUserAccessCode($user_id, $access_code)
05026         {
05027                 global $ilDB;
05028                 $query = sprintf("INSERT INTO survey_anonymous (survey_key, survey_fi, user_key) VALUES (%s, %s, %s)",
05029                         $ilDB->quote($access_code . ""),
05030                         $ilDB->quote($this->getSurveyId() . ""),
05031                         $ilDB->quote(md5($user_id) . "")
05032                 );
05033                 $result = $ilDB->query($query);
05034         }
05035         
05041         function createNewAccessCode()
05042         {
05043                 // create a 5 character code
05044                 $codestring = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
05045                 mt_srand();
05046                 $code = "";
05047                 for ($i = 1; $i <=5; $i++)
05048                 {
05049                         $index = mt_rand(0, strlen($codestring)-1);
05050                         $code .= substr($codestring, $index, 1);
05051                 }
05052                 // verify it against the database
05053                 while ($this->isSurveyCodeUsed($code))
05054                 {
05055                         $code = $this->createNewAccessCode();
05056                 }
05057                 return $code;
05058         }
05059         
05060 
05073         function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
05074         {
05075                 $resultarray = array();
05076                 foreach ($row as $rowindex => $entry)
05077                 {
05078                         $surround = FALSE;
05079                         if ($quoteAll)
05080                         {
05081                                 $surround = TRUE;
05082                         }
05083                         if (strpos($entry, "\"") !== FALSE)
05084                         {
05085                                 $entry = str_replace("\"", "\"\"", $entry);
05086                                 $surround = TRUE;
05087                         }
05088                         if (strpos($entry, $separator) !== FALSE)
05089                         {
05090                                 $surround = TRUE;
05091                         }
05092                         // replace all CR LF with LF (for Excel for Windows compatibility
05093                         $entry = str_replace(chr(13).chr(10), chr(10), $entry);
05094                         if ($surround)
05095                         {
05096                                 $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
05097                         }
05098                         else
05099                         {
05100                                 $resultarray[$rowindex] = utf8_decode($entry);
05101                         }
05102                 }
05103                 return $resultarray;
05104         }
05105 
05106         function _getLastAccess($finished_id)
05107         {
05108                 global $ilDB;
05109                 
05110                 $query = sprintf("SELECT TIMESTAMP+0 AS TIMESTAMP14 FROM survey_answer WHERE active_fi = %s ORDER BY TIMESTAMP DESC",
05111                         $ilDB->quote($finished_id . "")
05112                 );
05113                 $result = $ilDB->query($query);
05114                 if ($result->numRows())
05115                 {
05116                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
05117                         return $row["TIMESTAMP14"];
05118                 }
05119                 else
05120                 {
05121                         $query = sprintf("SELECT TIMESTAMP+0 AS TIMESTAMP14 FROM survey_finished WHERE finished_id = %s",
05122                                 $ilDB->quote($finished_id . "")
05123                         );
05124                         $result = $ilDB->query($query);
05125                         if ($result->numRows())
05126                         {
05127                                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
05128                                 return $row["TIMESTAMP14"];
05129                         }
05130                 }
05131                 return "";
05132         }
05133 
05140         function prepareTextareaOutput($txt_output)
05141         {
05142                 include_once "./Services/Utilities/classes/class.ilUtil.php";
05143                 return ilUtil::prepareTextareaOutput($txt_output, $prepare_for_latex_output);
05144         }
05145 
05153         function isHTML($a_text)
05154         {
05155                 if (preg_match("/<[^>]*?>/", $a_text))
05156                 {
05157                         return TRUE;
05158                 }
05159                 else
05160                 {
05161                         return FALSE; 
05162                 }
05163         }
05164 
05173         function addMaterialTag(&$a_xml_writer, $a_material, $close_material_tag = TRUE, $add_mobs = TRUE, $attribs = NULL)
05174         {
05175                 include_once "./Services/RTE/classes/class.ilRTE.php";
05176                 include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
05177 
05178                 $a_xml_writer->xmlStartTag("material", $attribs);
05179                 $attrs = array(
05180                         "type" => "text/plain"
05181                 );
05182                 if ($this->isHTML($a_material))
05183                 {
05184                         $attrs["type"] = "text/xhtml";
05185                 }
05186                 $mattext = ilRTE::_replaceMediaObjectImageSrc($a_material, 0);
05187                 $a_xml_writer->xmlElement("mattext", $attrs, $mattext);
05188 
05189                 if ($add_mobs)
05190                 {
05191                         $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
05192                         foreach ($mobs as $mob)
05193                         {
05194                                 $mob_id = "il_" . IL_INST_ID . "_mob_" . $mob;
05195                                 if (strpos($mattext, $mob_id) !== FALSE)
05196                                 {
05197                                         $mob_obj =& new ilObjMediaObject($mob);
05198                                         $imgattrs = array(
05199                                                 "label" => $mob_id,
05200                                                 "uri" => "objects/" . "il_" . IL_INST_ID . "_mob_" . $mob . "/" . $mob_obj->getTitle()
05201                                         );
05202                                         $a_xml_writer->xmlElement("matimage", $imgattrs, NULL);
05203                                 }
05204                         }
05205                 }               
05206                 if ($close_material_tag) $a_xml_writer->xmlEndTag("material");
05207         }
05208 
05217         function canExportSurveyCode()
05218         {
05219                 if ($this->getAnonymize() != ANONYMIZE_OFF)
05220                 {
05221                         if ($this->surveyCodeSecurity == FALSE)
05222                         {
05223                                 return TRUE;
05224                         }
05225                 }
05226                 return FALSE;
05227         }
05228 
05238         function processPrintoutput2FO($print_output)
05239         {
05240                 $print_output = str_replace("&nbsp;", "&#160;", $print_output);
05241                 $print_output = str_replace("&otimes;", "X", $print_output);
05242                 $xsl = file_get_contents("./Modules/Survey/xml/question2fo.xsl");
05243                 $args = array( '/_xml' => $print_output, '/_xsl' => $xsl );
05244                 $xh = xslt_create();
05245                 $params = array();
05246                 $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, $params);
05247                 xslt_error($xh);
05248                 xslt_free($xh);
05249                 global $ilLog; $ilLog->write($output);
05250                 return $output;
05251         }
05252         
05261         function deliverPDFfromFO($fo)
05262         {
05263                 include_once "./Services/Utilities/classes/class.ilUtil.php";
05264                 $fo_file = ilUtil::ilTempnam() . ".fo";
05265                 $fp = fopen($fo_file, "w"); fwrite($fp, $fo); fclose($fp);
05266                 include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
05267                 $fo2pdf = new ilFO2PDF();
05268                 $fo2pdf->setFOString($fo);
05269                 $result = $fo2pdf->send();
05270                 ilUtil::deliverData($result, ilUtil::getASCIIFilename($this->getTitle()) . ".pdf", "application/pdf");
05271         }
05272         
05273         function _checkCondition($a_svy_id,$a_operator,$a_value)
05274         {
05275                 global $ilUser;
05276 
05277                 switch($a_operator)
05278                 {
05279                         case 'finished':
05280                                 //if (ilExerciseMembers::_lookupStatus($a_exc_id, $ilias->account->getId()) == "passed")
05281                                 include_once("./Modules/Survey/classes/class.ilObjSurveyAccess.php");
05282                                 if (ilObjSurveyAccess::_lookupFinished($a_svy_id, $ilUser->getId()))
05283                                 {
05284                                         return true;
05285                                 }
05286                                 else
05287                                 {
05288                                         return false;
05289                                 }
05290                                 break;
05291 
05292                         default:
05293                                 return true;
05294                 }
05295                 return true;
05296         }
05297 
05298 } // END class.ilObjSurvey
05299 ?>

Generated on Fri Dec 13 2013 17:56:52 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1