ILIAS  Release_4_0_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 
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"] = $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"] = $_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"] = $anonymous_id;
169  }
170  else
171  {
172  $_SESSION["anonymous_id"] = $this->object->createNewAccessCode();
173  }
174  }
175 
176  $activepage = "";
177  if ($resume)
178  {
179  $activepage = $this->object->getLastActivePage($_SESSION["finished_id"]);
180  $direction = 0;
181  }
182  // explicitly set the survey started!
183  if ($this->object->isSurveyStarted($ilUser->getId(), $_SESSION["anonymous_id"]) === FALSE)
184  {
185  $_SESSION["finished_id"] = $this->object->startSurvey($ilUser->getId(), $_SESSION["anonymous_id"]);
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"]);
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"]);
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 = 1;
313  foreach ($page[0]["constraints"] as $constraint)
314  {
315  $working_data = $this->object->loadWorkingData($constraint["question"], $_SESSION["finished_id"]);
316  $constraint_true = $constraint_true & $this->object->checkConstraint($constraint, $working_data);
317  }
318  if ($constraint_true == 0)
319  {
320  $page = $this->object->getNextPage($page[0]["question_id"], $direction);
321  }
322  }
323  }
324 
325  if ($page === 0)
326  {
327  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
328  }
329  else if ($page === 1)
330  {
331  $this->object->finishSurvey($ilUser->id, $_SESSION["anonymous_id"]);
332  if (array_key_exists("anonymous_id", $_SESSION)) unset($_SESSION["anonymous_id"]);
333  $this->runShowFinishedPage();
334  return;
335  }
336  else
337  {
338  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_content.html", "Modules/Survey");
339 
340  if (!($this->object->getAnonymize() && $this->object->isAccessibleWithoutCode() && ($_SESSION["AccountId"] == ANONYMOUS_USER_ID)))
341  {
342  $this->tpl->setCurrentBlock("suspend_survey");
343  $this->tpl->setVariable("TEXT_SUSPEND", $this->lng->txt("cancel_survey"));
344  $this->tpl->setVariable("HREF_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
345  $this->tpl->setVariable("HREF_IMG_SUSPEND", $this->ctrl->getLinkTargetByClass("ilObjSurveyGUI", "infoScreen"));
346  $this->tpl->setVariable("ALT_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
347  $this->tpl->setVariable("TITLE_IMG_SUSPEND", $this->lng->txt("cancel_survey"));
348  $this->tpl->setVariable("IMG_SUSPEND", ilUtil::getImagePath("cancel.gif"));
349  $this->tpl->parseCurrentBlock();
350  }
351  $this->outNavigationButtons("top", $page);
352  $this->tpl->setCurrentBlock("percentage");
353  $percentage = (int)(($page[0]["position"])*100);
354  $this->tpl->setVariable("PERCENT_BAR_START", ilUtil::getImagePath("bar_start.gif"));
355  $this->tpl->setVariable("PERCENT_BAR_FILLED", ilUtil::getImagePath("bar_filled.gif"));
356  $this->tpl->setVariable("PERCENT_BAR_EMPTY", ilUtil::getImagePath("bar_empty.gif"));
357  $this->tpl->setVariable("PERCENT_BAR_END", ilUtil::getImagePath("bar_end.gif"));
358  $this->tpl->setVariable("PERCENTAGE_ALT", $this->lng->txt("percentage"));
359  $this->tpl->setVariable("PERCENTAGE_VALUE", $percentage);
360  $this->tpl->setVariable("PERCENTAGE_UNFINISHED", 100-$percentage);
361  $this->tpl->parseCurrentBlock();
362  if (count($page) > 1)
363  {
364  $this->tpl->setCurrentBlock("questionblock_title");
365  $this->tpl->setVariable("TEXT_QUESTIONBLOCK_TITLE", $page[0]["questionblock_title"]);
366  $this->tpl->parseCurrentBlock();
367  }
368  foreach ($page as $data)
369  {
370  $this->tpl->setCurrentBlock("survey_content");
371  if ($data["heading"])
372  {
373  $this->tpl->setVariable("QUESTION_HEADING", $data["heading"]);
374  }
375  $question_gui = $this->object->getQuestionGUI($data["type_tag"], $data["question_id"]);
376  if (is_array($_SESSION["svy_errors"]))
377  {
378  $working_data =& $question_gui->object->getWorkingDataFromUserInput($_SESSION["postdata"]);
379  }
380  else
381  {
382  $working_data = $this->object->loadWorkingData($data["question_id"], $_SESSION["finished_id"]);
383  }
384  $question_gui->object->setObligatory($data["obligatory"]);
385  $error_messages = array();
386  if (is_array($_SESSION["svy_errors"]))
387  {
388  $error_messages = $_SESSION["svy_errors"];
389  }
390  $show_questiontext = ($data["questionblock_show_questiontext"]) ? 1 : 0;
391  $question_output = $question_gui->getWorkingForm($working_data, $this->object->getShowQuestionTitles(), $show_questiontext, $error_messages[$data["question_id"]], $this->object->getSurveyId());
392  $this->tpl->setVariable("QUESTION_OUTPUT", $question_output);
393  $this->ctrl->setParameter($this, "qid", $data["question_id"]);
394  $this->tpl->parse("survey_content");
395  }
396  $this->outNavigationButtons("bottom", $page);
397  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "redirectQuestion"));
398  }
399  }
400 
406  function saveUserInput($navigationDirection = "next")
407  {
408  global $ilUser;
409  global $rbacsystem;
410 
411  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
412  {
413  // only with read access it is possible to run the test
414  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
415  }
416 
417  // check users input when it is a metric question
418  unset($_SESSION["svy_errors"]);
419  $_SESSION["postdata"] = $_POST;
420  $page_error = 0;
421  $page = $this->object->getNextPage($_GET["qid"], 0);
422  foreach ($page as $data)
423  {
424  $page_error += $this->saveActiveQuestionData($data);
425  }
426  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
427  {
428  if ($page_error == 1)
429  {
430  ilUtil::sendFailure($this->lng->txt("svy_page_error"), TRUE);
431  }
432  else
433  {
434  ilUtil::sendFailure($this->lng->txt("svy_page_errors"), TRUE);
435  }
436  }
437  else
438  {
439  $page_error = "";
440  unset($_SESSION["svy_errors"]);
441  }
442  return $page_error;
443  }
444 
452  function navigate($navigationDirection = "next")
453  {
454  global $ilUser;
455  global $rbacsystem;
456 
457  if (!$rbacsystem->checkAccess("read", $this->object->ref_id))
458  {
459  // only with read access it is possible to run the test
460  $this->ilias->raiseError($this->lng->txt("cannot_read_survey"),$this->ilias->error_obj->MESSAGE);
461  }
462 
463  // check users input when it is a metric question
464  unset($_SESSION["svy_errors"]);
465  $page_error = 0;
466  $page = $this->object->getNextPage($_GET["qid"], 0);
467  foreach ($page as $data)
468  {
469  $page_error += $this->saveActiveQuestionData($data);
470  }
471  if ($page_error && (strcmp($navigationDirection, "previous") != 0))
472  {
473  if ($page_error == 1)
474  {
475  ilUtil::sendFailure($this->lng->txt("svy_page_error"));
476  }
477  else
478  {
479  ilUtil::sendFailure($this->lng->txt("svy_page_errors"));
480  }
481  }
482  else
483  {
484  $page_error = "";
485  unset($_SESSION["svy_errors"]);
486  }
487 
488  $direction = 0;
489  switch ($navigationDirection)
490  {
491  case "next":
492  default:
493  $activepage = $_GET["qid"];
494  if (!$page_error)
495  {
496  $direction = 1;
497  }
498  break;
499  case "previous":
500  $activepage = $_GET["qid"];
501  if (!$page_error)
502  {
503  $direction = -1;
504  }
505  break;
506  }
507  $this->outSurveyPage($activepage, $direction);
508  }
509 
518  {
519  global $ilUser;
520 
521  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
522  $question =& SurveyQuestion::_instanciateQuestion($data["question_id"]);
523  $error = $question->checkUserInput($_POST, $this->object->getSurveyId());
524  if (strlen($error) == 0)
525  {
526  $user_id = $ilUser->getId();
527  // delete old answers
528  $this->object->deleteWorkingData($data["question_id"], $_SESSION["finished_id"]);
529 
530  if ($this->object->isSurveyStarted($user_id, $_SESSION["anonymous_id"]) === false)
531  {
532  $_SESSION["finished_id"] = $this->object->startSurvey($user_id, $_SESSION["anonymous_id"]);
533  }
534  if ($this->object->getAnonymize())
535  {
536  $user_id = 0;
537  }
538  $question->saveUserInput($_POST, $_SESSION["finished_id"]);
539  return 0;
540  }
541  else
542  {
543  $_SESSION["svy_errors"][$question->getId()] = $error;
544  return 1;
545  }
546  }
547 
555  function cancel()
556  {
557  $this->ctrl->redirectByClass("ilobjsurveygui", "infoScreen");
558  }
559 
568  {
569  unset($_SESSION["anonymous_id"]);
570  if (strlen($this->object->getOutro()) == 0)
571  {
572  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
573  }
574  else
575  {
576  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.il_svy_svy_finished.html", "Modules/Survey");
577  $this->tpl->setVariable("TEXT_FINISHED", $this->object->prepareTextareaOutput($this->object->getOutro()));
578  $this->tpl->setVariable("BTN_EXIT", $this->lng->txt("exit"));
579  $this->tpl->setVariable("FORM_ACTION", $this->ctrl->getFormAction($this, "runShowFinishedPage"));
580  }
581  }
582 
590  function exitSurvey()
591  {
592  $this->ctrl->redirectByClass("ilobjsurveygui", "backToRepository");
593  }
594 
604  function outNavigationButtons($navigationblock = "top", $page)
605  {
606  $prevpage = $this->object->getNextPage($page[0]["question_id"], -1);
607  $this->tpl->setCurrentBlock($navigationblock . "_prev");
608  if ($prevpage === 0)
609  {
610  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_start"));
611  }
612  else
613  {
614  $this->tpl->setVariable("BTN_PREV", $this->lng->txt("survey_previous"));
615  }
616  $this->tpl->parseCurrentBlock();
617  $nextpage = $this->object->getNextPage($page[0]["question_id"], 1);
618  $this->tpl->setCurrentBlock($navigationblock . "_next");
619  if ($nextpage === 1)
620  {
621  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_finish"));
622  }
623  else
624  {
625  $this->tpl->setVariable("BTN_NEXT", $this->lng->txt("survey_next"));
626  }
627  $this->tpl->parseCurrentBlock();
628  }
629 }
630 ?>