ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.SurveyQuestionGUI.php
Go to the documentation of this file.
1<?php
2
21
28abstract class SurveyQuestionGUI
29{
30 protected \ILIAS\Survey\InternalGUIService $gui;
34 protected ilObjUser $user;
36 protected ilTree $tree;
38 protected ilTabsGUI $tabs;
40 protected ilLanguage $lng;
41 protected ilCtrl $ctrl;
42 protected array $cumulated = [];
43 protected string $parent_url = "";
44 protected ilLogger $log;
45 public ?SurveyQuestion $object = null;
46
47 public function __construct($a_id = -1)
48 {
49 global $DIC;
50
51 $this->rbacsystem = $DIC->rbac()->system();
52 $this->user = $DIC->user();
53 $this->access = $DIC->access();
54 $this->tree = $DIC->repositoryTree();
55 $this->toolbar = $DIC->toolbar();
56 $lng = $DIC->language();
57 $tpl = $DIC["tpl"];
58 $ilCtrl = $DIC->ctrl();
59
60
61 $this->request = $DIC->surveyQuestionPool()
62 ->internal()
63 ->gui()
64 ->editing()
65 ->request();
66
67 $this->lng = $lng;
68 $this->tpl = $tpl;
69 $this->ctrl = $ilCtrl;
70 $this->ctrl->saveParameter($this, "q_id");
71 $this->ctrl->setParameterByClass(
72 $this->ctrl->getCmdClass(),
73 "sel_question_types",
74 $this->request->getSelectedQuestionTypes()
75 );
76 $this->cumulated = array();
77 $this->tabs = $DIC->tabs();
78
79 $this->initObject();
80
81 if ($a_id > 0) {
82 $this->object->loadFromDb($a_id);
83 }
84 $this->log = ilLoggerFactory::getLogger('svy');
85
86 $this->edit_manager = $DIC->surveyQuestionPool()
87 ->internal()
88 ->domain()
89 ->editing();
90 $this->gui = $DIC->survey()->internal()->gui();
91 }
92
93 abstract protected function initObject(): void;
94
95 abstract public function setQuestionTabs(): void;
96
97 public function executeCommand(): string
98 {
99 $cmd = $this->ctrl->getCmd();
100 $next_class = $this->ctrl->getNextClass($this);
101 switch ($next_class) {
102 default:
103 $ret = $this->$cmd();
104 break;
105 }
106 return (string) $ret;
107 }
108
113 public static function _getQuestionGUI(
114 ?string $questiontype,
115 int $question_id = -1
117 if ((!$questiontype) and ($question_id > 0)) {
118 $questiontype = SurveyQuestion::_getQuestionType($question_id);
119 }
120 SurveyQuestion::_includeClass($questiontype, 1);
121 $question_type_gui = $questiontype . "GUI";
122 $question = new $question_type_gui($question_id);
123 return $question;
124 }
125
126 public static function _getGUIClassNameForId(int $a_q_id): string
127 {
128 $q_type = SurveyQuestion::_getQuestionType($a_q_id);
129 $class_name = SurveyQuestionGUI::_getClassNameForQType($q_type);
130 return $class_name;
131 }
132
133 public static function _getClassNameForQType(string $q_type): string
134 {
135 return $q_type;
136 }
137
141 public function getQuestionType(): string
142 {
143 return $this->object->getQuestionType();
144 }
145
146 protected function outQuestionText(ilTemplate $template): void
147 {
148 $questiontext = $this->object->getQuestiontext();
149 if (preg_match("/^<.[\\>]?>(.*?)<\\/.[\\>]*?>$/", $questiontext, $matches)) {
150 $questiontext = $matches[1];
151 }
152 $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, true));
153 if ($this->object->getObligatory()) {
154 $template->setVariable("OBLIGATORY_TEXT", ' *');
155 }
156 }
157
158 public function setBackUrl(string $a_url): void
159 {
160 $this->parent_url = $a_url;
161 }
162
163 public function setQuestionTabsForClass(string $guiclass): void
164 {
165 $rbacsystem = $this->rbacsystem;
166 $ilTabs = $this->tabs;
167
168 $this->ctrl->setParameterByClass($guiclass, "sel_question_types", $this->getQuestionType());
169 $this->ctrl->setParameterByClass(
170 $guiclass,
171 "q_id",
172 $this->request->getQuestionId()
173 );
174
175 if ($this->parent_url) {
176 $addurl = "";
177 if ($this->request->getNewForSurvey() > 0) {
178 $addurl = "&new_id=" . $this->request->getQuestionId();
179 }
180 $ilTabs->setBackTarget($this->lng->txt("menubacktosurvey"), $this->parent_url . $addurl);
181 } else {
182 $ilTabs->setBackTarget($this->lng->txt("spl"), $this->ctrl->getLinkTargetByClass("ilObjSurveyQuestionPoolGUI", "questions"));
183 }
184 if ($this->request->getQuestionId()) {
185 $ilTabs->addNonTabbedLink(
186 "preview",
187 $this->lng->txt("preview"),
188 $this->ctrl->getLinkTargetByClass($guiclass, "preview")
189 );
190 }
191
192 if ($rbacsystem->checkAccess('edit', $this->request->getRefId())) {
193 $ilTabs->addTab(
194 "edit_properties",
195 $this->lng->txt("properties"),
196 $this->ctrl->getLinkTargetByClass($guiclass, "editQuestion")
197 );
198
199 if (stripos($guiclass, "matrix") !== false) {
200 $ilTabs->addTab(
201 "layout",
202 $this->lng->txt("layout"),
203 $this->ctrl->getLinkTargetByClass($guiclass, "layout")
204 );
205 }
206 }
207
208 if ($this->object->getId() > 0) {
209 $title = $this->lng->txt("edit") . " &quot;" . $this->object->getTitle() . "&quot";
210 } else {
211 $title = $this->lng->txt("create_new") . " " . $this->lng->txt($this->getQuestionType());
212 }
213
214 $this->tpl->setVariable("HEADER", $title);
215 }
216
217
218 //
219 // EDITOR
220 //
221
222 protected function initEditForm(): ilPropertyFormGUI
223 {
224 $form = new ilPropertyFormGUI();
225 $form->setFormAction($this->ctrl->getFormAction($this, "save"));
226 $form->setTitle($this->lng->txt($this->getQuestionType()));
227 $form->setMultipart(false);
228 $form->setTableWidth("100%");
229 // $form->setId("essay");
230
231 // title
232 $title = new ilTextInputGUI($this->lng->txt("title"), "title");
233 $title->setMaxLength(200);
234 $title->setRequired(true);
235 $form->addItem($title);
236
237 // label
238 $label = new ilTextInputGUI($this->lng->txt("label"), "label");
239 $label->setInfo($this->lng->txt("label_info"));
240 $title->setMaxLength(255);
241 $label->setRequired(false);
242 $form->addItem($label);
243
244 // author
245 $author = new ilTextInputGUI($this->lng->txt("author"), "author");
246 $author->setRequired(true);
247 $title->setMaxLength(100);
248 $form->addItem($author);
249
250 // description
251 $description = new ilTextInputGUI($this->lng->txt("description"), "description");
252 $description->setRequired(false);
253 $title->setMaxLength(200);
254 $form->addItem($description);
255
256 // questiontext
257 $question = new ilTextAreaInputGUI($this->lng->txt("question"), "question");
258 $question->setRequired(true);
259 $question->setRows(10);
260 $question->setCols(80);
261 if ((new \ilRTESettings($this->lng, $this->user))->getRichTextEditor() === "tinymce") {
262 $question->setUseRte(true);
263 $question->setRteTagSet("mini");
264 }
265 $form->addItem($question);
266
267 // obligatory
268 $shuffle = new ilCheckboxInputGUI($this->lng->txt("obligatory"), "obligatory");
269 $shuffle->setValue(1);
270 $shuffle->setRequired(false);
271 $form->addItem($shuffle);
272
273 $this->addFieldsToEditForm($form);
274
275 $this->addCommandButtons($form);
276
277 // values
278 $title->setValue($this->object->getTitle());
279 $label->setValue($this->object->label);
280 $author->setValue($this->object->getAuthor());
281 $description->setValue($this->object->getDescription());
282 $question->setValue($this->object->prepareTextareaOutput($this->object->getQuestiontext()));
283 $shuffle->setChecked($this->object->getObligatory());
284
285 return $form;
286 }
287
288 protected function addCommandButtons(ilPropertyFormGUI $a_form): void
289 {
290 $a_form->addCommandButton("saveReturn", $this->lng->txt("save_return"));
291 $a_form->addCommandButton("save", $this->lng->txt("save"));
292
293 // pool question?
294 if (ilObject::_lookupType($this->object->getObjId()) === "spl" && $this->object->hasCopies()) {
295 $a_form->addCommandButton("saveSync", $this->lng->txt("svy_save_sync"));
296 }
297 }
298
299 protected function editQuestion(?ilPropertyFormGUI $a_form = null): void
300 {
301 $ilTabs = $this->tabs;
302
303 $ilTabs->activateTab("edit_properties");
304
305 if (!$a_form) {
306 $a_form = $this->initEditForm();
307 }
308 $this->tpl->setContent($a_form->getHTML());
309 }
310
311 protected function saveSync(): void
312 {
313 $this->save($this->request->getReturn(), true);
314 }
315
316 protected function saveReturn(): void
317 {
318 $this->save(true);
319 }
320
321 protected function saveForm(): bool
322 {
323 $form = $this->initEditForm();
324 if ($form->checkInput() && $this->validateEditForm($form)) {
325 $this->object->setTitle($form->getInput("title"));
326 $this->object->label = ($form->getInput("label"));
327 $this->object->setAuthor($form->getInput("author"));
328 $this->object->setDescription($form->getInput("description"));
329 $this->object->setQuestiontext($form->getInput("question"));
330 $this->object->setObligatory($form->getInput("obligatory"));
331
332 $this->importEditFormValues($form);
333
334 // will save both core and extended data
335 $this->object->saveToDb();
336
337 return true;
338 }
339
340 $form->setValuesByPost();
341 $this->editQuestion($form);
342 return false;
343 }
344
345 protected function save(
346 bool $a_return = false,
347 bool $a_sync = false
348 ): void {
349 $ilUser = $this->user;
350
351 if ($this->saveForm()) {
352 // #13784
353 if ($a_return &&
354 !SurveyQuestion::_isComplete($this->object->getId())) {
355 $this->tpl->setOnScreenMessage('failure', $this->lng->txt("survey_error_insert_incomplete_question"));
356 $this->editQuestion();
357 return;
358 }
359
360 $ilUser->setPref("svy_lastquestiontype", $this->object->getQuestionType());
361 $ilUser->writePref("svy_lastquestiontype", $this->object->getQuestionType());
362
363 $originalexists = SurveyQuestion::_questionExists((int) $this->object->original_id);
364 $this->ctrl->setParameter($this, "q_id", $this->object->getId());
365
366 // pool question?
367 if ($a_sync) {
368 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
369 $this->ctrl->redirect($this, 'copySyncForm');
370 } elseif ($originalexists &&
371 SurveyQuestion::_isWriteable($this->object->original_id, $ilUser->getId())) {
372 // form: update original pool question, too?
373 if ($a_return) {
374 $this->ctrl->setParameter($this, 'rtrn', 1);
375 }
376 $this->ctrl->redirect($this, 'originalSyncForm');
377 }
378
379 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
380 $this->redirectAfterSaving($a_return);
381 }
382 }
383
384 protected function copySyncForm(): void
385 {
386 $ilTabs = $this->tabs;
387
388 $ilTabs->activateTab("edit_properties");
389
390 $tbl = new ilSurveySyncTableGUI($this, "copySyncForm", $this->object);
391
392 $this->tpl->setContent($tbl->getHTML());
393 }
394
395 protected function syncCopies(): void
396 {
398 $ilAccess = $this->access;
399
400 $qids = $this->request->getQuestionIds();
401 if (count($qids) === 0) {
402 $this->tpl->setOnScreenMessage('failure', $lng->txt("select_one"));
403 $this->copySyncForm();
404 return;
405 }
406
407 foreach ($this->object->getCopyIds(true) as $survey_id => $questions) {
408 // check permissions for "parent" survey
409 $can_write = false;
410 $ref_ids = ilObject::_getAllReferences($survey_id);
411 foreach ($ref_ids as $ref_id) {
412 if ($ilAccess->checkAccess("edit", "", $ref_id)) {
413 $can_write = true;
414 break;
415 }
416 }
417
418 if ($can_write) {
419 foreach ($questions as $qid) {
420 if (in_array($qid, $qids)) {
421 $id = $this->object->getId();
422
423 $this->object->setId($qid);
424 $this->object->setOriginalId($id);
425 $this->object->saveToDb();
426
427 $this->object->setId($id);
428 $this->object->setOriginalId(null);
429
430 // see: SurveyQuestion::syncWithOriginal()
431 // what about material?
432 }
433 }
434 }
435 }
436
437 $this->tpl->setOnScreenMessage('success', $lng->txt("survey_sync_success"), true);
438 $this->redirectAfterSaving($this->request->getReturn());
439 }
440
441 protected function originalSyncForm(): void
442 {
443 $ilTabs = $this->tabs;
444
445 $ilTabs->activateTab("edit_properties");
446
447 $this->ctrl->saveParameter($this, "rtrn");
448
449 $cgui = new ilConfirmationGUI();
450 $cgui->setHeaderText($this->lng->txt("confirm_sync_questions"));
451
452 $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmRemoveQuestions"));
453 $cgui->setCancel($this->lng->txt("no"), "cancelSync");
454 $cgui->setConfirm($this->lng->txt("yes"), "sync");
455
456 $this->tpl->setContent($cgui->getHTML());
457 }
458
459 protected function sync(): void
460 {
461 $original_id = $this->object->original_id;
462 if ($original_id) {
463 $this->object->syncWithOriginal();
464 }
465
466 $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
467 $this->redirectAfterSaving($this->request->getReturn());
468 }
469
470 protected function cancelSync(): void
471 {
472 $this->tpl->setOnScreenMessage('info', $this->lng->txt("question_changed_in_survey_only"), true);
473 $this->redirectAfterSaving($this->request->getReturn());
474 }
475
479 protected function redirectAfterSaving(
480 bool $a_return = false
481 ): void {
482 // return?
483 if ($a_return) {
484 // to calling survey
485 if ($this->parent_url) {
486 $addurl = "";
487 if ($this->request->getNewForSurvey() > 0) {
488 $addurl = "&new_id=" . $this->request->getQuestionId();
489 }
490 ilUtil::redirect(str_replace("&amp;", "&", $this->parent_url) . $addurl);
491 }
492 // to pool
493 else {
494 $this->ctrl->redirectByClass("ilObjSurveyQuestionPoolGUI", "questions");
495 }
496 }
497 // stay in form
498 else {
499 $this->ctrl->setParameterByClass(
500 $this->ctrl->getCmdClass(),
501 "q_id",
502 $this->object->getId()
503 );
504 $this->ctrl->setParameterByClass(
505 $this->ctrl->getCmdClass(),
506 "sel_question_types",
507 $this->request->getSelectedQuestionTypes()
508 );
509 $this->ctrl->setParameterByClass(
510 $this->ctrl->getCmdClass(),
511 "new_for_survey",
512 $this->request->getNewForSurvey()
513 );
514 $this->ctrl->redirectByClass($this->ctrl->getCmdClass(), "editQuestion");
515 }
516 }
517
518 protected function cancel(): void
519 {
520 if ($this->parent_url) {
521 ilUtil::redirect($this->parent_url);
522 } else {
523 $this->ctrl->redirectByClass("ilobjsurveyquestionpoolgui", "questions");
524 }
525 }
526
527 protected function validateEditForm(ilPropertyFormGUI $a_form): bool
528 {
529 return true;
530 }
531
532 abstract protected function addFieldsToEditForm(ilPropertyFormGUI $a_form): void;
533
534 abstract protected function importEditFormValues(ilPropertyFormGUI $a_form): void;
535
536 abstract public function getPrintView(
537 int $question_title = 1,
538 bool $show_questiontext = true,
539 ?int $survey_id = null,
540 ?array $working_data = null
541 ): string;
542
543 protected function getPrintViewQuestionTitle(
544 int $question_title = 1
545 ): string {
546 $title = "";
547 switch ($question_title) {
549 $title = ilLegacyFormElementsUtil::prepareFormOutput($this->object->getTitle());
550 break;
551
552 #19448 get rid of showing only the label without title
553 //case 2:
554 // $title = ilUtil::prepareFormOutput($this->object->getLabel());
555 // break;
556
558 $title = ilLegacyFormElementsUtil::prepareFormOutput($this->object->getTitle());
559 if (trim($this->object->getLabel())) {
560 $title .= ' <span class="questionLabel">(' . ilLegacyFormElementsUtil::prepareFormOutput(
561 $this->object->getLabel()
562 ) . ')</span>';
563 }
564 break;
565 }
566 return $title;
567 }
568
569 protected function getQuestionTitle(
570 int $question_title_mode = 1
571 ): string {
572 $title = "";
573 switch ($question_title_mode) {
575 $title = $this->object->getTitle();
576 break;
577
579 $title = $this->object->getTitle();
580 if (trim($this->object->getLabel())) {
581 $title .= ' <span class="questionLabel">(' .
582 $this->object->getLabel()
583 . ')</span>';
584 }
585 break;
586 }
587 return $title;
588 }
589
590 public function preview(): void
591 {
592 $ilTabs = $this->tabs;
593
594 $ilTabs->activateTab("preview");
595
596 $tpl = new ilTemplate("tpl.il_svy_qpl_preview.html", true, true, "components/ILIAS/SurveyQuestionPool");
597
598 if ($this->object->getObligatory()) {
599 $tpl->setCurrentBlock("required");
600 $tpl->setVariable("TEXT_REQUIRED", $this->lng->txt("required_field"));
601 $tpl->parseCurrentBlock();
602 }
603
604 $tpl->setVariable("QUESTION_OUTPUT", $this->getWorkingForm());
605
606 $f = $this->gui->ui()->factory();
607 $r = $this->gui->ui()->renderer();
608 $p = $f->panel()->standard(
609 "",
610 $f->legacy()->content($tpl->get())
611 );
612
613 $this->tpl->setContent($r->render($p));
614 }
615
616
617 //
618 // EXECUTION
619 //
620
621 abstract public function getWorkingForm(
622 ?array $working_data = null,
623 int $question_title = 1,
624 bool $show_questiontext = true,
625 string $error_message = "",
626 ?int $survey_id = null,
627 bool $compress_view = false
628 ): string;
629
630
632 array $a_head,
633 array $a_rows,
634 ?array $a_foot = null
635 ): string {
636 $html = array();
637 $html[] = '<div class="ilTableOuter table-responsive">';
638 $html[] = '<table class="table table-striped">';
639
640 $html[] = "<thead>";
641 $html[] = "<tr>";
642 foreach ($a_head as $col) {
643 $col = trim($col);
644 $html[] = "<th>";
645 $html[] = ($col != "") ? $col : "&nbsp;";
646 $html[] = "</th>";
647 }
648 $html[] = "</tr>";
649 $html[] = "</thead>";
650
651 $html[] = "<tbody>";
652 foreach ($a_rows as $row) {
653 $html[] = "<tr>";
654 foreach ($row as $col) {
655 $col = trim($col);
656 $html[] = "<td>";
657 $html[] = ($col != "") ? $col : "&nbsp;";
658 $html[] = "</td>";
659 }
660 $html[] = "</tr>";
661 }
662 $html[] = "</tbody>";
663
664 if ($a_foot) {
665 $html[] = "<tfoot>";
666 $html[] = "<tr>";
667 foreach ($a_foot as $col) {
668 $col = trim($col);
669 $html[] = "<td>";
670 $html[] = ($col != "") ? $col : "&nbsp;";
671 $html[] = "</td>";
672 }
673 $html[] = "</tr>";
674 $html[] = "</tfoot>";
675 }
676
677 $html[] = "</table>";
678 $html[] = "</div>";
679 return implode("\n", $html);
680 }
681}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Basic class for all survey question types The SurveyQuestionGUI class defines and encapsulates basic ...
static _getQuestionGUI(?string $questiontype, int $question_id=-1)
Creates a question gui representation.
renderStatisticsDetailsTable(array $a_head, array $a_rows, ?array $a_foot=null)
getPrintViewQuestionTitle(int $question_title=1)
ilGlobalTemplateInterface $tpl
save(bool $a_return=false, bool $a_sync=false)
redirectAfterSaving(bool $a_return=false)
Redirect to calling survey or to edit form.
outQuestionText(ilTemplate $template)
addFieldsToEditForm(ilPropertyFormGUI $a_form)
getQuestionTitle(int $question_title_mode=1)
getWorkingForm(?array $working_data=null, int $question_title=1, bool $show_questiontext=true, string $error_message="", ?int $survey_id=null, bool $compress_view=false)
addCommandButtons(ilPropertyFormGUI $a_form)
ILIAS Survey InternalGUIService $gui
editQuestion(?ilPropertyFormGUI $a_form=null)
getQuestionType()
Returns the question type string.
validateEditForm(ilPropertyFormGUI $a_form)
static _getGUIClassNameForId(int $a_q_id)
EditingGUIRequest $request
getPrintView(int $question_title=1, bool $show_questiontext=true, ?int $survey_id=null, ?array $working_data=null)
static _getClassNameForQType(string $q_type)
setQuestionTabsForClass(string $guiclass)
importEditFormValues(ilPropertyFormGUI $a_form)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _isWriteable(int $question_id, int $user_id)
is question writeable by a certain user
static _includeClass(string $question_type, int $gui=0)
Include the php class file for a given question type.
static _questionExists(int $question_id)
static _getQuestionType(int $question_id)
Returns the question type of a question with a given id.
static _isComplete(int $question_id)
Checks whether the question is complete or not.
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrl provides processing control methods.
language handling
static prepareFormOutput($a_str, bool $a_strip=false)
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
User class.
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
This class represents a property form user interface.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
checkAccess(string $a_operations, int $a_ref_id, string $a_type="")
checkAccess represents the main method of the RBAC-system in ILIAS3 developers want to use With this ...
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...
special template class to simplify handling of ITX/PEAR
This class represents a text area property in a property form.
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
static redirect(string $a_script)
setVariable(string $variable, $value='')
Sets the given variable to the given value.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
$ref_id
Definition: ltiauth.php:66
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26