ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjSurvey.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 include_once "./classes/class.ilObject.php";
35 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
36 
37 class ilObjSurvey extends ilObject
38 {
46 
53  var $author;
54 
61 
67  var $outro;
68 
74  var $status;
75 
82 
89 
96 
103 
110 
117 
124 
131 
137 
143 
150 
157  function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
158  {
159  global $ilUser;
160  $this->type = "svy";
161  $this->ilObject($a_id,$a_call_by_reference);
162 
163  $this->survey_id = -1;
164  $this->introduction = "";
165  $this->outro = $this->lng->txt("survey_finished");
166  $this->author = $ilUser->fullname;
167  $this->status = STATUS_OFFLINE;
168  $this->evaluation_access = EVALUATION_ACCESS_OFF;
169  $this->startdate_enabled = 0;
170  $this->enddate_enabled = 0;
171  $this->questions = array();
172  $this->invitation = INVITATION_OFF;
173  $this->invitation_mode = MODE_PREDEFINED_USERS;
174  $this->anonymize = ANONYMIZE_OFF;
175  $this->display_question_titles = QUESTIONTITLES_VISIBLE;
176  $this->surveyCodeSecurity = TRUE;
177  }
178 
182  function create($a_upload = false)
183  {
184  parent::create();
185  if(!$a_upload)
186  {
187  $this->createMetaData();
188  }
189  }
190 
196  function createMetaData()
197  {
199  $this->saveAuthorToMetadata();
200  }
201 
208  function update()
209  {
210  $this->updateMetaData();
211 
212  if (!parent::update())
213  {
214  return false;
215  }
216 
217  // put here object specific stuff
218 
219  return true;
220  }
221 
222  function createReference()
223  {
225  $this->saveToDb();
226  return $result;
227  }
228 
234  function read($a_force_db = false)
235  {
236  parent::read($a_force_db);
237  $this->loadFromDb();
238  }
239 
246  function addQuestion($question_id)
247  {
248  array_push($this->questions, $question_id);
249  }
250 
251 
258  function delete()
259  {
260  $remove = parent::delete();
261  // always call parent delete function first!!
262  if (!$remove)
263  {
264  return false;
265  }
266 
267  $this->deleteMetaData();
268 
269  // Delete all survey questions, constraints and materials
270  foreach ($this->questions as $question_id)
271  {
272  $this->removeQuestion($question_id);
273  }
274  $this->deleteSurveyRecord();
275 
277  return true;
278  }
279 
286  {
287  global $ilDB;
288 
289  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_svy WHERE survey_id = %s",
290  array('integer'),
291  array($this->getSurveyId())
292  );
293 
294  $result = $ilDB->queryF("SELECT questionblock_fi FROM svy_qblk_qst WHERE survey_fi = %s",
295  array('integer'),
296  array($this->getSurveyId())
297  );
298  $questionblocks = array();
299  while ($row = $ilDB->fetchAssoc($result))
300  {
301  array_push($questionblocks, $row["questionblock_fi"]);
302  }
303  if (count($questionblocks))
304  {
305  $affectedRows = $ilDB->manipulate("DELETE FROM svy_qblk WHERE " . $ilDB->in('questionblock_id', $questionblocks, false, 'integer'));
306  }
307  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE survey_fi = %s",
308  array('integer'),
309  array($this->getSurveyId())
310  );
311  $this->deleteAllUserData();
312 
313  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_anonymous WHERE survey_fi = %s",
314  array('integer'),
315  array($this->getSurveyId())
316  );
317 
318  // delete export files
319  include_once "./Services/Utilities/classes/class.ilUtil.php";
320  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
321  $directory = $svy_data_dir."/svy_".$this->getId();
322  if (is_dir($directory))
323  {
324  include_once "./Services/Utilities/classes/class.ilUtil.php";
325  ilUtil::delDir($directory);
326  }
327 
328  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
329  $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
330  // remaining usages are not in text anymore -> delete them
331  // and media objects (note: delete method of ilObjMediaObject
332  // checks whether object is used in another context; if yes,
333  // the object is not deleted!)
334  foreach($mobs as $mob)
335  {
336  ilObjMediaObject::_removeUsage($mob, "svy:html", $this->getId());
337  $mob_obj =& new ilObjMediaObject($mob);
338  $mob_obj->delete();
339  }
340  }
341 
347  function deleteAllUserData()
348  {
349  global $ilDB;
350 
351  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
352  array('integer'),
353  array($this->getSurveyId())
354  );
355  $active_array = array();
356  while ($row = $ilDB->fetchAssoc($result))
357  {
358  array_push($active_array, $row["finished_id"]);
359  }
360 
361  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_finished WHERE survey_fi = %s",
362  array('integer'),
363  array($this->getSurveyId())
364  );
365 
366  foreach ($active_array as $active_fi)
367  {
368  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE active_fi = %s",
369  array('integer'),
370  array($active_fi)
371  );
372  }
373  }
374 
380  function removeSelectedSurveyResults($finished_ids)
381  {
382  global $ilDB;
383 
384  foreach ($finished_ids as $finished_id)
385  {
386  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE finished_id = %s",
387  array('integer'),
388  array($finished_id)
389  );
390  $row = $ilDB->fetchAssoc($result);
391 
392  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE active_fi = %s",
393  array('integer'),
394  array($row["finished_id"])
395  );
396 
397  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_finished WHERE finished_id = %s",
398  array('integer'),
399  array($finished_id)
400  );
401  }
402  }
403 
405  {
406  global $ilDB;
407 
408  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
409  array('integer'),
410  array($this->getSurveyId())
411  );
412  $participants = array();
413  if ($result->numRows() > 0)
414  {
415  while ($row = $ilDB->fetchAssoc($result))
416  {
417  $userdata = $this->getUserDataFromActiveId($row["finished_id"]);
418  $participants[$userdata["sortname"] . $userdata["active_id"]] = $userdata;
419  }
420  }
421  return $participants;
422  }
423 
437  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
438  {
439  global $tree;
440 
441  switch ($a_event)
442  {
443  case "link":
444 
445  //var_dump("<pre>",$a_params,"</pre>");
446  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
447  //exit;
448  break;
449 
450  case "cut":
451 
452  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
453  //exit;
454  break;
455 
456  case "copy":
457 
458  //var_dump("<pre>",$a_params,"</pre>");
459  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
460  //exit;
461  break;
462 
463  case "paste":
464 
465  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
466  //exit;
467  break;
468 
469  case "new":
470 
471  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
472  //exit;
473  break;
474  }
475 
476  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
477  if ($a_node_id==$_GET["ref_id"])
478  {
479  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
480  $parent_type = $parent_obj->getType();
481  if($parent_type == $this->getType())
482  {
483  $a_node_id = (int) $tree->getParentId($a_node_id);
484  }
485  }
486 
487  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
488  }
489 
496  function isComplete()
497  {
498  if (($this->getTitle()) and (count($this->questions)))
499  {
500  return 1;
501  }
502  else
503  {
504  return 0;
505  }
506  }
507 
514  function _isComplete($obj_id)
515  {
516  $survey = new ilObjSurvey($obj_id, false);
517  $survey->loadFromDb();
518  if (($survey->getTitle()) and (count($survey->questions)))
519  {
520  return 1;
521  }
522  else
523  {
524  return 0;
525  }
526  }
527 
534  function &_getGlobalSurveyData($obj_id)
535  {
536  $survey = new ilObjSurvey($obj_id, false);
537  $survey->loadFromDb();
538  $result = array();
539  if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
540  {
541  $result["complete"] = true;
542  }
543  else
544  {
545  $result["complete"] = false;
546  }
547  $result["evaluation_access"] = $survey->getEvaluationAccess();
548  return $result;
549  }
550 
557  {
558  global $ilDB;
559 
560  $complete = 0;
561  if ($this->isComplete())
562  {
563  $complete = 1;
564  }
565  if ($this->getSurveyId() > 0)
566  {
567  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy SET complete = %s, tstamp = %s WHERE survey_id = %s",
568  array('text','integer','integer'),
569  array($this->isComplete(), time(), $this->getSurveyId())
570  );
571  }
572  }
573 
581  function duplicateQuestionForSurvey($question_id)
582  {
583  global $ilUser;
584 
585  $questiontype = $this->getQuestionType($question_id);
586  $question_gui = $this->getQuestionGUI($questiontype, $question_id);
587  $duplicate_id = $question_gui->object->duplicate(true);
588  return $duplicate_id;
589  }
590 
596  function insertQuestion($question_id)
597  {
598  global $ilDB;
599 
600  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
601  if (!SurveyQuestion::_isComplete($question_id))
602  {
603  return FALSE;
604  }
605  else
606  {
607  // get maximum sequence index in test
608  $result = $ilDB->queryF("SELECT survey_question_id FROM svy_svy_qst WHERE survey_fi = %s",
609  array('integer'),
610  array($this->getSurveyId())
611  );
612  $sequence = $result->numRows();
613  $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
614  $next_id = $ilDB->nextId('svy_svy_qst');
615  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_svy_qst (survey_question_id, survey_fi, question_fi, sequence, tstamp) VALUES (%s, %s, %s, %s, %s)",
616  array('integer', 'integer', 'integer', 'integer', 'integer'),
617  array($next_id, $this->getSurveyId(), $duplicate_id, $sequence, time())
618  );
619  $this->loadQuestionsFromDb();
620  return TRUE;
621  }
622  }
623 
624 
630  function insertQuestionblock($questionblock_id)
631  {
632  global $ilDB;
633  $result = $ilDB->queryF("SELECT svy_qblk.title, svy_qblk.show_questiontext, svy_qblk_qst.question_fi FROM svy_qblk, svy_qblk_qst, svy_svy_qst WHERE svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_svy_qst.question_fi = svy_qblk_qst.question_fi AND svy_qblk.questionblock_id = %s ORDER BY svy_svy_qst.sequence",
634  array('integer'),
635  array($questionblock_id)
636  );
637  $questions = array();
638  $show_questiontext = 0;
639  while ($row = $ilDB->fetchAssoc($result))
640  {
641  $duplicate_id = $this->duplicateQuestionForSurvey($row["question_fi"]);
642  array_push($questions, $duplicate_id);
643  $title = $row["title"];
644  $show_questiontext = $row["show_questiontext"];
645  }
646  $this->createQuestionblock($title, $show_questiontext, $questions);
647  }
648 
654  function getAllRTEContent()
655  {
656  $result = array();
657  array_push($result, $this->getIntroduction());
658  array_push($result, $this->getOutro());
659  return $result;
660  }
661 
668  {
669  include_once("./Services/RTE/classes/class.ilRTE.php");
670  $completecontent = "";
671  foreach ($this->getAllRTEContent() as $content)
672  {
673  $completecontent .= $content;
674  }
675  ilRTE::_cleanupMediaObjectUsage($completecontent, $this->getType() . ":html",
676  $this->getId());
677  }
678 
684  function saveToDb()
685  {
686  global $ilDB;
687 
688  include_once("./Services/RTE/classes/class.ilRTE.php");
689  if ($this->getSurveyId() < 1)
690  {
691  $next_id = $ilDB->nextId('svy_svy');
692  $affectedRows = $ilDB->insert("svy_svy", array(
693  "survey_id" => array("integer", $next_id),
694  "obj_fi" => array("integer", $this->getId()),
695  "author" => array("text", $this->getAuthor()),
696  "introduction" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getIntroduction(), 0)),
697  "outro" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
698  "status" => array("text", $this->getStatus()),
699  "startdate" => array("timestamp", $this->getStartDate()),
700  "enddate" => array("timestamp", $this->getEndDate()),
701  "evaluation_access" => array("text", $this->getEvaluationAccess()),
702  "invitation" => array("text", $this->getInvitation()),
703  "invitation_mode" => array("text", $this->getInvitationMode()),
704  "complete" => array("text", $this->isComplete()),
705  "created" => array("integer", time()),
706  "anonymize" => array("text", $this->getAnonymize()),
707  "show_question_titles" => array("text", $this->getShowQuestionTitles()),
708  "tstamp" => array("integer", time())
709  ));
710  $this->setSurveyId($next_id);
711  }
712  else
713  {
714  $affectedRows = $ilDB->update("svy_svy", array(
715  "author" => array("text", $this->getAuthor()),
716  "introduction" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getIntroduction(), 0)),
717  "outro" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
718  "status" => array("text", $this->getStatus()),
719  "startdate" => array("timestamp", $this->getStartDate()),
720  "enddate" => array("timestamp", $this->getEndDate()),
721  "evaluation_access" => array("text", $this->getEvaluationAccess()),
722  "invitation" => array("text", $this->getInvitation()),
723  "invitation_mode" => array("text", $this->getInvitationMode()),
724  "complete" => array("text", $this->isComplete()),
725  "anonymize" => array("text", $this->getAnonymize()),
726  "show_question_titles" => array("text", $this->getShowQuestionTitles()),
727  "tstamp" => array("integer", time())
728  ), array(
729  "survey_id" => array("integer", $this->getSurveyId())
730  ));
731  }
732  if ($affectedRows)
733  {
734  // save questions to db
735  $this->saveQuestionsToDb();
736  }
737  }
738 
745  function saveQuestionsToDb()
746  {
747  global $ilDB;
748  // save old questions state
749  $old_questions = array();
750  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s",
751  array('integer'),
752  array($this->getSurveyId())
753  );
754  if ($result->numRows())
755  {
756  while ($row = $ilDB->fetchAssoc($result))
757  {
758  $old_questions[$row["question_fi"]] = $row;
759  }
760  }
761 
762  // delete existing question relations
763  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_svy_qst WHERE survey_fi = %s",
764  array('integer'),
765  array($this->getSurveyId())
766  );
767  // create new question relations
768  foreach ($this->questions as $key => $value)
769  {
770  $next_id = $ilDB->nextId('svy_svy_qst');
771  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_svy_qst (survey_question_id, survey_fi, question_fi, heading, sequence, tstamp) VALUES (%s, %s, %s, %s, %s, %s)",
772  array('integer','integer','integer','text','integer','integer'),
773  array($next_id, $this->getSurveyId(), $value, (strlen($old_questions[$value]["heading"])) ? $old_questions[$value]["heading"] : NULL, $key, time())
774  );
775  }
776  }
777 
785  function getAnonymousId($id)
786  {
787  global $ilDB;
788  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_finished WHERE anonymous_id = %s",
789  array('text'),
790  array($id)
791  );
792  if ($result->numRows())
793  {
794  $row = $ilDB->fetchAssoc($result);
795  return $row["anonymous_id"];
796  }
797  else
798  {
799  return "";
800  }
801  }
802 
809  function getQuestionGUI($questiontype, $question_id)
810  {
811  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestionGUI.php";
812  return SurveyQuestionGUI::_getQuestionGUI($questiontype, $question_id);
813  }
814 
822  function getQuestionType($question_id)
823  {
824  global $ilDB;
825  if ($question_id < 1) return -1;
826  $result = $ilDB->queryF("SELECT type_tag FROM svy_question, svy_qtype WHERE svy_question.question_id = %s AND " .
827  "svy_question.questiontype_fi = svy_qtype.questiontype_id",
828  array('integer'),
829  array($question_id)
830  );
831  if ($result->numRows() == 1)
832  {
833  $data = $ilDB->fetchAssoc($result);
834  return $data["type_tag"];
835  }
836  else
837  {
838  return "";
839  }
840  }
841 
848  function getSurveyId()
849  {
850  return $this->survey_id;
851  }
852 
856  function setAnonymize($a_anonymize)
857  {
858  switch ($a_anonymize)
859  {
860  case ANONYMIZE_OFF:
861  case ANONYMIZE_ON:
863  $this->anonymize = $a_anonymize;
864  break;
865  default:
866  $this->anonymize = ANONYMIZE_OFF;
867  break;
868  }
869  }
870 
876  function getAnonymize()
877  {
878  return ($this->anonymize) ? $this->anonymize : 0;
879  }
880 
887  {
888  if ($this->getAnonymize() == ANONYMIZE_FREEACCESS)
889  {
890  return true;
891  }
892  else
893  {
894  return false;
895  }
896  }
897 
903  function loadFromDb()
904  {
905  global $ilDB;
906  $result = $ilDB->queryF("SELECT * FROM svy_svy WHERE obj_fi = %s",
907  array('integer'),
908  array($this->getId())
909  );
910  if ($result->numRows() == 1)
911  {
912  $data = $ilDB->fetchAssoc($result);
913  $this->setSurveyId($data["survey_id"]);
914  $this->setAuthor($data["author"]);
915  include_once("./Services/RTE/classes/class.ilRTE.php");
916  $this->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($data["introduction"], 1));
917  if (strcmp($data["outro"], "survey_finished") == 0)
918  {
919  $this->setOutro($this->lng->txt("survey_finished"));
920  }
921  else
922  {
924  }
925  $this->setInvitation($data["invitation"]);
926  $this->setInvitationMode($data["invitation_mode"]);
927  $this->setShowQuestionTitles($data["show_question_titles"]);
928  $this->setStartDate($data["startdate"]);
929  $this->setStartDateEnabled(strlen($data["startdate"]));
930  $this->setEndDate($data["enddate"]);
931  $this->setEndDateEnabled(strlen($data["enddate"]));
932  $this->setAnonymize($data["anonymize"]);
933  $this->setEvaluationAccess($data["evaluation_access"]);
934  $this->loadQuestionsFromDb();
935  $this->setStatus($data["status"]);
936  }
937  }
938 
946  {
947  global $ilDB;
948  $this->questions = array();
949  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s ORDER BY sequence",
950  array('integer'),
951  array($this->getSurveyId())
952  );
953  while ($data = $ilDB->fetchAssoc($result))
954  {
955  $this->questions[$data["sequence"]] = $data["question_fi"];
956  }
957  }
958 
966  function setStartDateEnabled($enabled = false)
967  {
968  $this->startdate_enabled = ($enabled) ? $enabled : 0;
969  }
970 
979  {
980  return ($this->startdate_enabled) ? $this->startdate_enabled : 0;
981  }
982 
990  function setEndDateEnabled($enabled = false)
991  {
992  $this->enddate_enabled = ($enabled) ? $enabled : 0;
993  }
994 
1003  {
1004  return ($this->enddate_enabled) ? $this->enddate_enabled : 0;
1005  }
1006 
1014  function setAuthor($author = "")
1015  {
1016  $this->author = $author;
1017  }
1018 
1028  function saveAuthorToMetadata($a_author = "")
1029  {
1030  $md =& new ilMD($this->getId(), 0, $this->getType());
1031  $md_life =& $md->getLifecycle();
1032  if (!$md_life)
1033  {
1034  if (strlen($a_author) == 0)
1035  {
1036  global $ilUser;
1037  $a_author = $ilUser->getFullname();
1038  }
1039 
1040  $md_life =& $md->addLifecycle();
1041  $md_life->save();
1042  $con =& $md_life->addContribute();
1043  $con->setRole("Author");
1044  $con->save();
1045  $ent =& $con->addEntity();
1046  $ent->setEntity($a_author);
1047  $ent->save();
1048  }
1049  }
1050 
1058  function getAuthor()
1059  {
1060  $author = array();
1061  include_once "./Services/MetaData/classes/class.ilMD.php";
1062  $md =& new ilMD($this->getId(), 0, $this->getType());
1063  $md_life =& $md->getLifecycle();
1064  if ($md_life)
1065  {
1066  $ids =& $md_life->getContributeIds();
1067  foreach ($ids as $id)
1068  {
1069  $md_cont =& $md_life->getContribute($id);
1070  if (strcmp($md_cont->getRole(), "Author") == 0)
1071  {
1072  $entids =& $md_cont->getEntityIds();
1073  foreach ($entids as $entid)
1074  {
1075  $md_ent =& $md_cont->getEntity($entid);
1076  array_push($author, $md_ent->getEntity());
1077  }
1078  }
1079  }
1080  }
1081  return join($author, ",");
1082  }
1083 
1090  public function getShowQuestionTitles()
1091  {
1092  return ($this->display_question_titles) ? 1 : 0;
1093  }
1094 
1101  public function setShowQuestionTitles($a_show)
1102  {
1103  $this->display_question_titles = ($a_show) ? 1 : 0;
1104  }
1105 
1113  {
1114  $this->display_question_titles = 1;
1115  }
1116 
1124  {
1125  $this->display_question_titles = 0;
1126  }
1127 
1136  {
1137  global $ilDB;
1138  global $ilAccess;
1139  $this->invitation = $invitation;
1140  if ($invitation == INVITATION_OFF)
1141  {
1142  $this->disinviteAllUsers();
1143  }
1144  else if ($invitation == INVITATION_ON)
1145  {
1146  if ($this->getInvitationMode() == MODE_UNLIMITED)
1147  {
1148  $result = $ilDB->query("SELECT usr_id FROM usr_data");
1149  while ($row = $ilDB->fetchAssoc($result))
1150  {
1151  if ($ilAccess->checkAccessOfUser($row["usr_id"], "read", "", $this->getRefId(), "svy", $this->getId()))
1152  {
1153  $this->inviteUser($row['usr_id']);
1154  }
1155  }
1156  }
1157  }
1158  }
1159 
1168  {
1169  $this->invitation_mode = $invitation_mode;
1170  }
1171 
1181  {
1182  $this->invitation_mode = $invitation_mode;
1183  $this->setInvitation($invitation);
1184  }
1185 
1192  public function setIntroduction($introduction = "")
1193  {
1194  $this->introduction = $introduction;
1195  }
1196 
1203  public function setOutro($outro = "")
1204  {
1205  $this->outro = $outro;
1206  }
1207 
1215  function getInvitation()
1216  {
1217  return ($this->invitation) ? $this->invitation : INVITATION_OFF;
1218  }
1219 
1227  function getInvitationMode()
1228  {
1229  include_once "./Services/Administration/classes/class.ilSetting.php";
1230  $surveySetting = new ilSetting("survey");
1231  $unlimited_invitation = $surveySetting->get("unlimited_invitation");
1232  if (!$unlimited_invitation && $this->invitation_mode == MODE_UNLIMITED)
1233  {
1234  return MODE_PREDEFINED_USERS;
1235  }
1236  else
1237  {
1238  return ($this->invitation_mode) ? $this->invitation_mode : MODE_UNLIMITED;
1239  }
1240  }
1241 
1249  function getStatus()
1250  {
1251  return ($this->status) ? $this->status : STATUS_OFFLINE;
1252  }
1253 
1261  function isOnline()
1262  {
1263  return ($this->status == STATUS_ONLINE) ? true : false;
1264  }
1265 
1273  function isOffline()
1274  {
1275  return ($this->status == STATUS_OFFLINE) ? true : false;
1276  }
1277 
1287  {
1288  $result = "";
1289  if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
1290  {
1291  $this->status = STATUS_OFFLINE;
1292  $result = $this->lng->txt("cannot_switch_to_online_no_questions");
1293  }
1294  else
1295  {
1296  $this->status = $status;
1297  }
1298  return $result;
1299  }
1300 
1308  function getStartDate()
1309  {
1310  return (strlen($this->start_date)) ? $this->start_date : NULL;
1311  }
1312 
1319  function canStartSurvey($anonymous_id = NULL)
1320  {
1321  global $ilAccess;
1322 
1323  $result = TRUE;
1324  $messages = array();
1325  // check start date
1326  if ($this->getStartDateEnabled())
1327  {
1328  $epoch_time = mktime(0, 0, 0, $this->getStartMonth(), $this->getStartDay(), $this->getStartYear());
1329  $now = mktime();
1330  if ($now < $epoch_time)
1331  {
1332  array_push($messages,$this->lng->txt('start_date_not_reached').' ('.
1334  new ilDate($this->getStartYear().$this->getStartMonth().$this->getStartDay()."000000",IL_CAL_TIMESTAMP)). ")");
1335  $result = FALSE;
1336  }
1337  }
1338  // check end date
1339  if ($this->getEndDateEnabled())
1340  {
1341  $epoch_time = mktime(0, 0, 0, $this->getEndMonth(), $this->getEndDay(), $this->getEndYear());
1342  $now = mktime();
1343  if ($now > $epoch_time)
1344  {
1345  array_push($messages,$this->lng->txt('end_date_reached').' ('.
1347  new ilDate($this->getEndYear().$this->getEndMonth().$this->getEndDay()."000000",IL_CAL_TIMESTAMP)). ")");
1348  $result = FALSE;
1349  }
1350  }
1351  // check online status
1352  if ($this->getStatus() == STATUS_OFFLINE)
1353  {
1354  array_push($messages, $this->lng->txt("survey_is_offline"));
1355  $result = FALSE;
1356  }
1357  // check rbac permissions
1358  if (!$ilAccess->checkAccess("read", "", $this->ref_id))
1359  {
1360  array_push($messages, $this->lng->txt("cannot_participate_survey"));
1361  $result = FALSE;
1362  }
1363  // 2. check previous access
1364  if (!$result["error"])
1365  {
1366  global $ilUser;
1367  $survey_started = $this->isSurveyStarted($ilUser->getId(), $anonymous_id);
1368  if ($survey_started === 1)
1369  {
1370  array_push($messages, $this->lng->txt("already_completed_survey"));
1371  $result = FALSE;
1372  }
1373  }
1374  return array(
1375  "result" => $result,
1376  "messages" => $messages
1377  );
1378  }
1379 
1387  function setStartDate($start_date = "")
1388  {
1389  $this->start_date = $start_date;
1390  }
1391 
1399  function getStartMonth()
1400  {
1401  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
1402  {
1403  return $matches[2];
1404  }
1405  else
1406  {
1407  return "";
1408  }
1409  }
1410 
1418  function getStartDay()
1419  {
1420  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
1421  {
1422  return $matches[3];
1423  }
1424  else
1425  {
1426  return "";
1427  }
1428  }
1429 
1437  function getStartYear()
1438  {
1439  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->start_date, $matches))
1440  {
1441  return $matches[1];
1442  }
1443  else
1444  {
1445  return "";
1446  }
1447  }
1448 
1456  function getEndDate()
1457  {
1458  return (strlen($this->end_date)) ? $this->end_date : NULL;
1459  }
1460 
1468  function setEndDate($end_date = "")
1469  {
1470  $this->end_date = $end_date;
1471  }
1472 
1480  function getEndMonth()
1481  {
1482  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
1483  {
1484  return $matches[2];
1485  }
1486  else
1487  {
1488  return "";
1489  }
1490  }
1491 
1499  function getEndDay()
1500  {
1501  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
1502  {
1503  return $matches[3];
1504  }
1505  else
1506  {
1507  return "";
1508  }
1509  }
1510 
1518  function getEndYear()
1519  {
1520  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $this->end_date, $matches))
1521  {
1522  return $matches[1];
1523  }
1524  else
1525  {
1526  return "";
1527  }
1528  }
1529 
1538  {
1539  return ($this->evaluation_access) ? $this->evaluation_access : EVALUATION_ACCESS_OFF;
1540  }
1541 
1550  {
1551  $this->evaluation_access = ($evaluation_access) ? $evaluation_access : EVALUATION_ACCESS_OFF;
1552  }
1553 
1561  function getIntroduction()
1562  {
1563  return (strlen($this->introduction)) ? $this->introduction : NULL;
1564  }
1565 
1573  function getOutro()
1574  {
1575  return (strlen($this->outro)) ? $this->outro : NULL;
1576  }
1577 
1585  {
1586  global $ilDB;
1587  $existing_questions = array();
1588  $result = $ilDB->queryF("SELECT svy_question.original_id FROM svy_question, svy_svy_qst WHERE " .
1589  "svy_svy_qst.survey_fi = %s AND svy_svy_qst.question_fi = svy_question.question_id",
1590  array('integer'),
1591  array($this->getSurveyId())
1592  );
1593  while ($data = $ilDB->fetchAssoc($result))
1594  {
1595  array_push($existing_questions, $data["original_id"]);
1596  }
1597  return $existing_questions;
1598  }
1599 
1606  function &getQuestionpoolTitles($could_be_offline = FALSE, $showPath = FALSE)
1607  {
1608  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
1609  return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_object_id = TRUE, $could_be_offline, $showPath);
1610  }
1611 
1618  function moveUpQuestion($question_id)
1619  {
1620  $move_questions = array($question_id);
1621  $pages =& $this->getSurveyPages();
1622  $pageindex = -1;
1623  foreach ($pages as $idx => $page)
1624  {
1625  if ($page[0]["question_id"] == $question_id)
1626  {
1627  $pageindex = $idx;
1628  }
1629  }
1630  if ($pageindex > 0)
1631  {
1632  $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
1633  }
1634  else
1635  {
1636  // move up a question in a questionblock
1637  $questions = $this->getSurveyQuestions();
1638  $questions = array_keys($questions);
1639  $index = array_search($question_id, $questions);
1640  if (($index !== FALSE) && ($index > 0))
1641  {
1642  $this->moveQuestions($move_questions, $questions[$index-1], 0);
1643  }
1644  }
1645  }
1646 
1652  public function moveDownQuestion($question_id)
1653  {
1654  $move_questions = array($question_id);
1655  $pages =& $this->getSurveyPages();
1656  $pageindex = -1;
1657  foreach ($pages as $idx => $page)
1658  {
1659  if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
1660  {
1661  $pageindex = $idx;
1662  }
1663  }
1664  if (($pageindex < count($pages)-1) && ($pageindex >= 0))
1665  {
1666  $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
1667  }
1668  else
1669  {
1670  // move down a question in a questionblock
1671  $questions = $this->getSurveyQuestions();
1672  $questions = array_keys($questions);
1673  $index = array_search($question_id, $questions);
1674  if (($index !== FALSE) && ($index < count($questions)-1))
1675  {
1676  $this->moveQuestions($move_questions, $questions[$index+1], 1);
1677  }
1678  }
1679  }
1680 
1687  function moveUpQuestionblock($questionblock_id)
1688  {
1689  $pages =& $this->getSurveyPages();
1690  $move_questions = array();
1691  $pageindex = -1;
1692  foreach ($pages as $idx => $page)
1693  {
1694  if ($page[0]["questionblock_id"] == $questionblock_id)
1695  {
1696  foreach ($page as $pageidx => $question)
1697  {
1698  array_push($move_questions, $question["question_id"]);
1699  }
1700  $pageindex = $idx;
1701  }
1702  }
1703  if ($pageindex > 0)
1704  {
1705  $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
1706  }
1707  }
1708 
1715  function moveDownQuestionblock($questionblock_id)
1716  {
1717  $pages =& $this->getSurveyPages();
1718  $move_questions = array();
1719  $pageindex = -1;
1720  foreach ($pages as $idx => $page)
1721  {
1722  if ($page[0]["questionblock_id"] == $questionblock_id)
1723  {
1724  foreach ($page as $pageidx => $question)
1725  {
1726  array_push($move_questions, $question["question_id"]);
1727  }
1728  $pageindex = $idx;
1729  }
1730  }
1731  if ($pageindex < count($pages)-1)
1732  {
1733  $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
1734  }
1735  }
1736 
1745  function moveQuestions($move_questions, $target_index, $insert_mode)
1746  {
1747  $array_pos = array_search($target_index, $this->questions);
1748  if ($insert_mode == 0)
1749  {
1750  $part1 = array_slice($this->questions, 0, $array_pos);
1751  $part2 = array_slice($this->questions, $array_pos);
1752  }
1753  else if ($insert_mode == 1)
1754  {
1755  $part1 = array_slice($this->questions, 0, $array_pos + 1);
1756  $part2 = array_slice($this->questions, $array_pos + 1);
1757  }
1758  foreach ($move_questions as $question_id)
1759  {
1760  if (!(array_search($question_id, $part1) === FALSE))
1761  {
1762  unset($part1[array_search($question_id, $part1)]);
1763  }
1764  if (!(array_search($question_id, $part2) === FALSE))
1765  {
1766  unset($part2[array_search($question_id, $part2)]);
1767  }
1768  }
1769  $part1 = array_values($part1);
1770  $part2 = array_values($part2);
1771  $this->questions = array_values(array_merge($part1, $move_questions, $part2));
1772  foreach ($move_questions as $question_id)
1773  {
1774  $constraints = $this->getConstraints($question_id);
1775  foreach ($constraints as $idx => $constraint)
1776  {
1777  foreach ($part2 as $next_question_id)
1778  {
1779  if ($constraint["question"] == $next_question_id)
1780  {
1781  // constraint concerning a question that follows -> delete constraint
1782  $this->deleteConstraint($constraint["id"], $question_id);
1783  }
1784  }
1785  }
1786  }
1787  $this->saveQuestionsToDb();
1788  }
1789 
1796  function removeQuestion($question_id)
1797  {
1798  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
1799  $question =& $this->_instanciateQuestion($question_id);
1800  $question->delete($question_id);
1801  $this->removeConstraintsConcerningQuestion($question_id);
1802  }
1803 
1811  {
1812  global $ilDB;
1813  $result = $ilDB->queryF("SELECT constraint_fi FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
1814  array('integer','integer'),
1815  array($question_id, $this->getSurveyId())
1816  );
1817  if ($result->numRows() > 0)
1818  {
1819  $remove_constraints = array();
1820  while ($row = $ilDB->fetchAssoc($result))
1821  {
1822  array_push($remove_constraints, $row["constraint_fi"]);
1823  }
1824  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
1825  array('integer','integer'),
1826  array($question_id, $this->getSurveyId())
1827  );
1828  foreach ($remove_constraints as $key => $constraint_id)
1829  {
1830  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_constraint WHERE constraint_id = %s",
1831  array('integer'),
1832  array($constraint_id)
1833  );
1834  }
1835  }
1836  }
1837 
1845  function removeQuestions($remove_questions, $remove_questionblocks)
1846  {
1847  global $ilDB;
1848  $questions =& $this->getSurveyQuestions();
1849  foreach ($questions as $question_id => $data)
1850  {
1851  if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
1852  {
1853  unset($this->questions[array_search($question_id, $this->questions)]);
1854  $this->removeQuestion($question_id);
1855  }
1856  }
1857  foreach ($remove_questionblocks as $questionblock_id)
1858  {
1859  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk WHERE questionblock_id = %s",
1860  array('integer'),
1861  array($questionblock_id)
1862  );
1863  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE questionblock_fi = %s AND survey_fi = %s",
1864  array('integer','integer'),
1865  array($questionblock_id, $this->getSurveyId())
1866  );
1867  }
1868  $this->questions = array_values($this->questions);
1869  $this->saveQuestionsToDb();
1870  }
1871 
1878  function unfoldQuestionblocks($questionblocks)
1879  {
1880  global $ilDB;
1881  foreach ($questionblocks as $index)
1882  {
1883  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk WHERE questionblock_id = %s",
1884  array('integer'),
1885  array($index)
1886  );
1887  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE questionblock_fi = %s AND survey_fi = %s",
1888  array('integer','integer'),
1889  array($index, $this->getSurveyId())
1890  );
1891  }
1892  }
1893 
1900  function &getQuestionblockQuestions($questionblock_id)
1901  {
1902  global $ilDB;
1903  $titles = array();
1904  $result = $ilDB->queryF("SELECT svy_question.title, svy_qblk_qst.question_fi, svy_qblk_qst.survey_fi FROM ".
1905  "svy_qblk, svy_qblk_qst, svy_question WHERE svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND " .
1906  "svy_question.question_id = svy_qblk_qst.question_fi AND svy_qblk.questionblock_id = %s",
1907  array('integer'),
1908  array($questionblock_id)
1909  );
1910  $survey_id = "";
1911  while ($row = $ilDB->fetchAssoc($result))
1912  {
1913  $titles[$row["question_fi"]] = $row["title"];
1914  $survey_id = $row["survey_fi"];
1915  }
1916  $result = $ilDB->queryF("SELECT question_fi, sequence FROM svy_svy_qst WHERE survey_fi = %s ORDER BY sequence",
1917  array('integer'),
1918  array($survey_id)
1919  );
1920  $resultarray = array();
1921  $counter = 1;
1922  while ($row = $ilDB->fetchAssoc($result))
1923  {
1924  if (array_key_exists($row["question_fi"], $titles))
1925  {
1926  $resultarray[$counter++] = $titles[$row["question_fi"]];
1927  }
1928  }
1929  return $resultarray;
1930  }
1931 
1938  function &getQuestionblockQuestionIds($questionblock_id)
1939  {
1940  global $ilDB;
1941  $result = $ilDB->queryF("SELECT question_fi FROM svy_qblk_qst WHERE questionblock_fi = %s",
1942  array("integer"),
1943  array($questionblock_id)
1944  );
1945  $ids = array();
1946  if ($result->numRows())
1947  {
1948  while ($data = $ilDB->fetchAssoc($result))
1949  {
1950  array_push($ids, $data['question_fi']);
1951  }
1952  }
1953  return $ids;
1954  }
1955 
1963  function getQuestionblock($questionblock_id)
1964  {
1965  global $ilDB;
1966  $result = $ilDB->queryF("SELECT * FROM svy_qblk WHERE questionblock_id = %s",
1967  array('integer'),
1968  array($questionblock_id)
1969  );
1970  return $ilDB->fetchAssoc($result);
1971  }
1972 
1980  function _getQuestionblock($questionblock_id)
1981  {
1982  global $ilDB;
1983  $result = $ilDB->queryF("SELECT * FROM svy_qblk WHERE questionblock_id = %s",
1984  array('integer'),
1985  array($questionblock_id)
1986  );
1987  $row = $ilDB->fetchAssoc($result);
1988  return $row;
1989  }
1990 
1999  function _addQuestionblock($title = "", $owner = 0)
2000  {
2001  global $ilDB;
2002  $next_id = $ilDB->nextId('svy_qblk');
2003  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk (questionblock_id, title, owner_fi, tstamp) " .
2004  "VALUES (%s, %s, %s, %s)",
2005  array('integer','text','integer','integer'),
2006  array($next_id, $title, $owner, time())
2007  );
2008  return $next_id;
2009  }
2010 
2018  function createQuestionblock($title, $show_questiontext, $questions)
2019  {
2020  global $ilDB;
2021  // if the selected questions are not in a continous selection, move all questions of the
2022  // questionblock at the position of the first selected question
2023  $this->moveQuestions($questions, $questions[0], 0);
2024 
2025  // now save the question block
2026  global $ilUser;
2027  $next_id = $ilDB->nextId('svy_qblk');
2028  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk (questionblock_id, title, show_questiontext, owner_fi, ".
2029  "tstamp) VALUES (%s, %s, %s, %s, %s)",
2030  array('integer','text','text','integer','integer'),
2031  array($next_id, $title, $show_questiontext, $ilUser->getId(), time())
2032  );
2033  if ($affectedRows)
2034  {
2035  $questionblock_id = $next_id;
2036  foreach ($questions as $index)
2037  {
2038  $next_id = $ilDB->nextId('svy_qblk_qst');
2039  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk_qst (qblk_qst_id, survey_fi, questionblock_fi, " .
2040  "question_fi) VALUES (%s, %s, %s, %s)",
2041  array('integer','integer','integer','integer'),
2042  array($next_id, $this->getSurveyId(), $questionblock_id, $index)
2043  );
2044  $this->deleteConstraints($index);
2045  }
2046  }
2047  }
2048 
2056  function modifyQuestionblock($questionblock_id, $title, $show_questiontext)
2057  {
2058  global $ilDB;
2059  $affectedRows = $ilDB->manipulateF("UPDATE svy_qblk SET title = %s, show_questiontext = %s WHERE " .
2060  "questionblock_id = %s",
2061  array('text','text','integer'),
2062  array($title, $show_questiontext, $questionblock_id)
2063  );
2064  }
2065 
2072  function deleteConstraints($question_id)
2073  {
2074  global $ilDB;
2075  $result = $ilDB->queryF("SELECT * FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2076  array('integer','integer'),
2077  array($question_id, $this->getSurveyId())
2078  );
2079  while ($row = $ilDB->fetchAssoc($result))
2080  {
2081  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_constraint WHERE constraint_id = %s",
2082  array('integer'),
2083  array($row["constraint_fi"])
2084  );
2085  }
2086  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2087  array('integer','integer'),
2088  array($question_id, $this->getSurveyId())
2089  );
2090  }
2091 
2099  function deleteConstraint($constraint_id, $question_id)
2100  {
2101  global $ilDB;
2102  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_constraint WHERE constraint_id = %s",
2103  array('integer'),
2104  array($constraint_id)
2105  );
2106  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_constraint WHERE constraint_fi = %s " .
2107  "AND question_fi = %s AND survey_fi = %s",
2108  array('integer', 'integer', 'integer'),
2109  array($constraint_id, $question_id, $this->getSurveyId())
2110  );
2111  }
2112 
2118  public function &getSurveyQuestions($with_answers = false)
2119  {
2120  global $ilDB;
2121  $obligatory_states =& $this->getObligatoryStates();
2122  // get questionblocks
2123  $all_questions = array();
2124  $result = $ilDB->queryF("SELECT svy_qtype.type_tag, svy_qtype.plugin, svy_question.question_id, ".
2125  "svy_svy_qst.heading FROM svy_qtype, svy_question, svy_svy_qst WHERE svy_svy_qst.survey_fi = %s AND " .
2126  "svy_svy_qst.question_fi = svy_question.question_id AND svy_question.questiontype_fi = svy_qtype.questiontype_id " .
2127  "ORDER BY svy_svy_qst.sequence",
2128  array('integer'),
2129  array($this->getSurveyId())
2130  );
2131  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2132  while ($row = $ilDB->fetchAssoc($result))
2133  {
2134  $add = true;
2135  if ($row["plugin"])
2136  {
2137  if (!$this->isPluginActive($row["type_tag"]))
2138  {
2139  $add = false;
2140  }
2141  }
2142  if ($add)
2143  {
2144  $question =& $this->_instanciateQuestion($row["question_id"]);
2145  $questionrow = $question->_getQuestionDataArray($row["question_id"]);
2146  foreach ($row as $key => $value)
2147  {
2148  $questionrow[$key] = $value;
2149  }
2150  $all_questions[$row["question_id"]] = $questionrow;
2151  $all_questions[$row["question_id"]]["usableForPrecondition"] = $question->usableForPrecondition();
2152  $all_questions[$row["question_id"]]["availableRelations"] = $question->getAvailableRelations();
2153  if (array_key_exists($row["question_id"], $obligatory_states))
2154  {
2155  $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
2156  }
2157  }
2158  }
2159  // get all questionblocks
2160  $questionblocks = array();
2161  if (count($all_questions))
2162  {
2163  $result = $ilDB->queryF("SELECT svy_qblk.*, svy_qblk_qst.question_fi FROM svy_qblk, svy_qblk_qst WHERE " .
2164  "svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_qblk_qst.survey_fi = %s " .
2165  "AND " . $ilDB->in('svy_qblk_qst.question_fi', array_keys($all_questions), false, 'integer'),
2166  array('integer'),
2167  array($this->getSurveyId())
2168  );
2169  while ($row = $ilDB->fetchAssoc($result))
2170  {
2171  $questionblocks[$row['question_fi']] = $row;
2172  }
2173  }
2174 
2175  foreach ($all_questions as $question_id => $row)
2176  {
2177  $constraints = $this->getConstraints($question_id);
2178  if (isset($questionblocks[$question_id]))
2179  {
2180  $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]['title'];
2181  $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]['questionblock_id'];
2182  $all_questions[$question_id]["constraints"] = $constraints;
2183  }
2184  else
2185  {
2186  $all_questions[$question_id]["questionblock_title"] = "";
2187  $all_questions[$question_id]["questionblock_id"] = "";
2188  $all_questions[$question_id]["constraints"] = $constraints;
2189  }
2190  if ($with_answers)
2191  {
2192  $answers = array();
2193  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title FROM svy_variable, svy_category " .
2194  "WHERE svy_variable.question_fi = %s AND svy_variable.category_fi = svy_category.category_id ".
2195  "ORDER BY sequence ASC",
2196  array('integer'),
2197  array($question_id)
2198  );
2199  if ($result->numRows() > 0)
2200  {
2201  while ($data = $ilDB->fetchAssoc($result))
2202  {
2203  array_push($answers, $data["title"]);
2204  }
2205  }
2206  $all_questions[$question_id]["answers"] = $answers;
2207  }
2208  }
2209  return $all_questions;
2210  }
2211 
2218  function setObligatoryStates($obligatory_questions)
2219  {
2220  global $ilDB;
2221  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s",
2222  array('integer'),
2223  array($this->getSurveyId())
2224  );
2225  if ($result->numRows())
2226  {
2227  while ($row = $ilDB->fetchAssoc($result))
2228  {
2229  if (!array_key_exists($row["question_fi"], $obligatory_questions))
2230  {
2231  $obligatory_questions[$row["question_fi"]] = 0;
2232  }
2233  }
2234  }
2235 
2236  // set the obligatory states in the database
2237  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_oblig WHERE survey_fi = %s",
2238  array('integer'),
2239  array($this->getSurveyId())
2240  );
2241 
2242  // set the obligatory states in the database
2243  foreach ($obligatory_questions as $question_fi => $obligatory)
2244  {
2245  $next_id = $ilDB->nextId('svy_qst_oblig');
2246  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_oblig (question_obligatory_id, survey_fi, question_fi, " .
2247  "obligatory, tstamp) VALUES (%s, %s, %s, %s, %s)",
2248  array('integer','integer','integer','text','integer'),
2249  array($next_id, $this->getSurveyId(), $question_fi, (strlen($obligatory)) ? $obligatory : 0, time())
2250  );
2251  }
2252  }
2253 
2261  {
2262  global $ilDB;
2263  $obligatory_states = array();
2264  $result = $ilDB->queryF("SELECT * FROM svy_qst_oblig WHERE survey_fi = %s",
2265  array('integer'),
2266  array($this->getSurveyId())
2267  );
2268  if ($result->numRows())
2269  {
2270  while ($row = $ilDB->fetchAssoc($result))
2271  {
2272  $obligatory_states[$row["question_fi"]] = $row["obligatory"];
2273  }
2274  }
2275  return $obligatory_states;
2276  }
2277 
2283  function &getSurveyPages()
2284  {
2285  global $ilDB;
2286  $obligatory_states =& $this->getObligatoryStates();
2287  // get questionblocks
2288  $all_questions = array();
2289  $result = $ilDB->queryF("SELECT svy_question.*, svy_qtype.type_tag, svy_svy_qst.heading FROM " .
2290  "svy_question, svy_qtype, svy_svy_qst WHERE svy_svy_qst.survey_fi = %s AND " .
2291  "svy_svy_qst.question_fi = svy_question.question_id AND svy_question.questiontype_fi = svy_qtype.questiontype_id ".
2292  "ORDER BY svy_svy_qst.sequence",
2293  array('integer'),
2294  array($this->getSurveyId())
2295  );
2296  while ($row = $ilDB->fetchAssoc($result))
2297  {
2298  $all_questions[$row["question_id"]] = $row;
2299  }
2300  // get all questionblocks
2301  $questionblocks = array();
2302  if (count($all_questions))
2303  {
2304  $result = $ilDB->queryF("SELECT svy_qblk.*, svy_qblk_qst.question_fi FROM svy_qblk, svy_qblk_qst ".
2305  "WHERE svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_qblk_qst.survey_fi = %s ".
2306  "AND " . $ilDB->in('svy_qblk_qst.question_fi', array_keys($all_questions), false, 'integer'),
2307  array('integer'),
2308  array($this->getSurveyId())
2309  );
2310  while ($row = $ilDB->fetchAssoc($result))
2311  {
2312  $questionblocks[$row['question_fi']] = $row;
2313  }
2314  }
2315 
2316  $all_pages = array();
2317  $pageindex = -1;
2318  $currentblock = "";
2319  foreach ($all_questions as $question_id => $row)
2320  {
2321  if (array_key_exists($question_id, $obligatory_states))
2322  {
2323  $all_questions[$question_id]["obligatory"] = $obligatory_states[$question_id];
2324  }
2325  $constraints = array();
2326  if (isset($questionblocks[$question_id]))
2327  {
2328  if (!$currentblock or ($currentblock != $questionblocks[$question_id]['questionblock_id']))
2329  {
2330  $pageindex++;
2331  }
2332  $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]['title'];
2333  $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]['questionblock_id'];
2334  $all_questions[$question_id]["questionblock_show_questiontext"] = $questionblocks[$question_id]['show_questiontext'];
2335  $currentblock = $questionblocks[$question_id]['questionblock_id'];
2336  $constraints = $this->getConstraints($question_id);
2337  $all_questions[$question_id]["constraints"] = $constraints;
2338  }
2339  else
2340  {
2341  $pageindex++;
2342  $all_questions[$question_id]["questionblock_title"] = "";
2343  $all_questions[$question_id]["questionblock_id"] = "";
2344  $all_questions[$question_id]["questionblock_show_questiontext"] = 1;
2345  $currentblock = "";
2346  $constraints = $this->getConstraints($question_id);
2347  $all_questions[$question_id]["constraints"] = $constraints;
2348  }
2349  if (!isset($all_pages[$pageindex]))
2350  {
2351  $all_pages[$pageindex] = array();
2352  }
2353  array_push($all_pages[$pageindex], $all_questions[$question_id]);
2354  }
2355  // calculate position percentage for every page
2356  $max = count($all_pages);
2357  $counter = 1;
2358  foreach ($all_pages as $index => $block)
2359  {
2360  foreach ($block as $blockindex => $question)
2361  {
2362  $all_pages[$index][$blockindex]["position"] = $counter / $max;
2363  }
2364  $counter++;
2365  }
2366  return $all_pages;
2367  }
2368 
2377  function getNextPage($active_page_question_id, $direction)
2378  {
2379  $foundpage = -1;
2380  $pages =& $this->getSurveyPages();
2381  if (strcmp($active_page_question_id, "") == 0)
2382  {
2383  return $pages[0];
2384  }
2385 
2386  foreach ($pages as $key => $question_array)
2387  {
2388  foreach ($question_array as $question)
2389  {
2390  if ($active_page_question_id == $question["question_id"])
2391  {
2392  $foundpage = $key;
2393  }
2394  }
2395  }
2396  if ($foundpage == -1)
2397  {
2398  // error: page not found
2399  }
2400  else
2401  {
2402  $foundpage += $direction;
2403  if ($foundpage < 0)
2404  {
2405  return 0;
2406  }
2407  if ($foundpage >= count($pages))
2408  {
2409  return 1;
2410  }
2411  return $pages[$foundpage];
2412  }
2413  }
2414 
2421  function &getAvailableQuestionpools($use_obj_id = false, $could_be_offline = false, $showPath = FALSE, $permission = "read")
2422  {
2423  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
2424  return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_obj_id, $could_be_offline, $showPath, $permission);
2425  }
2426 
2433  {
2434  global $ilDB;
2435 
2436  $result_array = array();
2437  $result = $ilDB->queryF("SELECT svy_constraint.*, svy_relation.* FROM svy_qst_constraint, svy_constraint, ".
2438  "svy_relation WHERE svy_constraint.relation_fi = svy_relation.relation_id AND ".
2439  "svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_constraint.constraint_id = %s",
2440  array('integer'),
2441  array($id)
2442  );
2443  $pc = array();
2444  if ($result->numRows())
2445  {
2446  $pc = $ilDB->fetchAssoc($result);
2447  }
2448  return $pc;
2449  }
2450 
2456  function getConstraints($question_id)
2457  {
2458  global $ilDB;
2459 
2460  $result_array = array();
2461  $result = $ilDB->queryF("SELECT svy_constraint.*, svy_relation.* FROM svy_qst_constraint, svy_constraint, svy_relation ".
2462  "WHERE svy_constraint.relation_fi = svy_relation.relation_id AND ".
2463  "svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_qst_constraint.question_fi = %s ".
2464  "AND svy_qst_constraint.survey_fi = %s",
2465  array('integer','integer'),
2466  array($question_id, $this->getSurveyId())
2467  );
2468  while ($row = $ilDB->fetchAssoc($result))
2469  {
2470  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2471  $question_type = SurveyQuestion::_getQuestionType($row["question_fi"]);
2472  SurveyQuestion::_includeClass($question_type);
2473  $question = new $question_type();
2474  $question->loadFromDb($row["question_fi"]);
2475  $valueoutput = $question->getPreconditionValueOutput($row["value"]);
2476  array_push($result_array, array("id" => $row["constraint_id"], "question" => $row["question_fi"], "short" => $row["shortname"], "long" => $row["longname"], "value" => $row["value"], "valueoutput" => $valueoutput));
2477  }
2478  return $result_array;
2479  }
2480 
2487  {
2488  global $ilDB;
2489  $result_array = array();
2490  $result = $ilDB->queryF("SELECT svy_qst_constraint.question_fi as for_question, svy_constraint.*, svy_relation.* ".
2491  "FROM svy_qst_constraint, svy_constraint, svy_relation WHERE svy_constraint.relation_fi = svy_relation.relation_id ".
2492  "AND svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_qst_constraint.survey_fi = %s",
2493  array('integer'),
2494  array($survey_id)
2495  );
2496  while ($row = $ilDB->fetchAssoc($result))
2497  {
2498  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"]));
2499  }
2500  return $result_array;
2501  }
2502 
2503 
2509  function &getVariables($question_id)
2510  {
2511  global $ilDB;
2512 
2513  $result_array = array();
2514  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title FROM svy_variable LEFT JOIN ".
2515  "svy_category ON svy_variable.category_fi = svy_category.category_id WHERE svy_variable.question_fi = %s ".
2516  "ORDER BY svy_variable.sequence",
2517  array('integer'),
2518  array($question_id)
2519  );
2520  while ($row = $ilDB->fetchObject($result))
2521  {
2522  $result_array[$row->sequence] = $row;
2523  }
2524  return $result_array;
2525  }
2526 
2536  function addConstraint($to_question_id, $if_question_id, $relation, $value)
2537  {
2538  global $ilDB;
2539 
2540  $next_id = $ilDB->nextId('svy_constraint');
2541  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_constraint (constraint_id, question_fi, relation_fi, value) VALUES ".
2542  "(%s, %s, %s, %s)",
2543  array('integer','integer','integer','float'),
2544  array($next_id, $if_question_id, $relation, $value)
2545  );
2546  if ($affectedRows)
2547  {
2548  $constraint_id = $next_id;
2549  $next_id = $ilDB->nextId('svy_qst_constraint');
2550  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_constraint (question_constraint_id, survey_fi, question_fi, ".
2551  "constraint_fi) VALUES (%s, %s, %s, %s)",
2552  array('integer','integer','integer','integer'),
2553  array($next_id, $this->getSurveyId(), $to_question_id, $constraint_id)
2554  );
2555  }
2556  }
2557 
2568  function updateConstraint($to_question_id, $if_question_id, $relation, $value)
2569  {
2570  global $ilDB;
2571  $result = $ilDB->queryF("SELECT constraint_fi FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2572  array('integer','integer'),
2573  array($to_question_id, $this->getSurveyId())
2574  );
2575  if ($result->numRows())
2576  {
2577  $row = $ilDB->fetchAssoc($result);
2578  $affectedRows = $ilDB->manipulateF("UPDATE svy_constraint SET question_fi = %s, relation_fi = %s, value = %s ".
2579  "WHERE constraint_id = %s",
2580  array('integer','integer','float','integer'),
2581  array($if_question_id, $relation, $value, $row["constraint_fi"])
2582  );
2583  }
2584  }
2585 
2591  function getAllRelations($short_as_key = false)
2592  {
2593  global $ilDB;
2594 
2595  $result_array = array();
2596  $result = $ilDB->query("SELECT * FROM svy_relation");
2597  while ($row = $ilDB->fetchAssoc($result))
2598  {
2599  if ($short_as_key)
2600  {
2601  $result_array[$row["shortname"]] = array("short" => $row["shortname"], "long" => $row["longname"], "id" => $row["relation_id"]);
2602  }
2603  else
2604  {
2605  $result_array[$row["relation_id"]] = array("short" => $row["shortname"], "long" => $row["longname"]);
2606  }
2607  }
2608  return $result_array;
2609  }
2610 
2614  public function disinviteAllUsers()
2615  {
2616  global $ilDB;
2617  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE survey_fi = %s",
2618  array('integer'),
2619  array($this->getSurveyId())
2620  );
2621  while ($row = $ilDB->fetchAssoc($result))
2622  {
2623  $this->disinviteUser($row['user_fi']);
2624  }
2625  }
2626 
2632  public function disinviteUser($user_id)
2633  {
2634  global $ilDB;
2635 
2636  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_inv_usr WHERE survey_fi = %s AND user_fi = %s",
2637  array('integer','integer'),
2638  array($this->getSurveyId(), $user_id)
2639  );
2640  include_once './Services/User/classes/class.ilObjUser.php';
2641  ilObjUser::_dropDesktopItem($user_id, $this->getRefId(), "svy");
2642  }
2643 
2650  function inviteUser($user_id)
2651  {
2652  global $ilDB;
2653 
2654  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE user_fi = %s AND survey_fi = %s",
2655  array('integer','integer'),
2656  array($user_id, $this->getSurveyId())
2657  );
2658  if ($result->numRows() < 1)
2659  {
2660  $next_id = $ilDB->nextId('svy_inv_usr');
2661  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_inv_usr (invited_user_id, survey_fi, user_fi, tstamp) " .
2662  "VALUES (%s, %s, %s, %s)",
2663  array('integer','integer','integer','integer'),
2664  array($next_id, $this->getSurveyId(), $user_id, time())
2665  );
2666  }
2667  if ($this->getInvitation() == INVITATION_ON)
2668  {
2669  include_once './Services/User/classes/class.ilObjUser.php';
2670  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
2671  }
2672  }
2673 
2680  function inviteGroup($group_id)
2681  {
2682  global $ilAccess;
2683  $invited = 0;
2684  include_once "./Modules/Group/classes/class.ilObjGroup.php";
2685  $group = new ilObjGroup($group_id);
2686  $members = $group->getGroupMemberIds();
2687  foreach ($members as $user_id)
2688  {
2689  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
2690  {
2691  $this->inviteUser($user_id);
2692  if ($this->getInvitation() == INVITATION_ON)
2693  {
2694  include_once './Services/User/classes/class.ilObjUser.php';
2695  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
2696  }
2697  }
2698  }
2699  return $invited;
2700  }
2701 
2708  function inviteRole($role_id)
2709  {
2710  global $rbacreview;
2711  global $ilAccess;
2712  $invited = 0;
2713  $members = $rbacreview->assignedUsers($role_id);
2714  foreach ($members as $user_id)
2715  {
2716  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
2717  {
2718  $this->inviteUser($user_id);
2719  if ($this->getInvitation() == INVITATION_ON)
2720  {
2721  include_once './Services/User/classes/class.ilObjUser.php';
2722  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
2723  }
2724  }
2725  }
2726  return $invited;
2727  }
2728 
2735  function &getInvitedUsers()
2736  {
2737  global $ilDB;
2738 
2739  $result_array = array();
2740  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE survey_fi = %s",
2741  array('integer'),
2742  array($this->getSurveyId())
2743  );
2744  while ($row = $ilDB->fetchAssoc($result))
2745  {
2746  array_push($result_array, $row["user_fi"]);
2747  }
2748  return $result_array;
2749  }
2750 
2758  function deleteWorkingData($question_id, $active_id)
2759  {
2760  global $ilDB;
2761 
2762  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE question_fi = %s AND active_fi = %s",
2763  array('integer','integer'),
2764  array($question_id, $active_id)
2765  );
2766  }
2767 
2776  function loadWorkingData($question_id, $active_id)
2777  {
2778  global $ilDB;
2779  $result_array = array();
2780  $result = $ilDB->queryF("SELECT * FROM svy_answer WHERE question_fi = %s AND active_fi = %s",
2781  array('integer','integer'),
2782  array($question_id, $active_id)
2783  );
2784  if ($result->numRows() >= 1)
2785  {
2786  while ($row = $ilDB->fetchAssoc($result))
2787  {
2788  array_push($result_array, $row);
2789  }
2790  return $result_array;
2791  }
2792  else
2793  {
2794  return $result_array;
2795  }
2796  }
2797 
2804  function fillSurveyForUser($user_id = ANONYMOUS_USER_ID)
2805  {
2806  global $ilDB;
2807  // create an anonymous key
2808  $anonymous_id = $this->createNewAccessCode();
2809  $this->saveUserAccessCode($user_id, $anonymous_id);
2810  // create the survey_finished dataset and set the survey finished already
2811  $active_id = $ilDB->nextId('svy_finished');
2812  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp) ".
2813  "VALUES (%s, %s, %s, %s, %s, %s)",
2814  array('integer','integer','integer','text','text','integer'),
2815  array($active_id, $this->getSurveyId(), $user_id, $anonymous_id, 1, time())
2816  );
2817  // fill the questions randomly
2818  $pages =& $this->getSurveyPages();
2819  foreach ($pages as $key => $question_array)
2820  {
2821  foreach ($question_array as $question)
2822  {
2823  // instanciate question
2824  require_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2825  $question =& SurveyQuestion::_instanciateQuestion($question["question_id"]);
2826  $question->saveRandomData($active_id);
2827  }
2828  }
2829  }
2830 
2837  function startSurvey($user_id, $anonymous_id)
2838  {
2839  global $ilUser;
2840  global $ilDB;
2841 
2842  if ($this->getAnonymize() && (strlen($anonymous_id) == 0)) return;
2843 
2844  if (strcmp($user_id, "") == 0)
2845  {
2846  if ($user_id == ANONYMOUS_USER_ID)
2847  {
2848  $user_id = 0;
2849  }
2850  }
2851  $next_id = $ilDB->nextId('svy_finished');
2852  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp) ".
2853  "VALUES (%s, %s, %s, %s, %s, %s)",
2854  array('integer','integer','integer','text','text','integer'),
2855  array($next_id, $this->getSurveyId(), $user_id, $anonymous_id, 0, time())
2856  );
2857  return $next_id;
2858  }
2859 
2866  function finishSurvey($user_id, $anonymize_id)
2867  {
2868  global $ilDB;
2869 
2870  if ($this->getAnonymize())
2871  {
2872  $affectedRows = $ilDB->manipulateF("UPDATE svy_finished SET state = %s, user_fi = %s, tstamp = %s ".
2873  "WHERE survey_fi = %s AND anonymous_id = %s",
2874  array('text','integer','integer','integer','text'),
2875  array(1, $user_id, time(), $this->getSurveyId(), $anonymize_id)
2876  );
2877  }
2878  else
2879  {
2880  $affectedRows = $ilDB->manipulateF("UPDATE svy_finished SET state = %s, tstamp = %s WHERE survey_fi = %s AND user_fi = %s",
2881  array('text','integer','integer','integer'),
2882  array(1, time(), $this->getSurveyId(), $user_id)
2883  );
2884  }
2885  }
2886 
2894  function isAllowedToTakeMultipleSurveys($userid = "")
2895  {
2896  $result = FALSE;
2897  if ($this->getAnonymize())
2898  {
2899  if ($this->isAccessibleWithoutCode())
2900  {
2901  if (strlen($username) == 0)
2902  {
2903  global $ilUser;
2904  $userid = $ilUser->getId();
2905  }
2906  global $ilSetting;
2907  $surveysetting = new ilSetting("survey");
2908  $allowedUsers = strlen($surveysetting->get("multiple_survey_users")) ? explode(",",$surveysetting->get("multiple_survey_users")) : array();
2909  if (in_array($userid, $allowedUsers))
2910  {
2911  $result = TRUE;
2912  }
2913  }
2914  }
2915  return $result;
2916  }
2917 
2925  function isSurveyStarted($user_id, $anonymize_id)
2926  {
2927  global $ilDB;
2928 
2929  if ($this->getAnonymize())
2930  {
2931  if ((($user_id != ANONYMOUS_USER_ID) && (strlen($anonymize_id) == 0)) && (!($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys())))
2932  {
2933  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
2934  array('integer','integer'),
2935  array($this->getSurveyId(), $user_id)
2936  );
2937  }
2938  else
2939  {
2940  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND anonymous_id = %s",
2941  array('integer','text'),
2942  array($this->getSurveyId(), $anonymize_id)
2943  );
2944  }
2945  }
2946  else
2947  {
2948  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
2949  array('integer','integer'),
2950  array($this->getSurveyId(), $user_id)
2951  );
2952  }
2953  if ($result->numRows() == 0)
2954  {
2955  return false;
2956  }
2957  else
2958  {
2959  $row = $ilDB->fetchAssoc($result);
2960  $_SESSION["finished_id"] = $row["finished_id"];
2961  return (int)$row["state"];
2962  }
2963  }
2964 
2972  function getLastActivePage($active_id)
2973  {
2974  global $ilDB;
2975  $result = $ilDB->queryF("SELECT question_fi, tstamp FROM svy_answer WHERE active_fi = %s ORDER BY tstamp DESC",
2976  array('integer'),
2977  array($active_id)
2978  );
2979  if ($result->numRows() == 0)
2980  {
2981  return "";
2982  }
2983  else
2984  {
2985  $row = $ilDB->fetchAssoc($result);
2986  return $row["question_fi"];
2987  }
2988  }
2989 
2998  function checkConstraint($constraint_data, $working_data)
2999  {
3000  if (count($working_data) == 0)
3001  {
3002  return 0;
3003  }
3004 
3005  if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
3006  {
3007  return 0;
3008  }
3009 
3010  foreach ($working_data as $data)
3011  {
3012  switch ($constraint_data["short"])
3013  {
3014  case "<":
3015  if ($data["value"] < $constraint_data["value"])
3016  {
3017  return 1;
3018  }
3019  break;
3020  case "<=":
3021  if ($data["value"] <= $constraint_data["value"])
3022  {
3023  return 1;
3024  }
3025  break;
3026  case "=":
3027  if ($data["value"] == $constraint_data["value"])
3028  {
3029  return 1;
3030  }
3031  break;
3032  case "<>":
3033  if ($data["value"] != $constraint_data["value"])
3034  {
3035  return 1;
3036  }
3037  break;
3038  case ">=":
3039  if ($data["value"] >= $constraint_data["value"])
3040  {
3041  return 1;
3042  }
3043  break;
3044  case ">":
3045  if ($data["value"] > $constraint_data["value"])
3046  {
3047  return 1;
3048  }
3049  break;
3050  }
3051  }
3052  return 0;
3053  }
3054 
3056  {
3057  global $ilDB;
3058 
3059  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3060  array('integer'),
3061  array($survey_id)
3062  );
3063  return ($result->numRows()) ? true : false;
3064  }
3065 
3073  {
3074  global $ilDB, $ilLog;
3075 
3076  $users = array();
3077  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
3078  array('integer'),
3079  array($this->getSurveyId())
3080  );
3081  if ($result->numRows())
3082  {
3083  while ($row = $ilDB->fetchAssoc($result))
3084  {
3085  array_push($users, $row["finished_id"]);
3086  }
3087  }
3088  return $users;
3089  }
3090 
3098  {
3099  global $ilDB;
3100 
3101  $users = array();
3102  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
3103  array('integer'),
3104  array($this->getSurveyId())
3105  );
3106  if ($result->numRows())
3107  {
3108  while ($row = $ilDB->fetchAssoc($result))
3109  {
3110  array_push($users, $row);
3111  }
3112  }
3113  $evaluation = array();
3114  $questions =& $this->getSurveyQuestions();
3115  foreach ($questions as $question_id => $question_data)
3116  {
3117  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3118  $question_type = SurveyQuestion::_getQuestionType($question_id);
3119  SurveyQuestion::_includeClass($question_type);
3120  $question = new $question_type();
3121  $question->loadFromDb($question_id);
3122  $data =& $question->getUserAnswers($this->getSurveyId());
3123  $evaluation[$question_id] = $data;
3124  }
3125  return $evaluation;
3126  }
3127 
3135  function getUserDataFromActiveId($active_id)
3136  {
3137  global $ilDB;
3138 
3139  $surveySetting = new ilSetting("survey");
3140  $use_anonymous_id = array_key_exists("use_anonymous_id", $_GET) ? $_GET["use_anonymous_id"] : $surveySetting->get("use_anonymous_id");
3141  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE finished_id = %s",
3142  array('integer'),
3143  array($active_id)
3144  );
3145  $row = array();
3146  $foundrows = $result->numRows();
3147  if ($foundrows)
3148  {
3149  $row = $ilDB->fetchAssoc($result);
3150  }
3151  $name = ($use_anonymous_id) ? $row["anonymous_id"] : $this->lng->txt("anonymous");
3152  $userdata = array(
3153  "fullname" => $name,
3154  "sortname" => $name,
3155  "firstname" => "",
3156  "lastname" => "",
3157  "login" => "",
3158  "gender" => "",
3159  "active_id" => "$active_id"
3160  );
3161  if ($foundrows)
3162  {
3163  if (($row["user_fi"] > 0) && ($row["user_fi"] != ANONYMOUS_USER_ID) && ($this->getAnonymize() == 0))
3164  {
3165  include_once './Services/User/classes/class.ilObjUser.php';
3166  if (strlen(ilObjUser::_lookupLogin($row["user_fi"])) == 0)
3167  {
3168  $userdata["fullname"] = $this->lng->txt("deleted_user");
3169  }
3170  else
3171  {
3172  $user = new ilObjUser($row["user_fi"]);
3173  $userdata["fullname"] = $user->getFullname();
3174  $gender = $user->getGender();
3175  if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
3176  $userdata["gender"] = $gender;
3177  $userdata["firstname"] = $user->getFirstname();
3178  $userdata["lastname"] = $user->getLastname();
3179  $userdata["sortname"] = $user->getLastname() . ", " . $user->getFirstname();
3180  $userdata["login"] = $user->getLogin();
3181  }
3182  }
3183  }
3184  return $userdata;
3185  }
3186 
3196  function &getEvaluationByUser($questions, $active_id)
3197  {
3198  global $ilDB;
3199 
3200  // collect all answers
3201  $answers = array();
3202  $result = $ilDB->queryF("SELECT * FROM svy_answer WHERE active_fi = %s",
3203  array('integer'),
3204  array($active_id)
3205  );
3206  while ($row = $ilDB->fetchAssoc($result))
3207  {
3208  if (!is_array($answers[$row["question_fi"]]))
3209  {
3210  $answers[$row["question_fi"]] = array();
3211  }
3212  array_push($answers[$row["question_fi"]], $row);
3213  }
3214  $userdata = $this->getUserDataFromActiveId($active_id);
3215  $resultset = array(
3216  "name" => $userdata["fullname"],
3217  "gender" => $userdata["gender"],
3218  "answers" => array()
3219  );
3220  foreach ($questions as $key => $question)
3221  {
3222  if (array_key_exists($key, $answers))
3223  {
3224  $resultset["answers"][$key] = $answers[$key];
3225  }
3226  else
3227  {
3228  $resultset["answers"][$key] = array();
3229  }
3230  sort($resultset["answers"][$key]);
3231  }
3232  return $resultset;
3233  }
3234 
3243  function getCumulatedResults(&$question)
3244  {
3245  global $ilDB;
3246 
3247  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3248  array('integer'),
3249  array($this->getSurveyId())
3250  );
3251  $nr_of_users = $result->numRows();
3252 
3253  $result_array =& $question->getCumulatedResults($this->getSurveyId(), $nr_of_users);
3254  return $result_array;
3255  }
3256 
3265  {
3266  global $ilDB;
3267 
3268  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3269  array('integer'),
3270  array($survey_id)
3271  );
3272  return $result->numRows();
3273  }
3274 
3275  function &getQuestions($question_ids)
3276  {
3277  $result_array = array();
3278  $result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag FROM svy_question, svy_qtype WHERE ".
3279  "svy_question.questiontype_fi = svy_qtype.questiontype_id AND svy_question.tstamp > 0 AND ".
3280  $ilDB->in('svy_question.question_id', $question_ids, false, 'integer'));
3281  while ($row = $ilDB->fetchAssoc($result))
3282  {
3283  array_push($result_array, $row);
3284  }
3285  return $result_array;
3286  }
3287 
3293  function getQuestionsTable($arrFilter)
3294  {
3295  global $ilUser;
3296  global $ilDB;
3297  $where = "";
3298  if (is_array($arrFilter))
3299  {
3300  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title']))
3301  {
3302  $where .= " AND " . $ilDB->like('svy_question.title', 'text', "%%" . $arrFilter['title'] . "%%");
3303  }
3304  if (array_key_exists('description', $arrFilter) && strlen($arrFilter['description']))
3305  {
3306  $where .= " AND " . $ilDB->like('svy_question.description', 'text', "%%" . $arrFilter['description'] . "%%");
3307  }
3308  if (array_key_exists('author', $arrFilter) && strlen($arrFilter['author']))
3309  {
3310  $where .= " AND " . $ilDB->like('svy_question.author', 'text', "%%" . $arrFilter['author'] . "%%");
3311  }
3312  if (array_key_exists('type', $arrFilter) && strlen($arrFilter['type']))
3313  {
3314  $where .= " AND svy_qtype.type_tag = " . $ilDB->quote($arrFilter['type'], 'text');
3315  }
3316  if (array_key_exists('spl', $arrFilter) && strlen($arrFilter['spl']))
3317  {
3318  $where .= " AND svy_question.obj_fi = " . $ilDB->quote($arrFilter['spl'], 'integer');
3319  }
3320  }
3321 
3322  $spls =& $this->getAvailableQuestionpools($use_obj_id = TRUE, $could_be_offline = FALSE, $showPath = FALSE);
3323  $forbidden = "";
3324  $forbidden = " AND " . $ilDB->in('svy_question.obj_fi', array_keys($spls), false, 'integer');
3325  $forbidden .= " AND svy_question.complete = " . $ilDB->quote("1", 'text');
3326  $existing = "";
3327  $existing_questions =& $this->getExistingQuestions();
3328  if (count($existing_questions))
3329  {
3330  $existing = " AND " . $ilDB->in('svy_question.question_id', $existing_questions, true, 'integer');
3331  }
3332 
3333  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
3335 
3336  $query_result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag, svy_qtype.plugin, object_reference.ref_id FROM svy_question, svy_qtype, object_reference WHERE svy_question.original_id IS NULL$forbidden$existing AND svy_question.obj_fi = object_reference.obj_id AND svy_question.tstamp > 0 AND svy_question.questiontype_fi = svy_qtype.questiontype_id " . $where);
3337 
3338  $rows = array();
3339  if ($query_result->numRows())
3340  {
3341  while ($row = $ilDB->fetchAssoc($query_result))
3342  {
3343  $row['ttype'] = $trans[$row['type_tag']];
3344  if ($row["plugin"])
3345  {
3346  if ($this->isPluginActive($row["type_tag"]))
3347  {
3348  array_push($rows, $row);
3349  }
3350  }
3351  else
3352  {
3353  array_push($rows, $row);
3354  }
3355  }
3356  }
3357  return $rows;
3358  }
3359 
3365  function getQuestionblocksTable($arrFilter)
3366  {
3367  global $ilUser;
3368  global $ilDB;
3369  $where = "";
3370  if (is_array($arrFilter))
3371  {
3372  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title']))
3373  {
3374  $where .= " AND " . $ilDB->like('svy_qblk.title', 'text', "%%" . $arrFilter['title'] . "%%");
3375  }
3376  }
3377 
3378  $query_result = $ilDB->query("SELECT svy_qblk.*, svy_svy.obj_fi FROM svy_qblk , svy_qblk_qst, svy_svy WHERE ".
3379  "svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_svy.survey_id = svy_qblk_qst.survey_fi ".
3380  "$where GROUP BY svy_qblk.questionblock_id, svy_qblk.title, svy_qblk.show_questiontext, svy_qblk.owner_fi, svy_qblk.tstamp, svy_svy.obj_fi");
3381  $rows = array();
3382  if ($query_result->numRows())
3383  {
3384  $surveys = ilObject::_getObjectsDataForType('svy', true);
3385  $surveytitles = array();
3386  foreach ($surveys as $data)
3387  {
3388  $surveytitles[$data['id']] = $data['title'];
3389  }
3390  while ($row = $ilDB->fetchAssoc($query_result))
3391  {
3392  $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
3393  $counter = 1;
3394  foreach ($questions_array as $key => $value)
3395  {
3396  $questions_array[$key] = "$counter. $value";
3397  $counter++;
3398  }
3399  if (strlen($surveytitles[$row["obj_fi"]])) // only questionpools which are not in trash
3400  {
3401  $rows[$row["questionblock_id"]] = array(
3402  "questionblock_id" => $row["questionblock_id"],
3403  "title" => $row["title"],
3404  "svy" => $surveytitles[$row["obj_fi"]],
3405  "contains" => join($questions_array, ", "),
3406  "owner" => $row["owner_fi"]
3407  );
3408  }
3409  }
3410  }
3411  return $rows;
3412  }
3413 
3420  function toXML()
3421  {
3422  include_once("./classes/class.ilXmlWriter.php");
3423  $a_xml_writer = new ilXmlWriter;
3424  // set xml header
3425  $a_xml_writer->xmlHeader();
3426  $attrs = array(
3427  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
3428  "xsi:noNamespaceSchemaLocation" => "http://www.ilias.de/download/xsd/ilias_survey_3_8.xsd"
3429  );
3430  $a_xml_writer->xmlStartTag("surveyobject", $attrs);
3431  $attrs = array(
3432  "id" => $this->getSurveyId(),
3433  "title" => $this->getTitle()
3434  );
3435  $a_xml_writer->xmlStartTag("survey", $attrs);
3436 
3437  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
3438  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
3439  $a_xml_writer->xmlStartTag("objectives");
3440  $attrs = array(
3441  "label" => "introduction"
3442  );
3443  $this->addMaterialTag($a_xml_writer, $this->getIntroduction(), TRUE, TRUE, $attrs);
3444  $attrs = array(
3445  "label" => "outro"
3446  );
3447  $this->addMaterialTag($a_xml_writer, $this->getOutro(), TRUE, TRUE, $attrs);
3448  $a_xml_writer->xmlEndTag("objectives");
3449 
3450  if ($this->getAnonymize())
3451  {
3452  $attribs = array("enabled" => "1");
3453  }
3454  else
3455  {
3456  $attribs = array("enabled" => "0");
3457  }
3458  $a_xml_writer->xmlElement("anonymisation", $attribs);
3459  $a_xml_writer->xmlStartTag("restrictions");
3460  if ($this->getAnonymize() == 2)
3461  {
3462  $attribs = array("type" => "free");
3463  }
3464  else
3465  {
3466  $attribs = array("type" => "restricted");
3467  }
3468  $a_xml_writer->xmlElement("access", $attribs);
3469  if ($this->getStartDateEnabled())
3470  {
3471  $attrs = array("type" => "date");
3472  $a_xml_writer->xmlElement("startingtime", $attrs, sprintf("%04d-%02d-%02dT00:00:00", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay()));
3473  }
3474  if ($this->getEndDateEnabled())
3475  {
3476  $attrs = array("type" => "date");
3477  $a_xml_writer->xmlElement("endingtime", $attrs, sprintf("%04d-%02d-%02dT00:00:00", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay()));
3478  }
3479  $a_xml_writer->xmlEndTag("restrictions");
3480 
3481  // constraints
3482  $pages =& $this->getSurveyPages();
3483  $hasconstraints = FALSE;
3484  foreach ($pages as $question_array)
3485  {
3486  foreach ($question_array as $question)
3487  {
3488  if (count($question["constraints"]))
3489  {
3490  $hasconstraints = TRUE;
3491  }
3492  }
3493  }
3494 
3495  if ($hasconstraints)
3496  {
3497  $a_xml_writer->xmlStartTag("constraints");
3498  foreach ($pages as $question_array)
3499  {
3500  foreach ($question_array as $question)
3501  {
3502  if (count($question["constraints"]))
3503  {
3504  // found constraints
3505  foreach ($question["constraints"] as $constraint)
3506  {
3507  $attribs = array(
3508  "sourceref" => $question["question_id"],
3509  "destref" => $constraint["question"],
3510  "relation" => $constraint["short"],
3511  "value" => $constraint["value"]
3512  );
3513  $a_xml_writer->xmlElement("constraint", $attribs);
3514  }
3515  }
3516  }
3517  }
3518  $a_xml_writer->xmlEndTag("constraints");
3519  }
3520 
3521  // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
3522  $a_xml_writer->xmlStartTag("metadata");
3523 
3524  $a_xml_writer->xmlStartTag("metadatafield");
3525  $a_xml_writer->xmlElement("fieldlabel", NULL, "evaluation_access");
3526  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getEvaluationAccess());
3527  $a_xml_writer->xmlEndTag("metadatafield");
3528 
3529  $a_xml_writer->xmlStartTag("metadatafield");
3530  $a_xml_writer->xmlElement("fieldlabel", NULL, "status");
3531  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getStatus());
3532  $a_xml_writer->xmlEndTag("metadatafield");
3533 
3534  $a_xml_writer->xmlStartTag("metadatafield");
3535  $a_xml_writer->xmlElement("fieldlabel", NULL, "display_question_titles");
3536  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getShowQuestionTitles());
3537  $a_xml_writer->xmlEndTag("metadatafield");
3538 
3539  $a_xml_writer->xmlStartTag("metadatafield");
3540  $a_xml_writer->xmlElement("fieldlabel", NULL, "SCORM");
3541  include_once "./Services/MetaData/classes/class.ilMD.php";
3542  $md = new ilMD($this->getId(),0, $this->getType());
3543  $writer = new ilXmlWriter();
3544  $md->toXml($writer);
3545  $metadata = $writer->xmlDumpMem();
3546  $a_xml_writer->xmlElement("fieldentry", NULL, $metadata);
3547  $a_xml_writer->xmlEndTag("metadatafield");
3548 
3549  $a_xml_writer->xmlEndTag("metadata");
3550  $a_xml_writer->xmlEndTag("survey");
3551 
3552  $attribs = array("id" => $this->getId());
3553  $a_xml_writer->xmlStartTag("surveyquestions", $attribs);
3554  // add questionblock descriptions
3555  $obligatory_states =& $this->getObligatoryStates();
3556  foreach ($pages as $question_array)
3557  {
3558  if (count($question_array) > 1)
3559  {
3560  $attribs = array("id" => $question_array[0]["question_id"]);
3561  $attribs = array("showQuestiontext" => $question_array[0]["questionblock_show_questiontext"]);
3562  $a_xml_writer->xmlStartTag("questionblock", $attribs);
3563  if (strlen($question_array[0]["questionblock_title"]))
3564  {
3565  $a_xml_writer->xmlElement("questionblocktitle", NULL, $question_array[0]["questionblock_title"]);
3566  }
3567  }
3568  foreach ($question_array as $question)
3569  {
3570  if (strlen($question["heading"]))
3571  {
3572  $a_xml_writer->xmlElement("textblock", NULL, $question["heading"]);
3573  }
3574  $questionObject =& $this->_instanciateQuestion($question["question_id"]);
3575  if ($questionObject !== FALSE) $questionObject->insertXML($a_xml_writer, FALSE, $obligatory_states[$question["question_id"]]);
3576  }
3577  if (count($question_array) > 1)
3578  {
3579  $a_xml_writer->xmlEndTag("questionblock");
3580  }
3581  }
3582 
3583  $a_xml_writer->xmlEndTag("surveyquestions");
3584  $a_xml_writer->xmlEndTag("surveyobject");
3585  $xml = $a_xml_writer->xmlDumpMem(FALSE);
3586  return $xml;
3587  }
3588 
3596  function &_instanciateQuestion($question_id)
3597  {
3598  if ($question_id < 1) return FALSE;
3599  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3600  $question_type = SurveyQuestion::_getQuestionType($question_id);
3601  if (strlen($question_type) == 0) return FALSE;
3602  SurveyQuestion::_includeClass($question_type);
3603  $question = new $question_type();
3604  $question->loadFromDb($question_id);
3605  return $question;
3606  }
3607 
3614  function locateImportFiles($a_dir)
3615  {
3616  if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
3617  {
3618  return;
3619  }
3620  $importDirectory = "";
3621  $xmlFile = "";
3622 
3623  $current_dir = opendir($a_dir);
3624  $files = array();
3625  while($entryname = readdir($current_dir))
3626  {
3627  $files[] = $entryname;
3628  }
3629 
3630  foreach($files as $file)
3631  {
3632  if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
3633  {
3634  // found directory created by zip
3635  $importDirectory = $a_dir."/".$file;
3636  }
3637  }
3638  closedir($current_dir);
3639  if (strlen($importDirectory))
3640  {
3641  // find the xml file
3642  $current_dir = opendir($importDirectory);
3643  $files = array();
3644  while($entryname = readdir($current_dir))
3645  {
3646  $files[] = $entryname;
3647  }
3648  foreach($files as $file)
3649  {
3650  if(@is_file($importDirectory."/".$file) && ($file != "." && $file!="..") && (strcmp(strtolower(substr($file, -4, 4)), ".xml") == 0))
3651  {
3652  // found xml file
3653  $xmlFile = $importDirectory."/".$file;
3654  }
3655  }
3656  }
3657  return array("dir" => $importDirectory, "xml" => $xmlFile);
3658  }
3659 
3666  function importObject($file_info, $svy_qpl_id)
3667  {
3668  // check if file was uploaded
3669  $source = $file_info["tmp_name"];
3670  $error = "";
3671  if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
3672  {
3673  $error = $this->lng->txt("import_no_file_selected");
3674  }
3675  // check correct file type
3676  $isXml = FALSE;
3677  $isZip = FALSE;
3678  if ((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0))
3679  {
3680  $isXml = TRUE;
3681  }
3682  if (strpos($file_info["type"], "zip") !== FALSE)
3683  {
3684  $isZip = TRUE;
3685  }
3686  if (!$isXml && !$isZip)
3687  {
3688  $error = $this->lng->txt("import_wrong_file_type");
3689  global $ilLog;
3690  $ilLog->write("Survey: Import error. Filetype was \"" . $file_info["type"] ."\"");
3691  }
3692  if (strlen($error) == 0)
3693  {
3694  // import file as a survey
3695  $import_dir = $this->getImportDirectory();
3696  $import_subdir = "";
3697  $importfile = "";
3698  include_once "./Services/Utilities/classes/class.ilUtil.php";
3699  if ($isZip)
3700  {
3701  $importfile = $import_dir."/".$file_info["name"];
3702  ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
3703  ilUtil::unzip($importfile);
3704  $found = $this->locateImportFiles($import_dir);
3705  if (!((strlen($found["dir"]) > 0) && (strlen($found["xml"]) > 0)))
3706  {
3707  $error = $this->lng->txt("wrong_import_file_structure");
3708  return $error;
3709  }
3710  $importfile = $found["xml"];
3711  $import_subdir = $found["dir"];
3712  }
3713  else
3714  {
3715  $importfile = tempnam($import_dir, "survey_import");
3716  ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
3717  }
3718  $fh = fopen($importfile, "r");
3719  if (!$fh)
3720  {
3721  $error = $this->lng->txt("import_error_opening_file");
3722  return $error;
3723  }
3724  $xml = fread($fh, filesize($importfile));
3725  $result = fclose($fh);
3726  if (!$result)
3727  {
3728  $error = $this->lng->txt("import_error_closing_file");
3729  return $error;
3730  }
3731 
3732  unset($_SESSION["import_mob_xhtml"]);
3733  if (strpos($xml, "questestinterop"))
3734  {
3735  include_once "./Services/Survey/classes/class.SurveyImportParserPre38.php";
3736  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
3737  $spl = new ilObjSurveyQuestionPool($svy_qpl_id, FALSE);
3738  $import = new SurveyImportParserPre38($spl, "", TRUE);
3739  $import->setSurveyObject($this);
3740  $import->setXMLContent($xml);
3741  $import->startParsing();
3742  }
3743  else
3744  {
3745  include_once "./Services/Survey/classes/class.SurveyImportParser.php";
3746  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
3747  $spl = new ilObjSurveyQuestionPool($svy_qpl_id, FALSE);
3748  $import = new SurveyImportParser($spl, "", TRUE);
3749  $import->setSurveyObject($this);
3750  $import->setXMLContent($xml);
3751  $import->startParsing();
3752  }
3753 
3754  if (is_array($_SESSION["import_mob_xhtml"]))
3755  {
3756  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
3757  include_once "./Services/RTE/classes/class.ilRTE.php";
3758  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
3759  foreach ($_SESSION["import_mob_xhtml"] as $mob)
3760  {
3761  $importfile = $import_subdir . "/" . $mob["uri"];
3762  if (file_exists($importfile))
3763  {
3764  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
3765  ilObjMediaObject::_saveUsage($media_object->getId(), "svy:html", $this->getId());
3766  $this->setIntroduction(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getIntroduction()));
3767  $this->setOutro(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getOutro()));
3768  }
3769  else
3770  {
3771  global $ilLog;
3772  $ilLog->write("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
3773  }
3774  }
3777  $this->saveToDb();
3778  }
3779 
3780  // delete import directory
3782  }
3783  return $error;
3784  }
3785 
3794  public function cloneObject($a_target_id,$a_copy_id = 0)
3795  {
3796  global $ilDB;
3797 
3798  $this->loadFromDb();
3799 
3800  // Copy settings
3801  $newObj = parent::cloneObject($a_target_id,$a_copy_id);
3802  $this->cloneMetaData($newObj);
3803 
3804 
3805  $newObj->setAuthor($this->getAuthor());
3806  $newObj->setIntroduction($this->getIntroduction());
3807  $newObj->setOutro($this->getOutro());
3808  $newObj->setStatus($this->getStatus());
3809  $newObj->setEvaluationAccess($this->getEvaluationAccess());
3810  $newObj->setStartDate($this->getStartDate());
3811  $newObj->setStartDateEnabled($this->getStartDateEnabled());
3812  $newObj->setEndDate($this->getEndDate());
3813  $newObj->setEndDateEnabled($this->getEndDateEnabled());
3814  $newObj->setInvitation($this->getInvitation());
3815  $newObj->setInvitationMode($this->getInvitationMode());
3816  $newObj->setAnonymize($this->getAnonymize());
3817  $newObj->setShowQuestionTitles($this->getShowQuestionTitles());
3818 
3819 
3820  $question_pointer = array();
3821  // clone the questions
3822  $mapping = array();
3823  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3824  foreach ($this->questions as $key => $question_id)
3825  {
3826  $question = ilObjSurvey::_instanciateQuestion($question_id);
3827  $question->id = -1;
3828  $original_id = SurveyQuestion::_getOriginalId($question_id);
3829  $question->saveToDb($original_id);
3830  $newObj->questions[$key] = $question->getId();
3831  $question_pointer[$question_id] = $question->getId();
3832  $mapping[$question_id] = $question->getId();
3833  }
3834 
3835  $newObj->saveToDb();
3836  $newObj->cloneTextblocks($mapping);
3837 
3838  // clone the questionblocks
3839  $questionblocks = array();
3840  $questionblock_questions = array();
3841  $result = $ilDB->queryF("SELECT * FROM svy_qblk_qst WHERE survey_fi = %s",
3842  array('integer'),
3843  array($this->getSurveyId())
3844  );
3845  if ($result->numRows() > 0)
3846  {
3847  while ($row = $ilDB->fetchAssoc($result))
3848  {
3849  array_push($questionblock_questions, $row);
3850  $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
3851  }
3852  }
3853  // create new questionblocks
3854  foreach ($questionblocks as $key => $value)
3855  {
3856  $questionblock = ilObjSurvey::_getQuestionblock($key);
3857  $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
3858  $questionblocks[$key] = $questionblock_id;
3859  }
3860  // create new questionblock questions
3861  foreach ($questionblock_questions as $key => $value)
3862  {
3863  $next_id = $ilDB->nextId('svy_qblk_qst');
3864  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk_qst (qblk_qst_id, survey_fi, questionblock_fi, question_fi) ".
3865  "VALUES (%s, %s, %s, %s)",
3866  array('integer','integer','integer','integer'),
3867  array($next_id, $newObj->getSurveyId(), $questionblocks[$value["questionblock_fi"]], $question_pointer[$value["question_fi"]])
3868  );
3869  }
3870 
3871  // clone the constraints
3872  $constraints = ilObjSurvey::_getConstraints($this->getSurveyId());
3873  foreach ($constraints as $key => $constraint)
3874  {
3875  $newObj->addConstraint($question_pointer[$constraint["for_question"]], $question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"]);
3876  }
3877 
3878  // clone the obligatory states
3879  $result = $ilDB->queryF("SELECT * FROM svy_qst_oblig WHERE survey_fi = %s",
3880  array('integer'),
3881  array($this->getSurveyId())
3882  );
3883  if ($result->numRows() > 0)
3884  {
3885  while ($row = $ilDB->fetchAssoc($result))
3886  {
3887  $next_id = $ilDB->nextId('svy_qst_oblig');
3888  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_oblig (question_obligatory_id, survey_fi, question_fi, ".
3889  "obligatory, tstamp) VALUES (%s, %s, %s, %s, %s)",
3890  array('integer','integer','integer','text','integer'),
3891  array($next_id, $newObj->getSurveyId(), $question_pointer[$row["question_fi"]], $row["obligatory"], time())
3892  );
3893  }
3894  }
3895  return $newObj;
3896  }
3897 
3898  function getTextblock($question_id)
3899  {
3900  global $ilDB;
3901  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE question_fi = %s",
3902  array('integer'),
3903  array($question_id)
3904  );
3905  if ($result->numRows())
3906  {
3907  $row = $ilDB->fetchAssoc($result);
3908  return $row["heading"];
3909  }
3910  else
3911  {
3912  return "";
3913  }
3914  }
3915 
3921  function cloneTextblocks($mapping)
3922  {
3923  foreach ($mapping as $original_id => $new_id)
3924  {
3925  $textblock = $this->getTextblock($original_id);
3926  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
3927  $this->saveHeading(ilUtil::stripSlashes($textblock, TRUE, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("survey")), $new_id);
3928  }
3929  }
3930 
3937  {
3938  include_once "./Services/Utilities/classes/class.ilUtil.php";
3939  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
3940  ilUtil::makeDir($svy_data_dir);
3941  if(!is_writable($svy_data_dir))
3942  {
3943  $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
3944  .") not writeable.",$this->ilias->error_obj->FATAL);
3945  }
3946 
3947  // create learning module directory (data_dir/lm_data/lm_<id>)
3948  $svy_dir = $svy_data_dir."/svy_".$this->getId();
3949  ilUtil::makeDir($svy_dir);
3950  if(!@is_dir($svy_dir))
3951  {
3952  $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
3953  }
3954  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
3955  $export_dir = $svy_dir."/export";
3956  ilUtil::makeDir($export_dir);
3957  if(!@is_dir($export_dir))
3958  {
3959  $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
3960  }
3961  }
3962 
3967  {
3968  include_once "./Services/Utilities/classes/class.ilUtil.php";
3969  $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
3970 
3971  return $export_dir;
3972  }
3973 
3978  {
3979  // quit if import dir not available
3980  if (!@is_dir($dir) or
3981  !is_writeable($dir))
3982  {
3983  return array();
3984  }
3985 
3986  // open directory
3987  $dir = dir($dir);
3988 
3989  // initialize array
3990  $file = array();
3991 
3992  // get files and save the in the array
3993  while ($entry = $dir->read())
3994  {
3995  if ($entry != "." and
3996  $entry != ".." and
3997  ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml|zip\$", $entry))
3998  {
3999  $file[] = $entry;
4000  }
4001  }
4002 
4003  // close import directory
4004  $dir->close();
4005  // sort files
4006  sort ($file);
4007  reset ($file);
4008 
4009  return $file;
4010  }
4011 
4018  {
4019  include_once "./Services/Utilities/classes/class.ilUtil.php";
4020  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
4021  ilUtil::makeDir($svy_data_dir);
4022 
4023  if(!is_writable($svy_data_dir))
4024  {
4025  $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
4026  .") not writeable.",$this->ilias->error_obj->FATAL);
4027  }
4028 
4029  // create test directory (data_dir/svy_data/svy_<id>)
4030  $svy_dir = $svy_data_dir."/svy_".$this->getId();
4031  ilUtil::makeDir($svy_dir);
4032  if(!@is_dir($svy_dir))
4033  {
4034  $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
4035  }
4036 
4037  // create import subdirectory (data_dir/svy_data/svy_<id>/import)
4038  $import_dir = $svy_dir."/import";
4039  ilUtil::makeDir($import_dir);
4040  if(!@is_dir($import_dir))
4041  {
4042  $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
4043  }
4044  }
4045 
4050  {
4051  include_once "./Services/Utilities/classes/class.ilUtil.php";
4052  $import_dir = ilUtil::getDataDir()."/svy_data".
4053  "/svy_".$this->getId()."/import";
4054  if (!is_dir($import_dir))
4055  {
4056  ilUtil::makeDirParents($import_dir);
4057  }
4058  if(@is_dir($import_dir))
4059  {
4060  return $import_dir;
4061  }
4062  else
4063  {
4064  return false;
4065  }
4066  }
4067 
4068  function saveHeading($heading = "", $insertbefore)
4069  {
4070  global $ilDB;
4071  if ($heading)
4072  {
4073  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy_qst SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
4074  array('text','integer','integer'),
4075  array($heading, $this->getSurveyId(), $insertbefore)
4076  );
4077  }
4078  else
4079  {
4080  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy_qst SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
4081  array('text','integer','integer'),
4082  array(NULL, $this->getSurveyId(), $insertbefore)
4083  );
4084  }
4085  }
4086 
4088  {
4089  global $ilDB;
4090 
4091  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_key = %s AND survey_fi = %s",
4092  array('text','integer'),
4093  array($key, $this->getSurveyId())
4094  );
4095  return ($result->numRows() == 1) ? true : false;
4096  }
4097 
4098  function getUserSurveyCode($user_id)
4099  {
4100  global $ilDB;
4101 
4102  if (($user_id == ANONYMOUS_USER_ID) || (($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys()))) return "";
4103  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
4104  array('integer','integer'),
4105  array($this->getSurveyId(), $user_id)
4106  );
4107  if ($result->numRows() == 1)
4108  {
4109  $row = $ilDB->fetchAssoc($result);
4110  return $row["anonymous_id"];
4111  }
4112  else
4113  {
4114  return "";
4115  }
4116  }
4117 
4119  {
4120  global $ilDB;
4121 
4122  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE anonymous_id = %s AND survey_fi = %s",
4123  array('text','integer'),
4124  array($key, $this->getSurveyId())
4125  );
4126  return ($result->numRows() == 1) ? true : false;
4127  }
4128 
4129  function checkSurveyCode($code)
4130  {
4131  if ($this->isAnonymousKey($code))
4132  {
4133  if ($this->isSurveyStarted("", $code) == 1)
4134  {
4135  return false;
4136  }
4137  else
4138  {
4139  return true;
4140  }
4141  }
4142  else
4143  {
4144  return false;
4145  }
4146  }
4147 
4155  {
4156  global $ilDB;
4157 
4158  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_fi = %s AND user_key IS NULL",
4159  array('integer'),
4160  array($this->getSurveyId())
4161  );
4162  return $result->numRows();
4163  }
4164 
4172  function getSurveyCodesForExport($a_array)
4173  {
4174  global $ilDB;
4175 
4176  $result = $ilDB->queryF("SELECT svy_anonymous.*, svy_finished.state FROM svy_anonymous ".
4177  "LEFT JOIN svy_finished ON svy_anonymous.survey_key = svy_finished.anonymous_id ".
4178  "WHERE svy_anonymous.survey_fi = %s AND svy_anonymous.user_key IS NULL",
4179  array('integer'),
4180  array($this->getSurveyId())
4181  );
4182  $export = "";
4183  $lang = ($_POST["lang"] != 1) ? "&lang=" . $_POST["lang"] : "";
4184  while ($row = $ilDB->fetchAssoc($result))
4185  {
4186  if (in_array($row["survey_key"], $a_array) || (count($a_array) == 0))
4187  {
4188  $export .= $row["survey_key"] . ",";
4189 
4190  // No relative (today, tomorrow...) dates in export.
4191  $date = new ilDate($row['tstamp'],IL_CAL_UNIX);
4192  $created = $date->get(IL_CAL_DATE);
4193  $export .= "$created,";
4194  if ($this->isSurveyCodeUsed($row["survey_key"]))
4195  {
4196  $export .= "1,";
4197  }
4198  else
4199  {
4200  $export .= "0,";
4201  }
4202  $url = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&client_id=" . CLIENT_ID . "&accesscode=".$row["survey_key"].$lang;
4203  $export .= $url . "\n";
4204  }
4205  }
4206  return $export;
4207  }
4208 
4216  public function &getSurveyCodesTableData($lang = "en")
4217  {
4218  global $ilDB;
4219 
4220  include_once "./classes/class.ilFormat.php";
4221  if (strlen($lang) == 0) $lang = "en";
4222 
4223  $order = "ORDER BY tstamp, survey_key ASC";
4224  $codes = array();
4225  $result = $ilDB->queryF("SELECT svy_anonymous.anonymous_id, svy_anonymous.survey_key, svy_anonymous.survey_fi, ".
4226  "svy_anonymous.tstamp, svy_finished.state FROM svy_anonymous LEFT JOIN svy_finished ".
4227  "ON svy_anonymous.survey_key = svy_finished.anonymous_id WHERE svy_anonymous.survey_fi = %s ".
4228  "AND svy_anonymous.user_key IS NULL $order",
4229  array('integer'),
4230  array($this->getSurveyId())
4231  );
4232  if ($result->numRows() > 0)
4233  {
4234  while ($row = $ilDB->fetchAssoc($result))
4235  {
4236  $created = ilDatePresentation::formatDate(new ilDateTime($row["tstamp"],IL_CAL_UNIX));
4237  $url = "";
4238 
4239  $state = 0;
4240  if ($this->isSurveyCodeUsed($row["survey_key"]))
4241  {
4242  $state = 1;
4243  }
4244  else
4245  {
4246  $addlang = "";
4247  if (strlen($lang))
4248  {
4249  $addlang = "&amp;lang=$lang";
4250  }
4251  $href = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$row["survey_key"].$addlang;
4252  $url = $this->lng->txt("survey_code_url_name");
4253  }
4254  array_push($codes, array('code' => $row["survey_key"], 'date' => $created, 'used' => $state, 'url' => $url, 'href' => $href));
4255  }
4256  }
4257  return $codes;
4258  }
4259 
4260  function isSurveyCodeUsed($code)
4261  {
4262  global $ilDB;
4263  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s AND anonymous_id = %s",
4264  array('integer','text'),
4265  array($this->getSurveyId(), $code)
4266  );
4267  return ($result->numRows() > 0) ? true : false;
4268  }
4269 
4270  function isSurveyCodeUnique($code)
4271  {
4272  global $ilDB;
4273  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_fi = %s AND survey_key = %s",
4274  array('integer','text'),
4275  array($this->getSurveyId(), $code)
4276  );
4277  return ($result->numRows() > 0) ? false : true;
4278  }
4279 
4280  function createSurveyCodes($nrOfCodes)
4281  {
4282  global $ilDB;
4283  for ($i = 0; $i < $nrOfCodes; $i++)
4284  {
4285  $anonymize_key = $this->createNewAccessCode();
4286  $next_id = $ilDB->nextId('svy_anonymous');
4287  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_anonymous (anonymous_id, survey_key, survey_fi, tstamp) ".
4288  "VALUES (%s, %s, %s, %s)",
4289  array('integer','text','integer','integer'),
4290  array($next_id, $anonymize_key, $this->getSurveyId(), time())
4291  );
4292  }
4293  }
4294 
4300  function deleteSurveyCode($survey_code)
4301  {
4302  global $ilDB;
4303 
4304  if (strlen($survey_code) > 0)
4305  {
4306  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_anonymous WHERE survey_fi = %s AND survey_key = %s",
4307  array('integer', 'text'),
4308  array($this->getSurveyId(), $survey_code)
4309  );
4310  }
4311  }
4312 
4319  function getUserAccessCode($user_id)
4320  {
4321  global $ilDB;
4322  $access_code = "";
4323  $result = $ilDB->queryF("SELECT survey_key FROM svy_anonymous WHERE survey_fi = %s AND user_key = %s",
4324  array('integer','text'),
4325  array($this->getSurveyId(), md5($user_id))
4326  );
4327  if ($result->numRows())
4328  {
4329  $row = $ilDB->fetchAssoc($result);
4330  $access_code = $row["survey_key"];
4331  }
4332  return $access_code;
4333  }
4334 
4341  function saveUserAccessCode($user_id, $access_code)
4342  {
4343  global $ilDB;
4344  $next_id = $ilDB->nextId('svy_anonymous');
4345  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_anonymous (anonymous_id, survey_key, survey_fi, user_key, tstamp) ".
4346  "VALUES (%s, %s, %s, %s, %s)",
4347  array('integer','text', 'integer', 'text', 'integer'),
4348  array($next_id, $access_code, $this->getSurveyId(), md5($user_id), time())
4349  );
4350  }
4351 
4358  {
4359  // create a 5 character code
4360  $codestring = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4361  mt_srand();
4362  $code = "";
4363  for ($i = 1; $i <=5; $i++)
4364  {
4365  $index = mt_rand(0, strlen($codestring)-1);
4366  $code .= substr($codestring, $index, 1);
4367  }
4368  // verify it against the database
4369  while (!$this->isSurveyCodeUnique($code))
4370  {
4371  $code = $this->createNewAccessCode();
4372  }
4373  return $code;
4374  }
4375 
4376 
4387  function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
4388  {
4389  $resultarray = array();
4390  foreach ($row as $rowindex => $entry)
4391  {
4392  $surround = FALSE;
4393  if ($quoteAll)
4394  {
4395  $surround = TRUE;
4396  }
4397  if (strpos($entry, "\"") !== FALSE)
4398  {
4399  $entry = str_replace("\"", "\"\"", $entry);
4400  $surround = TRUE;
4401  }
4402  if (strpos($entry, $separator) !== FALSE)
4403  {
4404  $surround = TRUE;
4405  }
4406  // replace all CR LF with LF (for Excel for Windows compatibility
4407  $entry = str_replace(chr(13).chr(10), chr(10), $entry);
4408  if ($surround)
4409  {
4410  $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
4411  }
4412  else
4413  {
4414  $resultarray[$rowindex] = utf8_decode($entry);
4415  }
4416  }
4417  return $resultarray;
4418  }
4419 
4420  function _getLastAccess($finished_id)
4421  {
4422  global $ilDB;
4423 
4424  $result = $ilDB->queryF("SELECT tstamp FROM svy_answer WHERE active_fi = %s ORDER BY tstamp DESC",
4425  array('integer'),
4426  array($finished_id)
4427  );
4428  if ($result->numRows())
4429  {
4430  $row = $ilDB->fetchAssoc($result);
4431  return $row["tstamp"];
4432  }
4433  else
4434  {
4435  $result = $ilDB->queryF("SELECT tstamp FROM svy_finished WHERE finished_id = %s",
4436  array('integer'),
4437  array($finished_id)
4438  );
4439  if ($result->numRows())
4440  {
4441  $row = $ilDB->fetchAssoc($result);
4442  return $row["tstamp"];
4443  }
4444  }
4445  return "";
4446  }
4447 
4454  function prepareTextareaOutput($txt_output)
4455  {
4456  include_once "./Services/Utilities/classes/class.ilUtil.php";
4457  return ilUtil::prepareTextareaOutput($txt_output, $prepare_for_latex_output);
4458  }
4459 
4467  function isHTML($a_text)
4468  {
4469  if (preg_match("/<[^>]*?>/", $a_text))
4470  {
4471  return TRUE;
4472  }
4473  else
4474  {
4475  return FALSE;
4476  }
4477  }
4478 
4487  function addMaterialTag(&$a_xml_writer, $a_material, $close_material_tag = TRUE, $add_mobs = TRUE, $attribs = NULL)
4488  {
4489  include_once "./Services/RTE/classes/class.ilRTE.php";
4490  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
4491 
4492  $a_xml_writer->xmlStartTag("material", $attribs);
4493  $attrs = array(
4494  "type" => "text/plain"
4495  );
4496  if ($this->isHTML($a_material))
4497  {
4498  $attrs["type"] = "text/xhtml";
4499  }
4500  $mattext = ilRTE::_replaceMediaObjectImageSrc($a_material, 0);
4501  $a_xml_writer->xmlElement("mattext", $attrs, $mattext);
4502 
4503  if ($add_mobs)
4504  {
4505  $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
4506  foreach ($mobs as $mob)
4507  {
4508  $mob_id = "il_" . IL_INST_ID . "_mob_" . $mob;
4509  if (strpos($mattext, $mob_id) !== FALSE)
4510  {
4511  $mob_obj =& new ilObjMediaObject($mob);
4512  $imgattrs = array(
4513  "label" => $mob_id,
4514  "uri" => "objects/" . "il_" . IL_INST_ID . "_mob_" . $mob . "/" . $mob_obj->getTitle()
4515  );
4516  $a_xml_writer->xmlElement("matimage", $imgattrs, NULL);
4517  }
4518  }
4519  }
4520  if ($close_material_tag) $a_xml_writer->xmlEndTag("material");
4521  }
4522 
4532  {
4533  if ($this->getAnonymize() != ANONYMIZE_OFF)
4534  {
4535  if ($this->surveyCodeSecurity == FALSE)
4536  {
4537  return TRUE;
4538  }
4539  }
4540  return FALSE;
4541  }
4542 
4550  function processPrintoutput2FO($print_output)
4551  {
4552  $print_output = str_replace("&nbsp;", "&#160;", $print_output);
4553  $print_output = str_replace("&otimes;", "X", $print_output);
4554  $xsl = file_get_contents("./Modules/Survey/xml/question2fo.xsl");
4555  $args = array( '/_xml' => $print_output, '/_xsl' => $xsl );
4556  $xh = xslt_create();
4557  $params = array();
4558  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, $params);
4559  xslt_error($xh);
4560  xslt_free($xh);
4561  global $ilLog; $ilLog->write($output);
4562  return $output;
4563  }
4564 
4571  function deliverPDFfromFO($fo)
4572  {
4573  global $ilLog;
4574 
4575  include_once "./Services/Utilities/classes/class.ilUtil.php";
4576  $fo_file = ilUtil::ilTempnam() . ".fo";
4577  $fp = fopen($fo_file, "w"); fwrite($fp, $fo); fclose($fp);
4578 
4579  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
4580  try
4581  {
4582  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF($fo);
4583  ilUtil::deliverData($pdf_base64->scalar, ilUtil::getASCIIFilename($this->getTitle()) . ".pdf", "application/pdf");
4584  return true;
4585  }
4586  catch(XML_RPC2_FaultException $e)
4587  {
4588  $ilLog->write(__METHOD__.': '.$e->getMessage());
4589  return false;
4590  }
4591  catch(Exception $e)
4592  {
4593  $ilLog->write(__METHOD__.': '.$e->getMessage());
4594  return false;
4595  }
4596 
4597  /*
4598  include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
4599  $fo2pdf = new ilFO2PDF();
4600  $fo2pdf->setFOString($fo);
4601  $result = $fo2pdf->send();
4602  ilUtil::deliverData($result, ilUtil::getASCIIFilename($this->getTitle()) . ".pdf", "application/pdf");
4603  */
4604  }
4605 
4606  function _checkCondition($a_svy_id,$a_operator,$a_value,$a_usr_id = 0)
4607  {
4608  global $ilUser;
4609 
4610  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
4611 
4612  switch($a_operator)
4613  {
4614  case 'finished':
4615  //if (ilExerciseMembers::_lookupStatus($a_exc_id, $ilias->account->getId()) == "passed")
4616  include_once("./Modules/Survey/classes/class.ilObjSurveyAccess.php");
4617  if (ilObjSurveyAccess::_lookupFinished($a_svy_id, $a_usr_id))
4618  {
4619  return true;
4620  }
4621  else
4622  {
4623  return false;
4624  }
4625  break;
4626 
4627  default:
4628  return true;
4629  }
4630  return true;
4631  }
4632 
4639  function isPluginActive($a_pname)
4640  {
4641  global $ilPluginAdmin;
4642  if ($ilPluginAdmin->isActive(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $a_pname))
4643  {
4644  return TRUE;
4645  }
4646  else
4647  {
4648  return FALSE;
4649  }
4650  }
4651 
4657  public function setSurveyId($survey_id)
4658  {
4659  $this->survey_id = $survey_id;
4660  }
4661 
4668  public function &getUserData($ids)
4669  {
4670  global $ilDB;
4671 
4672  if (!is_array($ids) || count($ids) ==0) return array();
4673 
4674  $result = $ilDB->query("SELECT usr_id, login, lastname, firstname FROM usr_data WHERE " . $ilDB->in('usr_id', $ids, false, 'integer') . " ORDER BY login");
4675  $result_array = array();
4676  while ($row = $ilDB->fetchAssoc($result))
4677  {
4678  $result_array[$row["usr_id"]]= $row;
4679  }
4680  return $result_array;
4681  }
4682 
4683  function &getGroupData($ids)
4684  {
4685  if (!is_array($ids) || count($ids) ==0) return array();
4686  $result = array();
4687  foreach ($ids as $ref_id)
4688  {
4689  $obj_id = ilObject::_lookupObjId($ref_id);
4690  $result[$ref_id] = array("ref_id" => $ref_id, "title" => ilObject::_lookupTitle($obj_id), "description" => ilObject::_lookupDescription($obj_id));
4691  }
4692  return $result;
4693  }
4694 
4695  function &getRoleData($ids)
4696  {
4697  if (!is_array($ids) || count($ids) ==0) return array();
4698  $result = array();
4699  foreach ($ids as $obj_id)
4700  {
4701  $result[$obj_id] = array("obj_id" => $obj_id, "title" => ilObject::_lookupTitle($obj_id), "description" => ilObject::_lookupDescription($obj_id));
4702  }
4703  return $result;
4704  }
4705 } // END class.ilObjSurvey
4706 ?>