• Main Page
  • Related Pages
  • Modules
  • 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-2006 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 include_once "./classes/class.ilObject.php";
00036 include_once "./survey/classes/inc.SurveyConstants.php";
00037 
00038 class ilObjSurvey extends ilObject
00039 {
00048   var $survey_id;
00049 
00058   var $author;
00059 
00067   var $introduction;
00068 
00076   var $outro;
00077 
00085   var $status;
00086 
00094   var $evaluation_access;
00095 
00103   var $start_date;
00104 
00112         var $startdate_enabled;
00113 
00121   var $end_date;
00122 
00130         var $enddate_enabled;
00131 
00139         var $questions;
00140 
00148         var $invitation;
00149 
00157         var $invitation_mode;
00158         
00165         var $anonymize;
00166 
00173         var $display_question_titles;
00174 
00181         function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
00182         {
00183                 global $ilUser;
00184                 $this->type = "svy";
00185                 $this->ilObject($a_id,$a_call_by_reference);
00186 
00187                 $this->survey_id = -1;
00188                 $this->introduction = "";
00189                 $this->outro = $this->lng->txt("survey_finished");
00190                 $this->author = $ilUser->fullname;
00191                 $this->status = STATUS_OFFLINE;
00192                 $this->evaluation_access = EVALUATION_ACCESS_OFF;
00193                 $this->startdate_enabled = 0;
00194                 $this->enddate_enabled = 0;
00195                 $this->questions = array();
00196                 $this->invitation = INVITATION_OFF;
00197                 $this->invitation_mode = MODE_PREDEFINED_USERS;
00198                 $this->anonymize = ANONYMIZE_OFF;
00199                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
00200         }
00201 
00205         function create($a_upload = false)
00206         {
00207                 parent::create();
00208                 $this->createMetaData();
00209         }
00210 
00217         function update()
00218         {
00219                 $this->updateMetaData();
00220 
00221                 if (!parent::update())
00222                 {
00223                         return false;
00224                 }
00225 
00226                 // put here object specific stuff
00227 
00228                 return true;
00229         }
00230 
00231         function createReference() 
00232         {
00233                 $result = parent::createReference();
00234                 $this->saveToDb();
00235                 return $result;
00236         }
00237 
00243         function read($a_force_db = false)
00244         {
00245                 parent::read($a_force_db);
00246                 $this->loadFromDb();
00247         }
00248 
00256         function ilClone($a_parent_ref)
00257         {               
00258                 global $rbacadmin;
00259 
00260                 // always call parent ilClone function first!!
00261                 $new_ref_id = parent::ilClone($a_parent_ref);
00262                 
00263                 // get object instance of ilCloned object
00264                 //$newObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00265 
00266                 // create a local role folder & default roles
00267                 //$roles = $newObj->initDefaultRoles();
00268 
00269                 // ...finally assign role to creator of object
00270                 //$rbacadmin->assignUser($roles[0], $newObj->getOwner(), "n");          
00271 
00272                 // always destroy objects in ilClone method because ilClone() is recursive and creates instances for each object in subtree!
00273                 //unset($newObj);
00274 
00275                 // ... and finally always return new reference ID!!
00276                 return $new_ref_id;
00277         }
00278 
00285         function delete()
00286         {
00287                 $remove = parent::delete();
00288                 // always call parent delete function first!!
00289                 if (!$remove)
00290                 {
00291                         return false;
00292                 }
00293 
00294                 $this->deleteMetaData();
00295 
00296                 // Delete all survey questions, constraints and materials
00297                 foreach ($this->questions as $question_id)
00298                 {
00299                         $this->removeQuestion($question_id);
00300                 }
00301                 $this->deleteSurveyRecord();
00302                 
00303                 return true;
00304         }
00305         
00313         function deleteSurveyRecord()
00314         {
00315                 global $ilDB;
00316                 
00317                 $query = sprintf("DELETE FROM survey_survey WHERE survey_id = %s",
00318                         $ilDB->quote($this->getSurveyId())
00319                 );
00320                 $result = $ilDB->query($query);
00321 
00322                 $query = sprintf("SELECT questionblock_fi FROM survey_questionblock_question WHERE survey_fi = %s",
00323                         $ilDB->quote($this->getSurveyId())
00324                 );
00325                 $result = $ilDB->query($query);
00326                 $questionblocks = array();
00327                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00328                 {
00329                         array_push($questionblocks, $row["questionblock_fi"]);
00330                 }
00331                 if (count($questionblocks))
00332                 {
00333                         $query = "DELETE FROM survey_questionblock WHERE questionblock_id IN (" . join($questionblocks, ",") . ")";
00334                         $result = $ilDB->query($query);
00335                 }
00336                 $query = sprintf("DELETE FROM survey_questionblock_question WHERE survey_fi = %s",
00337                         $ilDB->quote($this->getSurveyId())
00338                 );
00339                 $result = $ilDB->query($query);
00340                 
00341                 $this->deleteAllUserData();
00342 
00343                 // delete export files
00344                 include_once "./classes/class.ilUtil.php";
00345                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
00346                 $directory = $svy_data_dir."/svy_".$this->getId();
00347                 if (is_dir($directory))
00348                 {
00349                         include_once "./classes/class.ilUtil.php";
00350                         ilUtil::delDir($directory);
00351                 }
00352 
00353                 include_once("./content/classes/Media/class.ilObjMediaObject.php");
00354                 $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
00355                 // remaining usages are not in text anymore -> delete them
00356                 // and media objects (note: delete method of ilObjMediaObject
00357                 // checks whether object is used in another context; if yes,
00358                 // the object is not deleted!)
00359                 foreach($mobs as $mob)
00360                 {
00361                         ilObjMediaObject::_removeUsage($mob, "svy:html", $this->getId());
00362                         $mob_obj =& new ilObjMediaObject($mob);
00363                         $mob_obj->delete();
00364                 }
00365         }
00366         
00374         function deleteAllUserData()
00375         {
00376                 global $ilDB, $ilLog;
00377                 
00378                 $query = sprintf("DELETE FROM survey_finished WHERE survey_fi = %s",
00379                         $ilDB->quote($this->getSurveyId())
00380                 );
00381                 $result = $ilDB->query($query);
00382                 if ($result != DB_OK)
00383                 {
00384                         $ilLog->write("SURVEY MAINTENANCE ERROR: $query failed!");
00385                 }
00386 
00387                 $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s",
00388                         $ilDB->quote($this->getSurveyId())
00389                 );
00390                 $result = $ilDB->query($query);
00391                 if ($result != DB_OK)
00392                 {
00393                         $ilLog->write("SURVEY MAINTENANCE ERROR: $query failed!");
00394                 }
00395         }
00396         
00404         function removeSelectedSurveyResults($finished_ids)
00405         {
00406                 global $ilDB;
00407                 
00408                 foreach ($finished_ids as $finished_id)
00409                 {
00410                         $query = sprintf("SELECT * FROM survey_finished WHERE finished_id = %s",
00411                                 $ilDB->quote($finished_id . "")
00412                         );
00413                         $result = $ilDB->query($query);
00414                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00415 
00416                         if (strlen($row["anonymous_id"]) > 0)
00417                         {
00418                                 $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s",
00419                                         $ilDB->quote($this->getSurveyId()),
00420                                         $ilDB->quote($row["anonymous_id"] . "")
00421                                 );
00422                         }
00423                         else
00424                         {
00425                                 $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND user_fi = %s AND anonymous_id = %s",
00426                                         $ilDB->quote($this->getSurveyId()),
00427                                         $ilDB->quote($row["user_fi"] . ""),
00428                                         $ilDB->quote($row["anonymous_id"] . "")
00429                                 );
00430                         }
00431                         $result = $ilDB->query($query);
00432 
00433                         $query = sprintf("DELETE FROM survey_finished WHERE finished_id = %s",
00434                                 $ilDB->quote($finished_id . "")
00435                         );
00436                         $result = $ilDB->query($query);
00437                 }
00438         }
00439         
00440         function &getSurveyParticipants()
00441         {
00442                 global $ilDB;
00443                 
00444                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s ORDER BY user_fi",
00445                         $ilDB->quote($this->getSurveyId() . "")
00446                 );
00447                 $result = $ilDB->query($query);
00448                 $participants = array();
00449                 if ($result->numRows() > 0)
00450                 {
00451                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00452                         {
00453                                 if (($row["user_fi"] > 0) && ($row["user_fi"] != ANONYMOUS_USER_ID) && (!strlen($row["anonymous_id"])))
00454                                 {
00455                                         $uname = ilObjUser::_lookupName($row["user_fi"]);
00456                                         if (strlen($uname["user_id"]))
00457                                         {
00458                                                 $login = ilObjUser::_lookupLogin($row["user_fi"]);
00459                                                 $participants[$row["finished_id"]] = array("name" => $uname["lastname"] . ", " . $uname["firstname"], "login" => $login);
00460                                         }
00461                                         else
00462                                         {
00463                                                 $participants[$row["finished_id"]] = array("name" => $this->lng->txt("deleted_user"));
00464                                         }
00465                                 }
00466                                 else
00467                                 {
00468                                         if (strlen($row["anonymous_id"]) > 0)
00469                                         {
00470                                                 $participants[$row["finished_id"]] = array("name" => $this->lng->txt("anonymous"));
00471                                         }
00472                                 }
00473                         }
00474                 }
00475                 return $participants;
00476         }
00477 
00491         function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
00492         {
00493                 global $tree;
00494                 
00495                 switch ($a_event)
00496                 {
00497                         case "link":
00498                                 
00499                                 //var_dump("<pre>",$a_params,"</pre>");
00500                                 //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
00501                                 //exit;
00502                                 break;
00503                         
00504                         case "cut":
00505                                 
00506                                 //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
00507                                 //exit;
00508                                 break;
00509                                 
00510                         case "copy":
00511                         
00512                                 //var_dump("<pre>",$a_params,"</pre>");
00513                                 //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
00514                                 //exit;
00515                                 break;
00516 
00517                         case "paste":
00518                                 
00519                                 //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
00520                                 //exit;
00521                                 break;
00522                         
00523                         case "new":
00524                                 
00525                                 //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
00526                                 //exit;
00527                                 break;
00528                 }
00529                 
00530                 // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
00531                 if ($a_node_id==$_GET["ref_id"])
00532                 {       
00533                         $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
00534                         $parent_type = $parent_obj->getType();
00535                         if($parent_type == $this->getType())
00536                         {
00537                                 $a_node_id = (int) $tree->getParentId($a_node_id);
00538                         }
00539                 }
00540                 
00541                 parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
00542         }
00543 
00552         function isComplete()
00553         {
00554                 if (($this->getTitle()) and ($this->author) and (count($this->questions)))
00555                 {
00556                         return true;
00557                 } 
00558                         else 
00559                 {
00560                         return false;
00561                 }
00562         }
00563 
00572         function _isComplete($obj_id)
00573         {
00574                 $survey = new ilObjSurvey($obj_id, false);
00575                 $survey->loadFromDb();
00576                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00577                 {
00578                         return true;
00579                 } 
00580                         else 
00581                 {
00582                         return false;
00583                 }
00584         }
00585 
00594         function &_getGlobalSurveyData($obj_id)
00595         {
00596                 $survey = new ilObjSurvey($obj_id, false);
00597                 $survey->loadFromDb();
00598                 $result = array();
00599                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00600                 {
00601                         $result["complete"] = true;
00602                 } 
00603                         else 
00604                 {
00605                         $result["complete"] = false;
00606                 }
00607                 $result["evaluation_access"] = $survey->getEvaluationAccess();
00608                 return $result;
00609         }
00610 
00618         function saveCompletionStatus() 
00619         {
00620                 global $ilDB;
00621                 
00622                 $complete = 0;
00623                 if ($this->isComplete()) 
00624                 {
00625                         $complete = 1;
00626                 }
00627     if ($this->survey_id > 0) 
00628                 {
00629                         $query = sprintf("UPDATE survey_survey SET complete = %s WHERE survey_id = %s",
00630                                 $ilDB->quote("$complete"),
00631                                 $ilDB->quote($this->survey_id) 
00632                         );
00633       $result = $ilDB->query($query);
00634                 }
00635         }
00636 
00646         function duplicateQuestionForSurvey($question_id)
00647         {
00648                 global $ilUser;
00649                 
00650                 $questiontype = $this->getQuestionType($question_id);
00651                 $question_gui = $this->getQuestionGUI($questiontype, $question_id);
00652                 $duplicate_id = $question_gui->object->duplicate(true);
00653                 return $duplicate_id;
00654         }
00655 
00663         function insertQuestion($question_id) 
00664         {
00665                 global $ilDB;
00666                 
00667                 include_once "./survey/classes/class.SurveyQuestion.php";
00668                 if (!SurveyQuestion::_isComplete($question_id))
00669                 {
00670                         return FALSE;
00671                 }
00672                 else
00673                 {
00674                         // get maximum sequence index in test
00675                         $query = sprintf("SELECT survey_question_id FROM survey_survey_question WHERE survey_fi = %s",
00676                                 $ilDB->quote($this->getSurveyId())
00677                         );
00678                         $result = $ilDB->query($query);
00679                         $sequence = $result->numRows();
00680                         $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
00681                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00682                                 $ilDB->quote($this->getSurveyId()),
00683                                 $ilDB->quote($duplicate_id),
00684                                 $ilDB->quote($sequence)
00685                         );
00686                         $result = $ilDB->query($query);
00687                         if ($result != DB_OK) 
00688                         {
00689                                 // Error
00690                         }
00691                         $this->loadQuestionsFromDb();
00692                         return TRUE;
00693                 }
00694         }
00695 
00696 
00697         
00705         function insertQuestionblock($questionblock_id) 
00706         {
00707                 global $ilDB;
00708                 $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",
00709                         $ilDB->quote($questionblock_id)
00710                 );
00711                 $result = $ilDB->query($query);
00712                 $questions = array();
00713                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00714                 {
00715                         array_push($questions, $row["question_id"]);
00716                         $title = $row["title"];
00717                 }
00718                 $this->createQuestionblock($title, $questions);
00719         }
00720         
00728   function saveToDb()
00729   {
00730                 global $ilDB;
00731                 $complete = 0;
00732                 if ($this->isComplete()) 
00733                 {
00734                         $complete = 1;
00735                 }
00736                 $startdate = $this->getStartDate();
00737                 if (!$startdate or !$this->startdate_enabled)
00738                 {
00739                         $startdate = "NULL";
00740                 }
00741                 else
00742                 {
00743                         $startdate = $ilDB->quote($startdate);
00744                 }
00745                 $enddate = $this->getEndDate();
00746                 if (!$enddate or !$this->enddate_enabled)
00747                 {
00748                         $enddate = "NULL";
00749                 }
00750                 else
00751                 {
00752                         $enddate = $ilDB->quote($enddate);
00753                 }
00754 
00755                 // cleanup RTE images which are not inserted into the question text
00756                 include_once("./Services/RTE/classes/class.ilRTE.php");
00757                 ilRTE::_cleanupMediaObjectUsage($this->introduction . $this->getOutro(), $this->getType() . ":html", $this->getId());
00758 
00759     if ($this->survey_id == -1) 
00760                 {
00761       // Write new dataset
00762       $now = getdate();
00763       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00764       $query = sprintf("INSERT INTO survey_survey (survey_id, obj_fi, author, introduction, outro, status, startdate, enddate, evaluation_access, invitation, invitation_mode, complete, created, anonymize, show_question_titles, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00765                                 $ilDB->quote($this->getId()),
00766                                 $ilDB->quote($this->author . ""),
00767                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->introduction, 0)),
00768                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
00769                                 $ilDB->quote($this->status . ""),
00770                                 $startdate,
00771                                 $enddate,
00772                                 $ilDB->quote($this->evaluation_access . ""),
00773                                 $ilDB->quote($this->invitation . ""),
00774                                 $ilDB->quote($this->invitation_mode . ""),
00775                                 $ilDB->quote($complete . ""),
00776                                 $ilDB->quote($this->getAnonymize() . ""),
00777                                 $ilDB->quote($this->getShowQuestionTitles() . ""),
00778                                 $ilDB->quote($created)
00779       );
00780       $result = $ilDB->query($query);
00781       if ($result == DB_OK) 
00782                         {
00783         $this->survey_id = $ilDB->getLastInsertId();
00784       }
00785     } 
00786                 else 
00787                 {
00788       // update existing dataset
00789                         $query = sprintf("UPDATE survey_survey SET author = %s, introduction = %s, outro = %s, status = %s, startdate = %s, enddate = %s, evaluation_access = %s, invitation = %s, invitation_mode = %s, complete = %s, anonymize = %s, show_question_titles = %s WHERE survey_id = %s",
00790                                 $ilDB->quote($this->author . ""),
00791                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->introduction, 0)),
00792                                 $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
00793                                 $ilDB->quote($this->status . ""),
00794                                 $startdate,
00795                                 $enddate,
00796                                 $ilDB->quote($this->evaluation_access . ""),
00797                                 $ilDB->quote($this->invitation . ""),
00798                                 $ilDB->quote($this->invitation_mode . ""),
00799                                 $ilDB->quote($complete . ""),
00800                                 $ilDB->quote($this->getAnonymize() . ""),
00801                                 $ilDB->quote($this->getShowQuestionTitles() . ""),
00802                                 $ilDB->quote($this->survey_id)
00803       );
00804       $result = $ilDB->query($query);
00805     }
00806     if ($result == DB_OK) 
00807                 {
00808                         // save questions to db
00809                         $this->saveQuestionsToDb();
00810     }
00811   }
00812 
00821         function saveQuestionsToDb() 
00822         {
00823                 global $ilDB;
00824                 // save old questions state
00825                 $old_questions = array();
00826                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
00827                         $ilDB->quote($this->getSurveyId())
00828                 );
00829                 $result = $ilDB->query($query);
00830                 if ($result->numRows())
00831                 {
00832                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00833                         {
00834                                 $old_questions[$row["question_fi"]] = $row;
00835                         }
00836                 }
00837                 
00838                 // delete existing question relations
00839     $query = sprintf("DELETE FROM survey_survey_question WHERE survey_fi = %s",
00840                         $ilDB->quote($this->getSurveyId())
00841                 );
00842                 $result = $ilDB->query($query);
00843                 // create new question relations
00844                 foreach ($this->questions as $key => $value) 
00845                 {
00846                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, heading, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00847                                 $ilDB->quote($this->getSurveyId() . ""),
00848                                 $ilDB->quote($value . ""),
00849                                 $ilDB->quote($old_questions[$value]["heading"]),
00850                                 $ilDB->quote($key . "")
00851                         );
00852                         $result = $ilDB->query($query);
00853                 }
00854         }
00855 
00865         function getAnonymousId($id)
00866         {
00867                 global $ilDB;
00868                 $query = sprintf("SELECT anonymous_id FROM survey_answer WHERE anonymous_id = %s",
00869                         $ilDB->quote($id)
00870                 );
00871                 $result = $ilDB->query($query);
00872                 if ($result->numRows())
00873                 {
00874                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00875                         return $row["anonymous_id"];
00876                 }
00877                 else
00878                 {
00879                         return "";
00880                 }
00881         }
00882 
00891         function getQuestionGUI($questiontype, $question_id)
00892         {
00893                 $questiontypegui = $questiontype . "GUI";
00894                 include_once "./survey/classes/class.$questiontypegui.php";
00895                 $question = new $questiontypegui();
00896                 $question->object->loadFromDb($question_id);
00897                 return $question;
00898         }
00899         
00909   function getQuestionType($question_id) 
00910         {
00911                 global $ilDB;
00912     if ($question_id < 1) return -1;
00913     $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",
00914       $ilDB->quote($question_id)
00915     );
00916     $result = $ilDB->query($query);
00917     if ($result->numRows() == 1) 
00918                 {
00919       $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00920       return $data->type_tag;
00921     } 
00922                 else 
00923                 {
00924       return "";
00925     }
00926   }
00927         
00936         function getSurveyId()
00937         {
00938                 return $this->survey_id;
00939         }
00940         
00944         function setAnonymize($a_anonymize)
00945         {
00946                 $this->anonymize = $a_anonymize;
00947         }
00948 
00954         function getAnonymize()
00955         {
00956                 return $this->anonymize;
00957         }
00958         
00964         function isAccessibleWithoutCode()
00965         {
00966                 if ($this->getAnonymize() == ANONYMIZE_FREEACCESS)
00967                 {
00968                         return true;
00969                 }
00970                 else
00971                 {
00972                         return false;
00973                 }
00974         }
00975 
00983   function loadFromDb()
00984   {
00985                 global $ilDB;
00986     $query = sprintf("SELECT * FROM survey_survey WHERE obj_fi = %s",
00987       $ilDB->quote($this->getId())
00988     );
00989     $result = $ilDB->query($query);
00990     if (strcmp(strtolower(get_class($result)), db_result) == 0) 
00991                 {
00992       if ($result->numRows() == 1) 
00993                         {
00994                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00995                                 $this->survey_id = $data->survey_id;
00996                                 $this->author = $data->author;
00997                                 include_once("./Services/RTE/classes/class.ilRTE.php");
00998                                 $this->introduction = ilRTE::_replaceMediaObjectImageSrc($data->introduction, 1);
00999                                 if (strcmp($data->outro, "survey_finished") == 0)
01000                                 {
01001                                         $this->setOutro($this->lng->txt("survey_finished"));
01002                                 }
01003                                 else
01004                                 {
01005                                         $this->setOutro(ilRTE::_replaceMediaObjectImageSrc($data->outro, 1));
01006                                 }
01007                                 $this->status = $data->status;
01008                                 $this->invitation = $data->invitation;
01009                                 $this->invitation_mode = $data->invitation_mode;
01010                                 $this->display_question_titles = $data->show_question_titles;
01011                                 $this->start_date = $data->startdate;
01012                                 if (!$data->startdate)
01013                                 {
01014                                         $this->startdate_enabled = 0;
01015                                 }
01016                                 else
01017                                 {
01018                                         $this->startdate_enabled = 1;
01019                                 }
01020         $this->end_date = $data->enddate;
01021                                 if (!$data->enddate)
01022                                 {
01023                                         $this->enddate_enabled = 0;
01024                                 }
01025                                 else
01026                                 {
01027                                         $this->enddate_enabled = 1;
01028                                 }
01029                                 switch ($data->anonymize)
01030                                 {
01031                                         case ANONYMIZE_OFF:
01032                                         case ANONYMIZE_ON:
01033                                         case ANONYMIZE_FREEACCESS:
01034                                                 $this->setAnonymize($data->anonymize);
01035                                                 break;
01036                                         default:
01037                                                 $this->setAnonymize(ANONYMIZE_OFF);
01038                                                 break;
01039                                 }
01040         $this->evaluation_access = $data->evaluation_access;
01041                                 $this->loadQuestionsFromDb();
01042       }
01043     }
01044         }
01045 
01054         function loadQuestionsFromDb() 
01055         {
01056                 global $ilDB;
01057                 $this->questions = array();
01058                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
01059                         $ilDB->quote($this->survey_id)
01060                 );
01061                 $result = $ilDB->query($query);
01062                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
01063                 {
01064                         $this->questions[$data->sequence] = $data->question_fi;
01065                 }
01066         }
01067 
01077         function setStartDateEnabled($enabled = false)
01078         {
01079                 if ($enabled)
01080                 {
01081                         $this->startdate_enabled = 1;
01082                 }
01083                 else
01084                 {
01085                         $this->startdate_enabled = 0;
01086                 }
01087         }
01088         
01098         function getStartDateEnabled()
01099         {
01100                 return $this->startdate_enabled;
01101         }
01102 
01112         function setEndDateEnabled($enabled = false)
01113         {
01114                 if ($enabled)
01115                 {
01116                         $this->enddate_enabled = 1;
01117                 }
01118                 else
01119                 {
01120                         $this->enddate_enabled = 0;
01121                 }
01122         }
01123         
01133         function getEndDateEnabled()
01134         {
01135                 return $this->enddate_enabled;
01136         }
01137 
01147         function setAuthor($author = "") 
01148         {
01149     if (!$author) 
01150                 {
01151       $author = $this->ilias->account->fullname;
01152     }
01153     $this->author = $author;
01154   }
01155 
01165   function getShowQuestionTitles() 
01166         {
01167                 return $this->display_question_titles;
01168   }
01169 
01178   function showQuestionTitles() 
01179         {
01180                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
01181   }
01182 
01191   function hideQuestionTitles() 
01192         {
01193                 $this->display_question_titles = QUESTIONTITLES_HIDDEN;
01194   }
01195         
01205   function setInvitation($invitation = 0) 
01206         {
01207                 global $ilDB;
01208     $this->invitation = $invitation;
01209                 // remove the survey from the personal desktops
01210                 $query = sprintf("DELETE FROM desktop_item WHERE type = %s AND item_id = %s",
01211                         $ilDB->quote("svy"),
01212                         $ilDB->quote($this->getRefId())
01213                 );
01214                 $result = $ilDB->query($query);
01215                 if ($invitation == INVITATION_OFF)
01216                 {
01217                         // already removed prior
01218                 }
01219                 else if ($invitation == INVITATION_ON)
01220                 {
01221                         if ($this->getInvitationMode() == MODE_UNLIMITED)
01222                         {
01223                                 $query = "SELECT usr_id FROM usr_data";
01224                                 $result = $ilDB->query($query);
01225                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01226                                 {
01227                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01228                                                 $ilDB->quote($row["usr_id"]),
01229                                                 $ilDB->quote($this->getRefId()),
01230                                                 $ilDB->quote("svy")
01231                                         );
01232                                         $insertresult = $ilDB->query($query);
01233                                 }
01234                         }
01235                         else
01236                         {
01237                                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
01238                                         $ilDB->quote($this->getSurveyId())
01239                                 );
01240                                 $result = $ilDB->query($query);
01241                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01242                                 {
01243                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01244                                                 $ilDB->quote($row["user_fi"]),
01245                                                 $ilDB->quote($this->getRefId()),
01246                                                 $ilDB->quote("svy")
01247                                         );
01248                                         $insertresult = $ilDB->query($query);
01249                                 }
01250                                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
01251                                         $ilDB->quote($this->getSurveyId())
01252                                 );
01253                                 $result = $ilDB->query($query);
01254                                 include_once "./classes/class.ilObjGroup.php";
01255                                 include_once "./classes/class.ilObjUser.php";
01256                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01257                                 {
01258                                         $group = new ilObjGroup($row["group_fi"]);
01259                                         $members = $group->getGroupMemberIds();
01260                                         foreach ($members as $user_id)
01261                                         {
01262                                                 if (ilObjUser::_lookupLogin($user_id))
01263                                                 {
01264                                                         $user = new ilObjUser($user_id);
01265                                                         $user->addDesktopItem($this->getRefId(), "svy");
01266                                                 }
01267                                         }
01268                                 }
01269                         }
01270                 }
01271   }
01272 
01282   function setInvitationMode($invitation_mode = 0) 
01283         {
01284                 global $ilDB;
01285     $this->invitation_mode = $invitation_mode;
01286                 if ($invitation_mode == MODE_UNLIMITED)
01287                 {
01288                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01289                                 $ilDB->quote($this->getSurveyId())
01290                         );
01291                         $result = $ilDB->query($query);
01292                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01293                                 $ilDB->quote($this->getSurveyId())
01294                         );
01295                         $result = $ilDB->query($query);
01296                 }
01297                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01298                 $this->setInvitation($this->getInvitation());
01299   }
01300         
01311         function setInvitationAndMode($invitation = 0, $invitation_mode = 0)
01312         {
01313                 global $ilDB;
01314     $this->invitation_mode = $invitation_mode;
01315                 if ($invitation_mode == MODE_UNLIMITED)
01316                 {
01317                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01318                                 $ilDB->quote($this->getSurveyId())
01319                         );
01320                         $result = $ilDB->query($query);
01321                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01322                                 $ilDB->quote($this->getSurveyId())
01323                         );
01324                         $result = $ilDB->query($query);
01325                 }
01326                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01327                 $this->setInvitation($invitation);
01328         }
01329 
01339   function setIntroduction($introduction = "") 
01340         {
01341     $this->introduction = $introduction;
01342   }
01343 
01353   function setOutro($outro = "") 
01354         {
01355     $this->outro = $outro;
01356   }
01357 
01367   function getAuthor() 
01368         {
01369     return $this->author;
01370   }
01371 
01381   function getInvitation() 
01382         {
01383     return $this->invitation;
01384   }
01385 
01395   function getInvitationMode() 
01396         {
01397     return $this->invitation_mode;
01398   }
01399 
01409   function getStatus() 
01410         {
01411     return $this->status;
01412   }
01413 
01423   function isOnline() 
01424         {
01425     if ($this->status == STATUS_ONLINE)
01426                 {
01427                         return true;
01428                 }
01429                 else
01430                 {
01431                         return false;
01432                 }
01433   }
01434 
01444   function isOffline() 
01445         {
01446     if ($this->status == STATUS_OFFLINE)
01447                 {
01448                         return true;
01449                 }
01450                 else
01451                 {
01452                         return false;
01453                 }
01454   }
01455 
01466   function setStatus($status = STATUS_OFFLINE) 
01467         {
01468                 $result = "";
01469                 if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
01470                 {
01471         $this->status = STATUS_OFFLINE;
01472                         $result = $this->lng->txt("cannot_switch_to_online_no_questions");
01473                 }
01474                 else
01475                 {
01476         $this->status = $status;
01477                 }
01478                 return $result;
01479   }
01480 
01490   function getStartDate() 
01491         {
01492     return $this->start_date;
01493   }
01494 
01503         function canStartSurvey($anonymous_id)
01504         {
01505                 global $ilAccess;
01506                 
01507                 $result = TRUE;
01508                 $messages = array();
01509                 // check start date
01510                 if ($this->getStartDateEnabled())
01511                 {
01512                         $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
01513                         $now = mktime();
01514                         if ($now < $epoch_time) 
01515                         {
01516                                 array_push($messages, $this->lng->txt("start_date_not_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getStartYear().$this->getStartMonth().$this->getStartDay()."000000"), "date") . ")");
01517                                 $result = FALSE;
01518                         }
01519                 }
01520                 // check end date
01521                 if ($this->getEndDateEnabled())
01522                 {
01523                         $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
01524                         $now = mktime();
01525                         if ($now > $epoch_time) 
01526                         {
01527                                 array_push($messages, $this->lng->txt("end_date_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getEndYear().$this->getEndMonth().$this->getEndDay()."000000"), "date") . ")");
01528                                 $result = FALSE;
01529                         }
01530                 }
01531                 // check online status
01532                 if ($this->getStatus() == STATUS_OFFLINE)
01533                 {
01534                         array_push($messages, $this->lng->txt("survey_is_offline"));
01535                         $result = FALSE;
01536                 }
01537                 // check rbac permissions
01538                 if ((!$ilAccess->checkAccess("read", "", $this->ref_id)) || (!$ilAccess->checkAccess("participate", "", $this->ref_id)))
01539                 {
01540                         array_push($messages, $this->lng->txt("cannot_participate_survey"));
01541                         $result = FALSE;
01542                 }
01543                 // 2. check previous access
01544                 if (!$result["error"])
01545                 {
01546                         global $ilUser;
01547                         $survey_started = $this->isSurveyStarted($ilUser->getId(), $anonymous_id);
01548                         if ($survey_started === 1)
01549                         {
01550                                 array_push($messages, $this->lng->txt("already_completed_survey"));
01551                                 $result = FALSE;
01552                         }
01553                 }
01554                 return array(
01555                         "result" => $result,
01556                         "messages" => $messages
01557                 );
01558         }
01559 
01560 
01570   function setStartDate($start_date = "") 
01571         {
01572     $this->start_date = $start_date;
01573   }
01574 
01584   function getStartMonth() 
01585         {
01586                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01587                 {
01588                         return $matches[2];
01589                 }
01590                 else
01591                 {
01592                         return "";
01593                 }
01594   }
01595 
01605   function getStartDay() 
01606         {
01607                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01608                 {
01609                         return $matches[3];
01610                 }
01611                 else
01612                 {
01613                         return "";
01614                 }
01615   }
01616 
01626   function getStartYear() 
01627         {
01628                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01629                 {
01630                         return $matches[1];
01631                 }
01632                 else
01633                 {
01634                         return "";
01635                 }
01636   }
01637 
01647   function getEndDate() 
01648         {
01649     return $this->end_date;
01650   }
01651 
01661   function setEndDate($end_date = "") 
01662         {
01663     $this->end_date = $end_date;
01664   }
01665 
01675   function getEndMonth() 
01676         {
01677                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01678                 {
01679                         return $matches[2];
01680                 }
01681                 else
01682                 {
01683                         return "";
01684                 }
01685   }
01686 
01696   function getEndDay() 
01697         {
01698                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01699                 {
01700                         return $matches[3];
01701                 }
01702                 else
01703                 {
01704                         return "";
01705                 }
01706   }
01707 
01717   function getEndYear() 
01718         {
01719                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01720                 {
01721                         return $matches[1];
01722                 }
01723                 else
01724                 {
01725                         return "";
01726                 }
01727   }
01728 
01738   function getEvaluationAccess() 
01739         {
01740     return $this->evaluation_access;
01741   }
01742 
01752   function setEvaluationAccess($evaluation_access = EVALUATION_ACCESS_OFF) 
01753         {
01754     $this->evaluation_access = $evaluation_access;
01755   }
01756 
01766   function getIntroduction() 
01767         {
01768     return $this->introduction;
01769   }
01770 
01780   function getOutro() 
01781         {
01782     return $this->outro;
01783   }
01784 
01793         function &getExistingQuestions() 
01794         {
01795                 global $ilDB;
01796                 $existing_questions = array();
01797                 $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",
01798                         $ilDB->quote($this->getSurveyId())
01799                 );
01800                 $result = $ilDB->query($query);
01801                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
01802                 {
01803                         array_push($existing_questions, $data->original_id);
01804                 }
01805                 return $existing_questions;
01806         }
01807 
01816         function &getQuestionpoolTitles() 
01817         {
01818                 global $rbacsystem;
01819                 global $ilDB;
01820                 
01821                 $qpl_titles = array();
01822                 // get all available questionpools and remove the trashed questionspools
01823                 $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'";
01824                 $result = $ilDB->query($query);
01825                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01826                 {               
01827                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
01828                         {
01829                                 include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
01830                                 if (ilObjSurveyQuestionPool::_lookupOnline($row->obj_id))
01831                                 {
01832                                         $qpl_titles["$row->obj_id"] = $row->title;
01833                                 }
01834                         }
01835                 }
01836                 return $qpl_titles;
01837         }
01838         
01847         function moveUpQuestion($question_id)
01848         {
01849                 $move_questions = array($question_id);
01850                 $pages =& $this->getSurveyPages();
01851                 $pageindex = -1;
01852                 foreach ($pages as $idx => $page)
01853                 {
01854                         if ($page[0]["question_id"] == $question_id)
01855                         {
01856                                 $pageindex = $idx;
01857                         }
01858                 }
01859                 if ($pageindex > 0)
01860                 {
01861                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01862                 }
01863                 else
01864                 {
01865                         // move up a question in a questionblock
01866                         $questions = $this->getSurveyQuestions();
01867                         $questions = array_keys($questions);
01868                         $index = array_search($question_id, $questions);
01869                         if (($index !== FALSE) && ($index > 0))
01870                         {
01871                                 $this->moveQuestions($move_questions, $questions[$index-1], 0);
01872                         }
01873                 }
01874         }
01875         
01884         function moveDownQuestion($question_id)
01885         {
01886                 $move_questions = array($question_id);
01887                 $pages =& $this->getSurveyPages();
01888                 $pageindex = -1;
01889                 foreach ($pages as $idx => $page)
01890                 {
01891                         if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
01892                         {
01893                                 $pageindex = $idx;
01894                         }
01895                 }
01896                 if (($pageindex < count($pages)-1) && ($pageindex >= 0))
01897                 {
01898                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01899                 }
01900                 else
01901                 {
01902                         // move down a question in a questionblock
01903                         $questions = $this->getSurveyQuestions();
01904                         $questions = array_keys($questions);
01905                         $index = array_search($question_id, $questions);
01906                         if (($index !== FALSE) && ($index < count($questions)-1))
01907                         {
01908                                 $this->moveQuestions($move_questions, $questions[$index+1], 1);
01909                         }
01910                 }
01911         }
01912         
01921         function moveUpQuestionblock($questionblock_id)
01922         {
01923                 $pages =& $this->getSurveyPages();
01924                 $move_questions = array();
01925                 $pageindex = -1;
01926                 foreach ($pages as $idx => $page)
01927                 {
01928                         if ($page[0]["questionblock_id"] == $questionblock_id)
01929                         {
01930                                 foreach ($page as $pageidx => $question)
01931                                 {
01932                                         array_push($move_questions, $question["question_id"]);
01933                                 }
01934                                 $pageindex = $idx;
01935                         }
01936                 }
01937                 if ($pageindex > 0)
01938                 {
01939                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01940                 }
01941         }
01942         
01951         function moveDownQuestionblock($questionblock_id)
01952         {
01953                 $pages =& $this->getSurveyPages();
01954                 $move_questions = array();
01955                 $pageindex = -1;
01956                 foreach ($pages as $idx => $page)
01957                 {
01958                         if ($page[0]["questionblock_id"] == $questionblock_id)
01959                         {
01960                                 foreach ($page as $pageidx => $question)
01961                                 {
01962                                         array_push($move_questions, $question["question_id"]);
01963                                 }
01964                                 $pageindex = $idx;
01965                         }
01966                 }
01967                 if ($pageindex < count($pages)-1)
01968                 {
01969                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01970                 }
01971         }
01972         
01983         function moveQuestions($move_questions, $target_index, $insert_mode)
01984         {
01985                 $array_pos = array_search($target_index, $this->questions);
01986                 if ($insert_mode == 0)
01987                 {
01988                         $part1 = array_slice($this->questions, 0, $array_pos);
01989                         $part2 = array_slice($this->questions, $array_pos);
01990                 }
01991                 else if ($insert_mode == 1)
01992                 {
01993                         $part1 = array_slice($this->questions, 0, $array_pos + 1);
01994                         $part2 = array_slice($this->questions, $array_pos + 1);
01995                 }
01996                 foreach ($move_questions as $question_id)
01997                 {
01998                         if (!(array_search($question_id, $part1) === FALSE))
01999                         {
02000                                 unset($part1[array_search($question_id, $part1)]);
02001                         }
02002                         if (!(array_search($question_id, $part2) === FALSE))
02003                         {
02004                                 unset($part2[array_search($question_id, $part2)]);
02005                         }
02006                 }
02007                 $part1 = array_values($part1);
02008                 $part2 = array_values($part2);
02009                 $this->questions = array_values(array_merge($part1, $move_questions, $part2));
02010                 foreach ($move_questions as $question_id)
02011                 {
02012                         $constraints = $this->getConstraints($question_id);
02013                         foreach ($constraints as $idx => $constraint)
02014                         {
02015                                 foreach ($part2 as $next_question_id)
02016                                 {
02017                                         if ($constraint["question"] == $next_question_id)
02018                                         {
02019                                                 // constraint concerning a question that follows -> delete constraint
02020                                                 $this->deleteConstraint($constraint["id"], $question_id);
02021                                         }
02022                                 }
02023                         }
02024                 }
02025                 $this->saveQuestionsToDb();
02026         }
02027         
02036         function removeQuestion($question_id)
02037         {
02038                 include_once "./survey/classes/class.SurveyQuestion.php";
02039                 $question =& $this->_instanciateQuestion($question_id);
02040                 $question->delete($question_id);
02041                 $this->removeConstraintsConcerningQuestion($question_id);
02042         }
02043         
02052         function removeConstraintsConcerningQuestion($question_id)
02053         {
02054                 global $ilDB;
02055                 $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02056                         $ilDB->quote($question_id . ""),
02057                         $ilDB->quote($this->getSurveyId() . "")
02058                 );
02059                 $result = $ilDB->query($query);
02060                 if ($result->numRows() > 0)
02061                 {
02062                         $remove_constraints = array();
02063                         while ($row = $result->fetchRow(DB_FETCHMODE_HASHREF))
02064                         {
02065                                 array_push($remove_constraints, $row["constraint_fi"]);
02066                         }
02067                         $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02068                                 $ilDB->quote($question_id . ""),
02069                                 $ilDB->quote($this->getSurveyId() . "")
02070                         );
02071                         $result = $ilDB->query($query);
02072                         foreach ($remove_constraints as $key => $constraint_id)
02073                         {
02074                                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02075                                         $ilDB->quote($constraint_id . "")
02076                                 );
02077                                 $result = $ilDB->query($query);
02078                         }
02079                 }
02080         }
02081                 
02091         function removeQuestions($remove_questions, $remove_questionblocks)
02092         {
02093                 global $ilDB;
02094                 $questions =& $this->getSurveyQuestions();
02095                 foreach ($questions as $question_id => $data)
02096                 {
02097                         if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
02098                         {
02099                                 unset($this->questions[array_search($question_id, $this->questions)]);
02100                                 $this->removeQuestion($question_id);
02101                         }
02102                 }
02103                 foreach ($remove_questionblocks as $questionblock_id)
02104                 {
02105                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
02106                                 $ilDB->quote($questionblock_id)
02107                         );
02108                         $result = $ilDB->query($query);
02109                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
02110                                 $ilDB->quote($questionblock_id),
02111                                 $ilDB->quote($this->getSurveyId())
02112                         );
02113                         $result = $ilDB->query($query);
02114                 }
02115                 $this->questions = array_values($this->questions);
02116                 $this->saveQuestionsToDb();
02117         }
02118                 
02127         function unfoldQuestionblocks($questionblocks)
02128         {
02129                 global $ilDB;
02130                 foreach ($questionblocks as $index)
02131                 {
02132                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
02133                                 $ilDB->quote($index)
02134                         );
02135                         $result = $ilDB->query($query);
02136                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
02137                                 $ilDB->quote($index),
02138                                 $ilDB->quote($this->getSurveyId())
02139                         );
02140                         $result = $ilDB->query($query);
02141                 }
02142         }
02143         
02152         function &getQuestionblockTitles()
02153         {
02154                 global $ilDB;
02155                 $titles = array();
02156                 $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",
02157                         $ilDB->quote($this->getId())
02158                 );
02159                 $result = $ilDB->query($query);
02160                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02161                 {
02162                         $titles[$row->questionblock_id] = $row->title;
02163                 }
02164                 return $titles;
02165         }
02166         
02175         function &getQuestionblockQuestions($questionblock_id)
02176         {
02177                 global $ilDB;
02178                 $titles = array();
02179                 $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",
02180                         $ilDB->quote($questionblock_id)
02181                 );
02182                 $result = $ilDB->query($query);
02183                 $survey_id = "";
02184                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02185                 {
02186                         $titles[$row["question_fi"]] = $row["title"];
02187                         $survey_id = $row["survey_fi"];
02188                 }
02189                 $query = sprintf("SELECT question_fi, sequence FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
02190                         $ilDB->quote($survey_id . "")
02191                 );
02192                 $result = $ilDB->query($query);
02193                 $resultarray = array();
02194                 $counter = 1;
02195                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02196                 {
02197                         if (array_key_exists($row["question_fi"], $titles))
02198                         {
02199                                 $resultarray[$counter++] = $titles[$row["question_fi"]];
02200                         }
02201                 }
02202                 return $resultarray;
02203         }
02204         
02213         function &getQuestionblockQuestionIds($questionblock_id)
02214         {
02215                 global $ilDB;
02216                 $ids = array();
02217                 $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",
02218                         $ilDB->quote($this->getId()),
02219                         $ilDB->quote($questionblock_id)
02220                 );
02221                 $result = $ilDB->query($query);
02222                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02223                 {
02224                         array_push($ids, $row->question_id);
02225                 }
02226                 return $ids;
02227         }
02228         
02238         function getQuestionblock($questionblock_id)
02239         {
02240                 global $ilDB;
02241                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02242                         $ilDB->quote($questionblock_id)
02243                 );
02244                 $result = $ilDB->query($query);
02245                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02246                 return $row;
02247         }
02248         
02258         function _getQuestionblock($questionblock_id)
02259         {
02260                 global $ilDB;
02261                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02262                         $ilDB->quote($questionblock_id)
02263                 );
02264                 $result = $ilDB->query($query);
02265                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02266                 return $row;
02267         }
02268 
02279         function _addQuestionblock($title = "", $owner = 0)
02280         {
02281                 global $ilDB;
02282                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02283                         $ilDB->quote($title . ""),
02284                         $ilDB->quote($owner . "")
02285                 );
02286                 $result = $ilDB->query($query);
02287                 return $ilDB->getLastInsertId();
02288         }
02289         
02299         function createQuestionblock($title, $questions)
02300         {
02301                 global $ilDB;
02302                 // if the selected questions are not in a continous selection, move all questions of the
02303                 // questionblock at the position of the first selected question
02304                 $this->moveQuestions($questions, $questions[0], 0);
02305                 
02306                 // now save the question block
02307                 global $ilUser;
02308                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02309                         $ilDB->quote($title),
02310                         $ilDB->quote($ilUser->id)
02311                 );
02312                 $result = $ilDB->query($query);
02313                 if ($result == DB_OK) {
02314                         $questionblock_id = $ilDB->getLastInsertId();
02315                         foreach ($questions as $index)
02316                         {
02317                                 $query = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
02318                                         $ilDB->quote($this->getSurveyId()),
02319                                         $ilDB->quote($questionblock_id),
02320                                         $ilDB->quote($index)
02321                                 );
02322                                 $result = $ilDB->query($query);
02323                                 $this->deleteConstraints($index);
02324                         }
02325                 }
02326         }
02327         
02337         function modifyQuestionblock($questionblock_id, $title)
02338         {
02339                 global $ilDB;
02340                 $query = sprintf("UPDATE survey_questionblock SET title = %s WHERE questionblock_id = %s",
02341                         $ilDB->quote($title),
02342                         $ilDB->quote($questionblock_id)
02343                 );
02344                 $result = $ilDB->query($query);
02345         }
02346         
02355         function deleteConstraints($question_id)
02356         {
02357                 global $ilDB;
02358                 $query = sprintf("SELECT * FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02359                         $ilDB->quote($question_id),
02360                         $ilDB->quote($this->getSurveyId())
02361                 );
02362                 $result = $ilDB->query($query);
02363                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02364                 {
02365                         $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02366                                 $ilDB->quote($row->constraint_fi)
02367                         );
02368                         $delresult = $ilDB->query($query);
02369                 }
02370                 $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02371                         $ilDB->quote($question_id),
02372                         $ilDB->quote($this->getSurveyId())
02373                 );
02374                 $delresult = $ilDB->query($query);
02375         }
02376 
02386         function deleteConstraint($constraint_id, $question_id)
02387         {
02388                 global $ilDB;
02389                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02390                         $ilDB->quote($constraint_id)
02391                 );
02392                 $delresult = $ilDB->query($query);
02393                 $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s AND question_fi = %s AND survey_fi = %s",
02394                         $ilDB->quote($constraint_id),
02395                         $ilDB->quote($question_id),
02396                         $ilDB->quote($this->getSurveyId())
02397                 );
02398                 $delresult = $ilDB->query($query);
02399         }
02400 
02408         function &getSurveyQuestions($with_answers = false)
02409         {
02410                 global $ilDB;
02411                 $obligatory_states =& $this->getObligatoryStates();
02412                 // get questionblocks
02413                 $all_questions = array();
02414                 $query = sprintf("SELECT survey_questiontype.type_tag, survey_question.question_id, survey_survey_question.heading FROM survey_questiontype, survey_question, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id AND survey_question.questiontype_fi = survey_questiontype.questiontype_id ORDER BY survey_survey_question.sequence",
02415 //              $query = sprintf("SELECT survey_question.question_id, survey_survey_question.heading FROM survey_question, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id ORDER BY survey_survey_question.sequence",
02416                         $ilDB->quote($this->getSurveyId())
02417                 );
02418                 $result = $ilDB->query($query);
02419                 include_once "./survey/classes/class.SurveyQuestion.php";
02420                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02421                 {
02422                         $question =& $this->_instanciateQuestion($row["question_id"]);
02423                         $questionrow = $question->_getQuestionDataArray($row["question_id"]);
02424                         foreach ($row as $key => $value)
02425                         {
02426                                 $questionrow[$key] = $value;
02427                         }
02428                         $all_questions[$row["question_id"]] = $questionrow;
02429                         if (array_key_exists($row["question_id"], $obligatory_states))
02430                         {
02431                                 $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
02432                         }
02433                 }
02434                 // get all questionblocks
02435                 $questionblocks = array();
02436                 $in = join(array_keys($all_questions), ",");
02437                 if ($in)
02438                 {
02439                         $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)",
02440                                 $ilDB->quote($this->getSurveyId())
02441                         );
02442                         $result = $ilDB->query($query);
02443                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02444                         {
02445                                 $questionblocks[$row->question_fi] = $row;
02446                         }                       
02447                 }
02448                 
02449                 foreach ($all_questions as $question_id => $row)
02450                 {
02451                         $constraints = $this->getConstraints($question_id);
02452                         if (isset($questionblocks[$question_id]))
02453                         {
02454                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02455                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02456                                 $all_questions[$question_id]["constraints"] = $constraints;
02457                         }
02458                         else
02459                         {
02460                                 $all_questions[$question_id]["questionblock_title"] = "";
02461                                 $all_questions[$question_id]["questionblock_id"] = "";
02462                                 $all_questions[$question_id]["constraints"] = $constraints;
02463                         }
02464                         if ($with_answers)
02465                         {
02466                                 $answers = array();
02467                                 $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",
02468                                         $ilDB->quote($question_id . "")
02469                                 );
02470                                 $result = $ilDB->query($query);
02471                                 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
02472                                         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
02473                                                 array_push($answers, $data->title);
02474                                         }
02475                                 }
02476                                 $all_questions[$question_id]["answers"] = $answers;                             
02477                         }
02478                 }
02479                 return $all_questions;
02480         }
02481         
02490         function &getQuestiontypes()
02491         {
02492                 global $ilDB;
02493                 $query = "SELECT type_tag FROM survey_questiontype";
02494                 $result = $ilDB->query($query);
02495                 $result_array = array();
02496                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02497                 {
02498                         array_push($result_array, $row->type_tag);
02499                 }
02500                 return $result_array;
02501         }
02502 
02511         function setObligatoryStates($obligatory_questions)
02512         {
02513                 global $ilDB;
02514                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
02515                         $ilDB->quote($this->getSurveyId() . "")
02516                 );
02517                 $result = $ilDB->query($query);
02518                 if ($result->numRows())
02519                 {
02520                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02521                         {
02522                                 if (!array_key_exists($row["question_fi"], $obligatory_questions))
02523                                 {
02524                                         $obligatory_questions[$row["question_fi"]] = 0;
02525                                 }
02526                         }
02527                 }
02528 
02529           // set the obligatory states in the database
02530                 $query = sprintf("DELETE FROM survey_question_obligatory WHERE survey_fi = %s",
02531                         $ilDB->quote($this->getSurveyId() . "")
02532                 );
02533                 $result = $ilDB->query($query);
02534 
02535           // set the obligatory states in the database
02536                 foreach ($obligatory_questions as $question_fi => $obligatory)
02537                 {
02538                         $query = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02539                                 $ilDB->quote($this->getSurveyId() . ""),
02540                                 $ilDB->quote($question_fi . ""),
02541                                 $ilDB->quote($obligatory . "")
02542                         );
02543                         $result = $ilDB->query($query);
02544                 }
02545         }
02546         
02555         function &getObligatoryStates()
02556         {
02557                 global $ilDB;
02558                 $obligatory_states = array();
02559                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
02560                         $ilDB->quote($this->getSurveyId() . "")
02561                 );
02562                 $result = $ilDB->query($query);
02563                 if ($result->numRows())
02564                 {
02565                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02566                         {
02567                                 $obligatory_states[$row["question_fi"]] = $row["obligatory"];
02568                         }
02569                 }
02570                 return $obligatory_states;
02571         }
02572         
02580         function &getSurveyPages()
02581         {
02582                 global $ilDB;
02583                 $obligatory_states =& $this->getObligatoryStates();
02584                 // get questionblocks
02585                 $all_questions = array();
02586                 $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",
02587                         $ilDB->quote($this->getSurveyId())
02588                 );
02589                 $result = $ilDB->query($query);
02590                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02591                 {
02592                         $all_questions[$row["question_id"]] = $row;
02593                 }
02594                 // get all questionblocks
02595                 $questionblocks = array();
02596                 $in = join(array_keys($all_questions), ",");
02597                 if ($in)
02598                 {
02599                         $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)",
02600                                 $ilDB->quote($this->getSurveyId())
02601                         );
02602                         $result = $ilDB->query($query);
02603                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02604                         {
02605                                 $questionblocks["$row->question_fi"] = $row;
02606                         }                       
02607                 }
02608                 
02609                 $all_pages = array();
02610                 $pageindex = -1;
02611                 $currentblock = "";
02612                 foreach ($all_questions as $question_id => $row)
02613                 {
02614                         if (array_key_exists($question_id, $obligatory_states))
02615                         {
02616                                 $all_questions["$question_id"]["obligatory"] = $obligatory_states["$question_id"];
02617                         }
02618                         $constraints = array();
02619                         if (isset($questionblocks[$question_id]))
02620                         {
02621                                 if (!$currentblock or ($currentblock != $questionblocks[$question_id]->questionblock_id))
02622                                 {
02623                                         $pageindex++;
02624                                 }
02625                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02626                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02627                                 $currentblock = $questionblocks[$question_id]->questionblock_id;
02628                                 $constraints = $this->getConstraints($question_id);
02629                                 $all_questions[$question_id]["constraints"] = $constraints;
02630                         }
02631                         else
02632                         {
02633                                 $pageindex++;
02634                                 $all_questions[$question_id]["questionblock_title"] = "";
02635                                 $all_questions[$question_id]["questionblock_id"] = "";
02636                                 $currentblock = "";
02637                                 $constraints = $this->getConstraints($question_id);
02638                                 $all_questions[$question_id]["constraints"] = $constraints;
02639                         }
02640                         if (!isset($all_pages[$pageindex]))
02641                         {
02642                                 $all_pages[$pageindex] = array();
02643                         }
02644                         array_push($all_pages[$pageindex], $all_questions[$question_id]);
02645                 }
02646                 // calculate position percentage for every page
02647                 $max = count($all_pages);
02648                 $counter = 1;
02649                 foreach ($all_pages as $index => $block)
02650                 {
02651                         foreach ($block as $blockindex => $question)
02652                         {
02653                                 $all_pages[$index][$blockindex][position] = $counter / $max;
02654                         }
02655                         $counter++;
02656                 }
02657                 return $all_pages;
02658         }
02659         
02670         function getNextPage($active_page_question_id, $direction)
02671         {
02672                 $foundpage = -1;
02673                 $pages =& $this->getSurveyPages();
02674                 if (strcmp($active_page_question_id, "") == 0)
02675                 {
02676                         return $pages[0];
02677                 }
02678                 
02679                 foreach ($pages as $key => $question_array)
02680                 {
02681                         foreach ($question_array as $question)
02682                         {
02683                                 if ($active_page_question_id == $question["question_id"])
02684                                 {
02685                                         $foundpage = $key;
02686                                 }
02687                         }
02688                 }
02689                 if ($foundpage == -1)
02690                 {
02691                         // error: page not found
02692                 }
02693                 else
02694                 {
02695                         $foundpage += $direction;
02696                         if ($foundpage < 0)
02697                         {
02698                                 return 0;
02699                         }
02700                         if ($foundpage >= count($pages))
02701                         {
02702                                 return 1;
02703                         }
02704                         return $pages[$foundpage];
02705                 }
02706         }
02707                 
02716         function &getAvailableQuestionpools($use_obj_id = false, $could_be_offline = false)
02717         {
02718                 global $rbacsystem;
02719                 global $ilDB;
02720                 
02721                 $result_array = array();
02722                 $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'";
02723                 $result = $ilDB->query($query);
02724                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02725                 {               
02726                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
02727                         {
02728                                 include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
02729                                 if (ilObjSurveyQuestionPool::_lookupOnline($row->obj_id) || $could_be_offline)
02730                                 {
02731                                         if ($use_obj_id)
02732                                         {
02733                                                 $result_array[$row->obj_id] = $row->title;
02734                                         }
02735                                         else
02736                                         {
02737                                                 $result_array[$row->ref_id] = $row->title;
02738                                         }
02739                                 }
02740                         }
02741                 }
02742                 return $result_array;
02743         }
02744         
02752         function getConstraints($question_id)
02753         {
02754                 global $ilDB;
02755                 
02756                 $result_array = array();
02757                 $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",
02758                         $ilDB->quote($question_id),
02759                         $ilDB->quote($this->getSurveyId())
02760                 );
02761                 $result = $ilDB->query($query);
02762                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02763                 {               
02764                         array_push($result_array, array("id" => $row->constraint_id, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "value" => $row->value));
02765                 }
02766                 return $result_array;
02767         }
02768 
02776         function _getConstraints($survey_id)
02777         {
02778                 global $ilDB;
02779                 $result_array = array();
02780                 $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",
02781                         $ilDB->quote($survey_id . "")
02782                 );
02783                 $result = $ilDB->query($query);
02784                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02785                 {               
02786                         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));
02787                 }
02788                 return $result_array;
02789         }
02790 
02791 
02799         function &getVariables($question_id)
02800         {
02801                 global $ilDB;
02802                 
02803                 $result_array = array();
02804                 $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",
02805                         $ilDB->quote($question_id)
02806                 );
02807                 $result = $ilDB->query($query);
02808                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02809                 {
02810                         $result_array[$row->sequence] = $row;
02811                 }
02812                 return $result_array;
02813         }
02814         
02826         function addConstraint($to_question_id, $if_question_id, $relation, $value)
02827         {
02828                 global $ilDB;
02829                 
02830                 $query = sprintf("INSERT INTO survey_constraint (constraint_id, question_fi, relation_fi, value) VALUES (NULL, %s, %s, %s)",
02831                         $ilDB->quote($if_question_id),
02832                         $ilDB->quote($relation),
02833                         $ilDB->quote($value)
02834                 );
02835                 $result = $ilDB->query($query);
02836                 if ($result == DB_OK) {
02837                         $constraint_id = $ilDB->getLastInsertId();
02838                         $query = sprintf("INSERT INTO survey_question_constraint (question_constraint_id, survey_fi, question_fi, constraint_fi) VALUES (NULL, %s, %s, %s)",
02839                                 $ilDB->quote($this->getSurveyId()),
02840                                 $ilDB->quote($to_question_id),
02841                                 $ilDB->quote($constraint_id)
02842                         );
02843                         $result = $ilDB->query($query);
02844                 }
02845         }
02846         
02854         function getAllRelations($short_as_key = false)
02855         {
02856                 global $ilDB;
02857                 
02858                 $result_array = array();
02859                 $query = "SELECT * FROM survey_relation";
02860                 $result = $ilDB->query($query);
02861                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02862                 {
02863                         if ($short_as_key)
02864                         {
02865                                 $result_array[$row->shortname] = array("short" => $row->shortname, "long" => $row->longname, "id" => $row->relation_id);
02866                         }
02867                         else
02868                         {
02869                                 $result_array[$row->relation_id] = array("short" => $row->shortname, "long" => $row->longname);
02870                         }
02871                 }
02872                 return $result_array;
02873         }
02874 
02883         function disinviteUser($user_id)
02884         {
02885                 global $ilDB;
02886                 
02887                 $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s AND user_fi = %s",
02888                         $ilDB->quote($this->getSurveyId()),
02889                         $ilDB->quote($user_id)
02890                 );
02891                 $result = $ilDB->query($query);
02892                 if ($this->getInvitation() == INVITATION_ON)
02893                 {
02894                         include_once "./classes/class.ilObjUser.php";
02895                         if (ilObjUser::_lookupLogin($user_id))
02896                         {
02897                                 $userObj = new ilObjUser($user_id);
02898                                 $userObj->dropDesktopItem($this->getRefId(), "svy");
02899                         }
02900                 }
02901         }
02902 
02911         function inviteUser($user_id)
02912         {
02913                 global $ilDB;
02914                 
02915                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE user_fi = %s AND survey_fi = %s",
02916                         $ilDB->quote($user_id),
02917                         $ilDB->quote($this->getSurveyId())
02918                 );
02919                 $result = $ilDB->query($query);
02920                 if ($result->numRows() < 1)
02921                 {
02922                         $query = sprintf("INSERT INTO survey_invited_user (invited_user_id, survey_fi, user_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02923                                 $ilDB->quote($this->getSurveyId()),
02924                                 $ilDB->quote($user_id)
02925                         );
02926                         $result = $ilDB->query($query);
02927                 }
02928                 if ($this->getInvitation() == INVITATION_ON)
02929                 {
02930                         include_once "./classes/class.ilObjUser.php";
02931                         if (ilObjUser::_lookupLogin($user_id))
02932                         {
02933                                 $userObj = new ilObjUser($user_id);
02934                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02935                         }
02936                 }
02937         }
02938 
02947         function inviteGroup($group_id)
02948         {
02949                 include_once "./classes/class.ilObjGroup.php";
02950                 $group = new ilObjGroup($group_id);
02951                 $members = $group->getGroupMemberIds();
02952                 foreach ($members as $user_id)
02953                 {
02954                         $this->inviteUser($user_id);
02955                         if ($this->getInvitation() == INVITATION_ON)
02956                         {
02957                                 if (ilObjUser::_lookupLogin($user_id))
02958                                 {
02959                                         $userObj = new ilObjUser($user_id);
02960                                         $userObj->addDesktopItem($this->getRefId(), "svy");
02961                                 }
02962                         }
02963                 }
02964         }
02965         
02974         function inviteRole($role_id)
02975         {
02976                 global $rbacreview;
02977                 $members = $rbacreview->assignedUsers($role_id);
02978                 foreach ($members as $user_id)
02979                 {
02980                         $this->inviteUser($user_id);
02981                         if ($this->getInvitation() == INVITATION_ON)
02982                         {
02983                                 if (ilObjUser::_lookupLogin($user_id))
02984                                 {
02985                                         $userObj = new ilObjUser($user_id);
02986                                         $userObj->addDesktopItem($this->getRefId(), "svy");
02987                                 }
02988                         }
02989                 }
02990         }
02991         
03000         function &getInvitedUsers()
03001         {
03002                 global $ilDB;
03003                 
03004                 $result_array = array();
03005                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
03006                         $ilDB->quote($this->getSurveyId())
03007                 );
03008                 $result = $ilDB->query($query);
03009                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03010                 {
03011                         array_push($result_array, $row->user_fi);
03012                 }
03013                 return $result_array;
03014         }
03015 
03024         function &getInvitedGroups()
03025         {
03026                 global $ilDB;
03027                 
03028                 $result_array = array();
03029                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
03030                         $ilDB->quote($this->getSurveyId())
03031                 );
03032                 $result = $ilDB->query($query);
03033                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03034                 {
03035                         array_push($result_array, $row->group_fi);
03036                 }
03037                 return $result_array;
03038         }
03039 
03049         function deleteWorkingData($question_id, $user_id)
03050         {
03051                 global $ilDB;
03052                 
03053                 $query = "";
03054                 if ($this->getAnonymize())
03055                 {
03056                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
03057                                 $ilDB->quote($this->getSurveyId()),
03058                                 $ilDB->quote($question_id),
03059                                 $ilDB->quote($_SESSION["anonymous_id"])
03060                         );
03061                 }
03062                 else
03063                 {
03064                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
03065                                 $ilDB->quote($this->getSurveyId()),
03066                                 $ilDB->quote($question_id),
03067                                 $ilDB->quote($user_id)
03068                         );
03069                 }
03070                 $result = $ilDB->query($query);
03071         }
03072         
03084         function saveWorkingData($question_id, $user_id, $anonymize_id, $value = "", $text = "")
03085         {
03086                 global $ilDB;
03087                 if ($this->isSurveyStarted($user_id, $anonymize_id) === false)
03088                 {
03089                         $this->startSurvey($user_id, $anonymize_id);
03090                 }
03091                 if (strcmp($value, "") == 0)
03092                 {
03093                         $value = "NULL";
03094                 }
03095                 else
03096                 {
03097                         $value = $ilDB->quote($value);
03098                 }
03099                 if (strcmp($text, "") == 0)
03100                 {
03101                         $text = "NULL";
03102                 }
03103                 else
03104                 {
03105                         $text = $ilDB->quote($text);
03106                 }
03107                 if ($this->getAnonymize())
03108                 {
03109                         $user_id = 0;
03110                 }
03111                 $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)",
03112                         $ilDB->quote($this->getSurveyId() . ""),
03113                         $ilDB->quote($question_id . ""),
03114                         $ilDB->quote($user_id . ""),
03115                         $ilDB->quote($anonymize_id),
03116                         $value,
03117                         $text
03118                 );
03119                 $result = $ilDB->query($query);
03120         }
03121         
03132         function loadWorkingData($question_id, $user_id)
03133         {
03134                 global $ilDB;
03135                 $result_array = array();
03136                 $query = "";
03137                 if ($this->getAnonymize())
03138                 {
03139                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
03140                                 $ilDB->quote($this->getSurveyId() . ""),
03141                                 $ilDB->quote($question_id. ""),
03142                                 $ilDB->quote($_SESSION["anonymous_id"])
03143                         );
03144                 }
03145                 else
03146                 {
03147                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
03148                                 $ilDB->quote($this->getSurveyId() . ""),
03149                                 $ilDB->quote($question_id . ""),
03150                                 $ilDB->quote($user_id . "")
03151                         );
03152                 }
03153                 $result = $ilDB->query($query);
03154                 if ($result->numRows() >= 1)
03155                 {
03156                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03157                         {
03158                                 array_push($result_array, $row);
03159                         }
03160                         return $result_array;
03161                 }
03162                 else
03163                 {
03164                         return $result_array;
03165                 }
03166         }
03167 
03176         function startSurvey($user_id, $anonymous_id)
03177         {
03178                 global $ilUser;
03179                 global $ilDB;
03180                 
03181                 if ($this->getAnonymize() && (strlen($anonymous_id) == 0)) return;
03182                 
03183                 if (strcmp($user_id, "") == 0)
03184                 {
03185                         if ($user_id == ANONYMOUS_USER_ID)
03186                         {
03187                                 $user_id = 0;
03188                         }
03189                 }
03190                 // only write the entry if there is valid data!
03191                 if (($user_id > 0) || (strlen($anonymous_id) > 0))
03192                 {
03193                         $query = sprintf("INSERT INTO survey_finished (finished_id, survey_fi, user_fi, anonymous_id, state, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
03194                                 $ilDB->quote($this->getSurveyId() . ""),
03195                                 $ilDB->quote($user_id . ""),
03196                                 $ilDB->quote($anonymous_id . ""),
03197                                 $ilDB->quote(0 . "")
03198                         );
03199                         $result = $ilDB->query($query);
03200                 }
03201         }
03202                         
03211         function finishSurvey($user_id, $anonymize_id)
03212         {
03213                 global $ilDB;
03214                 
03215                 if ($this->getAnonymize())
03216                 {
03217                         $query = sprintf("UPDATE survey_finished SET state = %s, user_fi = %s WHERE survey_fi = %s AND anonymous_id = %s",
03218                                 $ilDB->quote("1"),
03219                                 $ilDB->quote($user_id . ""),
03220                                 $ilDB->quote($this->getSurveyId() . ""),
03221                                 $ilDB->quote($anonymize_id . "")
03222                         );
03223                 }
03224                 else
03225                 {
03226                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND user_fi = %s",
03227                                 $ilDB->quote("1"),
03228                                 $ilDB->quote($this->getSurveyId() . ""),
03229                                 $ilDB->quote($user_id . "")
03230                         );
03231                 }
03232                 $result = $ilDB->query($query);
03233         }
03234         
03244         function isSurveyStarted($user_id, $anonymize_id)
03245         {
03246                 global $ilDB;
03247                 
03248                 if ($this->getAnonymize())
03249                 {
03250                         if (strlen($anonymize_id) == 0) return FALSE;
03251                         if (($user_id != ANONYMOUS_USER_ID) && (strlen($anonymize_id) == 0))
03252                         {
03253                                 $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03254                                         $ilDB->quote($this->getSurveyId()),
03255                                         $ilDB->quote($user_id)
03256                                 );
03257                         }
03258                         else
03259                         {
03260                                 $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
03261                                         $ilDB->quote($this->getSurveyId()),
03262                                         $ilDB->quote($anonymize_id)
03263                                 );
03264                         }
03265                 }
03266                 else
03267                 {
03268                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03269                                 $ilDB->quote($this->getSurveyId()),
03270                                 $ilDB->quote($user_id)
03271                         );
03272                 }
03273                 $result = $ilDB->query($query);
03274                 if ($result->numRows() == 0)
03275                 {
03276                         return false;
03277                 }                       
03278                 else
03279                 {
03280                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03281                         return (int)$row["state"];
03282                 }
03283         }
03284         
03294         function getLastActivePage($user_id)
03295         {
03296                 global $ilDB;
03297                 $query = "";
03298                 if ($this->getAnonymize())
03299                 {
03300                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s ORDER BY TIMESTAMP14 DESC",
03301                                 $ilDB->quote($this->getSurveyId() . ""),
03302                                 $ilDB->quote($_SESSION["anonymous_id"])
03303                         );
03304                 }
03305                 else
03306                 {
03307                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND user_fi = %s ORDER BY TIMESTAMP14 DESC",
03308                                 $ilDB->quote($this->getSurveyId() . ""),
03309                                 $ilDB->quote($user_id . "")
03310                         );
03311                 }
03312                 $result = $ilDB->query($query);
03313                 if ($result->numRows() == 0)
03314                 {
03315                         return "";
03316                 }
03317                 else
03318                 {
03319                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03320                         return $row["question_fi"];
03321                 }
03322         }
03323 
03334         function checkConstraint($constraint_data, $working_data)
03335         {
03336                 if (count($working_data) == 0)
03337                 {
03338                         return 0;
03339                 }
03340                 
03341                 if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
03342                 {
03343                         return 0;
03344                 }
03345                 
03346                 foreach ($working_data as $data)
03347                 {
03348                         switch ($constraint_data["short"])
03349                         {
03350                                 case "<":
03351                                         if ($data["value"] < $constraint_data["value"])
03352                                         {
03353                                                 return 1;
03354                                         }
03355                                         break;
03356                                 case "<=":
03357                                         if ($data["value"] <= $constraint_data["value"])
03358                                         {
03359                                                 return 1;
03360                                         }
03361                                         break;
03362                                 case "=":
03363                                         if ($data["value"] == $constraint_data["value"])
03364                                         {
03365                                                 return 1;
03366                                         }
03367                                         break;
03368                                 case "<>":
03369                                         if ($data["value"] != $constraint_data["value"])
03370                                         {
03371                                                 return 1;
03372                                         }
03373                                         break;
03374                                 case ">=":
03375                                         if ($data["value"] >= $constraint_data["value"])
03376                                         {
03377                                                 return 1;
03378                                         }
03379                                         break;
03380                                 case ">":
03381                                         if ($data["value"] > $constraint_data["value"])
03382                                         {
03383                                                 return 1;
03384                                         }
03385                                         break;
03386                         }
03387                 }
03388                 return 0;
03389         }
03390         
03391         function _hasDatasets($survey_id)
03392         {
03393                 global $ilDB;
03394                 
03395                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03396                         $ilDB->quote($survey_id . "")
03397                 );
03398                 $result = $ilDB->query($query);
03399                 if ($result->numRows())
03400                 {
03401                         return true;
03402                 }
03403                 else
03404                 {
03405                         return false;
03406                 }
03407         }
03408 
03409         function &getEvaluationForAllUsers()
03410         {
03411                 global $ilDB;
03412                 
03413                 $users = array();
03414                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03415                         $ilDB->quote($this->getSurveyId() . "")
03416                 );
03417                 $result = $ilDB->query($query);
03418                 if ($result->numRows())
03419                 {
03420                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03421                         {
03422                                 array_push($users, $row);
03423                         }
03424                 }
03425                 $evaluation = array();
03426                 $questions =& $this->getSurveyQuestions();
03427                 foreach ($users as $row)
03428                 {
03429                         if (($row["user_fi"] > 0) && ($row["user_fi"] != ANONYMOUS_USER_ID) && ($this->getAnonymize() == 0))
03430                         {
03431                                 $evaluation[$row["user_fi"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03432                         }
03433                         else
03434                         {
03435                                 if (strlen($row["anonymous_id"]) > 0)
03436                                 {
03437                                         $evaluation[$row["anonymous_id"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03438                                 }
03439                         }
03440                 }
03441                 return $evaluation;
03442         }
03443         
03455         function &getEvaluationByUser($questions, $user_id, $anonymous_id = "")
03456         {
03457                 global $ilDB;
03458                 
03459                 $wherecond = "";
03460                 $wherevalue = "";
03461                 if (strcmp($anonymous_id, "") != 0)
03462                 {
03463                         $wherecond = "anonymous_id = %s";
03464                         $wherevalue = $anonymous_id;
03465                 }
03466                 else
03467                 {
03468                         $wherecond = "user_fi = %s";
03469                         $wherevalue = $user_id;
03470                 }
03471                 // collect all answers
03472                 $answers = array();
03473                 $query = sprintf("SELECT * FROM survey_answer WHERE $wherecond AND survey_fi = %s",
03474                         $ilDB->quote($wherevalue),
03475                         $ilDB->quote($this->getSurveyId())
03476                 );
03477                 $result = $ilDB->query($query);
03478                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03479                 {
03480                         if (!is_array($answers[$row["question_fi"]]))
03481                         {
03482                                 $answers[$row["question_fi"]] = array();
03483                         }
03484                         array_push($answers[$row["question_fi"]], $row);
03485                 }
03486                 $username = $this->lng->txt("anonymous");
03487                 $gender = "";
03488                 if (($user_id > 0) && ($user_id != ANONYMOUS_USER_ID) && ($this->getAnonymize() == 0))
03489                 {
03490                         include_once "./classes/class.ilObjUser.php";
03491                         if (strlen(ilObjUser::_lookupLogin($user_id)) == 0)
03492                         {
03493                                 $username = $this->lng->txt("deleted_user");
03494                                 $gender = "";
03495                         }
03496                         else
03497                         {
03498                                 $user = new ilObjUser($user_id);
03499                                 $username = $user->getFullname();
03500                                 $gender = $user->getGender();
03501                                 if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
03502                         }
03503                 }
03504                 $resultset = array(
03505                         "name" => $username,
03506                         "gender" => $gender,
03507                         "answers" => array()
03508                 );
03509                 foreach ($questions as $key => $question)
03510                 {
03511                         if (array_key_exists($key, $answers))
03512                         {
03513                                 $resultset["answers"][$key] = $answers[$key];
03514                         }
03515                         else
03516                         {
03517                                 $resultset["answers"][$key] = array();
03518                         }
03519                         sort($resultset["answers"][$key]);
03520                 }
03521                 return $resultset;
03522         }
03523         
03534         function getCumulatedResults(&$question)
03535         {
03536                 global $ilDB;
03537                 
03538                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03539                         $ilDB->quote($this->getSurveyId())
03540                 );
03541                 $result = $ilDB->query($query);
03542                 $nr_of_users = $result->numRows();
03543                 
03544                 $result_array =& $question->getCumulatedResults($this->getSurveyId(), $nr_of_users);
03545                 return $result_array;
03546         }
03547 
03548         function &getQuestions($question_ids)
03549         {
03550                 $result_array = array();
03551                 $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, ",") . ")";
03552                 $result = $ilDB->query($query);
03553                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03554                 {
03555                         array_push($result_array, $row);
03556                 }
03557                 return $result_array;
03558         }
03559         
03560         function &getQuestionblocks($questionblock_ids)
03561         {
03562                 global $ilDB;
03563                 
03564                 $result_array = array();
03565     $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";
03566                 $result = $ilDB->query($query);
03567                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03568                 {
03569                         if ($row["questionblock_id"] != $qbid)
03570                         {
03571                                 $sequence = 1;
03572                         }
03573                         $row["sequence"] = $sequence++;
03574                         $result_array[$row["questionblock_id"]][$row["question_id"]] = $row;
03575                         $qbid = $row["questionblock_id"];
03576                 }
03577                 return $result_array;
03578         }
03579 
03580         function &getForbiddenQuestionpools()
03581         {
03582                 global $rbacsystem;
03583                 global $ilDB;
03584                 
03585                 // get all available questionpools and remove the trashed questionspools
03586                 $forbidden_pools = array();
03587                 $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'";
03588                 $result = $ilDB->query($query);
03589                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03590                 {               
03591                         include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
03592                         if (!$rbacsystem->checkAccess("write", $row->ref_id) || (!$this->_hasUntrashedReference($row->obj_id)) || (!ilObjSurveyQuestionPool::_lookupOnline($row->obj_id)))
03593                         {
03594                                 array_push($forbidden_pools, $row->obj_id);
03595                         }
03596                 }
03597                 return $forbidden_pools;
03598         }
03599         
03607         function getQuestionsTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0, $completeonly = 0, $filter_question_type = "", $filter_questionpool = "")
03608         {
03609                 global $ilUser;
03610                 global $ilDB;
03611                 
03612                 $where = "";
03613                 if (strlen($filter_text) > 0) {
03614                         switch($sel_filter_type) {
03615                                 case "title":
03616                                         $where = " AND survey_question.title LIKE " . $ilDB->quote("%" . $filter_text . "%");
03617                                         break;
03618                                 case "description":
03619                                         $where = " AND survey_question.description LIKE " . $ilDB->quote("%" . $filter_text . "%");
03620                                         break;
03621                                 case "author":
03622                                         $where = " AND survey_question.author LIKE " . $ilDB->quote("%" . $filter_text . "%");
03623                                         break;
03624                         }
03625                 }
03626   
03627                 if ($filter_question_type && (strcmp($filter_question_type, "all") != 0))
03628                 {
03629                         $where .= " AND survey_questiontype.type_tag = " . $ilDB->quote($filter_question_type);
03630                 }
03631                 
03632                 if ($filter_questionpool && (strcmp($filter_questionpool, "all") != 0))
03633                 {
03634                         $where .= " AND survey_question.obj_fi = $filter_questionpool";
03635                 }
03636   
03637     // build sort order for sql query
03638                 $order = "";
03639                 $images = array();
03640     if (count($sortoptions)) 
03641                 {
03642                         include_once "./classes/class.ilUtil.php";
03643       foreach ($sortoptions as $key => $value) 
03644                         {
03645         switch($key) 
03646                                 {
03647           case "title":
03648             $order = " ORDER BY title $value";
03649             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03650             break;
03651           case "description":
03652             $order = " ORDER BY description $value";
03653             $images["description"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03654             break;
03655           case "type":
03656             $order = " ORDER BY questiontype_id $value";
03657             $images["type"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03658             break;
03659           case "author":
03660             $order = " ORDER BY author $value";
03661             $images["author"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03662             break;
03663           case "created":
03664             $order = " ORDER BY created $value";
03665             $images["created"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03666             break;
03667           case "updated":
03668             $order = " ORDER BY TIMESTAMP14 $value";
03669             $images["updated"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03670             break;
03671                                         case "qpl":
03672                                                 $order = " ORDER BY obj_fi $value";
03673             $images["qpl"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03674                                                 break;
03675         }
03676       }
03677     }
03678                 $maxentries = $ilUser->prefs["hits_per_page"];
03679                 if ($maxentries < 1)
03680                 {
03681                         $maxentries = 9999;
03682                 }
03683 
03684                 $forbidden_pools =& $this->getForbiddenQuestionpools();
03685                 $forbidden = "";
03686                 if (count($forbidden_pools))
03687                 {
03688                         $forbidden = " AND survey_question.obj_fi NOT IN (" . join($forbidden_pools, ",") . ")";
03689                 }
03690                 if ($completeonly)
03691                 {
03692                         $forbidden .= " AND survey_question.complete = " . $ilDB->quote("1");
03693                 }
03694                 $existing = "";
03695                 $existing_questions =& $this->getExistingQuestions();
03696                 if (count($existing_questions))
03697                 {
03698                         $existing = " AND survey_question.question_id NOT IN (" . join($existing_questions, ",") . ")";
03699                 }
03700           $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";
03701     $query_result = $ilDB->query($query);
03702                 $max = $query_result->numRows();
03703                 if ($startrow > $max -1)
03704                 {
03705                         $startrow = $max - ($max % $maxentries);
03706                 }
03707                 else if ($startrow < 0)
03708                 {
03709                         $startrow = 0;
03710                 }
03711                 $limit = " LIMIT $startrow, $maxentries";
03712           $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";
03713     $query_result = $ilDB->query($query);
03714                 $rows = array();
03715                 if ($query_result->numRows())
03716                 {
03717                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03718                         {
03719                                 array_push($rows, $row);
03720                         }
03721                 }
03722                 $nextrow = $startrow + $maxentries;
03723                 if ($nextrow > $max - 1)
03724                 {
03725                         $nextrow = $startrow;
03726                 }
03727                 $prevrow = $startrow - $maxentries;
03728                 if ($prevrow < 0)
03729                 {
03730                         $prevrow = 0;
03731                 }
03732                 return array(
03733                         "rows" => $rows,
03734                         "images" => $images,
03735                         "startrow" => $startrow,
03736                         "nextrow" => $nextrow,
03737                         "prevrow" => $prevrow,
03738                         "step" => $maxentries,
03739                         "rowcount" => $max
03740                 );
03741         }
03742 
03750         function getQuestionblocksTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0)
03751         {
03752                 global $ilDB;
03753                 global $ilUser;
03754                 $where = "";
03755                 if (strlen($filter_text) > 0) {
03756                         switch($sel_filter_type) {
03757                                 case "title":
03758                                         $where = " AND survey_questionblock.title LIKE " . $ilDB->quote("%" . $filter_text . "%");
03759                                         break;
03760                         }
03761                 }
03762   
03763     // build sort order for sql query
03764                 $order = "";
03765                 $images = array();
03766     if (count($sortoptions)) 
03767                 {
03768                         include_once "./classes/class.ilUtil.php";
03769       foreach ($sortoptions as $key => $value) 
03770                         {
03771         switch($key) 
03772                                 {
03773           case "title":
03774                                                 $order = " ORDER BY survey_questionblock.title $value";
03775             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03776             break;
03777                                         case "svy":
03778                                                 $order = " ORDER BY survey_survey_question.survey_fi $value";
03779             $images["svy"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03780                                                 break;
03781         }
03782       }
03783     }
03784                 $maxentries = $ilUser->prefs["hits_per_page"];
03785                 if ($order)
03786                 {
03787                         $order .=  ",survey_survey_question.sequence ASC";
03788                 }
03789                 else
03790                 {
03791                         $order = " ORDER BY survey_survey_question.sequence ASC";
03792                 }
03793                 $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";
03794     $query_result = $ilDB->query($query);
03795                 $questionblock_ids = array();
03796                 if ($query_result->numRows())
03797                 {
03798                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03799                         {
03800                                 array_push($questionblock_ids, $row["questionblock_id"]);
03801                         }
03802                 }
03803                 
03804                 $max = $query_result->numRows();
03805                 if ($startrow > $max -1)
03806                 {
03807                         $startrow = $max - ($max % $maxentries);
03808                 }
03809                 else if ($startrow < 0)
03810                 {
03811                         $startrow = 0;
03812                 }
03813                 $limit = " LIMIT $startrow, $maxentries";
03814                 $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";
03815     $query_result = $ilDB->query($query);
03816                 $rows = array();
03817                 if ($query_result->numRows())
03818                 {
03819                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03820                         {
03821                                 $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
03822                                 $counter = 1;
03823                                 foreach ($questions_array as $key => $value)
03824                                 {
03825                                         $questions_array[$key] = "$counter. $value";
03826                                         $counter++;
03827                                 }
03828                                 $rows[$row["questionblock_id"]] = array(
03829                                         "questionblock_id" => $row["questionblock_id"],
03830                                         "title" => $row["title"], 
03831                                         "surveytitle" => $row["surveytitle"], 
03832                                         "questions" => join($questions_array, ", "),
03833                                         "owner" => $row["owner_fi"]
03834                                 );
03835                         }
03836                 }
03837                 $nextrow = $startrow + $maxentries;
03838                 if ($nextrow > $max - 1)
03839                 {
03840                         $nextrow = $startrow;
03841                 }
03842                 $prevrow = $startrow - $maxentries;
03843                 if ($prevrow < 0)
03844                 {
03845                         $prevrow = 0;
03846                 }
03847                 return array(
03848                         "rows" => $rows,
03849                         "images" => $images,
03850                         "startrow" => $startrow,
03851                         "nextrow" => $nextrow,
03852                         "prevrow" => $prevrow,
03853                         "step" => $maxentries,
03854                         "rowcount" => $max
03855                 );
03856         }
03857 
03866         function &_getQuestiontypes()
03867         {
03868                 global $ilDB;
03869                 
03870                 $questiontypes = array();
03871                 $query = "SELECT * FROM survey_questiontype ORDER BY type_tag";
03872                 $query_result = $ilDB->query($query);
03873                 while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03874                 {
03875                         array_push($questiontypes, $row["type_tag"]);
03876                 }
03877                 return $questiontypes;
03878         }
03879                 
03888         function to_xml()
03889         {
03890                 include_once("./classes/class.ilXmlWriter.php");
03891                 $a_xml_writer = new ilXmlWriter;
03892                 // set xml header
03893                 $a_xml_writer->xmlHeader();
03894                 $a_xml_writer->xmlStartTag("questestinterop");
03895                 $attrs = array(
03896                         "ident" => $this->getSurveyId(),
03897                         "title" => $this->getTitle()
03898                 );
03899                 $a_xml_writer->xmlStartTag("survey", $attrs);
03900                 
03901                 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
03902                 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
03903                 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
03904                 // add ILIAS specific metadata
03905                 $a_xml_writer->xmlStartTag("objectives");
03906                 $attrs = array(
03907                         "label" => "introduction"
03908                 );
03909                 $a_xml_writer->xmlStartTag("material", $attrs);
03910                 $a_xml_writer->xmlElement("mattext", NULL, $this->getIntroduction());
03911                 $a_xml_writer->xmlEndTag("material");
03912                 $attrs = array(
03913                         "label" => "outro"
03914                 );
03915                 $a_xml_writer->xmlStartTag("material", $attrs);
03916                 $a_xml_writer->xmlElement("mattext", NULL, $this->getOutro());
03917                 $a_xml_writer->xmlEndTag("material");
03918                 $a_xml_writer->xmlEndTag("objectives");
03919 
03920                 // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
03921                 $a_xml_writer->xmlStartTag("qtimetadata");
03922                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03923                 $a_xml_writer->xmlElement("fieldlabel", NULL, "SCORM");
03924                 include_once "./Services/MetaData/classes/class.ilMD.php";
03925                 $md = new ilMD($this->getId(),0, $this->getType());
03926                 $writer = new ilXmlWriter();
03927                 $md->toXml($writer);
03928                 $metadata = $writer->xmlDumpMem();
03929                 $a_xml_writer->xmlElement("fieldentry", NULL, $metadata);
03930                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03931                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03932                 $a_xml_writer->xmlElement("fieldlabel", NULL, "author");
03933                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getAuthor());
03934                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03935                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03936                 $a_xml_writer->xmlElement("fieldlabel", NULL, "description");
03937                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getDescription());
03938                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03939                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03940                 $a_xml_writer->xmlElement("fieldlabel", NULL, "evaluation_access");
03941                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getEvaluationAccess());
03942                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03943                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03944                 $a_xml_writer->xmlElement("fieldlabel", NULL, "anonymize");
03945                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getAnonymize());
03946                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03947                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03948                 $a_xml_writer->xmlElement("fieldlabel", NULL, "status");
03949                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getStatus());
03950                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03951                 if ($this->getStartDateEnabled())
03952                 {
03953                         $a_xml_writer->xmlStartTag("qtimetadatafield");
03954                         $a_xml_writer->xmlElement("fieldlabel", NULL, "startdate");
03955                         $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("P%dY%dM%dDT0H0M0S", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
03956                         $a_xml_writer->xmlEndTag("qtimetadatafield");
03957                 }
03958                 if ($this->getEndDateEnabled())
03959                 {
03960                         $a_xml_writer->xmlStartTag("qtimetadatafield");
03961                         $a_xml_writer->xmlElement("fieldlabel", NULL, "enddate");
03962                         $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("P%dY%dM%dDT0H0M0S", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
03963                         $a_xml_writer->xmlEndTag("qtimetadatafield");
03964                 }
03965                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03966                 $a_xml_writer->xmlElement("fieldlabel", NULL, "display_question_titles");
03967                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getShowQuestionTitles());
03968                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03969 
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                                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03983                                 $a_xml_writer->xmlElement("fieldlabel", NULL, "questionblock_" . $question_array[0]["questionblock_id"]);
03984                                 $a_xml_writer->xmlElement("fieldentry", NULL, "<title>" . $question["questionblock_title"]. "</title><questions>" . join($question_ids, ",") . "</questions>");
03985                                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03986                         }
03987                 }
03988                 // add constraints
03989                 foreach ($pages as $question_array)
03990                 {
03991                         foreach ($question_array as $question)
03992                         {
03993                                 if (count($question["constraints"]))
03994                                 {
03995                                         // found constraints
03996                                         foreach ($question["constraints"] as $constraint)
03997                                         {
03998                                                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03999                                                 $a_xml_writer->xmlElement("fieldlabel", NULL, "constraint_" . $question["question_id"]);
04000                                                 $a_xml_writer->xmlElement("fieldentry", NULL, $constraint["question"] . "," . $constraint["short"] . "," . $constraint["value"]);
04001                                                 $a_xml_writer->xmlEndTag("qtimetadatafield");
04002                                         }
04003                                 }
04004                         }
04005                 }
04006                 // add headings
04007                 foreach ($pages as $question_array)
04008                 {
04009                         foreach ($question_array as $question)
04010                         {
04011                                 if ($question["heading"])
04012                                 {
04013                                         $a_xml_writer->xmlStartTag("qtimetadatafield");
04014                                         $a_xml_writer->xmlElement("fieldlabel", NULL, "heading_" . $question["question_id"]);
04015                                         $a_xml_writer->xmlElement("fieldentry", NULL, $question["heading"]);
04016                                         $a_xml_writer->xmlEndTag("qtimetadatafield");
04017                                 }
04018                         }
04019                 }
04020                 $a_xml_writer->xmlEndTag("qtimetadata");
04021                 $a_xml_writer->xmlEndTag("survey");
04022                 $a_xml_writer->xmlEndTag("questestinterop");
04023                 $xml = $a_xml_writer->xmlDumpMem(FALSE);
04024 
04025                 $obligatory_states =& $this->getObligatoryStates();
04026                 foreach ($this->questions as $question_id) 
04027                 {
04028                         $question =& $this->_instanciateQuestion($question_id);
04029                         if ($question !== FALSE)
04030                         {
04031                                 $qti_question = $question->to_xml(false, $obligatory_states[$question_id]);
04032                                 $qti_question = preg_replace("/<questestinterop>/", "", $qti_question);
04033                                 $qti_question = preg_replace("/<\/questestinterop>/", "", $qti_question);
04034                                 $xml = str_replace("</questestinterop>", "$qti_question</questestinterop>", $xml);
04035                         }
04036                 }
04037                 return $xml;
04038         }
04039         
04049   function &_instanciateQuestion($question_id) 
04050         {
04051                 if ($question_id < 1) return FALSE;
04052                 include_once "./survey/classes/class.SurveyQuestion.php";
04053                 $question_type = SurveyQuestion::_getQuestionType($question_id);
04054                 if (strlen($question_type) == 0) return FALSE;
04055                 include_once "./survey/classes/class.$question_type.php";
04056                 $question = new $question_type();
04057                 $question->loadFromDb($question_id);
04058                 return $question;
04059   }
04060 
04069         function importObject($file_info, $survey_questionpool_id)
04070         {
04071                 // check if file was uploaded
04072                 $source = $file_info["tmp_name"];
04073                 $error = 0;
04074                 if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
04075                 {
04076                         $this->ilias->raiseError($this->lng->txt("import_no_file_selected"),$this->ilias->error_obj->MESSAGE);
04077                         $error = 1;
04078                 }
04079                 // check correct file type
04080                 if (!((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0)))
04081                 {
04082                         $this->ilias->raiseError($this->lng->txt("import_wrong_file_type"),$this->ilias->error_obj->MESSAGE);
04083                         $error = 1;
04084                 }
04085                 if (!$error)
04086                 {
04087                         // import file as a survey
04088                         $import_dir = $this->getImportDirectory();
04089                         $importfile = tempnam($import_dir, "survey_import");
04090                         //move_uploaded_file($source, $importfile);
04091                         include_once "./classes/class.ilUtil.php";
04092                         ilUtil::moveUploadedFile($source, "survey_import", $importfile);
04093                         $fh = fopen($importfile, "r");
04094                         if (!$fh)
04095                         {
04096                                 $this->ilias->raiseError($this->lng->txt("import_error_opening_file"),$this->ilias->error_obj->MESSAGE);
04097                                 $error = 1;
04098                                 return $error;
04099                         }
04100                         $xml = fread($fh, filesize($importfile));
04101                         $result = fclose($fh);
04102 
04103                         // delete import directory
04104                         ilUtil::delDir($this->getImportDirectory());
04105         
04106                         if (!$result)
04107                         {
04108                                 $this->ilias->raiseError($this->lng->txt("import_error_closing_file"),$this->ilias->error_obj->MESSAGE);
04109                                 $error = 1;
04110                                 return $error;
04111                         }
04112                         if (preg_match("/(<survey[^>]*>.*?<\/survey>)/si", $xml, $matches))
04113                         {
04114                                 // read survey properties
04115                                 $import_results = $this->from_xml($matches[1]);
04116                                 if ($import_results === false)
04117                                 {
04118                                         $this->ilias->raiseError($this->lng->txt("import_error_survey_no_proper_values"),$this->ilias->error_obj->MESSAGE);
04119                                         $error = 1;
04120                                         return $error;
04121                                 }
04122                         }
04123                         else
04124                         {
04125                                 $this->ilias->raiseError($this->lng->txt("import_error_survey_no_properties"),$this->ilias->error_obj->MESSAGE);
04126                                 $error = 1;
04127                                 return $error;
04128                         }
04129                         $question_counter = 0;
04130                         $new_question_ids = array();
04131                         if (preg_match_all("/(<item[^>]*>.*?<\/item>)/si", $xml, $matches))
04132                         {
04133                                 foreach ($matches[1] as $index => $item)
04134                                 {
04135                                         $question = "";
04136                                         if (preg_match("/<qticomment>Questiontype\=(.*?)<\/qticomment>/is", $item, $questiontype))
04137                                         {
04138                                                 include_once "./survey/classes/class.SurveyNominalQuestion.php";
04139                                                 include_once "./survey/classes/class.SurveyOrdinalQuestion.php";
04140                                                 include_once "./survey/classes/class.SurveyMetricQuestion.php";
04141                                                 include_once "./survey/classes/class.SurveyTextQuestion.php";
04142                                                 switch ($questiontype[1])
04143                                                 {
04144                                                         case NOMINAL_QUESTION_IDENTIFIER:
04145                                                                 $question = new SurveyNominalQuestion();
04146                                                                 break;
04147                                                         case ORDINAL_QUESTION_IDENTIFIER:
04148                                                                 $question = new SurveyOrdinalQuestion();
04149                                                                 break;
04150                                                         case METRIC_QUESTION_IDENTIFIER:
04151                                                                 $question = new SurveyMetricQuestion();
04152                                                                 break;
04153                                                         case TEXT_QUESTION_IDENTIFIER:
04154                                                                 $question = new SurveyTextQuestion();
04155                                                                 break;
04156                                                 }
04157                                                 if ($question)
04158                                                 {
04159                                                         $question->from_xml("<questestinterop>$item</questestinterop>");
04160                                                         if ($import_results !== false)
04161                                                         {
04162                                                                 $question->setObjId($survey_questionpool_id);
04163                                                                 $question->saveToDb();
04164                                                                 $question_id = $question->duplicate(true);
04165                                                                 $this->questions[$question_counter++] = $question_id;
04166                                                                 if (preg_match("/<item\s+ident\=\"(\d+)\"/", $item, $matches))
04167                                                                 {
04168                                                                         $original_question_id = $matches[1];
04169                                                                         $new_question_ids[$original_question_id] = $question_id;
04170                                                                 }
04171                                                         }
04172                                                         else
04173                                                         {
04174                                                                 $this->ilias->raiseError($this->lng->txt("error_importing_question"), $this->ilias->error_obj->MESSAGE);
04175                                                         }
04176                                                 }
04177                                         }
04178                                 }
04179                         }
04180 
04181                         $this->saveToDb();
04182                         // add question blocks
04183                         foreach ($import_results["questionblocks"] as $questionblock)
04184                         {
04185                                 foreach ($questionblock["questions"] as $key => $value)
04186                                 {
04187                                         $questionblock["questions"][$key] = $new_question_ids[$value];
04188                                 }
04189                                 $this->createQuestionblock($questionblock["title"], $questionblock["questions"]);
04190                         }
04191                         // add constraints
04192                         $relations = $this->getAllRelations(true);
04193                         foreach ($import_results["constraints"] as $constraint)
04194                         {
04195                                 $this->addConstraint($new_question_ids[$constraint["for"]], $new_question_ids[$constraint["question"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
04196                         }
04197                         foreach ($import_results["headings"] as $qid => $heading)
04198                         {
04199                                 $this->saveHeading($heading, $new_question_ids[$qid]);
04200                         }
04201                 }
04202                 return $error;
04203         }
04204 
04213         function from_xml($xml_text)
04214         {
04215                 $result = false;
04216                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
04217                 $domxml = domxml_open_mem($xml_text);
04218                 $constraints = array();
04219                 $headings = array();
04220                 $questionblocks = array();
04221                 if (!empty($domxml))
04222                 {
04223                         $root = $domxml->document_element();
04224                         $this->setTitle($root->get_attribute("title"));
04225                         $item = $root;
04226                         $itemnodes = $item->child_nodes();
04227                         foreach ($itemnodes as $index => $node)
04228                         {
04229                                 switch ($node->node_name())
04230                                 {
04231                                         case "qticomment":
04232                                                 $comment = $node->get_content();
04233                                                 if (strpos($comment, "ILIAS Version=") !== false)
04234                                                 {
04235                                                 }
04236                                                 elseif (strpos($comment, "Questiontype=") !== false)
04237                                                 {
04238                                                 }
04239                                                 elseif (strpos($comment, "Author=") !== false)
04240                                                 {
04241                                                         $comment = str_replace("Author=", "", $comment);
04242                                                         $this->setAuthor($comment);
04243                                                 }
04244                                                 else
04245                                                 {
04246                                                         $this->setDescription($comment);
04247                                                 }
04248                                                 break;
04249                                         case "objectives":
04250                                                 $materials = $node->child_nodes();
04251                                                 foreach ($materials as $material)
04252                                                 {
04253                                                         if (strcmp($material->get_attribute("label"), "introduction") == 0)
04254                                                         {
04255                                                                 $mattext = $material->first_child();
04256                                                                 $this->setIntroduction($mattext->get_content());
04257                                                         }
04258                                                         else if (strcmp($material->get_attribute("label"), "outro") == 0)
04259                                                         {
04260                                                                 $mattext = $material->first_child();
04261                                                                 $this->setOutro($mattext->get_content());
04262                                                         }
04263                                                 }
04264                                                 break;
04265                                         case "qtimetadata":
04266                                                 $metadata_fields = $node->child_nodes();
04267                                                 foreach ($metadata_fields as $index => $metadata_field)
04268                                                 {
04269                                                         $fieldlabel = $metadata_field->first_child();
04270                                                         $fieldentry = $fieldlabel->next_sibling();
04271                                                         switch ($fieldlabel->get_content())
04272                                                         {
04273                                                                 case "evaluation_access":
04274                                                                         $this->setEvaluationAccess($fieldentry->get_content());
04275                                                                         break;
04276                                                                 case "author":
04277                                                                         $this->setAuthor($fieldentry->get_content());
04278                                                                         break;
04279                                                                 case "description":
04280                                                                         $this->setDescription($fieldentry->get_content());
04281                                                                         break;
04282                                                                 case "anonymize":
04283                                                                         $this->setAnonymize($fieldentry->get_content());
04284                                                                         break;
04285                                                                 case "startdate":
04286                                                                         $iso8601period = $fieldentry->get_content();
04287                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04288                                                                         {
04289                                                                                 $this->setStartDateEnabled(true);
04290                                                                                 $this->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04291                                                                         }
04292                                                                         break;
04293                                                                 case "enddate":
04294                                                                         $iso8601period = $fieldentry->get_content();
04295                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04296                                                                         {
04297                                                                                 $this->setEndDateEnabled(true);
04298                                                                                 $this->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04299                                                                         }
04300                                                                         break;
04301                                                                 case "status":
04302                                                                         $this->setStatus($fieldentry->get_content());
04303                                                                         break;
04304                                                                 case "display_question_titles":
04305                                                                         if ($fieldentry->get_content() == QUESTIONTITLES_HIDDEN)
04306                                                                         {
04307                                                                                 $this->hideQuestionTitles();
04308                                                                         }
04309                                                                         else
04310                                                                         {
04311                                                                                 $this->showQuestionTitles();
04312                                                                         }
04313                                                         }
04314                                                         if (preg_match("/questionblock_\d+/", $fieldlabel->get_content()))
04315                                                         {
04316                                                                 $qb = $fieldentry->get_content();
04317                                                                 preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
04318                                                                 $qb_title = $matches[1];
04319                                                                 preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
04320                                                                 $qb_questions = $matches[1];
04321                                                                 $qb_questions_array = explode(",", $qb_questions);
04322                                                                 array_push($questionblocks, array(
04323                                                                         "title" => $qb_title,
04324                                                                         "questions" => $qb_questions_array
04325                                                                 ));
04326                                                         }
04327                                                         if (preg_match("/constraint_(\d+)/", $fieldlabel->get_content(), $matches))
04328                                                         {
04329                                                                 $constraint = $fieldentry->get_content();
04330                                                                 $constraint_array = explode(",", $constraint);
04331                                                                 if (count($constraint_array) == 3)
04332                                                                 {
04333                                                                         array_push($constraints, array(
04334                                                                                 "for"      => $matches[1], 
04335                                                                                 "question" => $constraint_array[0],
04336                                                                                 "relation" => $constraint_array[1],
04337                                                                                 "value"    => $constraint_array[2]
04338                                                                         ));
04339                                                                 }
04340                                                         }
04341                                                         if (preg_match("/heading_(\d+)/", $fieldlabel->get_content(), $matches))
04342                                                         {
04343                                                                 $heading = $fieldentry->get_content();
04344                                                                 $headings[$matches[1]] = $heading;
04345                                                         }
04346                                                 }
04347                                                 break;
04348                                 }
04349                         }
04350                         $result["questionblocks"] = $questionblocks;
04351                         $result["constraints"] = $constraints;
04352                         $result["headings"] = $headings;
04353                 }
04354                 return $result;
04355         }
04356 
04365         function &_getAvailableSurveys($use_object_id = false)
04366         {
04367                 global $rbacsystem;
04368                 global $ilDB;
04369                 
04370                 $result_array = array();
04371                 $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";
04372                 $result = $ilDB->query($query);
04373                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
04374                 {               
04375                         include_once "./classes/class.ilObject.php";
04376                         if ($rbacsystem->checkAccess("write", $row->ref_id) && (ilObject::_hasUntrashedReference($row->obj_id)))
04377                         {
04378                                 if ($use_object_id)
04379                                 {
04380                                         $result_array[$row->obj_id] = $row->title;
04381                                 }
04382                                 else
04383                                 {
04384                                         $result_array[$row->ref_id] = $row->title;
04385                                 }
04386                         }
04387                 }
04388                 return $result_array;
04389         }
04390 
04398         function _clone($obj_id)
04399         {
04400                 global $ilDB;
04401                 
04402                 $original = new ilObjSurvey($obj_id, false);
04403                 $original->loadFromDb();
04404                 
04405                 $newObj = new ilObjSurvey();
04406                 $newObj->setType("svy");
04407                 $newObj->setTitle($original->getTitle());
04408                 $newObj->setDescription($original->getDescription());
04409                 $newObj->create(true);
04410                 $newObj->createReference();
04411                 $newObj->putInTree($_GET["ref_id"]);
04412                 $newObj->setPermissions($_GET["ref_id"]);
04413 //              $newObj->notify("new",$_GET["ref_id"],$_GET["parent_non_rbac_id"],$_GET["ref_id"],$newObj->getRefId());
04414                 
04415                 $newObj->author = $original->getAuthor();
04416                 $newObj->introduction = $original->getIntroduction();
04417                 $newObj->outro = $original->getOutro();
04418                 $newObj->status = $original->getStatus();
04419                 $newObj->evaluation_access = $original->getEvaluationAccess();
04420                 $newObj->start_date = $original->getStartDate();
04421                 $newObj->startdate_enabled = $original->getStartDateEnabled();
04422                 $newObj->end_date = $original->getEndDate();
04423                 $newObj->enddate_enabled = $original->getEndDateEnabled();
04424                 $newObj->invitation = $original->getInvitation();
04425                 $newObj->invitation_mode = $original->getInvitationMode();
04426                 $newObj->anonymize = $original->getAnonymize();
04427 
04428                 $question_pointer = array();
04429                 // clone the questions
04430                 include_once "./survey/classes/class.SurveyQuestion.php";
04431                 foreach ($original->questions as $key => $question_id)
04432                 {
04433                         $question = ilObjSurvey::_instanciateQuestion($question_id);
04434                         $question->id = -1;
04435                         $original_id = SurveyQuestion::_getOriginalId($question_id);
04436                         $question->saveToDb($original_id);
04437                         $newObj->questions[$key] = $question->getId();
04438                         $question_pointer[$question_id] = $question->getId();
04439                 }
04440 
04441                 $newObj->saveToDb();            
04442 
04443                 // clone the questionblocks
04444                 $questionblocks = array();
04445                 $questionblock_questions = array();
04446                 $query = sprintf("SELECT * FROM survey_questionblock_question WHERE survey_fi = %s",
04447                         $ilDB->quote($original->getSurveyId() . "")
04448                 );
04449                 $result = $ilDB->query($query);
04450                 if ($result->numRows() > 0)
04451                 {
04452                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04453                         {
04454                                 array_push($questionblock_questions, $row);
04455                                 $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
04456                         }
04457                 }
04458                 // create new questionblocks
04459                 foreach ($questionblocks as $key => $value)
04460                 {
04461                         $questionblock = ilObjSurvey::_getQuestionblock($key);
04462                         $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
04463                         $questionblocks[$key] = $questionblock_id;
04464                 }
04465                 // create new questionblock questions
04466                 foreach ($questionblock_questions as $key => $value)
04467                 {
04468                         $clonequery = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
04469                                 $ilDB->quote($newObj->getSurveyId() . ""),
04470                                 $ilDB->quote($questionblocks[$value["questionblock_fi"]] . ""),
04471                                 $ilDB->quote($question_pointer[$value["question_fi"]] . "")
04472                         );
04473                         $cloneresult = $ilDB->query($clonequery);
04474                 }
04475                 
04476                 // clone the constraints
04477                 $constraints = ilObjSurvey::_getConstraints($original->getSurveyId());
04478                 foreach ($constraints as $key => $constraint)
04479                 {
04480                         $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
04481                 }
04482                 
04483                 // clone the obligatory states
04484                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
04485                         $ilDB->quote($original->getSurveyId() . "")
04486                 );
04487                 $result = $ilDB->query($query);
04488                 if ($result->numRows() > 0)
04489                 {
04490                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04491                         {
04492                                 $clonequery = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
04493                                         $ilDB->quote($newObj->getSurveyId() . ""),
04494                                         $ilDB->quote($question_pointer[$row["question_fi"]] . ""),
04495                                         $ilDB->quote($row["obligatory"])
04496                                 );
04497                                 $cloneresult = $ilDB->query($clonequery);
04498                         }
04499                 }
04500 
04501                 // clone meta data
04502                 include_once "./Services/MetaData/classes/class.ilMD.php";
04503                 $md = new ilMD($original->getId(),0,$original->getType());
04504                 $new_md =& $md->cloneMD($newObj->getId(),0,$newObj->getType());
04505                 return $newObj->getRefId();
04506         }
04507 
04513         function createExportDirectory()
04514         {
04515                 include_once "./classes/class.ilUtil.php";
04516                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04517                 ilUtil::makeDir($svy_data_dir);
04518                 if(!is_writable($svy_data_dir))
04519                 {
04520                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04521                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04522                 }
04523                 
04524                 // create learning module directory (data_dir/lm_data/lm_<id>)
04525                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04526                 ilUtil::makeDir($svy_dir);
04527                 if(!@is_dir($svy_dir))
04528                 {
04529                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04530                 }
04531                 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
04532                 $export_dir = $svy_dir."/export";
04533                 ilUtil::makeDir($export_dir);
04534                 if(!@is_dir($export_dir))
04535                 {
04536                         $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
04537                 }
04538         }
04539 
04543         function getExportDirectory()
04544         {
04545                 include_once "./classes/class.ilUtil.php";
04546                 $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
04547 
04548                 return $export_dir;
04549         }
04550         
04554         function getExportFiles($dir)
04555         {
04556                 // quit if import dir not available
04557                 if (!@is_dir($dir) or
04558                         !is_writeable($dir))
04559                 {
04560                         return array();
04561                 }
04562 
04563                 // open directory
04564                 $dir = dir($dir);
04565 
04566                 // initialize array
04567                 $file = array();
04568 
04569                 // get files and save the in the array
04570                 while ($entry = $dir->read())
04571                 {
04572                         if ($entry != "." and
04573                                 $entry != ".." and
04574                                 substr($entry, -4) == ".xml" and
04575                                 ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml\$", $entry))
04576                         {
04577                                 $file[] = $entry;
04578                         }
04579                 }
04580 
04581                 // close import directory
04582                 $dir->close();
04583                 // sort files
04584                 sort ($file);
04585                 reset ($file);
04586 
04587                 return $file;
04588         }
04589 
04595         function createImportDirectory()
04596         {
04597                 include_once "./classes/class.ilUtil.php";
04598                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04599                 ilUtil::makeDir($svy_data_dir);
04600                 
04601                 if(!is_writable($svy_data_dir))
04602                 {
04603                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04604                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04605                 }
04606 
04607                 // create test directory (data_dir/svy_data/svy_<id>)
04608                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04609                 ilUtil::makeDir($svy_dir);
04610                 if(!@is_dir($svy_dir))
04611                 {
04612                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04613                 }
04614 
04615                 // create import subdirectory (data_dir/svy_data/svy_<id>/import)
04616                 $import_dir = $svy_dir."/import";
04617                 ilUtil::makeDir($import_dir);
04618                 if(!@is_dir($import_dir))
04619                 {
04620                         $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
04621                 }
04622         }
04623 
04627         function getImportDirectory()
04628         {
04629                 include_once "./classes/class.ilUtil.php";
04630                 $import_dir = ilUtil::getDataDir()."/svy_data".
04631                         "/svy_".$this->getId()."/import";
04632                 if (!is_dir($import_dir))
04633                 {
04634                         ilUtil::makeDirParents($import_dir);
04635                 }
04636                 if(@is_dir($import_dir))
04637                 {
04638                         return $import_dir;
04639                 }
04640                 else
04641                 {
04642                         return false;
04643                 }
04644         }
04645         
04646         function saveHeading($heading = "", $insertbefore)
04647         {
04648                 global $ilDB;
04649                 if ($heading)
04650                 {
04651                         $query = sprintf("UPDATE survey_survey_question SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
04652                                 $ilDB->quote($heading),
04653                                 $ilDB->quote($this->getSurveyId() . ""),
04654                                 $ilDB->quote($insertbefore)
04655                         );
04656                 }
04657                 else
04658                 {
04659                         $query = sprintf("UPDATE survey_survey_question SET heading=NULL WHERE survey_fi=%s AND question_fi=%s",
04660                                 $ilDB->quote($this->getSurveyId() . ""),
04661                                 $ilDB->quote($insertbefore)
04662                         );
04663                 }
04664                 $ilDB->query($query);
04665         }
04666 
04667         function _getRefIdFromObjId($obj_id)
04668         {
04669                 global $ilDB;
04670                 
04671                 $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
04672                         $ilDB->quote($obj_id)
04673                         
04674                 );
04675                 $result = $ilDB->query($query);
04676                 if ($result->numRows())
04677                 {
04678                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04679                         return $row["ref_id"];
04680                 }
04681                 return 0;
04682         }
04683         
04684         function isAnonymousKey($key)
04685         {
04686                 global $ilDB;
04687                 
04688                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_key = %s AND survey_fi = %s",
04689                         $ilDB->quote($key . ""),
04690                         $ilDB->quote($this->getSurveyId() . "")
04691                 );
04692                 $result = $ilDB->query($query);
04693                 if ($result->numRows() == 1)
04694                 {
04695                         return true;
04696                 }
04697                 else
04698                 {
04699                         return false;
04700                 }
04701         }
04702         
04703         function getUserSurveyCode($user_id)
04704         {
04705                 global $ilDB;
04706                 
04707                 if ($user_id == ANONYMOUS_USER_ID) return "";
04708                 $query = sprintf("SELECT anonymous_id FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
04709                         $ilDB->quote($this->getSurveyId() . ""),
04710                         $ilDB->quote($user_id . "")
04711                 );
04712                 $result = $ilDB->query($query);
04713                 if ($result->numRows() == 1)
04714                 {
04715                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04716                         return $row["anonymous_id"];
04717                 }
04718                 else
04719                 {
04720                         return "";
04721                 }
04722         }
04723         
04724         function isAnonymizedParticipant($key)
04725         {
04726                 global $ilDB;
04727                 
04728                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE anonymous_id = %s AND survey_fi = %s",
04729                         $ilDB->quote($key . ""),
04730                         $ilDB->quote($this->getSurveyId() . "")
04731                 );
04732                 $result = $ilDB->query($query);
04733                 if ($result->numRows() == 1)
04734                 {
04735                         return true;
04736                 }
04737                 else
04738                 {
04739                         return false;
04740                 }
04741         }
04742         
04743         function checkSurveyCode($code)
04744         {
04745                 if ($this->isAnonymousKey($code))
04746                 {
04747                         if ($this->isSurveyStarted("", $code) == 1)
04748                         {
04749                                 return false;
04750                         }
04751                         else
04752                         {
04753                                 return true;
04754                         }
04755                 }
04756                 else
04757                 {
04758                         return false;
04759                 }
04760         }
04761 
04762         function getSurveyCodesCount()
04763         {
04764                 global $ilDB;
04765 
04766                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_fi = %s AND ISNULL(user_key)",
04767                         $ilDB->quote($this->getSurveyId() . "")
04768                 );
04769                 $result = $ilDB->query($query);
04770                 return $result->numRows();
04771         }
04772         
04773         function &getSurveyCodesTableData($lang = "en", $offset = 0, $limit = 10, $sort_by = "counter", $sort_order = "asc")
04774         {
04775                 global $ilDB;
04776 
04777                 include_once "./classes/class.ilFormat.php";
04778                 if (strlen($lang) == 0) $lang = "en";
04779                 if (strlen($offset) == 0) $offset = 0;
04780                 if (strlen($limit) == 0) $limit = 10;
04781                 if (strlen($sort_by) == 0) $sort_by = "counter";
04782                 if (strlen($sort_order) == 0) $sort_order = "asc";
04783                 
04784                 switch ($sort_by)
04785                 {
04786                         case "counter":
04787                                 $order = "ORDER BY survey_finished.anonymous_id $sort_order";
04788                                 break;
04789                         case "date":
04790                                 $order = "ORDER BY survey_finished.anonymous_id $sort_order";
04791                                 break;
04792                         case "used":
04793                                 $order = "ORDER BY survey_finished.anonymous_id $sort_order";
04794                                 break;
04795                         case "url":
04796                                 $order = "ORDER BY survey_finished.anonymous_id $sort_order";
04797                                 break;
04798                 }
04799                 
04800                 $codes = array();
04801                 $query = sprintf("SELECT survey_anonymous.anonymous_id, survey_anonymous.survey_key, survey_anonymous.survey_fi, survey_anonymous.TIMESTAMP + 0 AS TIMESTAMP14, survey_finished.state FROM survey_anonymous LEFT JOIN survey_finished ON survey_anonymous.survey_key = survey_finished.anonymous_id WHERE survey_anonymous.survey_fi = %s AND ISNULL(survey_anonymous.user_key) $order LIMIT $offset,$limit",
04802                         $ilDB->quote($this->getSurveyId() . "")
04803                 );
04804                 $result = $ilDB->query($query);
04805                 $counter = $offset+1;
04806                 if ($result->numRows() > 0)
04807                 {
04808                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04809                         {
04810                                 $created = ilFormat::formatDate(ilFormat::ftimestamp2dateDB($row["TIMESTAMP14"]), "date");
04811 
04812                                 $url = "";
04813                                 
04814                                 $state = "<span class=\"smallred\">" . $this->lng->txt("not_used") . "</span>";
04815                                 if ($this->isSurveyCodeUsed($row["survey_key"]))
04816                                 {
04817                                         $state = "<span class=\"smallgreen\">" . $this->lng->txt("used") . "</span>";
04818                                 }
04819                                 else
04820                                 {
04821                                         $addlang = "";
04822                                         if (strlen($lang))
04823                                         {
04824                                                 $addlang = "&amp;lang=$lang";
04825                                         }
04826                                         $url = "<a href=\"" . ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$row["survey_key"].$addlang . "\">";
04827                                         $url .= $this->lng->txt("survey_code_url_name");
04828                                         $url .= "</a>";
04829                                 }
04830                                 array_push($codes, array($counter, $row["survey_key"], $created, $state, $url));
04831                                 $counter++;
04832                         }
04833                 }
04834                 return $codes;
04835         }
04836         
04837         function isSurveyCodeUsed($code)
04838         {
04839                 global $ilDB;
04840                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
04841                         $ilDB->quote($this->getSurveyId() . ""),
04842                         $ilDB->quote($code)
04843                 );
04844                 $result = $ilDB->query($query);
04845                 if ($result->numRows() > 0)
04846                 {
04847                         return TRUE;
04848                 }
04849                 else
04850                 {
04851                         return FALSE;
04852                 }
04853         }
04854         
04855         function createSurveyCodes($nrOfCodes)
04856         {
04857                 global $ilDB;
04858                 for ($i = 0; $i < $nrOfCodes; $i++)
04859                 {
04860                         $anonymize_key = $this->createNewAccessCode();
04861                         $query = sprintf("INSERT INTO survey_anonymous (anonymous_id, survey_key, survey_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
04862                                 $ilDB->quote($anonymize_key . ""),
04863                                 $ilDB->quote($this->getSurveyId() . "")
04864                         );
04865                         $result = $ilDB->query($query);
04866                 }
04867         }
04868         
04877         function getUserAccessCode($user_id)
04878         {
04879                 global $ilDB;
04880                 $access_code = "";
04881                 $query = sprintf("SELECT survey_key FROM survey_anonymous WHERE survey_fi = %s AND user_key = %s",
04882                         $ilDB->quote($this->getSurveyId() . ""),
04883                         $ilDB->quote(md5($user_id))
04884                 );
04885                 $result = $ilDB->query($query);
04886                 if ($result->numRows())
04887                 {
04888                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04889                         $access_code = $row["survey_key"];
04890                 }
04891                 return $access_code;
04892         }
04893         
04902         function saveUserAccessCode($user_id, $access_code)
04903         {
04904                 global $ilDB;
04905                 $query = sprintf("INSERT INTO survey_anonymous (survey_key, survey_fi, user_key) VALUES (%s, %s, %s)",
04906                         $ilDB->quote($access_code . ""),
04907                         $ilDB->quote($this->getSurveyId() . ""),
04908                         $ilDB->quote(md5($user_id) . "")
04909                 );
04910                 $result = $ilDB->query($query);
04911         }
04912         
04918         function createNewAccessCode()
04919         {
04920                 // create a 5 character code
04921                 $codestring = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
04922                 mt_srand();
04923                 $code = "";
04924                 for ($i = 1; $i <=5; $i++)
04925                 {
04926                         $index = mt_rand(0, strlen($codestring)-1);
04927                         $code .= substr($codestring, $index, 1);
04928                 }
04929                 // verify it against the database
04930                 while ($this->isSurveyCodeUsed($code))
04931                 {
04932                         $code = $this->createNewAccessCode();
04933                 }
04934                 return $code;
04935         }
04936 
04949         function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
04950         {
04951                 $resultarray = array();
04952                 foreach ($row as $rowindex => $entry)
04953                 {
04954                         $surround = FALSE;
04955                         if ($quoteAll)
04956                         {
04957                                 $surround = TRUE;
04958                         }
04959                         if (strpos($entry, "\"") !== FALSE)
04960                         {
04961                                 $entry = str_replace("\"", "\"\"", $entry);
04962                                 $surround = TRUE;
04963                         }
04964                         if (strpos($entry, $separator) !== FALSE)
04965                         {
04966                                 $surround = TRUE;
04967                         }
04968                         // replace all CR LF with LF (for Excel for Windows compatibility
04969                         $entry = str_replace(chr(13).chr(10), chr(10), $entry);
04970                         if ($surround)
04971                         {
04972                                 $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
04973                         }
04974                         else
04975                         {
04976                                 $resultarray[$rowindex] = utf8_decode($entry);
04977                         }
04978                 }
04979                 return $resultarray;
04980         }
04981 
04982         function _getLastAccess($finished_id)
04983         {
04984                 global $ilDB;
04985                 
04986                 $query = sprintf("SELECT survey_answer.TIMESTAMP+0 AS TIMESTAMP14 FROM survey_answer, survey_finished WHERE (survey_finished.user_fi = survey_answer.user_fi OR survey_finished.anonymous_id = survey_answer.anonymous_id) AND survey_finished.finished_id = %s ORDER BY survey_answer.TIMESTAMP DESC",
04987                         $ilDB->quote($finished_id . "")
04988                 );
04989                 $result = $ilDB->query($query);
04990                 if ($result->numRows())
04991                 {
04992                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04993                         return $row["TIMESTAMP14"];
04994                 }
04995                 return "";
04996         }
04997 
05004         function prepareTextareaOutput($txt_output)
05005         {
05006                 include_once "./classes/class.ilObjAdvancedEditing.php";
05007                 $result = $txt_output;
05008                 if ($prepare_for_latex_output)
05009                 {
05010                         $result = ilUtil::insertLatexImages($result, "<span class\=\"latex\">", "<\/span>", URL_TO_LATEX);
05011                 }
05012                 // removed: did not work with magic_quotes_gpc = On
05013                 //$result = ilUtil::stripSlashes($result, true, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("survey"));
05014                 if (!$this->isHTML($result))
05015                 {
05016                         // if the string does not contain HTML code, replace the newlines with HTML line breaks
05017                         $result = preg_replace("/[\n]/", "<br />", $result);
05018                 }
05019                 else
05020                 {
05021                         // patch for problems with the <pre> tags in tinyMCE
05022                         if (preg_match_all("/(<pre>.*?<\/pre>)/ims", $result, $matches))
05023                         {
05024                                 foreach ($matches[0] as $found)
05025                                 {
05026                                         $replacement = "";
05027                                         if (strpos("\n", $found) === FALSE)
05028                                         {
05029                                                 $replacement = "\n";
05030                                         }
05031                                         $removed = preg_replace("/<br\s*?\/>/ims", $replacement, $found);
05032                                         $result = str_replace($found, $removed, $result);
05033                                 }
05034                         }
05035                 }
05036                 $result = str_replace("{", "&#123;", $result);
05037                 $result = str_replace("}", "&#125;", $result);
05038                 $result = str_replace("\\", "&#92;", $result);
05039                 return $result;
05040         }
05041 
05050         function isHTML($a_text)
05051         {
05052                 if (preg_match("/<[^>]*?>/", $a_text))
05053                 {
05054                         return TRUE;
05055                 }
05056                 else
05057                 {
05058                         return FALSE; 
05059                 }
05060         }
05061 
05062 } // END class.ilObjSurvey
05063 ?>

Generated on Fri Dec 13 2013 13:52:15 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1