ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSurveyExecutionGUI.php
Go to the documentation of this file.
1 <?php
2  /*
3  +----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 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 
36 {
37  var $object;
38  var $lng;
39  var $tpl;
40  var $ctrl;
41  var $ilias;
42  var $tree;
43  var $preview;
44 
53  function ilSurveyExecutionGUI($a_object)
54  {
55  global $lng, $tpl, $ilCtrl, $ilias, $tree;
56 
57  $this->lng =& $lng;
58  $this->tpl =& $tpl;
59  $this->ctrl =& $ilCtrl;
60  $this->ilias =& $ilias;
61  $this->object =& $a_object;
62  $this->tree =& $tree;
63 
64  $this->external_rater_360 = false;
65  if($this->object->get360Mode() &&
66  $_SESSION["anonymous_id"][$this->object->getId()] &&
67  ilObjSurvey::validateExternalRaterCode($this->object->getRefId(),
68  $_SESSION["anonymous_id"][$this->object->getId()]))
69  {
70  $this->external_rater_360 = true;
71  }
72 
73  // stay in preview mode
74  $this->preview = (bool)$_REQUEST["prvw"];
75  $this->ctrl->saveParameter($this, "prvw");
76  $this->ctrl->saveParameter($this, "pgov");
77  }
78 
82  function &executeCommand()
83  {
84  $cmd = $this->ctrl->getCmd();
85  $next_class = $this->ctrl->getNextClass($this);
86 
87  $cmd = $this->getCommand($cmd);
88  if (strlen($cmd) == 0)
89  {
90  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
91  $this->ctrl->redirect($this, "gotoPage");
92  }
93  switch($next_class)
94  {
95  default:
96  $ret =& $this->$cmd();
97  break;
98  }
99  return $ret;
100  }
101 
102  protected function checkAuth($a_may_start = false)
103  {
104  global $rbacsystem, $ilUser;
105 
106  if($this->preview)
107  {
108  if(!$rbacsystem->checkAccess("write", $this->object->ref_id))
109  {
110  // only with write access it is possible to preview the survey
111  $this->ilias->raiseError($this->lng->txt("survey_cannot_preview_survey"),$this->ilias->error_obj->MESSAGE);
112  }
113 
114  return true;
115  }
116 
117  if (!$this->external_rater_360 &&
118  !$rbacsystem->checkAccess("read", $this->object->ref_id))
119  {
120  // only with read access it is possible to run the test
121  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
122  }
123 
124  $user_id = $ilUser->getId();
125 
126  // check existing code
127  // see ilObjSurveyGUI::infoScreen()
128  $anonymous_id = $anonymous_code = null;
129  if ($this->object->getAnonymize() || !$this->object->isAccessibleWithoutCode())
130  {
131  $anonymous_code = $_SESSION["anonymous_id"][$this->object->getId()];
132  $anonymous_id = $this->object->getAnonymousIdByCode($anonymous_code);
133  if(!$anonymous_id)
134  {
135  ilUtil::sendFailure(sprintf($this->lng->txt("error_retrieving_anonymous_survey"), $anonymous_code, true));
136  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
137  }
138  }
139 
140  // appraisee validation
141  $appr_id = 0;
142  if($this->object->get360Mode())
143  {
144  $appr_id = $_REQUEST["appr_id"];
145  if(!$appr_id)
146  {
147  $appr_id = $_SESSION["appr_id"][$this->object->getId()];
148  }
149  // check if appraisee is valid
150  if($anonymous_id)
151  {
152  $appraisees = $this->object->getAppraiseesToRate(0, $anonymous_id);
153  }
154  if(!$appraisees && $user_id != ANONYMOUS_USER_ID)
155  {
156  $appraisees = $this->object->getAppraiseesToRate($user_id);
157  }
158  if(!in_array($appr_id, $appraisees))
159  {
160  ilUtil::sendFailure($this->lng->txt("survey_360_execution_invalid_appraisee"), true);
161  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
162  }
163  }
164  $_SESSION["appr_id"][$this->object->getId()] = $appr_id;
165 
166  $status = $this->object->isSurveyStarted($user_id, $anonymous_code, $appr_id);
167  // completed
168  if($status === 1)
169  {
170  ilUtil::sendFailure($this->lng->txt("already_completed_survey"), true);
171  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
172  }
173  // starting
174  else if ($status === false)
175  {
176  if($a_may_start)
177  {
178  $_SESSION["finished_id"][$this->object->getId()] =
179  $this->object->startSurvey($user_id, $anonymous_code, $appr_id);
180  }
181  else
182  {
183  ilUtil::sendFailure($this->lng->txt("survey_use_start_button"), true);
184  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
185  }
186  }
187  // resuming
188  else
189  {
190  // nothing todo
191  }
192 
193  // validate finished id
194  if($this->object->getActiveID($user_id, $anonymous_code, $appr_id) !=
195  $_SESSION["finished_id"][$this->object->getId()])
196  {
197  ilUtil::sendFailure($this->lng->txt("cannot_read_survey"), true);
198  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
199  }
200  }
201 
209  function getCommand($cmd)
210  {
211  return $cmd;
212  }
213 
221  function resume()
222  {
223  $this->start(true);
224  }
225 
233  function start($resume = false)
234  {
235  if($this->preview)
236  {
237  unset($_SESSION["preview_data"]);
238  }
239  unset($_SESSION["svy_errors"]);
240 
241  $this->checkAuth(!$resume);
242 
243  $activepage = "";
244  if ($resume)
245  {
246  $activepage = $this->object->getLastActivePage($_SESSION["finished_id"][$this->object->getId()]);
247  }
248 
249  if (strlen($activepage))
250  {
251  $this->ctrl->setParameter($this, "qid", $activepage);
252  }
253  $this->ctrl->setParameter($this, "activecommand", "default");
254  $this->ctrl->redirect($this, "redirectQuestion");
255  }
256 
263  function redirectQuestion()
264  {
265  switch ($_GET["activecommand"])
266  {
267  case "next":
268  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
269  break;
270  case "previous":
271  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
272  break;
273  case "gotoPage":
274  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
275  break;
276  case "default":
277  $this->outSurveyPage($_GET["qid"]);
278  break;
279  default:
280  // don't save input, go to the first page
281  $this->outSurveyPage();
282  break;
283  }
284  }
285 
286 
294  function previous()
295  {
296  $result = $this->saveUserInput("previous");
297  $this->ctrl->setParameter($this, "activecommand", "previous");
298  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
299  if (strlen($result))
300  {
301  $this->ctrl->setParameter($this, "direction", "0");
302  }
303  else
304  {
305  $this->ctrl->setParameter($this, "direction", "-1");
306  }
307  $this->ctrl->redirect($this, "redirectQuestion");
308  }
309 
315  function next()
316  {
317  $result = $this->saveUserInput("next");
318  $this->ctrl->setParameter($this, "activecommand", "next");
319  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
320  if (strlen($result))
321  {
322  $this->ctrl->setParameter($this, "direction", "0");
323  }
324  else
325  {
326  $this->ctrl->setParameter($this, "direction", "1");
327  }
328  $this->ctrl->redirect($this, "redirectQuestion");
329  }
330 
336  function gotoPage()
337  {
338  $this->ctrl->setParameter($this, "activecommand", "gotoPage");
339  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
340  $this->ctrl->setParameter($this, "direction", "0");
341  $this->ctrl->redirect($this, "redirectQuestion");
342  }
343 
351  function outSurveyPage($activepage = NULL, $direction = NULL)
352  {
353  global $ilUser;
354 
355  $this->checkAuth();
356 
357  $page = $this->object->getNextPage($activepage, $direction);
358  $constraint_true = 0;
359 
360  // check for constraints
361  if (count($page[0]["constraints"]))
362  {
363  while (is_array($page) and ($constraint_true == 0) and (count($page[0]["constraints"])))
364  {
365  $constraint_true = ($page[0]['constraints'][0]['conjunction'] == 0) ? true : false;
366  foreach ($page[0]["constraints"] as $constraint)
367  {
368  if(!$this->preview)
369  {
370  $working_data = $this->object->loadWorkingData($constraint["question"], $_SESSION["finished_id"][$this->object->getId()]);
371  }
372  else
373  {
374  $working_data = $_SESSION["preview_data"][$this->object->getId()][$constraint["question"]];
375  }
376  if ($constraint['conjunction'] == 0)
377  {
378  // and
379  $constraint_true = $constraint_true & $this->object->checkConstraint($constraint, $working_data);
380  }
381  else
382  {
383  // or
384  $constraint_true = $constraint_true | $this->object->checkConstraint($constraint, $working_data);
385  }
386  }
387  if ($constraint_true == 0)
388  {
389  // #11047 - we are skipping the page, so we have to get rid of existing answers for that question(s)
390  foreach($page as $page_question)
391  {
392  $qid = $page_question["question_id"];
393 
394  // see saveActiveQuestionData()
395  if(!$this->preview)
396  {
397  $this->object->deleteWorkingData($qid, $_SESSION["finished_id"][$this->object->getId()]);
398  }
399  else
400  {
401  $_SESSION["preview_data"][$this->object->getId()][$qid] = null;
402  }
403  }
404 
405  $page = $this->object->getNextPage($page[0]["question_id"], $direction);
406  }
407  }
408  }
409 
410  $first_question = -1;
411  if ($page === 0)
412  {
413  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
414  }
415  else if ($page === 1)
416  {
417  $this->object->finishSurvey($_SESSION["finished_id"][$this->object->getId()]);
418 
419  if ($this->object->getMailNotification())
420  {
421  $this->object->sendNotificationMail($ilUser->getId(),
422  $_SESSION["anonymous_id"][$this->object->getId()],
423  $_SESSION["appr_id"][$this->object->getId()]);
424  }
425 
426  /*
427  unset($_SESSION["anonymous_id"][$this->object->getId()]);
428  unset($_SESSION["appr_id"][$this->object->getId()]);
429  unset($_SESSION["finished_id"][$this->object->getId()]);
430  */
431 
432  $this->runShowFinishedPage();
433  return;
434  }
435  else
436  {
437  global $ilHelp;
438  $ilHelp->setScreenIdComponent("svy");
439  $ilHelp->setScreenId("quest_presentation");
440 
441  $required = false;
442  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_content.html", "Modules/Survey");
443 
444  if($this->object->get360Mode())
445  {
446  $appr_id = $_SESSION["appr_id"][$this->object->getId()];
447 
448  include_once "Services/User/classes/class.ilUserUtil.php";
449  $this->tpl->setTitle($this->object->getTitle()." (".
450  $this->lng->txt("survey_360_appraisee").": ".
451  ilUserUtil::getNamePresentation($appr_id).")");
452  }
453 
454  if (!($this->object->getAnonymize() && $this->object->isAccessibleWithoutCode() && ($ilUser->getId() == ANONYMOUS_USER_ID)))
455  {
456  $this->tpl->setCurrentBlock("suspend_survey");
457 
458  if(!$this->preview)
459  {
460  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("cancel_survey"));
461  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
462  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
463  }
464  else
465  {
466  $this->ctrl->setParameterByClass("ilObjSurveyGUI", "pgov", $_REQUEST["pgov"]);
467  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("survey_cancel_preview"));
468  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass(array("ilObjSurveyGUI", "ilSurveyEditorGUI"), "questions"));
469  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass(array("ilObjSurveyGUI", "ilSurveyEditorGUI"), "questions"));
470  }
471 
472  $this->tpl->setVariable("ALT_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
473  $this->tpl->setVariable("TITLE_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
474  $this->tpl->setVariable("IMG_SUSPEND", ilUtil::getImagePath("cancel.png"));
475  $this->tpl->parseCurrentBlock();
476  }
477  $this->outNavigationButtons("top", $page);
478  $this->tpl->setCurrentBlock("percentage");
479  $percentage = (int)(($page[0]["position"])*100);
480  $this->tpl->setVariable("PERCENT_BAR_START", ilUtil::getImagePath("bar_start.png"));
481  $this->tpl->setVariable("PERCENT_BAR_FILLED", ilUtil::getImagePath("bar_filled.png"));
482  $this->tpl->setVariable("PERCENT_BAR_EMPTY", ilUtil::getImagePath("bar_empty.png"));
483  $this->tpl->setVariable("PERCENT_BAR_END", ilUtil::getImagePath("bar_end.png"));
484  $this->tpl->setVariable("PERCENTAGE_ALT", $this->lng->txt("percentage"));
485  $this->tpl->setVariable("PERCENTAGE_VALUE", $percentage);
486  $this->tpl->setVariable("PERCENTAGE_UNFINISHED", 100-$percentage);
487  $this->tpl->parseCurrentBlock();
488  if (count($page) > 1 && $page[0]["questionblock_show_blocktitle"])
489  {
490  $this->tpl->setCurrentBlock("questionblock_title");
491  $this->tpl->setVariable("TEXT_QUESTIONBLOCK_TITLE", $page[0]["questionblock_title"]);
492  $this->tpl->parseCurrentBlock();
493  }
494  foreach ($page as $data)
495  {
496  $this->tpl->setCurrentBlock("survey_content");
497  if ($data["heading"])
498  {
499  $this->tpl->setVariable("QUESTION_HEADING", $data["heading"]);
500  }
501  if ($first_question == -1) $first_question = $data["question_id"];
502  $question_gui = $this->object->getQuestionGUI($data["type_tag"], $data["question_id"]);
503  if (is_array($_SESSION["svy_errors"]))
504  {
505  $working_data =& $question_gui->object->getWorkingDataFromUserInput($_SESSION["postdata"]);
506  }
507  else
508  {
509  $working_data = $this->object->loadWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
510  }
511  $question_gui->object->setObligatory($data["obligatory"]);
512  $error_messages = array();
513  if (is_array($_SESSION["svy_errors"]))
514  {
515  $error_messages = $_SESSION["svy_errors"];
516  }
517  $show_questiontext = ($data["questionblock_show_questiontext"]) ? 1 : 0;
518  $question_output = $question_gui->getWorkingForm($working_data, $this->object->getShowQuestionTitles(), $show_questiontext, $error_messages[$data["question_id"]], $this->object->getSurveyId());
519  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
520  $this->ctrl->setParameter($this, "qid", $data["question_id"]);
521  $this->tpl->parse("survey_content");
522  if ($data["obligatory"]) $required = true;
523  }
524  if ($required)
525  {
526  $this->tpl->setCurrentBlock("required");
527  $this->tpl->setVariable("TEXT_REQUIRED", $this->lng->txt("required_field"));
528  $this->tpl->parseCurrentBlock();
529  }
530 
531  $this->outNavigationButtons("bottom", $page);
532 
533  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "redirectQuestion"));
534  }
535 
536  if(!$this->preview)
537  {
538  $this->object->setPage($_SESSION["finished_id"][$this->object->getId()], $page[0]['question_id']);
539  $this->object->setStartTime($_SESSION["finished_id"][$this->object->getId()], $first_question);
540  }
541  }
542 
548  function saveUserInput($navigationDirection = "next")
549  {
550  if(!$this->preview)
551  {
552  $this->object->setEndTime($_SESSION["finished_id"][$this->object->getId()]);
553  }
554 
555  // check users input when it is a metric question
556  unset($_SESSION["svy_errors"]);
557  $_SESSION["postdata"] = $_POST;
558  $page_error = 0;
559  $page = $this->object->getNextPage($_GET["qid"], 0);
560  foreach ($page as $data)
561  {
562  $page_error += $this->saveActiveQuestionData($data);
563  }
564  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
565  {
566  if ($page_error == 1)
567  {
568  ilUtil::sendFailure($this->lng->txt("svy_page_error"), TRUE);
569  }
570  else
571  {
572  ilUtil::sendFailure($this->lng->txt("svy_page_errors"), TRUE);
573  }
574  }
575  else
576  {
577  $page_error = "";
578  unset($_SESSION["svy_errors"]);
579  }
580  return $page_error;
581  }
582 
590  /*
591  function navigate($navigationDirection = "next")
592  {
593  // check users input when it is a metric question
594  unset($_SESSION["svy_errors"]);
595  $page_error = 0;
596  $page = $this->object->getNextPage($_GET["qid"], 0);
597  foreach ($page as $data)
598  {
599  $page_error += $this->saveActiveQuestionData($data);
600  }
601  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
602  {
603  if ($page_error == 1)
604  {
605  ilUtil::sendFailure($this->lng->txt("svy_page_error"));
606  }
607  else
608  {
609  ilUtil::sendFailure($this->lng->txt("svy_page_errors"));
610  }
611  }
612  else
613  {
614  $page_error = "";
615  unset($_SESSION["svy_errors"]);
616  }
617 
618  $direction = 0;
619  switch ($navigationDirection)
620  {
621  case "next":
622  default:
623  $activepage = $_GET["qid"];
624  if (!$page_error)
625  {
626  $direction = 1;
627  }
628  break;
629  case "previous":
630  $activepage = $_GET["qid"];
631  if (!$page_error)
632  {
633  $direction = -1;
634  }
635  break;
636  }
637  $this->outSurveyPage($activepage, $direction);
638  }
639 */
640 
648  function saveActiveQuestionData(&$data)
649  {
650  global $ilUser;
651 
652  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
653  $question =& SurveyQuestion::_instanciateQuestion($data["question_id"]);
654  $error = $question->checkUserInput($_POST, $this->object->getSurveyId());
655  if (strlen($error) == 0)
656  {
657  if(!$this->preview)
658  {
659  // delete old answers
660  $this->object->deleteWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
661 
662  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()]);
663  }
664  else
665  {
666  $_SESSION["preview_data"][$this->object->getId()][$data["question_id"]] =
667  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()], true);
668  }
669  return 0;
670  }
671  else
672  {
673  $_SESSION["svy_errors"][$question->getId()] = $error;
674  return 1;
675  }
676  }
677 
685  function cancel()
686  {
687  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
688  }
689 
698  {
699  if (strlen($this->object->getOutro()) == 0)
700  {
701  $this->backToRepository();
702  }
703  else
704  {
705  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
706  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
707  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
708  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
709  }
710  }
711 
712  function backToRepository()
713  {
714  global $tree;
715 
716  // #14971
717  if($this->object->get360Mode())
718  {
719  $target_ref_id = $this->object->getRefId();
720  }
721  else
722  {
723  // #11534
724  $target_ref_id = $tree->getParentId($this->object->getRefId());
725  }
726 
727  include_once "Services/Link/classes/class.ilLink.php";
728  ilUtil::redirect(ilLink::_getLink($target_ref_id));
729  }
730 
738  function exitSurvey()
739  {
740  if(!$this->preview)
741  {
742  $this->backToRepository();
743  }
744  else
745  {
746  // #12841
747  $this->ctrl->setParameterByClass("ilsurveyeditorgui", "pgov", $_REQUEST["pgov"]);
748  $this->ctrl->redirectByClass(array("ilobjsurveygui", "ilsurveyeditorgui"), "questions");
749  }
750  }
751 
761  function outNavigationButtons($navigationblock = "top", $page)
762  {
763  $prevpage = $this->object->getNextPage($page[0]["question_id"], -1);
764  $this->tpl->setCurrentBlock($navigationblock . "_prev");
765  if ($prevpage === 0)
766  {
767  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_start"));
768  }
769  else
770  {
771  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_previous"));
772  }
773  $this->tpl->parseCurrentBlock();
774  $nextpage = $this->object->getNextPage($page[0]["question_id"], 1);
775  $this->tpl->setCurrentBlock($navigationblock . "_next");
776  if ($nextpage === 1)
777  {
778  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_finish"));
779  }
780  else
781  {
782  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_next"));
783  }
784  $this->tpl->parseCurrentBlock();
785  }
786 
787  function preview()
788  {
789  $this->outSurveyPage();
790  }
791 }
792 ?>