ILIAS  Release_4_1_x_branch Revision 61804
 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  $this->tpl->setCurrentBlock("suspend_survey");
354  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("cancel_survey"));
355  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
356  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
357  $this->tpl->setVariable("ALT_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
358  $this->tpl->setVariable("TITLE_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
359  $this->tpl->setVariable("IMG_SUSPEND", ilUtil::getImagePath("cancel.gif"));
360  $this->tpl->parseCurrentBlock();
361  }
362  $this->outNavigationButtons("top", $page);
363  $this->tpl->setCurrentBlock("percentage");
364  $percentage = (int)(($page[0]["position"])*100);
365  $this->tpl->setVariable("PERCENT_BAR_START", ilUtil::getImagePath("bar_start.gif"));
366  $this->tpl->setVariable("PERCENT_BAR_FILLED", ilUtil::getImagePath("bar_filled.gif"));
367  $this->tpl->setVariable("PERCENT_BAR_EMPTY", ilUtil::getImagePath("bar_empty.gif"));
368  $this->tpl->setVariable("PERCENT_BAR_END", ilUtil::getImagePath("bar_end.gif"));
369  $this->tpl->setVariable("PERCENTAGE_ALT", $this->lng->txt("percentage"));
370  $this->tpl->setVariable("PERCENTAGE_VALUE", $percentage);
371  $this->tpl->setVariable("PERCENTAGE_UNFINISHED", 100-$percentage);
372  $this->tpl->parseCurrentBlock();
373  if (count($page) > 1)
374  {
375  $this->tpl->setCurrentBlock("questionblock_title");
376  $this->tpl->setVariable("TEXT_QUESTIONBLOCK_TITLE", $page[0]["questionblock_title"]);
377  $this->tpl->parseCurrentBlock();
378  }
379  foreach ($page as $data)
380  {
381  $this->tpl->setCurrentBlock("survey_content");
382  if ($data["heading"])
383  {
384  $this->tpl->setVariable("QUESTION_HEADING", $data["heading"]);
385  }
386  if ($first_question == -1) $first_question = $data["question_id"];
387  $question_gui = $this->object->getQuestionGUI($data["type_tag"], $data["question_id"]);
388  if (is_array($_SESSION["svy_errors"]))
389  {
390  $working_data =& $question_gui->object->getWorkingDataFromUserInput($_SESSION["postdata"]);
391  }
392  else
393  {
394  $working_data = $this->object->loadWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
395  }
396  $question_gui->object->setObligatory($data["obligatory"]);
397  $error_messages = array();
398  if (is_array($_SESSION["svy_errors"]))
399  {
400  $error_messages = $_SESSION["svy_errors"];
401  }
402  $show_questiontext = ($data["questionblock_show_questiontext"]) ? 1 : 0;
403  $question_output = $question_gui->getWorkingForm($working_data, $this->object->getShowQuestionTitles(), $show_questiontext, $error_messages[$data["question_id"]], $this->object->getSurveyId());
404  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
405  $this->ctrl->setParameter($this, "qid", $data["question_id"]);
406  $this->tpl->parse("survey_content");
407  if ($data["obligatory"]) $required = true;
408  }
409  if ($required)
410  {
411  $this->tpl->setCurrentBlock("required");
412  $this->tpl->setVariable("TEXT_REQUIRED", $this->lng->txt("required_field"));
413  $this->tpl->parseCurrentBlock();
414  }
415 
416  $this->outNavigationButtons("bottom", $page);
417  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "redirectQuestion"));
418  }
419  $this->object->setPage($_SESSION["finished_id"][$this->object->getId()], $page[0]['question_id']);
420  $this->object->setStartTime($_SESSION["finished_id"][$this->object->getId()], $first_question);
421  }
422 
428  function saveUserInput($navigationDirection = "next")
429  {
430  global $ilUser;
431  global $rbacsystem;
432 
433  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
434  {
435  // only with read access it is possible to run the test
436  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
437  }
438 
439  $this->object->setEndTime($_SESSION["finished_id"][$this->object->getId()]);
440  // check users input when it is a metric question
441  unset($_SESSION["svy_errors"]);
442  $_SESSION["postdata"] = $_POST;
443  $page_error = 0;
444  $page = $this->object->getNextPage($_GET["qid"], 0);
445  foreach ($page as $data)
446  {
447  $page_error += $this->saveActiveQuestionData($data);
448  }
449  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
450  {
451  if ($page_error == 1)
452  {
453  ilUtil::sendFailure($this->lng->txt("svy_page_error"), TRUE);
454  }
455  else
456  {
457  ilUtil::sendFailure($this->lng->txt("svy_page_errors"), TRUE);
458  }
459  }
460  else
461  {
462  $page_error = "";
463  unset($_SESSION["svy_errors"]);
464  }
465  return $page_error;
466  }
467 
475  function navigate($navigationDirection = "next")
476  {
477  global $ilUser;
478  global $rbacsystem;
479 
480  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
481  {
482  // only with read access it is possible to run the test
483  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
484  }
485 
486  // check users input when it is a metric question
487  unset($_SESSION["svy_errors"]);
488  $page_error = 0;
489  $page = $this->object->getNextPage($_GET["qid"], 0);
490  foreach ($page as $data)
491  {
492  $page_error += $this->saveActiveQuestionData($data);
493  }
494  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
495  {
496  if ($page_error == 1)
497  {
498  ilUtil::sendFailure($this->lng->txt("svy_page_error"));
499  }
500  else
501  {
502  ilUtil::sendFailure($this->lng->txt("svy_page_errors"));
503  }
504  }
505  else
506  {
507  $page_error = "";
508  unset($_SESSION["svy_errors"]);
509  }
510 
511  $direction = 0;
512  switch ($navigationDirection)
513  {
514  case "next":
515  default:
516  $activepage = $_GET["qid"];
517  if (!$page_error)
518  {
519  $direction = 1;
520  }
521  break;
522  case "previous":
523  $activepage = $_GET["qid"];
524  if (!$page_error)
525  {
526  $direction = -1;
527  }
528  break;
529  }
530  $this->outSurveyPage($activepage, $direction);
531  }
532 
541  {
542  global $ilUser;
543 
544  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
545  $question =& SurveyQuestion::_instanciateQuestion($data["question_id"]);
546  $error = $question->checkUserInput($_POST, $this->object->getSurveyId());
547  if (strlen($error) == 0)
548  {
549  $user_id = $ilUser->getId();
550  // delete old answers
551  $this->object->deleteWorkingData($data["question_id"], $_SESSION["finished_id"][$this->object->getId()]);
552 
553  if ($this->object->isSurveyStarted($user_id, $_SESSION["anonymous_id"][$this->object->getId()]) === false)
554  {
555  $_SESSION["finished_id"][$this->object->getId()] = $this->object->startSurvey($user_id, $_SESSION["anonymous_id"][$this->object->getId()]);
556  }
557  if ($this->object->getAnonymize())
558  {
559  $user_id = 0;
560  }
561  $question->saveUserInput($_POST, $_SESSION["finished_id"][$this->object->getId()]);
562  return 0;
563  }
564  else
565  {
566  $_SESSION["svy_errors"][$question->getId()] = $error;
567  return 1;
568  }
569  }
570 
578  function cancel()
579  {
580  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
581  }
582 
591  {
592  unset($_SESSION["anonymous_id"][$this->object->getId()]);
593  if (strlen($this->object->getOutro()) == 0)
594  {
595  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
596  }
597  else
598  {
599  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
600  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
601  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
602  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
603  }
604  }
605 
613  function exitSurvey()
614  {
615  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
616  }
617 
627  function outNavigationButtons($navigationblock = "top", $page)
628  {
629  $prevpage = $this->object->getNextPage($page[0]["question_id"], -1);
630  $this->tpl->setCurrentBlock($navigationblock . "_prev");
631  if ($prevpage === 0)
632  {
633  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_start"));
634  }
635  else
636  {
637  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_previous"));
638  }
639  $this->tpl->parseCurrentBlock();
640  $nextpage = $this->object->getNextPage($page[0]["question_id"], 1);
641  $this->tpl->setCurrentBlock($navigationblock . "_next");
642  if ($nextpage === 1)
643  {
644  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_finish"));
645  }
646  else
647  {
648  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_next"));
649  }
650  $this->tpl->parseCurrentBlock();
651  }
652 }
653 ?>