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

survey/classes/class.ilObjSurvey.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 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 $status;
00077 
00085   var $evaluation_access;
00086 
00094   var $start_date;
00095 
00103         var $startdate_enabled;
00104 
00112   var $end_date;
00113 
00121         var $enddate_enabled;
00122 
00130         var $questions;
00131 
00139         var $invitation;
00140 
00148         var $invitation_mode;
00149         
00156         var $anonymize;
00157 
00164         var $display_question_titles;
00165 
00172         function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
00173         {
00174                 global $ilUser;
00175                 $this->type = "svy";
00176                 $this->ilObject($a_id,$a_call_by_reference);
00177 
00178                 $this->survey_id = -1;
00179                 $this->introduction = "";
00180                 $this->author = $ilUser->fullname;
00181                 $this->status = STATUS_OFFLINE;
00182                 $this->evaluation_access = EVALUATION_ACCESS_OFF;
00183                 $this->startdate_enabled = 0;
00184                 $this->enddate_enabled = 0;
00185                 $this->questions = array();
00186                 $this->invitation = INVITATION_OFF;
00187                 $this->invitation_mode = MODE_PREDEFINED_USERS;
00188                 $this->anonymize = ANONYMIZE_OFF;
00189                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
00190         }
00191 
00195         function create($a_upload = false)
00196         {
00197                 parent::create();
00198                 $this->createMetaData();
00199         }
00200 
00207         function update()
00208         {
00209                 $this->updateMetaData();
00210 
00211                 if (!parent::update())
00212                 {
00213                         return false;
00214                 }
00215 
00216                 // put here object specific stuff
00217 
00218                 return true;
00219         }
00220 
00221         function createReference() 
00222         {
00223                 $result = parent::createReference();
00224                 $this->saveToDb();
00225                 return $result;
00226         }
00227 
00233         function read($a_force_db = false)
00234         {
00235                 parent::read($a_force_db);
00236                 $this->loadFromDb();
00237         }
00238 
00246         function ilClone($a_parent_ref)
00247         {               
00248                 global $rbacadmin;
00249 
00250                 // always call parent ilClone function first!!
00251                 $new_ref_id = parent::ilClone($a_parent_ref);
00252                 
00253                 // get object instance of ilCloned object
00254                 //$newObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00255 
00256                 // create a local role folder & default roles
00257                 //$roles = $newObj->initDefaultRoles();
00258 
00259                 // ...finally assign role to creator of object
00260                 //$rbacadmin->assignUser($roles[0], $newObj->getOwner(), "n");          
00261 
00262                 // always destroy objects in ilClone method because ilClone() is recursive and creates instances for each object in subtree!
00263                 //unset($newObj);
00264 
00265                 // ... and finally always return new reference ID!!
00266                 return $new_ref_id;
00267         }
00268 
00275         function delete()
00276         {
00277                 $remove = parent::delete();
00278                 // always call parent delete function first!!
00279                 if (!$remove)
00280                 {
00281                         return false;
00282                 }
00283 
00284                 $this->deleteMetaData();
00285 
00286                 // Delete all survey questions, constraints and materials
00287                 foreach ($this->questions as $question_id)
00288                 {
00289                         $this->removeQuestion($question_id);
00290                 }
00291                 $this->deleteSurveyRecord();
00292                 
00293                 return true;
00294         }
00295         
00303         function deleteSurveyRecord()
00304         {
00305                 $query = sprintf("DELETE FROM survey_survey WHERE survey_id = %s",
00306                         $this->ilias->db->quote($this->getSurveyId())
00307                 );
00308                 $result = $this->ilias->db->query($query);
00309 
00310                 $query = sprintf("SELECT questionblock_fi FROM survey_questionblock_question WHERE survey_fi = %s",
00311                         $this->ilias->db->quote($this->getSurveyId())
00312                 );
00313                 $result = $this->ilias->db->query($query);
00314                 $questionblocks = array();
00315                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00316                 {
00317                         array_push($questionblocks, $row["questionblock_fi"]);
00318                 }
00319                 if (count($questionblocks))
00320                 {
00321                         $query = "DELETE FROM survey_questionblock WHERE questionblock_id IN (" . join($questionblocks, ",") . ")";
00322                         $result = $this->ilias->db->query($query);
00323                 }
00324                 $query = sprintf("DELETE FROM survey_questionblock_question WHERE survey_fi = %s",
00325                         $this->ilias->db->quote($this->getSurveyId())
00326                 );
00327                 $result = $this->ilias->db->query($query);
00328                 
00329                 $this->deleteAllUserData();
00330 
00331                 // delete export files
00332                 include_once "./classes/class.ilUtil.php";
00333                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
00334                 $directory = $svy_data_dir."/svy_".$this->getId();
00335                 if (is_dir($directory))
00336                 {
00337                         include_once "./classes/class.ilUtil.php";
00338                         ilUtil::delDir($directory);
00339                 }
00340         }
00341         
00349         function deleteAllUserData()
00350         {
00351                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
00352                         $this->ilias->db->quote($this->getSurveyId())
00353                 );
00354                 $result = $this->ilias->db->query($query);
00355                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00356                 {
00357                         $this->disinviteUser($row["user_fi"]);
00358                 }
00359 
00360                 $query = sprintf("DELETE FROM survey_finished WHERE survey_fi = %s",
00361                         $this->ilias->db->quote($this->getSurveyId())
00362                 );
00363                 $result = $this->ilias->db->query($query);
00364 
00365                 $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s",
00366                         $this->ilias->db->quote($this->getSurveyId())
00367                 );
00368                 $result = $this->ilias->db->query($query);
00369 
00370                 $query = sprintf("DELETE FROM survey_anonymous WHERE survey_fi = %s",
00371                         $this->ilias->db->quote($this->getSurveyId())
00372                 );
00373                 $result = $this->ilias->db->query($query);
00374         }
00375 
00389         function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
00390         {
00391                 global $tree;
00392                 
00393                 switch ($a_event)
00394                 {
00395                         case "link":
00396                                 
00397                                 //var_dump("<pre>",$a_params,"</pre>");
00398                                 //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
00399                                 //exit;
00400                                 break;
00401                         
00402                         case "cut":
00403                                 
00404                                 //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
00405                                 //exit;
00406                                 break;
00407                                 
00408                         case "copy":
00409                         
00410                                 //var_dump("<pre>",$a_params,"</pre>");
00411                                 //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
00412                                 //exit;
00413                                 break;
00414 
00415                         case "paste":
00416                                 
00417                                 //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
00418                                 //exit;
00419                                 break;
00420                         
00421                         case "new":
00422                                 
00423                                 //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
00424                                 //exit;
00425                                 break;
00426                 }
00427                 
00428                 // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
00429                 if ($a_node_id==$_GET["ref_id"])
00430                 {       
00431                         $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
00432                         $parent_type = $parent_obj->getType();
00433                         if($parent_type == $this->getType())
00434                         {
00435                                 $a_node_id = (int) $tree->getParentId($a_node_id);
00436                         }
00437                 }
00438                 
00439                 parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
00440         }
00441 
00450         function isComplete()
00451         {
00452                 if (($this->getTitle()) and ($this->author) and (count($this->questions)))
00453                 {
00454                         return true;
00455                 } 
00456                         else 
00457                 {
00458                         return false;
00459                 }
00460         }
00461 
00470         function _isComplete($obj_id)
00471         {
00472                 $survey = new ilObjSurvey($obj_id, false);
00473                 $survey->loadFromDb();
00474                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00475                 {
00476                         return true;
00477                 } 
00478                         else 
00479                 {
00480                         return false;
00481                 }
00482         }
00483 
00492         function &_getGlobalSurveyData($obj_id)
00493         {
00494                 $survey = new ilObjSurvey($obj_id, false);
00495                 $survey->loadFromDb();
00496                 $result = array();
00497                 if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
00498                 {
00499                         $result["complete"] = true;
00500                 } 
00501                         else 
00502                 {
00503                         $result["complete"] = false;
00504                 }
00505                 $result["evaluation_access"] = $survey->getEvaluationAccess();
00506                 return $result;
00507         }
00508 
00516         function saveCompletionStatus() 
00517         {
00518                 $complete = 0;
00519                 if ($this->isComplete()) 
00520                 {
00521                         $complete = 1;
00522                 }
00523     if ($this->survey_id > 0) 
00524                 {
00525                         $query = sprintf("UPDATE survey_survey SET complete = %s WHERE survey_id = %s",
00526                                 $this->ilias->db->quote("$complete"),
00527                                 $this->ilias->db->quote($this->survey_id) 
00528                         );
00529       $result = $this->ilias->db->query($query);
00530                 }
00531         }
00532 
00542         function duplicateQuestionForSurvey($question_id)
00543         {
00544                 global $ilUser;
00545                 
00546                 $questiontype = $this->getQuestionType($question_id);
00547                 $question_gui = $this->getQuestionGUI($questiontype, $question_id);
00548                 $duplicate_id = $question_gui->object->duplicate(true);
00549                 return $duplicate_id;
00550         }
00551 
00559         function insertQuestion($question_id) 
00560         {
00561                 include_once "./survey/classes/class.SurveyQuestion.php";
00562                 if (!SurveyQuestion::_isComplete($question_id))
00563                 {
00564                         return FALSE;
00565                 }
00566                 else
00567                 {
00568                         // get maximum sequence index in test
00569                         $query = sprintf("SELECT survey_question_id FROM survey_survey_question WHERE survey_fi = %s",
00570                                 $this->ilias->db->quote($this->getSurveyId())
00571                         );
00572                         $result = $this->ilias->db->query($query);
00573                         $sequence = $result->numRows();
00574                         $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
00575                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00576                                 $this->ilias->db->quote($this->getSurveyId()),
00577                                 $this->ilias->db->quote($duplicate_id),
00578                                 $this->ilias->db->quote($sequence)
00579                         );
00580                         $result = $this->ilias->db->query($query);
00581                         if ($result != DB_OK) 
00582                         {
00583                                 // Error
00584                         }
00585                         $this->loadQuestionsFromDb();
00586                         return TRUE;
00587                 }
00588         }
00589 
00590 
00591         
00599         function insertQuestionblock($questionblock_id) 
00600         {
00601                 $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",
00602                         $this->ilias->db->quote($questionblock_id)
00603                 );
00604                 $result = $this->ilias->db->query($query);
00605                 $questions = array();
00606                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00607                 {
00608                         array_push($questions, $row["question_id"]);
00609                         $title = $row["title"];
00610                 }
00611                 $this->createQuestionblock($title, $questions);
00612         }
00613         
00621   function saveToDb()
00622   {
00623                 $complete = 0;
00624                 if ($this->isComplete()) 
00625                 {
00626                         $complete = 1;
00627                 }
00628                 $startdate = $this->getStartDate();
00629                 if (!$startdate or !$this->startdate_enabled)
00630                 {
00631                         $startdate = "NULL";
00632                 }
00633                 else
00634                 {
00635                         $startdate = $this->ilias->db->quote($startdate);
00636                 }
00637                 $enddate = $this->getEndDate();
00638                 if (!$enddate or !$this->enddate_enabled)
00639                 {
00640                         $enddate = "NULL";
00641                 }
00642                 else
00643                 {
00644                         $enddate = $this->ilias->db->quote($enddate);
00645                 }
00646     if ($this->survey_id == -1) 
00647                 {
00648       // Write new dataset
00649       $now = getdate();
00650       $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
00651       $query = sprintf("INSERT INTO survey_survey (survey_id, obj_fi, author, introduction, status, startdate, enddate, evaluation_access, invitation, invitation_mode, complete, created, anonymize, show_question_titles, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
00652                                 $this->ilias->db->quote($this->getId()),
00653                                 $this->ilias->db->quote($this->author . ""),
00654                                 $this->ilias->db->quote($this->introduction . ""),
00655                                 $this->ilias->db->quote($this->status . ""),
00656                                 $startdate,
00657                                 $enddate,
00658                                 $this->ilias->db->quote($this->evaluation_access . ""),
00659                                 $this->ilias->db->quote($this->invitation . ""),
00660                                 $this->ilias->db->quote($this->invitation_mode . ""),
00661                                 $this->ilias->db->quote($complete . ""),
00662                                 $this->ilias->db->quote($this->getAnonymize() . ""),
00663                                 $this->ilias->db->quote($this->getShowQuestionTitles() . ""),
00664                                 $this->ilias->db->quote($created)
00665       );
00666       $result = $this->ilias->db->query($query);
00667       if ($result == DB_OK) 
00668                         {
00669         $this->survey_id = $this->ilias->db->getLastInsertId();
00670       }
00671     } 
00672                 else 
00673                 {
00674       // update existing dataset
00675                         $query = sprintf("UPDATE survey_survey SET author = %s, introduction = %s, status = %s, startdate = %s, enddate = %s, evaluation_access = %s, invitation = %s, invitation_mode = %s, complete = %s, anonymize = %s, show_question_titles = %s WHERE survey_id = %s",
00676                                 $this->ilias->db->quote($this->author . ""),
00677                                 $this->ilias->db->quote($this->introduction . ""),
00678                                 $this->ilias->db->quote($this->status . ""),
00679                                 $startdate,
00680                                 $enddate,
00681                                 $this->ilias->db->quote($this->evaluation_access . ""),
00682                                 $this->ilias->db->quote($this->invitation . ""),
00683                                 $this->ilias->db->quote($this->invitation_mode . ""),
00684                                 $this->ilias->db->quote($complete . ""),
00685                                 $this->ilias->db->quote($this->getAnonymize() . ""),
00686                                 $this->ilias->db->quote($this->getShowQuestionTitles() . ""),
00687                                 $this->ilias->db->quote($this->survey_id)
00688       );
00689       $result = $this->ilias->db->query($query);
00690     }
00691     if ($result == DB_OK) 
00692                 {
00693                         // save questions to db
00694                         $this->saveQuestionsToDb();
00695     }
00696   }
00697 
00706         function saveQuestionsToDb() 
00707         {
00708                 // save old questions state
00709                 $old_questions = array();
00710                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
00711                         $this->ilias->db->quote($this->getSurveyId())
00712                 );
00713                 $result = $this->ilias->db->query($query);
00714                 if ($result->numRows())
00715                 {
00716                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00717                         {
00718                                 $old_questions[$row["question_fi"]] = $row;
00719                         }
00720                 }
00721                 
00722                 // delete existing question relations
00723     $query = sprintf("DELETE FROM survey_survey_question WHERE survey_fi = %s",
00724                         $this->ilias->db->quote($this->getSurveyId())
00725                 );
00726                 $result = $this->ilias->db->query($query);
00727                 // create new question relations
00728                 foreach ($this->questions as $key => $value) 
00729                 {
00730                         $query = sprintf("INSERT INTO survey_survey_question (survey_question_id, survey_fi, question_fi, heading, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
00731                                 $this->ilias->db->quote($this->getSurveyId() . ""),
00732                                 $this->ilias->db->quote($value . ""),
00733                                 $this->ilias->db->quote($old_questions[$value]["heading"]),
00734                                 $this->ilias->db->quote($key . "")
00735                         );
00736                         $result = $this->ilias->db->query($query);
00737                 }
00738         }
00739 
00749         function getAnonymousId($id)
00750         {
00751                 $query = sprintf("SELECT anonymous_id FROM survey_answer WHERE anonymous_id = %s",
00752                         $this->ilias->db->quote($id)
00753                 );
00754                 $result = $this->ilias->db->query($query);
00755                 if ($result->numRows())
00756                 {
00757                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00758                         return $row["anonymous_id"];
00759                 }
00760                 else
00761                 {
00762                         return "";
00763                 }
00764         }
00765 
00774         function getQuestionGUI($questiontype, $question_id)
00775         {
00776                 switch ($questiontype)
00777                 {
00778                         case "qt_nominal":
00779                                 include_once "./survey/classes/class.SurveyNominalQuestionGUI.php";
00780                                 $question = new SurveyNominalQuestionGUI();
00781                                 break;
00782                         case "qt_ordinal":
00783                                 include_once "./survey/classes/class.SurveyOrdinalQuestionGUI.php";
00784                                 $question = new SurveyOrdinalQuestionGUI();
00785                                 break;
00786                         case "qt_metric":
00787                                 include_once "./survey/classes/class.SurveyMetricQuestionGUI.php";
00788                                 $question = new SurveyMetricQuestionGUI();
00789                                 break;
00790                         case "qt_text":
00791                                 include_once "./survey/classes/class.SurveyTextQuestionGUI.php";
00792                                 $question = new SurveyTextQuestionGUI();
00793                                 break;
00794                 }
00795                 $question->object->loadFromDb($question_id);
00796                 return $question;
00797         }
00798         
00808   function getQuestionType($question_id) 
00809         {
00810     if ($question_id < 1) return -1;
00811     $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",
00812       $this->ilias->db->quote($question_id)
00813     );
00814     $result = $this->ilias->db->query($query);
00815     if ($result->numRows() == 1) 
00816                 {
00817       $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00818       return $data->type_tag;
00819     } 
00820                 else 
00821                 {
00822       return "";
00823     }
00824   }
00825         
00834         function getSurveyId()
00835         {
00836                 return $this->survey_id;
00837         }
00838         
00842         function setAnonymize($a_anonymize)
00843         {
00844                 $this->anonymize = $a_anonymize;
00845         }
00846 
00852         function getAnonymize()
00853         {
00854                 return $this->anonymize;
00855         }
00856         
00862         function isAccessibleWithoutCode()
00863         {
00864                 return false;
00865                 /*
00866                 // patch for HSU-HH, activate when installed
00867                 // simply remove the line above and every survey which
00868                 // is accessible by anonymous could be done without survey code
00869                 if ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)
00870                 {
00871                         return true;
00872                 }
00873                 else
00874                 {
00875                         return false;
00876                 }*/
00877         }
00878 
00886   function loadFromDb()
00887   {
00888     $query = sprintf("SELECT * FROM survey_survey WHERE obj_fi = %s",
00889       $this->ilias->db->quote($this->getId())
00890     );
00891     $result = $this->ilias->db->query($query);
00892     if (strcmp(strtolower(get_class($result)), db_result) == 0) 
00893                 {
00894       if ($result->numRows() == 1) 
00895                         {
00896                                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00897                                 $this->survey_id = $data->survey_id;
00898                                 $this->author = $data->author;
00899                                 $this->introduction = $data->introduction;
00900                                 $this->status = $data->status;
00901                                 $this->invitation = $data->invitation;
00902                                 $this->invitation_mode = $data->invitation_mode;
00903                                 $this->display_question_titles = $data->show_question_titles;
00904                                 $this->start_date = $data->startdate;
00905                                 if (!$data->startdate)
00906                                 {
00907                                         $this->startdate_enabled = 0;
00908                                 }
00909                                 else
00910                                 {
00911                                         $this->startdate_enabled = 1;
00912                                 }
00913         $this->end_date = $data->enddate;
00914                                 if (!$data->enddate)
00915                                 {
00916                                         $this->enddate_enabled = 0;
00917                                 }
00918                                 else
00919                                 {
00920                                         $this->enddate_enabled = 1;
00921                                 }
00922                                 if (!$data->anonymize)
00923                                 {
00924                                         $this->setAnonymize(ANONYMIZE_OFF);
00925                                 }
00926                                 else
00927                                 {
00928                                         $this->setAnonymize(ANONYMIZE_ON);
00929                                 }
00930         $this->evaluation_access = $data->evaluation_access;
00931                                 $this->loadQuestionsFromDb();
00932       }
00933     }
00934         }
00935 
00944         function loadQuestionsFromDb() 
00945         {
00946                 $this->questions = array();
00947                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
00948                         $this->ilias->db->quote($this->survey_id)
00949                 );
00950                 $result = $this->ilias->db->query($query);
00951                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
00952                 {
00953                         $this->questions[$data->sequence] = $data->question_fi;
00954                 }
00955         }
00956 
00966         function setStartDateEnabled($enabled = false)
00967         {
00968                 if ($enabled)
00969                 {
00970                         $this->startdate_enabled = 1;
00971                 }
00972                 else
00973                 {
00974                         $this->startdate_enabled = 0;
00975                 }
00976         }
00977         
00987         function getStartDateEnabled()
00988         {
00989                 return $this->startdate_enabled;
00990         }
00991 
01001         function setEndDateEnabled($enabled = false)
01002         {
01003                 if ($enabled)
01004                 {
01005                         $this->enddate_enabled = 1;
01006                 }
01007                 else
01008                 {
01009                         $this->enddate_enabled = 0;
01010                 }
01011         }
01012         
01022         function getEndDateEnabled()
01023         {
01024                 return $this->enddate_enabled;
01025         }
01026 
01036         function setAuthor($author = "") 
01037         {
01038     if (!$author) 
01039                 {
01040       $author = $this->ilias->account->fullname;
01041     }
01042     $this->author = $author;
01043   }
01044 
01054   function getShowQuestionTitles() 
01055         {
01056                 return $this->display_question_titles;
01057   }
01058 
01067   function showQuestionTitles() 
01068         {
01069                 $this->display_question_titles = QUESTIONTITLES_VISIBLE;
01070   }
01071 
01080   function hideQuestionTitles() 
01081         {
01082                 $this->display_question_titles = QUESTIONTITLES_HIDDEN;
01083   }
01084         
01094   function setInvitation($invitation = 0) 
01095         {
01096     $this->invitation = $invitation;
01097                 // remove the survey from the personal desktops
01098                 $query = sprintf("DELETE FROM desktop_item WHERE type = %s AND item_id = %s",
01099                         $this->ilias->db->quote("svy"),
01100                         $this->ilias->db->quote($this->getRefId())
01101                 );
01102                 $result = $this->ilias->db->query($query);
01103                 if ($invitation == INVITATION_OFF)
01104                 {
01105                         // already removed prior
01106                 }
01107                 else if ($invitation == INVITATION_ON)
01108                 {
01109                         if ($this->getInvitationMode() == MODE_UNLIMITED)
01110                         {
01111                                 $query = "SELECT usr_id FROM usr_data";
01112                                 $result = $this->ilias->db->query($query);
01113                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01114                                 {
01115                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01116                                                 $this->ilias->db->quote($row["usr_id"]),
01117                                                 $this->ilias->db->quote($this->getRefId()),
01118                                                 $this->ilias->db->quote("svy")
01119                                         );
01120                                         $insertresult = $this->ilias->db->query($query);
01121                                 }
01122                         }
01123                         else
01124                         {
01125                                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
01126                                         $this->ilias->db->quote($this->getSurveyId())
01127                                 );
01128                                 $result = $this->ilias->db->query($query);
01129                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01130                                 {
01131                                         $query = sprintf("INSERT INTO desktop_item (user_id, item_id, type, parameters) VALUES (%s, %s, %s, NULL)",
01132                                                 $this->ilias->db->quote($row["user_fi"]),
01133                                                 $this->ilias->db->quote($this->getRefId()),
01134                                                 $this->ilias->db->quote("svy")
01135                                         );
01136                                         $insertresult = $this->ilias->db->query($query);
01137                                 }
01138                                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
01139                                         $this->ilias->db->quote($this->getSurveyId())
01140                                 );
01141                                 $result = $this->ilias->db->query($query);
01142                                 include_once "./classes/class.ilObjGroup.php";
01143                                 include_once "./classes/class.ilObjUser.php";
01144                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01145                                 {
01146                                         $group = new ilObjGroup($row["group_fi"]);
01147                                         $members = $group->getGroupMemberIds();
01148                                         foreach ($members as $user_id)
01149                                         {
01150                                                 $user = new ilObjUser($user_id);
01151                                                 $user->addDesktopItem($this->getRefId(), "svy");
01152                                         }
01153                                 }
01154                         }
01155                 }
01156   }
01157 
01167   function setInvitationMode($invitation_mode = 0) 
01168         {
01169     $this->invitation_mode = $invitation_mode;
01170                 if ($invitation_mode == MODE_UNLIMITED)
01171                 {
01172                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01173                                 $this->ilias->db->quote($this->getSurveyId())
01174                         );
01175                         $result = $this->ilias->db->query($query);
01176                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01177                                 $this->ilias->db->quote($this->getSurveyId())
01178                         );
01179                         $result = $this->ilias->db->query($query);
01180                 }
01181                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01182                 $this->setInvitation($this->getInvitation());
01183   }
01184         
01195         function setInvitationAndMode($invitation = 0, $invitation_mode = 0)
01196         {
01197     $this->invitation_mode = $invitation_mode;
01198                 if ($invitation_mode == MODE_UNLIMITED)
01199                 {
01200                         $query = sprintf("DELETE FROM survey_invited_group WHERE survey_fi = %s",
01201                                 $this->ilias->db->quote($this->getSurveyId())
01202                         );
01203                         $result = $this->ilias->db->query($query);
01204                         $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s",
01205                                 $this->ilias->db->quote($this->getSurveyId())
01206                         );
01207                         $result = $this->ilias->db->query($query);
01208                 }
01209                 // add/remove the survey from personal desktops -> calling getInvitation with the same value makes all changes for the new invitation mode
01210                 $this->setInvitation($invitation);
01211         }
01212 
01222   function setIntroduction($introduction = "") 
01223         {
01224     $this->introduction = $introduction;
01225   }
01226 
01236   function getAuthor() 
01237         {
01238     return $this->author;
01239   }
01240 
01250   function getInvitation() 
01251         {
01252     return $this->invitation;
01253   }
01254 
01264   function getInvitationMode() 
01265         {
01266     return $this->invitation_mode;
01267   }
01268 
01278   function getStatus() 
01279         {
01280     return $this->status;
01281   }
01282 
01292   function isOnline() 
01293         {
01294     if ($this->status == STATUS_ONLINE)
01295                 {
01296                         return true;
01297                 }
01298                 else
01299                 {
01300                         return false;
01301                 }
01302   }
01303 
01313   function isOffline() 
01314         {
01315     if ($this->status == STATUS_OFFLINE)
01316                 {
01317                         return true;
01318                 }
01319                 else
01320                 {
01321                         return false;
01322                 }
01323   }
01324 
01335   function setStatus($status = STATUS_OFFLINE) 
01336         {
01337                 $result = "";
01338                 if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
01339                 {
01340         $this->status = STATUS_OFFLINE;
01341                         $result = $this->lng->txt("cannot_switch_to_online_no_questions");
01342                 }
01343                 else
01344                 {
01345         $this->status = $status;
01346                 }
01347                 return $result;
01348   }
01349 
01359   function getStartDate() 
01360         {
01361     return $this->start_date;
01362   }
01363 
01372         function canStartSurvey()
01373         {
01374                 global $ilAccess;
01375                 
01376                 $result = TRUE;
01377                 $messages = array();
01378                 // check start date
01379                 if ($this->getStartDateEnabled())
01380                 {
01381                         $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
01382                         $now = mktime();
01383                         if ($now < $epoch_time) 
01384                         {
01385                                 array_push($messages, $this->lng->txt("start_date_not_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getStartYear().$this->getStartMonth().$this->getStartDay()."000000"), "date") . ")");
01386                                 $result = FALSE;
01387                         }
01388                 }
01389                 // check end date
01390                 if ($this->getEndDateEnabled())
01391                 {
01392                         $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
01393                         $now = mktime();
01394                         if ($now > $epoch_time) 
01395                         {
01396                                 array_push($messages, $this->lng->txt("end_date_reached") . " (".ilFormat::formatDate(ilFormat::ftimestamp2dateDB($this->getEndYear().$this->getEndMonth().$this->getEndDay()."000000"), "date") . ")");
01397                                 $result = FALSE;
01398                         }
01399                 }
01400                 // check online status
01401                 if ($this->getStatus() == STATUS_OFFLINE)
01402                 {
01403                         array_push($messages, $this->lng->txt("survey_is_offline"));
01404                         $result = FALSE;
01405                 }
01406                 // check rbac permissions
01407                 if ((!$ilAccess->checkAccess("read", "", $this->ref_id)) || (!$ilAccess->checkAccess("participate", "", $this->ref_id)))
01408                 {
01409                         array_push($messages, $this->lng->txt("cannot_participate_survey"));
01410                         $result = FALSE;
01411                 }
01412                 // 2. check previous access
01413                 if (!$result["error"])
01414                 {
01415                         global $ilUser;
01416                         $survey_started = $this->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"]);
01417                         if ($survey_started === 1)
01418                         {
01419                                 array_push($messages, $this->lng->txt("already_completed_survey"));
01420                                 $result = FALSE;
01421                         }
01422                 }
01423                 return array(
01424                         "result" => $result,
01425                         "messages" => $messages
01426                 );
01427         }
01428 
01438   function setStartDate($start_date = "") 
01439         {
01440     $this->start_date = $start_date;
01441   }
01442 
01452   function getStartMonth() 
01453         {
01454                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01455                 {
01456                         return $matches[2];
01457                 }
01458                 else
01459                 {
01460                         return "";
01461                 }
01462   }
01463 
01473   function getStartDay() 
01474         {
01475                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01476                 {
01477                         return $matches[3];
01478                 }
01479                 else
01480                 {
01481                         return "";
01482                 }
01483   }
01484 
01494   function getStartYear() 
01495         {
01496                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
01497                 {
01498                         return $matches[1];
01499                 }
01500                 else
01501                 {
01502                         return "";
01503                 }
01504   }
01505 
01515   function getEndDate() 
01516         {
01517     return $this->end_date;
01518   }
01519 
01529   function setEndDate($end_date = "") 
01530         {
01531     $this->end_date = $end_date;
01532   }
01533 
01543   function getEndMonth() 
01544         {
01545                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01546                 {
01547                         return $matches[2];
01548                 }
01549                 else
01550                 {
01551                         return "";
01552                 }
01553   }
01554 
01564   function getEndDay() 
01565         {
01566                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01567                 {
01568                         return $matches[3];
01569                 }
01570                 else
01571                 {
01572                         return "";
01573                 }
01574   }
01575 
01585   function getEndYear() 
01586         {
01587                 if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
01588                 {
01589                         return $matches[1];
01590                 }
01591                 else
01592                 {
01593                         return "";
01594                 }
01595   }
01596 
01606   function getEvaluationAccess() 
01607         {
01608     return $this->evaluation_access;
01609   }
01610 
01620   function setEvaluationAccess($evaluation_access = EVALUATION_ACCESS_OFF) 
01621         {
01622     $this->evaluation_access = $evaluation_access;
01623   }
01624 
01634   function getIntroduction() 
01635         {
01636     return $this->introduction;
01637   }
01638 
01647         function &getExistingQuestions() 
01648         {
01649                 $existing_questions = array();
01650                 $query = sprintf("SELECT survey_question.original_id FROM survey_question, survey_survey_question WHERE survey_survey_question.survey_fi = %s AND survey_survey_question.question_fi = survey_question.question_id",
01651                         $this->ilias->db->quote($this->getSurveyId())
01652                 );
01653                 $result = $this->ilias->db->query($query);
01654                 while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) 
01655                 {
01656                         array_push($existing_questions, $data->original_id);
01657                 }
01658                 return $existing_questions;
01659         }
01660 
01669         function &getQuestionpoolTitles() 
01670         {
01671                 global $rbacsystem;
01672                 
01673                 $qpl_titles = array();
01674                 // get all available questionpools and remove the trashed questionspools
01675                 $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'";
01676                 $result = $this->ilias->db->query($query);
01677                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01678                 {               
01679                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
01680                         {
01681                                 include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
01682                                 if (ilObjSurveyQuestionPool::_lookupOnline($row->obj_id))
01683                                 {
01684                                         $qpl_titles["$row->obj_id"] = $row->title;
01685                                 }
01686                         }
01687                 }
01688                 return $qpl_titles;
01689         }
01690         
01699         function moveUpQuestion($question_id)
01700         {
01701                 $move_questions = array($question_id);
01702                 $pages =& $this->getSurveyPages();
01703                 $pageindex = -1;
01704                 foreach ($pages as $idx => $page)
01705                 {
01706                         if ($page[0]["question_id"] == $question_id)
01707                         {
01708                                 $pageindex = $idx;
01709                         }
01710                 }
01711                 if ($pageindex > 0)
01712                 {
01713                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01714                 }
01715                 else
01716                 {
01717                         // move up a question in a questionblock
01718                         $questions = $this->getSurveyQuestions();
01719                         $questions = array_keys($questions);
01720                         $index = array_search($question_id, $questions);
01721                         if (($index !== FALSE) && ($index > 0))
01722                         {
01723                                 $this->moveQuestions($move_questions, $questions[$index-1], 0);
01724                         }
01725                 }
01726         }
01727         
01736         function moveDownQuestion($question_id)
01737         {
01738                 $move_questions = array($question_id);
01739                 $pages =& $this->getSurveyPages();
01740                 $pageindex = -1;
01741                 foreach ($pages as $idx => $page)
01742                 {
01743                         if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
01744                         {
01745                                 $pageindex = $idx;
01746                         }
01747                 }
01748                 if (($pageindex < count($pages)-1) && ($pageindex >= 0))
01749                 {
01750                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01751                 }
01752                 else
01753                 {
01754                         // move down a question in a questionblock
01755                         $questions = $this->getSurveyQuestions();
01756                         $questions = array_keys($questions);
01757                         $index = array_search($question_id, $questions);
01758                         if (($index !== FALSE) && ($index < count($questions)-1))
01759                         {
01760                                 $this->moveQuestions($move_questions, $questions[$index+1], 1);
01761                         }
01762                 }
01763         }
01764         
01773         function moveUpQuestionblock($questionblock_id)
01774         {
01775                 $pages =& $this->getSurveyPages();
01776                 $move_questions = array();
01777                 $pageindex = -1;
01778                 foreach ($pages as $idx => $page)
01779                 {
01780                         if ($page[0]["questionblock_id"] == $questionblock_id)
01781                         {
01782                                 foreach ($page as $pageidx => $question)
01783                                 {
01784                                         array_push($move_questions, $question["question_id"]);
01785                                 }
01786                                 $pageindex = $idx;
01787                         }
01788                 }
01789                 if ($pageindex > 0)
01790                 {
01791                         $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
01792                 }
01793         }
01794         
01803         function moveDownQuestionblock($questionblock_id)
01804         {
01805                 $pages =& $this->getSurveyPages();
01806                 $move_questions = array();
01807                 $pageindex = -1;
01808                 foreach ($pages as $idx => $page)
01809                 {
01810                         if ($page[0]["questionblock_id"] == $questionblock_id)
01811                         {
01812                                 foreach ($page as $pageidx => $question)
01813                                 {
01814                                         array_push($move_questions, $question["question_id"]);
01815                                 }
01816                                 $pageindex = $idx;
01817                         }
01818                 }
01819                 if ($pageindex < count($pages)-1)
01820                 {
01821                         $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
01822                 }
01823         }
01824         
01835         function moveQuestions($move_questions, $target_index, $insert_mode)
01836         {
01837                 $array_pos = array_search($target_index, $this->questions);
01838                 if ($insert_mode == 0)
01839                 {
01840                         $part1 = array_slice($this->questions, 0, $array_pos);
01841                         $part2 = array_slice($this->questions, $array_pos);
01842                 }
01843                 else if ($insert_mode == 1)
01844                 {
01845                         $part1 = array_slice($this->questions, 0, $array_pos + 1);
01846                         $part2 = array_slice($this->questions, $array_pos + 1);
01847                 }
01848                 foreach ($move_questions as $question_id)
01849                 {
01850                         if (!(array_search($question_id, $part1) === FALSE))
01851                         {
01852                                 unset($part1[array_search($question_id, $part1)]);
01853                         }
01854                         if (!(array_search($question_id, $part2) === FALSE))
01855                         {
01856                                 unset($part2[array_search($question_id, $part2)]);
01857                         }
01858                 }
01859                 $part1 = array_values($part1);
01860                 $part2 = array_values($part2);
01861                 $this->questions = array_values(array_merge($part1, $move_questions, $part2));
01862                 foreach ($move_questions as $question_id)
01863                 {
01864                         $constraints = $this->getConstraints($question_id);
01865                         foreach ($constraints as $idx => $constraint)
01866                         {
01867                                 foreach ($part2 as $next_question_id)
01868                                 {
01869                                         if ($constraint["question"] == $next_question_id)
01870                                         {
01871                                                 // constraint concerning a question that follows -> delete constraint
01872                                                 $this->deleteConstraint($constraint["id"], $question_id);
01873                                         }
01874                                 }
01875                         }
01876                 }
01877                 $this->saveQuestionsToDb();
01878         }
01879         
01888         function removeQuestion($question_id)
01889         {
01890                 include_once "./survey/classes/class.SurveyQuestion.php";
01891                 $question = new SurveyQuestion();
01892                 $question->delete($question_id);
01893                 $this->removeConstraintsConcerningQuestion($question_id);
01894         }
01895         
01904         function removeConstraintsConcerningQuestion($question_id)
01905         {
01906                 $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01907                         $this->ilias->db->quote($question_id . ""),
01908                         $this->ilias->db->quote($this->getSurveyId() . "")
01909                 );
01910                 $result = $this->ilias->db->query($query);
01911                 if ($result->numRows() > 0)
01912                 {
01913                         $remove_constraints = array();
01914                         while ($row = $result->fetchRow(DB_FETCHMODE_HASHREF))
01915                         {
01916                                 array_push($remove_constraints, $row["constraint_fi"]);
01917                         }
01918                         $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
01919                                 $this->ilias->db->quote($question_id . ""),
01920                                 $this->ilias->db->quote($this->getSurveyId() . "")
01921                         );
01922                         $result = $this->ilias->db->query($query);
01923                         foreach ($remove_constraints as $key => $constraint_id)
01924                         {
01925                                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
01926                                         $this->ilias->db->quote($constraint_id . "")
01927                                 );
01928                                 $result = $this->ilias->db->query($query);
01929                         }
01930                 }
01931         }
01932                 
01942         function removeQuestions($remove_questions, $remove_questionblocks)
01943         {
01944                 $questions =& $this->getSurveyQuestions();
01945                 foreach ($questions as $question_id => $data)
01946                 {
01947                         if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
01948                         {
01949                                 unset($this->questions[array_search($question_id, $this->questions)]);
01950                                 $this->removeQuestion($question_id);
01951                         }
01952                 }
01953                 foreach ($remove_questionblocks as $questionblock_id)
01954                 {
01955                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01956                                 $this->ilias->db->quote($questionblock_id)
01957                         );
01958                         $result = $this->ilias->db->query($query);
01959                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01960                                 $this->ilias->db->quote($questionblock_id),
01961                                 $this->ilias->db->quote($this->getSurveyId())
01962                         );
01963                         $result = $this->ilias->db->query($query);
01964                 }
01965                 $this->questions = array_values($this->questions);
01966                 $this->saveQuestionsToDb();
01967         }
01968                 
01977         function unfoldQuestionblocks($questionblocks)
01978         {
01979                 foreach ($questionblocks as $index)
01980                 {
01981                         $query = sprintf("DELETE FROM survey_questionblock WHERE questionblock_id = %s",
01982                                 $this->ilias->db->quote($index)
01983                         );
01984                         $result = $this->ilias->db->query($query);
01985                         $query = sprintf("DELETE FROM survey_questionblock_question WHERE questionblock_fi = %s AND survey_fi = %s",
01986                                 $this->ilias->db->quote($index),
01987                                 $this->ilias->db->quote($this->getSurveyId())
01988                         );
01989                         $result = $this->ilias->db->query($query);
01990                 }
01991         }
01992         
02001         function &getQuestionblockTitles()
02002         {
02003                 $titles = array();
02004                 $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",
02005                         $this->ilias->db->quote($this->getId())
02006                 );
02007                 $result = $this->ilias->db->query($query);
02008                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02009                 {
02010                         $titles[$row->questionblock_id] = $row->title;
02011                 }
02012                 return $titles;
02013         }
02014         
02023         function &getQuestionblockQuestions($questionblock_id)
02024         {
02025                 $titles = array();
02026                 $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",
02027                         $this->ilias->db->quote($questionblock_id)
02028                 );
02029                 $result = $this->ilias->db->query($query);
02030                 $survey_id = "";
02031                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02032                 {
02033                         $titles[$row["question_fi"]] = $row["title"];
02034                         $survey_id = $row["survey_fi"];
02035                 }
02036                 $query = sprintf("SELECT question_fi, sequence FROM survey_survey_question WHERE survey_fi = %s ORDER BY sequence",
02037                         $this->ilias->db->quote($survey_id . "")
02038                 );
02039                 $result = $this->ilias->db->query($query);
02040                 $resultarray = array();
02041                 $counter = 1;
02042                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02043                 {
02044                         if (array_key_exists($row["question_fi"], $titles))
02045                         {
02046                                 $resultarray[$counter++] = $titles[$row["question_fi"]];
02047                         }
02048                 }
02049                 return $resultarray;
02050         }
02051         
02060         function &getQuestionblockQuestionIds($questionblock_id)
02061         {
02062                 $ids = array();
02063                 $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",
02064                         $this->ilias->db->quote($this->getId()),
02065                         $this->ilias->db->quote($questionblock_id)
02066                 );
02067                 $result = $this->ilias->db->query($query);
02068                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02069                 {
02070                         array_push($ids, $row->question_id);
02071                 }
02072                 return $ids;
02073         }
02074         
02084         function getQuestionblock($questionblock_id)
02085         {
02086                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02087                         $this->ilias->db->quote($questionblock_id)
02088                 );
02089                 $result = $this->ilias->db->query($query);
02090                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02091                 return $row;
02092         }
02093         
02103         function _getQuestionblock($questionblock_id)
02104         {
02105                 global $ilDB;
02106                 $query = sprintf("SELECT * FROM survey_questionblock WHERE questionblock_id = %s",
02107                         $ilDB->quote($questionblock_id)
02108                 );
02109                 $result = $ilDB->query($query);
02110                 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
02111                 return $row;
02112         }
02113 
02124         function _addQuestionblock($title = "", $owner = 0)
02125         {
02126                 global $ilDB;
02127                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02128                         $ilDB->quote($title . ""),
02129                         $ilDB->quote($owner . "")
02130                 );
02131                 $result = $ilDB->query($query);
02132                 return $ilDB->getLastInsertId();
02133         }
02134         
02144         function createQuestionblock($title, $questions)
02145         {
02146                 // if the selected questions are not in a continous selection, move all questions of the
02147                 // questionblock at the position of the first selected question
02148                 $this->moveQuestions($questions, $questions[0], 0);
02149                 
02150                 // now save the question block
02151                 global $ilUser;
02152                 $query = sprintf("INSERT INTO survey_questionblock (questionblock_id, title, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02153                         $this->ilias->db->quote($title),
02154                         $this->ilias->db->quote($ilUser->id)
02155                 );
02156                 $result = $this->ilias->db->query($query);
02157                 if ($result == DB_OK) {
02158                         $questionblock_id = $this->ilias->db->getLastInsertId();
02159                         foreach ($questions as $index)
02160                         {
02161                                 $query = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
02162                                         $this->ilias->db->quote($this->getSurveyId()),
02163                                         $this->ilias->db->quote($questionblock_id),
02164                                         $this->ilias->db->quote($index)
02165                                 );
02166                                 $result = $this->ilias->db->query($query);
02167                                 $this->deleteConstraints($index);
02168                         }
02169                 }
02170         }
02171         
02181         function modifyQuestionblock($questionblock_id, $title)
02182         {
02183                 $query = sprintf("UPDATE survey_questionblock SET title = %s WHERE questionblock_id = %s",
02184                         $this->ilias->db->quote($title),
02185                         $this->ilias->db->quote($questionblock_id)
02186                 );
02187                 $result = $this->ilias->db->query($query);
02188         }
02189         
02198         function deleteConstraints($question_id)
02199         {
02200                 $query = sprintf("SELECT * FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02201                         $this->ilias->db->quote($question_id),
02202                         $this->ilias->db->quote($this->getSurveyId())
02203                 );
02204                 $result = $this->ilias->db->query($query);
02205                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02206                 {
02207                         $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02208                                 $this->ilias->db->quote($row->constraint_fi)
02209                         );
02210                         $delresult = $this->ilias->db->query($query);
02211                 }
02212                 $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s AND survey_fi = %s",
02213                         $this->ilias->db->quote($question_id),
02214                         $this->ilias->db->quote($this->getSurveyId())
02215                 );
02216                 $delresult = $this->ilias->db->query($query);
02217         }
02218 
02228         function deleteConstraint($constraint_id, $question_id)
02229         {
02230                 $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
02231                         $this->ilias->db->quote($constraint_id)
02232                 );
02233                 $delresult = $this->ilias->db->query($query);
02234                 $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s AND question_fi = %s AND survey_fi = %s",
02235                         $this->ilias->db->quote($constraint_id),
02236                         $this->ilias->db->quote($question_id),
02237                         $this->ilias->db->quote($this->getSurveyId())
02238                 );
02239                 $delresult = $this->ilias->db->query($query);
02240         }
02241 
02249         function &getSurveyQuestions($with_answers = false)
02250         {
02251                 $obligatory_states =& $this->getObligatoryStates();
02252                 // get questionblocks
02253                 $all_questions = array();
02254                 $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",
02255                         $this->ilias->db->quote($this->getSurveyId())
02256                 );
02257                 $result = $this->ilias->db->query($query);
02258                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02259                 {
02260                         $all_questions[$row["question_id"]] = $row;
02261                         if (array_key_exists($row["question_id"], $obligatory_states))
02262                         {
02263                                 $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
02264                         }
02265                 }
02266                 // get all questionblocks
02267                 $questionblocks = array();
02268                 $in = join(array_keys($all_questions), ",");
02269                 if ($in)
02270                 {
02271                         $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)",
02272                                 $this->ilias->db->quote($this->getSurveyId())
02273                         );
02274                         $result = $this->ilias->db->query($query);
02275                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02276                         {
02277                                 $questionblocks[$row->question_fi] = $row;
02278                         }                       
02279                 }
02280                 
02281                 foreach ($all_questions as $question_id => $row)
02282                 {
02283                         $constraints = $this->getConstraints($question_id);
02284                         if (isset($questionblocks[$question_id]))
02285                         {
02286                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02287                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02288                                 $all_questions[$question_id]["constraints"] = $constraints;
02289                         }
02290                         else
02291                         {
02292                                 $all_questions[$question_id]["questionblock_title"] = "";
02293                                 $all_questions[$question_id]["questionblock_id"] = "";
02294                                 $all_questions[$question_id]["constraints"] = $constraints;
02295                         }
02296                         if ($with_answers)
02297                         {
02298                                 $answers = array();
02299                                 $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",
02300                                         $this->ilias->db->quote($question_id . "")
02301                                 );
02302                                 $result = $this->ilias->db->query($query);
02303                                 if (strcmp(strtolower(get_class($result)), db_result) == 0) {
02304                                         while ($data = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
02305                                                 array_push($answers, $data->title);
02306                                         }
02307                                 }
02308                                 $all_questions[$question_id]["answers"] = $answers;                             
02309                         }
02310                 }
02311                 return $all_questions;
02312         }
02313         
02322         function &getQuestiontypes()
02323         {
02324                 $query = "SELECT type_tag FROM survey_questiontype";
02325                 $result = $this->ilias->db->query($query);
02326                 $result_array = array();
02327                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02328                 {
02329                         array_push($result_array, $row->type_tag);
02330                 }
02331                 return $result_array;
02332         }
02333 
02342         function setObligatoryStates($obligatory_questions)
02343         {
02344                 $query = sprintf("SELECT * FROM survey_survey_question WHERE survey_fi = %s",
02345                         $this->ilias->db->quote($this->getSurveyId() . "")
02346                 );
02347                 $result = $this->ilias->db->query($query);
02348                 if ($result->numRows())
02349                 {
02350                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02351                         {
02352                                 if (!array_key_exists($row["question_fi"], $obligatory_questions))
02353                                 {
02354                                         $obligatory_questions[$row["question_fi"]] = 0;
02355                                 }
02356                         }
02357                 }
02358 
02359           // set the obligatory states in the database
02360                 $query = sprintf("DELETE FROM survey_question_obligatory WHERE survey_fi = %s",
02361                         $this->ilias->db->quote($this->getSurveyId() . "")
02362                 );
02363                 $result = $this->ilias->db->query($query);
02364 
02365           // set the obligatory states in the database
02366                 foreach ($obligatory_questions as $question_fi => $obligatory)
02367                 {
02368                         $query = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
02369                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02370                                 $this->ilias->db->quote($question_fi . ""),
02371                                 $this->ilias->db->quote($obligatory . "")
02372                         );
02373                         $result = $this->ilias->db->query($query);
02374                 }
02375         }
02376         
02385         function &getObligatoryStates()
02386         {
02387                 $obligatory_states = array();
02388                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
02389                         $this->ilias->db->quote($this->getSurveyId() . "")
02390                 );
02391                 $result = $this->ilias->db->query($query);
02392                 if ($result->numRows())
02393                 {
02394                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02395                         {
02396                                 $obligatory_states[$row["question_fi"]] = $row["obligatory"];
02397                         }
02398                 }
02399                 return $obligatory_states;
02400         }
02401         
02409         function &getSurveyPages()
02410         {
02411                 $obligatory_states =& $this->getObligatoryStates();
02412                 // get questionblocks
02413                 $all_questions = array();
02414                 $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",
02415                         $this->ilias->db->quote($this->getSurveyId())
02416                 );
02417                 $result = $this->ilias->db->query($query);
02418                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02419                 {
02420                         $all_questions[$row["question_id"]] = $row;
02421                 }
02422                 // get all questionblocks
02423                 $questionblocks = array();
02424                 $in = join(array_keys($all_questions), ",");
02425                 if ($in)
02426                 {
02427                         $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)",
02428                                 $this->ilias->db->quote($this->getSurveyId())
02429                         );
02430                         $result = $this->ilias->db->query($query);
02431                         while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02432                         {
02433                                 $questionblocks["$row->question_fi"] = $row;
02434                         }                       
02435                 }
02436                 
02437                 $all_pages = array();
02438                 $pageindex = -1;
02439                 $currentblock = "";
02440                 foreach ($all_questions as $question_id => $row)
02441                 {
02442                         if (array_key_exists($question_id, $obligatory_states))
02443                         {
02444                                 $all_questions["$question_id"]["obligatory"] = $obligatory_states["$question_id"];
02445                         }
02446                         $constraints = array();
02447                         if (isset($questionblocks[$question_id]))
02448                         {
02449                                 if (!$currentblock or ($currentblock != $questionblocks[$question_id]->questionblock_id))
02450                                 {
02451                                         $pageindex++;
02452                                 }
02453                                 $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]->title;
02454                                 $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]->questionblock_id;
02455                                 $currentblock = $questionblocks[$question_id]->questionblock_id;
02456                                 $constraints = $this->getConstraints($question_id);
02457                                 $all_questions[$question_id]["constraints"] = $constraints;
02458                         }
02459                         else
02460                         {
02461                                 $pageindex++;
02462                                 $all_questions[$question_id]["questionblock_title"] = "";
02463                                 $all_questions[$question_id]["questionblock_id"] = "";
02464                                 $currentblock = "";
02465                                 $constraints = $this->getConstraints($question_id);
02466                                 $all_questions[$question_id]["constraints"] = $constraints;
02467                         }
02468                         if (!isset($all_pages[$pageindex]))
02469                         {
02470                                 $all_pages[$pageindex] = array();
02471                         }
02472                         array_push($all_pages[$pageindex], $all_questions[$question_id]);
02473                 }
02474                 // calculate position percentage for every page
02475                 $max = count($all_pages);
02476                 $counter = 1;
02477                 foreach ($all_pages as $index => $block)
02478                 {
02479                         foreach ($block as $blockindex => $question)
02480                         {
02481                                 $all_pages[$index][$blockindex][position] = $counter / $max;
02482                         }
02483                         $counter++;
02484                 }
02485                 return $all_pages;
02486         }
02487         
02498         function getNextPage($active_page_question_id, $direction)
02499         {
02500                 $foundpage = -1;
02501                 $pages =& $this->getSurveyPages();
02502                 if (strcmp($active_page_question_id, "") == 0)
02503                 {
02504                         return $pages[0];
02505                 }
02506                 
02507                 foreach ($pages as $key => $question_array)
02508                 {
02509                         foreach ($question_array as $question)
02510                         {
02511                                 if ($active_page_question_id == $question["question_id"])
02512                                 {
02513                                         $foundpage = $key;
02514                                 }
02515                         }
02516                 }
02517                 if ($foundpage == -1)
02518                 {
02519                         // error: page not found
02520                 }
02521                 else
02522                 {
02523                         $foundpage += $direction;
02524                         if ($foundpage < 0)
02525                         {
02526                                 return 0;
02527                         }
02528                         if ($foundpage >= count($pages))
02529                         {
02530                                 return 1;
02531                         }
02532                         return $pages[$foundpage];
02533                 }
02534         }
02535                 
02544         function &getAvailableQuestionpools($use_obj_id = false, $could_be_offline = false)
02545         {
02546                 global $rbacsystem;
02547                 
02548                 $result_array = array();
02549                 $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'";
02550                 $result = $this->ilias->db->query($query);
02551                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02552                 {               
02553                         if ($rbacsystem->checkAccess("write", $row->ref_id) && ($this->_hasUntrashedReference($row->obj_id)))
02554                         {
02555                                 include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
02556                                 if (ilObjSurveyQuestionPool::_lookupOnline($row->obj_id) || $could_be_offline)
02557                                 {
02558                                         if ($use_obj_id)
02559                                         {
02560                                                 $result_array[$row->obj_id] = $row->title;
02561                                         }
02562                                         else
02563                                         {
02564                                                 $result_array[$row->ref_id] = $row->title;
02565                                         }
02566                                 }
02567                         }
02568                 }
02569                 return $result_array;
02570         }
02571         
02579         function getConstraints($question_id)
02580         {
02581                 $result_array = array();
02582                 $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",
02583                         $this->ilias->db->quote($question_id),
02584                         $this->ilias->db->quote($this->getSurveyId())
02585                 );
02586                 $result = $this->ilias->db->query($query);
02587                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02588                 {               
02589                         array_push($result_array, array("id" => $row->constraint_id, "question" => $row->question_fi, "short" => $row->shortname, "long" => $row->longname, "value" => $row->value));
02590                 }
02591                 return $result_array;
02592         }
02593 
02601         function _getConstraints($survey_id)
02602         {
02603                 global $ilDB;
02604                 $result_array = array();
02605                 $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",
02606                         $ilDB->quote($survey_id . "")
02607                 );
02608                 $result = $ilDB->query($query);
02609                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02610                 {               
02611                         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));
02612                 }
02613                 return $result_array;
02614         }
02615 
02616 
02624         function &getVariables($question_id)
02625         {
02626                 $result_array = array();
02627                 $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",
02628                         $this->ilias->db->quote($question_id)
02629                 );
02630                 $result = $this->ilias->db->query($query);
02631                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02632                 {
02633                         $result_array[$row->sequence] = $row;
02634                 }
02635                 return $result_array;
02636         }
02637         
02649         function addConstraint($to_question_id, $if_question_id, $relation, $value)
02650         {
02651                 $query = sprintf("INSERT INTO survey_constraint (constraint_id, question_fi, relation_fi, value) VALUES (NULL, %s, %s, %s)",
02652                         $this->ilias->db->quote($if_question_id),
02653                         $this->ilias->db->quote($relation),
02654                         $this->ilias->db->quote($value)
02655                 );
02656                 $result = $this->ilias->db->query($query);
02657                 if ($result == DB_OK) {
02658                         $constraint_id = $this->ilias->db->getLastInsertId();
02659                         $query = sprintf("INSERT INTO survey_question_constraint (question_constraint_id, survey_fi, question_fi, constraint_fi) VALUES (NULL, %s, %s, %s)",
02660                                 $this->ilias->db->quote($this->getSurveyId()),
02661                                 $this->ilias->db->quote($to_question_id),
02662                                 $this->ilias->db->quote($constraint_id)
02663                         );
02664                         $result = $this->ilias->db->query($query);
02665                 }
02666         }
02667         
02675         function getAllRelations($short_as_key = false)
02676         {
02677                 $result_array = array();
02678                 $query = "SELECT * FROM survey_relation";
02679                 $result = $this->ilias->db->query($query);
02680                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02681                 {
02682                         if ($short_as_key)
02683                         {
02684                                 $result_array[$row->shortname] = array("short" => $row->shortname, "long" => $row->longname, "id" => $row->relation_id);
02685                         }
02686                         else
02687                         {
02688                                 $result_array[$row->relation_id] = array("short" => $row->shortname, "long" => $row->longname);
02689                         }
02690                 }
02691                 return $result_array;
02692         }
02693 
02702         function disinviteUser($user_id)
02703         {
02704                 $query = sprintf("DELETE FROM survey_invited_user WHERE survey_fi = %s AND user_fi = %s",
02705                         $this->ilias->db->quote($this->getSurveyId()),
02706                         $this->ilias->db->quote($user_id)
02707                 );
02708                 $result = $this->ilias->db->query($query);
02709                 if ($this->getInvitation() == INVITATION_ON)
02710                 {
02711                         include_once "./classes/class.ilObjUser.php";
02712                         $userObj = new ilObjUser($user_id);
02713                         $userObj->dropDesktopItem($this->getRefId(), "svy");
02714                 }
02715         }
02716 
02725         function inviteUser($user_id)
02726         {
02727                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE user_fi = %s AND survey_fi = %s",
02728                         $this->ilias->db->quote($user_id),
02729                         $this->ilias->db->quote($this->getSurveyId())
02730                 );
02731                 $result = $this->ilias->db->query($query);
02732                 if ($result->numRows() < 1)
02733                 {
02734                         $query = sprintf("INSERT INTO survey_invited_user (invited_user_id, survey_fi, user_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
02735                                 $this->ilias->db->quote($this->getSurveyId()),
02736                                 $this->ilias->db->quote($user_id)
02737                         );
02738                         $result = $this->ilias->db->query($query);
02739                 }
02740                 if ($this->getInvitation() == INVITATION_ON)
02741                 {
02742                         include_once "./classes/class.ilObjUser.php";
02743                         $userObj = new ilObjUser($user_id);
02744                         $userObj->addDesktopItem($this->getRefId(), "svy");
02745                 }
02746         }
02747 
02756         function inviteGroup($group_id)
02757         {
02758                 include_once "./classes/class.ilObjGroup.php";
02759                 $group = new ilObjGroup($group_id);
02760                 $members = $group->getGroupMemberIds();
02761                 foreach ($members as $user_id)
02762                 {
02763                         $this->inviteUser($user_id);
02764                         if ($this->getInvitation() == INVITATION_ON)
02765                         {
02766                                 $userObj = new ilObjUser($user_id);
02767                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02768                         }
02769                 }
02770         }
02771         
02780         function inviteRole($role_id)
02781         {
02782                 global $rbacreview;
02783                 $members = $rbacreview->assignedUsers($role_id);
02784                 foreach ($members as $user_id)
02785                 {
02786                         $this->inviteUser($user_id);
02787                         if ($this->getInvitation() == INVITATION_ON)
02788                         {
02789                                 $userObj = new ilObjUser($user_id);
02790                                 $userObj->addDesktopItem($this->getRefId(), "svy");
02791                         }
02792                 }
02793         }
02794         
02803         function &getInvitedUsers()
02804         {
02805                 $result_array = array();
02806                 $query = sprintf("SELECT user_fi FROM survey_invited_user WHERE survey_fi = %s",
02807                         $this->ilias->db->quote($this->getSurveyId())
02808                 );
02809                 $result = $this->ilias->db->query($query);
02810                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02811                 {
02812                         array_push($result_array, $row->user_fi);
02813                 }
02814                 return $result_array;
02815         }
02816 
02825         function &getInvitedGroups()
02826         {
02827                 $result_array = array();
02828                 $query = sprintf("SELECT group_fi FROM survey_invited_group WHERE survey_fi = %s",
02829                         $this->ilias->db->quote($this->getSurveyId())
02830                 );
02831                 $result = $this->ilias->db->query($query);
02832                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
02833                 {
02834                         array_push($result_array, $row->group_fi);
02835                 }
02836                 return $result_array;
02837         }
02838 
02848         function deleteWorkingData($question_id, $user_id)
02849         {
02850                 $query = "";
02851                 if ($this->getAnonymize())
02852                 {
02853                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02854                                 $this->ilias->db->quote($this->getSurveyId()),
02855                                 $this->ilias->db->quote($question_id),
02856                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02857                         );
02858                 }
02859                 else
02860                 {
02861                         $query = sprintf("DELETE FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02862                                 $this->ilias->db->quote($this->getSurveyId()),
02863                                 $this->ilias->db->quote($question_id),
02864                                 $this->ilias->db->quote($user_id)
02865                         );
02866                 }
02867                 $result = $this->ilias->db->query($query);
02868         }
02869         
02881         function saveWorkingData($question_id, $user_id, $anonymize_id, $value = "", $text = "")
02882         {
02883                 if ($this->isSurveyStarted($user_id, $anonymize_id) === false)
02884                 {
02885                         $this->startSurvey($user_id, $anonymize_id);
02886                 }
02887                 if (strcmp($value, "") == 0)
02888                 {
02889                         $value = "NULL";
02890                 }
02891                 else
02892                 {
02893                         $value = $this->ilias->db->quote($value);
02894                 }
02895                 if (strcmp($text, "") == 0)
02896                 {
02897                         $text = "NULL";
02898                 }
02899                 else
02900                 {
02901                         $text = $this->ilias->db->quote($text);
02902                 }
02903                 if ($this->getAnonymize())
02904                 {
02905                         $user_id = 0;
02906                 }
02907                 $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)",
02908                         $this->ilias->db->quote($this->getSurveyId() . ""),
02909                         $this->ilias->db->quote($question_id . ""),
02910                         $this->ilias->db->quote($user_id . ""),
02911                         $this->ilias->db->quote($anonymize_id),
02912                         $value,
02913                         $text
02914                 );
02915                 $result = $this->ilias->db->query($query);
02916         }
02917         
02928         function loadWorkingData($question_id, $user_id)
02929         {
02930                 $result_array = array();
02931                 $query = "";
02932                 if ($this->getAnonymize())
02933                 {
02934                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND anonymous_id = %s",
02935                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02936                                 $this->ilias->db->quote($question_id. ""),
02937                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
02938                         );
02939                 }
02940                 else
02941                 {
02942                         $query = sprintf("SELECT * FROM survey_answer WHERE survey_fi = %s AND question_fi = %s AND user_fi = %s",
02943                                 $this->ilias->db->quote($this->getSurveyId() . ""),
02944                                 $this->ilias->db->quote($question_id . ""),
02945                                 $this->ilias->db->quote($user_id . "")
02946                         );
02947                 }
02948                 $result = $this->ilias->db->query($query);
02949                 if ($result->numRows() >= 1)
02950                 {
02951                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
02952                         {
02953                                 array_push($result_array, $row);
02954                         }
02955                         return $result_array;
02956                 }
02957                 else
02958                 {
02959                         return $result_array;
02960                 }
02961         }
02962 
02971         function startSurvey($user_id, $anonymous_id)
02972         {
02973                 global $ilUser;
02974                 
02975                 if (strcmp($user_id, "") == 0)
02976                 {
02977                         $user_id = 0;
02978                 }
02979                 if ($this->getAnonymize())
02980                 {
02981                         $user_id = 0;
02982                 }
02983                 $query = sprintf("INSERT INTO survey_finished (finished_id, survey_fi, user_fi, anonymous_id, state, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
02984                         $this->ilias->db->quote($this->getSurveyId() . ""),
02985                         $this->ilias->db->quote($user_id . ""),
02986                         $this->ilias->db->quote($anonymous_id . ""),
02987                         $this->ilias->db->quote(0 . "")
02988                 );
02989                 $result = $this->ilias->db->query($query);
02990                 if ($this->getAnonymize())
02991                 {
02992                         if (strcmp($ilUser->login, "anonymous") != 0)
02993                         {
02994                                 include_once "./include/inc.mail.php";
02995                                 include_once "./classes/class.ilFormatMail.php";
02996                                 include_once "./classes/class.ilMailbox.php";
02997                                 $subject = sprintf($this->lng->txt("subject_mail_survey_id"), $this->getTitle());
02998                                 $message = sprintf($this->lng->txt("message_mail_survey_id"), $this->getTitle(), $_SESSION["anonymous_id"]);
02999                                 $umail = new ilFormatMail($ilUser->id);
03000                                 $f_message = $umail->formatLinebreakMessage($message);
03001                                 $umail->setSaveInSentbox(true);
03002                                 if($error_message = $umail->sendMail($ilUser->getLogin(),"",
03003                                                                                                          "",$subject,$f_message,
03004                                                                                                          array(),array("normal")))
03005                                 {
03006                                         sendInfo($error_message);
03007                                 }
03008                         }
03009                 }
03010         }
03011                         
03020         function finishSurvey($user_id, $anonymize_id)
03021         {
03022                 if ($this->getAnonymize())
03023                 {
03024                         $user_id = 0;
03025                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND anonymous_id = %s",
03026                                 $this->ilias->db->quote("1"),
03027                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03028                                 $this->ilias->db->quote($anonymize_id . "")
03029                         );
03030                 }
03031                 else
03032                 {
03033                         $query = sprintf("UPDATE survey_finished SET state = %s WHERE survey_fi = %s AND user_fi = %s",
03034                                 $this->ilias->db->quote("1"),
03035                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03036                                 $this->ilias->db->quote($user_id . "")
03037                         );
03038                 }
03039                 $result = $this->ilias->db->query($query);
03040         }
03041         
03051         function isSurveyStarted($user_id, $anonymize_id)
03052         {
03053                 if ($this->getAnonymize())
03054                 {
03055                         if (strlen($anonymize_id) == 0) return FALSE;
03056                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND anonymous_id = %s",
03057                                 $this->ilias->db->quote($this->getSurveyId()),
03058                                 $this->ilias->db->quote($anonymize_id)
03059                         );
03060                 }
03061                 else
03062                 {
03063                         $query = sprintf("SELECT state FROM survey_finished WHERE survey_fi = %s AND user_fi = %s",
03064                                 $this->ilias->db->quote($this->getSurveyId()),
03065                                 $this->ilias->db->quote($user_id)
03066                         );
03067                 }
03068                 $result = $this->ilias->db->query($query);
03069                 if ($result->numRows() == 0)
03070                 {
03071                         return false;
03072                 }                       
03073                 else
03074                 {
03075                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03076                         return (int)$row["state"];
03077                 }
03078         }
03079         
03089         function getLastActivePage($user_id)
03090         {
03091                 $query = "";
03092                 if ($this->getAnonymize())
03093                 {
03094                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s ORDER BY TIMESTAMP14 DESC",
03095                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03096                                 $this->ilias->db->quote($_SESSION["anonymous_id"])
03097                         );
03098                 }
03099                 else
03100                 {
03101                         $query = sprintf("SELECT question_fi, TIMESTAMP + 0 AS TIMESTAMP14 FROM survey_answer WHERE survey_fi = %s AND user_fi = %s ORDER BY TIMESTAMP14 DESC",
03102                                 $this->ilias->db->quote($this->getSurveyId() . ""),
03103                                 $this->ilias->db->quote($user_id . "")
03104                         );
03105                 }
03106                 $result = $this->ilias->db->query($query);
03107                 if ($result->numRows() == 0)
03108                 {
03109                         return "";
03110                 }
03111                 else
03112                 {
03113                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
03114                         return $row["question_fi"];
03115                 }
03116         }
03117 
03128         function checkConstraint($constraint_data, $working_data)
03129         {
03130                 if (count($working_data) == 0)
03131                 {
03132                         return 0;
03133                 }
03134                 
03135                 if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
03136                 {
03137                         return 0;
03138                 }
03139                 
03140                 foreach ($working_data as $data)
03141                 {
03142                         switch ($constraint_data["short"])
03143                         {
03144                                 case "<":
03145                                         if ($data["value"] < $constraint_data["value"])
03146                                         {
03147                                                 return 1;
03148                                         }
03149                                         break;
03150                                 case "<=":
03151                                         if ($data["value"] <= $constraint_data["value"])
03152                                         {
03153                                                 return 1;
03154                                         }
03155                                         break;
03156                                 case "=":
03157                                         if ($data["value"] == $constraint_data["value"])
03158                                         {
03159                                                 return 1;
03160                                         }
03161                                         break;
03162                                 case "<>":
03163                                         if ($data["value"] != $constraint_data["value"])
03164                                         {
03165                                                 return 1;
03166                                         }
03167                                         break;
03168                                 case ">=":
03169                                         if ($data["value"] >= $constraint_data["value"])
03170                                         {
03171                                                 return 1;
03172                                         }
03173                                         break;
03174                                 case ">":
03175                                         if ($data["value"] > $constraint_data["value"])
03176                                         {
03177                                                 return 1;
03178                                         }
03179                                         break;
03180                         }
03181                 }
03182                 return 0;
03183         }
03184         
03185         function _hasDatasets($survey_id)
03186         {
03187                 global $ilDB;
03188                 
03189                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03190                         $ilDB->quote($survey_id . "")
03191                 );
03192                 $result = $ilDB->query($query);
03193                 if ($result->numRows())
03194                 {
03195                         return true;
03196                 }
03197                 else
03198                 {
03199                         return false;
03200                 }
03201         }
03202 
03203         function &getEvaluationForAllUsers()
03204         {
03205                 $users = array();
03206                 $query = sprintf("SELECT * FROM survey_finished WHERE survey_fi = %s",
03207                         $this->ilias->db->quote($this->getSurveyId() . "")
03208                 );
03209                 $result = $this->ilias->db->query($query);
03210                 if ($result->numRows())
03211                 {
03212                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03213                         {
03214                                 array_push($users, $row);
03215                         }
03216                 }
03217                 $evaluation = array();
03218                 $questions =& $this->getSurveyQuestions();
03219                 foreach ($users as $row)
03220                 {
03221                         if (($row["user_fi"] > 0) && ($this->getAnonymize() == 0))
03222                         {
03223                                 $evaluation[$row["user_fi"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03224                         }
03225                         else
03226                         {
03227                                 if (strlen($row["anonymous_id"]) > 0)
03228                                 {
03229                                         $evaluation[$row["anonymous_id"]] = $this->getEvaluationByUser($questions, $row["user_fi"], $row["anonymous_id"]);
03230                                 }
03231                         }
03232                 }
03233                 return $evaluation;
03234         }
03235         
03247         function &getEvaluationByUser($questions, $user_id, $anonymous_id = "")
03248         {
03249                 $wherecond = "";
03250                 $wherevalue = "";
03251                 if (strcmp($anonymous_id, "") != 0)
03252                 {
03253                         $wherecond = "anonymous_id = %s";
03254                         $wherevalue = $anonymous_id;
03255                 }
03256                 else
03257                 {
03258                         $wherecond = "user_fi = %s";
03259                         $wherevalue = $user_id;
03260                 }
03261                 // collect all answers
03262                 $answers = array();
03263                 $query = sprintf("SELECT * FROM survey_answer WHERE $wherecond AND survey_fi = %s",
03264                         $this->ilias->db->quote($wherevalue),
03265                         $this->ilias->db->quote($this->getSurveyId())
03266                 );
03267                 $result = $this->ilias->db->query($query);
03268                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03269                 {
03270                         if (!is_array($answers[$row["question_fi"]]))
03271                         {
03272                                 $answers[$row["question_fi"]] = array();
03273                         }
03274                         array_push($answers[$row["question_fi"]], $row);
03275                 }
03276                 $username = "";
03277                 $gender = "";
03278                 if (($user_id > 0) && ($this->getAnonymize() == 0))
03279                 {
03280                         include_once "./classes/class.ilObjUser.php";
03281                         if (strlen(ilObjUser::_lookupLogin($user_id)) == 0)
03282                         {
03283                                 $username = $this->lng->txt("deleted_user");
03284                                 $gender = "";
03285                         }
03286                         else
03287                         {
03288                                 $user = new ilObjUser($user_id);
03289                                 $username = $user->getFullname();
03290                                 $gender = $user->getGender();
03291                                 if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
03292                         }
03293                 }
03294                 $resultset = array(
03295                         "name" => $username,
03296                         "gender" => $gender,
03297                         "answers" => array()
03298                 );
03299                 foreach ($questions as $key => $question)
03300                 {
03301                         if (array_key_exists($key, $answers))
03302                         {
03303                                 $resultset["answers"][$key] = $answers[$key];
03304                         }
03305                         else
03306                         {
03307                                 $resultset["answers"][$key] = array();
03308                         }
03309                         sort($resultset["answers"][$key]);
03310                 }
03311                 return $resultset;
03312         }
03313         
03324         function getEvaluation($question_id)
03325         {
03326                 $questions =& $this->getSurveyQuestions();
03327                 $result_array = array();
03328                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE survey_fi = %s",
03329                         $this->ilias->db->quote($this->getSurveyId())
03330                 );
03331                 $result = $this->ilias->db->query($query);
03332                 $nr_of_users = $result->numRows();
03333                                 
03334                 $query = sprintf("SELECT * FROM survey_answer WHERE question_fi = %s AND survey_fi = %s",
03335                         $this->ilias->db->quote($question_id),
03336                         $this->ilias->db->quote($this->getSurveyId())
03337                 );
03338                 $result = $this->ilias->db->query($query);
03339                 $cumulated = array();
03340                 $textvalues = array();
03341                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03342                 {
03343                         $cumulated["$row->value"]++;
03344                         array_push($textvalues, $row->textanswer);
03345                 }
03346                 asort($cumulated, SORT_NUMERIC);
03347                 end($cumulated);
03348                 $numrows = $result->numRows();
03349                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03350                 {
03351                         if ($this->getAnonymize())
03352                         {
03353                                 $query = sprintf("SELECT answer_id, concat( question_fi,  \"_\", anonymous_id )  AS groupval FROM `survey_answer` WHERE question_fi = %s AND survey_fi = %s GROUP BY groupval",
03354                                         $this->ilias->db->quote($question_id),
03355                                         $this->ilias->db->quote($this->getSurveyId())
03356                                 );
03357                         }
03358                         else
03359                         {
03360                                 $query = sprintf("SELECT answer_id, concat( question_fi,  \"_\", user_fi )  AS groupval FROM `survey_answer` WHERE question_fi = %s AND survey_fi = %s GROUP BY groupval",
03361                                         $this->ilias->db->quote($question_id),
03362                                         $this->ilias->db->quote($this->getSurveyId())
03363                                 );
03364                         }
03365                         $mcmr_result = $this->ilias->db->query($query);
03366                         $result_array["USERS_ANSWERED"] = $mcmr_result->numRows();
03367                         $result_array["USERS_SKIPPED"] = $nr_of_users - $mcmr_result->numRows();
03368                         $numrows = $mcmr_result->numRows();
03369                 }
03370                 else
03371                 {
03372                         $result_array["USERS_ANSWERED"] = $result->numRows();
03373                         $result_array["USERS_SKIPPED"] = $nr_of_users - $result->numRows();
03374                 }
03375                 $variables =& $this->getVariables($question_id);
03376                 switch ($questions[$question_id]["type_tag"])
03377                 {
03378                         case "qt_nominal":
03379                                 $result_array["MEDIAN"] = "";
03380                                 $result_array["ARITHMETIC_MEAN"] = "";
03381                                 $prefix = "";
03382                                 if (strcmp(key($cumulated), "") != 0)
03383                                 {
03384                                         $prefix = (key($cumulated)+1) . " - ";
03385                                 }
03386                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03387                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03388                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03389                                 $maxvalues = 0;
03390                                 foreach ($variables as $key => $value)
03391                                 {
03392                                         $maxvalues += $cumulated[$key];
03393                                 }
03394                                 foreach ($variables as $key => $value)
03395                                 {
03396                                         $percentage = 0;
03397                                         if ($numrows > 0)
03398                                         {
03399                                                 if ($questions[$question_id]["subtype"] == SUBTYPE_MCMR)
03400                                                 {
03401                                                         if ($maxvalues > 0)
03402                                                         {
03403                                                                 $percentage = (float)((int)$cumulated[$key]/$result_array["USERS_ANSWERED"]);
03404                                                         }
03405                                                 }
03406                                                 else
03407                                                 {
03408                                                         $percentage = (float)((int)$cumulated[$key]/$numrows);
03409                                                 }
03410                                         }
03411                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03412                                 }
03413                                 break;
03414                         case "qt_ordinal":
03415                                 $prefix = "";
03416                                 if (strcmp(key($cumulated), "") != 0)
03417                                 {
03418                                         $prefix = (key($cumulated)+1) . " - ";
03419                                 }
03420                                 $result_array["MODE"] =  $prefix . $variables[key($cumulated)]->title;
03421                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03422                                 foreach ($variables as $key => $value)
03423                                 {
03424                                         $percentage = 0;
03425                                         if ($numrows > 0)
03426                                         {
03427                                                 $percentage = (float)((int)$cumulated[$key]/$numrows);
03428                                         }
03429                                         $result_array["variables"][$key] = array("title" => $value->title, "selected" => (int)$cumulated[$key], "percentage" => $percentage);
03430                                 }
03431                                 ksort($cumulated, SORT_NUMERIC);
03432                                 $median = array();
03433                                 $total = 0;
03434                                 foreach ($cumulated as $value => $key)
03435                                 {
03436                                         $total += $key;
03437                                         for ($i = 0; $i < $key; $i++)
03438                                         {
03439                                                 array_push($median, $value+1);
03440                                         }
03441                                 }
03442                                 if ($total > 0)
03443                                 {
03444                                         if (($total % 2) == 0)
03445                                         {
03446                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03447                                                 if (round($median_value) != $median_value)
03448                                                 {
03449                                                         $median_value = $median_value . "<br />" . "(" . $this->lng->txt("median_between") . " " . (floor($median_value)) . "-" . $variables[floor($median_value)-1]->title . " " . $this->lng->txt("and") . " " . (ceil($median_value)) . "-" . $variables[ceil($median_value)-1]->title . ")";
03450                                                 }
03451                                         }
03452                                         else
03453                                         {
03454                                                 $median_value = $median[(($total+1)/2)-1];
03455                                         }
03456                                 }
03457                                 else
03458                                 {
03459                                         $median_value = "";
03460                                 }
03461                                 $result_array["ARITHMETIC_MEAN"] = "";
03462                                 $result_array["MEDIAN"] = $median_value;
03463                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03464                                 break;
03465                         case "qt_metric":
03466                                 $result_array["MODE"] = key($cumulated);
03467                                 $result_array["MODE_NR_OF_SELECTIONS"] = $cumulated[key($cumulated)];
03468                                 ksort($cumulated, SORT_NUMERIC);
03469                                 $counter = 0;
03470                                 foreach ($cumulated as $value => $nr_of_users)
03471                                 {
03472                                         $percentage = 0;
03473                                         if ($numrows > 0)
03474                                         {
03475                                                 $percentage = (float)($nr_of_users/$numrows);
03476                                         }
03477                                         $result_array["values"][$counter++] = array("value" => $value, "selected" => (int)$nr_of_users, "percentage" => $percentage);
03478                                 }
03479                                 $median = array();
03480                                 $total = 0;
03481                                 $x_i = 0;
03482                                 $p_i = 1;
03483                                 $x_i_inv = 0;
03484                                 $sum_part_zero = false;
03485                                 foreach ($cumulated as $value => $key)
03486                                 {
03487                                         $total += $key;
03488                                         for ($i = 0; $i < $key; $i++)
03489                                         {
03490                                                 array_push($median, $value);
03491                                                 $x_i += $value;
03492                                                 $p_i *= $value;
03493                                                 if ($value != 0)
03494                                                 {
03495                                                         $sum_part_zero = true;
03496                                                         $x_i_inv += 1/$value;
03497                                                 }
03498                                         }
03499                                 }
03500                                 if ($total > 0)
03501                                 {
03502                                         if (($total % 2) == 0)
03503                                         {
03504                                                 $median_value = 0.5 * ($median[($total/2)-1] + $median[($total/2)]);
03505                                         }
03506                                         else
03507                                         {
03508                                                 $median_value = $median[(($total+1)/2)-1];
03509                                         }
03510                                 }
03511                                 else
03512                                 {
03513                                         $median_value = "";
03514                                 }
03515                                 if ($total > 0)
03516                                 {
03517                                         if (($x_i/$total) == (int)($x_i/$total))
03518                                         {
03519                                                 $result_array["ARITHMETIC_MEAN"] = $x_i/$total;
03520                                         }
03521                                         else
03522                                         {
03523                                                 $result_array["ARITHMETIC_MEAN"] = sprintf("%.2f", $x_i/$total);
03524                                         }
03525                                 }
03526                                 else
03527                                 {
03528                                         $result_array["ARITHMETIC_MEAN"] = "";
03529                                 }
03530                                 $result_array["MEDIAN"] = $median_value;
03531                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03532                                 break;
03533                         case "qt_text":
03534                                 $result_array["ARITHMETIC_MEAN"] = "";
03535                                 $result_array["MEDIAN"] = "";
03536                                 $result_array["MODE"] = "";
03537                                 $result_array["MODE_NR_OF_SELECTIONS"] = "";
03538                                 $result_array["QUESTION_TYPE"] = $questions[$question_id]["type_tag"];
03539                                 $result_array["textvalues"] = $textvalues;
03540                                 break;
03541                 }
03542                 return $result_array;
03543         }
03544 
03545         function &getQuestions($question_ids)
03546         {
03547                 $result_array = array();
03548                 $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, ",") . ")";
03549                 $result = $this->ilias->db->query($query);
03550                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03551                 {
03552                         array_push($result_array, $row);
03553                 }
03554                 return $result_array;
03555         }
03556         
03557         function &getQuestionblocks($questionblock_ids)
03558         {
03559                 $result_array = array();
03560     $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";
03561                 $result = $this->ilias->db->query($query);
03562                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
03563                 {
03564                         if ($row["questionblock_id"] != $qbid)
03565                         {
03566                                 $sequence = 1;
03567                         }
03568                         $row["sequence"] = $sequence++;
03569                         $result_array[$row["questionblock_id"]][$row["question_id"]] = $row;
03570                         $qbid = $row["questionblock_id"];
03571                 }
03572                 return $result_array;
03573         }
03574 
03575         function &getForbiddenQuestionpools()
03576         {
03577                 global $rbacsystem;
03578                 
03579                 // get all available questionpools and remove the trashed questionspools
03580                 $forbidden_pools = array();
03581                 $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'";
03582                 $result = $this->ilias->db->query($query);
03583                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
03584                 {               
03585                         include_once("./survey/classes/class.ilObjSurveyQuestionPool.php");
03586                         if (!$rbacsystem->checkAccess("write", $row->ref_id) || (!$this->_hasUntrashedReference($row->obj_id)) || (!ilObjSurveyQuestionPool::_lookupOnline($row->obj_id)))
03587                         {
03588                                 array_push($forbidden_pools, $row->obj_id);
03589                         }
03590                 }
03591                 return $forbidden_pools;
03592         }
03593         
03601         function getQuestionsTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0, $completeonly = 0, $filter_question_type = "", $filter_questionpool = "")
03602         {
03603                 global $ilUser;
03604                 $where = "";
03605                 if (strlen($filter_text) > 0) {
03606                         switch($sel_filter_type) {
03607                                 case "title":
03608                                         $where = " AND survey_question.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03609                                         break;
03610                                 case "description":
03611                                         $where = " AND survey_question.description LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03612                                         break;
03613                                 case "author":
03614                                         $where = " AND survey_question.author LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03615                                         break;
03616                         }
03617                 }
03618   
03619                 if ($filter_question_type && (strcmp($filter_question_type, "all") != 0))
03620                 {
03621                         $where .= " AND survey_questiontype.type_tag = " . $this->ilias->db->quote($filter_question_type);
03622                 }
03623                 
03624                 if ($filter_questionpool && (strcmp($filter_questionpool, "all") != 0))
03625                 {
03626                         $where .= " AND survey_question.obj_fi = $filter_questionpool";
03627                 }
03628   
03629     // build sort order for sql query
03630                 $order = "";
03631                 $images = array();
03632     if (count($sortoptions)) 
03633                 {
03634                         include_once "./classes/class.ilUtil.php";
03635       foreach ($sortoptions as $key => $value) 
03636                         {
03637         switch($key) 
03638                                 {
03639           case "title":
03640             $order = " ORDER BY title $value";
03641             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03642             break;
03643           case "description":
03644             $order = " ORDER BY description $value";
03645             $images["description"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03646             break;
03647           case "type":
03648             $order = " ORDER BY questiontype_id $value";
03649             $images["type"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03650             break;
03651           case "author":
03652             $order = " ORDER BY author $value";
03653             $images["author"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03654             break;
03655           case "created":
03656             $order = " ORDER BY created $value";
03657             $images["created"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03658             break;
03659           case "updated":
03660             $order = " ORDER BY TIMESTAMP14 $value";
03661             $images["updated"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03662             break;
03663                                         case "qpl":
03664                                                 $order = " ORDER BY obj_fi $value";
03665             $images["qpl"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03666                                                 break;
03667         }
03668       }
03669     }
03670                 $maxentries = $ilUser->prefs["hits_per_page"];
03671                 if ($maxentries < 1)
03672                 {
03673                         $maxentries = 9999;
03674                 }
03675 
03676                 $forbidden_pools =& $this->getForbiddenQuestionpools();
03677                 $forbidden = "";
03678                 if (count($forbidden_pools))
03679                 {
03680                         $forbidden = " AND survey_question.obj_fi NOT IN (" . join($forbidden_pools, ",") . ")";
03681                 }
03682                 if ($completeonly)
03683                 {
03684                         $forbidden .= " AND survey_question.complete = " . $this->ilias->db->quote("1");
03685                 }
03686                 $existing = "";
03687                 $existing_questions =& $this->getExistingQuestions();
03688                 if (count($existing_questions))
03689                 {
03690                         $existing = " AND survey_question.question_id NOT IN (" . join($existing_questions, ",") . ")";
03691                 }
03692           $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";
03693     $query_result = $this->ilias->db->query($query);
03694                 $max = $query_result->numRows();
03695                 if ($startrow > $max -1)
03696                 {
03697                         $startrow = $max - ($max % $maxentries);
03698                 }
03699                 else if ($startrow < 0)
03700                 {
03701                         $startrow = 0;
03702                 }
03703                 $limit = " LIMIT $startrow, $maxentries";
03704           $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";
03705     $query_result = $this->ilias->db->query($query);
03706                 $rows = array();
03707                 if ($query_result->numRows())
03708                 {
03709                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03710                         {
03711                                 array_push($rows, $row);
03712                         }
03713                 }
03714                 $nextrow = $startrow + $maxentries;
03715                 if ($nextrow > $max - 1)
03716                 {
03717                         $nextrow = $startrow;
03718                 }
03719                 $prevrow = $startrow - $maxentries;
03720                 if ($prevrow < 0)
03721                 {
03722                         $prevrow = 0;
03723                 }
03724                 return array(
03725                         "rows" => $rows,
03726                         "images" => $images,
03727                         "startrow" => $startrow,
03728                         "nextrow" => $nextrow,
03729                         "prevrow" => $prevrow,
03730                         "step" => $maxentries,
03731                         "rowcount" => $max
03732                 );
03733         }
03734 
03742         function getQuestionblocksTable($sortoptions, $filter_text, $sel_filter_type, $startrow = 0)
03743         {
03744                 global $ilUser;
03745                 $where = "";
03746                 if (strlen($filter_text) > 0) {
03747                         switch($sel_filter_type) {
03748                                 case "title":
03749                                         $where = " AND survey_questionblock.title LIKE " . $this->ilias->db->quote("%" . $filter_text . "%");
03750                                         break;
03751                         }
03752                 }
03753   
03754     // build sort order for sql query
03755                 $order = "";
03756                 $images = array();
03757     if (count($sortoptions)) 
03758                 {
03759                         include_once "./classes/class.ilUtil.php";
03760       foreach ($sortoptions as $key => $value) 
03761                         {
03762         switch($key) 
03763                                 {
03764           case "title":
03765                                                 $order = " ORDER BY survey_questionblock.title $value";
03766             $images["title"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03767             break;
03768                                         case "svy":
03769                                                 $order = " ORDER BY survey_survey_question.survey_fi $value";
03770             $images["svy"] = " <img src=\"" . ilUtil::getImagePath(strtolower($value) . "_order.png", true) . "\" alt=\"" . strtolower($value) . "ending order\" />";
03771                                                 break;
03772         }
03773       }
03774     }
03775                 $maxentries = $ilUser->prefs["hits_per_page"];
03776                 if ($order)
03777                 {
03778                         $order .=  ",survey_survey_question.sequence ASC";
03779                 }
03780                 else
03781                 {
03782                         $order = " ORDER BY survey_survey_question.sequence ASC";
03783                 }
03784                 $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";
03785     $query_result = $this->ilias->db->query($query);
03786                 $questionblock_ids = array();
03787                 if ($query_result->numRows())
03788                 {
03789                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03790                         {
03791                                 array_push($questionblock_ids, $row["questionblock_id"]);
03792                         }
03793                 }
03794                 
03795                 $max = $query_result->numRows();
03796                 if ($startrow > $max -1)
03797                 {
03798                         $startrow = $max - ($max % $maxentries);
03799                 }
03800                 else if ($startrow < 0)
03801                 {
03802                         $startrow = 0;
03803                 }
03804                 $limit = " LIMIT $startrow, $maxentries";
03805                 $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";
03806     $query_result = $this->ilias->db->query($query);
03807                 $rows = array();
03808                 if ($query_result->numRows())
03809                 {
03810                         while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03811                         {
03812                                 $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
03813                                 $counter = 1;
03814                                 foreach ($questions_array as $key => $value)
03815                                 {
03816                                         $questions_array[$key] = "$counter. $value";
03817                                         $counter++;
03818                                 }
03819                                 $rows[$row["questionblock_id"]] = array(
03820                                         "questionblock_id" => $row["questionblock_id"],
03821                                         "title" => $row["title"], 
03822                                         "surveytitle" => $row["surveytitle"], 
03823                                         "questions" => join($questions_array, ", "),
03824                                         "owner" => $row["owner_fi"]
03825                                 );
03826                         }
03827                 }
03828                 $nextrow = $startrow + $maxentries;
03829                 if ($nextrow > $max - 1)
03830                 {
03831                         $nextrow = $startrow;
03832                 }
03833                 $prevrow = $startrow - $maxentries;
03834                 if ($prevrow < 0)
03835                 {
03836                         $prevrow = 0;
03837                 }
03838                 return array(
03839                         "rows" => $rows,
03840                         "images" => $images,
03841                         "startrow" => $startrow,
03842                         "nextrow" => $nextrow,
03843                         "prevrow" => $prevrow,
03844                         "step" => $maxentries,
03845                         "rowcount" => $max
03846                 );
03847         }
03848 
03857         function &_getQuestiontypes()
03858         {
03859                 global $ilDB;
03860                 
03861                 $questiontypes = array();
03862                 $query = "SELECT * FROM survey_questiontype ORDER BY type_tag";
03863                 $query_result = $ilDB->query($query);
03864                 while ($row = $query_result->fetchRow(DB_FETCHMODE_ASSOC))
03865                 {
03866                         array_push($questiontypes, $row["type_tag"]);
03867                 }
03868                 return $questiontypes;
03869         }
03870                 
03879         function to_xml()
03880         {
03881                 include_once("./classes/class.ilXmlWriter.php");
03882                 $a_xml_writer = new ilXmlWriter;
03883                 // set xml header
03884                 $a_xml_writer->xmlHeader();
03885                 $a_xml_writer->xmlStartTag("questestinterop");
03886                 $attrs = array(
03887                         "ident" => $this->getSurveyId(),
03888                         "title" => $this->getTitle()
03889                 );
03890                 $a_xml_writer->xmlStartTag("survey", $attrs);
03891                 
03892                 $a_xml_writer->xmlElement("qticomment", NULL, $this->getDescription());
03893                 $a_xml_writer->xmlElement("qticomment", NULL, "ILIAS Version=".$this->ilias->getSetting("ilias_version"));
03894                 $a_xml_writer->xmlElement("qticomment", NULL, "Author=".$this->getAuthor());
03895                 // add ILIAS specific metadata
03896                 $a_xml_writer->xmlStartTag("objectives");
03897                 $attrs = array(
03898                         "label" => "introduction"
03899                 );
03900                 $a_xml_writer->xmlStartTag("material", $attrs);
03901                 $a_xml_writer->xmlElement("mattext", NULL, $this->getIntroduction());
03902                 $a_xml_writer->xmlEndTag("material");
03903                 $a_xml_writer->xmlEndTag("objectives");
03904 
03905                 // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
03906                 $a_xml_writer->xmlStartTag("qtimetadata");
03907                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03908                 $a_xml_writer->xmlElement("fieldlabel", NULL, "author");
03909                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getAuthor());
03910                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03911                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03912                 $a_xml_writer->xmlElement("fieldlabel", NULL, "description");
03913                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getDescription());
03914                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03915                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03916                 $a_xml_writer->xmlElement("fieldlabel", NULL, "evaluation_access");
03917                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getEvaluationAccess());
03918                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03919                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03920                 $a_xml_writer->xmlElement("fieldlabel", NULL, "anonymize");
03921                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getAnonymize());
03922                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03923                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03924                 $a_xml_writer->xmlElement("fieldlabel", NULL, "status");
03925                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getStatus());
03926                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03927                 if ($this->getStartDateEnabled())
03928                 {
03929                         $a_xml_writer->xmlStartTag("qtimetadatafield");
03930                         $a_xml_writer->xmlElement("fieldlabel", NULL, "startdate");
03931                         $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("P%dY%dM%dDT0H0M0S", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
03932                         $a_xml_writer->xmlEndTag("qtimetadatafield");
03933                 }
03934                 if ($this->getEndDateEnabled())
03935                 {
03936                         $a_xml_writer->xmlStartTag("qtimetadatafield");
03937                         $a_xml_writer->xmlElement("fieldlabel", NULL, "enddate");
03938                         $a_xml_writer->xmlElement("fieldentry", NULL, sprintf("P%dY%dM%dDT0H0M0S", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
03939                         $a_xml_writer->xmlEndTag("qtimetadatafield");
03940                 }
03941                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03942                 $a_xml_writer->xmlElement("fieldlabel", NULL, "display_question_titles");
03943                 $a_xml_writer->xmlElement("fieldentry", NULL, $this->getShowQuestionTitles());
03944                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03945 
03946                 // add questionblock descriptions
03947                 $pages =& $this->getSurveyPages();
03948                 foreach ($pages as $question_array)
03949                 {
03950                         if (count($question_array) > 1)
03951                         {
03952                                 $question_ids = array();
03953                                 // found a questionblock
03954                                 foreach ($question_array as $question)
03955                                 {
03956                                         array_push($question_ids, $question["question_id"]);
03957                                 }
03958                                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03959                                 $a_xml_writer->xmlElement("fieldlabel", NULL, "questionblock_" . $question_array[0]["questionblock_id"]);
03960                                 $a_xml_writer->xmlElement("fieldentry", NULL, "<title>" . $question["questionblock_title"]. "</title><questions>" . join($question_ids, ",") . "</questions>");
03961                                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03962                         }
03963                 }
03964                 // add constraints
03965                 foreach ($pages as $question_array)
03966                 {
03967                         foreach ($question_array as $question)
03968                         {
03969                                 if (count($question["constraints"]))
03970                                 {
03971                                         // found constraints
03972                                         foreach ($question["constraints"] as $constraint)
03973                                         {
03974                                                 $a_xml_writer->xmlStartTag("qtimetadatafield");
03975                                                 $a_xml_writer->xmlElement("fieldlabel", NULL, "constraint_" . $question["question_id"]);
03976                                                 $a_xml_writer->xmlElement("fieldentry", NULL, $constraint["question"] . "," . $constraint["short"] . "," . $constraint["value"]);
03977                                                 $a_xml_writer->xmlEndTag("qtimetadatafield");
03978                                         }
03979                                 }
03980                         }
03981                 }
03982                 // add headings
03983                 foreach ($pages as $question_array)
03984                 {
03985                         foreach ($question_array as $question)
03986                         {
03987                                 if ($question["heading"])
03988                                 {
03989                                         $a_xml_writer->xmlStartTag("qtimetadatafield");
03990                                         $a_xml_writer->xmlElement("fieldlabel", NULL, "heading_" . $question["question_id"]);
03991                                         $a_xml_writer->xmlElement("fieldentry", NULL, $question["heading"]);
03992                                         $a_xml_writer->xmlEndTag("qtimetadatafield");
03993                                 }
03994                         }
03995                 }
03996                 $a_xml_writer->xmlEndTag("qtimetadata");
03997                 $a_xml_writer->xmlEndTag("survey");
03998                 $a_xml_writer->xmlEndTag("questestinterop");
03999                 $xml = $a_xml_writer->xmlDumpMem(FALSE);
04000 
04001                 $obligatory_states =& $this->getObligatoryStates();
04002                 foreach ($this->questions as $question_id) 
04003                 {
04004                         $question =& $this->_instanciateQuestion($question_id);
04005                         $qti_question = $question->to_xml(false, $obligatory_states[$question_id]);
04006                         $qti_question = preg_replace("/<questestinterop>/", "", $qti_question);
04007                         $qti_question = preg_replace("/<\/questestinterop>/", "", $qti_question);
04008                         $xml = str_replace("</questestinterop>", "$qti_question</questestinterop>", $xml);
04009                 }
04010                 return $xml;
04011         }
04012         
04022   function &_instanciateQuestion($question_id) 
04023         {
04024                 include_once "./survey/classes/class.SurveyQuestion.php";
04025                 $question_type = SurveyQuestion::_getQuestionType($question_id);
04026                 switch ($question_type) 
04027                 {
04028                         case "qt_nominal":
04029                                 include_once "./survey/classes/class.SurveyNominalQuestion.php";
04030                                 $question = new SurveyNominalQuestion();
04031                                 break;
04032                         case "qt_ordinal":
04033                                 include_once "./survey/classes/class.SurveyOrdinalQuestion.php";
04034                                 $question = new SurveyOrdinalQuestion();
04035                                 break;
04036                         case "qt_metric":
04037                                 include_once "./survey/classes/class.SurveyMetricQuestion.php";
04038                                 $question = new SurveyMetricQuestion();
04039                                 break;
04040                         case "qt_text":
04041                                 include_once "./survey/classes/class.SurveyTextQuestion.php";
04042                                 $question = new SurveyTextQuestion();
04043                                 break;
04044                 } 
04045                 $question->loadFromDb($question_id);
04046                 return $question;
04047   }
04048 
04057         function importObject($file_info, $survey_questionpool_id)
04058         {
04059                 // check if file was uploaded
04060                 $source = $file_info["tmp_name"];
04061                 $error = 0;
04062                 if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
04063                 {
04064                         $this->ilias->raiseError($this->lng->txt("import_no_file_selected"),$this->ilias->error_obj->MESSAGE);
04065                         $error = 1;
04066                 }
04067                 // check correct file type
04068                 if (!((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0)))
04069                 {
04070                         $this->ilias->raiseError($this->lng->txt("import_wrong_file_type"),$this->ilias->error_obj->MESSAGE);
04071                         $error = 1;
04072                 }
04073                 if (!$error)
04074                 {
04075                         // import file as a survey
04076                         $import_dir = $this->getImportDirectory();
04077                         $importfile = tempnam($import_dir, "survey_import");
04078                         //move_uploaded_file($source, $importfile);
04079                         include_once "./classes/class.ilUtil.php";
04080                         ilUtil::moveUploadedFile($source, "survey_import", $importfile);
04081                         $fh = fopen($importfile, "r");
04082                         if (!$fh)
04083                         {
04084                                 $this->ilias->raiseError($this->lng->txt("import_error_opening_file"),$this->ilias->error_obj->MESSAGE);
04085                                 $error = 1;
04086                                 return $error;
04087                         }
04088                         $xml = fread($fh, filesize($importfile));
04089                         $result = fclose($fh);
04090 
04091                         // delete import directory
04092                         ilUtil::delDir($this->getImportDirectory());
04093         
04094                         if (!$result)
04095                         {
04096                                 $this->ilias->raiseError($this->lng->txt("import_error_closing_file"),$this->ilias->error_obj->MESSAGE);
04097                                 $error = 1;
04098                                 return $error;
04099                         }
04100                         if (preg_match("/(<survey[^>]*>.*?<\/survey>)/si", $xml, $matches))
04101                         {
04102                                 // read survey properties
04103                                 $import_results = $this->from_xml($matches[1]);
04104                                 if ($import_results === false)
04105                                 {
04106                                         $this->ilias->raiseError($this->lng->txt("import_error_survey_no_proper_values"),$this->ilias->error_obj->MESSAGE);
04107                                         $error = 1;
04108                                         return $error;
04109                                 }
04110                         }
04111                         else
04112                         {
04113                                 $this->ilias->raiseError($this->lng->txt("import_error_survey_no_properties"),$this->ilias->error_obj->MESSAGE);
04114                                 $error = 1;
04115                                 return $error;
04116                         }
04117                         $question_counter = 0;
04118                         $new_question_ids = array();
04119                         if (preg_match_all("/(<item[^>]*>.*?<\/item>)/si", $xml, $matches))
04120                         {
04121                                 foreach ($matches[1] as $index => $item)
04122                                 {
04123                                         $question = "";
04124                                         if (preg_match("/<qticomment>Questiontype\=(.*?)<\/qticomment>/is", $item, $questiontype))
04125                                         {
04126                                                 include_once "./survey/classes/class.SurveyNominalQuestion.php";
04127                                                 include_once "./survey/classes/class.SurveyOrdinalQuestion.php";
04128                                                 include_once "./survey/classes/class.SurveyMetricQuestion.php";
04129                                                 include_once "./survey/classes/class.SurveyTextQuestion.php";
04130                                                 switch ($questiontype[1])
04131                                                 {
04132                                                         case NOMINAL_QUESTION_IDENTIFIER:
04133                                                                 $question = new SurveyNominalQuestion();
04134                                                                 break;
04135                                                         case ORDINAL_QUESTION_IDENTIFIER:
04136                                                                 $question = new SurveyOrdinalQuestion();
04137                                                                 break;
04138                                                         case METRIC_QUESTION_IDENTIFIER:
04139                                                                 $question = new SurveyMetricQuestion();
04140                                                                 break;
04141                                                         case TEXT_QUESTION_IDENTIFIER:
04142                                                                 $question = new SurveyTextQuestion();
04143                                                                 break;
04144                                                 }
04145                                                 if ($question)
04146                                                 {
04147                                                         $question->from_xml("<questestinterop>$item</questestinterop>");
04148                                                         if ($import_results !== false)
04149                                                         {
04150                                                                 $question->setObjId($survey_questionpool_id);
04151                                                                 $question->saveToDb();
04152                                                                 $question_id = $question->duplicate(true);
04153                                                                 $this->questions[$question_counter++] = $question_id;
04154                                                                 if (preg_match("/<item\s+ident\=\"(\d+)\"/", $item, $matches))
04155                                                                 {
04156                                                                         $original_question_id = $matches[1];
04157                                                                         $new_question_ids[$original_question_id] = $question_id;
04158                                                                 }
04159                                                         }
04160                                                         else
04161                                                         {
04162                                                                 $this->ilias->raiseError($this->lng->txt("error_importing_question"), $this->ilias->error_obj->MESSAGE);
04163                                                         }
04164                                                 }
04165                                         }
04166                                 }
04167                         }
04168 
04169                         $this->saveToDb();
04170                         // add question blocks
04171                         foreach ($import_results["questionblocks"] as $questionblock)
04172                         {
04173                                 foreach ($questionblock["questions"] as $key => $value)
04174                                 {
04175                                         $questionblock["questions"][$key] = $new_question_ids[$value];
04176                                 }
04177                                 $this->createQuestionblock($questionblock["title"], $questionblock["questions"]);
04178                         }
04179                         // add constraints
04180                         $relations = $this->getAllRelations(true);
04181                         foreach ($import_results["constraints"] as $constraint)
04182                         {
04183                                 $this->addConstraint($new_question_ids[$constraint["for"]], $new_question_ids[$constraint["question"]], $relations[$constraint["relation"]]["id"], $constraint["value"]);
04184                         }
04185                         foreach ($import_results["headings"] as $qid => $heading)
04186                         {
04187                                 $this->saveHeading($heading, $new_question_ids[$qid]);
04188                         }
04189                 }
04190                 return $error;
04191         }
04192 
04201         function from_xml($xml_text)
04202         {
04203                 $result = false;
04204                 $xml_text = preg_replace("/>\s*?</", "><", $xml_text);
04205                 $domxml = domxml_open_mem($xml_text);
04206                 $constraints = array();
04207                 $headings = array();
04208                 $questionblocks = array();
04209                 if (!empty($domxml))
04210                 {
04211                         $root = $domxml->document_element();
04212                         $this->setTitle($root->get_attribute("title"));
04213                         $item = $root;
04214                         $itemnodes = $item->child_nodes();
04215                         foreach ($itemnodes as $index => $node)
04216                         {
04217                                 switch ($node->node_name())
04218                                 {
04219                                         case "qticomment":
04220                                                 $comment = $node->get_content();
04221                                                 if (strpos($comment, "ILIAS Version=") !== false)
04222                                                 {
04223                                                 }
04224                                                 elseif (strpos($comment, "Questiontype=") !== false)
04225                                                 {
04226                                                 }
04227                                                 elseif (strpos($comment, "Author=") !== false)
04228                                                 {
04229                                                         $comment = str_replace("Author=", "", $comment);
04230                                                         $this->setAuthor($comment);
04231                                                 }
04232                                                 else
04233                                                 {
04234                                                         $this->setDescription($comment);
04235                                                 }
04236                                                 break;
04237                                         case "objectives":
04238                                                 $material = $node->first_child();
04239                                                 if (strcmp($material->get_attribute("label"), "introduction") == 0)
04240                                                 {
04241                                                         $mattext = $material->first_child();
04242                                                         $this->setIntroduction($mattext->get_content());
04243                                                 }
04244                                                 break;
04245                                         case "qtimetadata":
04246                                                 $metadata_fields = $node->child_nodes();
04247                                                 foreach ($metadata_fields as $index => $metadata_field)
04248                                                 {
04249                                                         $fieldlabel = $metadata_field->first_child();
04250                                                         $fieldentry = $fieldlabel->next_sibling();
04251                                                         switch ($fieldlabel->get_content())
04252                                                         {
04253                                                                 case "evaluation_access":
04254                                                                         $this->setEvaluationAccess($fieldentry->get_content());
04255                                                                         break;
04256                                                                 case "author":
04257                                                                         $this->setAuthor($fieldentry->get_content());
04258                                                                         break;
04259                                                                 case "description":
04260                                                                         $this->setDescription($fieldentry->get_content());
04261                                                                         break;
04262                                                                 case "anonymize":
04263                                                                         $this->setAnonymize($fieldentry->get_content());
04264                                                                         break;
04265                                                                 case "startdate":
04266                                                                         $iso8601period = $fieldentry->get_content();
04267                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04268                                                                         {
04269                                                                                 $this->setStartDateEnabled(true);
04270                                                                                 $this->setStartDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04271                                                                         }
04272                                                                         break;
04273                                                                 case "enddate":
04274                                                                         $iso8601period = $fieldentry->get_content();
04275                                                                         if (preg_match("/P(\d+)Y(\d+)M(\d+)DT(\d+)H(\d+)M(\d+)S/", $iso8601period, $matches))
04276                                                                         {
04277                                                                                 $this->setEndDateEnabled(true);
04278                                                                                 $this->setEndDate(sprintf("%04d-%02d-%02d", $matches[1], $matches[2], $matches[3]));
04279                                                                         }
04280                                                                         break;
04281                                                                 case "status":
04282                                                                         $this->setStatus($fieldentry->get_content());
04283                                                                         break;
04284                                                                 case "display_question_titles":
04285                                                                         if ($fieldentry->get_content() == QUESTIONTITLES_HIDDEN)
04286                                                                         {
04287                                                                                 $this->hideQuestionTitles();
04288                                                                         }
04289                                                                         else
04290                                                                         {
04291                                                                                 $this->showQuestionTitles();
04292                                                                         }
04293                                                         }
04294                                                         if (preg_match("/questionblock_\d+/", $fieldlabel->get_content()))
04295                                                         {
04296                                                                 $qb = $fieldentry->get_content();
04297                                                                 preg_match("/<title>(.*?)<\/title>/", $qb, $matches);
04298                                                                 $qb_title = $matches[1];
04299                                                                 preg_match("/<questions>(.*?)<\/questions>/", $qb, $matches);
04300                                                                 $qb_questions = $matches[1];
04301                                                                 $qb_questions_array = explode(",", $qb_questions);
04302                                                                 array_push($questionblocks, array(
04303                                                                         "title" => $qb_title,
04304                                                                         "questions" => $qb_questions_array
04305                                                                 ));
04306                                                         }
04307                                                         if (preg_match("/constraint_(\d+)/", $fieldlabel->get_content(), $matches))
04308                                                         {
04309                                                                 $constraint = $fieldentry->get_content();
04310                                                                 $constraint_array = explode(",", $constraint);
04311                                                                 if (count($constraint_array) == 3)
04312                                                                 {
04313                                                                         array_push($constraints, array(
04314                                                                                 "for"      => $matches[1], 
04315                                                                                 "question" => $constraint_array[0],
04316                                                                                 "relation" => $constraint_array[1],
04317                                                                                 "value"    => $constraint_array[2]
04318                                                                         ));
04319                                                                 }
04320                                                         }
04321                                                         if (preg_match("/heading_(\d+)/", $fieldlabel->get_content(), $matches))
04322                                                         {
04323                                                                 $heading = $fieldentry->get_content();
04324                                                                 $headings[$matches[1]] = $heading;
04325                                                         }
04326                                                 }
04327                                                 break;
04328                                 }
04329                         }
04330                         $result["questionblocks"] = $questionblocks;
04331                         $result["constraints"] = $constraints;
04332                         $result["headings"] = $headings;
04333                 }
04334                 return $result;
04335         }
04336 
04345         function &_getAvailableSurveys($use_object_id = false)
04346         {
04347                 global $rbacsystem;
04348                 global $ilDB;
04349                 
04350                 $result_array = array();
04351                 $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";
04352                 $result = $ilDB->query($query);
04353                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
04354                 {               
04355                         include_once "./classes/class.ilObject.php";
04356                         if ($rbacsystem->checkAccess("write", $row->ref_id) && (ilObject::_hasUntrashedReference($row->obj_id)))
04357                         {
04358                                 if ($use_object_id)
04359                                 {
04360                                         $result_array[$row->obj_id] = $row->title;
04361                                 }
04362                                 else
04363                                 {
04364                                         $result_array[$row->ref_id] = $row->title;
04365                                 }
04366                         }
04367                 }
04368                 return $result_array;
04369         }
04370 
04378         function _clone($obj_id)
04379         {
04380                 global $ilDB;
04381                 
04382                 $original = new ilObjSurvey($obj_id, false);
04383                 $original->loadFromDb();
04384                 
04385                 $newObj = new ilObjSurvey();
04386                 $newObj->setType("svy");
04387                 $newObj->setTitle($original->getTitle());
04388                 $newObj->setDescription($original->getDescription());
04389                 $newObj->create(true);
04390                 $newObj->createReference();
04391                 $newObj->putInTree($_GET["ref_id"]);
04392                 $newObj->setPermissions($_GET["ref_id"]);
04393 //              $newObj->notify("new",$_GET["ref_id"],$_GET["parent_non_rbac_id"],$_GET["ref_id"],$newObj->getRefId());
04394                 
04395                 $newObj->author = $original->getAuthor();
04396                 $newObj->introduction = $original->getIntroduction();
04397                 $newObj->status = $original->getStatus();
04398                 $newObj->evaluation_access = $original->getEvaluationAccess();
04399                 $newObj->start_date = $original->getStartDate();
04400                 $newObj->startdate_enabled = $original->getStartDateEnabled();
04401                 $newObj->end_date = $original->getEndDate();
04402                 $newObj->enddate_enabled = $original->getEndDateEnabled();
04403                 $newObj->invitation = $original->getInvitation();
04404                 $newObj->invitation_mode = $original->getInvitationMode();
04405                 $newObj->anonymize = $original->getAnonymize();
04406 
04407                 $question_pointer = array();
04408                 // clone the questions
04409                 include_once "./survey/classes/class.SurveyQuestion.php";
04410                 foreach ($original->questions as $key => $question_id)
04411                 {
04412                         $question = ilObjSurvey::_instanciateQuestion($question_id);
04413                         $question->id = -1;
04414                         $original_id = SurveyQuestion::_getOriginalId($question_id);
04415                         $question->saveToDb($original_id);
04416                         $newObj->questions[$key] = $question->getId();
04417                         $question_pointer[$question_id] = $question->getId();
04418                 }
04419 
04420                 $newObj->saveToDb();            
04421 
04422                 // clone the questionblocks
04423                 $questionblocks = array();
04424                 $questionblock_questions = array();
04425                 $query = sprintf("SELECT * FROM survey_questionblock_question WHERE survey_fi = %s",
04426                         $this->ilias->db->quote($original->getSurveyId() . "")
04427                 );
04428                 $result = $this->ilias->db->query($query);
04429                 if ($result->numRows() > 0)
04430                 {
04431                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04432                         {
04433                                 array_push($questionblock_questions, $row);
04434                                 $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
04435                         }
04436                 }
04437                 // create new questionblocks
04438                 foreach ($questionblocks as $key => $value)
04439                 {
04440                         $questionblock = ilObjSurvey::_getQuestionblock($key);
04441                         $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
04442                         $questionblocks[$key] = $questionblock_id;
04443                 }
04444                 // create new questionblock questions
04445                 foreach ($questionblock_questions as $key => $value)
04446                 {
04447                         $clonequery = sprintf("INSERT INTO survey_questionblock_question (questionblock_question_id, survey_fi, questionblock_fi, question_fi) VALUES (NULL, %s, %s, %s)",
04448                                 $ilDB->quote($newObj->getSurveyId() . ""),
04449                                 $ilDB->quote($questionblocks[$value["questionblock_fi"]] . ""),
04450                                 $ilDB->quote($question_pointer[$value["question_fi"]] . "")
04451                         );
04452                         $cloneresult = $this->ilias->db->query($clonequery);
04453                 }
04454                 
04455                 // clone the constraints
04456                 $constraints = ilObjSurvey::_getConstraints($original->getSurveyId());
04457                 foreach ($constraints as $key => $constraint)
04458                 {
04459                         $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
04460                 }
04461                 
04462                 // clone the obligatory states
04463                 $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s",
04464                         $this->ilias->db->quote($original->getSurveyId() . "")
04465                 );
04466                 $result = $this->ilias->db->query($query);
04467                 if ($result->numRows() > 0)
04468                 {
04469                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04470                         {
04471                                 $clonequery = sprintf("INSERT INTO survey_question_obligatory (question_obligatory_id, survey_fi, question_fi, obligatory, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
04472                                         $this->ilias->db->quote($newObj->getSurveyId() . ""),
04473                                         $this->ilias->db->quote($question_pointer[$row["question_fi"]] . ""),
04474                                         $this->ilias->db->quote($row["obligatory"])
04475                                 );
04476                                 $cloneresult = $this->ilias->db->query($clonequery);
04477                         }
04478                 }
04479 
04480                 // clone meta data
04481                 include_once "./Services/MetaData/classes/class.ilMD.php";
04482                 $md = new ilMD($original->getId(),0,$original->getType());
04483                 $new_md =& $md->cloneMD($newObj->getId(),0,$newObj->getType());
04484                 return $newObj->getRefId();
04485         }
04486 
04492         function createExportDirectory()
04493         {
04494                 include_once "./classes/class.ilUtil.php";
04495                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04496                 ilUtil::makeDir($svy_data_dir);
04497                 if(!is_writable($svy_data_dir))
04498                 {
04499                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04500                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04501                 }
04502                 
04503                 // create learning module directory (data_dir/lm_data/lm_<id>)
04504                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04505                 ilUtil::makeDir($svy_dir);
04506                 if(!@is_dir($svy_dir))
04507                 {
04508                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04509                 }
04510                 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
04511                 $export_dir = $svy_dir."/export";
04512                 ilUtil::makeDir($export_dir);
04513                 if(!@is_dir($export_dir))
04514                 {
04515                         $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
04516                 }
04517         }
04518 
04522         function getExportDirectory()
04523         {
04524                 include_once "./classes/class.ilUtil.php";
04525                 $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
04526 
04527                 return $export_dir;
04528         }
04529         
04533         function getExportFiles($dir)
04534         {
04535                 // quit if import dir not available
04536                 if (!@is_dir($dir) or
04537                         !is_writeable($dir))
04538                 {
04539                         return array();
04540                 }
04541 
04542                 // open directory
04543                 $dir = dir($dir);
04544 
04545                 // initialize array
04546                 $file = array();
04547 
04548                 // get files and save the in the array
04549                 while ($entry = $dir->read())
04550                 {
04551                         if ($entry != "." and
04552                                 $entry != ".." and
04553                                 substr($entry, -4) == ".xml" and
04554                                 ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml\$", $entry))
04555                         {
04556                                 $file[] = $entry;
04557                         }
04558                 }
04559 
04560                 // close import directory
04561                 $dir->close();
04562                 // sort files
04563                 sort ($file);
04564                 reset ($file);
04565 
04566                 return $file;
04567         }
04568 
04574         function createImportDirectory()
04575         {
04576                 include_once "./classes/class.ilUtil.php";
04577                 $svy_data_dir = ilUtil::getDataDir()."/svy_data";
04578                 ilUtil::makeDir($svy_data_dir);
04579                 
04580                 if(!is_writable($svy_data_dir))
04581                 {
04582                         $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
04583                                 .") not writeable.",$this->ilias->error_obj->FATAL);
04584                 }
04585 
04586                 // create test directory (data_dir/svy_data/svy_<id>)
04587                 $svy_dir = $svy_data_dir."/svy_".$this->getId();
04588                 ilUtil::makeDir($svy_dir);
04589                 if(!@is_dir($svy_dir))
04590                 {
04591                         $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
04592                 }
04593 
04594                 // create import subdirectory (data_dir/svy_data/svy_<id>/import)
04595                 $import_dir = $svy_dir."/import";
04596                 ilUtil::makeDir($import_dir);
04597                 if(!@is_dir($import_dir))
04598                 {
04599                         $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
04600                 }
04601         }
04602 
04606         function getImportDirectory()
04607         {
04608                 include_once "./classes/class.ilUtil.php";
04609                 $import_dir = ilUtil::getDataDir()."/svy_data".
04610                         "/svy_".$this->getId()."/import";
04611                 if (!is_dir($import_dir))
04612                 {
04613                         ilUtil::makeDirParents($import_dir);
04614                 }
04615                 if(@is_dir($import_dir))
04616                 {
04617                         return $import_dir;
04618                 }
04619                 else
04620                 {
04621                         return false;
04622                 }
04623         }
04624         
04625         function saveHeading($heading = "", $insertbefore)
04626         {
04627                 if ($heading)
04628                 {
04629                         $query = sprintf("UPDATE survey_survey_question SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
04630                                 $this->ilias->db->quote($heading),
04631                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04632                                 $this->ilias->db->quote($insertbefore)
04633                         );
04634                 }
04635                 else
04636                 {
04637                         $query = sprintf("UPDATE survey_survey_question SET heading=NULL WHERE survey_fi=%s AND question_fi=%s",
04638                                 $this->ilias->db->quote($this->getSurveyId() . ""),
04639                                 $this->ilias->db->quote($insertbefore)
04640                         );
04641                 }
04642                 $this->ilias->db->query($query);
04643         }
04644 
04645         function _getRefIdFromObjId($obj_id)
04646         {
04647                 global $ilDB;
04648                 
04649                 $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
04650                         $ilDB->quote($obj_id)
04651                         
04652                 );
04653                 $result = $ilDB->query($query);
04654                 if ($result->numRows())
04655                 {
04656                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
04657                         return $row["ref_id"];
04658                 }
04659                 return 0;
04660         }
04661         
04662         function isAnonymousKey($key)
04663         {
04664                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_key = %s AND survey_fi = %s",
04665                         $this->ilias->db->quote($key . ""),
04666                         $this->ilias->db->quote($this->getSurveyId() . "")
04667                 );
04668                 $result = $this->ilias->db->query($query);
04669                 if ($result->numRows() == 1)
04670                 {
04671                         return true;
04672                 }
04673                 else
04674                 {
04675                         return false;
04676                 }
04677         }
04678         
04679         function isAnonymizedParticipant($key)
04680         {
04681                 $query = sprintf("SELECT finished_id FROM survey_finished WHERE anonymous_id = %s AND survey_fi = %s",
04682                         $this->ilias->db->quote($key . ""),
04683                         $this->ilias->db->quote($this->getSurveyId() . "")
04684                 );
04685                 $result = $this->ilias->db->query($query);
04686                 if ($result->numRows() == 1)
04687                 {
04688                         return true;
04689                 }
04690                 else
04691                 {
04692                         return false;
04693                 }
04694         }
04695         
04696         function getUserSurveyCode()
04697         {
04698                 global $ilUser;
04699                 return md5($ilUser->id . $this->getSurveyId());
04700         }
04701         
04702         function checkSurveyCode($code)
04703         {
04704                 global $ilUser;
04705                 // check for the correct survey code
04706                 if (strcmp($ilUser->login, "anonymous") != 0)
04707                 {
04708                         $anonymize_key = $this->getUserSurveyCode();
04709                         if (strcmp(strtolower($anonymize_key), strtolower($code)) == 0)
04710                         {
04711                                 return true;
04712                         }
04713                         else
04714                         {
04715                                 return false;
04716                         }
04717                 }
04718                 else
04719                 {
04720                         if ($this->isAnonymousKey($code))
04721                         {
04722                                 if ($this->isSurveyStarted("", $code) == 1)
04723                                 {
04724                                         return false;
04725                                 }
04726                                 else
04727                                 {
04728                                         return true;
04729                                 }
04730                         }
04731                         else
04732                         {
04733                                 return false;
04734                         }
04735                 }
04736                 return false;
04737         }
04738 
04739         function &getSurveyCodes()
04740         {
04741                 $codes = array();
04742                 $query = sprintf("SELECT survey_anonymous.anonymous_id, survey_anonymous.survey_key, survey_anonymous.survey_fi, survey_anonymous.TIMESTAMP + 0 AS TIMESTAMP14, survey_finished.state FROM survey_anonymous LEFT JOIN survey_finished ON survey_anonymous.survey_key = survey_finished.anonymous_id WHERE survey_anonymous.survey_fi = %s ORDER BY TIMESTAMP14, survey_finished.anonymous_id",
04743                         $this->ilias->db->quote($this->getSurveyId() . "")
04744                 );
04745                 $result = $this->ilias->db->query($query);
04746                 
04747                 if ($result->numRows() > 0)
04748                 {
04749                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04750                         {
04751                                 array_push($codes, $row);
04752                         }
04753                 }
04754                 return $codes;
04755         }
04756         
04757         function getSurveyCodesCount()
04758         {
04759                 global $ilDB;
04760 
04761                 $query = sprintf("SELECT anonymous_id FROM survey_anonymous WHERE survey_fi = %s",
04762                         $ilDB->quote($this->getSurveyId() . "")
04763                 );
04764                 $result = $ilDB->query($query);
04765                 return $result->numRows();
04766         }
04767         
04768         function &getSurveyCodesTableData($lang = "en", $offset = 0, $limit = 10, $sort_by = "counter", $sort_order = "asc")
04769         {
04770                 global $ilDB;
04771 
04772                 include_once "./classes/class.ilFormat.php";
04773                 if (strlen($lang) == 0) $lang = "en";
04774                 if (strlen($offset) == 0) $offset = 0;
04775                 if (strlen($limit) == 0) $limit = 10;
04776                 if (strlen($sort_by) == 0) $sort_by = "counter";
04777                 if (strlen($sort_order) == 0) $sort_order = "asc";
04778                 
04779                 switch ($sort_by)
04780                 {
04781                         case "counter":
04782                                 $order = "ORDER BY TIMESTAMP14, survey_finished.anonymous_id $sort_order";
04783                                 break;
04784                         case "date":
04785                                 $order = "ORDER BY TIMESTAMP14, survey_finished.anonymous_id $sort_order";
04786                                 break;
04787                         case "used":
04788                                 $order = "ORDER BY TIMESTAMP14, survey_finished.anonymous_id $sort_order";
04789                                 break;
04790                         case "url":
04791                                 $order = "ORDER BY TIMESTAMP14, survey_finished.anonymous_id $sort_order";
04792                                 break;
04793                 }
04794                 
04795                 $codes = array();
04796                 $query = sprintf("SELECT survey_anonymous.anonymous_id, survey_anonymous.survey_key, survey_anonymous.survey_fi, survey_anonymous.TIMESTAMP + 0 AS TIMESTAMP14, survey_finished.state FROM survey_anonymous LEFT JOIN survey_finished ON survey_anonymous.survey_key = survey_finished.anonymous_id WHERE survey_anonymous.survey_fi = %s $order LIMIT $offset,$limit",
04797                         $ilDB->quote($this->getSurveyId() . "")
04798                 );
04799                 $result = $ilDB->query($query);
04800                 $counter = $offset+1;
04801                 if ($result->numRows() > 0)
04802                 {
04803                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
04804                         {
04805                                 $created = ilFormat::formatDate(ilFormat::ftimestamp2dateDB($row["TIMESTAMP14"]), "date");
04806 
04807                                 $url = "";
04808                                 
04809                                 $state = "<span class=\"smallred\">" . $this->lng->txt("not_used") . "</span>";
04810                                 if ($this->isSurveyCodeUsed($row["survey_key"]))
04811                                 {
04812                                         $state = "<span class=\"smallgreen\">" . $this->lng->txt("used") . "</span>";
04813                                 }
04814                                 else
04815                                 {
04816                                         $addlang = "";
04817                                         if (strlen($lang))
04818                                         {
04819                                                 $addlang = "&amp;lang=$lang";
04820                                         }
04821                                         $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 . "\">";
04822                                         $url .= $this->lng->txt("survey_code_url_name");
04823                                         $url .= "</a>";
04824                                 }
04825                                 array_push($codes, array($counter, $row["survey_key"], $created, $state, $url));
04826                                 $counter++;
04827                         }
04828                 }
04829                 return $codes;
04830         }
04831         
04832         function isSurveyCodeUsed($code)
04833         {
04834                 $query = sprintf("SELECT answer_id FROM survey_answer WHERE survey_fi = %s AND anonymous_id = %s",
04835                         $this->ilias->db->quote($this->getSurveyId() . ""),
04836                         $this->ilias->db->quote($code)
04837                 );
04838                 $result = $this->ilias->db->query($query);
04839                 if ($result->numRows() > 0)
04840                 {
04841                         return TRUE;
04842                 }
04843                 else
04844                 {
04845                         return FALSE;
04846                 }
04847         }
04848         
04849         function createSurveyCodes($nrOfCodes)
04850         {
04851                 for ($i = 0; $i < $nrOfCodes; $i++)
04852                 {
04853                         $anonymize_key = md5((time() + ($i*$nrOfCodes)) . $this->getSurveyId());
04854                         $query = sprintf("INSERT INTO survey_anonymous (anonymous_id, survey_key, survey_fi, TIMESTAMP) VALUES (NULL, %s, %s, NULL)",
04855                                 $this->ilias->db->quote($anonymize_key . ""),
04856                                 $this->ilias->db->quote($this->getSurveyId() . "")
04857                         );
04858                         $result = $this->ilias->db->query($query);
04859                 }
04860         }
04861         
04867         function getRandomSurveyCode()
04868         {
04869                 return md5(time() . $this->getSurveyId());
04870         }
04871         
04877         function _goto($a_target, $a_access_code = "")
04878         {
04879                 global $rbacsystem, $ilErr, $lng;
04880 
04881                 include_once './classes/class.ilSearch.php';
04882                         
04883                 // Added this additional check (ParentConditions) to avoid calls of objects inside e.g courses.
04884                 // Will be replaced in future releases by ilAccess::checkAccess()
04885                 if ($rbacsystem->checkAccess("read", $a_target) and ilSearch::_checkParentConditions($a_target))
04886                 {
04887                         include_once "./classes/class.ilUtil.php";
04888                         if (strlen($a_access_code))
04889                         {
04890                                 $_SESSION["accesscode"] = $a_access_code;
04891                                 ilUtil::redirect("ilias.php?baseClass=ilObjSurveyGUI&cmd=run&ref_id=$a_target");
04892                         }
04893                         else
04894                         {
04895                                 ilUtil::redirect("ilias.php?baseClass=ilObjSurveyGUI&cmd=run&ref_id=$a_target");
04896                         }
04897                 }
04898                 else
04899                 {
04900                         $ilErr->raiseError($lng->txt("msg_no_perm_read_lm"), $ilErr->FATAL);
04901                 }
04902         }
04903 
04916         function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
04917         {
04918                 $resultarray = array();
04919                 foreach ($row as $rowindex => $entry)
04920                 {
04921                         $surround = FALSE;
04922                         if ($quoteAll)
04923                         {
04924                                 $surround = TRUE;
04925                         }
04926                         if (strpos($entry, "\"") !== FALSE)
04927                         {
04928                                 $entry = str_replace("\"", "\"\"", $entry);
04929                                 $surround = TRUE;
04930                         }
04931                         if (strpos($entry, $separator) !== FALSE)
04932                         {
04933                                 $surround = TRUE;
04934                         }
04935                         // replace all CR LF with LF (for Excel for Windows compatibility
04936                         $entry = str_replace(chr(13).chr(10), chr(10), $entry);
04937                         if ($surround)
04938                         {
04939                                 $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
04940                         }
04941                         else
04942                         {
04943                                 $resultarray[$rowindex] = utf8_decode($entry);
04944                         }
04945                 }
04946                 return $resultarray;
04947         }
04948 } // END class.ilObjSurvey
04949 ?>

Generated on Fri Dec 13 2013 11:58:01 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1