ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSurveyEditorGUI.php
Go to the documentation of this file.
1 <?php
2 
21 
31 {
32  protected \ILIAS\Survey\Sequence\SequenceManager $sequence_manager;
33  protected \ILIAS\Survey\PrintView\GUIService $print;
34  protected \ILIAS\HTTP\Services $http;
35  protected \ILIAS\DI\UIServices $ui;
36  protected string $requested_pgov;
39  protected ilCtrl $ctrl;
40  protected ilLanguage $lng;
42  protected ilTabsGUI $tabs;
43  protected ilHelpGUI $help;
45  protected ilObjUser $user;
46  protected ilTree $tree;
48  protected ilObjSurvey $object;
49  protected array $print_options;
50 
51  public function __construct(ilObjSurveyGUI $a_parent_gui)
52  {
53  global $DIC;
54 
55  $this->tabs = $DIC->tabs();
56  $this->help = $DIC["ilHelp"];
57  $this->toolbar = $DIC->toolbar();
58  $this->user = $DIC->user();
59  $this->tree = $DIC->repositoryTree();
60  $ilCtrl = $DIC->ctrl();
61  $lng = $DIC->language();
62  $tpl = $DIC["tpl"];
63 
64  $this->parent_gui = $a_parent_gui;
66  $survey = $this->parent_gui->getObject();
67  $this->object = $survey;
68 
69  $this->ctrl = $ilCtrl;
70  $this->lng = $lng;
71  $this->tpl = $tpl;
72 
73  $this->ctrl->saveParameter($this, array("pgov", "pgov_pos"));
74 
75  $this->print_options = array(
76  //0 => $this->lng->txt('none'),
77  ilObjSurvey::PRINT_HIDE_LABELS => $this->lng->txt('svy_print_hide_labels'),
78  //2 => $this->lng->txt('svy_print_label_only'),
79  ilObjSurvey::PRINT_SHOW_LABELS => $this->lng->txt('svy_print_show_labels')
80  );
81  $this->edit_manager = $DIC->survey()
82  ->internal()
83  ->domain()
84  ->edit();
85  $this->request = $DIC->survey()
86  ->internal()
87  ->gui()
88  ->editing()
89  ->request();
90  $this->requested_pgov = $this->request->getTargetPosition();
91  $this->ui = $DIC->ui();
92  $this->http = $DIC->http();
93  $this->print = $DIC->survey()
94  ->internal()
95  ->gui()
96  ->print();
97  $this->sequence_manager = $DIC->survey()->internal()->domain()->sequence(
98  $this->object->getSurveyId(),
100  );
101  }
102 
103  public function setRequestedPgov(string $pgov): void
104  {
105  $this->requested_pgov = $pgov;
106  }
107 
108  public function executeCommand(): void
109  {
110  $ilTabs = $this->tabs;
111 
112  $cmd = $this->ctrl->getCmd("questions");
113 
114  if ($this->requested_pgov !== "") {
115  if ($cmd === "questions") {
116  $this->ctrl->setCmdClass("ilSurveyPageEditGUI");
117  $this->ctrl->setCmd("renderpage");
118  } elseif ($cmd === "confirmRemoveQuestions") {
119  // #14324
120  $this->ctrl->setCmdClass("ilSurveyPageEditGUI");
121  $this->ctrl->setCmd("confirmRemoveQuestions");
122  }
123  }
124 
125  $next_class = $this->ctrl->getNextClass($this);
126  switch ($next_class) {
127  case 'ilsurveypageeditgui':
128  $this->questionsSubtabs("page");
129  $pg = new ilSurveyPageEditGUI($this->object, $this);
130  $this->ctrl->forwardCommand($pg);
131  break;
132 
133  default:
134  // question gui
135  if (stripos($next_class, "questiongui") !== false) {
136  $ilTabs->clearTargets();
137  $this->ctrl->saveParameter($this, array("new_for_survey"));
139  null,
140  $this->request->getQuestionId()
141  );
142  if (is_object($q_gui->object)) {
143  $ilHelp = $this->help;
144  $ilHelp->setScreenIdComponent("spl_qt" . $q_gui->object->getQuestionTypeId());
145  }
146  // $q_gui->object->setObjId($this->object->getId());
147  $q_gui->setBackUrl($this->ctrl->getLinkTarget($this, "questions"));
148  $q_gui->setQuestionTabs();
149  $this->ctrl->forwardCommand($q_gui);
150 
151  if (!$this->request->getNewForSurvey()) {
152  // not on create
153  $this->tpl->setTitle($this->lng->txt("question") . ": " . $q_gui->object->getTitle());
154  }
155  } else {
156  $cmd .= "Object";
157  $this->$cmd();
158  }
159  break;
160  }
161  }
162 
163  protected function questionsSubtabs(
164  string $a_cmd
165  ): void {
166  $ilTabs = $this->tabs;
167 
168  if ($a_cmd === "questions" && $this->requested_pgov !== "") {
169  $a_cmd = "page";
170  }
171 
172  $ilTabs->addSubTab(
173  "page",
174  $this->lng->txt("survey_per_page_view"),
175  $this->ctrl->getLinkTargetByClass("ilSurveyPageEditGUI", "renderPage")
176  );
177 
178  $this->ctrl->setParameter($this, "pgov", "");
179  $ilTabs->addSubTab(
180  "questions",
181  $this->lng->txt("survey_question_editor"),
182  $this->ctrl->getLinkTarget($this, "questions")
183  );
184  $this->ctrl->setParameter($this, "pgov", $this->requested_pgov);
185 
186  if ($this->object->getSurveyPages()) {
187  if ($a_cmd === "page") {
188  $this->ctrl->setParameterByClass("ilsurveyexecutiongui", "pgov", max(1, $this->request->getPage()));
189  }
190  $this->ctrl->setParameterByClass("ilsurveyexecutiongui", "prvw", 1);
191  $ilTabs->addSubTab(
192  "preview",
193  $this->lng->txt("preview"),
194  $this->ctrl->getLinkTargetByClass(array("ilobjsurveygui", "ilsurveyexecutiongui"), "preview")
195  );
196  }
197 
198  $ilTabs->activateSubTab($a_cmd);
199  }
200 
201 
202  //
203  // QUESTIONS BROWSER INCL. MULTI-ACTIONS
204  //
205 
206  public function questionsObject(): void
207  {
208  $ilToolbar = $this->toolbar;
210 
211  // insert new questions?
212  if ($this->request->getNewId() > 0) {
213  // add a question to the survey previous created in a questionpool
214  $existing = $this->object->getExistingQuestions();
215  if (!in_array($this->request->getNewId(), $existing)) {
216  $inserted = $this->object->insertQuestion($this->request->getNewId());
217  if (!$inserted) {
218  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("survey_error_insert_incomplete_question"));
219  } else {
220  // this ensures the status update of the survey, see #36162
221  $this->ctrl->redirect($this, "questions");
222  }
223  }
224  }
225  $this->questionsSubtabs("questions");
226 
227  $hasDatasets = ilObjSurvey::_hasDatasets($this->object->getSurveyId());
228  $read_only = $hasDatasets;
229 
230  // toolbar
231  if (!$read_only) {
232  $qtypes = array();
233  foreach (ilObjSurveyQuestionPool::_getQuestiontypes() as $translation => $data) {
234  $qtypes[$data["type_tag"]] = $translation;
235  }
236 
237  $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
238  $types = new ilSelectInputGUI($this->lng->txt("create_new"), "sel_question_types");
239  $types->setOptions($qtypes);
240  $ilToolbar->addStickyItem($types, "");
241 
242  $button = ilSubmitButton::getInstance();
243  $button->setCaption("svy_create_question");
244  $button->setCommand("createQuestion");
245  $ilToolbar->addStickyItem($button);
246 
247  if ($this->object->getPoolUsage()) {
248  $cmd = ((int) $ilUser->getPref('svy_insert_type') === 1 ||
249  ($ilUser->getPref('svy_insert_type') ?? '') === '')
250  ? 'browseForQuestions'
251  : 'browseForQuestionblocks';
252 
253  $button = ilLinkButton::getInstance();
254  $button->setCaption("browse_for_questions");
255  $button->setUrl($this->ctrl->getLinkTarget($this, $cmd));
256  $ilToolbar->addStickyItem($button);
257  }
258 
259  $ilToolbar->addSeparator();
260 
261  $button = ilLinkButton::getInstance();
262  $button->setCaption("add_heading");
263  $button->setUrl($this->ctrl->getLinkTarget($this, "addHeading"));
264  $ilToolbar->addInputItem($button);
265 
266  $ilToolbar->addSeparator();
267  $print_view = $this->print->list($this->object->getRefId());
268  $modal_elements = $print_view->getModalElements(
269  $this->ctrl->getLinkTarget(
270  $this,
271  "printListViewSelection"
272  )
273  );
274  $ilToolbar->addComponent($modal_elements->button);
275  $ilToolbar->addComponent($modal_elements->modal);
276  }
277  $mess = "";
278  if ($hasDatasets) {
279  $mbox = new ilSurveyContainsDataMessageBoxGUI();
280  $mess = $mbox->getHTML();
281  }
282 
283  // table gui
284  $table = new ilSurveyQuestionTableGUI(
285  $this,
286  "questions",
287  $this->object,
288  $read_only
289  );
290  $this->tpl->setContent($mess . $table->getHTML());
291  }
292 
297  protected function gatherSelectedTableItems(
298  bool $allow_blocks = true,
299  bool $allow_questions = true,
300  bool $allow_headings = false,
301  bool $allow_questions_in_blocks = false
302  ): array {
303  $block_map = array();
304  foreach ($this->object->getSurveyQuestions() as $item) {
305  $block_map[$item["question_id"]] = $item["questionblock_id"];
306  }
307 
308  $questions = $blocks = $headings = array();
309  $ids = $this->request->getIds();
310  if (count($ids) > 0) {
311  foreach ($ids as $key) {
312  // questions
313  if ($allow_questions && preg_match("/cb_(\d+)/", $key, $matches)) {
314  if (($allow_questions_in_blocks || !$block_map[$matches[1]]) &&
315  !in_array($block_map[$matches[1]], $blocks)) {
316  $questions[] = $matches[1];
317  }
318  }
319  // blocks
320  if ($allow_blocks && preg_match("/cb_qb_(\d+)/", $key, $matches)) {
321  $blocks[] = $matches[1];
322  }
323  // headings
324  if ($allow_headings && preg_match("/cb_tb_(\d+)/", $key, $matches)) {
325  $headings[] = $matches[1];
326  }
327  }
328  }
329 
330  return array("questions" => $questions,
331  "blocks" => $blocks,
332  "headings" => $headings);
333  }
334 
335  public function saveObligatoryObject(): void
336  {
337  $req_order = $this->request->getOrder();
338  $req_block_order = $this->request->getBlockOrder();
339  if (count($req_order) > 0) {
340  $position = -1;
341  $order = array();
342  asort($req_order);
343  foreach (array_keys($req_order) as $id) {
344  // block items
345  if (strpos($id, "qb_") === 0) {
346  $block_id = substr($id, 3);
347  $block = $req_block_order[$block_id];
348  asort($block);
349  foreach (array_keys($block) as $question_id) {
350  $position++;
351  $order[$question_id] = $position;
352  }
353  } else {
354  $question_id = substr($id, 2);
355  $position++;
356  $order[$question_id] = $position;
357  }
358  }
359  $this->object->updateOrder($order);
360  }
361 
362  $obligatory = $this->request->getObligatory();
363  $this->object->setObligatoryStates($obligatory);
364  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
365  $this->ctrl->redirect($this, "questions");
366  }
367 
368  public function unfoldQuestionblockObject(): void
369  {
370  $items = $this->gatherSelectedTableItems(true, false, false, false);
371  if (count($items["blocks"])) {
372  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
373  $this->object->unfoldQuestionblocks($items["blocks"]);
374  } else {
375  $this->tpl->setOnScreenMessage('info', $this->lng->txt("qpl_unfold_select_none"), true);
376  }
377  $this->ctrl->redirect($this, "questions");
378  }
379 
380  public function moveQuestionsObject(): void
381  {
382  $items = $this->gatherSelectedTableItems(true, true, false, false);
383 
384  $move_questions = $items["questions"];
385  foreach ($items["blocks"] as $block_id) {
386  foreach ($this->object->getQuestionblockQuestionIds($block_id) as $qid) {
387  $move_questions[] = $qid;
388  }
389  }
390  if (count($move_questions) === 0) {
391  $this->tpl->setOnScreenMessage('info', $this->lng->txt("no_question_selected_for_move"), true);
392  $this->ctrl->redirect($this, "questions");
393  } else {
394  $this->edit_manager->setMoveSurveyQuestions($this->object->getId(), $move_questions);
395  $this->tpl->setOnScreenMessage('info', $this->lng->txt("select_target_position_for_move_question"));
396  $this->questionsObject();
397  }
398  }
399 
400  public function insertQuestionsBeforeObject(): void
401  {
402  $this->insertQuestions(0);
403  }
404 
405  public function insertQuestionsAfterObject(): void
406  {
407  $this->insertQuestions(1);
408  }
409 
410  protected function insertQuestions(
411  int $insert_mode
412  ): void {
413  $insert_id = null;
414  $ids = $this->request->getIds();
415  if (count($ids) > 0) {
416  $items = $this->gatherSelectedTableItems(true, true, false, false);
417 
418  // we are using POST id for original order
419  while (!$insert_id && count($ids) > 0) {
420  $target = array_shift($ids);
421  if (preg_match("/^cb_(\d+)$/", $target, $matches)) {
422  // questions in blocks are not allowed
423  if (in_array($matches[1], $items["questions"])) {
424  $insert_id = $matches[1];
425  }
426  }
427  if (!$insert_id && preg_match("/^cb_qb_(\d+)$/", $target, $matches)) {
428  $ids = $this->object->getQuestionblockQuestionIds($matches[1]);
429  if (count($ids)) {
430  if ($insert_mode === 0) {
431  $insert_id = $ids[0];
432  } elseif ($insert_mode === 1) {
433  $insert_id = $ids[count($ids) - 1];
434  }
435  }
436  }
437  }
438  }
439 
440  if (!$insert_id) {
441  $this->tpl->setOnScreenMessage('info', $this->lng->txt("no_target_selected_for_move"), true);
442  } else {
443  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
444  if ($this->edit_manager->getMoveSurveyId() === $this->object->getId()) {
445  $this->object->moveQuestions(
446  $this->edit_manager->getMoveSurveyQuestions(),
447  $insert_id,
448  $insert_mode
449  );
450  $this->edit_manager->clearMoveSurveyQuestions();
451  }
452  }
453 
454  $this->ctrl->redirect($this, "questions");
455  }
456 
457  public function removeQuestionsObject(): void
458  {
459  $items = $this->gatherSelectedTableItems(true, true, true, true);
460  if (count($items["blocks"]) + count($items["questions"]) + count($items["headings"]) > 0) {
461  $this->tpl->setOnScreenMessage('question', $this->lng->txt("remove_questions"));
462  $this->removeQuestionsForm($items["blocks"], $items["questions"], $items["headings"]);
463  } else {
464  $this->tpl->setOnScreenMessage('info', $this->lng->txt("no_question_selected_for_removal"), true);
465  $this->ctrl->redirect($this, "questions");
466  }
467  }
468 
469  public function removeQuestionsForm(
470  array $checked_questionblocks,
471  array $checked_questions,
472  array $checked_headings
473  ): void {
474  $cgui = new ilConfirmationGUI();
475  $cgui->setHeaderText($this->lng->txt("survey_sure_delete_questions"));
476  $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmRemoveQuestions"));
477  $cgui->setCancel($this->lng->txt("cancel"), "questions");
478  $cgui->setConfirm($this->lng->txt("confirm"), "confirmRemoveQuestions");
479  $surveyquestions = $this->object->getSurveyQuestions();
480  foreach ($surveyquestions as $question_id => $data) {
481  if (in_array($data["question_id"], $checked_questions)) {
483 
484  $cgui->addItem(
485  "q_id[]",
486  $data["question_id"],
487  $type . ": " . $data["title"]
488  );
489  } elseif ((in_array($data["questionblock_id"], $checked_questionblocks))) {
491 
492  $cgui->addItem(
493  "cb[" . $data["questionblock_id"] . "]",
494  $data["questionblock_id"],
495  $data["questionblock_title"] . " - " . $type . ": " . $data["title"]
496  );
497  } elseif (in_array($data["question_id"], $checked_headings)) {
498  $cgui->addItem(
499  "heading[" . $data["question_id"] . "]",
500  $data["question_id"],
501  $data["heading"]
502  );
503  }
504  }
505 
506  $this->tpl->setContent($cgui->getHTML());
507  }
508 
509  public function confirmRemoveQuestionsObject(): void
510  {
511  $checked_questions = $this->request->getQuestionIds();
512  $checked_questionblocks = $this->request->getBlockIds();
513  $checked_headings = $this->request->getHeadings();
514 
515  if (count($checked_questions) || count($checked_questionblocks)) {
516  $this->object->removeQuestions($checked_questions, $checked_questionblocks);
517  }
518  if ($checked_headings) {
519  foreach ($checked_headings as $q_id) {
520  $this->object->saveHeading("", $q_id);
521  }
522  }
523  $this->object->saveCompletionStatus();
524  $this->tpl->setOnScreenMessage('success', $this->lng->txt("questions_removed"), true);
525  $this->ctrl->redirect($this, "questions");
526  }
527 
528  public function copyQuestionsToPoolObject(): void
529  {
530  $items = $this->gatherSelectedTableItems(true, true, false, true);
531 
532  // gather questions from blocks
533  $copy_questions = $items["questions"];
534  foreach ($items["blocks"] as $block_id) {
535  foreach ($this->object->getQuestionblockQuestionIds($block_id) as $qid) {
536  $copy_questions[] = $qid;
537  }
538  }
539  $copy_questions = array_unique($copy_questions);
540 
541  // only if not already in pool
542  if (count($copy_questions)) {
543  foreach ($copy_questions as $idx => $question_id) {
544  $question = ilObjSurvey::_instanciateQuestion($question_id);
545  if ($question->getOriginalId()) {
546  unset($copy_questions[$idx]);
547  }
548  }
549  }
550  if (count($copy_questions) === 0) {
551  $this->tpl->setOnScreenMessage('info', $this->lng->txt("no_question_selected_for_copy_to_pool"), true);
552  $this->ctrl->redirect($this, "questions");
553  } else {
554  $this->questionsSubtabs("questions");
555 
556  $form = new ilPropertyFormGUI();
557 
558  $form->setFormAction($this->ctrl->getFormAction($this, "executeCreateQuestion"));
559 
560  $ids = new ilHiddenInputGUI("question_ids");
561  $ids->setValue(implode(";", $copy_questions));
562  $form->addItem($ids);
563 
564  $questionpools = $this->object->getAvailableQuestionpools(false, false, true, "write");
565  $pools = new ilSelectInputGUI($this->lng->txt("survey_copy_select_questionpool"), "sel_spl");
566  $pools->setOptions($questionpools);
567  $form->addItem($pools);
568 
569  $form->addCommandButton("executeCopyQuestionsToPool", $this->lng->txt("submit"));
570  $form->addCommandButton("questions", $this->lng->txt("cancel"));
571 
572  $this->tpl->setContent($form->getHTML());
573  }
574  }
575 
576  public function executeCopyQuestionsToPoolObject(): void
577  {
578  $question_ids = $this->request->getQuestionIdsFromString();
579  $pool_id = ilObject::_lookupObjId($this->request->getSelectedPool());
580 
581  foreach ($question_ids as $qid) {
582  // create copy (== pool "original")
583  $new_question = ilObjSurvey::_instanciateQuestion($qid);
584  $new_question->setId();
585  $new_question->setObjId($pool_id);
586  $new_question->saveToDb();
587 
588  // link "source" (survey) to copy (pool)
589  SurveyQuestion::_changeOriginalId($qid, $new_question->getId(), $pool_id);
590  }
591 
592  $this->tpl->setOnScreenMessage('success', $this->lng->txt("survey_copy_to_questionpool_success"), true);
593  $this->ctrl->redirect($this, "questions");
594  }
595 
596 
597  //
598  // QUESTION CREATION
599  //
600 
601  public function createQuestionObject(
602  ilPropertyFormGUI $a_form = null,
603  $sel_question_types = null,
604  string $pgov_pos = null
605  ): ?ilPropertyFormGUI {
606  if (!$this->object->getPoolUsage()) {
607  $this->executeCreateQuestionObject(null, 1, $pgov_pos);
608  return null;
609  }
610 
611  if (!$a_form) {
612  $this->questionsSubtabs("questions");
613  $form = new ilPropertyFormGUI();
614 
615  if (is_null($sel_question_types)) {
616  $sel_question_types = $this->request->getSelectedQuestionTypes();
617  }
618  $this->ctrl->setParameter($this, "sel_question_types", $sel_question_types);
619  $form->setFormAction($this->ctrl->getFormAction($this, "executeCreateQuestion"));
620  } else {
621  $form = $a_form;
622  }
623 
624  $usage = new ilRadioGroupInputGUI($this->lng->txt("survey_pool_selection"), "usage");
625  $usage->setRequired(true);
626  $no_pool = new ilRadioOption($this->lng->txt("survey_no_pool"), 1);
627  $usage->addOption($no_pool);
628  $existing_pool = new ilRadioOption($this->lng->txt("survey_existing_pool"), 3);
629  $usage->addOption($existing_pool);
630  $new_pool = new ilRadioOption($this->lng->txt("survey_new_pool"), 2);
631  $usage->addOption($new_pool);
632  $form->addItem($usage);
633 
634  if ($this->edit_manager->getPoolChoice() > 0) {
635  $usage->setValue($this->edit_manager->getPoolChoice());
636  } else {
637  // default: no pool
638  $usage->setValue(1);
639  }
640 
641  $questionpools = $this->object->getAvailableQuestionpools(false, true, true, "write");
642  $pools = new ilSelectInputGUI($this->lng->txt("select_questionpool"), "sel_spl");
643  $pools->setOptions($questionpools);
644  $existing_pool->addSubItem($pools);
645 
646  $name = new ilTextInputGUI($this->lng->txt("spl_new"), "name_spl"); // #11740
647  $name->setSize(50);
648  $name->setMaxLength(50);
649  $new_pool->addSubItem($name);
650 
651  if ($a_form) {
652  return $a_form;
653  }
654 
655  $form->addCommandButton("executeCreateQuestion", $this->lng->txt("submit"));
656  $form->addCommandButton("questions", $this->lng->txt("cancel"));
657 
658  $this->tpl->setContent($form->getHTML());
659  return null;
660  }
661 
663  ?string $q_type = null,
664  ?int $pool_usage = null,
665  ?string $pgov_pos = null
666  ): void {
667  $this->edit_manager->setPoolChoice($this->request->getPoolUsage());
668 
669  if (is_null($q_type)) {
670  $q_type = $this->request->getSelectedQuestionTypes();
671  }
672 
673  $pgov = $this->requested_pgov;
674  if (is_null($pgov_pos)) {
675  $pgov_pos = $this->request->getTargetQuestionPosition();
676  }
677 
678  if (is_null($pool_usage)) {
679  $pool_usage = $this->request->getPoolUsage();
680  }
681 
682  $obj_id = 0;
683 
684  // no pool
685  if ($pool_usage == 1) {
686  $obj_id = $this->object->getId();
687  }
688  // existing pool
689  elseif ($pool_usage == 3 && $this->request->getSelectedPool() > 0) {
690  $obj_id = ilObject::_lookupObjId($this->request->getSelectedPool());
691  }
692  // new pool
693  elseif ($pool_usage == 2 && $this->request->getPoolName() !== "") {
694  $obj_id = $this->createQuestionPool($this->request->getPoolName());
695  } else {
696  if (!$pool_usage) {
697  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("select_one"), true);
698  } else {
699  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("err_no_pool_name"), true);
700  }
701  $this->ctrl->setParameter($this, "sel_question_types", $q_type);
702  $this->ctrl->redirect($this, "createQuestion");
703  }
704 
705 
706  // create question and redirect to question form
707 
708  $q_gui = SurveyQuestionGUI::_getQuestionGUI($q_type);
709  $q_gui->object->setObjId($obj_id); // survey/pool!
710  $q_gui->object->createNewQuestion();
711  $q_gui_class = get_class($q_gui);
712 
713  if ($pgov !== "") {
714  $this->ctrl->setParameterByClass($q_gui_class, "pgov", $pgov);
715  $this->ctrl->setParameterByClass($q_gui_class, "pgov_pos", $pgov_pos);
716  }
717 
718  $this->ctrl->setParameterByClass($q_gui_class, "ref_id", $this->object->getRefId());
719  $this->ctrl->setParameterByClass($q_gui_class, "new_for_survey", $this->object->getRefId());
720  $this->ctrl->setParameterByClass($q_gui_class, "q_id", $q_gui->object->getId());
721  $this->ctrl->setParameterByClass($q_gui_class, "sel_question_types", $q_gui->getQuestionType());
722  $this->ctrl->redirectByClass($q_gui_class, "editQuestion");
723  }
724 
725  protected function createQuestionPool($name = "dummy"): int
726  {
727  $tree = $this->tree;
728 
729  $parent_ref = $tree->getParentId($this->object->getRefId());
730 
731  $qpl = new ilObjSurveyQuestionPool();
732  $qpl->setType("spl");
733  $qpl->setTitle($name);
734  $qpl->setDescription("");
735  $qpl->create();
736  $qpl->createReference();
737  $qpl->putInTree($parent_ref);
738  $qpl->setPermissions($parent_ref);
739  $qpl->setOnline(1); // must be online to be available
740  $qpl->saveToDb();
741 
742  return $qpl->getId();
743  }
744 
745 
746  //
747  // ADD FROM POOL
748  //
749 
750  protected function setBrowseForQuestionsSubtabs(): void
751  {
752  $ilTabs = $this->tabs;
753  $ilToolbar = $this->toolbar;
755 
756  if ($this->requested_pgov === "") {
757  $link = $this->ctrl->getLinkTarget($this, "questions");
758  } else {
759  $link = $this->ctrl->getLinkTargetByClass("ilSurveyPageEditGUI", "renderpage");
760  }
761  $ilTabs->setBackTarget($this->lng->txt("menubacktosurvey"), $link);
762 
763  // type selector
764  $types = new ilSelectInputGUI($this->lng->txt("display_all_available"), "datatype");
765  $types->setOptions(array(
766  1 => $this->lng->txt("questions"),
767  2 => $this->lng->txt("questionblocks")
768  ));
769  $types->setValue($ilUser->getPref('svy_insert_type'));
770  $ilToolbar->addInputItem($types, true);
771  $ilToolbar->addFormButton($this->lng->txt("change"), "changeDatatype");
772  $ilToolbar->setFormAction($this->ctrl->getFormAction($this, "changeDatatype"));
773  }
774 
775  public function changeDatatypeObject(): void
776  {
778 
779  $ilUser->writePref('svy_insert_type', $this->request->getDataType());
780 
781  switch ($this->request->getDataType()) {
782  case 2:
783  $this->ctrl->redirect($this, 'browseForQuestionblocks');
784  break;
785 
786  case 1:
787  default:
788  $this->ctrl->redirect($this, 'browseForQuestions');
789  break;
790  }
791  }
792 
793  public function browseForQuestionsObject(): void
794  {
796 
797  $table_gui = new ilSurveyQuestionbrowserTableGUI($this, 'browseForQuestions', $this->object, true);
798  $table_gui->setEditable(true);
799  $this->tpl->setContent($table_gui->getHTML());
800  }
801 
802  public function filterQuestionBrowserObject(): void
803  {
804  $table_gui = new ilSurveyQuestionbrowserTableGUI($this, 'browseForQuestions', $this->object);
805  $table_gui->writeFilterToSession();
806  $this->ctrl->redirect($this, 'browseForQuestions');
807  }
808 
809  public function resetfilterQuestionBrowserObject(): void
810  {
811  $table_gui = new ilSurveyQuestionbrowserTableGUI($this, 'browseForQuestions', $this->object);
812  $table_gui->resetFilter();
813  $this->ctrl->redirect($this, 'browseForQuestions');
814  }
815 
816  public function insertQuestionsObject(): void
817  {
818  $inserted_objects = 0;
819  $page_gui = null;
820  $qids = $this->request->getQuestionIds();
821  if (count($qids) > 0) {
822  if ($this->requested_pgov !== "") {
823  $page_gui = new ilSurveyPageEditGUI($this->object, $this);
824  $page_gui->determineCurrentPage();
825 
826  // as target position is predefined, insert in reverse order
827  $qids = array_reverse($qids);
828  }
829  foreach ($qids as $question_id) {
830  if ($this->requested_pgov === "") {
831  $this->object->insertQuestion($question_id);
832  } else {
833  // "pgov" must be set to 1 to land here
834  // target position in page (pgov_pos) is processed there
835  $page_gui->insertNewQuestion($question_id);
836  }
837  $inserted_objects++;
838  }
839  }
840  if ($inserted_objects) {
841  $this->object->saveCompletionStatus();
842  $this->tpl->setOnScreenMessage('success', $this->lng->txt("questions_inserted"), true);
843  if ($this->requested_pgov === "") {
844  $this->ctrl->redirect($this, "questions");
845  } else {
846  $target_page = $this->requested_pgov;
847  if (substr($this->request->getTargetQuestionPosition(), -1) === "c") {
848  // see ilSurveyPageEditGUI::insertNewQuestion()
849  if ((int) $this->request->getTargetQuestionPosition()) {
850  $target_page++;
851  } else {
852  $target_page = 1;
853  }
854  }
855  $this->ctrl->setParameterByClass("ilSurveyPageEditGUI", "pgov", $target_page);
856  $this->ctrl->redirectByClass("ilSurveyPageEditGUI", "renderpage");
857  }
858  } else {
859  $this->tpl->setOnScreenMessage('info', $this->lng->txt("insert_missing_question"), true);
860  $this->ctrl->redirect($this, 'browseForQuestions');
861  }
862  }
863 
864  public function browseForQuestionblocksObject(): void
865  {
867 
868  $table_gui = new ilSurveyQuestionblockbrowserTableGUI($this, 'browseForQuestionblocks', $this->object, true);
869  $table_gui->setEditable(true);
870  $this->tpl->setContent($table_gui->getHTML());
871  }
872 
873  public function filterQuestionblockBrowserObject(): void
874  {
875  $table_gui = new ilSurveyQuestionblockbrowserTableGUI($this, 'browseForQuestionblocks', $this->object);
876  $table_gui->writeFilterToSession();
877  $this->ctrl->redirect($this, 'browseForQuestionblocks');
878  }
879 
881  {
882  $table_gui = new ilSurveyQuestionblockbrowserTableGUI($this, 'browseForQuestionblocks', $this->object);
883  $table_gui->resetFilter();
884  $this->ctrl->redirect($this, 'browseForQuestionblocks');
885  }
886 
887  public function insertQuestionblocksObject(): void
888  {
889  $inserted_objects = 0;
890  $page_gui = null;
891  $block_ids = $this->request->getBlockIds();
892  if (count($block_ids) > 0) {
893  if ($this->requested_pgov !== "") {
894  $page_gui = new ilSurveyPageEditGUI($this->object, $this);
895  $page_gui->determineCurrentPage();
896 
897  // as target position is predefined, insert in reverse order
898  $block_ids = array_reverse($block_ids);
899  }
900  foreach ($block_ids as $questionblock_id) {
901  if ($this->requested_pgov === "") {
902  $this->object->insertQuestionblock($questionblock_id);
903  } else {
904  $page_gui->insertQuestionBlock($questionblock_id);
905  }
906  $inserted_objects++;
907  }
908  }
909  if ($inserted_objects) {
910  $this->object->saveCompletionStatus();
911  $this->tpl->setOnScreenMessage('success', ($inserted_objects === 1) ? $this->lng->txt("questionblock_inserted") : $this->lng->txt("questionblocks_inserted"), true);
912  if ($this->requested_pgov === "") {
913  $this->ctrl->redirect($this, "questions");
914  } else {
915  $target_page = $this->requested_pgov;
916  if (substr($this->request->getTargetQuestionPosition(), -1) === "c") {
917  $target_page++;
918  }
919  $this->ctrl->setParameterByClass("ilSurveyPageEditGUI", "pgov", $target_page);
920  $this->ctrl->redirectByClass("ilSurveyPageEditGUI", "renderpage");
921  }
922  } else {
923  $this->tpl->setOnScreenMessage('info', $this->lng->txt("insert_missing_questionblock"), true);
924  $this->ctrl->redirect($this, 'browseForQuestionblocks');
925  }
926  }
927 
928 
929  //
930  // BLOCKS
931  //
932 
933  public function editQuestionblockObject(
934  ilPropertyFormGUI $a_form = null
935  ): void {
936  $block_id = $this->request->getBlockId();
937  $this->ctrl->setParameter($this, "bl_id", $block_id);
938 
939  if (!$a_form) {
940  $a_form = $this->initQuestionblockForm($block_id);
941  }
942 
943  $this->questionsSubtabs("questions");
944  $this->tpl->setContent($a_form->getHTML());
945  }
946 
947  public function createQuestionblockObject(
948  ilPropertyFormGUI $a_form = null
949  ): void {
950  if (!$a_form) {
951  // gather questions from table selected
952  $items = $this->gatherSelectedTableItems(false, true, false, false);
953 
954  $qids = $this->request->getQuestionIds();
955  if (count($qids) > 0) {
956  $items["questions"] = $qids;
957  }
958  if (count($items["questions"]) < 2) {
959  $this->tpl->setOnScreenMessage('info', $this->lng->txt("qpl_define_questionblock_select_missing"), true);
960  $this->ctrl->redirect($this, "questions");
961  }
962 
963  $a_form = $this->initQuestionblockForm(null, $items["questions"]);
964  }
965 
966  $this->questionsSubtabs("questions");
967  $this->tpl->setContent($a_form->getHTML());
968  }
969 
970  protected function initQuestionblockForm(
971  ?int $a_block_id = null,
972  ?array $a_question_ids = null
973  ): ilPropertyFormGUI {
974  $questionblock = null;
975  $form = new ilPropertyFormGUI();
976  $form->setFormAction($this->ctrl->getFormAction($this, "saveDefineQuestionblock"));
977  $form->setTitle($this->lng->txt("define_questionblock"));
978 
979  $title = new ilTextInputGUI($this->lng->txt("title"), "title");
980  $title->setRequired(true);
981  $form->addItem($title);
982 
983  $toggle_blocktitle = new ilCheckboxInputGUI($this->lng->txt("survey_show_blocktitle"), "show_blocktitle");
984  $toggle_blocktitle->setInfo($this->lng->txt("survey_show_blocktitle_description"));
985  $form->addItem($toggle_blocktitle);
986 
987  $toggle_questiontitle = new ilCheckboxInputGUI($this->lng->txt("show_questiontext"), "show_questiontext");
988  $toggle_questiontitle->setInfo($this->lng->txt("show_questiontext_description"));
989  $form->addItem($toggle_questiontitle);
990 
991  if ($a_block_id) {
992  $questionblock = ilObjSurvey::_getQuestionblock($a_block_id);
993  $title->setValue($questionblock["title"]);
994  $toggle_blocktitle->setChecked((bool) $questionblock["show_blocktitle"]);
995  $toggle_questiontitle->setChecked((bool) $questionblock["show_questiontext"]);
996  } else {
997  $toggle_blocktitle->setChecked(true);
998  $toggle_questiontitle->setChecked(true);
999  }
1000 
1001  $compress_view = new ilCheckboxInputGUI($this->lng->txt("svy_compress_view"), "compress_view");
1002  $compress_view->setInfo($this->lng->txt("svy_compress_view_info"));
1003  $compress_view->setChecked((bool) ($questionblock["compress_view"] ?? false));
1004  $form->addItem($compress_view);
1005 
1006  $form->addCommandButton("saveDefineQuestionblock", $this->lng->txt("save"));
1007  $form->addCommandButton("questions", $this->lng->txt("cancel"));
1008 
1009  // reload?
1010  $qids = $this->request->getQuestionIds();
1011  if (!$a_question_ids && count($qids) > 0) {
1012  $a_question_ids = $qids;
1013  }
1014 
1015  if ($a_question_ids) {
1016  foreach ($a_question_ids as $q_id) {
1017  $hidden = new ilHiddenInputGUI("qids[]");
1018  $hidden->setValue($q_id);
1019  $form->addItem($hidden);
1020  }
1021  }
1022 
1023  return $form;
1024  }
1025 
1026  public function saveDefineQuestionblockObject(): void
1027  {
1028  $block_id = $this->request->getBlockId();
1029  $q_ids = $this->request->getQuestionIds();
1030 
1031  $this->ctrl->setParameter($this, "bl_id", $block_id);
1032 
1033  if (!$block_id && count($q_ids) === 0) {
1034  $this->ctrl->redirect($this, "questions");
1035  }
1036 
1037  $form = $this->initQuestionblockForm($block_id);
1038  if ($form->checkInput()) {
1039  $title = $form->getInput("title");
1040  $show_questiontext = $form->getInput("show_questiontext");
1041  $show_blocktitle = $form->getInput("show_blocktitle") ;
1042  $compress_view = $form->getInput("compress_view") ;
1043  if ($block_id) {
1044  $this->object->modifyQuestionblock(
1045  $block_id,
1046  $title,
1047  $show_questiontext,
1048  $show_blocktitle,
1049  $compress_view
1050  );
1051  } elseif ($q_ids) {
1052  $this->object->createQuestionblock(
1053  $title,
1054  $show_questiontext,
1055  $show_blocktitle,
1056  $q_ids,
1057  $compress_view
1058  );
1059  }
1060 
1061  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_obj_modified'), true);
1062  $this->ctrl->redirect($this, "questions");
1063  }
1064 
1065  $form->setValuesByPost();
1066  $this->editQuestionblockObject($form);
1067  }
1068 
1069 
1070  //
1071  // HEADING
1072  //
1073 
1074  protected function initHeadingForm(
1075  ?int $a_question_id = null
1076  ): ilPropertyFormGUI {
1077  $survey_questions = $this->object->getSurveyQuestions();
1078 
1079  $form = new ilPropertyFormGUI();
1080  $form->setFormAction($this->ctrl->getFormAction($this, ""));
1081 
1082  // heading
1083  $heading = new ilTextAreaInputGUI($this->lng->txt("heading"), "heading");
1084  $heading->setRows(10);
1085  $heading->setCols(80);
1086  if (ilObjAdvancedEditing::_getRichTextEditor() === "tinymce") {
1087  $heading->setUseRte(true);
1088  $heading->setRteTags(ilObjAdvancedEditing::_getUsedHTMLTags("survey"));
1089  $heading->removePlugin(ilRTE::ILIAS_IMG_MANAGER_PLUGIN);
1090  $heading->setRTESupport($this->object->getId(), "svy", "survey");
1091  }
1092  $heading->setRequired(true);
1093  $form->addItem($heading);
1094 
1095  $insertbefore = new ilSelectInputGUI($this->lng->txt("insert"), "insertbefore");
1096  $options = array();
1097  foreach ($survey_questions as $key => $value) {
1098  $options[$key] = $this->lng->txt("before") . ": \"" . $value["title"] . "\"";
1099  }
1100  $insertbefore->setOptions($options);
1101  $insertbefore->setRequired(true);
1102  $form->addItem($insertbefore);
1103 
1104  $form->addCommandButton("saveHeading", $this->lng->txt("save"));
1105  $form->addCommandButton("questions", $this->lng->txt("cancel"));
1106 
1107  if ($a_question_id) {
1108  $form->setTitle($this->lng->txt("edit_heading"));
1109 
1110  $heading->setValue($this->object->prepareTextareaOutput($survey_questions[$a_question_id]["heading"] ?? ""));
1111  $insertbefore->setValue($a_question_id);
1112  $insertbefore->setDisabled(true);
1113  } else {
1114  $form->setTitle($this->lng->txt("add_heading"));
1115  }
1116 
1117  return $form;
1118  }
1119 
1120  public function addHeadingObject(
1121  ilPropertyFormGUI $a_form = null
1122  ): void {
1123  $q_id = $this->request->getQuestionId();
1124  $this->ctrl->setParameter($this, "q_id", $q_id);
1125 
1126  $this->questionsSubtabs("questions");
1127 
1128  if (!$a_form) {
1129  $a_form = $this->initHeadingForm($q_id);
1130  }
1131 
1132  $this->tpl->setContent($a_form->getHTML());
1133  }
1134 
1135  public function editHeadingObject(
1136  ilPropertyFormGUI $a_form = null
1137  ): void {
1138  $q_id = $this->request->getQuestionId();
1139  $this->ctrl->setParameter($this, "q_id", $q_id);
1140 
1141  $this->questionsSubtabs("questions");
1142 
1143  if (!$a_form) {
1144  $a_form = $this->initHeadingForm($q_id);
1145  }
1146 
1147  $this->tpl->setContent($a_form->getHTML());
1148  }
1149 
1150  public function saveHeadingObject(): void
1151  {
1152  // #15474
1153  $q_id = $this->request->getQuestionId();
1154  $this->ctrl->setParameter($this, "q_id", $q_id);
1155 
1156  $form = $this->initHeadingForm($q_id);
1157  if ($form->checkInput()) {
1158  $this->object->saveHeading(
1160  $form->getInput("heading"),
1161  true,
1163  ),
1164  $form->getInput("insertbefore")
1165  );
1166  $this->ctrl->redirect($this, "questions");
1167  }
1168 
1169  $form->setValuesByPost();
1170  $this->addHeadingObject($form);
1171  }
1172 
1173  public function removeHeadingObject(): void
1174  {
1175  $q_id = $this->request->getQuestionId();
1176  $this->ctrl->setParameter($this, "q_id", $q_id);
1177 
1178  if (!$q_id) {
1179  $this->ctrl->redirect($this, "questions");
1180  }
1181 
1182  $this->questionsSubtabs("questions");
1183 
1184  $cgui = new ilConfirmationGUI();
1185  $cgui->setHeaderText($this->lng->txt("confirm_remove_heading"));
1186 
1187  $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmedRemoveHeading"));
1188  $cgui->setCancel($this->lng->txt("cancel"), "questions");
1189  $cgui->setConfirm($this->lng->txt("confirm"), "confirmedRemoveHeading");
1190 
1191  $this->tpl->setContent($cgui->getHTML());
1192  }
1193 
1194  public function confirmedRemoveHeadingObject(): void
1195  {
1196  $q_id = $this->request->getQuestionId();
1197  if (!$q_id) {
1198  $this->ctrl->redirect($this, "questions");
1199  }
1200 
1201  $this->object->saveHeading("", $q_id);
1202  $this->ctrl->redirect($this, "questions");
1203  }
1204 
1205  public function printViewObject(): void
1206  {
1207  $print_view = $this->print->page($this->object->getRefId());
1208  $print_view->sendPrintView();
1209  }
1210 
1211  public function printListViewSelectionObject(): void
1212  {
1213  $view = $this->print->list($this->object->getRefId());
1214  $view->sendForm();
1215  }
1216 
1217  public function printListViewObject(): void
1218  {
1219  $print_view = $this->print->list($this->object->getRefId());
1220  $print_view->sendPrintView();
1221  }
1222 }
initHeadingForm(?int $a_question_id=null)
createQuestionObject(ilPropertyFormGUI $a_form=null, $sel_question_types=null, string $pgov_pos=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
editHeadingObject(ilPropertyFormGUI $a_form=null)
ILIAS HTTP Services $http
ILIAS DI UIServices $ui
Class ilSurveyEditorGUI.
static _hasDatasets(int $survey_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getRichTextEditor()
Returns the identifier for the Rich Text Editor.
ILIAS Survey PrintView GUIService $print
insertQuestions(int $insert_mode)
$type
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
Help GUI class.
static _getQuestionblock(int $questionblock_id)
get question block properties
removeQuestionsForm(array $checked_questionblocks, array $checked_questions, array $checked_headings)
addHeadingObject(ilPropertyFormGUI $a_form=null)
initQuestionblockForm(?int $a_block_id=null, ?array $a_question_ids=null)
executeCreateQuestionObject(?string $q_type=null, ?int $pool_usage=null, ?string $pgov_pos=null)
static _changeOriginalId(int $a_question_id, int $a_original_id, int $a_object_id)
Change original id of existing question in db.
ilGlobalTemplateInterface $tpl
This class represents a checkbox property in a property form.
EditingGUIRequest $request
static _getQuestionTypeName(string $type_tag)
Return the translation for a given question type.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
editQuestionblockObject(ilPropertyFormGUI $a_form=null)
createQuestionblockObject(ilPropertyFormGUI $a_form=null)
gatherSelectedTableItems(bool $allow_blocks=true, bool $allow_questions=true, bool $allow_headings=false, bool $allow_questions_in_blocks=false)
Gather (and filter) selected items from table gui.
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
Class ilObjSurveyGUI.
if($format !==null) $name
Definition: metadata.php:247
createQuestionPool($name="dummy")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getQuestionGUI(?string $questiontype, int $question_id=-1)
Creates a question gui representation.
addSubTab(string $a_id, string $a_text, string $a_link, string $a_frame="")
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
static _instanciateQuestion(int $question_id)
Creates an instance of a question with a given question id.
const ILIAS_IMG_MANAGER_PLUGIN
Definition: class.ilRTE.php:31
setScreenIdComponent(string $a_comp)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setValue(string $a_value)
string $key
Consumer key/client ID value.
Definition: System.php:193
static _getUsedHTMLTagsAsString(string $a_module="")
Returns a string of all allowed HTML tags for text editing.
getParentId(int $a_node_id)
get parent id of given node
setRequired(bool $a_required)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
This class represents a text area property in a property form.
static _getQuestiontypes()
Get all available question types.
$ilUser
Definition: imgupload.php:34
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ILIAS Survey Sequence SequenceManager $sequence_manager
static _getUsedHTMLTags(string $a_module="")
Returns an array of all allowed HTML tags for text editing.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...