ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestQuestionsTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
35 {
37  private const CLASS_PATH_FOR_QUESTION_EDIT_LINKS = [ilRepositoryGUI::class, ilObjQuestionPoolGUI::class];
38 
43 
47  protected $questionManagingEnabled = false;
48 
52  protected $positionInsertCommandsEnabled = false;
53 
57  protected $questionPositioningEnabled = false;
58 
63 
64  protected float $totalPoints = 0;
65  protected string $totalWorkingTime = '';
66  private int $position = 0;
67 
68  public function __construct(
70  string $parent_cmd,
71  private int $parent_ref_id,
72  private ilAccessHandler $access,
73  private UIFactory $ui_factory,
74  private UIRenderer $ui_renderer,
75  private QuestionInfoService $questioninfo
76  ) {
77  $this->setId('tst_qst_lst_' . $parent_ref_id);
78 
79  parent::__construct($parent_obj, $parent_cmd);
80 
81  $this->setFormName('questionbrowser');
82  $this->setStyle('table', 'fullwidth');
83 
84  $this->setExternalSegmentation(true);
85 
86  $this->setRowTemplate("tpl.il_as_tst_questions_row.html", "Modules/Test");
87 
88  $this->setFormAction($this->ctrl->getFormAction($parent_obj, $parent_cmd));
89 
90  $this->disable('sort');
91  $this->enable('header');
92 
93  $this->setShowRowsSelector(false);
94  }
95 
96  public function getSelectableColumns(): array
97  {
98  $cols = [
99  'qid' => ['txt' => $this->lng->txt('question_id'), 'default' => true],
100  'description' => ['txt' => $this->lng->txt('description'), 'default' => false],
101  'author' => ['txt' => $this->lng->txt('author'), 'default' => false],
102  'lifecycle' => ['txt' => $this->lng->txt('qst_lifecycle'), 'default' => true]
103  ];
104 
105  return $cols;
106  }
107 
108  public function init(): void
109  {
110  $this->initColumns();
111  $this->initCommands();
112 
113  if ($this->isQuestionManagingEnabled()) {
114  $this->setSelectAllCheckbox('q_id');
115  }
116  }
117 
118  protected function initColumns(): void
119  {
120  if ($this->isCheckboxColumnRequired()) {
121  $this->addColumn('', 'f', '1%', true);
122  }
123 
124  if ($this->isQuestionPositioningEnabled()) {
125  $this->addColumn($this->lng->txt('order'), 'f', '1%');
126  }
127 
128  if ($this->isColumnSelected('qid')) {
129  $this->addColumn($this->lng->txt('question_id'), 'qid', '');
130  }
131 
132  $this->addColumn($this->lng->txt("tst_question_title"), 'title', '');
133 
135  $this->addColumn($this->lng->txt("obligatory"), 'obligatory', '');
136  }
137 
138  if ($this->isColumnSelected('description')) {
139  $this->addColumn($this->lng->txt('description'), 'description', '');
140  }
141 
142  $this->addColumn($this->lng->txt("tst_question_type"), 'type', '');
143  $this->addColumn($this->buildPointsHeader(), '', '');
144 
145  if ($this->isColumnSelected('author')) {
146  $this->addColumn($this->lng->txt('author'), 'author', '');
147  }
148  if ($this->isColumnSelected('lifecycle')) {
149  $this->addColumn($this->lng->txt('qst_lifecycle'), 'lifecycle', '');
150  }
151 
152  $this->addColumn($this->lng->txt('qpl'), 'qpl', '');
153 
154  $this->addColumn($this->lng->txt('actions'), '');
155  }
156 
157  protected function initCommands(): void
158  {
159  if ($this->isQuestionManagingEnabled()) {
160  $this->addMultiCommand('removeQuestions', $this->lng->txt('remove_question'));
161  $this->addMultiCommand('moveQuestions', $this->lng->txt('move'));
162  }
163 
164  if ($this->isPositionInsertCommandsEnabled()) {
165  $this->addMultiCommand('insertQuestionsBefore', $this->lng->txt('insert_before'));
166  $this->addMultiCommand('insertQuestionsAfter', $this->lng->txt('insert_after'));
167  }
168 
169  if ($this->isQuestionManagingEnabled()) {
170  $this->addMultiCommand('copyQuestion', $this->lng->txt('copy'));
171  $this->addMultiCommand('copyAndLinkToQuestionpool', $this->lng->txt('copy_and_link_to_questionpool'));
172  }
173 
174  if ($this->isTableSaveCommandRequired()) {
175  $this->addCommandButton('saveOrderAndObligations', $this->buildTableSaveCommandLabel());
176  }
177  }
178 
179  public function fillRow(array $a_set): void
180  {
181  if ($this->isCheckboxColumnRequired()) {
182  $this->tpl->setVariable("CHECKBOX_QID", $a_set['question_id']);
183  }
184 
185  if ($this->isQuestionPositioningEnabled()) {
186  $this->position += 10;
187  $inputField = $this->buildPositionInput($a_set['question_id'], $this->position);
188 
189  $this->tpl->setVariable("QUESTION_POSITION", $inputField);
190  $this->tpl->setVariable("POSITION_QID", $a_set['question_id']);
191  }
192 
193  if ($this->isColumnSelected('qid')) {
194  $this->tpl->setVariable("QUESTION_ID_PRESENTATION", $a_set['question_id']);
195  }
196 
197  $this->tpl->setVariable("QUESTION_TITLE", $this->buildQuestionTitleLink($a_set));
198 
199  if (!$a_set['complete']) {
200  $this->tpl->setVariable("QUESTION_INCOMPLETE_WARNING", $this->lng->txt("warning_question_not_complete"));
201  }
202 
204  $this->tpl->setVariable("QUESTION_OBLIGATORY", $this->buildObligatoryColumnContent($a_set));
205  }
206 
207  if ($this->isColumnSelected('description')) {
208  $this->tpl->setVariable("QUESTION_COMMENT", $a_set["description"] ? $a_set["description"] : '&nbsp;');
209  }
210 
211  $this->tpl->setVariable("QUESTION_TYPE", $this->questioninfo->getQuestionTypeName($a_set['question_id']));
212  $this->tpl->setVariable("QUESTION_POINTS", $a_set["points"]);
213 
214  if ($this->isColumnSelected('author')) {
215  $this->tpl->setVariable("QUESTION_AUTHOR", $a_set["author"]);
216  }
217 
218  if ($this->isColumnSelected('lifecycle')) {
219  try {
220  $lifecycle = ilAssQuestionLifecycle::getInstance($a_set['lifecycle'])->getTranslation($this->lng);
221  $this->tpl->setVariable("QUESTION_LIFECYCLE", $lifecycle);
223  $this->tpl->setVariable("QUESTION_LIFECYCLE", '');
224  }
225  }
226 
227  $question_pool_title = $this->lng->txt('tst_question_not_from_pool_info');
228 
229  if (isset($a_set['orig_obj_fi']) && ilObject::_lookupTitle($a_set['orig_obj_fi']) !== null) {
230  $question_pool_title = $this->buildPossiblyLinkedQuestonPoolTitle(
231  $this->ctrl,
232  $this->access,
233  $this->lng,
234  $this->ui_factory,
235  $this->ui_renderer,
236  $a_set["orig_obj_fi"],
237  ilObject::_lookupTitle($a_set["orig_obj_fi"])
238  );
239  }
240 
241  $this->tpl->setVariable(
242  "QUESTION_POOL",
243  $question_pool_title
244  );
245 
246  $actions = [];
247 
248  $actions[] = $this->ui_factory->link()->standard(
249  $this->lng->txt('preview'),
250  $this->getPreviewLink($a_set)
251  );
252 
253  $actions[] = $this->ui_factory->link()->standard(
254  $this->lng->txt('statistics'),
255  $this->getQuestionEditLink($a_set, 'ilAssQuestionPreviewGUI', ilAssQuestionPreviewGUI::CMD_STATISTICS)
256  );
257 
258  if ($this->isQuestionManagingEnabled()) {
259  $editHref = $this->getQuestionEditLink($a_set, $a_set['type_tag'] . 'GUI', 'editQuestion');
260  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('edit_question'), $editHref);
261 
262  $editPageHref = $this->getQuestionEditLink($a_set, 'ilAssQuestionPageGUI', 'edit');
263  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('edit_page'), $editPageHref);
264 
265 
266  $moveHref = $this->getEditLink($a_set, get_class($this->getParentObject()), 'moveQuestions');
267  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('move'), $moveHref);
268 
269  $copyHref = $this->getEditLink($a_set, get_class($this->getParentObject()), 'copyQuestion');
270  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('copy'), $copyHref);
271 
272  $deleteHref = $this->getEditLink($a_set, get_class($this->getParentObject()), 'removeQuestions');
273  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('delete'), $deleteHref);
274 
275  $feedbackHref = $this->getQuestionEditLink($a_set, 'ilAssQuestionFeedbackEditingGUI', ilAssQuestionFeedbackEditingGUI::CMD_SHOW);
276  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('tst_feedback'), $feedbackHref);
277 
278  $hintsHref = $this->getQuestionEditLink($a_set, 'ilAssQuestionHintsGUI', ilAssQuestionHintsGUI::CMD_SHOW_LIST);
279  $actions[] = $this->ui_factory->link()->standard($this->lng->txt('tst_question_hints_tab'), $hintsHref);
280  }
281  $dropdown = $this->ui_factory->dropdown()->standard($actions);
282 
283  $this->tpl->setVariable('ROW_ACTIONS', $this->ui_renderer->render($dropdown));
284  if ($this->isQuestionRemoveRowButtonEnabled()) {
285  $this->tpl->setVariable('ROW_ACTIONS', $this->buildQuestionRemoveButton($a_set));
286  }
287  }
288 
289  protected function buildQuestionRemoveButton(array $row_data): string
290  {
291  $this->ctrl->setParameter($this->getParentObject(), 'removeQid', $row_data['question_id']);
292  $removeUrl = $this->ctrl->getLinkTarget($this->getParentObject(), $this->getParentCmd());
293  $this->ctrl->setParameter($this->getParentObject(), 'removeQid', '');
294 
295  return $this->ui_renderer->render($this->ui_factory->button()->standard($this->lng->txt('remove_question'), $removeUrl));
296  }
297 
298  protected function buildQuestionTitleLink(array $row_data): string
299  {
300  return '<a href="' . $this->getPreviewLink($row_data) . '">' . $row_data["title"] . '</a>';
301  }
302 
303  protected function getPreviewLink(array $row_data): string
304  {
305  $target_class = get_class($this->getParentObject());
306  $this->ctrl->setParameterByClass(
307  $target_class,
308  'ref_id',
309  current(ilObject::_getAllReferences($row_data['obj_fi']))
310  );
311 
312  $this->ctrl->setParameterByClass(
313  $target_class,
314  'eqpl',
315  current(ilObject::_getAllReferences($row_data['obj_fi']))
316  );
317 
318  $this->ctrl->setParameterByClass(
319  $target_class,
320  'eqid',
321  $row_data['question_id']
322  );
323 
324  $this->ctrl->setParameterByClass(
325  $target_class,
326  'q_id',
327  $row_data['question_id']
328  );
329 
330  $this->ctrl->setParameterByClass(
331  $target_class,
332  'calling_test',
333  (string) $this->parent_ref_id
334  );
335 
336  $question_href = $this->ctrl->getLinkTargetByClass(
337  $target_class,
338  $this->getParentCmd()
339  );
340  $this->ctrl->setParameterByClass($target_class, 'eqpl', '');
341  $this->ctrl->setParameterByClass($target_class, 'eqid', '');
342  $this->ctrl->setParameterByClass($target_class, 'q_id', '');
343  $this->ctrl->setParameterByClass($target_class, 'calling_test', '');
344 
345  return $question_href;
346  }
347 
348  protected function getQuestionEditLink(array $row_data, string $target_class, string $cmd, array $target_class_path = []): string
349  {
350  $target_class_path = array_merge(self::CLASS_PATH_FOR_QUESTION_EDIT_LINKS, [$target_class]);
351  return $this->getEditLink($row_data, $target_class, $cmd, $target_class_path);
352  }
353 
354  protected function getEditLink(array $row_data, string $target_class, string $cmd, array $target_class_path = []): string
355  {
356  if ($target_class_path === []) {
357  $target_class_path = $target_class;
358  }
359  $this->ctrl->setParameterByClass(
360  $target_class,
361  'ref_id',
362  current(ilObject::_getAllReferences($row_data['obj_fi']))
363  );
364 
365  $this->ctrl->setParameterByClass(
366  $target_class,
367  'q_id',
368  $row_data['question_id']
369  );
370  $this->ctrl->setParameterByClass(
371  $target_class,
372  'calling_test',
373  $_GET['ref_id']
374  );
375 
376  $link = $this->ctrl->getLinkTargetByClass($target_class_path, $cmd);
377 
378  $this->ctrl->setParameterByClass($target_class, 'ref_id', '');
379  $this->ctrl->setParameterByClass($target_class, 'q_id', '');
380  $this->ctrl->setParameterByClass($target_class, 'calling_test', '');
381  return $link;
382  }
383 
384  protected function buildObligatoryColumnContent(array $row_data): string
385  {
386  if (!$row_data['obligationPossible']) {
387  return '&nbsp;';
388  }
389 
390  if ($row_data['obligatory'] && !$this->isQuestionManagingEnabled()) {
391  return $this->ui_renderer->render(
392  $this->ui_factory->symbol()->icon()->custom(
393  ilUtil::getImagePath('standard/icon_alert.svg'),
394  $this->lng->txt('question_obligatory')
395  )
396  );
397  }
398 
399  $checkedAttr = $row_data['obligatory'] ? 'checked="checked"' : '';
400  return '<input type="checkbox" name="obligatory[' . $row_data['question_id'] . ']" value="1" ' . $checkedAttr . ' />';
401  }
402 
403  protected function buildPositionInput($questionId, $position): string
404  {
405  return '<input type="text" name="order[' . $questionId . ']" value="' . $position . '" maxlength="4" size="4" />';
406  }
407 
408  protected function buildTableSaveCommandLabel(): string
409  {
411  return $this->lng->txt('saveOrderAndObligations');
412  }
413 
415  return $this->lng->txt('saveObligations');
416  }
417 
418  if ($this->isQuestionPositioningEnabled()) {
419  return $this->lng->txt('saveOrder');
420  }
421 
422  return $this->lng->txt('save');
423  }
424 
425  protected function buildPointsHeader(): string
426  {
427  if ($this->getTotalPoints()) {
428  return $this->lng->txt('points') . ' (' . $this->getTotalPoints() . ')';
429  }
430 
431  return $this->lng->txt('points');
432  }
433 
434  protected function isTableSaveCommandRequired(): bool
435  {
436  if (!$this->isQuestionManagingEnabled()) {
437  return false;
438  }
439 
441  }
442 
443  protected function isCheckboxColumnRequired(): bool
444  {
446  }
447 
448  public function isQuestionManagingEnabled(): bool
449  {
451  }
452 
454  {
455  $this->questionManagingEnabled = $questionManagingEnabled;
456  }
457 
458  public function isPositionInsertCommandsEnabled(): bool
459  {
461  }
462 
464  {
465  $this->positionInsertCommandsEnabled = $positionInsertCommandsEnabled;
466  }
467 
468  public function isQuestionPositioningEnabled(): bool
469  {
471  }
472 
474  {
475  $this->questionPositioningEnabled = $questionPositioningEnabled;
476  }
477 
478  public function isObligatoryQuestionsHandlingEnabled(): bool
479  {
481  }
482 
484  {
485  $this->obligatoryQuestionsHandlingEnabled = $obligatoryQuestionsHandlingEnabled;
486  }
487 
488  public function getTotalPoints(): float
489  {
490  return $this->totalPoints;
491  }
492 
493  public function setTotalPoints(float $totalPoints): void
494  {
495  $this->totalPoints = $totalPoints;
496  }
497 
498  public function getTotalWorkingTime(): string
499  {
501  }
502 
503  public function isQuestionRemoveRowButtonEnabled(): bool
504  {
506  }
507 
509  {
510  $this->questionRemoveRowButtonEnabled = $questionRemoveRowButtonEnabled;
511  }
512 }
setQuestionManagingEnabled(bool $questionManagingEnabled)
$_GET["client_id"]
Definition: webdav.php:30
enable(string $a_module_name)
Class ilObjTestGUI.
setFormAction(string $a_form_action, bool $a_multipart=false)
addCommandButton(string $a_cmd, string $a_text, string $a_onclick='', string $a_id="", string $a_class="")
setObligatoryQuestionsHandlingEnabled(bool $obligatoryQuestionsHandlingEnabled)
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
setQuestionRemoveRowButtonEnabled(bool $questionRemoveRowButtonEnabled)
setFormName(string $a_name="")
const CMD_SHOW_LIST
command constants
setId(string $a_val)
setStyle(string $a_element, string $a_style)
isColumnSelected(string $col)
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
__construct(VocabulariesInterface $vocabularies)
static _lookupTitle(int $obj_id)
getQuestionEditLink(array $row_data, string $target_class, string $cmd, array $target_class_path=[])
setPositionInsertCommandsEnabled(bool $positionInsertCommandsEnabled)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
buildPositionInput($questionId, $position)
$lifecycle
getEditLink(array $row_data, string $target_class, string $cmd, array $target_class_path=[])
setQuestionPositioningEnabled(bool $questionPositioningEnabled)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
disable(string $a_module_name)
addMultiCommand(string $a_cmd, string $a_text)
__construct(ilObjTestGUI|ilTestCorrectionsGUI $parent_obj, string $parent_cmd, private int $parent_ref_id, private ilAccessHandler $access, private UIFactory $ui_factory, private UIRenderer $ui_renderer, private QuestionInfoService $questioninfo)
setExternalSegmentation(bool $a_val)