ILIAS  eassessment Revision 61809
 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 
54  function ilSurveyExecutionGUI($a_object)
55  {
56  global $lng, $tpl, $ilCtrl, $ilias, $tree;
57 
58  $this->lng =& $lng;
59  $this->tpl =& $tpl;
60  $this->ctrl =& $ilCtrl;
61  $this->ilias =& $ilias;
62  $this->object =& $a_object;
63  $this->tree =& $tree;
64  }
65 
69  function &executeCommand()
70  {
71  $cmd = $this->ctrl->getCmd();
72  $next_class = $this->ctrl->getNextClass($this);
73 
74  $cmd = $this->getCommand($cmd);
75  if (strlen($cmd) == 0)
76  {
77  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
78  $this->ctrl->redirect($this, "gotoPage");
79  }
80  switch($next_class)
81  {
82  default:
83  $ret =& $this->$cmd();
84  break;
85  }
86  return $ret;
87  }
88 
96  function getCommand($cmd)
97  {
98  return $cmd;
99  }
100 
108  function resume()
109  {
110  $this->start(true);
111  }
112 
120  function start($resume = false)
121  {
122  global $ilUser;
123  global $rbacsystem;
124 
125  unset($_SESSION["svy_errors"]);
126  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
127  {
128  // only with read access it is possible to run the test
129  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
130  }
131 
132  if ($this->object->getAnonymize() && !$this->object->isAccessibleWithoutCode())
133  {
134  if ($resume)
135  {
136  $anonymize_key = $this->object->getAnonymousId($_POST["anonymous_id"]);
137  if ($anonymize_key)
138  {
139  $_SESSION["anonymous_id"][$this->object->getId()] = $anonymize_key;
140  }
141  else
142  {
143  unset($_POST["cmd"]["resume"]);
144  ilUtil::sendFailure(sprintf($this->lng->txt("error_retrieving_anonymous_survey"), $_POST["anonymous_id"]));
145  }
146  }
147  }
148 
149  $direction = 0;
150 
151  if ($this->object->getAnonymize() && !$this->object->isAccessibleWithoutCode())
152  {
153  if ($this->object->checkSurveyCode($_POST["anonymous_id"]))
154  {
155  $_SESSION["anonymous_id"][$this->object->getId()] = $_POST["anonymous_id"];
156  }
157  else
158  {
159  ilUtil::sendFailure(sprintf($this->lng->txt("error_retrieving_anonymous_survey"), $_POST["anonymous_id"]), true);
160  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
161  }
162  }
163  if ($this->object->isAccessibleWithoutCode())
164  {
165  $anonymous_id = $this->object->getUserSurveyCode($ilUser->getId());
166  if (strlen($anonymous_id))
167  {
168  $_SESSION["anonymous_id"][$this->object->getId()] = $anonymous_id;
169  }
170  else
171  {
172  $_SESSION["anonymous_id"][$this->object->getId()] = $this->object->createNewAccessCode();
173  }
174  }
175 
176  $activepage = "";
177  if ($resume)
178  {
179  $activepage = $this->object->getLastActivePage($_SESSION["finished_id"][$this->object->getId()]);
180  $direction = 0;
181  }
182  // explicitly set the survey started!
183  if ($this->object->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]) === FALSE)
184  {
185  $_SESSION["finished_id"][$this->object->getId()] = $this->object->startSurvey($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]);
186  }
187  if (strlen($activepage)) $this->ctrl->setParameter($this, "qid", $activepage);
188  $this->ctrl->setParameter($this, "activecommand", "default");
189  $this->ctrl->redirect($this, "redirectQuestion");
190  }
191 
198  function redirectQuestion()
199  {
200  switch ($_GET["activecommand"])
201  {
202  case "next":
203  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
204  break;
205  case "previous":
206  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
207  break;
208  case "gotoPage":
209  $this->outSurveyPage($_GET["qid"], $_GET["direction"]);
210  break;
211  case "default":
212  $this->outSurveyPage($_GET["qid"]);
213  break;
214  default:
215  // don't save input, go to the first page
216  $this->outSurveyPage();
217  break;
218  }
219  }
220 
221 
229  function previous()
230  {
231  $result = $this->saveUserInput("previous");
232  $this->ctrl->setParameter($this, "activecommand", "previous");
233  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
234  if (strlen($result))
235  {
236  $this->ctrl->setParameter($this, "direction", "0");
237  }
238  else
239  {
240  $this->ctrl->setParameter($this, "direction", "-1");
241  }
242  $this->ctrl->redirect($this, "redirectQuestion");
243  }
244 
250  function next()
251  {
252  $result = $this->saveUserInput("next");
253  $this->ctrl->setParameter($this, "activecommand", "next");
254  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
255  if (strlen($result))
256  {
257  $this->ctrl->setParameter($this, "direction", "0");
258  }
259  else
260  {
261  $this->ctrl->setParameter($this, "direction", "1");
262  }
263  $this->ctrl->redirect($this, "redirectQuestion");
264  }
265 
271  function gotoPage()
272  {
273  $this->ctrl->setParameter($this, "activecommand", "gotoPage");
274  $this->ctrl->setParameter($this, "qid", $_GET["qid"]);
275  $this->ctrl->setParameter($this, "direction", "0");
276  $this->ctrl->redirect($this, "redirectQuestion");
277  }
278 
286  function outSurveyPage($activepage = NULL, $direction = NULL)
287  {
288  global $ilUser;
289 
290  // security check if someone tries to go into a survey using an URL to one of the questions
291  $canStart = $this->object->canStartSurvey($_SESSION["anonymous_id"][$this->object->getId()]);
292  if (!$canStart["result"])
293  {
294  ilUtil::sendInfo(implode("<br />", $canStart["messages"]), TRUE);
295  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
296  }
297  $survey_started = $this->object->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"][$this->object->getId()]);
298  if ($survey_started === FALSE)
299  {
300  ilUtil::sendInfo($this->lng->txt("survey_use_start_button"), TRUE);
301  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
302  }
303 
304  $page = $this->object->getNextPage($activepage, $direction);
305  $constraint_true = 0;
306 
307  // check for constraints
308  if (count($page[0]["constraints"]))
309  {
310  while (is_array($page) and ($constraint_true == 0) and (count($page[0]["constraints"])))
311  {
312  $constraint_true = ($page[0]['constraints'][0]['conjunction'] == 0) ? true : false;
313  foreach ($page[0]["constraints"] as $constraint)
314  {
315  $working_data = $this->object->loadWorkingData($constraint["question"], $_SESSION["finished_id"][$this->object->getId()]);
316  if ($constraint['conjunction'] == 0)
317  {
318  // and
319  $constraint_true = $constraint_true & $this->object->checkConstraint($constraint, $working_data);
320  }
321  else
322  {
323  // or
324  $constraint_true = $constraint_true | $this->object->checkConstraint($constraint, $working_data);
325  }
326  }
327  if ($constraint_true == 0)
328  {
329  $page = $this->object->getNextPage($page[0]["question_id"], $direction);
330  }
331  }
332  }
333 
334  $first_question = -1;
335  if ($page === 0)
336  {
337  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
338  }
339  else if ($page === 1)
340  {
341  $this->object->finishSurvey($ilUser->id, $_SESSION["anonymous_id"][$this->object->getId()]);
342  if (array_key_exists("anonymous_id", $_SESSION)) unset($_SESSION["anonymous_id"][$this->object->getId()]);
343  $this->runShowFinishedPage();
344  return;
345  }
346  else
347  {
348  $required = false;
349  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_content.html", "Modules/Survey");
350 
351  if (!($this->object->getAnonymize() && $this->object->isAccessibleWithoutCode() && ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)))
352  {
353  /* Change Sn */
354  /*
355  $this->tpl->setCurrentBlock("suspend_survey");
356  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("cancel_survey"));
357  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
358  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
359  $this->tpl->setVariable("ALT_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
360  $this->tpl->setVariable("TITLE_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
361  $this->tpl->setVariable("IMG_SUSPEND", ilUtil::getImagePath("cancel.gif"));
362  $this->tpl->parseCurrentBlock();
363  */
364  }
365  $this->outNavigationButtons("top", $page);
366  $this->tpl->setCurrentBlock("percentage");
367  $percentage = (int)(($page[0]["position"])*100);
368  $this->tpl->setVariable("PERCENT_BAR_START", ilUtil::getImagePath("bar_start.gif"));
369  $this->tpl->setVariable("PERCENT_BAR_FILLED", ilUtil::getImagePath("bar_filled.gif"));
370  $this->tpl->setVariable("PERCENT_BAR_EMPTY", ilUtil::getImagePath("bar_empty.gif"));
371  $this->tpl->setVariable("PERCENT_BAR_END", ilUtil::getImagePath("bar_end.gif"));
372  $this->tpl->setVariable("PERCENTAGE_ALT", $this->lng->txt("percentage"));
373  $this->tpl->setVariable("PERCENTAGE_VALUE", $percentage);
374  $this->tpl->setVariable("PERCENTAGE_UNFINISHED", 100-$percentage);
375  $this->tpl->parseCurrentBlock();
376  if (count($page) > 1)
377  {
378  $this->tpl->setCurrentBlock("questionblock_title");
379  $this->tpl->setVariable("TEXT_QUESTIONBLOCK_TITLE", $page[0]["questionblock_title"]);
380  $this->tpl->parseCurrentBlock();
381  }
382  foreach ($page as $data)
383  {
384  $this->tpl->setCurrentBlock("survey_content");
385  if ($data["heading"])
386  {
387  $this->tpl->setVariable("QUESTION_HEADING", $data["heading"]);
388  }
389  if ($first_question == -1) $first_question = $data["question_id"];
390  $question_gui = $this->object->getQuestionGUI($data["type_tag"], $data["question_id"]);
391  if (is_array($_SESSION["svy_errors"]))
392  {
393  $working_data =& $question_gui->object->getWorkingDataFromUserInput($_SESSION["postdata"]);
394  }
395  else
396  {
397  $working_data = $this->object->loadWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
398  }
399  $question_gui->object->setObligatory($data["obligatory"]);
400  $error_messages = array();
401  if (is_array($_SESSION["svy_errors"]))
402  {
403  $error_messages = $_SESSION["svy_errors"];
404  }
405  $show_questiontext = ($data["questionblock_show_questiontext"]) ? 1 : 0;
406  $question_output = $question_gui->getWorkingForm($working_data, $this->object->getShowQuestionTitles(), $show_questiontext, $error_messages[$data["question_id"]], $this->object->getSurveyId());
407  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
408  $this->ctrl->setParameter($this, "qid", $data["question_id"]);
409  $this->tpl->parse("survey_content");
410  if ($data["obligatory"]) $required = true;
411  }
412  if ($required)
413  {
414  $this->tpl->setCurrentBlock("required");
415  $this->tpl->setVariable("TEXT_REQUIRED", $this->lng->txt("required_field"));
416  $this->tpl->parseCurrentBlock();
417  }
418 
419  $this->outNavigationButtons("bottom", $page);
420  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "redirectQuestion"));
421  }
422  $this->object->setPage($_SESSION["finished_id"][$this->object->getId()], $page[0]['question_id']);
423  $this->object->setStartTime($_SESSION["finished_id"][$this->object->getId()], $first_question);
424  }
425 
431  function saveUserInput($navigationDirection = "next")
432  {
433  global $ilUser;
434  global $rbacsystem;
435 
436  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
437  {
438  // only with read access it is possible to run the test
439  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
440  }
441 
442  $this->object->setEndTime($_SESSION["finished_id"][$this->object->getId()]);
443  // check users input when it is a metric question
444  unset($_SESSION["svy_errors"]);
445  $_SESSION["postdata"] = $_POST;
446  $page_error = 0;
447  $page = $this->object->getNextPage($_GET["qid"], 0);
448  foreach ($page as $data)
449  {
450  $page_error += $this->saveActiveQuestionData($data);
451  }
452  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
453  {
454  if ($page_error == 1)
455  {
456  ilUtil::sendFailure($this->lng->txt("svy_page_error"), TRUE);
457  }
458  else
459  {
460  ilUtil::sendFailure($this->lng->txt("svy_page_errors"), TRUE);
461  }
462  }
463  else
464  {
465  $page_error = "";
466  unset($_SESSION["svy_errors"]);
467  }
468  return $page_error;
469  }
470 
478  function navigate($navigationDirection = "next")
479  {
480  global $ilUser;
481  global $rbacsystem;
482 
483  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
484  {
485  // only with read access it is possible to run the test
486  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
487  }
488 
489  // check users input when it is a metric question
490  unset($_SESSION["svy_errors"]);
491  $page_error = 0;
492  $page = $this->object->getNextPage($_GET["qid"], 0);
493  foreach ($page as $data)
494  {
495  $page_error += $this->saveActiveQuestionData($data);
496  }
497  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
498  {
499  if ($page_error == 1)
500  {
501  ilUtil::sendFailure($this->lng->txt("svy_page_error"));
502  }
503  else
504  {
505  ilUtil::sendFailure($this->lng->txt("svy_page_errors"));
506  }
507  }
508  else
509  {
510  $page_error = "";
511  unset($_SESSION["svy_errors"]);
512  }
513 
514  $direction = 0;
515  switch ($navigationDirection)
516  {
517  case "next":
518  default:
519  $activepage = $_GET["qid"];
520  if (!$page_error)
521  {
522  $direction = 1;
523  }
524  break;
525  case "previous":
526  $activepage = $_GET["qid"];
527  if (!$page_error)
528  {
529  $direction = -1;
530  }
531  break;
532  }
533  $this->outSurveyPage($activepage, $direction);
534  }
535 
544  {
545  global $ilUser;
546 
547  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
548  $question =& SurveyQuestion::_instanciateQuestion($data["question_id"]);
549  $error = $question->checkUserInput($_POST, $this->object->getSurveyId());
550  if (strlen($error) == 0)
551  {
552  $user_id = $ilUser->getId();
553  // delete old answers
554  $this->object->deleteWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
555 
556  if ($this->object->isSurveyStarted($user_id, $_SESSION["anonymous_id"][$this->object->getId()]) === false)
557  {
558  $_SESSION["finished_id"][$this->object->getId()] = $this->object->startSurvey($user_id, $_SESSION["anonymous_id"][$this->object->getId()]);
559  }
560  if ($this->object->getAnonymize())
561  {
562  $user_id = 0;
563  }
564  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()]);
565  return 0;
566  }
567  else
568  {
569  $_SESSION["svy_errors"][$question->getId()] = $error;
570  return 1;
571  }
572  }
573 
581  function cancel()
582  {
583  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
584  }
585 
594  {
595  global $ilSetting; // Change Sn
596  unset($_SESSION["anonymous_id"][$this->object->getId()]);
597 
598  /* Change Sn */
599  if ($this->object->getMailConfirmation())
600  {
601  include_once "./Services/Mail/classes/class.ilMimeMail.php";
602  $email = $this->object->ilias->account->getEmail();
603  $subject = $this->object->getMailConfirmationSubject();
604  $body = $this->object->getMailConfirmationBody();
605  $body = str_replace('[SURVEY_TITLE]',$this->object->getTitle(),$body);
606  $body = str_replace('[FULL_NAME]',$this->object->ilias->account->getFullname(),$body);
607  $body = str_replace('[LOGIN]',$this->object->ilias->account->getLogin(),$body);
608  $body = str_replace('[EMAIL]',$this->object->ilias->account->getEmail(),$body);
609  $body = str_replace('[MATRICULATION]',$this->object->ilias->account->getMatriculation(),$body);
610  $body = str_replace('[DATE]',date("c"),$body);
611  $mmail = new ilMimeMail();
612  $mmail->autoCheck(false);
613  $mmail->From($ilSetting->get("admin_email"));
614  $mmail->Subject($subject);
615  $mmail->To($email);
616  $mmail->Body($body);
617  $mmail->Send();
618  }
619 
620  $redirect_after_survey = $this->object->getRedirectAfterSurvey();
621 
622  if (strlen($this->object->getOutro()) != 0)
623  {
624 
625  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
626  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
627  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
628  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
629  }
630  else
631  {
632  if ($redirect_after_survey != "")
633  {
634  if (!$this->object->getRedirectOnlyKioskMode())
635  {
636  ilUtil::redirect($redirect_after_survey);
637  }
638  else
639  {
640  if (KIOSK_MODE)
641  {
642  ilUtil::redirect($redirect_after_survey);
643  }
644  else
645  {
646  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
647  }
648  }
649  }
650  else
651  {
652  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
653  }
654  }
655 
656 
657  /* Change Sn End */
658  /* To Do: show redirection link in outro */
659  /*
660  if (strlen($this->object->getOutro()) == 0)
661  {
662  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
663  }
664  else
665  {
666  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
667  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
668  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
669  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
670  }
671  */
672  }
673 
681  function exitSurvey()
682  {
683  /* Change Sn */
684  $redirect_after_survey = $this->object->getRedirectAfterSurvey();
685  if ($redirect_after_survey != "")
686  {
687  if (!$this->object->getRedirectOnlyKioskMode())
688  {
689  ilUtil::redirect($redirect_after_survey);
690  }
691  else
692  {
693  if (KIOSK_MODE)
694  {
695  ilUtil::redirect($redirect_after_survey);
696  }
697  else
698  {
699  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
700  }
701  }
702  }
703  else
704  {
705  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
706  }
707  /* Change Sn End */
708  }
709 
719  function outNavigationButtons($navigationblock = "top", $page)
720  {
721  $prevpage = $this->object->getNextPage($page[0]["question_id"], -1);
722  $this->tpl->setCurrentBlock($navigationblock . "_prev");
723  if ($prevpage === 0)
724  {
725  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_start"));
726  }
727  else
728  {
729  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_previous"));
730  }
731  $this->tpl->parseCurrentBlock();
732  $nextpage = $this->object->getNextPage($page[0]["question_id"], 1);
733  $this->tpl->setCurrentBlock($navigationblock . "_next");
734  if ($nextpage === 1)
735  {
736  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_finish"));
737  }
738  else
739  {
740  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_next"));
741  }
742  $this->tpl->parseCurrentBlock();
743  }
744 }
745 ?>