• 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                 if ($a_id == 0)
00206                 {
00207                         $new_meta =& new ilMetaData();
00208                         $this->assignMetaData($new_meta);
00209                 }
00210                 $this->survey_id = -1;
00211                 $this->introduction = "";
00212                 $this->author = $ilUser->fullname;
00213                 $this->status = STATUS_OFFLINE;
00214                 $this->evaluation_access = EVALUATION_ACCESS_OFF;
00215                 $this->startdate_enabled = 0;
00216                 $this->enddate_enabled = 0;
00217                 $this->questions = array();
00218                 $this->invitation = INVITATION_OFF;
00219                 $this->invitation_mode = MODE_PREDEFINED_USERS;
00220                 $this->anonymize = ANONYMIZE_OFF;
00221                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
00222         }
00223 
00227         function create($a_upload = false)
00228         {
00229                 parent::create();
00230                 if (!$a_upload)
00231                 {
00232                         $this->meta_data->setId($this->getId());
00233                         $this->meta_data->setType($this->getType());
00234                         $this->meta_data->setTitle($this->getTitle());
00235                         $this->meta_data->setDescription($this->getDescription());
00236                         $this->meta_data->setObject($this);
00237                         $this->meta_data->create();
00238                 }
00239         }
00240 
00247         function update()
00248         {
00249                 if (!parent::update())
00250                 {                       
00251                         return false;
00252                 }
00253 
00254                 // put here object specific stuff
00255                 
00256                 return true;
00257         }
00258         
00259         function createReference() {
00260                 $result = parent::createReference();
00261                 $this->saveToDb();
00262                 return $result;
00263         }
00264 
00270         function read($a_force_db = false)
00271         {
00272                 parent::read($a_force_db);
00273                 $this->loadFromDb();
00274                 $this->meta_data =& new ilMetaData($this->getType(), $this->getId());
00275         }
00276         
00284         function ilClone($a_parent_ref)
00285         {               
00286                 global $rbacadmin;
00287 
00288                 // always call parent ilClone function first!!
00289                 $new_ref_id = parent::ilClone($a_parent_ref);
00290                 
00291                 // get object instance of ilCloned object
00292                 //$newObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00293 
00294                 // create a local role folder & default roles
00295                 //$roles = $newObj->initDefaultRoles();
00296 
00297                 // ...finally assign role to creator of object
00298                 //$rbacadmin->assignUser($roles[0], $newObj->getOwner(), "n");          
00299 
00300                 // always destroy objects in ilClone method because ilClone() is recursive and creates instances for each object in subtree!
00301                 //unset($newObj);
00302 
00303                 // ... and finally always return new reference ID!!
00304                 return $new_ref_id;
00305         }
00306 
00313         function delete()
00314         {               
00315                 $remove = parent::delete();
00316                 // always call parent delete function first!!
00317                 if (!$remove)
00318                 {
00319                         return false;
00320                 }
00321                 
00322                 // Delete all survey questions, constraints and materials
00323                 foreach ($this->questions as $question_id)
00324                 {
00325                         $this->removeQuestion($question_id);
00326                 }
00327                 $this->deleteSurveyRecord();
00328                 
00329                 return true;
00330         }
00331         
00339         function deleteSurveyRecord()
00340         {
00341                 $query = sprintf("DELETE FROM survey_survey WHERE survey_id = %s",
00342                         $this->ilias->db->quote($this->getSurveyId())
00343                 );
00344                 $result = $this->ilias->db->query($query);
00345 
00346                 $query = sprintf("SELECT questionblock_fi FROM survey_questionblock_question WHERE survey_fi = %s",
00347                         $this->ilias->db->quote($this->getSurveyId())
00348                 );
00349                 $result = $this->ilias->db->query($query);
00350                 $questionblocks = array();
00351                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00352                 {
00353                         array_push($questionblocks, $row["questionblock_fi"]);
00354                 }
00355                 if (count($questionblocks))
00356                 {
00357                         $query = "DELETE FROM survey_questionblock WHERE questionblock_id IN (" . join($questionblocks, ",") . ")";
00358                         $result = $this->ilias->db->query($query);
00359                 }
00360                 $query = sprintf("DELETE FROM survey_questionblock_question WHERE survey_fi = %s",
00361                         $this->ilias->db->quote($this->getSurveyId())
00362                 );
00363                 $result = $this->ilias->db->query($query);
00364                 
00365                 $this->deleteAllUserData();
00366 
00367                 // delete export files
00368                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
00369                 $directory = $svy_data_dir."/svy_".$this->getId();
00370                 if (is_dir($directory))
00371                 {
00372                         $directory = escapeshellarg($directory);
00373                         exec("rm -rf $directory");
00374                 }
00375         }
00376         
00384         function deleteAllUserData()
00385         {
00386                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
00387                         $this->ilias->db->quote($this->getSurveyId())
00388                 );
00389                 $result = $this->ilias->db->query($query);
00390                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00391                 {
00392                         $this->disinviteUser($row["user_fi"]);
00393                 }
00394 
00395                 $query = sprintf("SELECT group_fi FROM survey_invited_group 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->disinviteGroup($row["group_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 
00900         function initMeta()
00901         {
00902                 if (!is_object($this->meta_data))
00903                 {
00904                         if ($this->getId())
00905                         {
00906                                 $new_meta =& new ilMetaData($this->getType(), $this->getId());
00907                         }       
00908                         else
00909                         {
00910                                 $new_meta =& new ilMetaData();
00911                         }
00912                         $this->assignMetaData($new_meta);
00913                 }
00914         }
00915 
00923   function loadFromDb()
00924   {
00925     $query = sprintf("SELECT * FROM survey_survey WHERE obj_fi = %s",
00926       $this->ilias->db->quote($this->getId())
00927     );
00928     $result = $this->ilias->db->query($query);
00929     if (strcmp(strtolower(get_class($result)), db_result) == 0) {
00930       if ($result->numRows() == 1) {
00931         $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00932                                 $this->survey_id = $data->survey_id;
00933         $this->author = $data->author;
00934         $this->introduction = $data->introduction;
00935         $this->status = $data->status;
00936                                 $this->invitation = $data->invitation;
00937                                 $this->invitation_mode = $data->invitation_mode;
00938                                 $this->display_question_titles = $data->show_question_titles;
00939         $this->start_date = $data->startdate;
00940                                 if (!$data->startdate)
00941                                 {
00942                                         $this->startdate_enabled = 0;
00943                                 }
00944                                 else
00945                                 {
00946                                         $this->startdate_enabled = 1;
00947                                 }
00948         $this->end_date = $data->enddate;
00949                                 if (!$data->enddate)
00950                                 {
00951                                         $this->enddate_enabled = 0;
00952                                 }
00953                                 else
00954                                 {
00955                                         $this->enddate_enabled = 1;
00956                                 }
00957                                 if (!$data->anonymize)
00958                                 {
00959                                         $this->setAnonymize(ANONYMIZE_OFF);
00960                                 }
00961                                 else
00962                                 {
00963                                         $this->setAnonymize(ANONYMIZE_ON);
00964                                 }
00965         $this->evaluation_access = $data->evaluation_access;
00966                                 $this->loadQuestionsFromDb();
00967       }
00968     }
00969         }
00970 
00979         function loadQuestionsFromDb() {
00980                 $this->questions = array();
00981                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
00982                         $this->ilias->db->quote($this->survey_id)
00983                 );
00984                 $result = $this->ilias->db->query($query);
00985                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
00986                         $this->questions[$data->sequence] = $data->question_fi;
00987                 }
00988         }
00989 
00999         function setStartDateEnabled($enabled = false)
01000         {
01001                 if ($enabled)
01002                 {
01003                         $this->startdate_enabled = 1;
01004                 }
01005                 else
01006                 {
01007                         $this->startdate_enabled = 0;
01008                 }
01009         }
01010         
01020         function getStartDateEnabled()
01021         {
01022                 return $this->startdate_enabled;
01023         }
01024 
01034         function setEndDateEnabled($enabled = false)
01035         {
01036                 if ($enabled)
01037                 {
01038                         $this->enddate_enabled = 1;
01039                 }
01040                 else
01041                 {
01042                         $this->enddate_enabled = 0;
01043                 }
01044         }
01045         
01055         function getEndDateEnabled()
01056         {
01057                 return $this->enddate_enabled;
01058         }
01059 
01065         function assignMetaData(&$a_meta_data)
01066         {
01067                 $this->meta_data =& $a_meta_data;
01068         }
01069 
01075         function &getMetaData()
01076         {
01077                 return $this->meta_data;
01078         }
01079 
01089   function setAuthor($author = "") {
01090     if (!$author) {
01091       $author = $this->ilias->account->fullname;
01092     }
01093     $this->author = $author;
01094   }
01095 
01105   function getShowQuestionTitles() {
01106                 return $this->display_question_titles;
01107   }
01108 
01117   function showQuestionTitles() {
01118                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
01119   }
01120 
01129   function hideQuestionTitles() {
01130                 $this->display_question_titles = QUESTIONTITLES_HIDDEN;
01131   }
01132         
01142   function setInvitation($invitation = 0) {
01143     $this->invitation = $invitation;
01144                 // remove the survey from the personal desktops
01145                 $query = sprintf("DELETE FROM desktop_item WHERE type = %s AND item_id = %s",
01146                         $this->ilias->db->quote("svy"),
01147                         $this->ilias->db->quote($this->getRefId())
01148                 );
01149                 $result = $this->ilias->db->query($query);
01150                 if ($invitation == INVITATION_OFF)
01151                 {
01152                         // already removed prior
01153                 }
01154                 else if ($invitation == INVITATION_ON)
01155                 {
01156                         if ($this->getInvitationMode() == MODE_UNLIMITED)
01157                         {
01158                                 $query = "SELECT usr_id FROM usr_data";
01159                                 $result = $this->ilias->db->query($query);
01160                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01161                                 {
01162                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01163                                                 $this->ilias->db->quote($row["usr_id"]),
01164                                                 $this->ilias->db->quote($this->getRefId()),
01165                                                 $this->ilias->db->quote("svy")
01166                                         );
01167                                         $insertresult = $this->ilias->db->query($query);
01168                                 }
01169                         }
01170                         else
01171                         {
01172                                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
01173                                         $this->ilias->db->quote($this->getSurveyId())
01174                                 );
01175                                 $result = $this->ilias->db->query($query);
01176                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01177                                 {
01178                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01179                                                 $this->ilias->db->quote($row["user_fi"]),
01180                                                 $this->ilias->db->quote($this->getRefId()),
01181                                                 $this->ilias->db->quote("svy")
01182                                         );
01183                                         $insertresult = $this->ilias->db->query($query);
01184                                 }
01185                                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
01186                                         $this->ilias->db->quote($this->getSurveyId())
01187                                 );
01188                                 $result = $this->ilias->db->query($query);
01189                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01190                                 {
01191                                         $group = new ilObjGroup($row["group_fi"]);
01192                                         $members = $group->getGroupMemberIds();
01193                                         foreach ($members as $user_id)
01194                                         {
01195                                                 $user = new ilObjUser($user_id);
01196                                                 $user->addDesktopItem($this->getRefId(), "svy");
01197                                         }
01198                                 }
01199                         }
01200                 }
01201   }
01202 
01212   function setInvitationMode($invitation_mode = 0) {
01213     $this->invitation_mode = $invitation_mode;
01214                 if ($invitation_mode == MODE_UNLIMITED)
01215                 {
01216                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01217                                 $this->ilias->db->quote($this->getSurveyId())
01218                         );
01219                         $result = $this->ilias->db->query($query);
01220                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01221                                 $this->ilias->db->quote($this->getSurveyId())
01222                         );
01223                         $result = $this->ilias->db->query($query);
01224                 }
01225                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01226                 $this->setInvitation($this->getInvitation());
01227   }
01228         
01239         function setInvitationAndMode($invitation = 0, $invitation_mode = 0)
01240         {
01241     $this->invitation_mode = $invitation_mode;
01242                 if ($invitation_mode == MODE_UNLIMITED)
01243                 {
01244                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01245                                 $this->ilias->db->quote($this->getSurveyId())
01246                         );
01247                         $result = $this->ilias->db->query($query);
01248                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01249                                 $this->ilias->db->quote($this->getSurveyId())
01250                         );
01251                         $result = $this->ilias->db->query($query);
01252                 }
01253                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01254                 $this->setInvitation($invitation);
01255         }
01256 
01266   function setIntroduction($introduction = "") {
01267     $this->introduction = $introduction;
01268   }
01269 
01279   function getAuthor() {
01280     return $this->author;
01281   }
01282 
01292   function getInvitation() {
01293     return $this->invitation;
01294   }
01295 
01305   function getInvitationMode() {
01306     return $this->invitation_mode;
01307   }
01308 
01318   function getStatus() {
01319     return $this->status;
01320   }
01321 
01331   function isOnline() 
01332         {
01333     if ($this->status == STATUS_ONLINE)
01334                 {
01335                         return true;
01336                 }
01337                 else
01338                 {
01339                         return false;
01340                 }
01341   }
01342 
01352   function isOffline() 
01353         {
01354     if ($this->status == STATUS_OFFLINE)
01355                 {
01356                         return true;
01357                 }
01358                 else
01359                 {
01360                         return false;
01361                 }
01362   }
01363 
01374   function setStatus($status = STATUS_OFFLINE) {
01375                 $result = "";
01376                 if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
01377                 {
01378         $this->status = STATUS_OFFLINE;
01379                         $result = $this->lng->txt("cannot_switch_to_online_no_questions");
01380                 }
01381                 else
01382                 {
01383         $this->status = $status;
01384                 }
01385                 return $result;
01386   }
01387 
01397   function getStartDate() {
01398     return $this->start_date;
01399   }
01400 
01409         function canStartSurvey()
01410         {
01411                 $result = 0;
01412                 if ($this->getStartDateEnabled())
01413                 {
01414                         $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
01415                         $now = mktime();
01416                         if ($now < $epoch_time) {
01417                                 $result = SURVEY_START_START_DATE_NOT_REACHED;
01418                         }
01419                 }
01420                 if ($this->getEndDateEnabled())
01421                 {
01422                         $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
01423                         $now = mktime();
01424                         if ($now > $epoch_time) {
01425                                 $result = SURVEY_START_END_DATE_REACHED;
01426                         }
01427                 }
01428                 if ($this->getStatus() == STATUS_OFFLINE)
01429                 {
01430                         $result = SURVEY_START_OFFLINE;
01431                 }
01432                 return $result;
01433         }
01434 
01435 
01445   function setStartDate($start_date = "") {
01446     $this->start_date = $start_date;
01447   }
01448 
01458   function getStartMonth() {
01459                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01460                 {
01461                         return $matches[2];
01462                 }
01463                 else
01464                 {
01465                         return "";
01466                 }
01467   }
01468 
01478   function getStartDay() {
01479                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01480                 {
01481                         return $matches[3];
01482                 }
01483                 else
01484                 {
01485                         return "";
01486                 }
01487   }
01488 
01498   function getStartYear() {
01499                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01500                 {
01501                         return $matches[1];
01502                 }
01503                 else
01504                 {
01505                         return "";
01506                 }
01507   }
01508 
01518   function getEndDate() {
01519     return $this->end_date;
01520   }
01521 
01531   function setEndDate($end_date = "") {
01532     $this->end_date = $end_date;
01533   }
01534 
01544   function getEndMonth() {
01545                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01546                 {
01547                         return $matches[2];
01548                 }
01549                 else
01550                 {
01551                         return "";
01552                 }
01553   }
01554 
01564   function getEndDay() {
01565                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01566                 {
01567                         return $matches[3];
01568                 }
01569                 else
01570                 {
01571                         return "";
01572                 }
01573   }
01574 
01584   function getEndYear() {
01585                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01586                 {
01587                         return $matches[1];
01588                 }
01589                 else
01590                 {
01591                         return "";
01592                 }
01593   }
01594 
01604   function getEvaluationAccess() {
01605     return $this->evaluation_access;
01606   }
01607 
01617   function setEvaluationAccess($evaluation_access = EVALUATION_ACCESS_OFF) {
01618     $this->evaluation_access = $evaluation_access;
01619   }
01620 
01630   function getIntroduction() {
01631     return $this->introduction;
01632   }
01633 
01642         function &getExistingQuestions() {
01643                 $existing_questions = array();
01644                 $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",
01645                         $this->ilias->db->quote($this->getSurveyId())
01646                 );
01647                 $result = $this->ilias->db->query($query);
01648                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
01649                         array_push($existing_questions, $data->original_id);
01650                 }
01651                 return $existing_questions;
01652         }
01653 
01662         function &getQuestionpoolTitles() {
01663                 global $rbacsystem;
01664                 
01665                 $qpl_titles = array();
01666                 // get all available questionpools and remove the trashed questionspools
01667                 $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'";
01668                 $result = $this->ilias->db->query($query);
01669                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01670                 {               
01671                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
01672                         {
01673                                 $qpl_titles["$row->obj_id"] = $row->title;
01674                         }
01675                 }
01676                 return $qpl_titles;
01677         }
01678         
01687         function moveUpQuestion($question_id)
01688         {
01689                 $move_questions = array($question_id);
01690                 $pages =& $this->getSurveyPages();
01691                 $pageindex = -1;
01692                 foreach ($pages as $idx => $page)
01693                 {
01694                         if ($page[0]["question_id"] == $question_id)
01695                         {
01696                                 $pageindex = $idx;
01697                         }
01698                 }
01699                 if ($pageindex > 0)
01700                 {
01701                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01702                 }
01703                 else
01704                 {
01705                         // move up a question in a questionblock
01706                         $questions = $this->getSurveyQuestions();
01707                         $questions = array_keys($questions);
01708                         $index = array_search($question_id, $questions);
01709                         if (($index !== FALSE) && ($index > 0))
01710                         {
01711                                 $this->moveQuestions($move_questions, $questions[$index-1], 0);
01712                         }
01713                 }
01714         }
01715         
01724         function moveDownQuestion($question_id)
01725         {
01726                 $move_questions = array($question_id);
01727                 $pages =& $this->getSurveyPages();
01728                 $pageindex = -1;
01729                 foreach ($pages as $idx => $page)
01730                 {
01731                         if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
01732                         {
01733                                 $pageindex = $idx;
01734                         }
01735                 }
01736                 if (($pageindex < count($pages)-1) && ($pageindex >= 0))
01737                 {
01738                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01739                 }
01740                 else
01741                 {
01742                         // move down a question in a questionblock
01743                         $questions = $this->getSurveyQuestions();
01744                         $questions = array_keys($questions);
01745                         $index = array_search($question_id, $questions);
01746                         if (($index !== FALSE) && ($index < count($questions)-1))
01747                         {
01748                                 $this->moveQuestions($move_questions, $questions[$index+1], 1);
01749                         }
01750                 }
01751         }
01752         
01761         function moveUpQuestionblock($questionblock_id)
01762         {
01763                 $pages =& $this->getSurveyPages();
01764                 $move_questions = array();
01765                 $pageindex = -1;
01766                 foreach ($pages as $idx => $page)
01767                 {
01768                         if ($page[0]["questionblock_id"] == $questionblock_id)
01769                         {
01770                                 foreach ($page as $pageidx => $question)
01771                                 {
01772                                         array_push($move_questions, $question["question_id"]);
01773                                 }
01774                                 $pageindex = $idx;
01775                         }
01776                 }
01777                 if ($pageindex > 0)
01778                 {
01779                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01780                 }
01781         }
01782         
01791         function moveDownQuestionblock($questionblock_id)
01792         {
01793                 $pages =& $this->getSurveyPages();
01794                 $move_questions = array();
01795                 $pageindex = -1;
01796                 foreach ($pages as $idx => $page)
01797                 {
01798                         if ($page[0]["questionblock_id"] == $questionblock_id)
01799                         {
01800                                 foreach ($page as $pageidx => $question)
01801                                 {
01802                                         array_push($move_questions, $question["question_id"]);
01803                                 }
01804                                 $pageindex = $idx;
01805                         }
01806                 }
01807                 if ($pageindex < count($pages)-1)
01808                 {
01809                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01810                 }
01811         }
01812         
01823         function moveQuestions($move_questions, $target_index, $insert_mode)
01824         {
01825                 $array_pos = array_search($target_index, $this->questions);
01826                 if ($insert_mode == 0)
01827                 {
01828                         $part1 = array_slice($this->questions, 0, $array_pos);
01829                         $part2 = array_slice($this->questions, $array_pos);
01830                 }
01831                 else if ($insert_mode == 1)
01832                 {
01833                         $part1 = array_slice($this->questions, 0, $array_pos + 1);
01834                         $part2 = array_slice($this->questions, $array_pos + 1);
01835                 }
01836                 foreach ($move_questions as $question_id)
01837                 {
01838                         if (!(array_search($question_id, $part1) === FALSE))
01839                         {
01840                                 unset($part1[array_search($question_id, $part1)]);
01841                         }
01842                         if (!(array_search($question_id, $part2) === FALSE))
01843                         {
01844                                 unset($part2[array_search($question_id, $part2)]);
01845                         }
01846                 }
01847                 $part1 = array_values($part1);
01848                 $part2 = array_values($part2);
01849                 $this->questions = array_values(array_merge($part1, $move_questions, $part2));
01850                 foreach ($move_questions as $question_id)
01851                 {
01852                         $constraints = $this->getConstraints($question_id);
01853                         foreach ($constraints as $idx => $constraint)
01854                         {
01855                                 foreach ($part2 as $next_question_id)
01856                                 {
01857                                         if ($constraint["question"] == $next_question_id)
01858                                         {
01859                                                 // constraint concerning a question that follows -> delete constraint
01860                                                 $this->deleteConstraint($constraint["id"], $question_id);
01861                                         }
01862                                 }
01863                         }
01864                 }
01865                 $this->saveQuestionsToDb();
01866         }
01867         
01876         function removeQuestion($question_id)
01877         {
01878                 $question = new SurveyQuestion();
01879                 $question->delete($question_id);
01880                 $this->removeConstraintsConcerningQuestion($question_id);
01881         }
01882         
01891         function removeConstraintsConcerningQuestion($question_id)
01892         {
01893                 $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01894                         $this->ilias->db->quote($question_id . ""),
01895                         $this->ilias->db->quote($this->getSurveyId() . "")
01896                 );
01897                 $result = $this->ilias->db->query($query);
01898                 if ($result->numRows() > 0)
01899                 {
01900                         $remove_constraints = array();
01901                         while ($row = $result->fetchRow(DB_FETCHMODE_HASHREF))
01902                         {
01903                                 array_push($remove_constraints, $row["constraint_fi"]);
01904                         }
01905                         $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01906                                 $this->ilias->db->quote($question_id . ""),
01907                                 $this->ilias->db->quote($this->getSurveyId() . "")
01908                         );
01909                         $result = $this->ilias->db->query($query);
01910                         foreach ($remove_constraints as $key => $constraint_id)
01911                         {
01912                                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
01913                                         $this->ilias->db->quote($constraint_id . "")
01914                                 );
01915                                 $result = $this->ilias->db->query($query);
01916                         }
01917                 }
01918         }
01919                 
01929         function removeQuestions($remove_questions, $remove_questionblocks)
01930         {
01931                 $questions =& $this->getSurveyQuestions();
01932                 foreach ($questions as $question_id => $data)
01933                 {
01934                         if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
01935                         {
01936                                 unset($this->questions[array_search($question_id, $this->questions)]);
01937                                 $this->removeQuestion($question_id);
01938                         }
01939                 }
01940                 foreach ($remove_questionblocks as $questionblock_id)
01941                 {
01942                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01943                                 $this->ilias->db->quote($questionblock_id)
01944                         );
01945                         $result = $this->ilias->db->query($query);
01946                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01947                                 $this->ilias->db->quote($questionblock_id),
01948                                 $this->ilias->db->quote($this->getSurveyId())
01949                         );
01950                         $result = $this->ilias->db->query($query);
01951                 }
01952                 $this->questions = array_values($this->questions);
01953                 $this->saveQuestionsToDb();
01954         }
01955                 
01964         function unfoldQuestionblocks($questionblocks)
01965         {
01966                 foreach ($questionblocks as $index)
01967                 {
01968                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01969                                 $this->ilias->db->quote($index)
01970                         );
01971                         $result = $this->ilias->db->query($query);
01972                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01973                                 $this->ilias->db->quote($index),
01974                                 $this->ilias->db->quote($this->getSurveyId())
01975                         );
01976                         $result = $this->ilias->db->query($query);
01977                 }
01978         }
01979         
01988         function &getQuestionblockTitles()
01989         {
01990                 $titles = array();
01991                 $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",
01992                         $this->ilias->db->quote($this->getId())
01993                 );
01994                 $result = $this->ilias->db->query($query);
01995                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01996                 {
01997                         $titles[$row->questionblock_id] = $row->title;
01998                 }
01999                 return $titles;
02000         }
02001         
02010         function &getQuestionblockQuestions($questionblock_id)
02011         {
02012                 $titles = array();
02013                 $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",
02014                         $this->ilias->db->quote($questionblock_id)
02015                 );
02016                 $result = $this->ilias->db->query($query);
02017                 $survey_id = "";
02018                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02019                 {
02020                         $titles[$row["question_fi"]] = $row["title"];
02021                         $survey_id = $row["survey_fi"];
02022                 }
02023                 $query = sprintf("SELECT question_fi, sequence FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
02024                         $this->ilias->db->quote($survey_id . "")
02025                 );
02026                 $result = $this->ilias->db->query($query);
02027                 $resultarray = array();
02028                 $counter = 1;
02029                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02030                 {
02031                         if (array_key_exists($row["question_fi"], $titles))
02032                         {
02033                                 $resultarray[$counter++] = $titles[$row["question_fi"]];
02034                         }
02035                 }
02036                 return $resultarray;
02037         }
02038         
02047         function &getQuestionblockQuestionIds($questionblock_id)
02048         {
02049                 $ids = array();
02050                 $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",
02051                         $this->ilias->db->quote($this->getId()),
02052                         $this->ilias->db->quote($questionblock_id)
02053                 );
02054                 $result = $this->ilias->db->query($query);
02055                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02056                 {
02057                         array_push($ids, $row->question_id);
02058                 }
02059                 return $ids;
02060         }
02061         
02071         function getQuestionblock($questionblock_id)
02072         {
02073                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02074                         $this->ilias->db->quote($questionblock_id)
02075                 );
02076                 $result = $this->ilias->db->query($query);
02077                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02078                 return $row;
02079         }
02080         
02090         function _getQuestionblock($questionblock_id)
02091         {
02092                 global $ilDB;
02093                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02094                         $ilDB->quote($questionblock_id)
02095                 );
02096                 $result = $ilDB->query($query);
02097                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02098                 return $row;
02099         }
02100 
02111         function _addQuestionblock($title = "", $owner = 0)
02112         {
02113                 global $ilDB;
02114                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02115                         $ilDB->quote($title . ""),
02116                         $ilDB->quote($owner . "")
02117                 );
02118                 $result = $ilDB->query($query);
02119                 return $ilDB->getLastInsertId();
02120         }
02121         
02131         function createQuestionblock($title, $questions)
02132         {
02133                 // if the selected questions are not in a continous selection, move all questions of the
02134                 // questionblock at the position of the first selected question
02135                 $this->moveQuestions($questions, $questions[0], 0);
02136                 
02137                 // now save the question block
02138                 global $ilUser;
02139                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02140                         $this->ilias->db->quote($title),
02141                         $this->ilias->db->quote($ilUser->id)
02142                 );
02143                 $result = $this->ilias->db->query($query);
02144                 if ($result == DB_OK) {
02145                         $questionblock_id = $this->ilias->db->getLastInsertId();
02146                         foreach ($questions as $index)
02147                         {
02148                                 $query = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
02149                                         $this->ilias->db->quote($this->getSurveyId()),
02150                                         $this->ilias->db->quote($questionblock_id),
02151                                         $this->ilias->db->quote($index)
02152                                 );
02153                                 $result = $this->ilias->db->query($query);
02154                                 $this->deleteConstraints($index);
02155                         }
02156                 }
02157         }
02158         
02168         function modifyQuestionblock($questionblock_id, $title)
02169         {
02170                 $query = sprintf("UPDATE survey_questionblock SET title = %s WHERE questionblock_id = %s",
02171                         $this->ilias->db->quote($title),
02172                         $this->ilias->db->quote($questionblock_id)
02173                 );
02174                 $result = $this->ilias->db->query($query);
02175         }
02176         
02185         function deleteConstraints($question_id)
02186         {
02187                 $query = sprintf("SELECT * FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02188                         $this->ilias->db->quote($question_id),
02189                         $this->ilias->db->quote($this->getSurveyId())
02190                 );
02191                 $result = $this->ilias->db->query($query);
02192                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02193                 {
02194                         $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02195                                 $this->ilias->db->quote($row->constraint_fi)
02196                         );
02197                         $delresult = $this->ilias->db->query($query);
02198                 }
02199                 $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02200                         $this->ilias->db->quote($question_id),
02201                         $this->ilias->db->quote($this->getSurveyId())
02202                 );
02203                 $delresult = $this->ilias->db->query($query);
02204         }
02205 
02215         function deleteConstraint($constraint_id, $question_id)
02216         {
02217                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02218                         $this->ilias->db->quote($constraint_id)
02219                 );
02220                 $delresult = $this->ilias->db->query($query);
02221                 $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s AND question_fi = %s AND survey_fi = %s",
02222                         $this->ilias->db->quote($constraint_id),
02223                         $this->ilias->db->quote($question_id),
02224                         $this->ilias->db->quote($this->getSurveyId())
02225                 );
02226                 $delresult = $this->ilias->db->query($query);
02227         }
02228 
02236         function &getSurveyQuestions($with_answers = false)
02237         {
02238                 $obligatory_states =& $this->getObligatoryStates();
02239                 // get questionblocks
02240                 $all_questions = array();
02241                 $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",
02242                         $this->ilias->db->quote($this->getSurveyId())
02243                 );
02244                 $result = $this->ilias->db->query($query);
02245                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02246                 {
02247                         $all_questions[$row["question_id"]] = $row;
02248                         if (array_key_exists($row["question_id"], $obligatory_states))
02249                         {
02250                                 $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
02251                         }
02252                 }
02253                 // get all questionblocks
02254                 $questionblocks = array();
02255                 $in = join(array_keys($all_questions), ",");
02256                 if ($in)
02257                 {
02258                         $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)",
02259                                 $this->ilias->db->quote($this->getSurveyId())
02260                         );
02261                         $result = $this->ilias->db->query($query);
02262                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02263                         {
02264                                 $questionblocks[$row->question_fi] = $row;
02265                         }                       
02266                 }
02267                 
02268                 foreach ($all_questions as $question_id => $row)
02269                 {
02270                         $constraints = $this->getConstraints($question_id);
02271                         if (isset($questionblocks[$question_id]))
02272                         {
02273                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02274                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02275                                 $all_questions[$question_id]["constraints"] = $constraints;
02276                         }
02277                         else
02278                         {
02279                                 $all_questions[$question_id]["questionblock_title"] = "";
02280                                 $all_questions[$question_id]["questionblock_id"] = "";
02281                                 $all_questions[$question_id]["constraints"] = $constraints;
02282                         }
02283                         if ($with_answers)
02284                         {
02285                                 $answers = array();
02286                                 $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",
02287                                         $this->ilias->db->quote($question_id . "")
02288                                 );
02289                                 $result = $this->ilias->db->query($query);
02290                                 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
02291                                         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
02292                                                 array_push($answers, $data->title);
02293                                         }
02294                                 }
02295                                 $all_questions[$question_id]["answers"] = $answers;                             
02296                         }
02297                 }
02298                 return $all_questions;
02299         }
02300         
02309         function &getQuestiontypes()
02310         {
02311                 $query = "SELECT type_tag FROM survey_questiontype";
02312                 $result = $this->ilias->db->query($query);
02313                 $result_array = array();
02314                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02315                 {
02316                         array_push($result_array, $row->type_tag);
02317                 }
02318                 return $result_array;
02319         }
02320 
02329         function setObligatoryStates($obligatory_questions)
02330         {
02331                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
02332                         $this->ilias->db->quote($this->getSurveyId() . "")
02333                 );
02334                 $result = $this->ilias->db->query($query);
02335                 if ($result->numRows())
02336                 {
02337                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02338                         {
02339                                 if (!array_key_exists($row["question_fi"], $obligatory_questions))
02340                                 {
02341                                         $obligatory_questions[$row["question_fi"]] = 0;
02342                                 }
02343                         }
02344                 }
02345 
02346           // set the obligatory states in the database
02347                 $query = sprintf("DELETE FROM survey_question_obligatory WHERE survey_fi = %s",
02348                         $this->ilias->db->quote($this->getSurveyId() . "")
02349                 );
02350                 $result = $this->ilias->db->query($query);
02351 
02352           // set the obligatory states in the database
02353                 foreach ($obligatory_questions as $question_fi => $obligatory)
02354                 {
02355                         $query = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02356                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02357                                 $this->ilias->db->quote($question_fi . ""),
02358                                 $this->ilias->db->quote($obligatory . "")
02359                         );
02360                         $result = $this->ilias->db->query($query);
02361                 }
02362         }
02363         
02372         function &getObligatoryStates()
02373         {
02374                 $obligatory_states = array();
02375                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
02376                         $this->ilias->db->quote($this->getSurveyId() . "")
02377                 );
02378                 $result = $this->ilias->db->query($query);
02379                 if ($result->numRows())
02380                 {
02381                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02382                         {
02383                                 $obligatory_states[$row["question_fi"]] = $row["obligatory"];
02384                         }
02385                 }
02386                 return $obligatory_states;
02387         }
02388         
02396         function &getSurveyPages()
02397         {
02398                 $obligatory_states =& $this->getObligatoryStates();
02399                 // get questionblocks
02400                 $all_questions = array();
02401                 $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",
02402                         $this->ilias->db->quote($this->getSurveyId())
02403                 );
02404                 $result = $this->ilias->db->query($query);
02405                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02406                 {
02407                         $all_questions[$row["question_id"]] = $row;
02408                 }
02409                 // get all questionblocks
02410                 $questionblocks = array();
02411                 $in = join(array_keys($all_questions), ",");
02412                 if ($in)
02413                 {
02414                         $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)",
02415                                 $this->ilias->db->quote($this->getSurveyId())
02416                         );
02417                         $result = $this->ilias->db->query($query);
02418                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02419                         {
02420                                 $questionblocks["$row->question_fi"] = $row;
02421                         }                       
02422                 }
02423                 
02424                 $all_pages = array();
02425                 $pageindex = -1;
02426                 $currentblock = "";
02427                 foreach ($all_questions as $question_id => $row)
02428                 {
02429                         if (array_key_exists($question_id, $obligatory_states))
02430                         {
02431                                 $all_questions["$question_id"]["obligatory"] = $obligatory_states["$question_id"];
02432                         }
02433                         $constraints = array();
02434                         if (isset($questionblocks[$question_id]))
02435                         {
02436                                 if (!$currentblock or ($currentblock != $questionblocks[$question_id]->questionblock_id))
02437                                 {
02438                                         $pageindex++;
02439                                 }
02440                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02441                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02442                                 $currentblock = $questionblocks[$question_id]->questionblock_id;
02443                                 $constraints = $this->getConstraints($question_id);
02444                                 $all_questions[$question_id]["constraints"] = $constraints;
02445                         }
02446                         else
02447                         {
02448                                 $pageindex++;
02449                                 $all_questions[$question_id]["questionblock_title"] = "";
02450                                 $all_questions[$question_id]["questionblock_id"] = "";
02451                                 $currentblock = "";
02452                                 $constraints = $this->getConstraints($question_id);
02453                                 $all_questions[$question_id]["constraints"] = $constraints;
02454                         }
02455                         if (!isset($all_pages[$pageindex]))
02456                         {
02457                                 $all_pages[$pageindex] = array();
02458                         }
02459                         array_push($all_pages[$pageindex], $all_questions[$question_id]);
02460                 }
02461                 // calculate position percentage for every page
02462                 $max = count($all_pages);
02463                 $counter = 1;
02464                 foreach ($all_pages as $index => $block)
02465                 {
02466                         foreach ($block as $blockindex => $question)
02467                         {
02468                                 $all_pages[$index][$blockindex][position] = $counter / $max;
02469                         }
02470                         $counter++;
02471                 }
02472                 return $all_pages;
02473         }
02474         
02485         function getNextPage($active_page_question_id, $direction)
02486         {
02487                 $foundpage = -1;
02488                 $pages =& $this->getSurveyPages();
02489                 if (strcmp($active_page_question_id, "") == 0)
02490                 {
02491                         return $pages[0];
02492                 }
02493                 
02494                 foreach ($pages as $key => $question_array)
02495                 {
02496                         foreach ($question_array as $question)
02497                         {
02498                                 if ($active_page_question_id == $question["question_id"])
02499                                 {
02500                                         $foundpage = $key;
02501                                 }
02502                         }
02503                 }
02504                 if ($foundpage == -1)
02505                 {
02506                         // error: page not found
02507                 }
02508                 else
02509                 {
02510                         $foundpage += $direction;
02511                         if ($foundpage < 0)
02512                         {
02513                                 return 0;
02514                         }
02515                         if ($foundpage >= count($pages))
02516                         {
02517                                 return 1;
02518                         }
02519                         return $pages[$foundpage];
02520                 }
02521         }
02522                 
02531         function &getAvailableQuestionpools($use_obj_id = false)
02532         {
02533                 global $rbacsystem;
02534                 
02535                 $result_array = array();
02536                 $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'";
02537                 $result = $this->ilias->db->query($query);
02538                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02539                 {               
02540                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
02541                         {
02542                                 if ($use_obj_id)
02543                                 {
02544                                         $result_array[$row->obj_id] = $row->title;
02545                                 }
02546                                 else
02547                                 {
02548                                         $result_array[$row->ref_id] = $row->title;
02549                                 }
02550                         }
02551                 }
02552                 return $result_array;
02553         }
02554         
02562         function getConstraints($question_id)
02563         {
02564                 $result_array = array();
02565                 $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",
02566                         $this->ilias->db->quote($question_id),
02567                         $this->ilias->db->quote($this->getSurveyId())
02568                 );
02569                 $result = $this->ilias->db->query($query);
02570                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02571                 {               
02572                         array_push($result_array, array("id" => $row->constraint_id, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "value" => $row->value));
02573                 }
02574                 return $result_array;
02575         }
02576 
02584         function _getConstraints($survey_id)
02585         {
02586                 global $ilDB;
02587                 $result_array = array();
02588                 $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",
02589                         $ilDB->quote($survey_id . "")
02590                 );
02591                 $result = $ilDB->query($query);
02592                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02593                 {               
02594                         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));
02595                 }
02596                 return $result_array;
02597         }
02598 
02599 
02607         function &getVariables($question_id)
02608         {
02609                 $result_array = array();
02610                 $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",
02611                         $this->ilias->db->quote($question_id)
02612                 );
02613                 $result = $this->ilias->db->query($query);
02614                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02615                 {
02616                         $result_array[$row->sequence] = $row;
02617                 }
02618                 return $result_array;
02619         }
02620         
02632         function addConstraint($to_question_id, $if_question_id, $relation, $value)
02633         {
02634                 $query = sprintf("INSERT INTO survey_constraint (constraint_id, question_fi, relation_fi, value) VALUES (NULL, %s, %s, %s)",
02635                         $this->ilias->db->quote($if_question_id),
02636                         $this->ilias->db->quote($relation),
02637                         $this->ilias->db->quote($value)
02638                 );
02639                 $result = $this->ilias->db->query($query);
02640                 if ($result == DB_OK) {
02641                         $constraint_id = $this->ilias->db->getLastInsertId();
02642                         $query = sprintf("INSERT INTO survey_question_constraint (question_constraint_id, survey_fi, question_fi, constraint_fi) VALUES (NULL, %s, %s, %s)",
02643                                 $this->ilias->db->quote($this->getSurveyId()),
02644                                 $this->ilias->db->quote($to_question_id),
02645                                 $this->ilias->db->quote($constraint_id)
02646                         );
02647                         $result = $this->ilias->db->query($query);
02648                 }
02649         }
02650         
02658         function getAllRelations($short_as_key = false)
02659         {
02660                 $result_array = array();
02661                 $query = "SELECT * FROM survey_relation";
02662                 $result = $this->ilias->db->query($query);
02663                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02664                 {
02665                         if ($short_as_key)
02666                         {
02667                                 $result_array[$row->shortname] = array("short" => $row->shortname, "long" => $row->longname, "id" => $row->relation_id);
02668                         }
02669                         else
02670                         {
02671                                 $result_array[$row->relation_id] = array("short" => $row->shortname, "long" => $row->longname);
02672                         }
02673                 }
02674                 return $result_array;
02675         }
02676 
02685         function disinviteUser($user_id)
02686         {
02687                 $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s AND user_fi = %s",
02688                         $this->ilias->db->quote($this->getSurveyId()),
02689                         $this->ilias->db->quote($user_id)
02690                 );
02691                 $result = $this->ilias->db->query($query);
02692                 if ($this->getInvitation() == INVITATION_ON)
02693                 {
02694                         $userObj = new ilObjUser($user_id);
02695                         $userObj->dropDesktopItem($this->getRefId(), "svy");
02696                 }
02697         }
02698 
02707         function inviteUser($user_id)
02708         {
02709                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE user_fi = %s AND survey_fi = %s",
02710                         $this->ilias->db->quote($user_id),
02711                         $this->ilias->db->quote($this->getSurveyId())
02712                 );
02713                 $result = $this->ilias->db->query($query);
02714                 if ($result->numRows() < 1)
02715                 {
02716                         $query = sprintf("INSERT INTO survey_invited_user (invited_user_id, survey_fi, user_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02717                                 $this->ilias->db->quote($this->getSurveyId()),
02718                                 $this->ilias->db->quote($user_id)
02719                         );
02720                         $result = $this->ilias->db->query($query);
02721                 }
02722                 if ($this->getInvitation() == INVITATION_ON)
02723                 {
02724                         $userObj = new ilObjUser($user_id);
02725                         $userObj->addDesktopItem($this->getRefId(), "svy");
02726                 }
02727         }
02728 
02737         function disinviteGroup($group_id)
02738         {
02739                 $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s AND group_fi = %s",
02740                         $this->ilias->db->quote($this->getSurveyId()),
02741                         $this->ilias->db->quote($group_id)
02742                 );
02743                 $result = $this->ilias->db->query($query);
02744                 if ($this->getInvitation() == INVITATION_ON)
02745                 {
02746                         $group = new ilObjGroup($group_id);
02747                         $members = $group->getGroupMemberIds();
02748                         foreach ($members as $user_id)
02749                         {
02750                                 $userObj = new ilObjUser($user_id);
02751                                 $userObj->dropDesktopItem($this->getRefId(), "svy");
02752                         }
02753                 }
02754         }
02755 
02764         function inviteGroup($group_id)
02765         {
02766                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE group_fi = %s AND survey_fi = %s",
02767                         $this->ilias->db->quote($group_id),
02768                         $this->ilias->db->quote($this->getSurveyId())
02769                 );
02770                 $result = $this->ilias->db->query($query);
02771                 if ($result->numRows() < 1)
02772                 {
02773                         $query = sprintf("INSERT INTO survey_invited_group (invited_group_id, survey_fi, group_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02774                                 $this->ilias->db->quote($this->getSurveyId()),
02775                                 $this->ilias->db->quote($group_id)
02776                         );
02777                         $result = $this->ilias->db->query($query);
02778                 }
02779                 
02780                 if ($this->getInvitation() == INVITATION_ON)
02781                 {
02782                         $group = new ilObjGroup($group_id);
02783                         $members = $group->getGroupMemberIds();
02784                         foreach ($members as $user_id)
02785                         {
02786                                 $userObj = new ilObjUser($user_id);
02787                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02788                         }
02789                 }
02790         }
02791         
02800         function &getInvitedUsers()
02801         {
02802                 $result_array = array();
02803                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
02804                         $this->ilias->db->quote($this->getSurveyId())
02805                 );
02806                 $result = $this->ilias->db->query($query);
02807                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02808                 {
02809                         array_push($result_array, $row->user_fi);
02810                 }
02811                 return $result_array;
02812         }
02813 
02822         function &getInvitedGroups()
02823         {
02824                 $result_array = array();
02825                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
02826                         $this->ilias->db->quote($this->getSurveyId())
02827                 );
02828                 $result = $this->ilias->db->query($query);
02829                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02830                 {
02831                         array_push($result_array, $row->group_fi);
02832                 }
02833                 return $result_array;
02834         }
02835 
02845         function deleteWorkingData($question_id, $user_id)
02846         {
02847                 $query = "";
02848                 if ($this->getAnonymize())
02849                 {
02850                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02851                                 $this->ilias->db->quote($this->getSurveyId()),
02852                                 $this->ilias->db->quote($question_id),
02853                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02854                         );
02855                 }
02856                 else
02857                 {
02858                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02859                                 $this->ilias->db->quote($this->getSurveyId()),
02860                                 $this->ilias->db->quote($question_id),
02861                                 $this->ilias->db->quote($user_id)
02862                         );
02863                 }
02864                 $result = $this->ilias->db->query($query);
02865         }
02866         
02878         function saveWorkingData($question_id, $user_id, $anonymize_id, $value = "", $text = "")
02879         {
02880                 if ($this->isSurveyStarted($user_id, $anonymize_id) === false)
02881                 {
02882                         $this->startSurvey($user_id, $anonymize_id);
02883                 }
02884                 if (strcmp($value, "") == 0)
02885                 {
02886                         $value = "NULL";
02887                 }
02888                 else
02889                 {
02890                         $value = $this->ilias->db->quote($value);
02891                 }
02892                 if (strcmp($text, "") == 0)
02893                 {
02894                         $text = "NULL";
02895                 }
02896                 else
02897                 {
02898                         $text = $this->ilias->db->quote($text);
02899                 }
02900                 if ($this->getAnonymize())
02901                 {
02902                         $user_id = 0;
02903                 }
02904                 $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)",
02905                         $this->ilias->db->quote($this->getSurveyId() . ""),
02906                         $this->ilias->db->quote($question_id . ""),
02907                         $this->ilias->db->quote($user_id . ""),
02908                         $this->ilias->db->quote($anonymize_id),
02909                         $value,
02910                         $text
02911                 );
02912                 $result = $this->ilias->db->query($query);
02913         }
02914         
02925         function loadWorkingData($question_id, $user_id)
02926         {
02927                 $result_array = array();
02928                 $query = "";
02929                 if ($this->getAnonymize())
02930                 {
02931                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02932                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02933                                 $this->ilias->db->quote($question_id. ""),
02934                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02935                         );
02936                 }
02937                 else
02938                 {
02939                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02940                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02941                                 $this->ilias->db->quote($question_id . ""),
02942                                 $this->ilias->db->quote($user_id . "")
02943                         );
02944                 }
02945                 $result = $this->ilias->db->query($query);
02946                 if ($result->numRows() >= 1)
02947                 {
02948                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02949                         {
02950                                 array_push($result_array, $row);
02951                         }
02952                         return $result_array;
02953                 }
02954                 else
02955                 {
02956                         return $result_array;
02957                 }
02958         }
02959 
02968         function startSurvey($user_id, $anonymous_id)
02969         {
02970                 global $ilUser;
02971                 
02972                 if (strcmp($user_id, "") == 0)
02973                 {
02974                         $user_id = 0;
02975                 }
02976                 if ($this->getAnonymize())
02977                 {
02978                         $user_id = 0;
02979                 }
02980                 $query = sprintf("INSERT INTO survey_finished (finished_id, survey_fi, user_fi, anonymous_id, state, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
02981                         $this->ilias->db->quote($this->getSurveyId() . ""),
02982                         $this->ilias->db->quote($user_id . ""),
02983                         $this->ilias->db->quote($anonymous_id . ""),
02984                         $this->ilias->db->quote(0 . "")
02985                 );
02986                 $result = $this->ilias->db->query($query);
02987                 if ($this->getAnonymize())
02988                 {
02989                         if (strcmp($ilUser->login, "anonymous") != 0)
02990                         {
02991                                 require_once "./include/inc.mail.php";
02992                                 require_once "./classes/class.ilFormatMail.php";
02993                                 require_once "./classes/class.ilMailbox.php";
02994                                 $subject = sprintf($this->lng->txt("subject_mail_survey_id"), $this->getTitle());
02995                                 $message = sprintf($this->lng->txt("message_mail_survey_id"), $this->getTitle(), $_SESSION["anonymous_id"]);
02996                                 $umail = new ilFormatMail($ilUser->id);
02997                                 $f_message = $umail->formatLinebreakMessage($message);
02998                                 $umail->setSaveInSentbox(true);
02999                                 if($error_message = $umail->sendMail($ilUser->getLogin(),"",
03000                                                                                                          "",$subject,$f_message,
03001                                                                                                          "",array("normal")))
03002                                 {
03003                                         sendInfo($error_message);
03004                                 }
03005                         }
03006                 }
03007         }
03008                         
03017         function finishSurvey($user_id, $anonymize_id)
03018         {
03019                 if ($this->getAnonymize())
03020                 {
03021                         $user_id = 0;
03022                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND anonymous_id = %s",
03023                                 $this->ilias->db->quote("1"),
03024                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03025                                 $this->ilias->db->quote($anonymize_id . "")
03026                         );
03027                 }
03028                 else
03029                 {
03030                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND user_fi = %s",
03031                                 $this->ilias->db->quote("1"),
03032                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03033                                 $this->ilias->db->quote($user_id . "")
03034                         );
03035                 }
03036                 $result = $this->ilias->db->query($query);
03037         }
03038         
03048         function isSurveyStarted($user_id, $anonymize_id)
03049         {
03050                 if ($this->getAnonymize())
03051                 {
03052                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
03053                                 $this->ilias->db->quote($this->getSurveyId()),
03054                                 $this->ilias->db->quote($anonymize_id)
03055                         );
03056                 }
03057                 else
03058                 {
03059                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03060                                 $this->ilias->db->quote($this->getSurveyId()),
03061                                 $this->ilias->db->quote($user_id)
03062                         );
03063                 }
03064                 $result = $this->ilias->db->query($query);
03065                 if ($result->numRows() == 0)
03066                 {
03067                         return false;
03068                 }                       
03069                 else
03070                 {
03071                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03072                         return (int)$row["state"];
03073                 }
03074         }
03075         
03085         function getLastActivePage($user_id)
03086         {
03087                 $query = "";
03088                 if ($this->getAnonymize())
03089                 {
03090                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s ORDER BY TIMESTAMP14 DESC",
03091                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03092                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
03093                         );
03094                 }
03095                 else
03096                 {
03097                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND user_fi = %s ORDER BY TIMESTAMP14 DESC",
03098                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03099                                 $this->ilias->db->quote($user_id . "")
03100                         );
03101                 }
03102                 $result = $this->ilias->db->query($query);
03103                 if ($result->numRows() == 0)
03104                 {
03105                         return "";
03106                 }
03107                 else
03108                 {
03109                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03110                         return $row["question_fi"];
03111                 }
03112         }
03113 
03124         function checkConstraint($constraint_data, $working_data)
03125         {
03126                 if (count($working_data) == 0)
03127                 {
03128                         return 0;
03129                 }
03130                 
03131                 if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
03132                 {
03133                         return 0;
03134                 }
03135                 
03136                 foreach ($working_data as $data)
03137                 {
03138                         switch ($constraint_data["short"])
03139                         {
03140                                 case "<":
03141                                         if ($data["value"] < $constraint_data["value"])
03142                                         {
03143                                                 return 1;
03144                                         }
03145                                         break;
03146                                 case "<=":
03147                                         if ($data["value"] <= $constraint_data["value"])
03148                                         {
03149                                                 return 1;
03150                                         }
03151                                         break;
03152                                 case "=":
03153                                         if ($data["value"] == $constraint_data["value"])
03154                                         {
03155                                                 return 1;
03156                                         }
03157                                         break;
03158                                 case "<>":
03159                                         if ($data["value"] != $constraint_data["value"])
03160                                         {
03161                                                 return 1;
03162                                         }
03163                                         break;
03164                                 case ">=":
03165                                         if ($data["value"] >= $constraint_data["value"])
03166                                         {
03167                                                 return 1;
03168                                         }
03169                                         break;
03170                                 case ">":
03171                                         if ($data["value"] > $constraint_data["value"])
03172                                         {
03173                                                 return 1;
03174                                         }
03175                                         break;
03176                         }
03177                 }
03178                 return 0;
03179         }
03180 
03181         function &getEvaluationForAllUsers()
03182         {
03183                 $users = array();
03184                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03185                         $this->ilias->db->quote($this->getSurveyId() . "")
03186                 );
03187                 $result = $this->ilias->db->query($query);
03188                 if ($result->numRows())
03189                 {
03190                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03191                         {
03192                                 array_push($users, $row);
03193                         }
03194                 }
03195                 $evaluation = array();
03196                 $questions =& $this->getSurveyQuestions();
03197                 foreach ($users as $row)
03198                 {
03199                         if ($row["user_fi"] > 0)
03200                         {
03201                                 $evaluation[$row["user_fi"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03202                         }
03203                         else
03204                         {
03205                                 $evaluation[$row["anonymous_id"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03206                         }
03207                 }
03208                 return $evaluation;
03209         }
03210         
03222         function &getEvaluationByUser($questions, $user_id, $anonymous_id = "")
03223         {
03224                 $wherecond = "";
03225                 $wherevalue = "";
03226                 if (strcmp($anonymous_id, "") != 0)
03227                 {
03228                         $wherecond = "anonymous_id = %s";
03229                         $wherevalue = $anonymous_id;
03230                 }
03231                 else
03232                 {
03233                         $wherecond = "user_fi = %s";
03234                         $wherevalue = $user_id;
03235                 }
03236                 // collect all answers
03237                 $answers = array();
03238                 $query = sprintf("SELECT * FROM survey_answer WHERE $wherecond AND survey_fi = %s",
03239                         $this->ilias->db->quote($wherevalue),
03240                         $this->ilias->db->quote($this->getSurveyId())
03241                 );
03242                 $result = $this->ilias->db->query($query);
03243                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03244                 {
03245                         if (!is_array($answers[$row["question_fi"]]))
03246                         {
03247                                 $answers[$row["question_fi"]] = array();
03248                         }
03249                         array_push($answers[$row["question_fi"]], $row);
03250                 }
03251                 $username = "";
03252                 if ($user_id > 0)
03253                 {
03254                         include_once "./classes/class.ilObjUser.php";
03255                         if (strlen(ilObjUser::_lookupLogin($user_id)) == 0)
03256                         {
03257                                 $username = $this->lng->txt("deleted_user");
03258                         }
03259                         else
03260                         {
03261                                 $user = new ilObjUser($user_id);
03262                                 $username = $user->getFullname();
03263                         }
03264                 }
03265                 $resultset = array(
03266                         "name" => $username,
03267                         "answers" => array()
03268                 );
03269                 foreach ($questions as $key => $question)
03270                 {
03271                         if (array_key_exists($key, $answers))
03272                         {
03273                                 $resultset["answers"][$key] = $answers[$key];
03274                         }
03275                         else
03276                         {
03277                                 $resultset["answers"][$key] = array();
03278                         }
03279                         sort($resultset["answers"][$key]);
03280                 }
03281                 return $resultset;
03282         }
03283         
03294         function getEvaluation($question_id)
03295         {
03296                 $questions =& $this->getSurveyQuestions();
03297                 $result_array = array();
03298                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03299                         $this->ilias->db->quote($this->getSurveyId())
03300                 );
03301                 $result = $this->ilias->db->query($query);
03302                 $nr_of_users = $result->numRows();
03303                                 
03304                 $query = sprintf("SELECT * FROM survey_answer WHERE question_fi = %s AND survey_fi = %s",
03305                         $this->ilias->db->quote($question_id),
03306                         $this->ilias->db->quote($this->getSurveyId())
03307                 );
03308                 $result = $this->ilias->db->query($query);
03309                 $cumulated = array();
03310                 $textvalues = array();
03311                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03312                 {
03313                         $cumulated["$row->value"]++;
03314                         array_push($textvalues, $row->textanswer);
03315                 }
03316                 asort($cumulated, SORT_NUMERIC);
03317                 end($cumulated);
03318                 $numrows = $result->numRows();
03319                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03320                 {
03321                         if ($this->getAnonymize())
03322                         {
03323                                 $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",
03324                                         $this->ilias->db->quote($question_id),
03325                                         $this->ilias->db->quote($this->getSurveyId())
03326                                 );
03327                         }
03328                         else
03329                         {
03330                                 $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",
03331                                         $this->ilias->db->quote($question_id),
03332                                         $this->ilias->db->quote($this->getSurveyId())
03333                                 );
03334                         }
03335                         $mcmr_result = $this->ilias->db->query($query);
03336                         $result_array["USERS_ANSWERED"] = $mcmr_result->numRows();
03337                         $result_array["USERS_SKIPPED"] = $nr_of_users - $mcmr_result->numRows();
03338                         $numrows = $mcmr_result->numRows();
03339                 }
03340                 else
03341                 {
03342                         $result_array["USERS_ANSWERED"] = $result->numRows();
03343                         $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
03344                 }
03345                 $variables =& $this->getVariables($question_id);
03346                 switch ($questions[$question_id]["type_tag"])
03347                 {
03348                         case "qt_nominal":
03349                                 $result_array["MEDIAN"] = "";
03350                                 $result_array["ARITHMETIC_MEAN"] = "";
03351                                 $prefix = "";
03352                                 if (strcmp(key($cumulated), "") != 0)
03353                                 {
03354                                         $prefix = (key($cumulated)+1) . " - ";
03355                                 }
03356                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03357                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03358                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03359                                 $maxvalues = 0;
03360                                 foreach ($variables as $key => $value)
03361                                 {
03362                                         $maxvalues += $cumulated[$key];
03363                                 }
03364                                 foreach ($variables as $key => $value)
03365                                 {
03366                                         $percentage = 0;
03367                                         if ($numrows > 0)
03368                                         {
03369                                                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03370                                                 {
03371                                                         if ($maxvalues > 0)
03372                                                         {
03373                                                                 $percentage = (float)((int)$cumulated[$key]/$maxvalues);
03374                                                         }
03375                                                 }
03376                                                 else
03377                                                 {
03378                                                         $percentage = (float)((int)$cumulated[$key]/$numrows);
03379                                                 }
03380                                         }
03381                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03382                                 }
03383                                 break;
03384                         case "qt_ordinal":
03385                                 $prefix = "";
03386                                 if (strcmp(key($cumulated), "") != 0)
03387                                 {
03388                                         $prefix = (key($cumulated)+1) . " - ";
03389                                 }
03390                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03391                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03392                                 foreach ($variables as $key => $value)
03393                                 {
03394                                         $percentage = 0;
03395                                         if ($numrows > 0)
03396                                         {
03397                                                 $percentage = (float)((int)$cumulated[$key]/$numrows);
03398                                         }
03399                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03400                                 }
03401                                 ksort($cumulated, SORT_NUMERIC);
03402                                 $median = array();
03403                                 $total = 0;
03404                                 foreach ($cumulated as $value => $key)
03405                                 {
03406                                         $total += $key;
03407                                         for ($i = 0; $i < $key; $i++)
03408                                         {
03409                                                 array_push($median, $value+1);
03410                                         }
03411                                 }
03412                                 if ($total > 0)
03413                                 {
03414                                         if (($total % 2) == 0)
03415                                         {
03416                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03417                                                 if (round($median_value) != $median_value)
03418                                                 {
03419                                                         $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 . ")";
03420                                                 }
03421                                         }
03422                                         else
03423                                         {
03424                                                 $median_value = $median[(($total+1)/2)-1];
03425                                         }
03426                                 }
03427                                 else
03428                                 {
03429                                         $median_value = "";
03430                                 }
03431                                 $result_array["ARITHMETIC_MEAN"] = "";
03432                                 $result_array["MEDIAN"] = $median_value;
03433                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03434                                 break;
03435                         case "qt_metric":
03436                                 $result_array["MODE"] = key($cumulated);
03437                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03438                                 ksort($cumulated, SORT_NUMERIC);
03439                                 $counter = 0;
03440                                 foreach ($cumulated as $value => $nr_of_users)
03441                                 {
03442                                         $percentage = 0;
03443                                         if ($numrows > 0)
03444                                         {
03445                                                 $percentage = (float)($nr_of_users/$numrows);
03446                                         }
03447                                         $result_array["values"][$counter++] = array("value" => $value, "selected" => (int)$nr_of_users, "percentage" => $percentage);
03448                                 }
03449                                 $median = array();
03450                                 $total = 0;
03451                                 $x_i = 0;
03452                                 $p_i = 1;
03453                                 $x_i_inv = 0;
03454                                 $sum_part_zero = false;
03455                                 foreach ($cumulated as $value => $key)
03456                                 {
03457                                         $total += $key;
03458                                         for ($i = 0; $i < $key; $i++)
03459                                         {
03460                                                 array_push($median, $value);
03461                                                 $x_i += $value;
03462                                                 $p_i *= $value;
03463                                                 if ($value != 0)
03464                                                 {
03465                                                         $sum_part_zero = true;
03466                                                         $x_i_inv += 1/$value;
03467                                                 }
03468                                         }
03469                                 }
03470                                 if ($total > 0)
03471                                 {
03472                                         if (($total % 2) == 0)
03473                                         {
03474                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03475                                         }
03476                                         else
03477                                         {
03478                                                 $median_value = $median[(($total+1)/2)-1];
03479                                         }
03480                                 }
03481                                 else
03482                                 {
03483                                         $median_value = "";
03484                                 }
03485                                 if ($total > 0)
03486                                 {
03487                                         if (($x_i/$total) == (int)($x_i/$total))
03488                                         {
03489                                                 $result_array["ARITHMETIC_MEAN"] = $x_i/$total;
03490                                         }
03491                                         else
03492                                         {
03493                                                 $result_array["ARITHMETIC_MEAN"] = sprintf("%.2f", $x_i/$total);
03494                                         }
03495                                 }
03496                                 else
03497                                 {
03498                                         $result_array["ARITHMETIC_MEAN"] = "";
03499                                 }
03500                                 $result_array["MEDIAN"] = $median_value;
03501                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03502                                 break;
03503                         case "qt_text":
03504                                 $result_array["ARITHMETIC_MEAN"] = "";
03505                                 $result_array["MEDIAN"] = "";
03506                                 $result_array["MODE"] = "";
03507                                 $result_array["MODE_NR_OF_SELECTIONS"] = "";
03508                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03509                                 $result_array["textvalues"] = $textvalues;
03510                                 break;
03511                 }
03512                 return $result_array;
03513         }
03514 
03515         function &getQuestions($question_ids)
03516         {
03517                 $result_array = array();
03518                 $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, ",") . ")";
03519                 $result = $this->ilias->db->query($query);
03520                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03521                 {
03522                         array_push($result_array, $row);
03523                 }
03524                 return $result_array;
03525         }
03526         
03527         function &getQuestionblocks($questionblock_ids)
03528         {
03529                 $result_array = array();
03530     $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";
03531                 $result = $this->ilias->db->query($query);
03532                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03533                 {
03534                         if ($row["questionblock_id"] != $qbid)
03535                         {
03536                                 $sequence = 1;
03537                         }
03538                         $row["sequence"] = $sequence++;
03539                         $result_array[$row["questionblock_id"]][$row["question_id"]] = $row;
03540                         $qbid = $row["questionblock_id"];
03541                 }
03542                 return $result_array;
03543         }
03544 
03545         function &getForbiddenQuestionpools()
03546         {
03547                 global $rbacsystem;
03548                 
03549                 // get all available questionpools and remove the trashed questionspools
03550                 $forbidden_pools = array();
03551                 $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'";
03552                 $result = $this->ilias->db->query($query);
03553                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03554                 {               
03555                         if (!$rbacsystem->checkAccess("write", $row->ref_id) || (!$this->_hasUntrashedReference($row->obj_id)))
03556                         {
03557                                 array_push($forbidden_pools, $row->obj_id);
03558                         }
03559                 }
03560                 return $forbidden_pools;
03561         }
03562         
03570         function getQuestionsTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0, $completeonly = 0, $filter_question_type = "", $filter_questionpool = "")
03571         {
03572                 global $ilUser;
03573                 $where = "";
03574                 if (strlen($filter_text) > 0) {
03575                         switch($sel_filter_type) {
03576                                 case "title":
03577                                         $where = " AND survey_question.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03578                                         break;
03579                                 case "description":
03580                                         $where = " AND survey_question.description LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03581                                         break;
03582                                 case "author":
03583                                         $where = " AND survey_question.author LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03584                                         break;
03585                         }
03586                 }
03587   
03588                 if ($filter_question_type && (strcmp($filter_question_type, "all") != 0))
03589                 {
03590                         $where .= " AND survey_questiontype.type_tag = " . $this->ilias->db->quote($filter_question_type);
03591                 }
03592                 
03593                 if ($filter_questionpool && (strcmp($filter_questionpool, "all") != 0))
03594                 {
03595                         $where .= " AND survey_question.obj_fi = $filter_questionpool";
03596                 }
03597   
03598     // build sort order for sql query
03599                 $order = "";
03600                 $images = array();
03601     if (count($sortoptions)) {
03602       foreach ($sortoptions as $key => $value) {
03603         switch($key) {
03604           case "title":
03605             $order = " ORDER BY title $value";
03606             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03607             break;
03608           case "description":
03609             $order = " ORDER BY description $value";
03610             $images["description"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03611             break;
03612           case "type":
03613             $order = " ORDER BY questiontype_id $value";
03614             $images["type"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03615             break;
03616           case "author":
03617             $order = " ORDER BY author $value";
03618             $images["author"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03619             break;
03620           case "created":
03621             $order = " ORDER BY created $value";
03622             $images["created"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03623             break;
03624           case "updated":
03625             $order = " ORDER BY TIMESTAMP14 $value";
03626             $images["updated"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03627             break;
03628                                         case "qpl":
03629                                                 $order = " ORDER BY obj_fi $value";
03630             $images["qpl"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03631                                                 break;
03632         }
03633       }
03634     }
03635                 $maxentries = $ilUser->prefs["hits_per_page"];
03636                 if ($maxentries < 1)
03637                 {
03638                         $maxentries = 9999;
03639                 }
03640 
03641                 $forbidden_pools =& $this->getForbiddenQuestionpools();
03642                 $forbidden = "";
03643                 if (count($forbidden_pools))
03644                 {
03645                         $forbidden = " AND survey_question.obj_fi NOT IN (" . join($forbidden_pools, ",") . ")";
03646                 }
03647                 if ($completeonly)
03648                 {
03649                         $forbidden .= " AND survey_question.complete = " . $this->ilias->db->quote("1");
03650                 }
03651 
03652                 $existing = "";
03653                 $existing_questions =& $this->getExistingQuestions();
03654                 if (count($existing_questions))
03655                 {
03656                         $existing = " AND survey_question.question_id NOT IN (" . join($existing_questions, ",") . ")";
03657                 }
03658           $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";
03659     $query_result = $this->ilias->db->query($query);
03660                 $max = $query_result->numRows();
03661                 if ($startrow > $max -1)
03662                 {
03663                         $startrow = $max - ($max % $maxentries);
03664                 }
03665                 else if ($startrow < 0)
03666                 {
03667                         $startrow = 0;
03668                 }
03669                 $limit = " LIMIT $startrow, $maxentries";
03670           $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";
03671     $query_result = $this->ilias->db->query($query);
03672                 $rows = array();
03673                 if ($query_result->numRows())
03674                 {
03675                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03676                         {
03677                                 array_push($rows, $row);
03678                         }
03679                 }
03680                 $nextrow = $startrow + $maxentries;
03681                 if ($nextrow > $max - 1)
03682                 {
03683                         $nextrow = $startrow;
03684                 }
03685                 $prevrow = $startrow - $maxentries;
03686                 if ($prevrow < 0)
03687                 {
03688                         $prevrow = 0;
03689                 }
03690                 return array(
03691                         "rows" => $rows,
03692                         "images" => $images,
03693                         "startrow" => $startrow,
03694                         "nextrow" => $nextrow,
03695                         "prevrow" => $prevrow,
03696                         "step" => $maxentries,
03697                         "rowcount" => $max
03698                 );
03699         }
03700 
03708         function getQuestionblocksTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0)
03709         {
03710                 global $ilUser;
03711                 $where = "";
03712                 if (strlen($filter_text) > 0) {
03713                         switch($sel_filter_type) {
03714                                 case "title":
03715                                         $where = " AND survey_questionblock.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03716                                         break;
03717                         }
03718                 }
03719   
03720     // build sort order for sql query
03721                 $order = "";
03722                 $images = array();
03723     if (count($sortoptions)) {
03724       foreach ($sortoptions as $key => $value) {
03725         switch($key) {
03726           case "title":
03727                                                 $order = " ORDER BY survey_questionblock.title $value";
03728             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03729             break;
03730                                         case "svy":
03731                                                 $order = " ORDER BY survey_survey_question.survey_fi $value";
03732             $images["svy"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03733                                                 break;
03734         }
03735       }
03736     }
03737                 $maxentries = $ilUser->prefs["hits_per_page"];
03738                 if ($order)
03739                 {
03740                         $order .=  ",survey_survey_question.sequence ASC";
03741                 }
03742                 else
03743                 {
03744                         $order = " ORDER BY survey_survey_question.sequence ASC";
03745                 }
03746                 $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";
03747     $query_result = $this->ilias->db->query($query);
03748                 $questionblock_ids = array();
03749                 if ($query_result->numRows())
03750                 {
03751                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03752                         {
03753                                 array_push($questionblock_ids, $row["questionblock_id"]);
03754                         }
03755                 }
03756                 
03757                 $max = $query_result->numRows();
03758                 if ($startrow > $max -1)
03759                 {
03760                         $startrow = $max - ($max % $maxentries);
03761                 }
03762                 else if ($startrow < 0)
03763                 {
03764                         $startrow = 0;
03765                 }
03766                 $limit = " LIMIT $startrow, $maxentries";
03767                 $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";
03768     $query_result = $this->ilias->db->query($query);
03769                 $rows = array();
03770                 if ($query_result->numRows())
03771                 {
03772                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03773                         {
03774                                 $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
03775                                 $counter = 1;
03776                                 foreach ($questions_array as $key => $value)
03777                                 {
03778                                         $questions_array[$key] = "$counter. $value";
03779                                         $counter++;
03780                                 }
03781                                 $rows[$row["questionblock_id"]] = array(
03782                                         "questionblock_id" => $row["questionblock_id"],
03783                                         "title" => $row["title"], 
03784                                         "surveytitle" => $row["surveytitle"], 
03785                                         "questions" => join($questions_array, ", "),
03786                                         "owner" => $row["owner_fi"]
03787                                 );
03788                         }
03789                 }
03790                 $nextrow = $startrow + $maxentries;
03791                 if ($nextrow > $max - 1)
03792                 {
03793                         $nextrow = $startrow;
03794                 }
03795                 $prevrow = $startrow - $maxentries;
03796                 if ($prevrow < 0)
03797                 {
03798                         $prevrow = 0;
03799                 }
03800                 return array(
03801                         "rows" => $rows,
03802                         "images" => $images,
03803                         "startrow" => $startrow,
03804                         "nextrow" => $nextrow,
03805                         "prevrow" => $prevrow,
03806                         "step" => $maxentries,
03807                         "rowcount" => $max
03808                 );
03809         }
03810 
03819         function &_getQuestiontypes()
03820         {
03821                 global $ilDB;
03822                 
03823                 $questiontypes = array();
03824                 $query = "SELECT * FROM survey_questiontype ORDER BY type_tag";
03825                 $query_result = $ilDB->query($query);
03826                 while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03827                 {
03828                         array_push($questiontypes, $row["type_tag"]);
03829                 }
03830                 return $questiontypes;
03831         }
03832                 
03841         function to_xml()
03842         {
03843                 $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<questestinterop></questestinterop>\n";
03844                 $domxml = domxml_open_mem($xml_header);
03845                 $root = $domxml->document_element();
03846                 // qti assessment
03847                 $qtiSurvey = $domxml->create_element("survey");
03848                 $qtiSurvey->set_attribute("ident", $this->getSurveyId());
03849                 $qtiSurvey->set_attribute("title", $this->getTitle());
03850                 
03851                 // add qti comment
03852                 $qtiComment = $domxml->create_element("qticomment");
03853                 $qtiCommentText = $domxml->create_text_node($this->getDescription());
03854                 $qtiComment->append_child($qtiCommentText);
03855                 $qtiSurvey->append_child($qtiComment);
03856                 $qtiComment = $domxml->create_element("qticomment");
03857                 $qtiCommentText = $domxml->create_text_node("ILIAS Version=".$this->ilias->getSetting("ilias_version"));
03858                 $qtiComment->append_child($qtiCommentText);
03859                 $qtiSurvey->append_child($qtiComment);
03860                 $qtiComment = $domxml->create_element("qticomment");
03861                 $qtiCommentText = $domxml->create_text_node("Author=".$this->getAuthor());
03862                 $qtiComment->append_child($qtiCommentText);
03863                 $qtiSurvey->append_child($qtiComment);
03864                 // add qti objectives
03865                 $qtiObjectives = $domxml->create_element("objectives");
03866                 $qtiMaterial = $domxml->create_element("material");
03867                 $qtiMaterial->set_attribute("label", "introduction");
03868                 $qtiMatText = $domxml->create_element("mattext");
03869                 $qtiMatTextText = $domxml->create_text_node($this->getIntroduction());
03870                 $qtiMatText->append_child($qtiMatTextText);
03871                 $qtiMaterial->append_child($qtiMatText);
03872                 $qtiObjectives->append_child($qtiMaterial);
03873                 $qtiSurvey->append_child($qtiObjectives);
03874                 // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
03875                 $qtiMetadata = $domxml->create_element("qtimetadata");
03876                 // author
03877                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03878                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03879                 $qtiFieldLabelText = $domxml->create_text_node("author");
03880                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03881                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03882                 $qtiFieldEntryText = $domxml->create_text_node($this->getAuthor());
03883                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03884                 $qtiMetadatafield->append_child($qtiFieldLabel);
03885                 $qtiMetadatafield->append_child($qtiFieldEntry);
03886                 $qtiMetadata->append_child($qtiMetadatafield);
03887                 // description
03888                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03889                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03890                 $qtiFieldLabelText = $domxml->create_text_node("description");
03891                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03892                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03893                 $qtiFieldEntryText = $domxml->create_text_node($this->getDescription());
03894                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03895                 $qtiMetadatafield->append_child($qtiFieldLabel);
03896                 $qtiMetadatafield->append_child($qtiFieldEntry);
03897                 $qtiMetadata->append_child($qtiMetadatafield);
03898                 // evaluation access
03899                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03900                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03901                 $qtiFieldLabelText = $domxml->create_text_node("evaluation_access");
03902                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03903                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03904                 $qtiFieldEntryText = $domxml->create_text_node($this->getEvaluationAccess());
03905                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03906                 $qtiMetadatafield->append_child($qtiFieldLabel);
03907                 $qtiMetadatafield->append_child($qtiFieldEntry);
03908                 $qtiMetadata->append_child($qtiMetadatafield);
03909                 // anonymization
03910                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03911                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03912                 $qtiFieldLabelText = $domxml->create_text_node("anonymize");
03913                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03914                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03915                 $qtiFieldEntryText = $domxml->create_text_node($this->getAnonymize());
03916                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03917                 $qtiMetadatafield->append_child($qtiFieldLabel);
03918                 $qtiMetadatafield->append_child($qtiFieldEntry);
03919                 $qtiMetadata->append_child($qtiMetadatafield);
03920                 // status
03921                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03922                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03923                 $qtiFieldLabelText = $domxml->create_text_node("status");
03924                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03925                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03926                 $qtiFieldEntryText = $domxml->create_text_node($this->getStatus());
03927                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03928                 $qtiMetadatafield->append_child($qtiFieldLabel);
03929                 $qtiMetadatafield->append_child($qtiFieldEntry);
03930                 $qtiMetadata->append_child($qtiMetadatafield);
03931                 // start date
03932                 if ($this->getStartDateEnabled())
03933                 {
03934                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03935                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
03936                         $qtiFieldLabelText = $domxml->create_text_node("startdate");
03937                         $qtiFieldLabel->append_child($qtiFieldLabelText);
03938                         $qtiFieldEntry = $domxml->create_element("fieldentry");
03939                         $qtiFieldEntryText = $domxml->create_text_node(sprintf("P%dY%dM%dDT0H0M0S", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
03940                         $qtiFieldEntry->append_child($qtiFieldEntryText);
03941                         $qtiMetadatafield->append_child($qtiFieldLabel);
03942                         $qtiMetadatafield->append_child($qtiFieldEntry);
03943                         $qtiMetadata->append_child($qtiMetadatafield);
03944                 }
03945                 // end date
03946                 if ($this->getEndDateEnabled())
03947                 {
03948                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03949                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
03950                         $qtiFieldLabelText = $domxml->create_text_node("enddate");
03951                         $qtiFieldLabel->append_child($qtiFieldLabelText);
03952                         $qtiFieldEntry = $domxml->create_element("fieldentry");
03953                         $qtiFieldEntryText = $domxml->create_text_node(sprintf("P%dY%dM%dDT0H0M0S", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
03954                         $qtiFieldEntry->append_child($qtiFieldEntryText);
03955                         $qtiMetadatafield->append_child($qtiFieldLabel);
03956                         $qtiMetadatafield->append_child($qtiFieldEntry);
03957                         $qtiMetadata->append_child($qtiMetadatafield);
03958                 }
03959                 // show question titles
03960                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03961                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03962                 $qtiFieldLabelText = $domxml->create_text_node("display_question_titles");
03963                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03964                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03965                 $qtiFieldEntryText = $domxml->create_text_node($this->getShowQuestionTitles());
03966                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03967                 $qtiMetadatafield->append_child($qtiFieldLabel);
03968                 $qtiMetadatafield->append_child($qtiFieldEntry);
03969                 $qtiMetadata->append_child($qtiMetadatafield);
03970                 // add questionblock descriptions
03971                 $pages =& $this->getSurveyPages();
03972                 foreach ($pages as $question_array)
03973                 {
03974                         if (count($question_array) > 1)
03975                         {
03976                                 $question_ids = array();
03977                                 // found a questionblock
03978                                 foreach ($question_array as $question)
03979                                 {
03980                                         array_push($question_ids, $question["question_id"]);
03981                                 }
03982                                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
03983                                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
03984                                 $qtiFieldLabelText = $domxml->create_text_node("questionblock_" . $question_array[0]["questionblock_id"]);
03985                                 $qtiFieldLabel->append_child($qtiFieldLabelText);
03986                                 $qtiFieldEntry = $domxml->create_element("fieldentry");
03987                                 $qtiFieldEntryText = $domxml->create_text_node("<title>" . $question["questionblock_title"]. "</title><questions>" . join($question_ids, ",") . "</questions>");
03988                                 $qtiFieldEntry->append_child($qtiFieldEntryText);
03989                                 $qtiMetadatafield->append_child($qtiFieldLabel);
03990                                 $qtiMetadatafield->append_child($qtiFieldEntry);
03991                                 $qtiMetadata->append_child($qtiMetadatafield);                          
03992                         }
03993                 }
03994                 // add constraints
03995                 foreach ($pages as $question_array)
03996                 {
03997                         foreach ($question_array as $question)
03998                         {
03999                                 if (count($question["constraints"]))
04000                                 {
04001                                         // found constraints
04002                                         foreach ($question["constraints"] as $constraint)
04003                                         {
04004                                                 $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
04005                                                 $qtiFieldLabel = $domxml->create_element("fieldlabel");
04006                                                 $qtiFieldLabelText = $domxml->create_text_node("constraint_" . $question["question_id"]);
04007                                                 $qtiFieldLabel->append_child($qtiFieldLabelText);
04008                                                 $qtiFieldEntry = $domxml->create_element("fieldentry");
04009                                                 $qtiFieldEntryText = $domxml->create_text_node($constraint["question"] . "," . $constraint["short"] . "," . $constraint["value"]);
04010                                                 $qtiFieldEntry->append_child($qtiFieldEntryText);
04011                                                 $qtiMetadatafield->append_child($qtiFieldLabel);
04012                                                 $qtiMetadatafield->append_child($qtiFieldEntry);
04013                                                 $qtiMetadata->append_child($qtiMetadatafield);                          
04014                                         }
04015                                 }
04016                         }
04017                 }
04018                 // add headings
04019                 foreach ($pages as $question_array)
04020                 {
04021                         foreach ($question_array as $question)
04022                         {
04023                                 if ($question["heading"])
04024                                 {
04025                                         $qtiMetadatafield = $domxml->create_element("qtimetadatafield");
04026                                         $qtiFieldLabel = $domxml->create_element("fieldlabel");
04027                                         $qtiFieldLabelText = $domxml->create_text_node("heading_" . $question["question_id"]);
04028                                         $qtiFieldLabel->append_child($qtiFieldLabelText);
04029                                         $qtiFieldEntry = $domxml->create_element("fieldentry");
04030                                         $qtiFieldEntryText = $domxml->create_text_node($question["heading"]);
04031                                         $qtiFieldEntry->append_child($qtiFieldEntryText);
04032                                         $qtiMetadatafield->append_child($qtiFieldLabel);
04033                                         $qtiMetadatafield->append_child($qtiFieldEntry);
04034                                         $qtiMetadata->append_child($qtiMetadatafield);                          
04035                                 }
04036                         }
04037                 }
04038                 $qtiSurvey->append_child($qtiMetadata);
04039                 $root->append_child($qtiSurvey);
04040                 $xml = $domxml->dump_mem(true);
04041                 $domxml->free();
04042                 $obligatory_states =& $this->getObligatoryStates();
04043                 foreach ($this->questions as $question_id) {
04044                         $question =& $this->_instanciateQuestion($question_id);
04045                         $qti_question = $question->to_xml(false, $obligatory_states[$question_id]);
04046                         $qti_question = preg_replace("/<questestinterop>/", "", $qti_question);
04047                         $qti_question = preg_replace("/<\/questestinterop>/", "", $qti_question);
04048                         $xml = str_replace("</questestinterop>", "$qti_question</questestinterop>", $xml);
04049                 }
04050                 return $xml;
04051                         }
04052         
04062   function &_instanciateQuestion($question_id) {
04063       $question_type = SurveyQuestion::_getQuestionType($question_id);
04064       switch ($question_type) {
04065                                 case "qt_nominal":
04066                                         $question = new SurveyNominalQuestion();
04067                                         break;
04068                                 case "qt_ordinal":
04069                                         $question = new SurveyOrdinalQuestion();
04070                                         break;
04071                                 case "qt_metric":
04072                                         $question = new SurveyMetricQuestion();
04073                                         break;
04074                                 case "qt_text":
04075                                         $question = new SurveyTextQuestion();
04076                                         break;
04077                         }
04078       $question->loadFromDb($question_id);
04079                         return $question;
04080   }
04081 
04090         function importObject($file_info, $survey_questionpool_id)
04091         {
04092                 // check if file was uploaded
04093                 $source = $file_info["tmp_name"];
04094                 $error = 0;
04095                 if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
04096                 {
04097                         $this->ilias->raiseError($this->lng->txt("import_no_file_selected"),$this->ilias->error_obj->MESSAGE);
04098                         $error = 1;
04099                 }
04100                 // check correct file type
04101                 if (!((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0)))
04102                 {
04103                         $this->ilias->raiseError($this->lng->txt("import_wrong_file_type"),$this->ilias->error_obj->MESSAGE);
04104                         $error = 1;
04105                 }
04106                 if (!$error)
04107                 {
04108                         // import file as a survey
04109                         $import_dir = $this->getImportDirectory();
04110                         $importfile = tempnam($import_dir, "survey_import");
04111                         //move_uploaded_file($source, $importfile);
04112                         ilUtil::moveUploadedFile($source, "survey_import", $importfile);
04113                         $fh = fopen($importfile, "r");
04114                         if (!$fh)
04115                         {
04116                                 $this->ilias->raiseError($this->lng->txt("import_error_opening_file"),$this->ilias->error_obj->MESSAGE);
04117                                 $error = 1;
04118                                 return $error;
04119                         }
04120                         $xml = fread($fh, filesize($importfile));
04121                         $result = fclose($fh);
04122                         unlink($importfile);
04123                         if (!$result)
04124                         {
04125                                 $this->ilias->raiseError($this->lng->txt("import_error_closing_file"),$this->ilias->error_obj->MESSAGE);
04126                                 $error = 1;
04127                                 return $error;
04128                         }
04129                         if (preg_match("/(<survey[^>]*>.*?<\/survey>)/si", $xml, $matches))
04130                         {
04131                                 // read survey properties
04132                                 $import_results = $this->from_xml($matches[1]);
04133                                 if ($import_results === false)
04134                                 {
04135                                         $this->ilias->raiseError($this->lng->txt("import_error_survey_no_proper_values"),$this->ilias->error_obj->MESSAGE);
04136                                         $error = 1;
04137                                         return $error;
04138                                 }
04139                         }
04140                         else
04141                         {
04142                                 $this->ilias->raiseError($this->lng->txt("import_error_survey_no_properties"),$this->ilias->error_obj->MESSAGE);
04143                                 $error = 1;
04144                                 return $error;
04145                         }
04146                         $question_counter = 0;
04147                         $new_question_ids = array();
04148                         if (preg_match_all("/(<item[^>]*>.*?<\/item>)/si", $xml, $matches))
04149                         {
04150                                 foreach ($matches[1] as $index => $item)
04151                                 {
04152                                         $question = "";
04153                                         if (preg_match("/<qticomment>Questiontype\=(.*?)<\/qticomment>/is", $item, $questiontype))
04154                                         {
04155                                                 switch ($questiontype[1])
04156                                                 {
04157                                                         case NOMINAL_QUESTION_IDENTIFIER:
04158                                                                 $question = new SurveyNominalQuestion();
04159                                                                 break;
04160                                                         case ORDINAL_QUESTION_IDENTIFIER:
04161                                                                 $question = new SurveyOrdinalQuestion();
04162                                                                 break;
04163                                                         case METRIC_QUESTION_IDENTIFIER:
04164                                                                 $question = new SurveyMetricQuestion();
04165                                                                 break;
04166                                                         case TEXT_QUESTION_IDENTIFIER:
04167                                                                 $question = new SurveyTextQuestion();
04168                                                                 break;
04169                                                 }
04170                                                 if ($question)
04171                                                 {
04172                                                         $question->from_xml("<questestinterop>$item</questestinterop>");
04173                                                         if ($import_results !== false)
04174                                                         {
04175                                                                 $question->setObjId($survey_questionpool_id);
04176                                                                 $question->saveToDb();
04177                                                                 $question_id = $question->duplicate(true);
04178                                                                 $this->questions[$question_counter++] = $question_id;
04179                                                                 if (preg_match("/<item\s+ident\=\"(\d+)\"/", $item, $matches))
04180                                                                 {
04181                                                                         $original_question_id = $matches[1];
04182                                                                         $new_question_ids[$original_question_id] = $question_id;
04183                                                                 }
04184                                                         }
04185                                                         else
04186                                                         {
04187                                                                 $this->ilias->raiseError($this->lng->txt("error_importing_question"), $this->ilias->error_obj->MESSAGE);
04188                                                         }
04189                                                 }
04190                                         }
04191                                 }
04192                         }
04193 
04194                         $this->saveToDb();
04195                         // add question blocks
04196                         foreach ($import_results["questionblocks"] as $questionblock)
04197                         {
04198                                 foreach ($questionblock["questions"] as $key => $value)
04199                                 {
04200                                         $questionblock["questions"][$key] = $new_question_ids[$value];
04201                                 }
04202                                 $this->createQuestionblock($questionblock["title"], $questionblock["questions"]);
04203                         }
04204                         // add constraints
04205                         $relations = $this->getAllRelations(true);
04206                         foreach ($import_results["constraints"] as $constraint)
04207                         {
04208                                 $this->addConstraint($new_question_ids[$constraint["for"]], $new_question_ids[$constraint["question"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
04209                         }
04210                         foreach ($import_results["headings"] as $qid => $heading)
04211                         {
04212                                 $this->saveHeading($heading, $new_question_ids[$qid]);
04213                         }
04214                 }
04215                 return $error;
04216         }
04217 
04226         function from_xml($xml_text)
04227         {
04228                 $result = false;
04229                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
04230                 $domxml = domxml_open_mem($xml_text);
04231                 $constraints = array();
04232                 $headings = array();
04233                 $questionblocks = array();
04234                 if (!empty($domxml))
04235                 {
04236                         $root = $domxml->document_element();
04237                         $this->setTitle($root->get_attribute("title"));
04238                         $item = $root;
04239                         $itemnodes = $item->child_nodes();
04240                         foreach ($itemnodes as $index => $node)
04241                         {
04242                                 switch ($node->node_name())
04243                                 {
04244                                         case "qticomment":
04245                                                 $comment = $node->get_content();
04246                                                 if (strpos($comment, "ILIAS Version=") !== false)
04247                                                 {
04248                                                 }
04249                                                 elseif (strpos($comment, "Questiontype=") !== false)
04250                                                 {
04251                                                 }
04252                                                 elseif (strpos($comment, "Author=") !== false)
04253                                                 {
04254                                                         $comment = str_replace("Author=", "", $comment);
04255                                                         $this->setAuthor($comment);
04256                                                 }
04257                                                 else
04258                                                 {
04259                                                         $this->setDescription($comment);
04260                                                 }
04261                                                 break;
04262                                         case "objectives":
04263                                                 $material = $node->first_child();
04264                                                 if (strcmp($material->get_attribute("label"), "introduction") == 0)
04265                                                 {
04266                                                         $mattext = $material->first_child();
04267                                                         $this->setIntroduction($mattext->get_content());
04268                                                 }
04269                                                 break;
04270                                         case "qtimetadata":
04271                                                 $metadata_fields = $node->child_nodes();
04272                                                 foreach ($metadata_fields as $index => $metadata_field)
04273                                                 {
04274                                                         $fieldlabel = $metadata_field->first_child();
04275                                                         $fieldentry = $fieldlabel->next_sibling();
04276                                                         switch ($fieldlabel->get_content())
04277                                                         {
04278                                                                 case "evaluation_access":
04279                                                                         $this->setEvaluationAccess($fieldentry->get_content());
04280                                                                         break;
04281                                                                 case "author":
04282                                                                         $this->setAuthor($fieldentry->get_content());
04283                                                                         break;
04284                                                                 case "description":
04285                                                                         $this->setDescription($fieldentry->get_content());
04286                                                                         break;
04287                                                                 case "anonymize":
04288                                                                         $this->setAnonymize($fieldentry->get_content());
04289                                                                         break;
04290                                                                 case "startdate":
04291                                                                         $iso8601period = $fieldentry->get_content();
04292                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04293                                                                         {
04294                                                                                 $this->setStartDateEnabled(true);
04295                                                                                 $this->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04296                                                                         }
04297                                                                         break;
04298                                                                 case "enddate":
04299                                                                         $iso8601period = $fieldentry->get_content();
04300                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04301                                                                         {
04302                                                                                 $this->setEndDateEnabled(true);
04303                                                                                 $this->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04304                                                                         }
04305                                                                         break;
04306                                                                 case "status":
04307                                                                         $this->setStatus($fieldentry->get_content());
04308                                                                         break;
04309                                                                 case "display_question_titles":
04310                                                                         if ($fieldentry->get_content() == QUESTIONTITLES_HIDDEN)
04311                                                                         {
04312                                                                                 $this->hideQuestionTitles();
04313                                                                         }
04314                                                                         else
04315                                                                         {
04316                                                                                 $this->showQuestionTitles();
04317                                                                         }
04318                                                         }
04319                                                         if (preg_match("/questionblock_\d+/", $fieldlabel->get_content()))
04320                                                         {
04321                                                                 $qb = $fieldentry->get_content();
04322                                                                 preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
04323                                                                 $qb_title = $matches[1];
04324                                                                 preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
04325                                                                 $qb_questions = $matches[1];
04326                                                                 $qb_questions_array = explode(",", $qb_questions);
04327                                                                 array_push($questionblocks, array(
04328                                                                         "title" => $qb_title,
04329                                                                         "questions" => $qb_questions_array
04330                                                                 ));
04331                                                         }
04332                                                         if (preg_match("/constraint_(\d+)/", $fieldlabel->get_content(), $matches))
04333                                                         {
04334                                                                 $constraint = $fieldentry->get_content();
04335                                                                 $constraint_array = explode(",", $constraint);
04336                                                                 if (count($constraint_array) == 3)
04337                                                                 {
04338                                                                         array_push($constraints, array(
04339                                                                                 "for"      => $matches[1], 
04340                                                                                 "question" => $constraint_array[0],
04341                                                                                 "relation" => $constraint_array[1],
04342                                                                                 "value"    => $constraint_array[2]
04343                                                                         ));
04344                                                                 }
04345                                                         }
04346                                                         if (preg_match("/heading_(\d+)/", $fieldlabel->get_content(), $matches))
04347                                                         {
04348                                                                 $heading = $fieldentry->get_content();
04349                                                                 $headings[$matches[1]] = $heading;
04350                                                         }
04351                                                 }
04352                                                 break;
04353                                 }
04354                         }
04355                         $result["questionblocks"] = $questionblocks;
04356                         $result["constraints"] = $constraints;
04357                         $result["headings"] = $headings;
04358                 }
04359                 return $result;
04360         }
04361         
04365         function updateTitleAndDescription()
04366         {
04367                 $this->initMeta();
04368                 $this->meta_data->updateTitleAndDescription($this->getTitle(), $this->getDescription());
04369         }
04370 
04374         function updateMetaData()
04375         {
04376                 $this->initMeta();
04377                 $this->meta_data->update();
04378                 if ($this->meta_data->section != "General")
04379                 {
04380                         $meta = $this->meta_data->getElement("Title", "General");
04381                         $this->meta_data->setTitle($meta[0]["value"]);
04382                         $meta = $this->meta_data->getElement("Description", "General");
04383                         $this->meta_data->setDescription($meta[0]["value"]);
04384                 }
04385                 else
04386                 {
04387                         $this->setTitle($this->meta_data->getTitle());
04388                         $this->setDescription($this->meta_data->getDescription());
04389                 }
04390                 parent::update();
04391         }
04392 
04401         function &_getAvailableSurveys($use_object_id = false)
04402         {
04403                 global $rbacsystem;
04404                 global $ilDB;
04405                 
04406                 $result_array = array();
04407                 $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";
04408                 $result = $ilDB->query($query);
04409                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
04410                 {               
04411                         if ($rbacsystem->checkAccess("write", $row->ref_id) && (ilObject::_hasUntrashedReference($row->obj_id)))
04412                         {
04413                                 if ($use_object_id)
04414                                 {
04415                                         $result_array[$row->obj_id] = $row->title;
04416                                 }
04417                                 else
04418                                 {
04419                                         $result_array[$row->ref_id] = $row->title;
04420                                 }
04421                         }
04422                 }
04423                 return $result_array;
04424         }
04425 
04433         function _clone($obj_id)
04434         {
04435                 global $ilDB;
04436                 
04437                 $original = new ilObjSurvey($obj_id, false);
04438                 $original->loadFromDb();
04439                 
04440                 $newObj = new ilObjSurvey();
04441                 $newObj->setType("svy");
04442                 $newObj->setTitle($original->getTitle());
04443                 $newObj->setDescription($original->getDescription());
04444                 $newObj->create(true);
04445                 $newObj->createReference();
04446                 $newObj->putInTree($_GET["ref_id"]);
04447                 $newObj->setPermissions($_GET["ref_id"]);
04448 //              $newObj->notify("new",$_GET["ref_id"],$_GET["parent_non_rbac_id"],$_GET["ref_id"],$newObj->getRefId());
04449                 
04450                 $newObj->$author = $original->getAuthor();
04451                 $newObj->introduction = $original->getIntroduction();
04452                 $newObj->status = $original->getStatus();
04453                 $newObj->evaluation_access = $original->getEvaluationAccess();
04454                 $newObj->start_date = $original->getStartDate();
04455                 $newObj->startdate_enabled = $original->getStartDateEnabled();
04456                 $newObj->end_date = $original->getEndDate();
04457                 $newObj->enddate_enabled = $original->getEndDateEnabled();
04458                 $newObj->invitation = $original->getInvitation();
04459                 $newObj->invitation_mode = $original->getInvitationMode();
04460                 $newObj->anonymize = $original->getAnonymize();
04461 
04462                 $question_pointer = array();
04463                 // clone the questions
04464                 foreach ($original->questions as $key => $question_id)
04465                 {
04466                         $question = ilObjSurvey::_instanciateQuestion($question_id);
04467                         $question->id = -1;
04468                         $original_id = SurveyQuestion::_getOriginalId($question_id);
04469                         $question->saveToDb($original_id);
04470                         $newObj->questions[$key] = $question->getId();
04471                         $question_pointer[$question_id] = $question->getId();
04472                 }
04473 
04474                 $newObj->saveToDb();            
04475 
04476                 // clone the questionblocks
04477                 $questionblocks = array();
04478                 $questionblock_questions = array();
04479                 $query = sprintf("SELECT * FROM survey_questionblock_question WHERE survey_fi = %s",
04480                         $this->ilias->db->quote($original->getSurveyId() . "")
04481                 );
04482                 $result = $this->ilias->db->query($query);
04483                 if ($result->numRows() > 0)
04484                 {
04485                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04486                         {
04487                                 array_push($questionblock_questions, $row);
04488                                 $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
04489                         }
04490                 }
04491                 // create new questionblocks
04492                 foreach ($questionblocks as $key => $value)
04493                 {
04494                         $questionblock = ilObjSurvey::_getQuestionblock($key);
04495                         $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
04496                         $questionblocks[$key] = $questionblock_id;
04497                 }
04498                 // create new questionblock questions
04499                 foreach ($questionblock_questions as $key => $value)
04500                 {
04501                         $clonequery = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
04502                                 $ilDB->quote($newObj->getSurveyId() . ""),
04503                                 $ilDB->quote($questionblocks[$value["questionblock_fi"]] . ""),
04504                                 $ilDB->quote($question_pointer[$value["question_fi"]] . "")
04505                         );
04506                         $cloneresult = $this->ilias->db->query($clonequery);
04507                 }
04508                 
04509                 // clone the constraints
04510                 $constraints = ilObjSurvey::_getConstraints($original->getSurveyId());
04511                 foreach ($constraints as $key => $constraint)
04512                 {
04513                         $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
04514                 }
04515                 
04516                 // clone the obligatory states
04517                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
04518                         $this->ilias->db->quote($original->getSurveyId() . "")
04519                 );
04520                 $result = $this->ilias->db->query($query);
04521                 if ($result->numRows() > 0)
04522                 {
04523                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04524                         {
04525                                 $clonequery = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
04526                                         $this->ilias->db->quote($newObj->getSurveyId() . ""),
04527                                         $this->ilias->db->quote($question_pointer[$row["question_fi"]] . ""),
04528                                         $this->ilias->db->quote($row["obligatory"])
04529                                 );
04530                                 $cloneresult = $this->ilias->db->query($clonequery);
04531                         }
04532                 }
04533 
04534                 // clone meta data
04535                 $meta_data =& new ilMetaData($original->getType(), $original->getId());
04536                 include_once("./classes/class.ilNestedSetXML.php");
04537                 $nested = new ilNestedSetXML();
04538                 $nested->dom = domxml_open_mem($meta_data->nested_obj->dom->dump_mem(0));
04539                 $nodes = $nested->getDomContent("//MetaData/General", "Identifier");
04540                 if (is_array($nodes))
04541                 {
04542                         $nodes[0]["Entry"] = "il__" . $newObj->getType() . "_" . $newObj->getId();
04543                         $nested->updateDomContent("//MetaData/General", "Identifier", 0, $nodes[0]);
04544                 }
04545                 $xml = $nested->dom->dump_mem(0);
04546                 $nested->import($xml, $newObj->getId(), $newObj->getType());
04547         }
04548         
04554         function createExportDirectory()
04555         {
04556                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04557                 ilUtil::makeDir($svy_data_dir);
04558                 if(!is_writable($svy_data_dir))
04559                 {
04560                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04561                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04562                 }
04563                 
04564                 // create learning module directory (data_dir/lm_data/lm_<id>)
04565                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04566                 ilUtil::makeDir($svy_dir);
04567                 if(!@is_dir($svy_dir))
04568                 {
04569                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04570                 }
04571                 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
04572                 $export_dir = $svy_dir."/export";
04573                 ilUtil::makeDir($export_dir);
04574                 if(!@is_dir($export_dir))
04575                 {
04576                         $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
04577                 }
04578         }
04579 
04583         function getExportDirectory()
04584         {
04585                 $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
04586 
04587                 return $export_dir;
04588         }
04589         
04593         function getExportFiles($dir)
04594         {
04595                 // quit if import dir not available
04596                 if (!@is_dir($dir) or
04597                         !is_writeable($dir))
04598                 {
04599                         return array();
04600                 }
04601 
04602                 // open directory
04603                 $dir = dir($dir);
04604 
04605                 // initialize array
04606                 $file = array();
04607 
04608                 // get files and save the in the array
04609                 while ($entry = $dir->read())
04610                 {
04611                         if ($entry != "." and
04612                                 $entry != ".." and
04613                                 substr($entry, -4) == ".xml" and
04614                                 ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml\$", $entry))
04615                         {
04616                                 $file[] = $entry;
04617                         }
04618                 }
04619 
04620                 // close import directory
04621                 $dir->close();
04622                 // sort files
04623                 sort ($file);
04624                 reset ($file);
04625 
04626                 return $file;
04627         }
04628 
04634         function createImportDirectory()
04635         {
04636                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04637                 ilUtil::makeDir($svy_data_dir);
04638                 
04639                 if(!is_writable($svy_data_dir))
04640                 {
04641                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04642                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04643                 }
04644 
04645                 // create test directory (data_dir/svy_data/svy_<id>)
04646                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04647                 ilUtil::makeDir($svy_dir);
04648                 if(!@is_dir($svy_dir))
04649                 {
04650                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04651                 }
04652 
04653                 // create import subdirectory (data_dir/svy_data/svy_<id>/import)
04654                 $import_dir = $svy_dir."/import";
04655                 ilUtil::makeDir($import_dir);
04656                 if(!@is_dir($import_dir))
04657                 {
04658                         $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
04659                 }
04660         }
04661 
04665         function getImportDirectory()
04666         {
04667                 $import_dir = ilUtil::getDataDir()."/svy_data".
04668                         "/svy_".$this->getId()."/import";
04669                 if (!is_dir($import_dir))
04670                 {
04671                         ilUtil::makeDirParents($import_dir);
04672                 }
04673                 if(@is_dir($import_dir))
04674                 {
04675                         return $import_dir;
04676                 }
04677                 else
04678                 {
04679                         return false;
04680                 }
04681         }
04682         
04683         function saveHeading($heading = "", $insertbefore)
04684         {
04685                 if ($heading)
04686                 {
04687                         $query = sprintf("UPDATE survey_survey_question SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
04688                                 $this->ilias->db->quote($heading),
04689                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04690                                 $this->ilias->db->quote($insertbefore)
04691                         );
04692                 }
04693                 else
04694                 {
04695                         $query = sprintf("UPDATE survey_survey_question SET heading=NULL WHERE survey_fi=%s AND question_fi=%s",
04696                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04697                                 $this->ilias->db->quote($insertbefore)
04698                         );
04699                 }
04700                 $this->ilias->db->query($query);
04701         }
04702 
04703         function _getRefIdFromObjId($obj_id)
04704         {
04705                 global $ilDB;
04706                 
04707                 $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
04708                         $ilDB->quote($obj_id)
04709                         
04710                 );
04711                 $result = $ilDB->query($query);
04712                 if ($result->numRows())
04713                 {
04714                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04715                         return $row["ref_id"];
04716                 }
04717                 return 0;
04718         }
04719         
04720         function isAnonymousKey($key)
04721         {
04722                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_key = %s AND survey_fi = %s",
04723                         $this->ilias->db->quote($key . ""),
04724                         $this->ilias->db->quote($this->getSurveyId() . "")
04725                 );
04726                 $result = $this->ilias->db->query($query);
04727                 if ($result->numRows() == 1)
04728                 {
04729                         return true;
04730                 }
04731                 else
04732                 {
04733                         return false;
04734                 }
04735         }
04736         
04737         function getUserSurveyCode()
04738         {
04739                 global $ilUser;
04740                 return md5($ilUser->id . $this->getSurveyId());
04741         }
04742         
04743         function checkSurveyCode($code)
04744         {
04745                 global $ilUser;
04746                 // check for the correct survey code
04747                 if (strcmp($ilUser->login, "anonymous") != 0)
04748                 {
04749                         $anonymize_key = $this->getUserSurveyCode();
04750                         if (strcmp(strtolower($anonymize_key), strtolower($code)) == 0)
04751                         {
04752                                 return true;
04753                         }
04754                         else
04755                         {
04756                                 return false;
04757                         }
04758                 }
04759                 else
04760                 {
04761                         if ($this->isAnonymousKey($code))
04762                         {
04763                                 if ($this->isSurveyStarted("", $code) == 1)
04764                                 {
04765                                         return false;
04766                                 }
04767                                 else
04768                                 {
04769                                         return true;
04770                                 }
04771                         }
04772                         else
04773                         {
04774                                 return false;
04775                         }
04776                 }
04777                 return false;
04778         }
04779 
04780         function &getSurveyCodes()
04781         {
04782                 $codes = array();
04783                 $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",
04784                         $this->ilias->db->quote($this->getSurveyId() . "")
04785                 );
04786                 $result = $this->ilias->db->query($query);
04787                 
04788                 if ($result->numRows() > 0)
04789                 {
04790                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04791                         {
04792                                 array_push($codes, $row);
04793                         }
04794                 }
04795                 return $codes;
04796         }
04797         
04798         function isSurveyCodeUsed($code)
04799         {
04800                 $query = sprintf("SELECT answer_id FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s",
04801                         $this->ilias->db->quote($this->getSurveyId() . ""),
04802                         $this->ilias->db->quote($code)
04803                 );
04804                 $result = $this->ilias->db->query($query);
04805                 if ($result->numRows() > 0)
04806                 {
04807                         return TRUE;
04808                 }
04809                 else
04810                 {
04811                         return FALSE;
04812                 }
04813         }
04814         
04815         function createSurveyCodes($nrOfCodes)
04816         {
04817                 for ($i = 0; $i < $nrOfCodes; $i++)
04818                 {
04819                         $anonymize_key = md5((time() + ($i*$nrOfCodes)) . $this->getSurveyId());
04820                         $query = sprintf("INSERT INTO survey_anonymous (anonymous_id, survey_key, survey_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
04821                                 $this->ilias->db->quote($anonymize_key . ""),
04822                                 $this->ilias->db->quote($this->getSurveyId() . "")
04823                         );
04824                         $result = $this->ilias->db->query($query);
04825                 }
04826         }
04827         
04833         function _goto($a_target, $a_access_code = "")
04834         {
04835                 global $rbacsystem, $ilErr, $lng;
04836 
04837                 include_once 'classes/class.ilSearch.php';
04838                         
04839                 // Added this additional check (ParentConditions) to avoid calls of objects inside e.g courses.
04840                 // Will be replaced in future releases by ilAccess::checkAccess()
04841                 if ($rbacsystem->checkAccess("read", $a_target) and ilSearch::_checkParentConditions($a_target))
04842                 {
04843                         if (strlen($a_access_code))
04844                         {
04845                                 ilUtil::redirect("survey/survey.php?cmd=run&ref_id=$a_target&accesscode=$a_access_code");
04846                         }
04847                         else
04848                         {
04849                                 ilUtil::redirect("survey/survey.php?cmd=run&ref_id=$a_target");
04850                         }
04851                 }
04852                 else
04853                 {
04854                         $ilErr->raiseError($lng->txt("msg_no_perm_read_lm"), $ilErr->FATAL);
04855                 }
04856         }
04857 
04870         function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
04871         {
04872                 $resultarray = array();
04873                 foreach ($row as $rowindex => $entry)
04874                 {
04875                         $surround = FALSE;
04876                         if ($quoteAll)
04877                         {
04878                                 $surround = TRUE;
04879                         }
04880                         if (strpos($entry, "\"") !== FALSE)
04881                         {
04882                                 $entry = str_replace("\"", "\"\"", $entry);
04883                                 $surround = TRUE;
04884                         }
04885                         if (strpos($entry, $separator) !== FALSE)
04886                         {
04887                                 $surround = TRUE;
04888                         }
04889                         // replace all CR LF with LF (for Excel for Windows compatibility
04890                         $entry = str_replace(chr(13).chr(10), chr(10), $entry);
04891                         if ($surround)
04892                         {
04893                                 $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
04894                         }
04895                         else
04896                         {
04897                                 $resultarray[$rowindex] = utf8_decode($entry);
04898                         }
04899                 }
04900                 return $resultarray;
04901         }
04902 } // END class.ilObjSurvey
04903 ?>

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