ILIAS  eassessment Revision 61809
 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 
69 /* Change Sn */
70 
77 
84 
91 
98 
99 
106 
107 /* Change Sn End */
108 
114  var $status;
115 
122 
129 
136 
143 
150 
157 
164 
171 
177 
183 
190 
194 
201  function ilObjSurvey($a_id = 0,$a_call_by_reference = true)
202  {
203  global $ilUser;
204  $this->type = "svy";
205  $this->ilObject($a_id,$a_call_by_reference);
206 
207  $this->survey_id = -1;
208  $this->introduction = "";
209  $this->outro = $this->lng->txt("survey_finished");
210  /* Change Sn */
211  $this->_redirect_only_kiosk_mode = TRUE;
212  $this->_mail_confirmation = TRUE;
213  $this->_mail_confirmation_subject = $this->lng->txt("mail_confirmation_subject");
214  $this->_mail_confirmation_body = $this->lng->txt("mail_confirmation_body");
215  /* Change Sn End */
216  $this->author = $ilUser->fullname;
217  $this->status = STATUS_OFFLINE;
218  $this->evaluation_access = EVALUATION_ACCESS_OFF;
219  $this->startdate_enabled = 0;
220  $this->enddate_enabled = 0;
221  $this->questions = array();
222  $this->invitation = INVITATION_OFF;
223  $this->invitation_mode = MODE_PREDEFINED_USERS;
224  $this->anonymize = ANONYMIZE_OFF;
225  $this->display_question_titles = QUESTIONTITLES_VISIBLE;
226  $this->surveyCodeSecurity = TRUE;
227  $this->mailnotification = false;
228  }
229 
233  function create($a_upload = false)
234  {
235  parent::create();
236  if(!$a_upload)
237  {
238  $this->createMetaData();
239  }
240  }
241 
247  function createMetaData()
248  {
250  $this->saveAuthorToMetadata();
251  }
252 
259  function update()
260  {
261  $this->updateMetaData();
262 
263  if (!parent::update())
264  {
265  return false;
266  }
267 
268  // put here object specific stuff
269 
270  return true;
271  }
272 
273  function createReference()
274  {
275  $result = parent::createReference();
276  $this->saveToDb();
277  return $result;
278  }
279 
285  function read($a_force_db = false)
286  {
287  parent::read($a_force_db);
288  $this->loadFromDb();
289  }
290 
297  function addQuestion($question_id)
298  {
299  array_push($this->questions, $question_id);
300  }
301 
302 
309  function delete()
310  {
311  $remove = parent::delete();
312  // always call parent delete function first!!
313  if (!$remove)
314  {
315  return false;
316  }
317 
318  $this->deleteMetaData();
319 
320  // Delete all survey questions, constraints and materials
321  foreach ($this->questions as $question_id)
322  {
323  $this->removeQuestion($question_id);
324  }
325  $this->deleteSurveyRecord();
326 
328  return true;
329  }
330 
337  {
338  global $ilDB;
339 
340  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_svy WHERE survey_id = %s",
341  array('integer'),
342  array($this->getSurveyId())
343  );
344 
345  $result = $ilDB->queryF("SELECT questionblock_fi FROM svy_qblk_qst WHERE survey_fi = %s",
346  array('integer'),
347  array($this->getSurveyId())
348  );
349  $questionblocks = array();
350  while ($row = $ilDB->fetchAssoc($result))
351  {
352  array_push($questionblocks, $row["questionblock_fi"]);
353  }
354  if (count($questionblocks))
355  {
356  $affectedRows = $ilDB->manipulate("DELETE FROM svy_qblk WHERE " . $ilDB->in('questionblock_id', $questionblocks, false, 'integer'));
357  }
358  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE survey_fi = %s",
359  array('integer'),
360  array($this->getSurveyId())
361  );
362  $this->deleteAllUserData();
363 
364  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_anonymous WHERE survey_fi = %s",
365  array('integer'),
366  array($this->getSurveyId())
367  );
368 
369  // delete export files
370  include_once "./Services/Utilities/classes/class.ilUtil.php";
371  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
372  $directory = $svy_data_dir."/svy_".$this->getId();
373  if (is_dir($directory))
374  {
375  include_once "./Services/Utilities/classes/class.ilUtil.php";
376  ilUtil::delDir($directory);
377  }
378 
379  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
380  $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
381  // remaining usages are not in text anymore -> delete them
382  // and media objects (note: delete method of ilObjMediaObject
383  // checks whether object is used in another context; if yes,
384  // the object is not deleted!)
385  foreach($mobs as $mob)
386  {
387  ilObjMediaObject::_removeUsage($mob, "svy:html", $this->getId());
388  $mob_obj =& new ilObjMediaObject($mob);
389  $mob_obj->delete();
390  }
391  }
392 
398  function deleteAllUserData()
399  {
400  global $ilDB;
401 
402  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
403  array('integer'),
404  array($this->getSurveyId())
405  );
406  $active_array = array();
407  while ($row = $ilDB->fetchAssoc($result))
408  {
409  array_push($active_array, $row["finished_id"]);
410  }
411 
412  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_finished WHERE survey_fi = %s",
413  array('integer'),
414  array($this->getSurveyId())
415  );
416 
417  foreach ($active_array as $active_fi)
418  {
419  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE active_fi = %s",
420  array('integer'),
421  array($active_fi)
422  );
423  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_times WHERE finished_fi = %s",
424  array('integer'),
425  array($active_fi)
426  );
427  }
428  }
429 
435  function removeSelectedSurveyResults($finished_ids)
436  {
437  global $ilDB;
438 
439  foreach ($finished_ids as $finished_id)
440  {
441  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE finished_id = %s",
442  array('integer'),
443  array($finished_id)
444  );
445  $row = $ilDB->fetchAssoc($result);
446 
447  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE active_fi = %s",
448  array('integer'),
449  array($row["finished_id"])
450  );
451 
452  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_finished WHERE finished_id = %s",
453  array('integer'),
454  array($finished_id)
455  );
456 
457  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_times WHERE finished_fi = %s",
458  array('integer'),
459  array($row["finished_id"])
460  );
461  }
462  }
463 
465  {
466  global $ilDB;
467 
468  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
469  array('integer'),
470  array($this->getSurveyId())
471  );
472  $participants = array();
473  if ($result->numRows() > 0)
474  {
475  while ($row = $ilDB->fetchAssoc($result))
476  {
477  $userdata = $this->getUserDataFromActiveId($row["finished_id"]);
478  $participants[$userdata["sortname"] . $userdata["active_id"]] = $userdata;
479  }
480  }
481  return $participants;
482  }
483 
484  /* Change Sn */
486  {
487  global $ilDB;
488 
489  $query = sprintf("SELECT * FROM svy_finished WHERE survey_fi = %s",
490  $ilDB->quote($this->getSurveyId() . "")
491  );
492  $result = $ilDB->query($query);
493  $participants = array();
494  if ($result->numRows() > 0)
495  {
496  while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
497  {
498  $userdata = $this->getUserStateFromActiveId($row["finished_id"]);
499  $userdata["status"] = $row["state"];
500  $participants[$userdata["sortname"] . $userdata["active_id"]] = $userdata;
501  }
502  }
503  return $participants;
504  }
505  /* Change Sn End */
519  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
520  {
521  global $tree;
522 
523  switch ($a_event)
524  {
525  case "link":
526 
527  //var_dump("<pre>",$a_params,"</pre>");
528  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
529  //exit;
530  break;
531 
532  case "cut":
533 
534  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
535  //exit;
536  break;
537 
538  case "copy":
539 
540  //var_dump("<pre>",$a_params,"</pre>");
541  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
542  //exit;
543  break;
544 
545  case "paste":
546 
547  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
548  //exit;
549  break;
550 
551  case "new":
552 
553  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
554  //exit;
555  break;
556  }
557 
558  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
559  if ($a_node_id==$_GET["ref_id"])
560  {
561  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
562  $parent_type = $parent_obj->getType();
563  if($parent_type == $this->getType())
564  {
565  $a_node_id = (int) $tree->getParentId($a_node_id);
566  }
567  }
568 
569  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
570  }
571 
578  function isComplete()
579  {
580  if (($this->getTitle()) and (count($this->questions)))
581  {
582  return 1;
583  }
584  else
585  {
586  return 0;
587  }
588  }
589 
596  function _isComplete($obj_id)
597  {
598  $survey = new ilObjSurvey($obj_id, false);
599  $survey->loadFromDb();
600  if (($survey->getTitle()) and (count($survey->questions)))
601  {
602  return 1;
603  }
604  else
605  {
606  return 0;
607  }
608  }
609 
616  function &_getGlobalSurveyData($obj_id)
617  {
618  $survey = new ilObjSurvey($obj_id, false);
619  $survey->loadFromDb();
620  $result = array();
621  if (($survey->getTitle()) and ($survey->author) and (count($survey->questions)))
622  {
623  $result["complete"] = true;
624  }
625  else
626  {
627  $result["complete"] = false;
628  }
629  $result["evaluation_access"] = $survey->getEvaluationAccess();
630  return $result;
631  }
632 
639  {
640  global $ilDB;
641 
642  $complete = 0;
643  if ($this->isComplete())
644  {
645  $complete = 1;
646  }
647  if ($this->getSurveyId() > 0)
648  {
649  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy SET complete = %s, tstamp = %s WHERE survey_id = %s",
650  array('text','integer','integer'),
651  array($this->isComplete(), time(), $this->getSurveyId())
652  );
653  }
654  }
655 
663  function duplicateQuestionForSurvey($question_id)
664  {
665  global $ilUser;
666 
667  $questiontype = $this->getQuestionType($question_id);
668  $question_gui = $this->getQuestionGUI($questiontype, $question_id);
669  $duplicate_id = $question_gui->object->duplicate(true);
670  return $duplicate_id;
671  }
672 
678  function insertQuestion($question_id)
679  {
680  global $ilDB;
681 
682  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
683  if (!SurveyQuestion::_isComplete($question_id))
684  {
685  return FALSE;
686  }
687  else
688  {
689  // get maximum sequence index in test
690  $result = $ilDB->queryF("SELECT survey_question_id FROM svy_svy_qst WHERE survey_fi = %s",
691  array('integer'),
692  array($this->getSurveyId())
693  );
694  $sequence = $result->numRows();
695  $duplicate_id = $this->duplicateQuestionForSurvey($question_id);
696  $next_id = $ilDB->nextId('svy_svy_qst');
697  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_svy_qst (survey_question_id, survey_fi, question_fi, sequence, tstamp) VALUES (%s, %s, %s, %s, %s)",
698  array('integer', 'integer', 'integer', 'integer', 'integer'),
699  array($next_id, $this->getSurveyId(), $duplicate_id, $sequence, time())
700  );
701  $this->loadQuestionsFromDb();
702  return TRUE;
703  }
704  }
705 
706 
712  function insertQuestionblock($questionblock_id)
713  {
714  global $ilDB;
715  $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",
716  array('integer'),
717  array($questionblock_id)
718  );
719  $questions = array();
720  $show_questiontext = 0;
721  while ($row = $ilDB->fetchAssoc($result))
722  {
723  $duplicate_id = $this->duplicateQuestionForSurvey($row["question_fi"]);
724  array_push($questions, $duplicate_id);
725  $title = $row["title"];
726  $show_questiontext = $row["show_questiontext"];
727  }
728  $this->createQuestionblock($title, $show_questiontext, $questions);
729  }
730 
736  function getAllRTEContent()
737  {
738  $result = array();
739  array_push($result, $this->getIntroduction());
740  array_push($result, $this->getOutro());
741  return $result;
742  }
743 
750  {
751  include_once("./Services/RTE/classes/class.ilRTE.php");
752  $completecontent = "";
753  foreach ($this->getAllRTEContent() as $content)
754  {
755  $completecontent .= $content;
756  }
757  ilRTE::_cleanupMediaObjectUsage($completecontent, $this->getType() . ":html",
758  $this->getId());
759  }
760 
761  public function saveUserSettings($usr_id, $key, $title, $value)
762  {
763  global $ilDB;
764 
765  $next_id = $ilDB->nextId('svy_settings');
766  $affectedRows = $ilDB->insert("svy_settings", array(
767  "settings_id" => array("integer", $next_id),
768  "usr_id" => array("integer", $usr_id),
769  "keyword" => array("text", $key),
770  "title" => array("text", $title),
771  "value" => array("clob", $value)
772  ));
773  }
774 
775  public function deleteUserSettings($id)
776  {
777  global $ilDB;
778 
779  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_settings WHERE settings_id = %s",
780  array('integer'),
781  array($id)
782  );
783  return $affectedRows;
784  }
785 
786  public function getUserSettings($usr_id, $key)
787  {
788  global $ilDB;
789 
790  $result = $ilDB->queryF("SELECT * FROM svy_settings WHERE usr_id = %s AND keyword = %s",
791  array('integer', 'text'),
792  array($usr_id, $key)
793  );
794  $found = array();
795  if ($result->numRows())
796  {
797  while ($row = $ilDB->fetchAssoc($result))
798  {
799  $found[$row['settings_id']] = $row;
800  }
801  }
802  return $found;
803  }
804 
810  function saveToDb()
811  {
812  global $ilDB;
813 
814  include_once("./Services/RTE/classes/class.ilRTE.php");
815  if ($this->getSurveyId() < 1)
816  {
817  //mail_confirmation_subject, mail_confirmation_body
818  $next_id = $ilDB->nextId('svy_svy');
819  $affectedRows = $ilDB->insert("svy_svy", array(
820  "survey_id" => array("integer", $next_id),
821  "obj_fi" => array("integer", $this->getId()),
822  "author" => array("text", $this->getAuthor()),
823  "introduction" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getIntroduction(), 0)),
824  "outro" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
825  "redirect_after_survey" => array("text", $this->getRedirectAfterSurvey()),
826  "redirect_only_kiosk_mode" => array("integer", $this->getRedirectOnlyKioskMode()),
827  "mail_confirmation" => array("integer", $this->getMailConfirmation()),
828  "mail_confirmation_subject" => array("text", $this->getMailConfirmationSubject()),
829  "mail_confirmation_body" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getMailConfirmationBody(), 0)),
830  "status" => array("text", $this->getStatus()),
831  "startdate" => array("text", $this->getStartDate()),
832  "enddate" => array("text", $this->getEndDate()),
833  "evaluation_access" => array("text", $this->getEvaluationAccess()),
834  "invitation" => array("text", $this->getInvitation()),
835  "invitation_mode" => array("text", $this->getInvitationMode()),
836  "complete" => array("text", $this->isComplete()),
837  "created" => array("integer", time()),
838  "anonymize" => array("text", $this->getAnonymize()),
839  "show_question_titles" => array("text", $this->getShowQuestionTitles()),
840  "mailnotification" => array('integer', ($this->getMailNotification()) ? 1 : 0),
841  "mailaddresses" => array('text', strlen($this->getMailAddresses()) ? $this->getMailAddresses() : NULL),
842  "mailparticipantdata" => array('text', strlen($this->getMailParticipantData()) ? $this->getMailParticipantData() : NULL),
843  "tstamp" => array("integer", time())
844  ));
845  $this->setSurveyId($next_id);
846  }
847  else
848  {
849  $affectedRows = $ilDB->update("svy_svy", array(
850  "author" => array("text", $this->getAuthor()),
851  "introduction" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getIntroduction(), 0)),
852  "outro" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getOutro(), 0)),
853  "redirect_after_survey" => array("text", $this->getRedirectAfterSurvey()),
854  "redirect_only_kiosk_mode" => array("integer", $this->getRedirectOnlyKioskMode()),
855  "mail_confirmation" => array("integer", $this->getMailConfirmation()),
856  "mail_confirmation_subject" => array("text", $this->getMailConfirmationSubject()),
857  "mail_confirmation_body" => array("clob", ilRTE::_replaceMediaObjectImageSrc($this->getMailConfirmationBody(), 0)),
858  "status" => array("text", $this->getStatus()),
859  "startdate" => array("text", $this->getStartDate()),
860  "enddate" => array("text", $this->getEndDate()),
861  "evaluation_access" => array("text", $this->getEvaluationAccess()),
862  "invitation" => array("text", $this->getInvitation()),
863  "invitation_mode" => array("text", $this->getInvitationMode()),
864  "complete" => array("text", $this->isComplete()),
865  "anonymize" => array("text", $this->getAnonymize()),
866  "show_question_titles" => array("text", $this->getShowQuestionTitles()),
867  "mailnotification" => array('integer', ($this->getMailNotification()) ? 1 : 0),
868  "mailaddresses" => array('text', strlen($this->getMailAddresses()) ? $this->getMailAddresses() : NULL),
869  "mailparticipantdata" => array('text', strlen($this->getMailParticipantData()) ? $this->getMailParticipantData() : NULL),
870  "tstamp" => array("integer", time())
871  ), array(
872  "survey_id" => array("integer", $this->getSurveyId())
873  ));
874  }
875  if ($affectedRows)
876  {
877  // save questions to db
878  $this->saveQuestionsToDb();
879  }
880  }
881 
888  function saveQuestionsToDb()
889  {
890  global $ilDB;
891  // save old questions state
892  $old_questions = array();
893  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s",
894  array('integer'),
895  array($this->getSurveyId())
896  );
897  if ($result->numRows())
898  {
899  while ($row = $ilDB->fetchAssoc($result))
900  {
901  $old_questions[$row["question_fi"]] = $row;
902  }
903  }
904 
905  // delete existing question relations
906  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_svy_qst WHERE survey_fi = %s",
907  array('integer'),
908  array($this->getSurveyId())
909  );
910  // create new question relations
911  foreach ($this->questions as $key => $value)
912  {
913  $next_id = $ilDB->nextId('svy_svy_qst');
914  $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)",
915  array('integer','integer','integer','text','integer','integer'),
916  array($next_id, $this->getSurveyId(), $value, (strlen($old_questions[$value]["heading"])) ? $old_questions[$value]["heading"] : NULL, $key, time())
917  );
918  }
919  }
920 
928  function getAnonymousId($id)
929  {
930  global $ilDB;
931  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_finished WHERE anonymous_id = %s",
932  array('text'),
933  array($id)
934  );
935  if ($result->numRows())
936  {
937  $row = $ilDB->fetchAssoc($result);
938  return $row["anonymous_id"];
939  }
940  else
941  {
942  return "";
943  }
944  }
945 
952  function getQuestionGUI($questiontype, $question_id)
953  {
954  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestionGUI.php";
955  return SurveyQuestionGUI::_getQuestionGUI($questiontype, $question_id);
956  }
957 
965  function getQuestionType($question_id)
966  {
967  global $ilDB;
968  if ($question_id < 1) return -1;
969  $result = $ilDB->queryF("SELECT type_tag FROM svy_question, svy_qtype WHERE svy_question.question_id = %s AND " .
970  "svy_question.questiontype_fi = svy_qtype.questiontype_id",
971  array('integer'),
972  array($question_id)
973  );
974  if ($result->numRows() == 1)
975  {
976  $data = $ilDB->fetchAssoc($result);
977  return $data["type_tag"];
978  }
979  else
980  {
981  return "";
982  }
983  }
984 
991  function getSurveyId()
992  {
993  return $this->survey_id;
994  }
995 
999  function setAnonymize($a_anonymize)
1000  {
1001  switch ($a_anonymize)
1002  {
1003  case ANONYMIZE_OFF:
1004  case ANONYMIZE_ON:
1005  case ANONYMIZE_FREEACCESS:
1006  $this->anonymize = $a_anonymize;
1007  break;
1008  default:
1009  $this->anonymize = ANONYMIZE_OFF;
1010  break;
1011  }
1012  }
1013 
1019  function getAnonymize()
1020  {
1021  return ($this->anonymize) ? $this->anonymize : 0;
1022  }
1023 
1030  {
1031  if ($this->getAnonymize() == ANONYMIZE_FREEACCESS)
1032  {
1033  return true;
1034  }
1035  else
1036  {
1037  return false;
1038  }
1039  }
1040 
1046  function loadFromDb()
1047  {
1048  global $ilDB;
1049  $result = $ilDB->queryF("SELECT * FROM svy_svy WHERE obj_fi = %s",
1050  array('integer'),
1051  array($this->getId())
1052  );
1053  if ($result->numRows() == 1)
1054  {
1055  $data = $ilDB->fetchAssoc($result);
1056  $this->setSurveyId($data["survey_id"]);
1057  $this->setAuthor($data["author"]);
1058  include_once("./Services/RTE/classes/class.ilRTE.php");
1059  $this->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($data["introduction"], 1));
1060  if (strcmp($data["outro"], "survey_finished") == 0)
1061  {
1062  $this->setOutro($this->lng->txt("survey_finished"));
1063  }
1064  else
1065  {
1066  $this->setOutro(ilRTE::_replaceMediaObjectImageSrc($data["outro"], 1));
1067  }
1068  $this->setRedirectAfterSurvey($data["redirect_after_survey"]);
1069  $this->setRedirectOnlyKioskMode($data["redirect_only_kiosk_mode"]);
1070  $this->setMailConfirmation($data["mail_confirmation"]);
1071  $this->setMailConfirmationSubject($data["mail_confirmation_subject"]);
1072  if (strcmp($data["mail_confirmation_body"], "mail_confirmation_body") == 0)
1073  {
1074  $this->setMailConfirmationBody($this->lng->txt("mail_confirmation_body"));
1075  }
1076  else
1077  {
1078  $this->setMailConfirmationBody(ilRTE::_replaceMediaObjectImageSrc($data["mail_confirmation_body"], 1));
1079  }
1080  $this->setInvitation($data["invitation"]);
1081  $this->setInvitationMode($data["invitation_mode"]);
1082  $this->setShowQuestionTitles($data["show_question_titles"]);
1083  $this->setStartDate($data["startdate"]);
1084  $this->setStartDateEnabled(strlen($data["startdate"]));
1085  $this->setEndDate($data["enddate"]);
1086  $this->setEndDateEnabled(strlen($data["enddate"]));
1087  $this->setAnonymize($data["anonymize"]);
1088  $this->setEvaluationAccess($data["evaluation_access"]);
1089  $this->loadQuestionsFromDb();
1090  $this->setStatus($data["status"]);
1091  $this->setMailNotification($data['mailnotification']);
1092  $this->setMailAddresses($data['mailaddresses']);
1093  $this->setMailParticipantData($data['mailparticipantdata']);
1094  }
1095  }
1096 
1104  {
1105  global $ilDB;
1106  $this->questions = array();
1107  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s ORDER BY sequence",
1108  array('integer'),
1109  array($this->getSurveyId())
1110  );
1111  while ($data = $ilDB->fetchAssoc($result))
1112  {
1113  $this->questions[$data["sequence"]] = $data["question_fi"];
1114  }
1115  }
1116 
1124  function setStartDateEnabled($enabled = false)
1125  {
1126  $this->startdate_enabled = ($enabled) ? $enabled : 0;
1127  }
1128 
1137  {
1138  return ($this->startdate_enabled) ? $this->startdate_enabled : 0;
1139  }
1140 
1148  function setEndDateEnabled($enabled = false)
1149  {
1150  $this->enddate_enabled = ($enabled) ? $enabled : 0;
1151  }
1152 
1161  {
1162  return ($this->enddate_enabled) ? $this->enddate_enabled : 0;
1163  }
1164 
1172  function setAuthor($author = "")
1173  {
1174  $this->author = $author;
1175  }
1176 
1186  function saveAuthorToMetadata($a_author = "")
1187  {
1188  $md =& new ilMD($this->getId(), 0, $this->getType());
1189  $md_life =& $md->getLifecycle();
1190  if (!$md_life)
1191  {
1192  if (strlen($a_author) == 0)
1193  {
1194  global $ilUser;
1195  $a_author = $ilUser->getFullname();
1196  }
1197 
1198  $md_life =& $md->addLifecycle();
1199  $md_life->save();
1200  $con =& $md_life->addContribute();
1201  $con->setRole("Author");
1202  $con->save();
1203  $ent =& $con->addEntity();
1204  $ent->setEntity($a_author);
1205  $ent->save();
1206  }
1207  }
1208 
1216  function getAuthor()
1217  {
1218  $author = array();
1219  include_once "./Services/MetaData/classes/class.ilMD.php";
1220  $md =& new ilMD($this->getId(), 0, $this->getType());
1221  $md_life =& $md->getLifecycle();
1222  if ($md_life)
1223  {
1224  $ids =& $md_life->getContributeIds();
1225  foreach ($ids as $id)
1226  {
1227  $md_cont =& $md_life->getContribute($id);
1228  if (strcmp($md_cont->getRole(), "Author") == 0)
1229  {
1230  $entids =& $md_cont->getEntityIds();
1231  foreach ($entids as $entid)
1232  {
1233  $md_ent =& $md_cont->getEntity($entid);
1234  array_push($author, $md_ent->getEntity());
1235  }
1236  }
1237  }
1238  }
1239  return join($author, ",");
1240  }
1241 
1248  public function getShowQuestionTitles()
1249  {
1250  return ($this->display_question_titles) ? 1 : 0;
1251  }
1252 
1259  public function setShowQuestionTitles($a_show)
1260  {
1261  $this->display_question_titles = ($a_show) ? 1 : 0;
1262  }
1263 
1271  {
1272  $this->display_question_titles = 1;
1273  }
1274 
1282  {
1283  $this->display_question_titles = 0;
1284  }
1285 
1294  {
1295  global $ilDB;
1296  global $ilAccess;
1297  $this->invitation = $invitation;
1298  if ($invitation == INVITATION_OFF)
1299  {
1300  $this->disinviteAllUsers();
1301  }
1302  else if ($invitation == INVITATION_ON)
1303  {
1304  if ($this->getInvitationMode() == MODE_UNLIMITED)
1305  {
1306  $result = $ilDB->query("SELECT usr_id FROM usr_data");
1307  while ($row = $ilDB->fetchAssoc($result))
1308  {
1309  if ($ilAccess->checkAccessOfUser($row["usr_id"], "read", "", $this->getRefId(), "svy", $this->getId()))
1310  {
1311  $this->inviteUser($row['usr_id']);
1312  }
1313  }
1314  }
1315  }
1316  }
1317 
1326  {
1327  $this->invitation_mode = $invitation_mode;
1328  }
1329 
1339  {
1340  $this->invitation_mode = $invitation_mode;
1341  $this->setInvitation($invitation);
1342  }
1343 
1350  public function setIntroduction($introduction = "")
1351  {
1352  $this->introduction = $introduction;
1353  }
1354 
1361  public function setOutro($outro = "")
1362  {
1363  $this->outro = $outro;
1364  }
1365 
1366 /* Change Sn */
1376 function setRedirectAfterSurvey($a_url = "")
1377 {
1378  $this->_redirect_after_survey = $a_url;
1379 }
1380 
1390 public function setRedirectOnlyKioskMode($a_int = 1)
1391 {
1392  $this->_redirect_only_kiosk_mode = $a_int;
1393 }
1394 
1404 public function setMailConfirmation($a_int = 0)
1405 {
1406  $this->_mail_confirmation = $a_int;
1407 }
1408 
1418 function setMailConfirmationSubject($a_subject = "")
1419 {
1420  $this->_mail_confirmation_subject = $a_subject;
1421 }
1422 
1432 function setMailConfirmationBody($a_body = "")
1433 {
1434  $this->_mail_confirmation_body = $a_body;
1435 }
1436 /* Change Sn End */
1437 
1438 
1446  function getInvitation()
1447  {
1448  return ($this->invitation) ? $this->invitation : INVITATION_OFF;
1449  }
1450 
1458  function getInvitationMode()
1459  {
1460  include_once "./Services/Administration/classes/class.ilSetting.php";
1461  $surveySetting = new ilSetting("survey");
1462  $unlimited_invitation = $surveySetting->get("unlimited_invitation");
1463  if (!$unlimited_invitation && $this->invitation_mode == MODE_UNLIMITED)
1464  {
1465  return MODE_PREDEFINED_USERS;
1466  }
1467  else
1468  {
1469  return ($this->invitation_mode) ? $this->invitation_mode : MODE_UNLIMITED;
1470  }
1471  }
1472 
1480  function getStatus()
1481  {
1482  return ($this->status) ? $this->status : STATUS_OFFLINE;
1483  }
1484 
1492  function isOnline()
1493  {
1494  return ($this->status == STATUS_ONLINE) ? true : false;
1495  }
1496 
1504  function isOffline()
1505  {
1506  return ($this->status == STATUS_OFFLINE) ? true : false;
1507  }
1508 
1518  {
1519  $result = "";
1520  if (($status == STATUS_ONLINE) && (count($this->questions) == 0))
1521  {
1522  $this->status = STATUS_OFFLINE;
1523  $result = $this->lng->txt("cannot_switch_to_online_no_questions");
1524  }
1525  else
1526  {
1527  $this->status = $status;
1528  }
1529  return $result;
1530  }
1531 
1539  function getStartDate()
1540  {
1541  return (strlen($this->start_date)) ? $this->start_date : NULL;
1542  }
1543 
1550  function canStartSurvey($anonymous_id = NULL)
1551  {
1552  global $ilAccess;
1553 
1554  $result = TRUE;
1555  $messages = array();
1556  // check start date
1557  if ($this->getStartDateEnabled())
1558  {
1559  if (preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $this->getStartDate(), $matches))
1560  {
1561  $epoch_time = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1562  $now = mktime();
1563  if ($now < $epoch_time)
1564  {
1565  array_push($messages,$this->lng->txt('start_date_not_reached').' ('.
1567  $result = FALSE;
1568  }
1569  }
1570  }
1571  // check end date
1572  if ($this->getEndDateEnabled())
1573  {
1574  if (preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $this->getEndDate(), $matches))
1575  {
1576  $epoch_time = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
1577  $now = mktime();
1578  if ($now > $epoch_time)
1579  {
1580  array_push($messages,$this->lng->txt('end_date_reached').' ('.
1582  $result = FALSE;
1583  }
1584  }
1585  }
1586  // check online status
1587  if ($this->getStatus() == STATUS_OFFLINE)
1588  {
1589  array_push($messages, $this->lng->txt("survey_is_offline"));
1590  $result = FALSE;
1591  }
1592  // check rbac permissions
1593  if (!$ilAccess->checkAccess("read", "", $this->ref_id))
1594  {
1595  array_push($messages, $this->lng->txt("cannot_participate_survey"));
1596  $result = FALSE;
1597  }
1598  // 2. check previous access
1599  if (!$result["error"])
1600  {
1601  global $ilUser;
1602  $survey_started = $this->isSurveyStarted($ilUser->getId(), $anonymous_id);
1603  if ($survey_started === 1)
1604  {
1605  array_push($messages, $this->lng->txt("already_completed_survey"));
1606  $result = FALSE;
1607  }
1608  }
1609  return array(
1610  "result" => $result,
1611  "messages" => $messages
1612  );
1613  }
1614 
1622  function setStartDate($start_date = "")
1623  {
1624  $this->start_date = $start_date;
1625  }
1626 
1635  function setStartDateAndTime($start_date = "", $start_time)
1636  {
1637  $y = ''; $m = ''; $d = ''; $h = ''; $i = ''; $s = '';
1638  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $start_date, $matches))
1639  {
1640  $y = $matches[1];
1641  $m = $matches[2];
1642  $d = $matches[3];
1643  }
1644  if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $start_time, $matches))
1645  {
1646  $h = $matches[1];
1647  $i = $matches[2];
1648  $s = $matches[3];
1649  }
1650  $this->start_date = sprintf('%04d%02d%02d%02d%02d%02d', $y, $m, $d, $h, $i, $s);
1651  }
1652 
1660  function getStartMonth()
1661  {
1662  $dt = new ilDateTime($this->getStartDate(), IL_CAL_TIMESTAMP);
1663  return $dt->get(IL_CAL_FKT_DATE, 'm');
1664  }
1665 
1673  function getStartDay()
1674  {
1675  $dt = new ilDateTime($this->getStartDate(), IL_CAL_TIMESTAMP);
1676  return $dt->get(IL_CAL_FKT_DATE, 'd');
1677  }
1678 
1686  function getStartYear()
1687  {
1688  $dt = new ilDateTime($this->getStartDate(), IL_CAL_TIMESTAMP);
1689  return $dt->get(IL_CAL_FKT_DATE, 'Y');
1690  }
1691 
1699  function getStartHour()
1700  {
1701  $dt = new ilDateTime($this->getStartDate(), IL_CAL_TIMESTAMP);
1702  return $dt->get(IL_CAL_FKT_DATE, 'H');
1703  }
1704 
1712  function getStartMinute()
1713  {
1714  $dt = new ilDateTime($this->getStartDate(), IL_CAL_TIMESTAMP);
1715  return $dt->get(IL_CAL_FKT_DATE, 'i');
1716  }
1717 
1725  function getEndDate()
1726  {
1727  return (strlen($this->end_date)) ? $this->end_date : NULL;
1728  }
1729 
1737  function setEndDate($end_date = "")
1738  {
1739  $this->end_date = $end_date;
1740  }
1741 
1750  function setEndDateAndTime($end_date = "", $end_time)
1751  {
1752  $y = ''; $m = ''; $d = ''; $h = ''; $i = ''; $s = '';
1753  if (preg_match("/(\d{4})-(\d{2})-(\d{2})/", $end_date, $matches))
1754  {
1755  $y = $matches[1];
1756  $m = $matches[2];
1757  $d = $matches[3];
1758  }
1759  if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $end_time, $matches))
1760  {
1761  $h = $matches[1];
1762  $i = $matches[2];
1763  $s = $matches[3];
1764  }
1765  $this->end_date = sprintf('%04d%02d%02d%02d%02d%02d', $y, $m, $d, $h, $i, $s);
1766  }
1767 
1775  function getEndMonth()
1776  {
1777  $dt = new ilDateTime($this->getEndDate(), IL_CAL_TIMESTAMP);
1778  return $dt->get(IL_CAL_FKT_DATE, 'm');
1779  }
1780 
1788  function getEndDay()
1789  {
1790  $dt = new ilDateTime($this->getEndDate(), IL_CAL_TIMESTAMP);
1791  return $dt->get(IL_CAL_FKT_DATE, 'd');
1792  }
1793 
1801  function getEndYear()
1802  {
1803  $dt = new ilDateTime($this->getEndDate(), IL_CAL_TIMESTAMP);
1804  return $dt->get(IL_CAL_FKT_DATE, 'Y');
1805  }
1806 
1814  function getEndHour()
1815  {
1816  $dt = new ilDateTime($this->getEndDate(), IL_CAL_TIMESTAMP);
1817  return $dt->get(IL_CAL_FKT_DATE, 'H');
1818  }
1819 
1827  function getEndMinute()
1828  {
1829  $dt = new ilDateTime($this->getEndDate(), IL_CAL_TIMESTAMP);
1830  return $dt->get(IL_CAL_FKT_DATE, 'i');
1831  }
1832 
1841  {
1842  return ($this->evaluation_access) ? $this->evaluation_access : EVALUATION_ACCESS_OFF;
1843  }
1844 
1853  {
1854  $this->evaluation_access = ($evaluation_access) ? $evaluation_access : EVALUATION_ACCESS_OFF;
1855  }
1856 
1864  function getIntroduction()
1865  {
1866  return (strlen($this->introduction)) ? $this->introduction : NULL;
1867  }
1868 
1876  function getOutro()
1877  {
1878  return (strlen($this->outro)) ? $this->outro : NULL;
1879  }
1880 
1881 /* Change Sn */
1889 public function getRedirectAfterSurvey()
1890 {
1892 }
1900 public function getRedirectOnlyKioskMode()
1901 {
1903 }
1911 public function getMailConfirmation()
1912 {
1914 }
1915 
1924 {
1926 }
1927 
1935 public function getMailConfirmationBody()
1936 {
1938 }
1939 /* Change Sn End */
1940 
1941 
1949  {
1950  global $ilDB;
1951  $existing_questions = array();
1952  $result = $ilDB->queryF("SELECT svy_question.original_id FROM svy_question, svy_svy_qst WHERE " .
1953  "svy_svy_qst.survey_fi = %s AND svy_svy_qst.question_fi = svy_question.question_id",
1954  array('integer'),
1955  array($this->getSurveyId())
1956  );
1957  while ($data = $ilDB->fetchAssoc($result))
1958  {
1959  array_push($existing_questions, $data["original_id"]);
1960  }
1961  return $existing_questions;
1962  }
1963 
1970  function &getQuestionpoolTitles($could_be_offline = FALSE, $showPath = FALSE)
1971  {
1972  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
1973  return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_object_id = TRUE, $could_be_offline, $showPath);
1974  }
1975 
1982  function moveUpQuestion($question_id)
1983  {
1984  $move_questions = array($question_id);
1985  $pages =& $this->getSurveyPages();
1986  $pageindex = -1;
1987  foreach ($pages as $idx => $page)
1988  {
1989  if ($page[0]["question_id"] == $question_id)
1990  {
1991  $pageindex = $idx;
1992  }
1993  }
1994  if ($pageindex > 0)
1995  {
1996  $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
1997  }
1998  else
1999  {
2000  // move up a question in a questionblock
2001  $questions = $this->getSurveyQuestions();
2002  $questions = array_keys($questions);
2003  $index = array_search($question_id, $questions);
2004  if (($index !== FALSE) && ($index > 0))
2005  {
2006  $this->moveQuestions($move_questions, $questions[$index-1], 0);
2007  }
2008  }
2009  }
2010 
2016  public function moveDownQuestion($question_id)
2017  {
2018  $move_questions = array($question_id);
2019  $pages =& $this->getSurveyPages();
2020  $pageindex = -1;
2021  foreach ($pages as $idx => $page)
2022  {
2023  if (($page[0]["question_id"] == $question_id) && (strcmp($page[0]["questionblock_id"], "") == 0))
2024  {
2025  $pageindex = $idx;
2026  }
2027  }
2028  if (($pageindex < count($pages)-1) && ($pageindex >= 0))
2029  {
2030  $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
2031  }
2032  else
2033  {
2034  // move down a question in a questionblock
2035  $questions = $this->getSurveyQuestions();
2036  $questions = array_keys($questions);
2037  $index = array_search($question_id, $questions);
2038  if (($index !== FALSE) && ($index < count($questions)-1))
2039  {
2040  $this->moveQuestions($move_questions, $questions[$index+1], 1);
2041  }
2042  }
2043  }
2044 
2051  function moveUpQuestionblock($questionblock_id)
2052  {
2053  $pages =& $this->getSurveyPages();
2054  $move_questions = array();
2055  $pageindex = -1;
2056  foreach ($pages as $idx => $page)
2057  {
2058  if ($page[0]["questionblock_id"] == $questionblock_id)
2059  {
2060  foreach ($page as $pageidx => $question)
2061  {
2062  array_push($move_questions, $question["question_id"]);
2063  }
2064  $pageindex = $idx;
2065  }
2066  }
2067  if ($pageindex > 0)
2068  {
2069  $this->moveQuestions($move_questions, $pages[$pageindex-1][0]["question_id"], 0);
2070  }
2071  }
2072 
2079  function moveDownQuestionblock($questionblock_id)
2080  {
2081  $pages =& $this->getSurveyPages();
2082  $move_questions = array();
2083  $pageindex = -1;
2084  foreach ($pages as $idx => $page)
2085  {
2086  if ($page[0]["questionblock_id"] == $questionblock_id)
2087  {
2088  foreach ($page as $pageidx => $question)
2089  {
2090  array_push($move_questions, $question["question_id"]);
2091  }
2092  $pageindex = $idx;
2093  }
2094  }
2095  if ($pageindex < count($pages)-1)
2096  {
2097  $this->moveQuestions($move_questions, $pages[$pageindex+1][count($pages[$pageindex+1])-1]["question_id"], 1);
2098  }
2099  }
2100 
2109  function moveQuestions($move_questions, $target_index, $insert_mode)
2110  {
2111  $array_pos = array_search($target_index, $this->questions);
2112  if ($insert_mode == 0)
2113  {
2114  $part1 = array_slice($this->questions, 0, $array_pos);
2115  $part2 = array_slice($this->questions, $array_pos);
2116  }
2117  else if ($insert_mode == 1)
2118  {
2119  $part1 = array_slice($this->questions, 0, $array_pos + 1);
2120  $part2 = array_slice($this->questions, $array_pos + 1);
2121  }
2122  foreach ($move_questions as $question_id)
2123  {
2124  if (!(array_search($question_id, $part1) === FALSE))
2125  {
2126  unset($part1[array_search($question_id, $part1)]);
2127  }
2128  if (!(array_search($question_id, $part2) === FALSE))
2129  {
2130  unset($part2[array_search($question_id, $part2)]);
2131  }
2132  }
2133  $part1 = array_values($part1);
2134  $part2 = array_values($part2);
2135  $this->questions = array_values(array_merge($part1, $move_questions, $part2));
2136  foreach ($move_questions as $question_id)
2137  {
2138  $constraints = $this->getConstraints($question_id);
2139  foreach ($constraints as $idx => $constraint)
2140  {
2141  foreach ($part2 as $next_question_id)
2142  {
2143  if ($constraint["question"] == $next_question_id)
2144  {
2145  // constraint concerning a question that follows -> delete constraint
2146  $this->deleteConstraint($constraint["id"]);
2147  }
2148  }
2149  }
2150  }
2151  $this->saveQuestionsToDb();
2152  }
2153 
2160  function removeQuestion($question_id)
2161  {
2162  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2163  $question =& $this->_instanciateQuestion($question_id);
2164  $question->delete($question_id);
2165  $this->removeConstraintsConcerningQuestion($question_id);
2166  }
2167 
2175  {
2176  global $ilDB;
2177  $result = $ilDB->queryF("SELECT constraint_fi FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2178  array('integer','integer'),
2179  array($question_id, $this->getSurveyId())
2180  );
2181  if ($result->numRows() > 0)
2182  {
2183  $remove_constraints = array();
2184  while ($row = $ilDB->fetchAssoc($result))
2185  {
2186  array_push($remove_constraints, $row["constraint_fi"]);
2187  }
2188  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2189  array('integer','integer'),
2190  array($question_id, $this->getSurveyId())
2191  );
2192  foreach ($remove_constraints as $key => $constraint_id)
2193  {
2194  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_constraint WHERE constraint_id = %s",
2195  array('integer'),
2196  array($constraint_id)
2197  );
2198  }
2199  }
2200  }
2201 
2209  function removeQuestions($remove_questions, $remove_questionblocks)
2210  {
2211  global $ilDB;
2212  $questions =& $this->getSurveyQuestions();
2213  foreach ($questions as $question_id => $data)
2214  {
2215  if (in_array($question_id, $remove_questions) or in_array($data["questionblock_id"], $remove_questionblocks))
2216  {
2217  unset($this->questions[array_search($question_id, $this->questions)]);
2218  $this->removeQuestion($question_id);
2219  }
2220  }
2221  foreach ($remove_questionblocks as $questionblock_id)
2222  {
2223  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk WHERE questionblock_id = %s",
2224  array('integer'),
2225  array($questionblock_id)
2226  );
2227  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE questionblock_fi = %s AND survey_fi = %s",
2228  array('integer','integer'),
2229  array($questionblock_id, $this->getSurveyId())
2230  );
2231  }
2232  $this->questions = array_values($this->questions);
2233  $this->saveQuestionsToDb();
2234  }
2235 
2242  function unfoldQuestionblocks($questionblocks)
2243  {
2244  global $ilDB;
2245  foreach ($questionblocks as $index)
2246  {
2247  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk WHERE questionblock_id = %s",
2248  array('integer'),
2249  array($index)
2250  );
2251  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qblk_qst WHERE questionblock_fi = %s AND survey_fi = %s",
2252  array('integer','integer'),
2253  array($index, $this->getSurveyId())
2254  );
2255  }
2256  }
2257 
2264  function &getQuestionblockQuestions($questionblock_id)
2265  {
2266  global $ilDB;
2267  $titles = array();
2268  $result = $ilDB->queryF("SELECT svy_question.title, svy_qblk_qst.question_fi, svy_qblk_qst.survey_fi FROM ".
2269  "svy_qblk, svy_qblk_qst, svy_question WHERE svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND " .
2270  "svy_question.question_id = svy_qblk_qst.question_fi AND svy_qblk.questionblock_id = %s",
2271  array('integer'),
2272  array($questionblock_id)
2273  );
2274  $survey_id = "";
2275  while ($row = $ilDB->fetchAssoc($result))
2276  {
2277  $titles[$row["question_fi"]] = $row["title"];
2278  $survey_id = $row["survey_fi"];
2279  }
2280  $result = $ilDB->queryF("SELECT question_fi, sequence FROM svy_svy_qst WHERE survey_fi = %s ORDER BY sequence",
2281  array('integer'),
2282  array($survey_id)
2283  );
2284  $resultarray = array();
2285  $counter = 1;
2286  while ($row = $ilDB->fetchAssoc($result))
2287  {
2288  if (array_key_exists($row["question_fi"], $titles))
2289  {
2290  $resultarray[$counter++] = $titles[$row["question_fi"]];
2291  }
2292  }
2293  return $resultarray;
2294  }
2295 
2302  function &getQuestionblockQuestionIds($questionblock_id)
2303  {
2304  global $ilDB;
2305  $result = $ilDB->queryF("SELECT question_fi FROM svy_qblk_qst WHERE questionblock_fi = %s",
2306  array("integer"),
2307  array($questionblock_id)
2308  );
2309  $ids = array();
2310  if ($result->numRows())
2311  {
2312  while ($data = $ilDB->fetchAssoc($result))
2313  {
2314  array_push($ids, $data['question_fi']);
2315  }
2316  }
2317  return $ids;
2318  }
2319 
2327  function getQuestionblock($questionblock_id)
2328  {
2329  global $ilDB;
2330  $result = $ilDB->queryF("SELECT * FROM svy_qblk WHERE questionblock_id = %s",
2331  array('integer'),
2332  array($questionblock_id)
2333  );
2334  return $ilDB->fetchAssoc($result);
2335  }
2336 
2344  function _getQuestionblock($questionblock_id)
2345  {
2346  global $ilDB;
2347  $result = $ilDB->queryF("SELECT * FROM svy_qblk WHERE questionblock_id = %s",
2348  array('integer'),
2349  array($questionblock_id)
2350  );
2351  $row = $ilDB->fetchAssoc($result);
2352  return $row;
2353  }
2354 
2363  function _addQuestionblock($title = "", $owner = 0)
2364  {
2365  global $ilDB;
2366  $next_id = $ilDB->nextId('svy_qblk');
2367  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk (questionblock_id, title, owner_fi, tstamp) " .
2368  "VALUES (%s, %s, %s, %s)",
2369  array('integer','text','integer','integer'),
2370  array($next_id, $title, $owner, time())
2371  );
2372  return $next_id;
2373  }
2374 
2382  function createQuestionblock($title, $show_questiontext, $questions)
2383  {
2384  global $ilDB;
2385  // if the selected questions are not in a continous selection, move all questions of the
2386  // questionblock at the position of the first selected question
2387  $this->moveQuestions($questions, $questions[0], 0);
2388 
2389  // now save the question block
2390  global $ilUser;
2391  $next_id = $ilDB->nextId('svy_qblk');
2392  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk (questionblock_id, title, show_questiontext, owner_fi, ".
2393  "tstamp) VALUES (%s, %s, %s, %s, %s)",
2394  array('integer','text','text','integer','integer'),
2395  array($next_id, $title, $show_questiontext, $ilUser->getId(), time())
2396  );
2397  if ($affectedRows)
2398  {
2399  $questionblock_id = $next_id;
2400  foreach ($questions as $index)
2401  {
2402  $next_id = $ilDB->nextId('svy_qblk_qst');
2403  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk_qst (qblk_qst_id, survey_fi, questionblock_fi, " .
2404  "question_fi) VALUES (%s, %s, %s, %s)",
2405  array('integer','integer','integer','integer'),
2406  array($next_id, $this->getSurveyId(), $questionblock_id, $index)
2407  );
2408  $this->deleteConstraints($index);
2409  }
2410  }
2411  }
2412 
2420  function modifyQuestionblock($questionblock_id, $title, $show_questiontext)
2421  {
2422  global $ilDB;
2423  $affectedRows = $ilDB->manipulateF("UPDATE svy_qblk SET title = %s, show_questiontext = %s WHERE " .
2424  "questionblock_id = %s",
2425  array('text','text','integer'),
2426  array($title, $show_questiontext, $questionblock_id)
2427  );
2428  }
2429 
2436  function deleteConstraints($question_id)
2437  {
2438  global $ilDB;
2439  $result = $ilDB->queryF("SELECT constraint_fi FROM svy_qst_constraint WHERE question_fi = %s AND survey_fi = %s",
2440  array('integer','integer'),
2441  array($question_id, $this->getSurveyId())
2442  );
2443  $constraints = array();
2444  while ($row = $ilDB->fetchAssoc($result))
2445  {
2446  array_push($constraints, $row["constraint_fi"]);
2447  }
2448  foreach ($constraints as $constraint_id)
2449  {
2450  $this->deleteConstraint($constraint_id);
2451  }
2452  }
2453 
2461  function deleteConstraint($constraint_id)
2462  {
2463  global $ilDB;
2464  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_constraint WHERE constraint_id = %s",
2465  array('integer'),
2466  array($constraint_id)
2467  );
2468  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_constraint WHERE constraint_fi = %s",
2469  array('integer'),
2470  array($constraint_id)
2471  );
2472  }
2473 
2479  public function &getSurveyQuestions($with_answers = false)
2480  {
2481  global $ilDB;
2482  $obligatory_states =& $this->getObligatoryStates();
2483  // get questionblocks
2484  $all_questions = array();
2485  $result = $ilDB->queryF("SELECT svy_qtype.type_tag, svy_qtype.plugin, svy_question.question_id, ".
2486  "svy_svy_qst.heading FROM svy_qtype, svy_question, svy_svy_qst WHERE svy_svy_qst.survey_fi = %s AND " .
2487  "svy_svy_qst.question_fi = svy_question.question_id AND svy_question.questiontype_fi = svy_qtype.questiontype_id " .
2488  "ORDER BY svy_svy_qst.sequence",
2489  array('integer'),
2490  array($this->getSurveyId())
2491  );
2492  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2493  while ($row = $ilDB->fetchAssoc($result))
2494  {
2495  $add = true;
2496  if ($row["plugin"])
2497  {
2498  if (!$this->isPluginActive($row["type_tag"]))
2499  {
2500  $add = false;
2501  }
2502  }
2503  if ($add)
2504  {
2505  $question =& $this->_instanciateQuestion($row["question_id"]);
2506  $questionrow = $question->_getQuestionDataArray($row["question_id"]);
2507  foreach ($row as $key => $value)
2508  {
2509  $questionrow[$key] = $value;
2510  }
2511  $all_questions[$row["question_id"]] = $questionrow;
2512  $all_questions[$row["question_id"]]["usableForPrecondition"] = $question->usableForPrecondition();
2513  $all_questions[$row["question_id"]]["availableRelations"] = $question->getAvailableRelations();
2514  if (array_key_exists($row["question_id"], $obligatory_states))
2515  {
2516  $all_questions[$row["question_id"]]["obligatory"] = $obligatory_states[$row["question_id"]];
2517  }
2518  }
2519  }
2520  // get all questionblocks
2521  $questionblocks = array();
2522  if (count($all_questions))
2523  {
2524  $result = $ilDB->queryF("SELECT svy_qblk.*, svy_qblk_qst.question_fi FROM svy_qblk, svy_qblk_qst WHERE " .
2525  "svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_qblk_qst.survey_fi = %s " .
2526  "AND " . $ilDB->in('svy_qblk_qst.question_fi', array_keys($all_questions), false, 'integer'),
2527  array('integer'),
2528  array($this->getSurveyId())
2529  );
2530  while ($row = $ilDB->fetchAssoc($result))
2531  {
2532  $questionblocks[$row['question_fi']] = $row;
2533  }
2534  }
2535 
2536  foreach ($all_questions as $question_id => $row)
2537  {
2538  $constraints = $this->getConstraints($question_id);
2539  if (isset($questionblocks[$question_id]))
2540  {
2541  $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]['title'];
2542  $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]['questionblock_id'];
2543  $all_questions[$question_id]["constraints"] = $constraints;
2544  }
2545  else
2546  {
2547  $all_questions[$question_id]["questionblock_title"] = "";
2548  $all_questions[$question_id]["questionblock_id"] = "";
2549  $all_questions[$question_id]["constraints"] = $constraints;
2550  }
2551  if ($with_answers)
2552  {
2553  $answers = array();
2554  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title FROM svy_variable, svy_category " .
2555  "WHERE svy_variable.question_fi = %s AND svy_variable.category_fi = svy_category.category_id ".
2556  "ORDER BY sequence ASC",
2557  array('integer'),
2558  array($question_id)
2559  );
2560  if ($result->numRows() > 0)
2561  {
2562  while ($data = $ilDB->fetchAssoc($result))
2563  {
2564  array_push($answers, $data["title"]);
2565  }
2566  }
2567  $all_questions[$question_id]["answers"] = $answers;
2568  }
2569  }
2570  return $all_questions;
2571  }
2572 
2579  function setObligatoryStates($obligatory_questions)
2580  {
2581  global $ilDB;
2582  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE survey_fi = %s",
2583  array('integer'),
2584  array($this->getSurveyId())
2585  );
2586  if ($result->numRows())
2587  {
2588  while ($row = $ilDB->fetchAssoc($result))
2589  {
2590  if (!array_key_exists($row["question_fi"], $obligatory_questions))
2591  {
2592  $obligatory_questions[$row["question_fi"]] = 0;
2593  }
2594  }
2595  }
2596 
2597  // set the obligatory states in the database
2598  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_qst_oblig WHERE survey_fi = %s",
2599  array('integer'),
2600  array($this->getSurveyId())
2601  );
2602 
2603  // set the obligatory states in the database
2604  foreach ($obligatory_questions as $question_fi => $obligatory)
2605  {
2606  $next_id = $ilDB->nextId('svy_qst_oblig');
2607  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_oblig (question_obligatory_id, survey_fi, question_fi, " .
2608  "obligatory, tstamp) VALUES (%s, %s, %s, %s, %s)",
2609  array('integer','integer','integer','text','integer'),
2610  array($next_id, $this->getSurveyId(), $question_fi, (strlen($obligatory)) ? $obligatory : 0, time())
2611  );
2612  }
2613  }
2614 
2622  {
2623  global $ilDB;
2624  $obligatory_states = array();
2625  $result = $ilDB->queryF("SELECT * FROM svy_qst_oblig WHERE survey_fi = %s",
2626  array('integer'),
2627  array($this->getSurveyId())
2628  );
2629  if ($result->numRows())
2630  {
2631  while ($row = $ilDB->fetchAssoc($result))
2632  {
2633  $obligatory_states[$row["question_fi"]] = $row["obligatory"];
2634  }
2635  }
2636  return $obligatory_states;
2637  }
2638 
2644  function &getSurveyPages()
2645  {
2646  global $ilDB;
2647  $obligatory_states =& $this->getObligatoryStates();
2648  // get questionblocks
2649  $all_questions = array();
2650  $result = $ilDB->queryF("SELECT svy_question.*, svy_qtype.type_tag, svy_svy_qst.heading FROM " .
2651  "svy_question, svy_qtype, svy_svy_qst WHERE svy_svy_qst.survey_fi = %s AND " .
2652  "svy_svy_qst.question_fi = svy_question.question_id AND svy_question.questiontype_fi = svy_qtype.questiontype_id ".
2653  "ORDER BY svy_svy_qst.sequence",
2654  array('integer'),
2655  array($this->getSurveyId())
2656  );
2657  while ($row = $ilDB->fetchAssoc($result))
2658  {
2659  $all_questions[$row["question_id"]] = $row;
2660  }
2661  // get all questionblocks
2662  $questionblocks = array();
2663  if (count($all_questions))
2664  {
2665  $result = $ilDB->queryF("SELECT svy_qblk.*, svy_qblk_qst.question_fi FROM svy_qblk, svy_qblk_qst ".
2666  "WHERE svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_qblk_qst.survey_fi = %s ".
2667  "AND " . $ilDB->in('svy_qblk_qst.question_fi', array_keys($all_questions), false, 'integer'),
2668  array('integer'),
2669  array($this->getSurveyId())
2670  );
2671  while ($row = $ilDB->fetchAssoc($result))
2672  {
2673  $questionblocks[$row['question_fi']] = $row;
2674  }
2675  }
2676 
2677  $all_pages = array();
2678  $pageindex = -1;
2679  $currentblock = "";
2680  foreach ($all_questions as $question_id => $row)
2681  {
2682  if (array_key_exists($question_id, $obligatory_states))
2683  {
2684  $all_questions[$question_id]["obligatory"] = $obligatory_states[$question_id];
2685  }
2686  $constraints = array();
2687  if (isset($questionblocks[$question_id]))
2688  {
2689  if (!$currentblock or ($currentblock != $questionblocks[$question_id]['questionblock_id']))
2690  {
2691  $pageindex++;
2692  }
2693  $all_questions[$question_id]['page'] = $pageindex;
2694  $all_questions[$question_id]["questionblock_title"] = $questionblocks[$question_id]['title'];
2695  $all_questions[$question_id]["questionblock_id"] = $questionblocks[$question_id]['questionblock_id'];
2696  $all_questions[$question_id]["questionblock_show_questiontext"] = $questionblocks[$question_id]['show_questiontext'];
2697  $currentblock = $questionblocks[$question_id]['questionblock_id'];
2698  $constraints = $this->getConstraints($question_id);
2699  $all_questions[$question_id]["constraints"] = $constraints;
2700  }
2701  else
2702  {
2703  $pageindex++;
2704  $all_questions[$question_id]['page'] = $pageindex;
2705  $all_questions[$question_id]["questionblock_title"] = "";
2706  $all_questions[$question_id]["questionblock_id"] = "";
2707  $all_questions[$question_id]["questionblock_show_questiontext"] = 1;
2708  $currentblock = "";
2709  $constraints = $this->getConstraints($question_id);
2710  $all_questions[$question_id]["constraints"] = $constraints;
2711  }
2712  if (!isset($all_pages[$pageindex]))
2713  {
2714  $all_pages[$pageindex] = array();
2715  }
2716  array_push($all_pages[$pageindex], $all_questions[$question_id]);
2717  }
2718  // calculate position percentage for every page
2719  $max = count($all_pages);
2720  $counter = 1;
2721  foreach ($all_pages as $index => $block)
2722  {
2723  foreach ($block as $blockindex => $question)
2724  {
2725  $all_pages[$index][$blockindex]["position"] = $counter / $max;
2726  }
2727  $counter++;
2728  }
2729  return $all_pages;
2730  }
2731 
2740  function getNextPage($active_page_question_id, $direction)
2741  {
2742  $foundpage = -1;
2743  $pages =& $this->getSurveyPages();
2744  if (strcmp($active_page_question_id, "") == 0)
2745  {
2746  return $pages[0];
2747  }
2748  foreach ($pages as $key => $question_array)
2749  {
2750  foreach ($question_array as $question)
2751  {
2752  if ($active_page_question_id == $question["question_id"])
2753  {
2754  $foundpage = $key;
2755  }
2756  }
2757  }
2758  if ($foundpage == -1)
2759  {
2760  // error: page not found
2761  }
2762  else
2763  {
2764  $foundpage += $direction;
2765  if ($foundpage < 0)
2766  {
2767  return 0;
2768  }
2769  if ($foundpage >= count($pages))
2770  {
2771  return 1;
2772  }
2773  return $pages[$foundpage];
2774  }
2775  }
2776 
2783  function &getAvailableQuestionpools($use_obj_id = false, $could_be_offline = false, $showPath = FALSE, $permission = "read")
2784  {
2785  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
2786  return ilObjSurveyQuestionPool::_getAvailableQuestionpools($use_obj_id, $could_be_offline, $showPath, $permission);
2787  }
2788 
2795  {
2796  global $ilDB;
2797 
2798  $result_array = array();
2799  $result = $ilDB->queryF("SELECT svy_constraint.*, svy_relation.* FROM svy_qst_constraint, svy_constraint, ".
2800  "svy_relation WHERE svy_constraint.relation_fi = svy_relation.relation_id AND ".
2801  "svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_constraint.constraint_id = %s",
2802  array('integer'),
2803  array($id)
2804  );
2805  $pc = array();
2806  if ($result->numRows())
2807  {
2808  $pc = $ilDB->fetchAssoc($result);
2809  }
2810  return $pc;
2811  }
2812 
2818  function getConstraints($question_id)
2819  {
2820  global $ilDB;
2821 
2822  $result_array = array();
2823  $result = $ilDB->queryF("SELECT svy_constraint.*, svy_relation.* FROM svy_qst_constraint, svy_constraint, svy_relation ".
2824  "WHERE svy_constraint.relation_fi = svy_relation.relation_id AND ".
2825  "svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_qst_constraint.question_fi = %s ".
2826  "AND svy_qst_constraint.survey_fi = %s",
2827  array('integer','integer'),
2828  array($question_id, $this->getSurveyId())
2829  );
2830  while ($row = $ilDB->fetchAssoc($result))
2831  {
2832  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
2833  $question_type = SurveyQuestion::_getQuestionType($row["question_fi"]);
2834  SurveyQuestion::_includeClass($question_type);
2835  $question = new $question_type();
2836  $question->loadFromDb($row["question_fi"]);
2837  $valueoutput = $question->getPreconditionValueOutput($row["value"]);
2838  array_push($result_array, array("id" => $row["constraint_id"], "question" => $row["question_fi"], "short" => $row["shortname"], "long" => $row["longname"], "value" => $row["value"], "conjunction" => $row["conjunction"], "valueoutput" => $valueoutput));
2839  }
2840  return $result_array;
2841  }
2842 
2849  {
2850  global $ilDB;
2851  $result_array = array();
2852  $result = $ilDB->queryF("SELECT svy_qst_constraint.question_fi as for_question, svy_constraint.*, svy_relation.* ".
2853  "FROM svy_qst_constraint, svy_constraint, svy_relation WHERE svy_constraint.relation_fi = svy_relation.relation_id ".
2854  "AND svy_qst_constraint.constraint_fi = svy_constraint.constraint_id AND svy_qst_constraint.survey_fi = %s",
2855  array('integer'),
2856  array($survey_id)
2857  );
2858  while ($row = $ilDB->fetchAssoc($result))
2859  {
2860  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"], 'conjunction' => $row['conjunction']));
2861  }
2862  return $result_array;
2863  }
2864 
2865 
2871  function &getVariables($question_id)
2872  {
2873  global $ilDB;
2874 
2875  $result_array = array();
2876  $result = $ilDB->queryF("SELECT svy_variable.*, svy_category.title FROM svy_variable LEFT JOIN ".
2877  "svy_category ON svy_variable.category_fi = svy_category.category_id WHERE svy_variable.question_fi = %s ".
2878  "ORDER BY svy_variable.sequence",
2879  array('integer'),
2880  array($question_id)
2881  );
2882  while ($row = $ilDB->fetchObject($result))
2883  {
2884  $result_array[$row->sequence] = $row;
2885  }
2886  return $result_array;
2887  }
2888 
2897  function addConstraint($if_question_id, $relation, $value, $conjunction)
2898  {
2899  global $ilDB;
2900 
2901  $next_id = $ilDB->nextId('svy_constraint');
2902  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_constraint (constraint_id, question_fi, relation_fi, value, conjunction) VALUES ".
2903  "(%s, %s, %s, %s, %s)",
2904  array('integer','integer','integer','float', 'integer'),
2905  array($next_id, $if_question_id, $relation, $value, $conjunction)
2906  );
2907  if ($affectedRows)
2908  {
2909  return $next_id;
2910  }
2911  else
2912  {
2913  return null;
2914  }
2915  }
2916 
2917 
2924  public function addConstraintToQuestion($to_question_id, $constraint_id)
2925  {
2926  global $ilDB;
2927 
2928  $next_id = $ilDB->nextId('svy_qst_constraint');
2929  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_constraint (question_constraint_id, survey_fi, question_fi, ".
2930  "constraint_fi) VALUES (%s, %s, %s, %s)",
2931  array('integer','integer','integer','integer'),
2932  array($next_id, $this->getSurveyId(), $to_question_id, $constraint_id)
2933  );
2934  }
2935 
2946  function updateConstraint($precondition_id, $if_question_id, $relation, $value, $conjunction)
2947  {
2948  global $ilDB;
2949  $affectedRows = $ilDB->manipulateF("UPDATE svy_constraint SET question_fi = %s, relation_fi = %s, value = %s, conjunction = %s ".
2950  "WHERE constraint_id = %s",
2951  array('integer','integer','float','integer','integer'),
2952  array($if_question_id, $relation, $value, $conjunction, $precondition_id)
2953  );
2954  }
2955 
2956  public function updateConjunctionForQuestions($questions, $conjunction)
2957  {
2958  global $ilDB;
2959  foreach ($questions as $question_id)
2960  {
2961  $affectedRows = $ilDB->manipulateF("UPDATE svy_constraint SET conjunction = %s ".
2962  "WHERE constraint_id IN (SELECT constraint_fi FROM svy_qst_constraint WHERE svy_qst_constraint.question_fi = %s)",
2963  array('integer','integer'),
2964  array($conjunction, $question_id)
2965  );
2966  }
2967  }
2968 
2974  function getAllRelations($short_as_key = false)
2975  {
2976  global $ilDB;
2977 
2978  $result_array = array();
2979  $result = $ilDB->query("SELECT * FROM svy_relation");
2980  while ($row = $ilDB->fetchAssoc($result))
2981  {
2982  if ($short_as_key)
2983  {
2984  $result_array[$row["shortname"]] = array("short" => $row["shortname"], "long" => $row["longname"], "id" => $row["relation_id"]);
2985  }
2986  else
2987  {
2988  $result_array[$row["relation_id"]] = array("short" => $row["shortname"], "long" => $row["longname"]);
2989  }
2990  }
2991  return $result_array;
2992  }
2993 
2997  public function disinviteAllUsers()
2998  {
2999  global $ilDB;
3000  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE survey_fi = %s",
3001  array('integer'),
3002  array($this->getSurveyId())
3003  );
3004  while ($row = $ilDB->fetchAssoc($result))
3005  {
3006  $this->disinviteUser($row['user_fi']);
3007  }
3008  }
3009 
3015  public function disinviteUser($user_id)
3016  {
3017  global $ilDB;
3018 
3019  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_inv_usr WHERE survey_fi = %s AND user_fi = %s",
3020  array('integer','integer'),
3021  array($this->getSurveyId(), $user_id)
3022  );
3023  include_once './Services/User/classes/class.ilObjUser.php';
3024  ilObjUser::_dropDesktopItem($user_id, $this->getRefId(), "svy");
3025  }
3026 
3033  function inviteUser($user_id)
3034  {
3035  global $ilDB;
3036 
3037  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE user_fi = %s AND survey_fi = %s",
3038  array('integer','integer'),
3039  array($user_id, $this->getSurveyId())
3040  );
3041  if ($result->numRows() < 1)
3042  {
3043  $next_id = $ilDB->nextId('svy_inv_usr');
3044  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_inv_usr (invited_user_id, survey_fi, user_fi, tstamp) " .
3045  "VALUES (%s, %s, %s, %s)",
3046  array('integer','integer','integer','integer'),
3047  array($next_id, $this->getSurveyId(), $user_id, time())
3048  );
3049  }
3050  if ($this->getInvitation() == INVITATION_ON)
3051  {
3052  include_once './Services/User/classes/class.ilObjUser.php';
3053  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
3054  }
3055  }
3056 
3063  function inviteGroup($group_id)
3064  {
3065  global $ilAccess;
3066  $invited = 0;
3067  include_once "./Modules/Group/classes/class.ilObjGroup.php";
3068  $group = new ilObjGroup($group_id);
3069  $members = $group->getGroupMemberIds();
3070  foreach ($members as $user_id)
3071  {
3072  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
3073  {
3074  $this->inviteUser($user_id);
3075  if ($this->getInvitation() == INVITATION_ON)
3076  {
3077  include_once './Services/User/classes/class.ilObjUser.php';
3078  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
3079  }
3080  }
3081  }
3082  return $invited;
3083  }
3084 
3091  function inviteRole($role_id)
3092  {
3093  global $rbacreview;
3094  global $ilAccess;
3095  $invited = 0;
3096  $members = $rbacreview->assignedUsers($role_id);
3097  foreach ($members as $user_id)
3098  {
3099  if ($ilAccess->checkAccessOfUser($user_id, "read", "", $this->getRefId(), "svy", $this->getId()))
3100  {
3101  $this->inviteUser($user_id);
3102  if ($this->getInvitation() == INVITATION_ON)
3103  {
3104  include_once './Services/User/classes/class.ilObjUser.php';
3105  ilObjUser::_addDesktopItem($user_id, $this->getRefId(), "svy");
3106  }
3107  }
3108  }
3109  return $invited;
3110  }
3111 
3118  function &getInvitedUsers()
3119  {
3120  global $ilDB;
3121 
3122  $result_array = array();
3123  $result = $ilDB->queryF("SELECT user_fi FROM svy_inv_usr WHERE survey_fi = %s",
3124  array('integer'),
3125  array($this->getSurveyId())
3126  );
3127  while ($row = $ilDB->fetchAssoc($result))
3128  {
3129  array_push($result_array, $row["user_fi"]);
3130  }
3131  return $result_array;
3132  }
3133 
3141  function deleteWorkingData($question_id, $active_id)
3142  {
3143  global $ilDB;
3144 
3145  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_answer WHERE question_fi = %s AND active_fi = %s",
3146  array('integer','integer'),
3147  array($question_id, $active_id)
3148  );
3149  }
3150 
3159  function loadWorkingData($question_id, $active_id)
3160  {
3161  global $ilDB;
3162  $result_array = array();
3163  $result = $ilDB->queryF("SELECT * FROM svy_answer WHERE question_fi = %s AND active_fi = %s",
3164  array('integer','integer'),
3165  array($question_id, $active_id)
3166  );
3167  if ($result->numRows() >= 1)
3168  {
3169  while ($row = $ilDB->fetchAssoc($result))
3170  {
3171  array_push($result_array, $row);
3172  }
3173  return $result_array;
3174  }
3175  else
3176  {
3177  return $result_array;
3178  }
3179  }
3180 
3187  function fillSurveyForUser($user_id = ANONYMOUS_USER_ID)
3188  {
3189  global $ilDB;
3190  // create an anonymous key
3191  $anonymous_id = $this->createNewAccessCode();
3192  $this->saveUserAccessCode($user_id, $anonymous_id);
3193  // create the survey_finished dataset and set the survey finished already
3194  $active_id = $ilDB->nextId('svy_finished');
3195  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp) ".
3196  "VALUES (%s, %s, %s, %s, %s, %s)",
3197  array('integer','integer','integer','text','text','integer'),
3198  array($active_id, $this->getSurveyId(), $user_id, $anonymous_id, 1, time())
3199  );
3200  // fill the questions randomly
3201  $pages =& $this->getSurveyPages();
3202  foreach ($pages as $key => $question_array)
3203  {
3204  foreach ($question_array as $question)
3205  {
3206  // instanciate question
3207  require_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3208  $question =& SurveyQuestion::_instanciateQuestion($question["question_id"]);
3209  $question->saveRandomData($active_id);
3210  }
3211  }
3212  }
3213 
3220  function startSurvey($user_id, $anonymous_id)
3221  {
3222  global $ilUser;
3223  global $ilDB;
3224 
3225  if ($this->getAnonymize() && (strlen($anonymous_id) == 0)) return;
3226 
3227  if (strcmp($user_id, "") == 0)
3228  {
3229  if ($user_id == ANONYMOUS_USER_ID)
3230  {
3231  $user_id = 0;
3232  }
3233  }
3234  $next_id = $ilDB->nextId('svy_finished');
3235  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_finished (finished_id, survey_fi, user_fi, anonymous_id, state, tstamp) ".
3236  "VALUES (%s, %s, %s, %s, %s, %s)",
3237  array('integer','integer','integer','text','text','integer'),
3238  array($next_id, $this->getSurveyId(), $user_id, $anonymous_id, 0, time())
3239  );
3240  return $next_id;
3241  }
3242 
3249  function finishSurvey($user_id, $anonymize_id)
3250  {
3251  global $ilDB;
3252 
3253  if ($this->getAnonymize())
3254  {
3255  $affectedRows = $ilDB->manipulateF("UPDATE svy_finished SET state = %s, user_fi = %s, tstamp = %s ".
3256  "WHERE survey_fi = %s AND anonymous_id = %s",
3257  array('text','integer','integer','integer','text'),
3258  array(1, $user_id, time(), $this->getSurveyId(), $anonymize_id)
3259  );
3260  }
3261  else
3262  {
3263  $affectedRows = $ilDB->manipulateF("UPDATE svy_finished SET state = %s, tstamp = %s WHERE survey_fi = %s AND user_fi = %s",
3264  array('text','integer','integer','integer'),
3265  array(1, time(), $this->getSurveyId(), $user_id)
3266  );
3267  }
3268  if ($this->getMailNotification())
3269  {
3270  $this->sendNotificationMail($user_id, $anonymize_id);
3271  }
3272  }
3273 
3281  function setPage($finished_id, $page_id)
3282  {
3283  global $ilDB;
3284 
3285  $affectedRows = $ilDB->manipulateF("UPDATE svy_finished SET lastpage = %s WHERE finished_id = %s",
3286  array('integer','integer'),
3287  array(($page_id) ? $page_id : 0, $finished_id)
3288  );
3289  }
3290 
3291  function sendNotificationMail($user_id, $anonymize_id)
3292  {
3293  include_once "./Services/User/classes/class.ilObjUser.php";
3294  include_once "./Services/Mail/classes/class.ilMail.php";
3295  $mail = new ilMail(ANONYMOUS_USER_ID);
3296  $recipients = preg_split('/,/', $this->mailaddresses);
3297  foreach ($recipients as $recipient)
3298  {
3299  $messagetext = $this->mailparticipantdata;
3300  $data = ilObjUser::_getUserData(array($user_id));
3301  foreach ($data[0] as $key => $value)
3302  {
3303  if ($this->getAnonymize())
3304  {
3305  $messagetext = str_replace('[' . $key . ']', '', $messagetext);
3306  }
3307  else
3308  {
3309  $messagetext = str_replace('[' . $key . ']', $value, $messagetext);
3310  }
3311  }
3312  $active_id = $this->getActiveID($user_id, $anonymize_id);
3313  $messagetext .= ((strlen($messagetext)) ? "\n\n\n" : '') . $this->lng->txt('results') . "\n\n". $this->getParticipantTextResults($active_id);
3314  if (($not_sent != 1) || $data['sent'] == 0)
3315  {
3316  $res = $mail->sendMail(
3317  $recipient, // to
3318  "", // cc
3319  "", // bcc
3320  $this->lng->txt('finished_mail_subject') . ': ' . $this->getTitle(), // subject
3321  $messagetext, // message
3322  array(), // attachments
3323  array('normal') // type
3324  );
3325  }
3326  }
3327  }
3328 
3329  protected function getParticipantTextResults($active_id)
3330  {
3331  $textresult = "";
3332  $userResults =& $this->getUserSpecificResults();
3333  $questions =& $this->getSurveyQuestions(true);
3334  $questioncounter = 1;
3335  foreach ($questions as $question_id => $question_data)
3336  {
3337  $textresult .= $questioncounter++ . ". " . $question_data["title"] . "\n";
3338  $found = $userResults[$question_id][$active_id];
3339  $text = "";
3340  if (is_array($found))
3341  {
3342  $text = implode("\n", $found);
3343  }
3344  else
3345  {
3346  $text = $found;
3347  }
3348  if (strlen($text) == 0) $text = $this->lng->txt("skipped");
3349  $text = str_replace("<br />", "\n", $text);
3350  $textresult .= $text . "\n\n";
3351  }
3352  return $textresult;
3353  }
3354 
3356  {
3357  $counter = 0;
3358  $questions =& $this->getSurveyQuestions();
3359  $counter++;
3360  foreach ($questions as $data)
3361  {
3362  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3363  $question = SurveyQuestion::_instanciateQuestion($data["question_id"]);
3364 
3365  $eval = $this->getCumulatedResults($question);
3366  }
3367  }
3368 
3376  function isAllowedToTakeMultipleSurveys($userid = "")
3377  {
3378  $result = FALSE;
3379  if ($this->getAnonymize())
3380  {
3381  if ($this->isAccessibleWithoutCode())
3382  {
3383  if (strlen($username) == 0)
3384  {
3385  global $ilUser;
3386  $userid = $ilUser->getId();
3387  }
3388  global $ilSetting;
3389  $surveysetting = new ilSetting("survey");
3390  $allowedUsers = strlen($surveysetting->get("multiple_survey_users")) ? explode(",",$surveysetting->get("multiple_survey_users")) : array();
3391  if (in_array($userid, $allowedUsers))
3392  {
3393  $result = TRUE;
3394  }
3395  }
3396  }
3397  return $result;
3398  }
3399 
3407  function isSurveyStarted($user_id, $anonymize_id)
3408  {
3409  global $ilDB;
3410 
3411  if ($this->getAnonymize())
3412  {
3413  if ((($user_id != ANONYMOUS_USER_ID) && (strlen($anonymize_id) == 0)) && (!($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys())))
3414  {
3415  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
3416  array('integer','integer'),
3417  array($this->getSurveyId(), $user_id)
3418  );
3419  }
3420  else
3421  {
3422  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND anonymous_id = %s",
3423  array('integer','text'),
3424  array($this->getSurveyId(), $anonymize_id)
3425  );
3426  }
3427  }
3428  else
3429  {
3430  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
3431  array('integer','integer'),
3432  array($this->getSurveyId(), $user_id)
3433  );
3434  }
3435  if ($result->numRows() == 0)
3436  {
3437  return false;
3438  }
3439  else
3440  {
3441  $row = $ilDB->fetchAssoc($result);
3442  $_SESSION["finished_id"][$this->getId()] = $row["finished_id"];
3443  return (int)$row["state"];
3444  }
3445  }
3446 
3454  function getActiveID($user_id, $anonymize_id)
3455  {
3456  global $ilDB;
3457 
3458  if ($this->getAnonymize())
3459  {
3460  if ((($user_id != ANONYMOUS_USER_ID) && (strlen($anonymize_id) == 0)) && (!($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys())))
3461  {
3462  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
3463  array('integer','integer'),
3464  array($this->getSurveyId(), $user_id)
3465  );
3466  }
3467  else
3468  {
3469  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s AND anonymous_id = %s",
3470  array('integer','text'),
3471  array($this->getSurveyId(), $anonymize_id)
3472  );
3473  }
3474  }
3475  else
3476  {
3477  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
3478  array('integer','integer'),
3479  array($this->getSurveyId(), $user_id)
3480  );
3481  }
3482  if ($result->numRows() == 0)
3483  {
3484  return false;
3485  }
3486  else
3487  {
3488  $row = $ilDB->fetchAssoc($result);
3489  return $row["finished_id"];
3490  }
3491  }
3492 
3500  function getLastActivePage($active_id)
3501  {
3502  global $ilDB;
3503  $result = $ilDB->queryF("SELECT lastpage FROM svy_finished WHERE finished_id = %s",
3504  array('integer'),
3505  array($active_id)
3506  );
3507  if ($result->numRows() == 0)
3508  {
3509  return "";
3510  }
3511  else
3512  {
3513  $row = $ilDB->fetchAssoc($result);
3514  return ($row["lastpage"]) ? $row["lastpage"] : '';
3515  }
3516  }
3517 
3526  function checkConstraint($constraint_data, $working_data)
3527  {
3528  if (count($working_data) == 0)
3529  {
3530  return 0;
3531  }
3532 
3533  if ((count($working_data) == 1) and (strcmp($working_data[0]["value"], "") == 0))
3534  {
3535  return 0;
3536  }
3537  switch ($constraint_data["short"])
3538  {
3539  case "<":
3540  $all = true;
3541  foreach ($working_data as $data)
3542  {
3543  if (!($data["value"] < $constraint_data["value"]))
3544  {
3545  $all = false;
3546  }
3547  }
3548  if ($all) return 1;
3549  break;
3550  case "<=":
3551  $all = true;
3552  foreach ($working_data as $data)
3553  {
3554  if (!($data["value"] <= $constraint_data["value"]))
3555  {
3556  $all = false;
3557  }
3558  }
3559  if ($all) return 1;
3560  break;
3561  case "=":
3562  $found = false;
3563  foreach ($working_data as $data)
3564  {
3565  if ($data["value"] == $constraint_data["value"])
3566  {
3567  $found = true;
3568  }
3569  }
3570  if ($found) return 1;
3571  break;
3572  case "<>":
3573  $all = true;
3574  foreach ($working_data as $data)
3575  {
3576  if (!($data["value"] <> $constraint_data["value"]))
3577  {
3578  $all = false;
3579  }
3580  }
3581  if ($all) return 1;
3582  break;
3583  case ">=":
3584  $all = true;
3585  foreach ($working_data as $data)
3586  {
3587  if (!($data["value"] >= $constraint_data["value"]))
3588  {
3589  $all = false;
3590  }
3591  }
3592  if ($all) return 1;
3593  break;
3594  case ">":
3595  $all = true;
3596  foreach ($working_data as $data)
3597  {
3598  if (!($data["value"] > $constraint_data["value"]))
3599  {
3600  $all = false;
3601  }
3602  }
3603  if ($all) return 1;
3604  break;
3605  }
3606  return 0;
3607  }
3608 
3610  {
3611  global $ilDB;
3612 
3613  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3614  array('integer'),
3615  array($survey_id)
3616  );
3617  return ($result->numRows()) ? true : false;
3618  }
3619 
3627  {
3628  global $ilDB, $ilLog;
3629 
3630  $users = array();
3631  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
3632  array('integer'),
3633  array($this->getSurveyId())
3634  );
3635  if ($result->numRows())
3636  {
3637  while ($row = $ilDB->fetchAssoc($result))
3638  {
3639  array_push($users, $row["finished_id"]);
3640  }
3641  }
3642  return $users;
3643  }
3644 
3652  {
3653  global $ilDB;
3654 
3655  $users = array();
3656  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE survey_fi = %s",
3657  array('integer'),
3658  array($this->getSurveyId())
3659  );
3660  if ($result->numRows())
3661  {
3662  while ($row = $ilDB->fetchAssoc($result))
3663  {
3664  array_push($users, $row);
3665  }
3666  }
3667  $evaluation = array();
3668  $questions =& $this->getSurveyQuestions();
3669  foreach ($questions as $question_id => $question_data)
3670  {
3671  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
3672  $question_type = SurveyQuestion::_getQuestionType($question_id);
3673  SurveyQuestion::_includeClass($question_type);
3674  $question = new $question_type();
3675  $question->loadFromDb($question_id);
3676  $data =& $question->getUserAnswers($this->getSurveyId());
3677  $evaluation[$question_id] = $data;
3678  }
3679  return $evaluation;
3680  }
3681 
3689  function getUserDataFromActiveId($active_id)
3690  {
3691  global $ilDB;
3692 
3693  $surveySetting = new ilSetting("survey");
3694  $use_anonymous_id = array_key_exists("use_anonymous_id", $_GET) ? $_GET["use_anonymous_id"] : $surveySetting->get("use_anonymous_id");
3695  $result = $ilDB->queryF("SELECT * FROM svy_finished WHERE finished_id = %s",
3696  array('integer'),
3697  array($active_id)
3698  );
3699  $row = array();
3700  $foundrows = $result->numRows();
3701  if ($foundrows)
3702  {
3703  $row = $ilDB->fetchAssoc($result);
3704  }
3705  $name = ($use_anonymous_id) ? $row["anonymous_id"] : $this->lng->txt("anonymous");
3706  $userdata = array(
3707  "fullname" => $name,
3708  "sortname" => $name,
3709  "firstname" => "",
3710  "lastname" => "",
3711  "login" => "",
3712  "gender" => "",
3713  "active_id" => "$active_id"
3714  );
3715  if ($foundrows)
3716  {
3717  if (($row["user_fi"] > 0) && ($row["user_fi"] != ANONYMOUS_USER_ID) && ($this->getAnonymize() == 0))
3718  {
3719  include_once './Services/User/classes/class.ilObjUser.php';
3720  if (strlen(ilObjUser::_lookupLogin($row["user_fi"])) == 0)
3721  {
3722  $userdata["fullname"] = $this->lng->txt("deleted_user");
3723  }
3724  else
3725  {
3726  $user = new ilObjUser($row["user_fi"]);
3727  $userdata["fullname"] = $user->getFullname();
3728  $gender = $user->getGender();
3729  if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
3730  $userdata["gender"] = $gender;
3731  $userdata["firstname"] = $user->getFirstname();
3732  $userdata["lastname"] = $user->getLastname();
3733  $userdata["sortname"] = $user->getLastname() . ", " . $user->getFirstname();
3734  $userdata["login"] = $user->getLogin();
3735  }
3736  }
3737  }
3738  return $userdata;
3739  }
3740  /* Change Sn */
3741  function getUserStateFromActiveId($active_id)
3742  {
3743  global $ilDB;
3744 
3745  $query = sprintf("SELECT * FROM svy_finished WHERE finished_id = %s",
3746  $ilDB->quote($active_id)
3747  );
3748  $result = $ilDB->query($query);
3749  $userdata = array(
3750  "fullname" => "",
3751  "sortname" => "",
3752  "firstname" => "",
3753  "lastname" => "",
3754  "login" => "",
3755  "gender" => "",
3756  "email" => "",
3757  "active_id" => "$active_id"
3758  );
3759  if ($result->numRows())
3760  {
3761  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
3762  if ($row["user_fi"] > 0)
3763  {
3764  include_once './Services/User/classes/class.ilObjUser.php';
3765  if (strlen(ilObjUser::_lookupLogin($row["user_fi"])) == 0)
3766  {
3767  $userdata["fullname"] = $this->lng->txt("deleted_user");
3768  }
3769  else
3770  {
3771  $user = new ilObjUser($row["user_fi"]);
3772  $userdata["fullname"] = $user->getFullname();
3773  $userdata["login"] = $user->getLogin();
3774  $gender = $user->getGender();
3775  if (strlen($gender) == 1) $gender = $this->lng->txt("gender_$gender");
3776  $userdata["gender"] = $gender;
3777  $userdata["firstname"] = $user->getFirstname();
3778  $userdata["lastname"] = $user->getLastname();
3779  $userdata["sortname"] = $user->getLastname() . ", " . $user->getFirstname();
3780  $userdata["email"] = $user->getEmail();
3781  }
3782  }
3783  }
3784  return $userdata;
3785  }
3786  /* Change Sn End */
3787 
3797  function &getEvaluationByUser($questions, $active_id)
3798  {
3799  global $ilDB;
3800 
3801  // collect all answers
3802  $answers = array();
3803  $result = $ilDB->queryF("SELECT * FROM svy_answer WHERE active_fi = %s",
3804  array('integer'),
3805  array($active_id)
3806  );
3807  while ($row = $ilDB->fetchAssoc($result))
3808  {
3809  if (!is_array($answers[$row["question_fi"]]))
3810  {
3811  $answers[$row["question_fi"]] = array();
3812  }
3813  array_push($answers[$row["question_fi"]], $row);
3814  }
3815  $userdata = $this->getUserDataFromActiveId($active_id);
3816  $resultset = array(
3817  "name" => $userdata["fullname"],
3818  "gender" => $userdata["gender"],
3819  "answers" => array()
3820  );
3821  foreach ($questions as $key => $question)
3822  {
3823  if (array_key_exists($key, $answers))
3824  {
3825  $resultset["answers"][$key] = $answers[$key];
3826  }
3827  else
3828  {
3829  $resultset["answers"][$key] = array();
3830  }
3831  sort($resultset["answers"][$key]);
3832  }
3833  return $resultset;
3834  }
3835 
3844  function getCumulatedResults(&$question)
3845  {
3846  global $ilDB;
3847 
3848  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3849  array('integer'),
3850  array($this->getSurveyId())
3851  );
3852  $nr_of_users = $result->numRows();
3853 
3854  $result_array =& $question->getCumulatedResults($this->getSurveyId(), $nr_of_users);
3855  return $result_array;
3856  }
3857 
3866  {
3867  global $ilDB;
3868 
3869  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s",
3870  array('integer'),
3871  array($survey_id)
3872  );
3873  return $result->numRows();
3874  }
3875 
3876  function &getQuestions($question_ids)
3877  {
3878  $result_array = array();
3879  $result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag FROM svy_question, svy_qtype WHERE ".
3880  "svy_question.questiontype_fi = svy_qtype.questiontype_id AND svy_question.tstamp > 0 AND ".
3881  $ilDB->in('svy_question.question_id', $question_ids, false, 'integer'));
3882  while ($row = $ilDB->fetchAssoc($result))
3883  {
3884  array_push($result_array, $row);
3885  }
3886  return $result_array;
3887  }
3888 
3894  function getQuestionsTable($arrFilter)
3895  {
3896  global $ilUser;
3897  global $ilDB;
3898  $where = "";
3899  if (is_array($arrFilter))
3900  {
3901  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title']))
3902  {
3903  $where .= " AND " . $ilDB->like('svy_question.title', 'text', "%%" . $arrFilter['title'] . "%%");
3904  }
3905  if (array_key_exists('description', $arrFilter) && strlen($arrFilter['description']))
3906  {
3907  $where .= " AND " . $ilDB->like('svy_question.description', 'text', "%%" . $arrFilter['description'] . "%%");
3908  }
3909  if (array_key_exists('author', $arrFilter) && strlen($arrFilter['author']))
3910  {
3911  $where .= " AND " . $ilDB->like('svy_question.author', 'text', "%%" . $arrFilter['author'] . "%%");
3912  }
3913  if (array_key_exists('type', $arrFilter) && strlen($arrFilter['type']))
3914  {
3915  $where .= " AND svy_qtype.type_tag = " . $ilDB->quote($arrFilter['type'], 'text');
3916  }
3917  if (array_key_exists('spl', $arrFilter) && strlen($arrFilter['spl']))
3918  {
3919  $where .= " AND svy_question.obj_fi = " . $ilDB->quote($arrFilter['spl'], 'integer');
3920  }
3921  }
3922 
3923  $spls =& $this->getAvailableQuestionpools($use_obj_id = TRUE, $could_be_offline = FALSE, $showPath = FALSE);
3924  $forbidden = "";
3925  $forbidden = " AND " . $ilDB->in('svy_question.obj_fi', array_keys($spls), false, 'integer');
3926  $forbidden .= " AND svy_question.complete = " . $ilDB->quote("1", 'text');
3927  $existing = "";
3928  $existing_questions =& $this->getExistingQuestions();
3929  if (count($existing_questions))
3930  {
3931  $existing = " AND " . $ilDB->in('svy_question.question_id', $existing_questions, true, 'integer');
3932  }
3933 
3934  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
3936 
3937  $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);
3938 
3939  $rows = array();
3940  if ($query_result->numRows())
3941  {
3942  while ($row = $ilDB->fetchAssoc($query_result))
3943  {
3944  $row['ttype'] = $trans[$row['type_tag']];
3945  if ($row["plugin"])
3946  {
3947  if ($this->isPluginActive($row["type_tag"]))
3948  {
3949  array_push($rows, $row);
3950  }
3951  }
3952  else
3953  {
3954  array_push($rows, $row);
3955  }
3956  }
3957  }
3958  return $rows;
3959  }
3960 
3966  function getQuestionblocksTable($arrFilter)
3967  {
3968  global $ilUser;
3969  global $ilDB;
3970  $where = "";
3971  if (is_array($arrFilter))
3972  {
3973  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title']))
3974  {
3975  $where .= " AND " . $ilDB->like('svy_qblk.title', 'text', "%%" . $arrFilter['title'] . "%%");
3976  }
3977  }
3978 
3979  $query_result = $ilDB->query("SELECT svy_qblk.*, svy_svy.obj_fi FROM svy_qblk , svy_qblk_qst, svy_svy WHERE ".
3980  "svy_qblk.questionblock_id = svy_qblk_qst.questionblock_fi AND svy_svy.survey_id = svy_qblk_qst.survey_fi ".
3981  "$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");
3982  $rows = array();
3983  if ($query_result->numRows())
3984  {
3985  $surveys = ilObject::_getObjectsDataForType('svy', true);
3986  $surveytitles = array();
3987  foreach ($surveys as $data)
3988  {
3989  $surveytitles[$data['id']] = $data['title'];
3990  }
3991  while ($row = $ilDB->fetchAssoc($query_result))
3992  {
3993  $questions_array =& $this->getQuestionblockQuestions($row["questionblock_id"]);
3994  $counter = 1;
3995  foreach ($questions_array as $key => $value)
3996  {
3997  $questions_array[$key] = "$counter. $value";
3998  $counter++;
3999  }
4000  if (strlen($surveytitles[$row["obj_fi"]])) // only questionpools which are not in trash
4001  {
4002  $rows[$row["questionblock_id"]] = array(
4003  "questionblock_id" => $row["questionblock_id"],
4004  "title" => $row["title"],
4005  "svy" => $surveytitles[$row["obj_fi"]],
4006  "contains" => join($questions_array, ", "),
4007  "owner" => $row["owner_fi"]
4008  );
4009  }
4010  }
4011  }
4012  return $rows;
4013  }
4014 
4021  function toXML()
4022  {
4023  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
4024  $a_xml_writer = new ilXmlWriter;
4025  // set xml header
4026  $a_xml_writer->xmlHeader();
4027  $attrs = array(
4028  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
4029  "xsi:noNamespaceSchemaLocation" => "http://www.ilias.de/download/xsd/ilias_survey_3_8.xsd"
4030  );
4031  $a_xml_writer->xmlStartTag("surveyobject", $attrs);
4032  $attrs = array(
4033  "id" => $this->getSurveyId(),
4034  "title" => $this->getTitle()
4035  );
4036  $a_xml_writer->xmlStartTag("survey", $attrs);
4037 
4038  $a_xml_writer->xmlElement("description", NULL, $this->getDescription());
4039  $a_xml_writer->xmlElement("author", NULL, $this->getAuthor());
4040  $a_xml_writer->xmlStartTag("objectives");
4041  $attrs = array(
4042  "label" => "introduction"
4043  );
4044  $this->addMaterialTag($a_xml_writer, $this->getIntroduction(), TRUE, TRUE, $attrs);
4045  $attrs = array(
4046  "label" => "outro"
4047  );
4048  $this->addMaterialTag($a_xml_writer, $this->getOutro(), TRUE, TRUE, $attrs);
4049  $a_xml_writer->xmlEndTag("objectives");
4050 
4051  if ($this->getAnonymize())
4052  {
4053  $attribs = array("enabled" => "1");
4054  }
4055  else
4056  {
4057  $attribs = array("enabled" => "0");
4058  }
4059  $a_xml_writer->xmlElement("anonymisation", $attribs);
4060  $a_xml_writer->xmlStartTag("restrictions");
4061  if ($this->getAnonymize() == 2)
4062  {
4063  $attribs = array("type" => "free");
4064  }
4065  else
4066  {
4067  $attribs = array("type" => "restricted");
4068  }
4069  $a_xml_writer->xmlElement("access", $attribs);
4070  if ($this->getStartDateEnabled())
4071  {
4072  $attrs = array("type" => "date");
4073  $a_xml_writer->xmlElement("startingtime", $attrs, sprintf("%04d-%02d-%02dT%02d:%02d:00", $this->getStartYear(), $this->getStartMonth(), $this->getStartDay(), $this->getStartHour(), $this->getStartMinute()));
4074  }
4075  if ($this->getEndDateEnabled())
4076  {
4077  $attrs = array("type" => "date");
4078  $a_xml_writer->xmlElement("endingtime", $attrs, sprintf("%04d-%02d-%02dT%02d:%02d:00", $this->getEndYear(), $this->getEndMonth(), $this->getEndDay(), $this->getEndHour(), $this->getEndMinute()));
4079  }
4080  $a_xml_writer->xmlEndTag("restrictions");
4081 
4082  // constraints
4083  $pages =& $this->getSurveyPages();
4084  $hasconstraints = FALSE;
4085  foreach ($pages as $question_array)
4086  {
4087  foreach ($question_array as $question)
4088  {
4089  if (count($question["constraints"]))
4090  {
4091  $hasconstraints = TRUE;
4092  }
4093  }
4094  }
4095 
4096  if ($hasconstraints)
4097  {
4098  $a_xml_writer->xmlStartTag("constraints");
4099  foreach ($pages as $question_array)
4100  {
4101  foreach ($question_array as $question)
4102  {
4103  if (count($question["constraints"]))
4104  {
4105  // found constraints
4106  foreach ($question["constraints"] as $constraint)
4107  {
4108  $attribs = array(
4109  "sourceref" => $question["question_id"],
4110  "destref" => $constraint["question"],
4111  "relation" => $constraint["short"],
4112  "value" => $constraint["value"]
4113  );
4114  $a_xml_writer->xmlElement("constraint", $attribs);
4115  }
4116  }
4117  }
4118  }
4119  $a_xml_writer->xmlEndTag("constraints");
4120  }
4121 
4122  // add the rest of the preferences in qtimetadata tags, because there is no correspondent definition in QTI
4123  $a_xml_writer->xmlStartTag("metadata");
4124 
4125  $a_xml_writer->xmlStartTag("metadatafield");
4126  $a_xml_writer->xmlElement("fieldlabel", NULL, "evaluation_access");
4127  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getEvaluationAccess());
4128  $a_xml_writer->xmlEndTag("metadatafield");
4129 
4130  $a_xml_writer->xmlStartTag("metadatafield");
4131  $a_xml_writer->xmlElement("fieldlabel", NULL, "status");
4132  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getStatus());
4133  $a_xml_writer->xmlEndTag("metadatafield");
4134 
4135  $a_xml_writer->xmlStartTag("metadatafield");
4136  $a_xml_writer->xmlElement("fieldlabel", NULL, "display_question_titles");
4137  $a_xml_writer->xmlElement("fieldentry", NULL, $this->getShowQuestionTitles());
4138  $a_xml_writer->xmlEndTag("metadatafield");
4139 
4140  $a_xml_writer->xmlStartTag("metadatafield");
4141  $a_xml_writer->xmlElement("fieldlabel", NULL, "SCORM");
4142  include_once "./Services/MetaData/classes/class.ilMD.php";
4143  $md = new ilMD($this->getId(),0, $this->getType());
4144  $writer = new ilXmlWriter();
4145  $md->toXml($writer);
4146  $metadata = $writer->xmlDumpMem();
4147  $a_xml_writer->xmlElement("fieldentry", NULL, $metadata);
4148  $a_xml_writer->xmlEndTag("metadatafield");
4149 
4150  $a_xml_writer->xmlEndTag("metadata");
4151  $a_xml_writer->xmlEndTag("survey");
4152 
4153  $attribs = array("id" => $this->getId());
4154  $a_xml_writer->xmlStartTag("surveyquestions", $attribs);
4155  // add questionblock descriptions
4156  $obligatory_states =& $this->getObligatoryStates();
4157  foreach ($pages as $question_array)
4158  {
4159  if (count($question_array) > 1)
4160  {
4161  $attribs = array("id" => $question_array[0]["question_id"]);
4162  $attribs = array("showQuestiontext" => $question_array[0]["questionblock_show_questiontext"]);
4163  $a_xml_writer->xmlStartTag("questionblock", $attribs);
4164  if (strlen($question_array[0]["questionblock_title"]))
4165  {
4166  $a_xml_writer->xmlElement("questionblocktitle", NULL, $question_array[0]["questionblock_title"]);
4167  }
4168  }
4169  foreach ($question_array as $question)
4170  {
4171  if (strlen($question["heading"]))
4172  {
4173  $a_xml_writer->xmlElement("textblock", NULL, $question["heading"]);
4174  }
4175  $questionObject =& $this->_instanciateQuestion($question["question_id"]);
4176  if ($questionObject !== FALSE) $questionObject->insertXML($a_xml_writer, FALSE, $obligatory_states[$question["question_id"]]);
4177  }
4178  if (count($question_array) > 1)
4179  {
4180  $a_xml_writer->xmlEndTag("questionblock");
4181  }
4182  }
4183 
4184  $a_xml_writer->xmlEndTag("surveyquestions");
4185  $a_xml_writer->xmlEndTag("surveyobject");
4186  $xml = $a_xml_writer->xmlDumpMem(FALSE);
4187  return $xml;
4188  }
4189 
4197  function &_instanciateQuestion($question_id)
4198  {
4199  if ($question_id < 1) return FALSE;
4200  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
4201  $question_type = SurveyQuestion::_getQuestionType($question_id);
4202  if (strlen($question_type) == 0) return FALSE;
4203  SurveyQuestion::_includeClass($question_type);
4204  $question = new $question_type();
4205  $question->loadFromDb($question_id);
4206  return $question;
4207  }
4208 
4215  function locateImportFiles($a_dir)
4216  {
4217  if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
4218  {
4219  return;
4220  }
4221  $importDirectory = "";
4222  $xmlFile = "";
4223 
4224  $current_dir = opendir($a_dir);
4225  $files = array();
4226  while($entryname = readdir($current_dir))
4227  {
4228  $files[] = $entryname;
4229  }
4230 
4231  foreach($files as $file)
4232  {
4233  if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
4234  {
4235  // found directory created by zip
4236  $importDirectory = $a_dir."/".$file;
4237  }
4238  }
4239  closedir($current_dir);
4240  if (strlen($importDirectory))
4241  {
4242  // find the xml file
4243  $current_dir = opendir($importDirectory);
4244  $files = array();
4245  while($entryname = readdir($current_dir))
4246  {
4247  $files[] = $entryname;
4248  }
4249  foreach($files as $file)
4250  {
4251  if(@is_file($importDirectory."/".$file) && ($file != "." && $file!="..") && (strcmp(strtolower(substr($file, -4, 4)), ".xml") == 0))
4252  {
4253  // found xml file
4254  $xmlFile = $importDirectory."/".$file;
4255  }
4256  }
4257  }
4258  return array("dir" => $importDirectory, "xml" => $xmlFile);
4259  }
4260 
4267  function importObject($file_info, $svy_qpl_id)
4268  {
4269  // check if file was uploaded
4270  $source = $file_info["tmp_name"];
4271  $error = "";
4272  if (($source == 'none') || (!$source) || $file_info["error"] > UPLOAD_ERR_OK)
4273  {
4274  $error = $this->lng->txt("import_no_file_selected");
4275  }
4276  // check correct file type
4277  $isXml = FALSE;
4278  $isZip = FALSE;
4279  if ((strcmp($file_info["type"], "text/xml") == 0) || (strcmp($file_info["type"], "application/xml") == 0))
4280  {
4281  $isXml = TRUE;
4282  }
4283  if (strpos($file_info["type"], "zip") !== FALSE)
4284  {
4285  $isZip = TRUE;
4286  }
4287  if (!$isXml && !$isZip)
4288  {
4289  $error = $this->lng->txt("import_wrong_file_type");
4290  global $ilLog;
4291  $ilLog->write("Survey: Import error. Filetype was \"" . $file_info["type"] ."\"");
4292  }
4293  if (strlen($error) == 0)
4294  {
4295  // import file as a survey
4296  $import_dir = $this->getImportDirectory();
4297  $import_subdir = "";
4298  $importfile = "";
4299  include_once "./Services/Utilities/classes/class.ilUtil.php";
4300  if ($isZip)
4301  {
4302  $importfile = $import_dir."/".$file_info["name"];
4303  ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
4304  ilUtil::unzip($importfile);
4305  $found = $this->locateImportFiles($import_dir);
4306  if (!((strlen($found["dir"]) > 0) && (strlen($found["xml"]) > 0)))
4307  {
4308  $error = $this->lng->txt("wrong_import_file_structure");
4309  return $error;
4310  }
4311  $importfile = $found["xml"];
4312  $import_subdir = $found["dir"];
4313  }
4314  else
4315  {
4316  $importfile = tempnam($import_dir, "survey_import");
4317  ilUtil::moveUploadedFile($source, $file_info["name"], $importfile);
4318  }
4319  $fh = fopen($importfile, "r");
4320  if (!$fh)
4321  {
4322  $error = $this->lng->txt("import_error_opening_file");
4323  return $error;
4324  }
4325  $xml = fread($fh, filesize($importfile));
4326  $result = fclose($fh);
4327  if (!$result)
4328  {
4329  $error = $this->lng->txt("import_error_closing_file");
4330  return $error;
4331  }
4332 
4333  unset($_SESSION["import_mob_xhtml"]);
4334  if (strpos($xml, "questestinterop"))
4335  {
4336  include_once "./Services/Survey/classes/class.SurveyImportParserPre38.php";
4337  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
4338  $spl = new ilObjSurveyQuestionPool($svy_qpl_id, FALSE);
4339  $import = new SurveyImportParserPre38($spl, "", TRUE);
4340  $import->setSurveyObject($this);
4341  $import->setXMLContent($xml);
4342  $import->startParsing();
4343  }
4344  else
4345  {
4346  include_once "./Services/Survey/classes/class.SurveyImportParser.php";
4347  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
4348  $spl = new ilObjSurveyQuestionPool($svy_qpl_id, FALSE);
4349  $import = new SurveyImportParser($spl, "", TRUE);
4350  $import->setSurveyObject($this);
4351  $import->setXMLContent($xml);
4352  $import->startParsing();
4353  }
4354 
4355  if (is_array($_SESSION["import_mob_xhtml"]))
4356  {
4357  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
4358  include_once "./Services/RTE/classes/class.ilRTE.php";
4359  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
4360  foreach ($_SESSION["import_mob_xhtml"] as $mob)
4361  {
4362  $importfile = $import_subdir . "/" . $mob["uri"];
4363  if (file_exists($importfile))
4364  {
4365  $media_object =& ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, FALSE);
4366  ilObjMediaObject::_saveUsage($media_object->getId(), "svy:html", $this->getId());
4367  $this->setIntroduction(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getIntroduction()));
4368  $this->setOutro(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $this->getOutro()));
4369  }
4370  else
4371  {
4372  global $ilLog;
4373  $ilLog->write("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
4374  }
4375  }
4378  $this->saveToDb();
4379  }
4380 
4381  // delete import directory
4383  }
4384  return $error;
4385  }
4386 
4395  public function cloneObject($a_target_id,$a_copy_id = 0)
4396  {
4397  global $ilDB;
4398 
4399  $this->loadFromDb();
4400 
4401  // Copy settings
4402  $newObj = parent::cloneObject($a_target_id,$a_copy_id);
4403  $this->cloneMetaData($newObj);
4404 
4405 
4406  $newObj->setAuthor($this->getAuthor());
4407  $newObj->setIntroduction($this->getIntroduction());
4408  $newObj->setOutro($this->getOutro());
4409  $newObj->setStatus($this->getStatus());
4410  $newObj->setEvaluationAccess($this->getEvaluationAccess());
4411  $newObj->setStartDate($this->getStartDate());
4412  $newObj->setStartDateEnabled($this->getStartDateEnabled());
4413  $newObj->setEndDate($this->getEndDate());
4414  $newObj->setEndDateEnabled($this->getEndDateEnabled());
4415  $newObj->setInvitation($this->getInvitation());
4416  $newObj->setInvitationMode($this->getInvitationMode());
4417  $newObj->setAnonymize($this->getAnonymize());
4418  $newObj->setShowQuestionTitles($this->getShowQuestionTitles());
4419 
4420 
4421  $question_pointer = array();
4422  // clone the questions
4423  $mapping = array();
4424  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
4425  foreach ($this->questions as $key => $question_id)
4426  {
4427  $question = ilObjSurvey::_instanciateQuestion($question_id);
4428  $question->id = -1;
4429  $original_id = SurveyQuestion::_getOriginalId($question_id);
4430  $question->saveToDb($original_id);
4431  $newObj->questions[$key] = $question->getId();
4432  $question_pointer[$question_id] = $question->getId();
4433  $mapping[$question_id] = $question->getId();
4434  }
4435 
4436  $newObj->saveToDb();
4437  $newObj->cloneTextblocks($mapping);
4438 
4439  // clone the questionblocks
4440  $questionblocks = array();
4441  $questionblock_questions = array();
4442  $result = $ilDB->queryF("SELECT * FROM svy_qblk_qst WHERE survey_fi = %s",
4443  array('integer'),
4444  array($this->getSurveyId())
4445  );
4446  if ($result->numRows() > 0)
4447  {
4448  while ($row = $ilDB->fetchAssoc($result))
4449  {
4450  array_push($questionblock_questions, $row);
4451  $questionblocks[$row["questionblock_fi"]] = $row["questionblock_fi"];
4452  }
4453  }
4454  // create new questionblocks
4455  foreach ($questionblocks as $key => $value)
4456  {
4457  $questionblock = ilObjSurvey::_getQuestionblock($key);
4458  $questionblock_id = ilObjSurvey::_addQuestionblock($questionblock["title"], $questionblock["owner_fi"]);
4459  $questionblocks[$key] = $questionblock_id;
4460  }
4461  // create new questionblock questions
4462  foreach ($questionblock_questions as $key => $value)
4463  {
4464  $next_id = $ilDB->nextId('svy_qblk_qst');
4465  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qblk_qst (qblk_qst_id, survey_fi, questionblock_fi, question_fi) ".
4466  "VALUES (%s, %s, %s, %s)",
4467  array('integer','integer','integer','integer'),
4468  array($next_id, $newObj->getSurveyId(), $questionblocks[$value["questionblock_fi"]], $question_pointer[$value["question_fi"]])
4469  );
4470  }
4471 
4472  // clone the constraints
4473  $constraints = ilObjSurvey::_getConstraints($this->getSurveyId());
4474  $newConstraints = array();
4475  foreach ($constraints as $key => $constraint)
4476  {
4477  if (!array_key_exists($constraint['id'], $newConstraints))
4478  {
4479  $constraint_id = $newObj->addConstraint($question_pointer[$constraint["question"]], $constraint["relation_id"], $constraint["value"], $constraint['conjunction']);
4480  $newConstraints[$constraint['id']] = $constraint_id;
4481  }
4482  $newObj->addConstraintToQuestion($question_pointer[$constraint["for_question"]], $newConstraints[$constraint['id']]);
4483  }
4484 
4485  // clone the obligatory states
4486  $result = $ilDB->queryF("SELECT * FROM svy_qst_oblig WHERE survey_fi = %s",
4487  array('integer'),
4488  array($this->getSurveyId())
4489  );
4490  if ($result->numRows() > 0)
4491  {
4492  while ($row = $ilDB->fetchAssoc($result))
4493  {
4494  $next_id = $ilDB->nextId('svy_qst_oblig');
4495  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_qst_oblig (question_obligatory_id, survey_fi, question_fi, ".
4496  "obligatory, tstamp) VALUES (%s, %s, %s, %s, %s)",
4497  array('integer','integer','integer','text','integer'),
4498  array($next_id, $newObj->getSurveyId(), $question_pointer[$row["question_fi"]], $row["obligatory"], time())
4499  );
4500  }
4501  }
4502  return $newObj;
4503  }
4504 
4505  function getTextblock($question_id)
4506  {
4507  global $ilDB;
4508  $result = $ilDB->queryF("SELECT * FROM svy_svy_qst WHERE question_fi = %s",
4509  array('integer'),
4510  array($question_id)
4511  );
4512  if ($result->numRows())
4513  {
4514  $row = $ilDB->fetchAssoc($result);
4515  return $row["heading"];
4516  }
4517  else
4518  {
4519  return "";
4520  }
4521  }
4522 
4528  function cloneTextblocks($mapping)
4529  {
4530  foreach ($mapping as $original_id => $new_id)
4531  {
4532  $textblock = $this->getTextblock($original_id);
4533  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
4534  $this->saveHeading(ilUtil::stripSlashes($textblock, TRUE, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("survey")), $new_id);
4535  }
4536  }
4537 
4544  {
4545  include_once "./Services/Utilities/classes/class.ilUtil.php";
4546  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
4547  ilUtil::makeDir($svy_data_dir);
4548  if(!is_writable($svy_data_dir))
4549  {
4550  $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
4551  .") not writeable.",$this->ilias->error_obj->FATAL);
4552  }
4553 
4554  // create learning module directory (data_dir/lm_data/lm_<id>)
4555  $svy_dir = $svy_data_dir."/svy_".$this->getId();
4556  ilUtil::makeDir($svy_dir);
4557  if(!@is_dir($svy_dir))
4558  {
4559  $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
4560  }
4561  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
4562  $export_dir = $svy_dir."/export";
4563  ilUtil::makeDir($export_dir);
4564  if(!@is_dir($export_dir))
4565  {
4566  $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
4567  }
4568  }
4569 
4574  {
4575  include_once "./Services/Utilities/classes/class.ilUtil.php";
4576  $export_dir = ilUtil::getDataDir()."/svy_data"."/svy_".$this->getId()."/export";
4577 
4578  return $export_dir;
4579  }
4580 
4585  {
4586  // quit if import dir not available
4587  if (!@is_dir($dir) or
4588  !is_writeable($dir))
4589  {
4590  return array();
4591  }
4592 
4593  // open directory
4594  $dir = dir($dir);
4595 
4596  // initialize array
4597  $file = array();
4598 
4599  // get files and save the in the array
4600  while ($entry = $dir->read())
4601  {
4602  if ($entry != "." and
4603  $entry != ".." and
4604  ereg("^[0-9]{10}_{2}[0-9]+_{2}(survey__)*[0-9]+\.xml|zip\$", $entry))
4605  {
4606  $file[] = $entry;
4607  }
4608  }
4609 
4610  // close import directory
4611  $dir->close();
4612  // sort files
4613  sort ($file);
4614  reset ($file);
4615 
4616  return $file;
4617  }
4618 
4625  {
4626  include_once "./Services/Utilities/classes/class.ilUtil.php";
4627  $svy_data_dir = ilUtil::getDataDir()."/svy_data";
4628  ilUtil::makeDir($svy_data_dir);
4629 
4630  if(!is_writable($svy_data_dir))
4631  {
4632  $this->ilias->raiseError("Survey Data Directory (".$svy_data_dir
4633  .") not writeable.",$this->ilias->error_obj->FATAL);
4634  }
4635 
4636  // create test directory (data_dir/svy_data/svy_<id>)
4637  $svy_dir = $svy_data_dir."/svy_".$this->getId();
4638  ilUtil::makeDir($svy_dir);
4639  if(!@is_dir($svy_dir))
4640  {
4641  $this->ilias->raiseError("Creation of Survey Directory failed.",$this->ilias->error_obj->FATAL);
4642  }
4643 
4644  // create import subdirectory (data_dir/svy_data/svy_<id>/import)
4645  $import_dir = $svy_dir."/import";
4646  ilUtil::makeDir($import_dir);
4647  if(!@is_dir($import_dir))
4648  {
4649  $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
4650  }
4651  }
4652 
4657  {
4658  include_once "./Services/Utilities/classes/class.ilUtil.php";
4659  $import_dir = ilUtil::getDataDir()."/svy_data".
4660  "/svy_".$this->getId()."/import";
4661  if (!is_dir($import_dir))
4662  {
4663  ilUtil::makeDirParents($import_dir);
4664  }
4665  if(@is_dir($import_dir))
4666  {
4667  return $import_dir;
4668  }
4669  else
4670  {
4671  return false;
4672  }
4673  }
4674 
4675  function saveHeading($heading = "", $insertbefore)
4676  {
4677  global $ilDB;
4678  if ($heading)
4679  {
4680  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy_qst SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
4681  array('text','integer','integer'),
4682  array($heading, $this->getSurveyId(), $insertbefore)
4683  );
4684  }
4685  else
4686  {
4687  $affectedRows = $ilDB->manipulateF("UPDATE svy_svy_qst SET heading=%s WHERE survey_fi=%s AND question_fi=%s",
4688  array('text','integer','integer'),
4689  array(NULL, $this->getSurveyId(), $insertbefore)
4690  );
4691  }
4692  }
4693 
4694  function isAnonymousKey($key)
4695  {
4696  global $ilDB;
4697 
4698  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_key = %s AND survey_fi = %s",
4699  array('text','integer'),
4700  array($key, $this->getSurveyId())
4701  );
4702  return ($result->numRows() == 1) ? true : false;
4703  }
4704 
4705  function getUserSurveyCode($user_id)
4706  {
4707  global $ilDB;
4708 
4709  if (($user_id == ANONYMOUS_USER_ID) || (($this->isAccessibleWithoutCode() && $this->isAllowedToTakeMultipleSurveys()))) return "";
4710  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_finished WHERE survey_fi = %s AND user_fi = %s",
4711  array('integer','integer'),
4712  array($this->getSurveyId(), $user_id)
4713  );
4714  if ($result->numRows() == 1)
4715  {
4716  $row = $ilDB->fetchAssoc($result);
4717  return $row["anonymous_id"];
4718  }
4719  else
4720  {
4721  return "";
4722  }
4723  }
4724 
4726  {
4727  global $ilDB;
4728 
4729  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE anonymous_id = %s AND survey_fi = %s",
4730  array('text','integer'),
4731  array($key, $this->getSurveyId())
4732  );
4733  return ($result->numRows() == 1) ? true : false;
4734  }
4735 
4736  function checkSurveyCode($code)
4737  {
4738  if ($this->isAnonymousKey($code))
4739  {
4740  if ($this->isSurveyStarted("", $code) == 1)
4741  {
4742  return false;
4743  }
4744  else
4745  {
4746  return true;
4747  }
4748  }
4749  else
4750  {
4751  return false;
4752  }
4753  }
4754 
4762  {
4763  global $ilDB;
4764 
4765  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_fi = %s AND user_key IS NULL",
4766  array('integer'),
4767  array($this->getSurveyId())
4768  );
4769  return $result->numRows();
4770  }
4771 
4779  function getSurveyCodesForExport($a_array)
4780  {
4781  global $ilDB, $ilUser;
4782 
4783  $result = $ilDB->queryF("SELECT svy_anonymous.*, svy_finished.state FROM svy_anonymous ".
4784  "LEFT JOIN svy_finished ON svy_anonymous.survey_key = svy_finished.anonymous_id ".
4785  "WHERE svy_anonymous.survey_fi = %s AND svy_anonymous.user_key IS NULL",
4786  array('integer'),
4787  array($this->getSurveyId())
4788  );
4789  $export = "";
4790  $default_lang = $ilUser->getPref("survey_code_language");
4791  $lang = (strlen($default_lang)) ? "&lang=" . $default_lang : "";
4792  while ($row = $ilDB->fetchAssoc($result))
4793  {
4794  if (in_array($row["survey_key"], $a_array) || (count($a_array) == 0))
4795  {
4796  $export .= $row["survey_key"] . ",";
4797 
4798  // No relative (today, tomorrow...) dates in export.
4799  $date = new ilDate($row['tstamp'],IL_CAL_UNIX);
4800  $created = $date->get(IL_CAL_DATE);
4801  $export .= "$created,";
4802  if ($this->isSurveyCodeUsed($row["survey_key"]))
4803  {
4804  $export .= "1,";
4805  }
4806  else
4807  {
4808  $export .= "0,";
4809  }
4810  $url = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&client_id=" . CLIENT_ID . "&accesscode=".$row["survey_key"].$lang;
4811  $export .= $url . "\n";
4812  }
4813  }
4814  return $export;
4815  }
4816 
4824  public function &getSurveyCodesTableData($lang = "en")
4825  {
4826  global $ilDB;
4827 
4828  include_once "./classes/class.ilFormat.php";
4829  if (strlen($lang) == 0) $lang = "en";
4830 
4831  $order = "ORDER BY tstamp, survey_key ASC";
4832  $codes = array();
4833  $result = $ilDB->queryF("SELECT svy_anonymous.anonymous_id, svy_anonymous.survey_key, svy_anonymous.survey_fi, ".
4834  "svy_anonymous.tstamp, svy_finished.state FROM svy_anonymous LEFT JOIN svy_finished ".
4835  "ON svy_anonymous.survey_key = svy_finished.anonymous_id WHERE svy_anonymous.survey_fi = %s ".
4836  "AND svy_anonymous.user_key IS NULL $order",
4837  array('integer'),
4838  array($this->getSurveyId())
4839  );
4840  if ($result->numRows() > 0)
4841  {
4842  while ($row = $ilDB->fetchAssoc($result))
4843  {
4844  $created = ilDatePresentation::formatDate(new ilDateTime($row["tstamp"],IL_CAL_UNIX));
4845  $url = "";
4846 
4847  $state = 0;
4848  if ($this->isSurveyCodeUsed($row["survey_key"]))
4849  {
4850  $state = 1;
4851  }
4852  else
4853  {
4854  $addlang = "";
4855  if (strlen($lang))
4856  {
4857  $addlang = "&amp;lang=$lang";
4858  }
4859  $href = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$row["survey_key"].$addlang;
4860  $url = $this->lng->txt("survey_code_url_name");
4861  }
4862  array_push($codes, array('code' => $row["survey_key"], 'date' => $created, 'used' => $state, 'url' => $url, 'href' => $href));
4863  }
4864  }
4865  return $codes;
4866  }
4867 
4868  function isSurveyCodeUsed($code)
4869  {
4870  global $ilDB;
4871  $result = $ilDB->queryF("SELECT finished_id FROM svy_finished WHERE survey_fi = %s AND anonymous_id = %s",
4872  array('integer','text'),
4873  array($this->getSurveyId(), $code)
4874  );
4875  return ($result->numRows() > 0) ? true : false;
4876  }
4877 
4878  function isSurveyCodeUnique($code)
4879  {
4880  global $ilDB;
4881  $result = $ilDB->queryF("SELECT anonymous_id FROM svy_anonymous WHERE survey_fi = %s AND survey_key = %s",
4882  array('integer','text'),
4883  array($this->getSurveyId(), $code)
4884  );
4885  return ($result->numRows() > 0) ? false : true;
4886  }
4887 
4888  function createSurveyCodes($nrOfCodes)
4889  {
4890  global $ilDB;
4891  for ($i = 0; $i < $nrOfCodes; $i++)
4892  {
4893  $anonymize_key = $this->createNewAccessCode();
4894  $next_id = $ilDB->nextId('svy_anonymous');
4895  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_anonymous (anonymous_id, survey_key, survey_fi, tstamp) ".
4896  "VALUES (%s, %s, %s, %s)",
4897  array('integer','text','integer','integer'),
4898  array($next_id, $anonymize_key, $this->getSurveyId(), time())
4899  );
4900  }
4901  }
4902 
4904  {
4905  global $ilDB;
4906  foreach ($data as $dataset)
4907  {
4908  $anonymize_key = $this->createNewAccessCode();
4909  $next_id = $ilDB->nextId('svy_anonymous');
4910  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_anonymous (anonymous_id, survey_key, survey_fi, externaldata, tstamp) ".
4911  "VALUES (%s, %s, %s, %s, %s)",
4912  array('integer','text','integer','text','integer'),
4913  array($next_id, $anonymize_key, $this->getSurveyId(), serialize($dataset), time())
4914  );
4915  }
4916  }
4917 
4918  function sendCodes($not_sent, $subject, $message)
4919  {
4920  include_once "./Services/Mail/classes/class.ilMail.php";
4921  $user_id = $this->getOwner();
4922  $mail = new ilMail($user_id);
4923  $recipients = $this->getExternalCodeRecipients();
4924  foreach ($recipients as $data)
4925  {
4926  $messagetext = $message;
4927  $url = ILIAS_HTTP_PATH."/goto.php?cmd=infoScreen&target=svy_".$this->getRefId() . "&amp;client_id=" . CLIENT_ID . "&amp;accesscode=".$data["code"];
4928  $messagetext = str_replace('[url]', "<" . $url . ">", $messagetext);
4929  foreach ($data as $key => $value)
4930  {
4931  $messagetext = str_replace('[' . $key . ']', $value, $messagetext);
4932  }
4933  if (($not_sent != 1) || $data['sent'] == 0)
4934  {
4935  $res = $mail->sendMail(
4936  $data['email'], // to
4937  "", // cc
4938  "", // bcc
4939  $subject, // subject
4940  $messagetext, // message
4941  array(), // attachments
4942  array('normal') // type
4943  );
4944  }
4945  }
4946 
4947  global $ilDB;
4948  $affectedRows = $ilDB->manipulateF("UPDATE svy_anonymous SET sent = %s WHERE survey_fi = %s AND externaldata IS NOT NULL",
4949  array('integer','integer'),
4950  array(1, $this->getSurveyId())
4951  );
4952  }
4953 
4955  {
4956  global $ilDB;
4957  $result = $ilDB->queryF("SELECT survey_key code, externaldata, sent FROM svy_anonymous WHERE survey_fi = %s AND externaldata IS NOT NULL",
4958  array('integer'),
4959  array($this->getSurveyId())
4960  );
4961  $res = array();
4962  while ($row = $ilDB->fetchAssoc($result))
4963  {
4964  $externaldata = unserialize($row['externaldata']);
4965  $externaldata['code'] = $row['code'];
4966  $externaldata['sent'] = $row['sent'];
4967  array_push($res, $externaldata);
4968  }
4969  return $res;
4970  }
4971 
4977  function deleteSurveyCode($survey_code)
4978  {
4979  global $ilDB;
4980 
4981  if (strlen($survey_code) > 0)
4982  {
4983  $affectedRows = $ilDB->manipulateF("DELETE FROM svy_anonymous WHERE survey_fi = %s AND survey_key = %s",
4984  array('integer', 'text'),
4985  array($this->getSurveyId(), $survey_code)
4986  );
4987  }
4988  }
4989 
4996  function getUserAccessCode($user_id)
4997  {
4998  global $ilDB;
4999  $access_code = "";
5000  $result = $ilDB->queryF("SELECT survey_key FROM svy_anonymous WHERE survey_fi = %s AND user_key = %s",
5001  array('integer','text'),
5002  array($this->getSurveyId(), md5($user_id))
5003  );
5004  if ($result->numRows())
5005  {
5006  $row = $ilDB->fetchAssoc($result);
5007  $access_code = $row["survey_key"];
5008  }
5009  return $access_code;
5010  }
5011 
5018  function saveUserAccessCode($user_id, $access_code)
5019  {
5020  global $ilDB;
5021  $next_id = $ilDB->nextId('svy_anonymous');
5022  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_anonymous (anonymous_id, survey_key, survey_fi, user_key, tstamp) ".
5023  "VALUES (%s, %s, %s, %s, %s)",
5024  array('integer','text', 'integer', 'text', 'integer'),
5025  array($next_id, $access_code, $this->getSurveyId(), md5($user_id), time())
5026  );
5027  }
5028 
5035  {
5036  // create a 5 character code
5037  $codestring = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
5038  mt_srand();
5039  $code = "";
5040  for ($i = 1; $i <=5; $i++)
5041  {
5042  $index = mt_rand(0, strlen($codestring)-1);
5043  $code .= substr($codestring, $index, 1);
5044  }
5045  // verify it against the database
5046  while (!$this->isSurveyCodeUnique($code))
5047  {
5048  $code = $this->createNewAccessCode();
5049  }
5050  return $code;
5051  }
5052 
5053 
5064  function &processCSVRow($row, $quoteAll = FALSE, $separator = ";")
5065  {
5066  $resultarray = array();
5067  foreach ($row as $rowindex => $entry)
5068  {
5069  if(is_array($entry))
5070  {
5071  $entry = implode("/", $entry);
5072  }
5073  $surround = FALSE;
5074  if ($quoteAll)
5075  {
5076  $surround = TRUE;
5077  }
5078  if (strpos($entry, "\"") !== FALSE)
5079  {
5080  $entry = str_replace("\"", "\"\"", $entry);
5081  $surround = TRUE;
5082  }
5083  if (strpos($entry, $separator) !== FALSE)
5084  {
5085  $surround = TRUE;
5086  }
5087  // replace all CR LF with LF (for Excel for Windows compatibility
5088  $entry = str_replace(chr(13).chr(10), chr(10), $entry);
5089  if ($surround)
5090  {
5091  $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
5092  }
5093  else
5094  {
5095  $resultarray[$rowindex] = utf8_decode($entry);
5096  }
5097  }
5098  return $resultarray;
5099  }
5100 
5101  function _getLastAccess($finished_id)
5102  {
5103  global $ilDB;
5104 
5105  $result = $ilDB->queryF("SELECT tstamp FROM svy_answer WHERE active_fi = %s ORDER BY tstamp DESC",
5106  array('integer'),
5107  array($finished_id)
5108  );
5109  if ($result->numRows())
5110  {
5111  $row = $ilDB->fetchAssoc($result);
5112  return $row["tstamp"];
5113  }
5114  else
5115  {
5116  $result = $ilDB->queryF("SELECT tstamp FROM svy_finished WHERE finished_id = %s",
5117  array('integer'),
5118  array($finished_id)
5119  );
5120  if ($result->numRows())
5121  {
5122  $row = $ilDB->fetchAssoc($result);
5123  return $row["tstamp"];
5124  }
5125  }
5126  return "";
5127  }
5128 
5135  function prepareTextareaOutput($txt_output)
5136  {
5137  include_once "./Services/Utilities/classes/class.ilUtil.php";
5138  return ilUtil::prepareTextareaOutput($txt_output, $prepare_for_latex_output);
5139  }
5140 
5148  function isHTML($a_text)
5149  {
5150  if (preg_match("/<[^>]*?>/", $a_text))
5151  {
5152  return TRUE;
5153  }
5154  else
5155  {
5156  return FALSE;
5157  }
5158  }
5159 
5168  function addMaterialTag(&$a_xml_writer, $a_material, $close_material_tag = TRUE, $add_mobs = TRUE, $attribs = NULL)
5169  {
5170  include_once "./Services/RTE/classes/class.ilRTE.php";
5171  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
5172 
5173  $a_xml_writer->xmlStartTag("material", $attribs);
5174  $attrs = array(
5175  "type" => "text/plain"
5176  );
5177  if ($this->isHTML($a_material))
5178  {
5179  $attrs["type"] = "text/xhtml";
5180  }
5181  $mattext = ilRTE::_replaceMediaObjectImageSrc($a_material, 0);
5182  $a_xml_writer->xmlElement("mattext", $attrs, $mattext);
5183 
5184  if ($add_mobs)
5185  {
5186  $mobs = ilObjMediaObject::_getMobsOfObject("svy:html", $this->getId());
5187  foreach ($mobs as $mob)
5188  {
5189  $mob_id = "il_" . IL_INST_ID . "_mob_" . $mob;
5190  if (strpos($mattext, $mob_id) !== FALSE)
5191  {
5192  $mob_obj =& new ilObjMediaObject($mob);
5193  $imgattrs = array(
5194  "label" => $mob_id,
5195  "uri" => "objects/" . "il_" . IL_INST_ID . "_mob_" . $mob . "/" . $mob_obj->getTitle()
5196  );
5197  $a_xml_writer->xmlElement("matimage", $imgattrs, NULL);
5198  }
5199  }
5200  }
5201  if ($close_material_tag) $a_xml_writer->xmlEndTag("material");
5202  }
5203 
5213  {
5214  if ($this->getAnonymize() != ANONYMIZE_OFF)
5215  {
5216  if ($this->surveyCodeSecurity == FALSE)
5217  {
5218  return TRUE;
5219  }
5220  }
5221  return FALSE;
5222  }
5223 
5224  function _checkCondition($a_svy_id,$a_operator,$a_value,$a_usr_id = 0)
5225  {
5226  global $ilUser;
5227 
5228  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
5229 
5230  switch($a_operator)
5231  {
5232  case 'finished':
5233  //if (ilExerciseMembers::_lookupStatus($a_exc_id, $ilias->account->getId()) == "passed")
5234  include_once("./Modules/Survey/classes/class.ilObjSurveyAccess.php");
5235  if (ilObjSurveyAccess::_lookupFinished($a_svy_id, $a_usr_id))
5236  {
5237  return true;
5238  }
5239  else
5240  {
5241  return false;
5242  }
5243  break;
5244 
5245  default:
5246  return true;
5247  }
5248  return true;
5249  }
5250 
5257  function isPluginActive($a_pname)
5258  {
5259  global $ilPluginAdmin;
5260  if ($ilPluginAdmin->isActive(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $a_pname))
5261  {
5262  return TRUE;
5263  }
5264  else
5265  {
5266  return FALSE;
5267  }
5268  }
5269 
5275  public function setSurveyId($survey_id)
5276  {
5277  $this->survey_id = $survey_id;
5278  }
5279 
5286  public function &getUserData($ids)
5287  {
5288  global $ilDB;
5289 
5290  if (!is_array($ids) || count($ids) ==0) return array();
5291 
5292  $result = $ilDB->query("SELECT usr_id, login, lastname, firstname FROM usr_data WHERE " . $ilDB->in('usr_id', $ids, false, 'integer') . " ORDER BY login");
5293  $result_array = array();
5294  while ($row = $ilDB->fetchAssoc($result))
5295  {
5296  $result_array[$row["usr_id"]]= $row;
5297  }
5298  return $result_array;
5299  }
5300 
5301  function &getGroupData($ids)
5302  {
5303  if (!is_array($ids) || count($ids) ==0) return array();
5304  $result = array();
5305  foreach ($ids as $ref_id)
5306  {
5307  $obj_id = ilObject::_lookupObjId($ref_id);
5308  $result[$ref_id] = array("ref_id" => $ref_id, "title" => ilObject::_lookupTitle($obj_id), "description" => ilObject::_lookupDescription($obj_id));
5309  }
5310  return $result;
5311  }
5312 
5313  function &getRoleData($ids)
5314  {
5315  if (!is_array($ids) || count($ids) ==0) return array();
5316  $result = array();
5317  foreach ($ids as $obj_id)
5318  {
5319  $result[$obj_id] = array("obj_id" => $obj_id, "title" => ilObject::_lookupTitle($obj_id), "description" => ilObject::_lookupDescription($obj_id));
5320  }
5321  return $result;
5322  }
5323 
5325  {
5326  return $this->mailnotification;
5327  }
5328 
5329  function setMailNotification($a_notification)
5330  {
5331  $this->mailnotification = ($a_notification) ? true : false;
5332  }
5333 
5334  function getMailAddresses()
5335  {
5336  return $this->mailaddresses;
5337  }
5338 
5339  function setMailAddresses($a_addresses)
5340  {
5341  $this->mailaddresses = $a_addresses;
5342  }
5343 
5345  {
5347  }
5348 
5349  function setMailParticipantData($a_data)
5350  {
5351  $this->mailparticipantdata = $a_data;
5352  }
5353 
5354  public function getSurveyTimes()
5355  {
5356  global $ilDB;
5357 
5358  $result = $ilDB->queryF("SELECT * FROM svy_times, svy_finished WHERE svy_finished.survey_fi = %s",
5359  array('integer'),
5360  array($this->getId())
5361  );
5362  $times = array();;
5363  while ($row = $ilDB->fetchAssoc($result))
5364  {
5365  if (strlen($row['left_page']) && strlen($row['entered_page']))
5366  $times[$row['finished_fi']] += ($row['left_page']-$row['entered_page']);
5367  }
5368  return $times;
5369  }
5370 
5371  function setStartTime($finished_id, $first_question)
5372  {
5373  global $ilDB;
5374  $time = time();
5375  $_SESSION['svy_entered_page'] = $time;
5376  $affectedRows = $ilDB->manipulateF("INSERT INTO svy_times (finished_fi, entered_page, left_page, first_question) VALUES (%s, %s, %s, %s)",
5377  array('integer', 'integer', 'integer', 'integer'),
5378  array($finished_id, $time, NULL, $first_question)
5379  );
5380  }
5381 
5382  function setEndTime($finished_id)
5383  {
5384  global $ilDB;
5385  $time = time();
5386  $affectedRows = $ilDB->manipulateF("UPDATE svy_times SET left_page = %s WHERE finished_fi = %s AND entered_page = %s",
5387  array('integer', 'integer', 'integer'),
5388  array($time, $finished_id, $_SESSION['svy_entered_page'])
5389  );
5390  unset($_SESSION['svy_entered_page']);
5391  }
5392 
5393  function getWorkingtimeForParticipant($finished_id)
5394  {
5395  global $ilDB;
5396 
5397  $result = $ilDB->queryF("SELECT * FROM svy_times WHERE finished_fi = %s",
5398  array('integer'),
5399  array($finished_id)
5400  );
5401  $total = 0;
5402  while ($row = $ilDB->fetchAssoc($result))
5403  {
5404  if ($row['left_page'] > 0 && $row['entered_page'] > 0)
5405  $total += $row['left_page'] - $row['entered_page'];
5406  }
5407  return $total;
5408  }
5409 
5410 } // END class.ilObjSurvey
5411 ?>