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

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-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 require_once "./classes/class.ilObject.php";
00036 //require_once "./classes/class.ilMetaData.php";
00037 require_once "./survey/classes/class.SurveyQuestion.php";
00038 require_once "./survey/classes/class.SurveyNominalQuestionGUI.php";
00039 require_once "./survey/classes/class.SurveyOrdinalQuestionGUI.php";
00040 require_once "./survey/classes/class.SurveyTextQuestionGUI.php";
00041 require_once "./survey/classes/class.SurveyMetricQuestionGUI.php";
00042 
00043 define("STATUS_OFFLINE", 0);
00044 define("STATUS_ONLINE", 1);
00045 
00046 define("EVALUATION_ACCESS_OFF", 0);
00047 define("EVALUATION_ACCESS_ON", 1);
00048 
00049 define("INVITATION_OFF", 0);
00050 define("INVITATION_ON", 1);
00051 
00052 define("MODE_UNLIMITED", 0);
00053 define("MODE_PREDEFINED_USERS", 1);
00054 
00055 define("SURVEY_START_ALLOWED", 0);
00056 define("SURVEY_START_START_DATE_NOT_REACHED", 1);
00057 define("SURVEY_START_END_DATE_REACHED", 2);
00058 define("SURVEY_START_OFFLINE", 3);
00059 
00060 define("ANONYMIZE_OFF", 0);
00061 define("ANONYMIZE_ON", 1);
00062 
00063 define("QUESTIONTITLES_HIDDEN", 0);
00064 define("QUESTIONTITLES_VISIBLE", 1);
00065 
00066 class ilObjSurvey extends ilObject
00067 {
00076   var $survey_id;
00077 
00086   var $author;
00087 
00095   var $introduction;
00096 
00104   var $status;
00105 
00113   var $evaluation_access;
00114 
00122   var $start_date;
00123 
00131         var $startdate_enabled;
00132 
00140   var $end_date;
00141 
00149         var $enddate_enabled;
00150 
00158         var $questions;
00159 
00167         var $invitation;
00168 
00176         var $invitation_mode;
00177         
00184         var $anonymize;
00185 
00192         var $display_question_titles;
00193 
00200         function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
00201         {
00202                 global $ilUser;
00203                 $this->type = "svy";
00204                 $this->ilObject($a_id,$a_call_by_reference);
00205 /*
00206                 if ($a_id == 0)
00207                 {
00208                         $new_meta =& new ilMetaData();
00209                         $this->assignMetaData($new_meta);
00210                 }
00211 */
00212                 $this->survey_id = -1;
00213                 $this->introduction = "";
00214                 $this->author = $ilUser->fullname;
00215                 $this->status = STATUS_OFFLINE;
00216                 $this->evaluation_access = EVALUATION_ACCESS_OFF;
00217                 $this->startdate_enabled = 0;
00218                 $this->enddate_enabled = 0;
00219                 $this->questions = array();
00220                 $this->invitation = INVITATION_OFF;
00221                 $this->invitation_mode = MODE_PREDEFINED_USERS;
00222                 $this->anonymize = ANONYMIZE_OFF;
00223                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
00224         }
00225 
00229         function create($a_upload = false)
00230         {
00231                 parent::create();
00232                 $this->createMetaData();
00233 /*
00234                 if (!$a_upload)
00235                 {
00236                         $this->meta_data->setId($this->getId());
00237                         $this->meta_data->setType($this->getType());
00238                         $this->meta_data->setTitle($this->getTitle());
00239                         $this->meta_data->setDescription($this->getDescription());
00240                         $this->meta_data->setObject($this);
00241                         $this->meta_data->create();
00242                 }
00243 */
00244         }
00245 
00252         function update()
00253         {
00254                 $this->updateMetaData();
00255 
00256                 if (!parent::update())
00257                 {
00258                         return false;
00259                 }
00260 
00261                 // put here object specific stuff
00262 
00263                 return true;
00264         }
00265 
00266         function createReference() {
00267                 $result = parent::createReference();
00268                 $this->saveToDb();
00269                 return $result;
00270         }
00271 
00277         function read($a_force_db = false)
00278         {
00279                 parent::read($a_force_db);
00280                 $this->loadFromDb();
00281 //              $this->meta_data =& new ilMetaData($this->getType(), $this->getId());
00282         }
00283 
00291         function ilClone($a_parent_ref)
00292         {               
00293                 global $rbacadmin;
00294 
00295                 // always call parent ilClone function first!!
00296                 $new_ref_id = parent::ilClone($a_parent_ref);
00297                 
00298                 // get object instance of ilCloned object
00299                 //$newObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00300 
00301                 // create a local role folder & default roles
00302                 //$roles = $newObj->initDefaultRoles();
00303 
00304                 // ...finally assign role to creator of object
00305                 //$rbacadmin->assignUser($roles[0], $newObj->getOwner(), "n");          
00306 
00307                 // always destroy objects in ilClone method because ilClone() is recursive and creates instances for each object in subtree!
00308                 //unset($newObj);
00309 
00310                 // ... and finally always return new reference ID!!
00311                 return $new_ref_id;
00312         }
00313 
00320         function delete()
00321         {
00322                 $remove = parent::delete();
00323                 // always call parent delete function first!!
00324                 if (!$remove)
00325                 {
00326                         return false;
00327                 }
00328 
00329                 $this->deleteMetaData();
00330 
00331                 // Delete all survey questions, constraints and materials
00332                 foreach ($this->questions as $question_id)
00333                 {
00334                         $this->removeQuestion($question_id);
00335                 }
00336                 $this->deleteSurveyRecord();
00337                 
00338                 return true;
00339         }
00340         
00348         function deleteSurveyRecord()
00349         {
00350                 $query = sprintf("DELETE FROM survey_survey WHERE survey_id = %s",
00351                         $this->ilias->db->quote($this->getSurveyId())
00352                 );
00353                 $result = $this->ilias->db->query($query);
00354 
00355                 $query = sprintf("SELECT questionblock_fi FROM survey_questionblock_question WHERE survey_fi = %s",
00356                         $this->ilias->db->quote($this->getSurveyId())
00357                 );
00358                 $result = $this->ilias->db->query($query);
00359                 $questionblocks = array();
00360                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00361                 {
00362                         array_push($questionblocks, $row["questionblock_fi"]);
00363                 }
00364                 if (count($questionblocks))
00365                 {
00366                         $query = "DELETE FROM survey_questionblock WHERE questionblock_id IN (" . join($questionblocks, ",") . ")";
00367                         $result = $this->ilias->db->query($query);
00368                 }
00369                 $query = sprintf("DELETE FROM survey_questionblock_question WHERE survey_fi = %s",
00370                         $this->ilias->db->quote($this->getSurveyId())
00371                 );
00372                 $result = $this->ilias->db->query($query);
00373                 
00374                 $this->deleteAllUserData();
00375 
00376                 // delete export files
00377                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
00378                 $directory = $svy_data_dir."/svy_".$this->getId();
00379                 if (is_dir($directory))
00380                 {
00381                         $directory = escapeshellarg($directory);
00382                         exec("rm -rf $directory");
00383                 }
00384         }
00385         
00393         function deleteAllUserData()
00394         {
00395                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
00396                         $this->ilias->db->quote($this->getSurveyId())
00397                 );
00398                 $result = $this->ilias->db->query($query);
00399                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00400                 {
00401                         $this->disinviteUser($row["user_fi"]);
00402                 }
00403 
00404                 $query = sprintf("DELETE FROM survey_finished WHERE survey_fi = %s",
00405                         $this->ilias->db->quote($this->getSurveyId())
00406                 );
00407                 $result = $this->ilias->db->query($query);
00408 
00409                 $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s",
00410                         $this->ilias->db->quote($this->getSurveyId())
00411                 );
00412                 $result = $this->ilias->db->query($query);
00413 
00414                 $query = sprintf("DELETE FROM survey_anonymous WHERE survey_fi = %s",
00415                         $this->ilias->db->quote($this->getSurveyId())
00416                 );
00417                 $result = $this->ilias->db->query($query);
00418         }
00419 
00429         function initDefaultRoles()
00430         {
00431                 global $rbacadmin;
00432                 
00433                 // create a local role folder
00434                 //$rfoldObj = $this->createRoleFolder("Local roles","Role Folder of forum obj_no.".$this->getId());
00435 
00436                 // create moderator role and assign role to rolefolder...
00437                 //$roleObj = $rfoldObj->createRole("Moderator","Moderator of forum obj_no.".$this->getId());
00438                 //$roles[] = $roleObj->getId();
00439 
00440                 //unset($rfoldObj);
00441                 //unset($roleObj);
00442 
00443                 return $roles ? $roles : array();
00444         }
00445 
00459         function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
00460         {
00461                 global $tree;
00462                 
00463                 switch ($a_event)
00464                 {
00465                         case "link":
00466                                 
00467                                 //var_dump("<pre>",$a_params,"</pre>");
00468                                 //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
00469                                 //exit;
00470                                 break;
00471                         
00472                         case "cut":
00473                                 
00474                                 //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
00475                                 //exit;
00476                                 break;
00477                                 
00478                         case "copy":
00479                         
00480                                 //var_dump("<pre>",$a_params,"</pre>");
00481                                 //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
00482                                 //exit;
00483                                 break;
00484 
00485                         case "paste":
00486                                 
00487                                 //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
00488                                 //exit;
00489                                 break;
00490                         
00491                         case "new":
00492                                 
00493                                 //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
00494                                 //exit;
00495                                 break;
00496                 }
00497                 
00498                 // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
00499                 if ($a_node_id==$_GET["ref_id"])
00500                 {       
00501                         $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
00502                         $parent_type = $parent_obj->getType();
00503                         if($parent_type == $this->getType())
00504                         {
00505                                 $a_node_id = (int) $tree->getParentId($a_node_id);
00506                         }
00507                 }
00508                 
00509                 parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
00510         }
00511 
00520         function isComplete()
00521         {
00522                 if (($this->getTitle()) and ($this->author) and (count($this->questions)))
00523                 {
00524                         return true;
00525                 } 
00526                         else 
00527                 {
00528                         return false;
00529                 }
00530         }
00531 
00540         function _isComplete($obj_id)
00541         {
00542                 $survey = new ilObjSurvey($obj_id, false);
00543                 $survey->loadFromDb();
00544                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00545                 {
00546                         return true;
00547                 } 
00548                         else 
00549                 {
00550                         return false;
00551                 }
00552         }
00553 
00562         function &_getGlobalSurveyData($obj_id)
00563         {
00564                 $survey = new ilObjSurvey($obj_id, false);
00565                 $survey->loadFromDb();
00566                 $result = array();
00567                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00568                 {
00569                         $result["complete"] = true;
00570                 } 
00571                         else 
00572                 {
00573                         $result["complete"] = false;
00574                 }
00575                 $result["evaluation_access"] = $survey->getEvaluationAccess();
00576                 return $result;
00577         }
00578 
00586         function saveCompletionStatus() {
00587                 $complete = 0;
00588                 if ($this->isComplete()) {
00589                         $complete = 1;
00590                 }
00591     if ($this->survey_id > 0) {
00592       $query = sprintf("UPDATE survey_survey SET complete = %s WHERE survey_id = %s",
00593                                 $this->ilias->db->quote("$complete"),
00594         $this->ilias->db->quote($this->survey_id) 
00595       );
00596       $result = $this->ilias->db->query($query);
00597                 }
00598         }
00599 
00609         function duplicateQuestionForSurvey($question_id)
00610         {
00611                 global $ilUser;
00612                 
00613                 $questiontype = $this->getQuestionType($question_id);
00614                 $question_gui = $this->getQuestionGUI($questiontype, $question_id);
00615                 $duplicate_id = $question_gui->object->duplicate(true);
00616                 return $duplicate_id;
00617         }
00618 
00626         function insertQuestion($question_id) {
00627     // get maximum sequence index in test
00628     $query = sprintf("SELECT survey_question_id FROM survey_survey_question WHERE survey_fi = %s",
00629       $this->ilias->db->quote($this->getSurveyId())
00630     );
00631     $result = $this->ilias->db->query($query);
00632     $sequence = $result->numRows();
00633                 $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
00634     $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00635       $this->ilias->db->quote($this->getSurveyId()),
00636       $this->ilias->db->quote($duplicate_id),
00637       $this->ilias->db->quote($sequence)
00638     );
00639     $result = $this->ilias->db->query($query);
00640     if ($result != DB_OK) {
00641       // Error
00642     }
00643                 $this->loadQuestionsFromDb();
00644         }
00645 
00646 
00647         
00655         function insertQuestionblock($questionblock_id) {
00656                 $query = sprintf("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 =%s ORDER BY survey_survey_question.sequence",
00657                         $this->ilias->db->quote($questionblock_id)
00658                 );
00659                 $result = $this->ilias->db->query($query);
00660                 $questions = array();
00661                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00662                 {
00663                         array_push($questions, $row["question_id"]);
00664                         $title = $row["title"];
00665                 }
00666                 $this->createQuestionblock($title, $questions);
00667         }
00668         
00676   function saveToDb()
00677   {
00678                 $complete = 0;
00679                 if ($this->isComplete()) {
00680                         $complete = 1;
00681                 }
00682                 $startdate = $this->getStartDate();
00683                 if (!$startdate or !$this->startdate_enabled)
00684                 {
00685                         $startdate = "NULL";
00686                 }
00687                 else
00688                 {
00689                         $startdate = $this->ilias->db->quote($startdate);
00690                 }
00691                 $enddate = $this->getEndDate();
00692                 if (!$enddate or !$this->enddate_enabled)
00693                 {
00694                         $enddate = "NULL";
00695                 }
00696                 else
00697                 {
00698                         $enddate = $this->ilias->db->quote($enddate);
00699                 }
00700     if ($this->survey_id == -1) {
00701       // Write new dataset
00702       $now = getdate();
00703       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00704       $query = sprintf("INSERT INTO survey_survey (survey_id, obj_fi, author, introduction, 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, NULL)",
00705         $this->ilias->db->quote($this->getId()),
00706         $this->ilias->db->quote($this->author . ""),
00707         $this->ilias->db->quote($this->introduction . ""),
00708         $this->ilias->db->quote($this->status . ""),
00709         $startdate,
00710                                 $enddate,
00711         $this->ilias->db->quote($this->evaluation_access . ""),
00712                                 $this->ilias->db->quote($this->invitation . ""),
00713                                 $this->ilias->db->quote($this->invitation_mode . ""),
00714                                 $this->ilias->db->quote($complete . ""),
00715                                 $this->ilias->db->quote($this->getAnonymize() . ""),
00716                                 $this->ilias->db->quote($this->getShowQuestionTitles() . ""),
00717         $this->ilias->db->quote($created)
00718       );
00719       $result = $this->ilias->db->query($query);
00720       if ($result == DB_OK) {
00721         $this->survey_id = $this->ilias->db->getLastInsertId();
00722       }
00723     } else {
00724       // update existing dataset
00725       $query = sprintf("UPDATE survey_survey SET author = %s, introduction = %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",
00726         $this->ilias->db->quote($this->author . ""),
00727         $this->ilias->db->quote($this->introduction . ""),
00728         $this->ilias->db->quote($this->status . ""),
00729         $startdate,
00730                                 $enddate,
00731         $this->ilias->db->quote($this->evaluation_access . ""),
00732                                 $this->ilias->db->quote($this->invitation . ""),
00733                                 $this->ilias->db->quote($this->invitation_mode . ""),
00734                                 $this->ilias->db->quote($complete . ""),
00735                                 $this->ilias->db->quote($this->getAnonymize() . ""),
00736                                 $this->ilias->db->quote($this->getShowQuestionTitles() . ""),
00737         $this->ilias->db->quote($this->survey_id)
00738       );
00739       $result = $this->ilias->db->query($query);
00740     }
00741     if ($result == DB_OK) {
00742                         // save questions to db
00743                         $this->saveQuestionsToDb();
00744     }
00745   }
00746 
00755         function saveQuestionsToDb() {
00756                 // save old questions state
00757                 $old_questions = array();
00758                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
00759                         $this->ilias->db->quote($this->getSurveyId())
00760                 );
00761                 $result = $this->ilias->db->query($query);
00762                 if ($result->numRows())
00763                 {
00764                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00765                         {
00766                                 $old_questions[$row["question_fi"]] = $row;
00767                         }
00768                 }
00769                 
00770                 // delete existing question relations
00771     $query = sprintf("DELETE FROM survey_survey_question WHERE survey_fi = %s",
00772                         $this->ilias->db->quote($this->getSurveyId())
00773                 );
00774                 $result = $this->ilias->db->query($query);
00775                 // create new question relations
00776                 foreach ($this->questions as $key => $value) {
00777                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, heading, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00778                                 $this->ilias->db->quote($this->getSurveyId() . ""),
00779                                 $this->ilias->db->quote($value . ""),
00780                                 $this->ilias->db->quote($old_questions[$value]["heading"]),
00781                                 $this->ilias->db->quote($key . "")
00782                         );
00783                         $result = $this->ilias->db->query($query);
00784                 }
00785         }
00786 
00796         function getAnonymousId($id)
00797         {
00798                 $query = sprintf("SELECT anonymous_id FROM survey_answer WHERE anonymous_id = %s",
00799                         $this->ilias->db->quote($id)
00800                 );
00801                 $result = $this->ilias->db->query($query);
00802                 if ($result->numRows())
00803                 {
00804                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00805                         return $row["anonymous_id"];
00806                 }
00807                 else
00808                 {
00809                         return "";
00810                 }
00811         }
00812 
00821         function getQuestionGUI($questiontype, $question_id)
00822         {
00823                 switch ($questiontype)
00824                 {
00825                         case "qt_nominal":
00826                                 $question = new SurveyNominalQuestionGUI();
00827                                 break;
00828                         case "qt_ordinal":
00829                                 $question = new SurveyOrdinalQuestionGUI();
00830                                 break;
00831                         case "qt_metric":
00832                                 $question = new SurveyMetricQuestionGUI();
00833                                 break;
00834                         case "qt_text":
00835                                 $question = new SurveyTextQuestionGUI();
00836                                 break;
00837                 }
00838                 $question->object->loadFromDb($question_id);
00839                 return $question;
00840         }
00841         
00851   function getQuestionType($question_id) {
00852     if ($question_id < 1)
00853       return -1;
00854     $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",
00855       $this->ilias->db->quote($question_id)
00856     );
00857     $result = $this->ilias->db->query($query);
00858     if ($result->numRows() == 1) {
00859       $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00860       return $data->type_tag;
00861     } else {
00862       return "";
00863     }
00864   }
00865         
00874         function getSurveyId()
00875         {
00876                 return $this->survey_id;
00877         }
00878         
00882         function setAnonymize($a_anonymize)
00883         {
00884                 $this->anonymize = $a_anonymize;
00885         }
00886 
00892         function getAnonymize()
00893         {
00894                 return $this->anonymize;
00895         }
00896 
00902         function isAccessibleWithoutCode()
00903         {
00904                 return false;
00905                 // patch for HSU-HH, activate when installed
00906                 // simply remove the line above and every survey which
00907                 // is accessible by anonymous could be done without survey code
00908                 if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
00909                 {
00910                         return true;
00911                 }
00912                 else
00913                 {
00914                         return false;
00915                 }
00916         }
00917 
00925   function loadFromDb()
00926   {
00927     $query = sprintf("SELECT * FROM survey_survey WHERE obj_fi = %s",
00928       $this->ilias->db->quote($this->getId())
00929     );
00930     $result = $this->ilias->db->query($query);
00931     if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00932       if ($result->numRows() == 1) {
00933         $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00934                                 $this->survey_id = $data->survey_id;
00935         $this->author = $data->author;
00936         $this->introduction = $data->introduction;
00937         $this->status = $data->status;
00938                                 $this->invitation = $data->invitation;
00939                                 $this->invitation_mode = $data->invitation_mode;
00940                                 $this->display_question_titles = $data->show_question_titles;
00941         $this->start_date = $data->startdate;
00942                                 if (!$data->startdate)
00943                                 {
00944                                         $this->startdate_enabled = 0;
00945                                 }
00946                                 else
00947                                 {
00948                                         $this->startdate_enabled = 1;
00949                                 }
00950         $this->end_date = $data->enddate;
00951                                 if (!$data->enddate)
00952                                 {
00953                                         $this->enddate_enabled = 0;
00954                                 }
00955                                 else
00956                                 {
00957                                         $this->enddate_enabled = 1;
00958                                 }
00959                                 if (!$data->anonymize)
00960                                 {
00961                                         $this->setAnonymize(ANONYMIZE_OFF);
00962                                 }
00963                                 else
00964                                 {
00965                                         $this->setAnonymize(ANONYMIZE_ON);
00966                                 }
00967         $this->evaluation_access = $data->evaluation_access;
00968                                 $this->loadQuestionsFromDb();
00969       }
00970     }
00971         }
00972 
00981         function loadQuestionsFromDb() {
00982                 $this->questions = array();
00983                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
00984                         $this->ilias->db->quote($this->survey_id)
00985                 );
00986                 $result = $this->ilias->db->query($query);
00987                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00988                         $this->questions[$data->sequence] = $data->question_fi;
00989                 }
00990         }
00991 
01001         function setStartDateEnabled($enabled = false)
01002         {
01003                 if ($enabled)
01004                 {
01005                         $this->startdate_enabled = 1;
01006                 }
01007                 else
01008                 {
01009                         $this->startdate_enabled = 0;
01010                 }
01011         }
01012         
01022         function getStartDateEnabled()
01023         {
01024                 return $this->startdate_enabled;
01025         }
01026 
01036         function setEndDateEnabled($enabled = false)
01037         {
01038                 if ($enabled)
01039                 {
01040                         $this->enddate_enabled = 1;
01041                 }
01042                 else
01043                 {
01044                         $this->enddate_enabled = 0;
01045                 }
01046         }
01047         
01057         function getEndDateEnabled()
01058         {
01059                 return $this->enddate_enabled;
01060         }
01061 
01067 /*
01068         function assignMetaData(&$a_meta_data)
01069         {
01070                 $this->meta_data =& $a_meta_data;
01071         }
01072 */
01073 
01079 /*
01080         function &getMetaData()
01081         {
01082                 return $this->meta_data;
01083         }
01084 */
01085 
01095   function setAuthor($author = "") {
01096     if (!$author) {
01097       $author = $this->ilias->account->fullname;
01098     }
01099     $this->author = $author;
01100   }
01101 
01111   function getShowQuestionTitles() {
01112                 return $this->display_question_titles;
01113   }
01114 
01123   function showQuestionTitles() {
01124                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
01125   }
01126 
01135   function hideQuestionTitles() {
01136                 $this->display_question_titles = QUESTIONTITLES_HIDDEN;
01137   }
01138         
01148   function setInvitation($invitation = 0) {
01149     $this->invitation = $invitation;
01150                 // remove the survey from the personal desktops
01151                 $query = sprintf("DELETE FROM desktop_item WHERE type = %s AND item_id = %s",
01152                         $this->ilias->db->quote("svy"),
01153                         $this->ilias->db->quote($this->getRefId())
01154                 );
01155                 $result = $this->ilias->db->query($query);
01156                 if ($invitation == INVITATION_OFF)
01157                 {
01158                         // already removed prior
01159                 }
01160                 else if ($invitation == INVITATION_ON)
01161                 {
01162                         if ($this->getInvitationMode() == MODE_UNLIMITED)
01163                         {
01164                                 $query = "SELECT usr_id FROM usr_data";
01165                                 $result = $this->ilias->db->query($query);
01166                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01167                                 {
01168                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01169                                                 $this->ilias->db->quote($row["usr_id"]),
01170                                                 $this->ilias->db->quote($this->getRefId()),
01171                                                 $this->ilias->db->quote("svy")
01172                                         );
01173                                         $insertresult = $this->ilias->db->query($query);
01174                                 }
01175                         }
01176                         else
01177                         {
01178                                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
01179                                         $this->ilias->db->quote($this->getSurveyId())
01180                                 );
01181                                 $result = $this->ilias->db->query($query);
01182                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01183                                 {
01184                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01185                                                 $this->ilias->db->quote($row["user_fi"]),
01186                                                 $this->ilias->db->quote($this->getRefId()),
01187                                                 $this->ilias->db->quote("svy")
01188                                         );
01189                                         $insertresult = $this->ilias->db->query($query);
01190                                 }
01191                                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
01192                                         $this->ilias->db->quote($this->getSurveyId())
01193                                 );
01194                                 $result = $this->ilias->db->query($query);
01195                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01196                                 {
01197                                         $group = new ilObjGroup($row["group_fi"]);
01198                                         $members = $group->getGroupMemberIds();
01199                                         foreach ($members as $user_id)
01200                                         {
01201                                                 $user = new ilObjUser($user_id);
01202                                                 $user->addDesktopItem($this->getRefId(), "svy");
01203                                         }
01204                                 }
01205                         }
01206                 }
01207   }
01208 
01218   function setInvitationMode($invitation_mode = 0) {
01219     $this->invitation_mode = $invitation_mode;
01220                 if ($invitation_mode == MODE_UNLIMITED)
01221                 {
01222                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01223                                 $this->ilias->db->quote($this->getSurveyId())
01224                         );
01225                         $result = $this->ilias->db->query($query);
01226                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01227                                 $this->ilias->db->quote($this->getSurveyId())
01228                         );
01229                         $result = $this->ilias->db->query($query);
01230                 }
01231                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01232                 $this->setInvitation($this->getInvitation());
01233   }
01234         
01245         function setInvitationAndMode($invitation = 0, $invitation_mode = 0)
01246         {
01247     $this->invitation_mode = $invitation_mode;
01248                 if ($invitation_mode == MODE_UNLIMITED)
01249                 {
01250                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01251                                 $this->ilias->db->quote($this->getSurveyId())
01252                         );
01253                         $result = $this->ilias->db->query($query);
01254                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01255                                 $this->ilias->db->quote($this->getSurveyId())
01256                         );
01257                         $result = $this->ilias->db->query($query);
01258                 }
01259                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01260                 $this->setInvitation($invitation);
01261         }
01262 
01272   function setIntroduction($introduction = "") {
01273     $this->introduction = $introduction;
01274   }
01275 
01285   function getAuthor() {
01286     return $this->author;
01287   }
01288 
01298   function getInvitation() {
01299     return $this->invitation;
01300   }
01301 
01311   function getInvitationMode() {
01312     return $this->invitation_mode;
01313   }
01314 
01324   function getStatus() {
01325     return $this->status;
01326   }
01327 
01337   function isOnline() 
01338         {
01339     if ($this->status == STATUS_ONLINE)
01340                 {
01341                         return true;
01342                 }
01343                 else
01344                 {
01345                         return false;
01346                 }
01347   }
01348 
01358   function isOffline() 
01359         {
01360     if ($this->status == STATUS_OFFLINE)
01361                 {
01362                         return true;
01363                 }
01364                 else
01365                 {
01366                         return false;
01367                 }
01368   }
01369 
01380   function setStatus($status = STATUS_OFFLINE) {
01381                 $result = "";
01382                 if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
01383                 {
01384         $this->status = STATUS_OFFLINE;
01385                         $result = $this->lng->txt("cannot_switch_to_online_no_questions");
01386                 }
01387                 else
01388                 {
01389         $this->status = $status;
01390                 }
01391                 return $result;
01392   }
01393 
01403   function getStartDate() {
01404     return $this->start_date;
01405   }
01406 
01415         function canStartSurvey()
01416         {
01417                 $result = 0;
01418                 if ($this->getStartDateEnabled())
01419                 {
01420                         $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
01421                         $now = mktime();
01422                         if ($now < $epoch_time) {
01423                                 $result = SURVEY_START_START_DATE_NOT_REACHED;
01424                         }
01425                 }
01426                 if ($this->getEndDateEnabled())
01427                 {
01428                         $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
01429                         $now = mktime();
01430                         if ($now > $epoch_time) {
01431                                 $result = SURVEY_START_END_DATE_REACHED;
01432                         }
01433                 }
01434                 if ($this->getStatus() == STATUS_OFFLINE)
01435                 {
01436                         $result = SURVEY_START_OFFLINE;
01437                 }
01438                 return $result;
01439         }
01440 
01441 
01451   function setStartDate($start_date = "") {
01452     $this->start_date = $start_date;
01453   }
01454 
01464   function getStartMonth() {
01465                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01466                 {
01467                         return $matches[2];
01468                 }
01469                 else
01470                 {
01471                         return "";
01472                 }
01473   }
01474 
01484   function getStartDay() {
01485                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01486                 {
01487                         return $matches[3];
01488                 }
01489                 else
01490                 {
01491                         return "";
01492                 }
01493   }
01494 
01504   function getStartYear() {
01505                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01506                 {
01507                         return $matches[1];
01508                 }
01509                 else
01510                 {
01511                         return "";
01512                 }
01513   }
01514 
01524   function getEndDate() {
01525     return $this->end_date;
01526   }
01527 
01537   function setEndDate($end_date = "") {
01538     $this->end_date = $end_date;
01539   }
01540 
01550   function getEndMonth() {
01551                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01552                 {
01553                         return $matches[2];
01554                 }
01555                 else
01556                 {
01557                         return "";
01558                 }
01559   }
01560 
01570   function getEndDay() {
01571                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01572                 {
01573                         return $matches[3];
01574                 }
01575                 else
01576                 {
01577                         return "";
01578                 }
01579   }
01580 
01590   function getEndYear() {
01591                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01592                 {
01593                         return $matches[1];
01594                 }
01595                 else
01596                 {
01597                         return "";
01598                 }
01599   }
01600 
01610   function getEvaluationAccess() {
01611     return $this->evaluation_access;
01612   }
01613 
01623   function setEvaluationAccess($evaluation_access = EVALUATION_ACCESS_OFF) {
01624     $this->evaluation_access = $evaluation_access;
01625   }
01626 
01636   function getIntroduction() {
01637     return $this->introduction;
01638   }
01639 
01648         function &getExistingQuestions() {
01649                 $existing_questions = array();
01650                 $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",
01651                         $this->ilias->db->quote($this->getSurveyId())
01652                 );
01653                 $result = $this->ilias->db->query($query);
01654                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
01655                         array_push($existing_questions, $data->original_id);
01656                 }
01657                 return $existing_questions;
01658         }
01659 
01668         function &getQuestionpoolTitles() {
01669                 global $rbacsystem;
01670                 
01671                 $qpl_titles = array();
01672                 // get all available questionpools and remove the trashed questionspools
01673                 $query = "SELECT object_data.*, object_data.obj_id, object_reference.ref_id FROM object_data, object_reference WHERE object_data.obj_id = object_reference.obj_id AND object_data.type = 'spl'";
01674                 $result = $this->ilias->db->query($query);
01675                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01676                 {               
01677                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
01678                         {
01679                                 $qpl_titles["$row->obj_id"] = $row->title;
01680                         }
01681                 }
01682                 return $qpl_titles;
01683         }
01684         
01693         function moveUpQuestion($question_id)
01694         {
01695                 $move_questions = array($question_id);
01696                 $pages =& $this->getSurveyPages();
01697                 $pageindex = -1;
01698                 foreach ($pages as $idx => $page)
01699                 {
01700                         if ($page[0]["question_id"] == $question_id)
01701                         {
01702                                 $pageindex = $idx;
01703                         }
01704                 }
01705                 if ($pageindex > 0)
01706                 {
01707                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01708                 }
01709                 else
01710                 {
01711                         // move up a question in a questionblock
01712                         $questions = $this->getSurveyQuestions();
01713                         $questions = array_keys($questions);
01714                         $index = array_search($question_id, $questions);
01715                         if (($index !== FALSE) && ($index > 0))
01716                         {
01717                                 $this->moveQuestions($move_questions, $questions[$index-1], 0);
01718                         }
01719                 }
01720         }
01721         
01730         function moveDownQuestion($question_id)
01731         {
01732                 $move_questions = array($question_id);
01733                 $pages =& $this->getSurveyPages();
01734                 $pageindex = -1;
01735                 foreach ($pages as $idx => $page)
01736                 {
01737                         if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
01738                         {
01739                                 $pageindex = $idx;
01740                         }
01741                 }
01742                 if (($pageindex < count($pages)-1) && ($pageindex >= 0))
01743                 {
01744                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01745                 }
01746                 else
01747                 {
01748                         // move down a question in a questionblock
01749                         $questions = $this->getSurveyQuestions();
01750                         $questions = array_keys($questions);
01751                         $index = array_search($question_id, $questions);
01752                         if (($index !== FALSE) && ($index < count($questions)-1))
01753                         {
01754                                 $this->moveQuestions($move_questions, $questions[$index+1], 1);
01755                         }
01756                 }
01757         }
01758         
01767         function moveUpQuestionblock($questionblock_id)
01768         {
01769                 $pages =& $this->getSurveyPages();
01770                 $move_questions = array();
01771                 $pageindex = -1;
01772                 foreach ($pages as $idx => $page)
01773                 {
01774                         if ($page[0]["questionblock_id"] == $questionblock_id)
01775                         {
01776                                 foreach ($page as $pageidx => $question)
01777                                 {
01778                                         array_push($move_questions, $question["question_id"]);
01779                                 }
01780                                 $pageindex = $idx;
01781                         }
01782                 }
01783                 if ($pageindex > 0)
01784                 {
01785                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01786                 }
01787         }
01788         
01797         function moveDownQuestionblock($questionblock_id)
01798         {
01799                 $pages =& $this->getSurveyPages();
01800                 $move_questions = array();
01801                 $pageindex = -1;
01802                 foreach ($pages as $idx => $page)
01803                 {
01804                         if ($page[0]["questionblock_id"] == $questionblock_id)
01805                         {
01806                                 foreach ($page as $pageidx => $question)
01807                                 {
01808                                         array_push($move_questions, $question["question_id"]);
01809                                 }
01810                                 $pageindex = $idx;
01811                         }
01812                 }
01813                 if ($pageindex < count($pages)-1)
01814                 {
01815                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01816                 }
01817         }
01818         
01829         function moveQuestions($move_questions, $target_index, $insert_mode)
01830         {
01831                 $array_pos = array_search($target_index, $this->questions);
01832                 if ($insert_mode == 0)
01833                 {
01834                         $part1 = array_slice($this->questions, 0, $array_pos);
01835                         $part2 = array_slice($this->questions, $array_pos);
01836                 }
01837                 else if ($insert_mode == 1)
01838                 {
01839                         $part1 = array_slice($this->questions, 0, $array_pos + 1);
01840                         $part2 = array_slice($this->questions, $array_pos + 1);
01841                 }
01842                 foreach ($move_questions as $question_id)
01843                 {
01844                         if (!(array_search($question_id, $part1) === FALSE))
01845                         {
01846                                 unset($part1[array_search($question_id, $part1)]);
01847                         }
01848                         if (!(array_search($question_id, $part2) === FALSE))
01849                         {
01850                                 unset($part2[array_search($question_id, $part2)]);
01851                         }
01852                 }
01853                 $part1 = array_values($part1);
01854                 $part2 = array_values($part2);
01855                 $this->questions = array_values(array_merge($part1, $move_questions, $part2));
01856                 foreach ($move_questions as $question_id)
01857                 {
01858                         $constraints = $this->getConstraints($question_id);
01859                         foreach ($constraints as $idx => $constraint)
01860                         {
01861                                 foreach ($part2 as $next_question_id)
01862                                 {
01863                                         if ($constraint["question"] == $next_question_id)
01864                                         {
01865                                                 // constraint concerning a question that follows -> delete constraint
01866                                                 $this->deleteConstraint($constraint["id"], $question_id);
01867                                         }
01868                                 }
01869                         }
01870                 }
01871                 $this->saveQuestionsToDb();
01872         }
01873         
01882         function removeQuestion($question_id)
01883         {
01884                 $question = new SurveyQuestion();
01885                 $question->delete($question_id);
01886                 $this->removeConstraintsConcerningQuestion($question_id);
01887         }
01888         
01897         function removeConstraintsConcerningQuestion($question_id)
01898         {
01899                 $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01900                         $this->ilias->db->quote($question_id . ""),
01901                         $this->ilias->db->quote($this->getSurveyId() . "")
01902                 );
01903                 $result = $this->ilias->db->query($query);
01904                 if ($result->numRows() > 0)
01905                 {
01906                         $remove_constraints = array();
01907                         while ($row = $result->fetchRow(DB_FETCHMODE_HASHREF))
01908                         {
01909                                 array_push($remove_constraints, $row["constraint_fi"]);
01910                         }
01911                         $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01912                                 $this->ilias->db->quote($question_id . ""),
01913                                 $this->ilias->db->quote($this->getSurveyId() . "")
01914                         );
01915                         $result = $this->ilias->db->query($query);
01916                         foreach ($remove_constraints as $key => $constraint_id)
01917                         {
01918                                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
01919                                         $this->ilias->db->quote($constraint_id . "")
01920                                 );
01921                                 $result = $this->ilias->db->query($query);
01922                         }
01923                 }
01924         }
01925                 
01935         function removeQuestions($remove_questions, $remove_questionblocks)
01936         {
01937                 $questions =& $this->getSurveyQuestions();
01938                 foreach ($questions as $question_id => $data)
01939                 {
01940                         if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
01941                         {
01942                                 unset($this->questions[array_search($question_id, $this->questions)]);
01943                                 $this->removeQuestion($question_id);
01944                         }
01945                 }
01946                 foreach ($remove_questionblocks as $questionblock_id)
01947                 {
01948                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01949                                 $this->ilias->db->quote($questionblock_id)
01950                         );
01951                         $result = $this->ilias->db->query($query);
01952                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01953                                 $this->ilias->db->quote($questionblock_id),
01954                                 $this->ilias->db->quote($this->getSurveyId())
01955                         );
01956                         $result = $this->ilias->db->query($query);
01957                 }
01958                 $this->questions = array_values($this->questions);
01959                 $this->saveQuestionsToDb();
01960         }
01961                 
01970         function unfoldQuestionblocks($questionblocks)
01971         {
01972                 foreach ($questionblocks as $index)
01973                 {
01974                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01975                                 $this->ilias->db->quote($index)
01976                         );
01977                         $result = $this->ilias->db->query($query);
01978                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01979                                 $this->ilias->db->quote($index),
01980                                 $this->ilias->db->quote($this->getSurveyId())
01981                         );
01982                         $result = $this->ilias->db->query($query);
01983                 }
01984         }
01985         
01994         function &getQuestionblockTitles()
01995         {
01996                 $titles = array();
01997                 $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",
01998                         $this->ilias->db->quote($this->getId())
01999                 );
02000                 $result = $this->ilias->db->query($query);
02001                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02002                 {
02003                         $titles[$row->questionblock_id] = $row->title;
02004                 }
02005                 return $titles;
02006         }
02007         
02016         function &getQuestionblockQuestions($questionblock_id)
02017         {
02018                 $titles = array();
02019                 $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",
02020                         $this->ilias->db->quote($questionblock_id)
02021                 );
02022                 $result = $this->ilias->db->query($query);
02023                 $survey_id = "";
02024                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02025                 {
02026                         $titles[$row["question_fi"]] = $row["title"];
02027                         $survey_id = $row["survey_fi"];
02028                 }
02029                 $query = sprintf("SELECT question_fi, sequence FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
02030                         $this->ilias->db->quote($survey_id . "")
02031                 );
02032                 $result = $this->ilias->db->query($query);
02033                 $resultarray = array();
02034                 $counter = 1;
02035                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02036                 {
02037                         if (array_key_exists($row["question_fi"], $titles))
02038                         {
02039                                 $resultarray[$counter++] = $titles[$row["question_fi"]];
02040                         }
02041                 }
02042                 return $resultarray;
02043         }
02044         
02053         function &getQuestionblockQuestionIds($questionblock_id)
02054         {
02055                 $ids = array();
02056                 $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",
02057                         $this->ilias->db->quote($this->getId()),
02058                         $this->ilias->db->quote($questionblock_id)
02059                 );
02060                 $result = $this->ilias->db->query($query);
02061                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02062                 {
02063                         array_push($ids, $row->question_id);
02064                 }
02065                 return $ids;
02066         }
02067         
02077         function getQuestionblock($questionblock_id)
02078         {
02079                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02080                         $this->ilias->db->quote($questionblock_id)
02081                 );
02082                 $result = $this->ilias->db->query($query);
02083                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02084                 return $row;
02085         }
02086         
02096         function _getQuestionblock($questionblock_id)
02097         {
02098                 global $ilDB;
02099                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02100                         $ilDB->quote($questionblock_id)
02101                 );
02102                 $result = $ilDB->query($query);
02103                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02104                 return $row;
02105         }
02106 
02117         function _addQuestionblock($title = "", $owner = 0)
02118         {
02119                 global $ilDB;
02120                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02121                         $ilDB->quote($title . ""),
02122                         $ilDB->quote($owner . "")
02123                 );
02124                 $result = $ilDB->query($query);
02125                 return $ilDB->getLastInsertId();
02126         }
02127         
02137         function createQuestionblock($title, $questions)
02138         {
02139                 // if the selected questions are not in a continous selection, move all questions of the
02140                 // questionblock at the position of the first selected question
02141                 $this->moveQuestions($questions, $questions[0], 0);
02142                 
02143                 // now save the question block
02144                 global $ilUser;
02145                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02146                         $this->ilias->db->quote($title),
02147                         $this->ilias->db->quote($ilUser->id)
02148                 );
02149                 $result = $this->ilias->db->query($query);
02150                 if ($result == DB_OK) {
02151                         $questionblock_id = $this->ilias->db->getLastInsertId();
02152                         foreach ($questions as $index)
02153                         {
02154                                 $query = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
02155                                         $this->ilias->db->quote($this->getSurveyId()),
02156                                         $this->ilias->db->quote($questionblock_id),
02157                                         $this->ilias->db->quote($index)
02158                                 );
02159                                 $result = $this->ilias->db->query($query);
02160                                 $this->deleteConstraints($index);
02161                         }
02162                 }
02163         }
02164         
02174         function modifyQuestionblock($questionblock_id, $title)
02175         {
02176                 $query = sprintf("UPDATE survey_questionblock SET title = %s WHERE questionblock_id = %s",
02177                         $this->ilias->db->quote($title),
02178                         $this->ilias->db->quote($questionblock_id)
02179                 );
02180                 $result = $this->ilias->db->query($query);
02181         }
02182         
02191         function deleteConstraints($question_id)
02192         {
02193                 $query = sprintf("SELECT * FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02194                         $this->ilias->db->quote($question_id),
02195                         $this->ilias->db->quote($this->getSurveyId())
02196                 );
02197                 $result = $this->ilias->db->query($query);
02198                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02199                 {
02200                         $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02201                                 $this->ilias->db->quote($row->constraint_fi)
02202                         );
02203                         $delresult = $this->ilias->db->query($query);
02204                 }
02205                 $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02206                         $this->ilias->db->quote($question_id),
02207                         $this->ilias->db->quote($this->getSurveyId())
02208                 );
02209                 $delresult = $this->ilias->db->query($query);
02210         }
02211 
02221         function deleteConstraint($constraint_id, $question_id)
02222         {
02223                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02224                         $this->ilias->db->quote($constraint_id)
02225                 );
02226                 $delresult = $this->ilias->db->query($query);
02227                 $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s AND question_fi = %s AND survey_fi = %s",
02228                         $this->ilias->db->quote($constraint_id),
02229                         $this->ilias->db->quote($question_id),
02230                         $this->ilias->db->quote($this->getSurveyId())
02231                 );
02232                 $delresult = $this->ilias->db->query($query);
02233         }
02234 
02242         function &getSurveyQuestions($with_answers = false)
02243         {
02244                 $obligatory_states =& $this->getObligatoryStates();
02245                 // get questionblocks
02246                 $all_questions = array();
02247                 $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",
02248                         $this->ilias->db->quote($this->getSurveyId())
02249                 );
02250                 $result = $this->ilias->db->query($query);
02251                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02252                 {
02253                         $all_questions[$row["question_id"]] = $row;
02254                         if (array_key_exists($row["question_id"], $obligatory_states))
02255                         {
02256                                 $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
02257                         }
02258                 }
02259                 // get all questionblocks
02260                 $questionblocks = array();
02261                 $in = join(array_keys($all_questions), ",");
02262                 if ($in)
02263                 {
02264                         $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)",
02265                                 $this->ilias->db->quote($this->getSurveyId())
02266                         );
02267                         $result = $this->ilias->db->query($query);
02268                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02269                         {
02270                                 $questionblocks[$row->question_fi] = $row;
02271                         }                       
02272                 }
02273                 
02274                 foreach ($all_questions as $question_id => $row)
02275                 {
02276                         $constraints = $this->getConstraints($question_id);
02277                         if (isset($questionblocks[$question_id]))
02278                         {
02279                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02280                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02281                                 $all_questions[$question_id]["constraints"] = $constraints;
02282                         }
02283                         else
02284                         {
02285                                 $all_questions[$question_id]["questionblock_title"] = "";
02286                                 $all_questions[$question_id]["questionblock_id"] = "";
02287                                 $all_questions[$question_id]["constraints"] = $constraints;
02288                         }
02289                         if ($with_answers)
02290                         {
02291                                 $answers = array();
02292                                 $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",
02293                                         $this->ilias->db->quote($question_id . "")
02294                                 );
02295                                 $result = $this->ilias->db->query($query);
02296                                 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
02297                                         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
02298                                                 array_push($answers, $data->title);
02299                                         }
02300                                 }
02301                                 $all_questions[$question_id]["answers"] = $answers;                             
02302                         }
02303                 }
02304                 return $all_questions;
02305         }
02306         
02315         function &getQuestiontypes()
02316         {
02317                 $query = "SELECT type_tag FROM survey_questiontype";
02318                 $result = $this->ilias->db->query($query);
02319                 $result_array = array();
02320                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02321                 {
02322                         array_push($result_array, $row->type_tag);
02323                 }
02324                 return $result_array;
02325         }
02326 
02335         function setObligatoryStates($obligatory_questions)
02336         {
02337                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
02338                         $this->ilias->db->quote($this->getSurveyId() . "")
02339                 );
02340                 $result = $this->ilias->db->query($query);
02341                 if ($result->numRows())
02342                 {
02343                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02344                         {
02345                                 if (!array_key_exists($row["question_fi"], $obligatory_questions))
02346                                 {
02347                                         $obligatory_questions[$row["question_fi"]] = 0;
02348                                 }
02349                         }
02350                 }
02351 
02352           // set the obligatory states in the database
02353                 $query = sprintf("DELETE FROM survey_question_obligatory WHERE survey_fi = %s",
02354                         $this->ilias->db->quote($this->getSurveyId() . "")
02355                 );
02356                 $result = $this->ilias->db->query($query);
02357 
02358           // set the obligatory states in the database
02359                 foreach ($obligatory_questions as $question_fi => $obligatory)
02360                 {
02361                         $query = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02362                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02363                                 $this->ilias->db->quote($question_fi . ""),
02364                                 $this->ilias->db->quote($obligatory . "")
02365                         );
02366                         $result = $this->ilias->db->query($query);
02367                 }
02368         }
02369         
02378         function &getObligatoryStates()
02379         {
02380                 $obligatory_states = array();
02381                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
02382                         $this->ilias->db->quote($this->getSurveyId() . "")
02383                 );
02384                 $result = $this->ilias->db->query($query);
02385                 if ($result->numRows())
02386                 {
02387                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02388                         {
02389                                 $obligatory_states[$row["question_fi"]] = $row["obligatory"];
02390                         }
02391                 }
02392                 return $obligatory_states;
02393         }
02394         
02402         function &getSurveyPages()
02403         {
02404                 $obligatory_states =& $this->getObligatoryStates();
02405                 // get questionblocks
02406                 $all_questions = array();
02407                 $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",
02408                         $this->ilias->db->quote($this->getSurveyId())
02409                 );
02410                 $result = $this->ilias->db->query($query);
02411                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02412                 {
02413                         $all_questions[$row["question_id"]] = $row;
02414                 }
02415                 // get all questionblocks
02416                 $questionblocks = array();
02417                 $in = join(array_keys($all_questions), ",");
02418                 if ($in)
02419                 {
02420                         $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)",
02421                                 $this->ilias->db->quote($this->getSurveyId())
02422                         );
02423                         $result = $this->ilias->db->query($query);
02424                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02425                         {
02426                                 $questionblocks["$row->question_fi"] = $row;
02427                         }                       
02428                 }
02429                 
02430                 $all_pages = array();
02431                 $pageindex = -1;
02432                 $currentblock = "";
02433                 foreach ($all_questions as $question_id => $row)
02434                 {
02435                         if (array_key_exists($question_id, $obligatory_states))
02436                         {
02437                                 $all_questions["$question_id"]["obligatory"] = $obligatory_states["$question_id"];
02438                         }
02439                         $constraints = array();
02440                         if (isset($questionblocks[$question_id]))
02441                         {
02442                                 if (!$currentblock or ($currentblock != $questionblocks[$question_id]->questionblock_id))
02443                                 {
02444                                         $pageindex++;
02445                                 }
02446                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02447                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02448                                 $currentblock = $questionblocks[$question_id]->questionblock_id;
02449                                 $constraints = $this->getConstraints($question_id);
02450                                 $all_questions[$question_id]["constraints"] = $constraints;
02451                         }
02452                         else
02453                         {
02454                                 $pageindex++;
02455                                 $all_questions[$question_id]["questionblock_title"] = "";
02456                                 $all_questions[$question_id]["questionblock_id"] = "";
02457                                 $currentblock = "";
02458                                 $constraints = $this->getConstraints($question_id);
02459                                 $all_questions[$question_id]["constraints"] = $constraints;
02460                         }
02461                         if (!isset($all_pages[$pageindex]))
02462                         {
02463                                 $all_pages[$pageindex] = array();
02464                         }
02465                         array_push($all_pages[$pageindex], $all_questions[$question_id]);
02466                 }
02467                 // calculate position percentage for every page
02468                 $max = count($all_pages);
02469                 $counter = 1;
02470                 foreach ($all_pages as $index => $block)
02471                 {
02472                         foreach ($block as $blockindex => $question)
02473                         {
02474                                 $all_pages[$index][$blockindex][position] = $counter / $max;
02475                         }
02476                         $counter++;
02477                 }
02478                 return $all_pages;
02479         }
02480         
02491         function getNextPage($active_page_question_id, $direction)
02492         {
02493                 $foundpage = -1;
02494                 $pages =& $this->getSurveyPages();
02495                 if (strcmp($active_page_question_id, "") == 0)
02496                 {
02497                         return $pages[0];
02498                 }
02499                 
02500                 foreach ($pages as $key => $question_array)
02501                 {
02502                         foreach ($question_array as $question)
02503                         {
02504                                 if ($active_page_question_id == $question["question_id"])
02505                                 {
02506                                         $foundpage = $key;
02507                                 }
02508                         }
02509                 }
02510                 if ($foundpage == -1)
02511                 {
02512                         // error: page not found
02513                 }
02514                 else
02515                 {
02516                         $foundpage += $direction;
02517                         if ($foundpage < 0)
02518                         {
02519                                 return 0;
02520                         }
02521                         if ($foundpage >= count($pages))
02522                         {
02523                                 return 1;
02524                         }
02525                         return $pages[$foundpage];
02526                 }
02527         }
02528                 
02537         function &getAvailableQuestionpools($use_obj_id = false)
02538         {
02539                 global $rbacsystem;
02540                 
02541                 $result_array = array();
02542                 $query = "SELECT object_data.*, object_data.obj_id, object_reference.ref_id FROM object_data, object_reference WHERE object_data.obj_id = object_reference.obj_id AND object_data.type = 'spl'";
02543                 $result = $this->ilias->db->query($query);
02544                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02545                 {               
02546                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
02547                         {
02548                                 if ($use_obj_id)
02549                                 {
02550                                         $result_array[$row->obj_id] = $row->title;
02551                                 }
02552                                 else
02553                                 {
02554                                         $result_array[$row->ref_id] = $row->title;
02555                                 }
02556                         }
02557                 }
02558                 return $result_array;
02559         }
02560         
02568         function getConstraints($question_id)
02569         {
02570                 $result_array = array();
02571                 $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",
02572                         $this->ilias->db->quote($question_id),
02573                         $this->ilias->db->quote($this->getSurveyId())
02574                 );
02575                 $result = $this->ilias->db->query($query);
02576                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02577                 {               
02578                         array_push($result_array, array("id" => $row->constraint_id, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "value" => $row->value));
02579                 }
02580                 return $result_array;
02581         }
02582 
02590         function _getConstraints($survey_id)
02591         {
02592                 global $ilDB;
02593                 $result_array = array();
02594                 $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",
02595                         $ilDB->quote($survey_id . "")
02596                 );
02597                 $result = $ilDB->query($query);
02598                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02599                 {               
02600                         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));
02601                 }
02602                 return $result_array;
02603         }
02604 
02605 
02613         function &getVariables($question_id)
02614         {
02615                 $result_array = array();
02616                 $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",
02617                         $this->ilias->db->quote($question_id)
02618                 );
02619                 $result = $this->ilias->db->query($query);
02620                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02621                 {
02622                         $result_array[$row->sequence] = $row;
02623                 }
02624                 return $result_array;
02625         }
02626         
02638         function addConstraint($to_question_id, $if_question_id, $relation, $value)
02639         {
02640                 $query = sprintf("INSERT INTO survey_constraint (constraint_id, question_fi, relation_fi, value) VALUES (NULL, %s, %s, %s)",
02641                         $this->ilias->db->quote($if_question_id),
02642                         $this->ilias->db->quote($relation),
02643                         $this->ilias->db->quote($value)
02644                 );
02645                 $result = $this->ilias->db->query($query);
02646                 if ($result == DB_OK) {
02647                         $constraint_id = $this->ilias->db->getLastInsertId();
02648                         $query = sprintf("INSERT INTO survey_question_constraint (question_constraint_id, survey_fi, question_fi, constraint_fi) VALUES (NULL, %s, %s, %s)",
02649                                 $this->ilias->db->quote($this->getSurveyId()),
02650                                 $this->ilias->db->quote($to_question_id),
02651                                 $this->ilias->db->quote($constraint_id)
02652                         );
02653                         $result = $this->ilias->db->query($query);
02654                 }
02655         }
02656         
02664         function getAllRelations($short_as_key = false)
02665         {
02666                 $result_array = array();
02667                 $query = "SELECT * FROM survey_relation";
02668                 $result = $this->ilias->db->query($query);
02669                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02670                 {
02671                         if ($short_as_key)
02672                         {
02673                                 $result_array[$row->shortname] = array("short" => $row->shortname, "long" => $row->longname, "id" => $row->relation_id);
02674                         }
02675                         else
02676                         {
02677                                 $result_array[$row->relation_id] = array("short" => $row->shortname, "long" => $row->longname);
02678                         }
02679                 }
02680                 return $result_array;
02681         }
02682 
02691         function disinviteUser($user_id)
02692         {
02693                 $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s AND user_fi = %s",
02694                         $this->ilias->db->quote($this->getSurveyId()),
02695                         $this->ilias->db->quote($user_id)
02696                 );
02697                 $result = $this->ilias->db->query($query);
02698                 if ($this->getInvitation() == INVITATION_ON)
02699                 {
02700                         $userObj = new ilObjUser($user_id);
02701                         $userObj->dropDesktopItem($this->getRefId(), "svy");
02702                 }
02703         }
02704 
02713         function inviteUser($user_id)
02714         {
02715                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE user_fi = %s AND survey_fi = %s",
02716                         $this->ilias->db->quote($user_id),
02717                         $this->ilias->db->quote($this->getSurveyId())
02718                 );
02719                 $result = $this->ilias->db->query($query);
02720                 if ($result->numRows() < 1)
02721                 {
02722                         $query = sprintf("INSERT INTO survey_invited_user (invited_user_id, survey_fi, user_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02723                                 $this->ilias->db->quote($this->getSurveyId()),
02724                                 $this->ilias->db->quote($user_id)
02725                         );
02726                         $result = $this->ilias->db->query($query);
02727                 }
02728                 if ($this->getInvitation() == INVITATION_ON)
02729                 {
02730                         $userObj = new ilObjUser($user_id);
02731                         $userObj->addDesktopItem($this->getRefId(), "svy");
02732                 }
02733         }
02734 
02743         function inviteGroup($group_id)
02744         {
02745                 include_once "./classes/class.ilObjGroup.php";
02746                 $group = new ilObjGroup($group_id);
02747                 $members = $group->getGroupMemberIds();
02748                 foreach ($members as $user_id)
02749                 {
02750                         $this->inviteUser($user_id);
02751                         if ($this->getInvitation() == INVITATION_ON)
02752                         {
02753                                 $userObj = new ilObjUser($user_id);
02754                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02755                         }
02756                 }
02757         }
02758         
02767         function inviteRole($role_id)
02768         {
02769                 global $rbacreview;
02770                 $members = $rbacreview->assignedUsers($role_id);
02771                 foreach ($members as $user_id)
02772                 {
02773                         $this->inviteUser($user_id);
02774                         if ($this->getInvitation() == INVITATION_ON)
02775                         {
02776                                 $userObj = new ilObjUser($user_id);
02777                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02778                         }
02779                 }
02780         }
02781         
02790         function &getInvitedUsers()
02791         {
02792                 $result_array = array();
02793                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
02794                         $this->ilias->db->quote($this->getSurveyId())
02795                 );
02796                 $result = $this->ilias->db->query($query);
02797                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02798                 {
02799                         array_push($result_array, $row->user_fi);
02800                 }
02801                 return $result_array;
02802         }
02803 
02813         function deleteWorkingData($question_id, $user_id)
02814         {
02815                 $query = "";
02816                 if ($this->getAnonymize())
02817                 {
02818                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02819                                 $this->ilias->db->quote($this->getSurveyId()),
02820                                 $this->ilias->db->quote($question_id),
02821                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02822                         );
02823                 }
02824                 else
02825                 {
02826                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02827                                 $this->ilias->db->quote($this->getSurveyId()),
02828                                 $this->ilias->db->quote($question_id),
02829                                 $this->ilias->db->quote($user_id)
02830                         );
02831                 }
02832                 $result = $this->ilias->db->query($query);
02833         }
02834         
02846         function saveWorkingData($question_id, $user_id, $anonymize_id, $value = "", $text = "")
02847         {
02848                 if ($this->isSurveyStarted($user_id, $anonymize_id) === false)
02849                 {
02850                         $this->startSurvey($user_id, $anonymize_id);
02851                 }
02852                 if (strcmp($value, "") == 0)
02853                 {
02854                         $value = "NULL";
02855                 }
02856                 else
02857                 {
02858                         $value = $this->ilias->db->quote($value);
02859                 }
02860                 if (strcmp($text, "") == 0)
02861                 {
02862                         $text = "NULL";
02863                 }
02864                 else
02865                 {
02866                         $text = $this->ilias->db->quote($text);
02867                 }
02868                 if ($this->getAnonymize())
02869                 {
02870                         $user_id = 0;
02871                 }
02872                 $query = sprintf("INSERT INTO survey_answer (answer_id, survey_fi, question_fi, user_fi, anonymous_id, value, textanswer, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, NULL)",
02873                         $this->ilias->db->quote($this->getSurveyId() . ""),
02874                         $this->ilias->db->quote($question_id . ""),
02875                         $this->ilias->db->quote($user_id . ""),
02876                         $this->ilias->db->quote($anonymize_id),
02877                         $value,
02878                         $text
02879                 );
02880                 $result = $this->ilias->db->query($query);
02881         }
02882         
02893         function loadWorkingData($question_id, $user_id)
02894         {
02895                 $result_array = array();
02896                 $query = "";
02897                 if ($this->getAnonymize())
02898                 {
02899                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02900                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02901                                 $this->ilias->db->quote($question_id. ""),
02902                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02903                         );
02904                 }
02905                 else
02906                 {
02907                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02908                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02909                                 $this->ilias->db->quote($question_id . ""),
02910                                 $this->ilias->db->quote($user_id . "")
02911                         );
02912                 }
02913                 $result = $this->ilias->db->query($query);
02914                 if ($result->numRows() >= 1)
02915                 {
02916                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02917                         {
02918                                 array_push($result_array, $row);
02919                         }
02920                         return $result_array;
02921                 }
02922                 else
02923                 {
02924                         return $result_array;
02925                 }
02926         }
02927 
02936         function startSurvey($user_id, $anonymous_id)
02937         {
02938                 global $ilUser;
02939                 
02940                 if (strcmp($user_id, "") == 0)
02941                 {
02942                         $user_id = 0;
02943                 }
02944                 if ($this->getAnonymize())
02945                 {
02946                         $user_id = 0;
02947                 }
02948                 $query = sprintf("INSERT INTO survey_finished (finished_id, survey_fi, user_fi, anonymous_id, state, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
02949                         $this->ilias->db->quote($this->getSurveyId() . ""),
02950                         $this->ilias->db->quote($user_id . ""),
02951                         $this->ilias->db->quote($anonymous_id . ""),
02952                         $this->ilias->db->quote(0 . "")
02953                 );
02954                 $result = $this->ilias->db->query($query);
02955                 if ($this->getAnonymize())
02956                 {
02957                         if (strcmp($ilUser->login, "anonymous") != 0)
02958                         {
02959                                 require_once "./include/inc.mail.php";
02960                                 require_once "./classes/class.ilFormatMail.php";
02961                                 require_once "./classes/class.ilMailbox.php";
02962                                 $subject = sprintf($this->lng->txt("subject_mail_survey_id"), $this->getTitle());
02963                                 $message = sprintf($this->lng->txt("message_mail_survey_id"), $this->getTitle(), $_SESSION["anonymous_id"]);
02964                                 $umail = new ilFormatMail($ilUser->id);
02965                                 $f_message = $umail->formatLinebreakMessage($message);
02966                                 $umail->setSaveInSentbox(true);
02967                                 if($error_message = $umail->sendMail($ilUser->getLogin(),"",
02968                                                                                                          "",$subject,$f_message,
02969                                                                                                          array(),array("normal")))
02970                                 {
02971                                         sendInfo($error_message);
02972                                 }
02973                         }
02974                 }
02975         }
02976                         
02985         function finishSurvey($user_id, $anonymize_id)
02986         {
02987                 if ($this->getAnonymize())
02988                 {
02989                         $user_id = 0;
02990                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND anonymous_id = %s",
02991                                 $this->ilias->db->quote("1"),
02992                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02993                                 $this->ilias->db->quote($anonymize_id . "")
02994                         );
02995                 }
02996                 else
02997                 {
02998                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND user_fi = %s",
02999                                 $this->ilias->db->quote("1"),
03000                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03001                                 $this->ilias->db->quote($user_id . "")
03002                         );
03003                 }
03004                 $result = $this->ilias->db->query($query);
03005         }
03006         
03016         function isSurveyStarted($user_id, $anonymize_id)
03017         {
03018                 if ($this->getAnonymize())
03019                 {
03020                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
03021                                 $this->ilias->db->quote($this->getSurveyId()),
03022                                 $this->ilias->db->quote($anonymize_id)
03023                         );
03024                 }
03025                 else
03026                 {
03027                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03028                                 $this->ilias->db->quote($this->getSurveyId()),
03029                                 $this->ilias->db->quote($user_id)
03030                         );
03031                 }
03032                 $result = $this->ilias->db->query($query);
03033                 if ($result->numRows() == 0)
03034                 {
03035                         return false;
03036                 }                       
03037                 else
03038                 {
03039                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03040                         return (int)$row["state"];
03041                 }
03042         }
03043         
03053         function getLastActivePage($user_id)
03054         {
03055                 $query = "";
03056                 if ($this->getAnonymize())
03057                 {
03058                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s ORDER BY TIMESTAMP14 DESC",
03059                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03060                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
03061                         );
03062                 }
03063                 else
03064                 {
03065                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND user_fi = %s ORDER BY TIMESTAMP14 DESC",
03066                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03067                                 $this->ilias->db->quote($user_id . "")
03068                         );
03069                 }
03070                 $result = $this->ilias->db->query($query);
03071                 if ($result->numRows() == 0)
03072                 {
03073                         return "";
03074                 }
03075                 else
03076                 {
03077                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03078                         return $row["question_fi"];
03079                 }
03080         }
03081 
03092         function checkConstraint($constraint_data, $working_data)
03093         {
03094                 if (count($working_data) == 0)
03095                 {
03096                         return 0;
03097                 }
03098                 
03099                 if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
03100                 {
03101                         return 0;
03102                 }
03103                 
03104                 foreach ($working_data as $data)
03105                 {
03106                         switch ($constraint_data["short"])
03107                         {
03108                                 case "<":
03109                                         if ($data["value"] < $constraint_data["value"])
03110                                         {
03111                                                 return 1;
03112                                         }
03113                                         break;
03114                                 case "<=":
03115                                         if ($data["value"] <= $constraint_data["value"])
03116                                         {
03117                                                 return 1;
03118                                         }
03119                                         break;
03120                                 case "=":
03121                                         if ($data["value"] == $constraint_data["value"])
03122                                         {
03123                                                 return 1;
03124                                         }
03125                                         break;
03126                                 case "<>":
03127                                         if ($data["value"] != $constraint_data["value"])
03128                                         {
03129                                                 return 1;
03130                                         }
03131                                         break;
03132                                 case ">=":
03133                                         if ($data["value"] >= $constraint_data["value"])
03134                                         {
03135                                                 return 1;
03136                                         }
03137                                         break;
03138                                 case ">":
03139                                         if ($data["value"] > $constraint_data["value"])
03140                                         {
03141                                                 return 1;
03142                                         }
03143                                         break;
03144                         }
03145                 }
03146                 return 0;
03147         }
03148 
03149         function &getEvaluationForAllUsers()
03150         {
03151                 $users = array();
03152                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03153                         $this->ilias->db->quote($this->getSurveyId() . "")
03154                 );
03155                 $result = $this->ilias->db->query($query);
03156                 if ($result->numRows())
03157                 {
03158                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03159                         {
03160                                 array_push($users, $row);
03161                         }
03162                 }
03163                 $evaluation = array();
03164                 $questions =& $this->getSurveyQuestions();
03165                 foreach ($users as $row)
03166                 {
03167                         if ($row["user_fi"] > 0)
03168                         {
03169                                 $evaluation[$row["user_fi"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03170                         }
03171                         else
03172                         {
03173                                 if (strlen($row["anonymous_id"]) > 0)
03174                                 {
03175                                         $evaluation[$row["anonymous_id"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03176                                 }
03177                         }
03178                 }
03179                 return $evaluation;
03180         }
03181         
03193         function &getEvaluationByUser($questions, $user_id, $anonymous_id = "")
03194         {
03195                 $wherecond = "";
03196                 $wherevalue = "";
03197                 if (strcmp($anonymous_id, "") != 0)
03198                 {
03199                         $wherecond = "anonymous_id = %s";
03200                         $wherevalue = $anonymous_id;
03201                 }
03202                 else
03203                 {
03204                         $wherecond = "user_fi = %s";
03205                         $wherevalue = $user_id;
03206                 }
03207                 // collect all answers
03208                 $answers = array();
03209                 $query = sprintf("SELECT * FROM survey_answer WHERE $wherecond AND survey_fi = %s",
03210                         $this->ilias->db->quote($wherevalue),
03211                         $this->ilias->db->quote($this->getSurveyId())
03212                 );
03213                 $result = $this->ilias->db->query($query);
03214                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03215                 {
03216                         if (!is_array($answers[$row["question_fi"]]))
03217                         {
03218                                 $answers[$row["question_fi"]] = array();
03219                         }
03220                         array_push($answers[$row["question_fi"]], $row);
03221                 }
03222                 $username = "";
03223                 $gender = "";
03224                 if ($user_id > 0)
03225                 {
03226                         include_once "./classes/class.ilObjUser.php";
03227                         if (strlen(ilObjUser::_lookupLogin($user_id)) == 0)
03228                         {
03229                                 $username = $this->lng->txt("deleted_user");
03230                                 $gender = "";
03231                         }
03232                         else
03233                         {
03234                                 $user = new ilObjUser($user_id);
03235                                 $username = $user->getFullname();
03236                                 $gender = $user->getGender();
03237                                 if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
03238                         }
03239                 }
03240                 $resultset = array(
03241                         "name" => $username,
03242                         "gender" => $gender,
03243                         "answers" => array()
03244                 );
03245                 foreach ($questions as $key => $question)
03246                 {
03247                         if (array_key_exists($key, $answers))
03248                         {
03249                                 $resultset["answers"][$key] = $answers[$key];
03250                         }
03251                         else
03252                         {
03253                                 $resultset["answers"][$key] = array();
03254                         }
03255                         sort($resultset["answers"][$key]);
03256                 }
03257                 return $resultset;
03258         }
03259         
03270         function getEvaluation($question_id)
03271         {
03272                 $questions =& $this->getSurveyQuestions();
03273                 $result_array = array();
03274                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03275                         $this->ilias->db->quote($this->getSurveyId())
03276                 );
03277                 $result = $this->ilias->db->query($query);
03278                 $nr_of_users = $result->numRows();
03279                                 
03280                 $query = sprintf("SELECT * FROM survey_answer WHERE question_fi = %s AND survey_fi = %s",
03281                         $this->ilias->db->quote($question_id),
03282                         $this->ilias->db->quote($this->getSurveyId())
03283                 );
03284                 $result = $this->ilias->db->query($query);
03285                 $cumulated = array();
03286                 $textvalues = array();
03287                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03288                 {
03289                         $cumulated["$row->value"]++;
03290                         array_push($textvalues, $row->textanswer);
03291                 }
03292                 asort($cumulated, SORT_NUMERIC);
03293                 end($cumulated);
03294                 $numrows = $result->numRows();
03295                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03296                 {
03297                         if ($this->getAnonymize())
03298                         {
03299                                 $query = sprintf("SELECT answer_id, concat( question_fi,  \"_\", anonymous_id )  AS groupval FROM `survey_answer` WHERE question_fi = %s AND survey_fi = %s GROUP BY groupval",
03300                                         $this->ilias->db->quote($question_id),
03301                                         $this->ilias->db->quote($this->getSurveyId())
03302                                 );
03303                         }
03304                         else
03305                         {
03306                                 $query = sprintf("SELECT answer_id, concat( question_fi,  \"_\", user_fi )  AS groupval FROM `survey_answer` WHERE question_fi = %s AND survey_fi = %s GROUP BY groupval",
03307                                         $this->ilias->db->quote($question_id),
03308                                         $this->ilias->db->quote($this->getSurveyId())
03309                                 );
03310                         }
03311                         $mcmr_result = $this->ilias->db->query($query);
03312                         $result_array["USERS_ANSWERED"] = $mcmr_result->numRows();
03313                         $result_array["USERS_SKIPPED"] = $nr_of_users - $mcmr_result->numRows();
03314                         $numrows = $mcmr_result->numRows();
03315                 }
03316                 else
03317                 {
03318                         $result_array["USERS_ANSWERED"] = $result->numRows();
03319                         $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
03320                 }
03321                 $variables =& $this->getVariables($question_id);
03322                 switch ($questions[$question_id]["type_tag"])
03323                 {
03324                         case "qt_nominal":
03325                                 $result_array["MEDIAN"] = "";
03326                                 $result_array["ARITHMETIC_MEAN"] = "";
03327                                 $prefix = "";
03328                                 if (strcmp(key($cumulated), "") != 0)
03329                                 {
03330                                         $prefix = (key($cumulated)+1) . " - ";
03331                                 }
03332                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03333                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03334                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03335                                 $maxvalues = 0;
03336                                 foreach ($variables as $key => $value)
03337                                 {
03338                                         $maxvalues += $cumulated[$key];
03339                                 }
03340                                 foreach ($variables as $key => $value)
03341                                 {
03342                                         $percentage = 0;
03343                                         if ($numrows > 0)
03344                                         {
03345                                                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03346                                                 {
03347                                                         if ($maxvalues > 0)
03348                                                         {
03349                                                                 $percentage = (float)((int)$cumulated[$key]/$maxvalues);
03350                                                         }
03351                                                 }
03352                                                 else
03353                                                 {
03354                                                         $percentage = (float)((int)$cumulated[$key]/$numrows);
03355                                                 }
03356                                         }
03357                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03358                                 }
03359                                 break;
03360                         case "qt_ordinal":
03361                                 $prefix = "";
03362                                 if (strcmp(key($cumulated), "") != 0)
03363                                 {
03364                                         $prefix = (key($cumulated)+1) . " - ";
03365                                 }
03366                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03367                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03368                                 foreach ($variables as $key => $value)
03369                                 {
03370                                         $percentage = 0;
03371                                         if ($numrows > 0)
03372                                         {
03373                                                 $percentage = (float)((int)$cumulated[$key]/$numrows);
03374                                         }
03375                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03376                                 }
03377                                 ksort($cumulated, SORT_NUMERIC);
03378                                 $median = array();
03379                                 $total = 0;
03380                                 foreach ($cumulated as $value => $key)
03381                                 {
03382                                         $total += $key;
03383                                         for ($i = 0; $i < $key; $i++)
03384                                         {
03385                                                 array_push($median, $value+1);
03386                                         }
03387                                 }
03388                                 if ($total > 0)
03389                                 {
03390                                         if (($total % 2) == 0)
03391                                         {
03392                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03393                                                 if (round($median_value) != $median_value)
03394                                                 {
03395                                                         $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $variables[floor($median_value)-1]->title . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $variables[ceil($median_value)-1]->title . ")";
03396                                                 }
03397                                         }
03398                                         else
03399                                         {
03400                                                 $median_value = $median[(($total+1)/2)-1];
03401                                         }
03402                                 }
03403                                 else
03404                                 {
03405                                         $median_value = "";
03406                                 }
03407                                 $result_array["ARITHMETIC_MEAN"] = "";
03408                                 $result_array["MEDIAN"] = $median_value;
03409                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03410                                 break;
03411                         case "qt_metric":
03412                                 $result_array["MODE"] = key($cumulated);
03413                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03414                                 ksort($cumulated, SORT_NUMERIC);
03415                                 $counter = 0;
03416                                 foreach ($cumulated as $value => $nr_of_users)
03417                                 {
03418                                         $percentage = 0;
03419                                         if ($numrows > 0)
03420                                         {
03421                                                 $percentage = (float)($nr_of_users/$numrows);
03422                                         }
03423                                         $result_array["values"][$counter++] = array("value" => $value, "selected" => (int)$nr_of_users, "percentage" => $percentage);
03424                                 }
03425                                 $median = array();
03426                                 $total = 0;
03427                                 $x_i = 0;
03428                                 $p_i = 1;
03429                                 $x_i_inv = 0;
03430                                 $sum_part_zero = false;
03431                                 foreach ($cumulated as $value => $key)
03432                                 {
03433                                         $total += $key;
03434                                         for ($i = 0; $i < $key; $i++)
03435                                         {
03436                                                 array_push($median, $value);
03437                                                 $x_i += $value;
03438                                                 $p_i *= $value;
03439                                                 if ($value != 0)
03440                                                 {
03441                                                         $sum_part_zero = true;
03442                                                         $x_i_inv += 1/$value;
03443                                                 }
03444                                         }
03445                                 }
03446                                 if ($total > 0)
03447                                 {
03448                                         if (($total % 2) == 0)
03449                                         {
03450                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03451                                         }
03452                                         else
03453                                         {
03454                                                 $median_value = $median[(($total+1)/2)-1];
03455                                         }
03456                                 }
03457                                 else
03458                                 {
03459                                         $median_value = "";
03460                                 }
03461                                 if ($total > 0)
03462                                 {
03463                                         if (($x_i/$total) == (int)($x_i/$total))
03464                                         {
03465                                                 $result_array["ARITHMETIC_MEAN"] = $x_i/$total;
03466                                         }
03467                                         else
03468                                         {
03469                                                 $result_array["ARITHMETIC_MEAN"] = sprintf("%.2f", $x_i/$total);
03470                                         }
03471                                 }
03472                                 else
03473                                 {
03474                                         $result_array["ARITHMETIC_MEAN"] = "";
03475                                 }
03476                                 $result_array["MEDIAN"] = $median_value;
03477                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03478                                 break;
03479                         case "qt_text":
03480                                 $result_array["ARITHMETIC_MEAN"] = "";
03481                                 $result_array["MEDIAN"] = "";
03482                                 $result_array["MODE"] = "";
03483                                 $result_array["MODE_NR_OF_SELECTIONS"] = "";
03484                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03485                                 $result_array["textvalues"] = $textvalues;
03486                                 break;
03487                 }
03488                 return $result_array;
03489         }
03490 
03491         function &getQuestions($question_ids)
03492         {
03493                 $result_array = array();
03494                 $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, ",") . ")";
03495                 $result = $this->ilias->db->query($query);
03496                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03497                 {
03498                         array_push($result_array, $row);
03499                 }
03500                 return $result_array;
03501         }
03502         
03503         function &getQuestionblocks($questionblock_ids)
03504         {
03505                 $result_array = array();
03506     $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";
03507                 $result = $this->ilias->db->query($query);
03508                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03509                 {
03510                         if ($row["questionblock_id"] != $qbid)
03511                         {
03512                                 $sequence = 1;
03513                         }
03514                         $row["sequence"] = $sequence++;
03515                         $result_array[$row["questionblock_id"]][$row["question_id"]] = $row;
03516                         $qbid = $row["questionblock_id"];
03517                 }
03518                 return $result_array;
03519         }
03520 
03521         function &getForbiddenQuestionpools()
03522         {
03523                 global $rbacsystem;
03524                 
03525                 // get all available questionpools and remove the trashed questionspools
03526                 $forbidden_pools = array();
03527                 $query = "SELECT object_data.*, object_data.obj_id, object_reference.ref_id FROM object_data, object_reference WHERE object_data.obj_id = object_reference.obj_id AND object_data.type = 'spl'";
03528                 $result = $this->ilias->db->query($query);
03529                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03530                 {               
03531                         if (!$rbacsystem->checkAccess("write", $row->ref_id) || (!$this->_hasUntrashedReference($row->obj_id)))
03532                         {
03533                                 array_push($forbidden_pools, $row->obj_id);
03534                         }
03535                 }
03536                 return $forbidden_pools;
03537         }
03538         
03546         function getQuestionsTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0, $completeonly = 0, $filter_question_type = "", $filter_questionpool = "")
03547         {
03548                 global $ilUser;
03549                 $where = "";
03550                 if (strlen($filter_text) > 0) {
03551                         switch($sel_filter_type) {
03552                                 case "title":
03553                                         $where = " AND survey_question.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03554                                         break;
03555                                 case "description":
03556                                         $where = " AND survey_question.description LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03557                                         break;
03558                                 case "author":
03559                                         $where = " AND survey_question.author LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03560                                         break;
03561                         }
03562                 }
03563   
03564                 if ($filter_question_type && (strcmp($filter_question_type, "all") != 0))
03565                 {
03566                         $where .= " AND survey_questiontype.type_tag = " . $this->ilias->db->quote($filter_question_type);
03567                 }
03568                 
03569                 if ($filter_questionpool && (strcmp($filter_questionpool, "all") != 0))
03570                 {
03571                         $where .= " AND survey_question.obj_fi = $filter_questionpool";
03572                 }
03573   
03574     // build sort order for sql query
03575                 $order = "";
03576                 $images = array();
03577     if (count($sortoptions)) {
03578       foreach ($sortoptions as $key => $value) {
03579         switch($key) {
03580           case "title":
03581             $order = " ORDER BY title $value";
03582             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03583             break;
03584           case "description":
03585             $order = " ORDER BY description $value";
03586             $images["description"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03587             break;
03588           case "type":
03589             $order = " ORDER BY questiontype_id $value";
03590             $images["type"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03591             break;
03592           case "author":
03593             $order = " ORDER BY author $value";
03594             $images["author"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03595             break;
03596           case "created":
03597             $order = " ORDER BY created $value";
03598             $images["created"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03599             break;
03600           case "updated":
03601             $order = " ORDER BY TIMESTAMP14 $value";
03602             $images["updated"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03603             break;
03604                                         case "qpl":
03605                                                 $order = " ORDER BY obj_fi $value";
03606             $images["qpl"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03607                                                 break;
03608         }
03609       }
03610     }
03611                 $maxentries = $ilUser->prefs["hits_per_page"];
03612                 if ($maxentries < 1)
03613                 {
03614                         $maxentries = 9999;
03615                 }
03616 
03617                 $forbidden_pools =& $this->getForbiddenQuestionpools();
03618                 $forbidden = "";
03619                 if (count($forbidden_pools))
03620                 {
03621                         $forbidden = " AND survey_question.obj_fi NOT IN (" . join($forbidden_pools, ",") . ")";
03622                 }
03623                 if ($completeonly)
03624                 {
03625                         $forbidden .= " AND survey_question.complete = " . $this->ilias->db->quote("1");
03626                 }
03627 
03628                 $existing = "";
03629                 $existing_questions =& $this->getExistingQuestions();
03630                 if (count($existing_questions))
03631                 {
03632                         $existing = " AND survey_question.question_id NOT IN (" . join($existing_questions, ",") . ")";
03633                 }
03634           $query = "SELECT survey_question.question_id, survey_question.TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_question, survey_questiontype WHERE survey_question.questiontype_fi = survey_questiontype.questiontype_id$forbidden$existing AND ISNULL(survey_question.original_id) " . " $where$order$limit";
03635     $query_result = $this->ilias->db->query($query);
03636                 $max = $query_result->numRows();
03637                 if ($startrow > $max -1)
03638                 {
03639                         $startrow = $max - ($max % $maxentries);
03640                 }
03641                 else if ($startrow < 0)
03642                 {
03643                         $startrow = 0;
03644                 }
03645                 $limit = " LIMIT $startrow, $maxentries";
03646           $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";
03647     $query_result = $this->ilias->db->query($query);
03648                 $rows = array();
03649                 if ($query_result->numRows())
03650                 {
03651                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03652                         {
03653                                 array_push($rows, $row);
03654                         }
03655                 }
03656                 $nextrow = $startrow + $maxentries;
03657                 if ($nextrow > $max - 1)
03658                 {
03659                         $nextrow = $startrow;
03660                 }
03661                 $prevrow = $startrow - $maxentries;
03662                 if ($prevrow < 0)
03663                 {
03664                         $prevrow = 0;
03665                 }
03666                 return array(
03667                         "rows" => $rows,
03668                         "images" => $images,
03669                         "startrow" => $startrow,
03670                         "nextrow" => $nextrow,
03671                         "prevrow" => $prevrow,
03672                         "step" => $maxentries,
03673                         "rowcount" => $max
03674                 );
03675         }
03676 
03684         function getQuestionblocksTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0)
03685         {
03686                 global $ilUser;
03687                 $where = "";
03688                 if (strlen($filter_text) > 0) {
03689                         switch($sel_filter_type) {
03690                                 case "title":
03691                                         $where = " AND survey_questionblock.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03692                                         break;
03693                         }
03694                 }
03695   
03696     // build sort order for sql query
03697                 $order = "";
03698                 $images = array();
03699     if (count($sortoptions)) {
03700       foreach ($sortoptions as $key => $value) {
03701         switch($key) {
03702           case "title":
03703                                                 $order = " ORDER BY survey_questionblock.title $value";
03704             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03705             break;
03706                                         case "svy":
03707                                                 $order = " ORDER BY survey_survey_question.survey_fi $value";
03708             $images["svy"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03709                                                 break;
03710         }
03711       }
03712     }
03713                 $maxentries = $ilUser->prefs["hits_per_page"];
03714                 if ($order)
03715                 {
03716                         $order .=  ",survey_survey_question.sequence ASC";
03717                 }
03718                 else
03719                 {
03720                         $order = " ORDER BY survey_survey_question.sequence ASC";
03721                 }
03722                 $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";
03723     $query_result = $this->ilias->db->query($query);
03724                 $questionblock_ids = array();
03725                 if ($query_result->numRows())
03726                 {
03727                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03728                         {
03729                                 array_push($questionblock_ids, $row["questionblock_id"]);
03730                         }
03731                 }
03732                 
03733                 $max = $query_result->numRows();
03734                 if ($startrow > $max -1)
03735                 {
03736                         $startrow = $max - ($max % $maxentries);
03737                 }
03738                 else if ($startrow < 0)
03739                 {
03740                         $startrow = 0;
03741                 }
03742                 $limit = " LIMIT $startrow, $maxentries";
03743                 $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";
03744     $query_result = $this->ilias->db->query($query);
03745                 $rows = array();
03746                 if ($query_result->numRows())
03747                 {
03748                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03749                         {
03750                                 $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
03751                                 $counter = 1;
03752                                 foreach ($questions_array as $key => $value)
03753                                 {
03754                                         $questions_array[$key] = "$counter. $value";
03755                                         $counter++;
03756                                 }
03757                                 $rows[$row["questionblock_id"]] = array(
03758                                         "questionblock_id" => $row["questionblock_id"],
03759                                         "title" => $row["title"], 
03760                                         "surveytitle" => $row["surveytitle"], 
03761                                         "questions" => join($questions_array, ", "),
03762                                         "owner" => $row["owner_fi"]
03763                                 );
03764                         }
03765                 }
03766                 $nextrow = $startrow + $maxentries;
03767                 if ($nextrow > $max - 1)
03768                 {
03769                         $nextrow = $startrow;
03770                 }
03771                 $prevrow = $startrow - $maxentries;
03772                 if ($prevrow < 0)
03773                 {
03774                         $prevrow = 0;
03775                 }
03776                 return array(
03777                         "rows" => $rows,
03778                         "images" => $images,
03779                         "startrow" => $startrow,
03780                         "nextrow" => $nextrow,
03781                         "prevrow" => $prevrow,
03782                         "step" => $maxentries,
03783                         "rowcount" => $max
03784                 );
03785         }
03786 
03795         function &_getQuestiontypes()
03796         {
03797                 global $ilDB;
03798                 
03799                 $questiontypes = array();
03800                 $query = "SELECT * FROM survey_questiontype ORDER BY type_tag";
03801                 $query_result = $ilDB->query($query);
03802                 while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03803                 {
03804                         array_push($questiontypes, $row["type_tag"]);
03805                 }
03806                 return $questiontypes;
03807         }
03808                 
03817         function to_xml()
03818         {
03819                 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<questestinterop></questestinterop>\n";
03820                 $domxml = domxml_open_mem($xml_header);
03821                 $root = $domxml->document_element();
03822                 // qti assessment
03823                 $qtiSurvey = $domxml->create_element("survey");
03824                 $qtiSurvey->set_attribute("ident", $this->getSurveyId());
03825                 $qtiSurvey->set_attribute("title", $this->getTitle());
03826                 
03827                 // add qti comment
03828                 $qtiComment = $domxml->create_element("qticomment");
03829                 $qtiCommentText = $domxml->create_text_node($this->getDescription());
03830                 $qtiComment->append_child($qtiCommentText);
03831                 $qtiSurvey->append_child($qtiComment);
03832                 $qtiComment = $domxml->create_element("qticomment");
03833                 $qtiCommentText = $domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
03834                 $qtiComment->append_child($qtiCommentText);
03835                 $qtiSurvey->append_child($qtiComment);
03836                 $qtiComment = $domxml->create_element("qticomment");
03837                 $qtiCommentText = $domxml->create_text_node("Author=".$this->getAuthor());
03838                 $qtiComment->append_child($qtiCommentText);
03839                 $qtiSurvey->append_child($qtiComment);
03840                 // add qti objectives
03841                 $qtiObjectives = $domxml->create_element("objectives");
03842                 $qtiMaterial = $domxml->create_element("material");
03843                 $qtiMaterial->set_attribute("label", "introduction");
03844                 $qtiMatText = $domxml->create_element("mattext");
03845                 $qtiMatTextText = $domxml->create_text_node($this->getIntroduction());
03846                 $qtiMatText->append_child($qtiMatTextText);
03847                 $qtiMaterial->append_child($qtiMatText);
03848                 $qtiObjectives->append_child($qtiMaterial);
03849                 $qtiSurvey->append_child($qtiObjectives);
03850                 // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
03851                 $qtiMetadata = $domxml->create_element("qtimetadata");
03852                 // author
03853                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03854                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03855                 $qtiFieldLabelText = $domxml->create_text_node("author");
03856                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03857                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03858                 $qtiFieldEntryText = $domxml->create_text_node($this->getAuthor());
03859                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03860                 $qtiMetadatafield->append_child($qtiFieldLabel);
03861                 $qtiMetadatafield->append_child($qtiFieldEntry);
03862                 $qtiMetadata->append_child($qtiMetadatafield);
03863                 // description
03864                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03865                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03866                 $qtiFieldLabelText = $domxml->create_text_node("description");
03867                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03868                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03869                 $qtiFieldEntryText = $domxml->create_text_node($this->getDescription());
03870                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03871                 $qtiMetadatafield->append_child($qtiFieldLabel);
03872                 $qtiMetadatafield->append_child($qtiFieldEntry);
03873                 $qtiMetadata->append_child($qtiMetadatafield);
03874                 // evaluation access
03875                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03876                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03877                 $qtiFieldLabelText = $domxml->create_text_node("evaluation_access");
03878                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03879                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03880                 $qtiFieldEntryText = $domxml->create_text_node($this->getEvaluationAccess());
03881                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03882                 $qtiMetadatafield->append_child($qtiFieldLabel);
03883                 $qtiMetadatafield->append_child($qtiFieldEntry);
03884                 $qtiMetadata->append_child($qtiMetadatafield);
03885                 // anonymization
03886                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03887                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03888                 $qtiFieldLabelText = $domxml->create_text_node("anonymize");
03889                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03890                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03891                 $qtiFieldEntryText = $domxml->create_text_node($this->getAnonymize());
03892                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03893                 $qtiMetadatafield->append_child($qtiFieldLabel);
03894                 $qtiMetadatafield->append_child($qtiFieldEntry);
03895                 $qtiMetadata->append_child($qtiMetadatafield);
03896                 // status
03897                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03898                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03899                 $qtiFieldLabelText = $domxml->create_text_node("status");
03900                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03901                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03902                 $qtiFieldEntryText = $domxml->create_text_node($this->getStatus());
03903                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03904                 $qtiMetadatafield->append_child($qtiFieldLabel);
03905                 $qtiMetadatafield->append_child($qtiFieldEntry);
03906                 $qtiMetadata->append_child($qtiMetadatafield);
03907                 // start date
03908                 if ($this->getStartDateEnabled())
03909                 {
03910                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03911                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
03912                         $qtiFieldLabelText = $domxml->create_text_node("startdate");
03913                         $qtiFieldLabel->append_child($qtiFieldLabelText);
03914                         $qtiFieldEntry = $domxml->create_element("fieldentry");
03915                         $qtiFieldEntryText = $domxml->create_text_node(sprintf("P%dY%dM%dDT0H0M0S", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
03916                         $qtiFieldEntry->append_child($qtiFieldEntryText);
03917                         $qtiMetadatafield->append_child($qtiFieldLabel);
03918                         $qtiMetadatafield->append_child($qtiFieldEntry);
03919                         $qtiMetadata->append_child($qtiMetadatafield);
03920                 }
03921                 // end date
03922                 if ($this->getEndDateEnabled())
03923                 {
03924                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03925                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
03926                         $qtiFieldLabelText = $domxml->create_text_node("enddate");
03927                         $qtiFieldLabel->append_child($qtiFieldLabelText);
03928                         $qtiFieldEntry = $domxml->create_element("fieldentry");
03929                         $qtiFieldEntryText = $domxml->create_text_node(sprintf("P%dY%dM%dDT0H0M0S", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
03930                         $qtiFieldEntry->append_child($qtiFieldEntryText);
03931                         $qtiMetadatafield->append_child($qtiFieldLabel);
03932                         $qtiMetadatafield->append_child($qtiFieldEntry);
03933                         $qtiMetadata->append_child($qtiMetadatafield);
03934                 }
03935                 // show question titles
03936                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03937                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03938                 $qtiFieldLabelText = $domxml->create_text_node("display_question_titles");
03939                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03940                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03941                 $qtiFieldEntryText = $domxml->create_text_node($this->getShowQuestionTitles());
03942                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03943                 $qtiMetadatafield->append_child($qtiFieldLabel);
03944                 $qtiMetadatafield->append_child($qtiFieldEntry);
03945                 $qtiMetadata->append_child($qtiMetadatafield);
03946                 // add questionblock descriptions
03947                 $pages =& $this->getSurveyPages();
03948                 foreach ($pages as $question_array)
03949                 {
03950                         if (count($question_array) > 1)
03951                         {
03952                                 $question_ids = array();
03953                                 // found a questionblock
03954                                 foreach ($question_array as $question)
03955                                 {
03956                                         array_push($question_ids, $question["question_id"]);
03957                                 }
03958                                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03959                                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03960                                 $qtiFieldLabelText = $domxml->create_text_node("questionblock_" . $question_array[0]["questionblock_id"]);
03961                                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03962                                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03963                                 $qtiFieldEntryText = $domxml->create_text_node("<title>" . $question["questionblock_title"]. "</title><questions>" . join($question_ids, ",") . "</questions>");
03964                                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03965                                 $qtiMetadatafield->append_child($qtiFieldLabel);
03966                                 $qtiMetadatafield->append_child($qtiFieldEntry);
03967                                 $qtiMetadata->append_child($qtiMetadatafield);
03968                         }
03969                 }
03970                 // add constraints
03971                 foreach ($pages as $question_array)
03972                 {
03973                         foreach ($question_array as $question)
03974                         {
03975                                 if (count($question["constraints"]))
03976                                 {
03977                                         // found constraints
03978                                         foreach ($question["constraints"] as $constraint)
03979                                         {
03980                                                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03981                                                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03982                                                 $qtiFieldLabelText = $domxml->create_text_node("constraint_" . $question["question_id"]);
03983                                                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03984                                                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03985                                                 $qtiFieldEntryText = $domxml->create_text_node($constraint["question"] . "," . $constraint["short"] . "," . $constraint["value"]);
03986                                                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03987                                                 $qtiMetadatafield->append_child($qtiFieldLabel);
03988                                                 $qtiMetadatafield->append_child($qtiFieldEntry);
03989                                                 $qtiMetadata->append_child($qtiMetadatafield);
03990                                         }
03991                                 }
03992                         }
03993                 }
03994                 // add headings
03995                 foreach ($pages as $question_array)
03996                 {
03997                         foreach ($question_array as $question)
03998                         {
03999                                 if ($question["heading"])
04000                                 {
04001                                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
04002                                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
04003                                         $qtiFieldLabelText = $domxml->create_text_node("heading_" . $question["question_id"]);
04004                                         $qtiFieldLabel->append_child($qtiFieldLabelText);
04005                                         $qtiFieldEntry = $domxml->create_element("fieldentry");
04006                                         $qtiFieldEntryText = $domxml->create_text_node($question["heading"]);
04007                                         $qtiFieldEntry->append_child($qtiFieldEntryText);
04008                                         $qtiMetadatafield->append_child($qtiFieldLabel);
04009                                         $qtiMetadatafield->append_child($qtiFieldEntry);
04010                                         $qtiMetadata->append_child($qtiMetadatafield);
04011                                 }
04012                         }
04013                 }
04014                 $qtiSurvey->append_child($qtiMetadata);
04015                 $root->append_child($qtiSurvey);
04016                 $xml = $domxml->dump_mem(true);
04017                 $domxml->free();
04018                 $obligatory_states =& $this->getObligatoryStates();
04019                 foreach ($this->questions as $question_id) {
04020                         $question =& $this->_instanciateQuestion($question_id);
04021                         $qti_question = $question->to_xml(false, $obligatory_states[$question_id]);
04022                         $qti_question = preg_replace("/<questestinterop>/", "", $qti_question);
04023                         $qti_question = preg_replace("/<\/questestinterop>/", "", $qti_question);
04024                         $xml = str_replace("</questestinterop>", "$qti_question</questestinterop>", $xml);
04025                 }
04026                 return $xml;
04027                         }
04028         
04038   function &_instanciateQuestion($question_id) {
04039       $question_type = SurveyQuestion::_getQuestionType($question_id);
04040       switch ($question_type) {
04041                                 case "qt_nominal":
04042                                         $question = new SurveyNominalQuestion();
04043                                         break;
04044                                 case "qt_ordinal":
04045                                         $question = new SurveyOrdinalQuestion();
04046                                         break;
04047                                 case "qt_metric":
04048                                         $question = new SurveyMetricQuestion();
04049                                         break;
04050                                 case "qt_text":
04051                                         $question = new SurveyTextQuestion();
04052                                         break;
04053                         }
04054       $question->loadFromDb($question_id);
04055                         return $question;
04056   }
04057 
04066         function importObject($file_info, $survey_questionpool_id)
04067         {
04068                 // check if file was uploaded
04069                 $source = $file_info["tmp_name"];
04070                 $error = 0;
04071                 if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
04072                 {
04073                         $this->ilias->raiseError($this->lng->txt("import_no_file_selected"),$this->ilias->error_obj->MESSAGE);
04074                         $error = 1;
04075                 }
04076                 // check correct file type
04077                 if (!((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0)))
04078                 {
04079                         $this->ilias->raiseError($this->lng->txt("import_wrong_file_type"),$this->ilias->error_obj->MESSAGE);
04080                         $error = 1;
04081                 }
04082                 if (!$error)
04083                 {
04084                         // import file as a survey
04085                         $import_dir = $this->getImportDirectory();
04086                         $importfile = tempnam($import_dir, "survey_import");
04087                         //move_uploaded_file($source, $importfile);
04088                         ilUtil::moveUploadedFile($source, "survey_import", $importfile);
04089                         $fh = fopen($importfile, "r");
04090                         if (!$fh)
04091                         {
04092                                 $this->ilias->raiseError($this->lng->txt("import_error_opening_file"),$this->ilias->error_obj->MESSAGE);
04093                                 $error = 1;
04094                                 return $error;
04095                         }
04096                         $xml = fread($fh, filesize($importfile));
04097                         $result = fclose($fh);
04098 
04099                         // delete import directory
04100                         ilUtil::delDir($this->getImportDirectory());
04101         
04102                         if (!$result)
04103                         {
04104                                 $this->ilias->raiseError($this->lng->txt("import_error_closing_file"),$this->ilias->error_obj->MESSAGE);
04105                                 $error = 1;
04106                                 return $error;
04107                         }
04108                         if (preg_match("/(<survey[^>]*>.*?<\/survey>)/si", $xml, $matches))
04109                         {
04110                                 // read survey properties
04111                                 $import_results = $this->from_xml($matches[1]);
04112                                 if ($import_results === false)
04113                                 {
04114                                         $this->ilias->raiseError($this->lng->txt("import_error_survey_no_proper_values"),$this->ilias->error_obj->MESSAGE);
04115                                         $error = 1;
04116                                         return $error;
04117                                 }
04118                         }
04119                         else
04120                         {
04121                                 $this->ilias->raiseError($this->lng->txt("import_error_survey_no_properties"),$this->ilias->error_obj->MESSAGE);
04122                                 $error = 1;
04123                                 return $error;
04124                         }
04125                         $question_counter = 0;
04126                         $new_question_ids = array();
04127                         if (preg_match_all("/(<item[^>]*>.*?<\/item>)/si", $xml, $matches))
04128                         {
04129                                 foreach ($matches[1] as $index => $item)
04130                                 {
04131                                         $question = "";
04132                                         if (preg_match("/<qticomment>Questiontype\=(.*?)<\/qticomment>/is", $item, $questiontype))
04133                                         {
04134                                                 switch ($questiontype[1])
04135                                                 {
04136                                                         case NOMINAL_QUESTION_IDENTIFIER:
04137                                                                 $question = new SurveyNominalQuestion();
04138                                                                 break;
04139                                                         case ORDINAL_QUESTION_IDENTIFIER:
04140                                                                 $question = new SurveyOrdinalQuestion();
04141                                                                 break;
04142                                                         case METRIC_QUESTION_IDENTIFIER:
04143                                                                 $question = new SurveyMetricQuestion();
04144                                                                 break;
04145                                                         case TEXT_QUESTION_IDENTIFIER:
04146                                                                 $question = new SurveyTextQuestion();
04147                                                                 break;
04148                                                 }
04149                                                 if ($question)
04150                                                 {
04151                                                         $question->from_xml("<questestinterop>$item</questestinterop>");
04152                                                         if ($import_results !== false)
04153                                                         {
04154                                                                 $question->setObjId($survey_questionpool_id);
04155                                                                 $question->saveToDb();
04156                                                                 $question_id = $question->duplicate(true);
04157                                                                 $this->questions[$question_counter++] = $question_id;
04158                                                                 if (preg_match("/<item\s+ident\=\"(\d+)\"/", $item, $matches))
04159                                                                 {
04160                                                                         $original_question_id = $matches[1];
04161                                                                         $new_question_ids[$original_question_id] = $question_id;
04162                                                                 }
04163                                                         }
04164                                                         else
04165                                                         {
04166                                                                 $this->ilias->raiseError($this->lng->txt("error_importing_question"), $this->ilias->error_obj->MESSAGE);
04167                                                         }
04168                                                 }
04169                                         }
04170                                 }
04171                         }
04172 
04173                         $this->saveToDb();
04174                         // add question blocks
04175                         foreach ($import_results["questionblocks"] as $questionblock)
04176                         {
04177                                 foreach ($questionblock["questions"] as $key => $value)
04178                                 {
04179                                         $questionblock["questions"][$key] = $new_question_ids[$value];
04180                                 }
04181                                 $this->createQuestionblock($questionblock["title"], $questionblock["questions"]);
04182                         }
04183                         // add constraints
04184                         $relations = $this->getAllRelations(true);
04185                         foreach ($import_results["constraints"] as $constraint)
04186                         {
04187                                 $this->addConstraint($new_question_ids[$constraint["for"]], $new_question_ids[$constraint["question"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
04188                         }
04189                         foreach ($import_results["headings"] as $qid => $heading)
04190                         {
04191                                 $this->saveHeading($heading, $new_question_ids[$qid]);
04192                         }
04193                 }
04194                 return $error;
04195         }
04196 
04205         function from_xml($xml_text)
04206         {
04207                 $result = false;
04208                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
04209                 $domxml = domxml_open_mem($xml_text);
04210                 $constraints = array();
04211                 $headings = array();
04212                 $questionblocks = array();
04213                 if (!empty($domxml))
04214                 {
04215                         $root = $domxml->document_element();
04216                         $this->setTitle($root->get_attribute("title"));
04217                         $item = $root;
04218                         $itemnodes = $item->child_nodes();
04219                         foreach ($itemnodes as $index => $node)
04220                         {
04221                                 switch ($node->node_name())
04222                                 {
04223                                         case "qticomment":
04224                                                 $comment = $node->get_content();
04225                                                 if (strpos($comment, "ILIAS Version=") !== false)
04226                                                 {
04227                                                 }
04228                                                 elseif (strpos($comment, "Questiontype=") !== false)
04229                                                 {
04230                                                 }
04231                                                 elseif (strpos($comment, "Author=") !== false)
04232                                                 {
04233                                                         $comment = str_replace("Author=", "", $comment);
04234                                                         $this->setAuthor($comment);
04235                                                 }
04236                                                 else
04237                                                 {
04238                                                         $this->setDescription($comment);
04239                                                 }
04240                                                 break;
04241                                         case "objectives":
04242                                                 $material = $node->first_child();
04243                                                 if (strcmp($material->get_attribute("label"), "introduction") == 0)
04244                                                 {
04245                                                         $mattext = $material->first_child();
04246                                                         $this->setIntroduction($mattext->get_content());
04247                                                 }
04248                                                 break;
04249                                         case "qtimetadata":
04250                                                 $metadata_fields = $node->child_nodes();
04251                                                 foreach ($metadata_fields as $index => $metadata_field)
04252                                                 {
04253                                                         $fieldlabel = $metadata_field->first_child();
04254                                                         $fieldentry = $fieldlabel->next_sibling();
04255                                                         switch ($fieldlabel->get_content())
04256                                                         {
04257                                                                 case "evaluation_access":
04258                                                                         $this->setEvaluationAccess($fieldentry->get_content());
04259                                                                         break;
04260                                                                 case "author":
04261                                                                         $this->setAuthor($fieldentry->get_content());
04262                                                                         break;
04263                                                                 case "description":
04264                                                                         $this->setDescription($fieldentry->get_content());
04265                                                                         break;
04266                                                                 case "anonymize":
04267                                                                         $this->setAnonymize($fieldentry->get_content());
04268                                                                         break;
04269                                                                 case "startdate":
04270                                                                         $iso8601period = $fieldentry->get_content();
04271                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04272                                                                         {
04273                                                                                 $this->setStartDateEnabled(true);
04274                                                                                 $this->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04275                                                                         }
04276                                                                         break;
04277                                                                 case "enddate":
04278                                                                         $iso8601period = $fieldentry->get_content();
04279                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04280                                                                         {
04281                                                                                 $this->setEndDateEnabled(true);
04282                                                                                 $this->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04283                                                                         }
04284                                                                         break;
04285                                                                 case "status":
04286                                                                         $this->setStatus($fieldentry->get_content());
04287                                                                         break;
04288                                                                 case "display_question_titles":
04289                                                                         if ($fieldentry->get_content() == QUESTIONTITLES_HIDDEN)
04290                                                                         {
04291                                                                                 $this->hideQuestionTitles();
04292                                                                         }
04293                                                                         else
04294                                                                         {
04295                                                                                 $this->showQuestionTitles();
04296                                                                         }
04297                                                         }
04298                                                         if (preg_match("/questionblock_\d+/", $fieldlabel->get_content()))
04299                                                         {
04300                                                                 $qb = $fieldentry->get_content();
04301                                                                 preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
04302                                                                 $qb_title = $matches[1];
04303                                                                 preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
04304                                                                 $qb_questions = $matches[1];
04305                                                                 $qb_questions_array = explode(",", $qb_questions);
04306                                                                 array_push($questionblocks, array(
04307                                                                         "title" => $qb_title,
04308                                                                         "questions" => $qb_questions_array
04309                                                                 ));
04310                                                         }
04311                                                         if (preg_match("/constraint_(\d+)/", $fieldlabel->get_content(), $matches))
04312                                                         {
04313                                                                 $constraint = $fieldentry->get_content();
04314                                                                 $constraint_array = explode(",", $constraint);
04315                                                                 if (count($constraint_array) == 3)
04316                                                                 {
04317                                                                         array_push($constraints, array(
04318                                                                                 "for"      => $matches[1], 
04319                                                                                 "question" => $constraint_array[0],
04320                                                                                 "relation" => $constraint_array[1],
04321                                                                                 "value"    => $constraint_array[2]
04322                                                                         ));
04323                                                                 }
04324                                                         }
04325                                                         if (preg_match("/heading_(\d+)/", $fieldlabel->get_content(), $matches))
04326                                                         {
04327                                                                 $heading = $fieldentry->get_content();
04328                                                                 $headings[$matches[1]] = $heading;
04329                                                         }
04330                                                 }
04331                                                 break;
04332                                 }
04333                         }
04334                         $result["questionblocks"] = $questionblocks;
04335                         $result["constraints"] = $constraints;
04336                         $result["headings"] = $headings;
04337                 }
04338                 return $result;
04339         }
04340         
04344 /*
04345         function updateTitleAndDescription()
04346         {
04347                 $this->initMeta();
04348                 $this->meta_data->updateTitleAndDescription($this->getTitle(), $this->getDescription());
04349         }
04350 */
04351 
04355 /*
04356         function updateMetaData()
04357         {
04358                 $this->initMeta();
04359                 $this->meta_data->update();
04360                 if ($this->meta_data->section != "General")
04361                 {
04362                         $meta = $this->meta_data->getElement("Title", "General");
04363                         $this->meta_data->setTitle($meta[0]["value"]);
04364                         $meta = $this->meta_data->getElement("Description", "General");
04365                         $this->meta_data->setDescription($meta[0]["value"]);
04366                 }
04367                 else
04368                 {
04369                         $this->setTitle($this->meta_data->getTitle());
04370                         $this->setDescription($this->meta_data->getDescription());
04371                 }
04372                 parent::update();
04373         }
04374 */
04375 
04384         function &_getAvailableSurveys($use_object_id = false)
04385         {
04386                 global $rbacsystem;
04387                 global $ilDB;
04388                 
04389                 $result_array = array();
04390                 $query = "SELECT object_data.*, object_data.obj_id, object_reference.ref_id FROM object_data, object_reference WHERE object_data.obj_id = object_reference.obj_id AND object_data.type = 'svy' ORDER BY object_data.title";
04391                 $result = $ilDB->query($query);
04392                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
04393                 {               
04394                         if ($rbacsystem->checkAccess("write", $row->ref_id) && (ilObject::_hasUntrashedReference($row->obj_id)))
04395                         {
04396                                 if ($use_object_id)
04397                                 {
04398                                         $result_array[$row->obj_id] = $row->title;
04399                                 }
04400                                 else
04401                                 {
04402                                         $result_array[$row->ref_id] = $row->title;
04403                                 }
04404                         }
04405                 }
04406                 return $result_array;
04407         }
04408 
04416         function _clone($obj_id)
04417         {
04418                 global $ilDB;
04419                 
04420                 $original = new ilObjSurvey($obj_id, false);
04421                 $original->loadFromDb();
04422                 
04423                 $newObj = new ilObjSurvey();
04424                 $newObj->setType("svy");
04425                 $newObj->setTitle($original->getTitle());
04426                 $newObj->setDescription($original->getDescription());
04427                 $newObj->create(true);
04428                 $newObj->createReference();
04429                 $newObj->putInTree($_GET["ref_id"]);
04430                 $newObj->setPermissions($_GET["ref_id"]);
04431 //              $newObj->notify("new",$_GET["ref_id"],$_GET["parent_non_rbac_id"],$_GET["ref_id"],$newObj->getRefId());
04432                 
04433                 $newObj->author = $original->getAuthor();
04434                 $newObj->introduction = $original->getIntroduction();
04435                 $newObj->status = $original->getStatus();
04436                 $newObj->evaluation_access = $original->getEvaluationAccess();
04437                 $newObj->start_date = $original->getStartDate();
04438                 $newObj->startdate_enabled = $original->getStartDateEnabled();
04439                 $newObj->end_date = $original->getEndDate();
04440                 $newObj->enddate_enabled = $original->getEndDateEnabled();
04441                 $newObj->invitation = $original->getInvitation();
04442                 $newObj->invitation_mode = $original->getInvitationMode();
04443                 $newObj->anonymize = $original->getAnonymize();
04444 
04445                 $question_pointer = array();
04446                 // clone the questions
04447                 foreach ($original->questions as $key => $question_id)
04448                 {
04449                         $question = ilObjSurvey::_instanciateQuestion($question_id);
04450                         $question->id = -1;
04451                         $original_id = SurveyQuestion::_getOriginalId($question_id);
04452                         $question->saveToDb($original_id);
04453                         $newObj->questions[$key] = $question->getId();
04454                         $question_pointer[$question_id] = $question->getId();
04455                 }
04456 
04457                 $newObj->saveToDb();            
04458 
04459                 // clone the questionblocks
04460                 $questionblocks = array();
04461                 $questionblock_questions = array();
04462                 $query = sprintf("SELECT * FROM survey_questionblock_question WHERE survey_fi = %s",
04463                         $this->ilias->db->quote($original->getSurveyId() . "")
04464                 );
04465                 $result = $this->ilias->db->query($query);
04466                 if ($result->numRows() > 0)
04467                 {
04468                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04469                         {
04470                                 array_push($questionblock_questions, $row);
04471                                 $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
04472                         }
04473                 }
04474                 // create new questionblocks
04475                 foreach ($questionblocks as $key => $value)
04476                 {
04477                         $questionblock = ilObjSurvey::_getQuestionblock($key);
04478                         $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
04479                         $questionblocks[$key] = $questionblock_id;
04480                 }
04481                 // create new questionblock questions
04482                 foreach ($questionblock_questions as $key => $value)
04483                 {
04484                         $clonequery = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
04485                                 $ilDB->quote($newObj->getSurveyId() . ""),
04486                                 $ilDB->quote($questionblocks[$value["questionblock_fi"]] . ""),
04487                                 $ilDB->quote($question_pointer[$value["question_fi"]] . "")
04488                         );
04489                         $cloneresult = $this->ilias->db->query($clonequery);
04490                 }
04491                 
04492                 // clone the constraints
04493                 $constraints = ilObjSurvey::_getConstraints($original->getSurveyId());
04494                 foreach ($constraints as $key => $constraint)
04495                 {
04496                         $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
04497                 }
04498                 
04499                 // clone the obligatory states
04500                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
04501                         $this->ilias->db->quote($original->getSurveyId() . "")
04502                 );
04503                 $result = $this->ilias->db->query($query);
04504                 if ($result->numRows() > 0)
04505                 {
04506                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04507                         {
04508                                 $clonequery = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
04509                                         $this->ilias->db->quote($newObj->getSurveyId() . ""),
04510                                         $this->ilias->db->quote($question_pointer[$row["question_fi"]] . ""),
04511                                         $this->ilias->db->quote($row["obligatory"])
04512                                 );
04513                                 $cloneresult = $this->ilias->db->query($clonequery);
04514                         }
04515                 }
04516 
04517                 // clone meta data
04518                 $md = new ilMD($original->getId(),0,$original->getType());
04519                 $new_md =& $md->cloneMD($newObj->getId(),0,$newObj->getType());
04520         }
04521 
04527         function createExportDirectory()
04528         {
04529                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04530                 ilUtil::makeDir($svy_data_dir);
04531                 if(!is_writable($svy_data_dir))
04532                 {
04533                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04534                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04535                 }
04536                 
04537                 // create learning module directory (data_dir/lm_data/lm_<id>)
04538                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04539                 ilUtil::makeDir($svy_dir);
04540                 if(!@is_dir($svy_dir))
04541                 {
04542                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04543                 }
04544                 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
04545                 $export_dir = $svy_dir."/export";
04546                 ilUtil::makeDir($export_dir);
04547                 if(!@is_dir($export_dir))
04548                 {
04549                         $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
04550                 }
04551         }
04552 
04556         function getExportDirectory()
04557         {
04558                 $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
04559 
04560                 return $export_dir;
04561         }
04562         
04566         function getExportFiles($dir)
04567         {
04568                 // quit if import dir not available
04569                 if (!@is_dir($dir) or
04570                         !is_writeable($dir))
04571                 {
04572                         return array();
04573                 }
04574 
04575                 // open directory
04576                 $dir = dir($dir);
04577 
04578                 // initialize array
04579                 $file = array();
04580 
04581                 // get files and save the in the array
04582                 while ($entry = $dir->read())
04583                 {
04584                         if ($entry != "." and
04585                                 $entry != ".." and
04586                                 substr($entry, -4) == ".xml" and
04587                                 ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml\$", $entry))
04588                         {
04589                                 $file[] = $entry;
04590                         }
04591                 }
04592 
04593                 // close import directory
04594                 $dir->close();
04595                 // sort files
04596                 sort ($file);
04597                 reset ($file);
04598 
04599                 return $file;
04600         }
04601 
04607         function createImportDirectory()
04608         {
04609                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04610                 ilUtil::makeDir($svy_data_dir);
04611                 
04612                 if(!is_writable($svy_data_dir))
04613                 {
04614                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04615                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04616                 }
04617 
04618                 // create test directory (data_dir/svy_data/svy_<id>)
04619                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04620                 ilUtil::makeDir($svy_dir);
04621                 if(!@is_dir($svy_dir))
04622                 {
04623                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04624                 }
04625 
04626                 // create import subdirectory (data_dir/svy_data/svy_<id>/import)
04627                 $import_dir = $svy_dir."/import";
04628                 ilUtil::makeDir($import_dir);
04629                 if(!@is_dir($import_dir))
04630                 {
04631                         $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
04632                 }
04633         }
04634 
04638         function getImportDirectory()
04639         {
04640                 $import_dir = ilUtil::getDataDir()."/svy_data".
04641                         "/svy_".$this->getId()."/import";
04642                 if (!is_dir($import_dir))
04643                 {
04644                         ilUtil::makeDirParents($import_dir);
04645                 }
04646                 if(@is_dir($import_dir))
04647                 {
04648                         return $import_dir;
04649                 }
04650                 else
04651                 {
04652                         return false;
04653                 }
04654         }
04655         
04656         function saveHeading($heading = "", $insertbefore)
04657         {
04658                 if ($heading)
04659                 {
04660                         $query = sprintf("UPDATE survey_survey_question SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
04661                                 $this->ilias->db->quote($heading),
04662                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04663                                 $this->ilias->db->quote($insertbefore)
04664                         );
04665                 }
04666                 else
04667                 {
04668                         $query = sprintf("UPDATE survey_survey_question SET heading=NULL WHERE survey_fi=%s AND question_fi=%s",
04669                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04670                                 $this->ilias->db->quote($insertbefore)
04671                         );
04672                 }
04673                 $this->ilias->db->query($query);
04674         }
04675 
04676         function _getRefIdFromObjId($obj_id)
04677         {
04678                 global $ilDB;
04679                 
04680                 $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
04681                         $ilDB->quote($obj_id)
04682                         
04683                 );
04684                 $result = $ilDB->query($query);
04685                 if ($result->numRows())
04686                 {
04687                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04688                         return $row["ref_id"];
04689                 }
04690                 return 0;
04691         }
04692         
04693         function isAnonymousKey($key)
04694         {
04695                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_key = %s AND survey_fi = %s",
04696                         $this->ilias->db->quote($key . ""),
04697                         $this->ilias->db->quote($this->getSurveyId() . "")
04698                 );
04699                 $result = $this->ilias->db->query($query);
04700                 if ($result->numRows() == 1)
04701                 {
04702                         return true;
04703                 }
04704                 else
04705                 {
04706                         return false;
04707                 }
04708         }
04709         
04710         function getUserSurveyCode()
04711         {
04712                 global $ilUser;
04713                 return md5($ilUser->id . $this->getSurveyId());
04714         }
04715         
04716         function checkSurveyCode($code)
04717         {
04718                 global $ilUser;
04719                 // check for the correct survey code
04720                 if (strcmp($ilUser->login, "anonymous") != 0)
04721                 {
04722                         $anonymize_key = $this->getUserSurveyCode();
04723                         if (strcmp(strtolower($anonymize_key), strtolower($code)) == 0)
04724                         {
04725                                 return true;
04726                         }
04727                         else
04728                         {
04729                                 return false;
04730                         }
04731                 }
04732                 else
04733                 {
04734                         if ($this->isAnonymousKey($code))
04735                         {
04736                                 if ($this->isSurveyStarted("", $code) == 1)
04737                                 {
04738                                         return false;
04739                                 }
04740                                 else
04741                                 {
04742                                         return true;
04743                                 }
04744                         }
04745                         else
04746                         {
04747                                 return false;
04748                         }
04749                 }
04750                 return false;
04751         }
04752 
04753         function &getSurveyCodes()
04754         {
04755                 $codes = array();
04756                 $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 ORDER BY TIMESTAMP14",
04757                         $this->ilias->db->quote($this->getSurveyId() . "")
04758                 );
04759                 $result = $this->ilias->db->query($query);
04760                 
04761                 if ($result->numRows() > 0)
04762                 {
04763                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04764                         {
04765                                 array_push($codes, $row);
04766                         }
04767                 }
04768                 return $codes;
04769         }
04770         
04771         function isSurveyCodeUsed($code)
04772         {
04773                 $query = sprintf("SELECT answer_id FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s",
04774                         $this->ilias->db->quote($this->getSurveyId() . ""),
04775                         $this->ilias->db->quote($code)
04776                 );
04777                 $result = $this->ilias->db->query($query);
04778                 if ($result->numRows() > 0)
04779                 {
04780                         return TRUE;
04781                 }
04782                 else
04783                 {
04784                         return FALSE;
04785                 }
04786         }
04787         
04788         function createSurveyCodes($nrOfCodes)
04789         {
04790                 for ($i = 0; $i < $nrOfCodes; $i++)
04791                 {
04792                         $anonymize_key = md5((time() + ($i*$nrOfCodes)) . $this->getSurveyId());
04793                         $query = sprintf("INSERT INTO survey_anonymous (anonymous_id, survey_key, survey_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
04794                                 $this->ilias->db->quote($anonymize_key . ""),
04795                                 $this->ilias->db->quote($this->getSurveyId() . "")
04796                         );
04797                         $result = $this->ilias->db->query($query);
04798                 }
04799         }
04800         
04806         function getRandomSurveyCode()
04807         {
04808                 return md5(time() . $this->getSurveyId());
04809         }
04810         
04816         function _goto($a_target, $a_access_code = "")
04817         {
04818                 global $rbacsystem, $ilErr, $lng;
04819 
04820                 include_once 'classes/class.ilSearch.php';
04821                         
04822                 // Added this additional check (ParentConditions) to avoid calls of objects inside e.g courses.
04823                 // Will be replaced in future releases by ilAccess::checkAccess()
04824                 if ($rbacsystem->checkAccess("read", $a_target) and ilSearch::_checkParentConditions($a_target))
04825                 {
04826                         if (strlen($a_access_code))
04827                         {
04828                                 ilUtil::redirect("survey/survey.php?cmd=run&ref_id=$a_target&accesscode=$a_access_code");
04829                         }
04830                         else
04831                         {
04832                                 ilUtil::redirect("survey/survey.php?cmd=run&ref_id=$a_target");
04833                         }
04834                 }
04835                 else
04836                 {
04837                         $ilErr->raiseError($lng->txt("msg_no_perm_read_lm"), $ilErr->FATAL);
04838                 }
04839         }
04840 
04853         function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
04854         {
04855                 $resultarray = array();
04856                 foreach ($row as $rowindex => $entry)
04857                 {
04858                         $surround = FALSE;
04859                         if ($quoteAll)
04860                         {
04861                                 $surround = TRUE;
04862                         }
04863                         if (strpos($entry, "\"") !== FALSE)
04864                         {
04865                                 $entry = str_replace("\"", "\"\"", $entry);
04866                                 $surround = TRUE;
04867                         }
04868                         if (strpos($entry, $separator) !== FALSE)
04869                         {
04870                                 $surround = TRUE;
04871                         }
04872                         // replace all CR LF with LF (for Excel for Windows compatibility
04873                         $entry = str_replace(chr(13).chr(10), chr(10), $entry);
04874                         if ($surround)
04875                         {
04876                                 $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
04877                         }
04878                         else
04879                         {
04880                                 $resultarray[$rowindex] = utf8_decode($entry);
04881                         }
04882                 }
04883                 return $resultarray;
04884         }
04885 } // END class.ilObjSurvey
04886 ?>

Generated on Fri Dec 13 2013 10:18:33 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1