ILIAS  Release_4_2_x_branch Revision 61807
 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 
24 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
25 
38 {
39  var $object;
40  var $lng;
41  var $tpl;
42  var $ctrl;
43  var $ilias;
44  var $tree;
45  var $preview;
46 
55  function ilSurveyExecutionGUI($a_object)
56  {
57  global $lng, $tpl, $ilCtrl, $ilias, $tree;
58 
59  $this->lng =& $lng;
60  $this->tpl =& $tpl;
61  $this->ctrl =& $ilCtrl;
62  $this->ilias =& $ilias;
63  $this->object =& $a_object;
64  $this->tree =& $tree;
65 
66  // stay in preview mode
67  $this->preview = (bool)$_REQUEST["prvw"];
68  $this->ctrl->saveParameter($this, "prvw");
69  $this->ctrl->saveParameter($this, "pgov");
70  }
71 
75  function &executeCommand()
76  {
77  $cmd = $this->ctrl->getCmd();
78  $next_class = $this->ctrl->getNextClass($this);
79 
80  $cmd = $this->getCommand($cmd);
81  if (strlen($cmd) == 0)
82  {
83  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
84  $this->ctrl->redirect($this, "gotoPage");
85  }
86  switch($next_class)
87  {
88  default:
89  $ret =& $this->$cmd();
90  break;
91  }
92  return $ret;
93  }
94 
102  function getCommand($cmd)
103  {
104  return $cmd;
105  }
106 
114  function resume()
115  {
116  $this->start(true);
117  }
118 
126  function start($resume = false)
127  {
128  global $ilUser;
129  global $rbacsystem;
130 
131  if($this->preview)
132  {
133  unset($_SESSION["preview_data"]);
134  }
135  unset($_SESSION["svy_errors"]);
136  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
137  {
138  // only with read access it is possible to run the test
139  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
140  }
141 
142  if ($this->object->getAnonymize() && !$this->object->isAccessibleWithoutCode())
143  {
144  if ($resume)
145  {
146  $anonymize_key = $this->object->getAnonymousId($_POST["anonymous_id"]);
147  if ($anonymize_key)
148  {
149  $_SESSION["anonymous_id"][$this->object->getId()] = $anonymize_key;
150  }
151  else
152  {
153  unset($_POST["cmd"]["resume"]);
154  ilUtil::sendFailure(sprintf($this->lng->txt("error_retrieving_anonymous_survey"), $_POST["anonymous_id"]));
155  }
156  }
157  }
158 
159  $direction = 0;
160 
161  if ($this->object->getAnonymize() && !$this->object->isAccessibleWithoutCode())
162  {
163  if ($this->object->checkSurveyCode($_POST["anonymous_id"]))
164  {
165  $_SESSION["anonymous_id"][$this->object->getId()] = $_POST["anonymous_id"];
166  }
167  else
168  {
169  ilUtil::sendFailure(sprintf($this->lng->txt("error_retrieving_anonymous_survey"), $_POST["anonymous_id"]), true);
170  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
171  }
172  }
173  if ($this->object->isAccessibleWithoutCode())
174  {
175  $anonymous_id = $this->object->getUserSurveyCode($ilUser->getId());
176  if (strlen($anonymous_id))
177  {
178  $_SESSION["anonymous_id"][$this->object->getId()] = $anonymous_id;
179  }
180  else
181  {
182  $_SESSION["anonymous_id"][$this->object->getId()] = $this->object->createNewAccessCode();
183  }
184  }
185 
186  $activepage = "";
187  if ($resume)
188  {
189  $activepage = $this->object->getLastActivePage($_SESSION["finished_id"][$this->object->getId()]);
190  $direction = 0;
191  }
192  // explicitly set the survey started!
193  if ($this->object->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]) === FALSE)
194  {
195  $_SESSION["finished_id"][$this->object->getId()] = $this->object->startSurvey($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]);
196  }
197  if (strlen($activepage)) $this->ctrl->setParameter($this, "qid", $activepage);
198  $this->ctrl->setParameter($this, "activecommand", "default");
199  $this->ctrl->redirect($this, "redirectQuestion");
200  }
201 
208  function redirectQuestion()
209  {
210  switch ($_GET["activecommand"])
211  {
212  case "next":
213  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
214  break;
215  case "previous":
216  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
217  break;
218  case "gotoPage":
219  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
220  break;
221  case "default":
222  $this->outSurveyPage($_GET["qid"]);
223  break;
224  default:
225  // don't save input, go to the first page
226  $this->outSurveyPage();
227  break;
228  }
229  }
230 
231 
239  function previous()
240  {
241  $result = $this->saveUserInput("previous");
242  $this->ctrl->setParameter($this, "activecommand", "previous");
243  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
244  if (strlen($result))
245  {
246  $this->ctrl->setParameter($this, "direction", "0");
247  }
248  else
249  {
250  $this->ctrl->setParameter($this, "direction", "-1");
251  }
252  $this->ctrl->redirect($this, "redirectQuestion");
253  }
254 
260  function next()
261  {
262  $result = $this->saveUserInput("next");
263  $this->ctrl->setParameter($this, "activecommand", "next");
264  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
265  if (strlen($result))
266  {
267  $this->ctrl->setParameter($this, "direction", "0");
268  }
269  else
270  {
271  $this->ctrl->setParameter($this, "direction", "1");
272  }
273  $this->ctrl->redirect($this, "redirectQuestion");
274  }
275 
281  function gotoPage()
282  {
283  $this->ctrl->setParameter($this, "activecommand", "gotoPage");
284  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
285  $this->ctrl->setParameter($this, "direction", "0");
286  $this->ctrl->redirect($this, "redirectQuestion");
287  }
288 
296  function outSurveyPage($activepage = NULL, $direction = NULL)
297  {
298  global $ilUser,$rbacsystem;
299 
300  if(!$this->preview)
301  {
302  // security check if someone tries to go into a survey using an URL to one of the questions
303  $canStart = $this->object->canStartSurvey($_SESSION["anonymous_id"][$this->object->getId()]);
304  if (!$canStart["result"])
305  {
306  ilUtil::sendInfo(implode("<br />", $canStart["messages"]), TRUE);
307  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
308  }
309  $survey_started = $this->object->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]);
310  if ($survey_started === FALSE)
311  {
312  ilUtil::sendInfo($this->lng->txt("survey_use_start_button"), TRUE);
313  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
314  }
315  }
316  else if (!$rbacsystem->checkAccess("write", $this->object->ref_id))
317  {
318  // only with write access it is possible to preview the survey
319  $this->ilias->raiseError($this->lng->txt("survey_cannot_preview_survey"),$this->ilias->error_obj->MESSAGE);
320  }
321 
322  $page = $this->object->getNextPage($activepage, $direction);
323  $constraint_true = 0;
324 
325  // check for constraints
326  if (count($page[0]["constraints"]))
327  {
328  while (is_array($page) and ($constraint_true == 0) and (count($page[0]["constraints"])))
329  {
330  $constraint_true = ($page[0]['constraints'][0]['conjunction'] == 0) ? true : false;
331  foreach ($page[0]["constraints"] as $constraint)
332  {
333  if(!$this->preview)
334  {
335  $working_data = $this->object->loadWorkingData($constraint["question"], $_SESSION["finished_id"][$this->object->getId()]);
336  }
337  else
338  {
339  $working_data = $_SESSION["preview_data"][$this->object->getId()][$constraint["question"]];
340  }
341  if ($constraint['conjunction'] == 0)
342  {
343  // and
344  $constraint_true = $constraint_true & $this->object->checkConstraint($constraint, $working_data);
345  }
346  else
347  {
348  // or
349  $constraint_true = $constraint_true | $this->object->checkConstraint($constraint, $working_data);
350  }
351  }
352  if ($constraint_true == 0)
353  {
354  // #11047 - we are skipping the page, so we have to get rid of existing answers for that question(s)
355  foreach($page as $page_question)
356  {
357  $qid = $page_question["question_id"];
358 
359  // see saveActiveQuestionData()
360  if(!$this->preview)
361  {
362  $this->object->deleteWorkingData($qid, $_SESSION["finished_id"][$this->object->getId()]);
363  }
364  else
365  {
366  $_SESSION["preview_data"][$this->object->getId()][$qid] = null;
367  }
368  }
369 
370  $page = $this->object->getNextPage($page[0]["question_id"], $direction);
371  }
372  }
373  }
374 
375  $first_question = -1;
376  if ($page === 0)
377  {
378  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
379  }
380  else if ($page === 1)
381  {
382  $this->object->finishSurvey($ilUser->id, $_SESSION["anonymous_id"][$this->object->getId()]);
383  if (array_key_exists("anonymous_id", $_SESSION)) unset($_SESSION["anonymous_id"][$this->object->getId()]);
384  $this->runShowFinishedPage();
385  return;
386  }
387  else
388  {
389  $required = false;
390  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_content.html", "Modules/Survey");
391 
392  if (!($this->object->getAnonymize() && $this->object->isAccessibleWithoutCode() && ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)))
393  {
394  $this->tpl->setCurrentBlock("suspend_survey");
395 
396  if(!$this->preview)
397  {
398  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("cancel_survey"));
399  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
400  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
401  }
402  else
403  {
404  $this->ctrl->setParameterByClass("ilObjSurveyGUI", "pgov", $_REQUEST["pgov"]);
405  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("survey_cancel_preview"));
406  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "questions"));
407  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "questions"));
408  }
409 
410  $this->tpl->setVariable("ALT_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
411  $this->tpl->setVariable("TITLE_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
412  $this->tpl->setVariable("IMG_SUSPEND", ilUtil::getImagePath("cancel.gif"));
413  $this->tpl->parseCurrentBlock();
414  }
415  $this->outNavigationButtons("top", $page);
416  $this->tpl->setCurrentBlock("percentage");
417  $percentage = (int)(($page[0]["position"])*100);
418  $this->tpl->setVariable("PERCENT_BAR_START", ilUtil::getImagePath("bar_start.gif"));
419  $this->tpl->setVariable("PERCENT_BAR_FILLED", ilUtil::getImagePath("bar_filled.gif"));
420  $this->tpl->setVariable("PERCENT_BAR_EMPTY", ilUtil::getImagePath("bar_empty.gif"));
421  $this->tpl->setVariable("PERCENT_BAR_END", ilUtil::getImagePath("bar_end.gif"));
422  $this->tpl->setVariable("PERCENTAGE_ALT", $this->lng->txt("percentage"));
423  $this->tpl->setVariable("PERCENTAGE_VALUE", $percentage);
424  $this->tpl->setVariable("PERCENTAGE_UNFINISHED", 100-$percentage);
425  $this->tpl->parseCurrentBlock();
426  if (count($page) > 1 && $page[0]["questionblock_show_blocktitle"])
427  {
428  $this->tpl->setCurrentBlock("questionblock_title");
429  $this->tpl->setVariable("TEXT_QUESTIONBLOCK_TITLE", $page[0]["questionblock_title"]);
430  $this->tpl->parseCurrentBlock();
431  }
432  foreach ($page as $data)
433  {
434  $this->tpl->setCurrentBlock("survey_content");
435  if ($data["heading"])
436  {
437  $this->tpl->setVariable("QUESTION_HEADING", $data["heading"]);
438  }
439  if ($first_question == -1) $first_question = $data["question_id"];
440  $question_gui = $this->object->getQuestionGUI($data["type_tag"], $data["question_id"]);
441  if (is_array($_SESSION["svy_errors"]))
442  {
443  $working_data =& $question_gui->object->getWorkingDataFromUserInput($_SESSION["postdata"]);
444  }
445  else
446  {
447  $working_data = $this->object->loadWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
448  }
449  $question_gui->object->setObligatory($data["obligatory"]);
450  $error_messages = array();
451  if (is_array($_SESSION["svy_errors"]))
452  {
453  $error_messages = $_SESSION["svy_errors"];
454  }
455  $show_questiontext = ($data["questionblock_show_questiontext"]) ? 1 : 0;
456  $question_output = $question_gui->getWorkingForm($working_data, $this->object->getShowQuestionTitles(), $show_questiontext, $error_messages[$data["question_id"]], $this->object->getSurveyId());
457  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
458  $this->ctrl->setParameter($this, "qid", $data["question_id"]);
459  $this->tpl->parse("survey_content");
460  if ($data["obligatory"]) $required = true;
461  }
462  if ($required)
463  {
464  $this->tpl->setCurrentBlock("required");
465  $this->tpl->setVariable("TEXT_REQUIRED", $this->lng->txt("required_field"));
466  $this->tpl->parseCurrentBlock();
467  }
468 
469  $this->outNavigationButtons("bottom", $page);
470 
471  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "redirectQuestion"));
472  }
473 
474  if(!$this->preview)
475  {
476  $this->object->setPage($_SESSION["finished_id"][$this->object->getId()], $page[0]['question_id']);
477  $this->object->setStartTime($_SESSION["finished_id"][$this->object->getId()], $first_question);
478  }
479  }
480 
486  function saveUserInput($navigationDirection = "next")
487  {
488  global $ilUser;
489  global $rbacsystem;
490 
491  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
492  {
493  // only with read access it is possible to run the test
494  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
495  }
496 
497  if(!$this->preview)
498  {
499  $this->object->setEndTime($_SESSION["finished_id"][$this->object->getId()]);
500  }
501 
502  // check users input when it is a metric question
503  unset($_SESSION["svy_errors"]);
504  $_SESSION["postdata"] = $_POST;
505  $page_error = 0;
506  $page = $this->object->getNextPage($_GET["qid"], 0);
507  foreach ($page as $data)
508  {
509  $page_error += $this->saveActiveQuestionData($data);
510  }
511  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
512  {
513  if ($page_error == 1)
514  {
515  ilUtil::sendFailure($this->lng->txt("svy_page_error"), TRUE);
516  }
517  else
518  {
519  ilUtil::sendFailure($this->lng->txt("svy_page_errors"), TRUE);
520  }
521  }
522  else
523  {
524  $page_error = "";
525  unset($_SESSION["svy_errors"]);
526  }
527  return $page_error;
528  }
529 
537  function navigate($navigationDirection = "next")
538  {
539  global $ilUser;
540  global $rbacsystem;
541 
542  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
543  {
544  // only with read access it is possible to run the test
545  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
546  }
547 
548  // check users input when it is a metric question
549  unset($_SESSION["svy_errors"]);
550  $page_error = 0;
551  $page = $this->object->getNextPage($_GET["qid"], 0);
552  foreach ($page as $data)
553  {
554  $page_error += $this->saveActiveQuestionData($data);
555  }
556  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
557  {
558  if ($page_error == 1)
559  {
560  ilUtil::sendFailure($this->lng->txt("svy_page_error"));
561  }
562  else
563  {
564  ilUtil::sendFailure($this->lng->txt("svy_page_errors"));
565  }
566  }
567  else
568  {
569  $page_error = "";
570  unset($_SESSION["svy_errors"]);
571  }
572 
573  $direction = 0;
574  switch ($navigationDirection)
575  {
576  case "next":
577  default:
578  $activepage = $_GET["qid"];
579  if (!$page_error)
580  {
581  $direction = 1;
582  }
583  break;
584  case "previous":
585  $activepage = $_GET["qid"];
586  if (!$page_error)
587  {
588  $direction = -1;
589  }
590  break;
591  }
592  $this->outSurveyPage($activepage, $direction);
593  }
594 
603  {
604  global $ilUser;
605 
606  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
607  $question =& SurveyQuestion::_instanciateQuestion($data["question_id"]);
608  $error = $question->checkUserInput($_POST, $this->object->getSurveyId());
609  if (strlen($error) == 0)
610  {
611  if(!$this->preview)
612  {
613  $user_id = $ilUser->getId();
614 
615  // delete old answers
616  $this->object->deleteWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
617 
618  if ($this->object->isSurveyStarted($user_id, $_SESSION["anonymous_id"][$this->object->getId()]) === false)
619  {
620  $_SESSION["finished_id"][$this->object->getId()] = $this->object->startSurvey($user_id, $_SESSION["anonymous_id"][$this->object->getId()]);
621  }
622  if ($this->object->getAnonymize())
623  {
624  $user_id = 0;
625  }
626  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()]);
627  }
628  else
629  {
630  $_SESSION["preview_data"][$this->object->getId()][$data["question_id"]] =
631  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()], true);
632  }
633  return 0;
634  }
635  else
636  {
637  $_SESSION["svy_errors"][$question->getId()] = $error;
638  return 1;
639  }
640  }
641 
649  function cancel()
650  {
651  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
652  }
653 
662  {
663  unset($_SESSION["anonymous_id"][$this->object->getId()]);
664  if (strlen($this->object->getOutro()) == 0)
665  {
666  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
667  }
668  else
669  {
670  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
671  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
672  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
673  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
674  }
675  }
676 
684  function exitSurvey()
685  {
686  if(!$this->preview)
687  {
688  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
689  }
690  else
691  {
692  $this->ctrl->setParameterByClass("ilobjsurveygui", "pgov", $_REQUEST["pgov"]);
693  $this->ctrl->redirectByClass("ilobjsurveygui", "questions");
694  }
695  }
696 
706  function outNavigationButtons($navigationblock = "top", $page)
707  {
708  $prevpage = $this->object->getNextPage($page[0]["question_id"], -1);
709  $this->tpl->setCurrentBlock($navigationblock . "_prev");
710  if ($prevpage === 0)
711  {
712  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_start"));
713  }
714  else
715  {
716  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_previous"));
717  }
718  $this->tpl->parseCurrentBlock();
719  $nextpage = $this->object->getNextPage($page[0]["question_id"], 1);
720  $this->tpl->setCurrentBlock($navigationblock . "_next");
721  if ($nextpage === 1)
722  {
723  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_finish"));
724  }
725  else
726  {
727  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_next"));
728  }
729  $this->tpl->parseCurrentBlock();
730  }
731 
732  function preview()
733  {
734  $this->outSurveyPage();
735  }
736 }
737 ?>