ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilAssQuestionPreviewGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 use ILIAS\Refinery\Random\Group as RandomGroup;
31 
46 {
47  public const CMD_SHOW = 'show';
48  public const CMD_RESET = 'reset';
49  public const CMD_STATISTICS = 'assessment';
50  public const CMD_INSTANT_RESPONSE = 'instantResponse';
51  public const CMD_HANDLE_QUESTION_ACTION = 'handleQuestionAction';
52  public const CMD_GATEWAY_CONFIRM_HINT_REQUEST = 'gatewayConfirmHintRequest';
53  public const CMD_GATEWAY_SHOW_HINT_LIST = 'gatewayShowHintList';
54 
55  public const TAB_ID_QUESTION = 'question';
56 
57  public const FEEDBACK_FOCUS_ANCHOR = 'focus';
58 
60 
61  private ?assQuestionGUI $question_gui = null;
62  private ?assQuestion $question_obj = null;
66 
67  private ?string $info_message = null;
68 
72  private array $primary_cmd = [];
73 
77  private array $additional_cmds = [];
78 
79  public function __construct(
80  private readonly ilCtrl $ctrl,
81  private readonly ilRbacSystem $rbac_system,
82  private ilTabsGUI $tabs,
83  private ilToolbarGUI $toolbar,
84  private ilGlobalTemplateInterface $tpl,
85  private readonly UIFactory $ui_factory,
86  private readonly ilLanguage $lng,
87  private readonly ilDBInterface $db,
88  private readonly RandomGroup $random_group,
89  private readonly GlobalScreen $global_screen,
90  private readonly HTTPServices $http,
91  private readonly Refinery $refinery,
92  private readonly int $parent_obj_ref_id
93  ) {
94  $this->tpl->addCss(ilObjStyleSheet::getContentStylePath(0));
95  $this->tpl->addCss(ilObjStyleSheet::getSyntaxStylePath());
96 
97  $local_dic = QuestionPoolDIC::dic();
98  $this->request_data_collector = $local_dic['request_data_collector'];
99  }
100 
101  public function setInfoMessage(string $message): void
102  {
103  $this->info_message = $message;
104  }
105 
106  public function setPrimaryCmd(string $label, string $cmd): void
107  {
108  $this->primary_cmd[$label] = $cmd;
109  }
110 
111  public function addAdditionalCmd(string $label, string $cmd): void
112  {
113  $this->additional_cmds[$label] = $cmd;
114  }
115 
116  public function getQuestion(): assQuestion
117  {
118  return $this->question_obj;
119  }
120 
121  public function getObject(): assQuestion
122  {
123  return $this->question_obj;
124  }
125 
126  public function initQuestion(assQuestionGUI $question_gui, int $parent_obj_id): void
127  {
128  $this->question_gui = $question_gui;
129  $this->question_obj = $this->question_gui->getObject();
130 
131  $this->question_obj->setObjId($parent_obj_id);
132 
133  $this->tabs->clearTargets();
134  $this->tabs->addTarget(
135  self::TAB_ID_QUESTION,
136  $this->ctrl->getLinkTargetByClass(self::class, self::CMD_SHOW),
137  '',
138  [strtolower(__CLASS__)]
139  );
140  // Assessment of questions sub menu entry
141  $q_type = $this->question_obj->getQuestionType();
142  $classname = $q_type . 'GUI';
143  $this->tabs->addTarget(
144  'statistics',
145  $this->ctrl->getLinkTargetByClass(self::class, 'assessment'),
146  ['assessment'],
147  $classname,
148  ''
149  );
150 
151  $this->question_gui->populateJavascriptFilesRequiredForWorkForm($this->tpl);
152  $this->question_gui->setTargetGui($this);
153  $this->question_gui->setQuestionActionCmd(self::CMD_HANDLE_QUESTION_ACTION);
154 
155  $this->question_gui->setRenderPurpose(assQuestionGUI::RENDER_PURPOSE_DEMOPLAY);
156  }
157 
158  public function initPreviewSettings(int $parent_ref_id): void
159  {
160  $this->preview_settings = new ilAssQuestionPreviewSettings($parent_ref_id);
161 
162  $this->preview_settings->init();
163  }
164 
165  public function initPreviewSession(int $user_id, int $question_id): void
166  {
167  $this->preview_session = new ilAssQuestionPreviewSession($user_id, $question_id);
168  $this->preview_session->init();
169  }
170 
171  public function initHintTracking(): void
172  {
173  $this->hint_tracking = new ilAssQuestionPreviewHintTracking($this->db, $this->preview_session);
174  }
175 
176  public function initStyleSheets(): void
177  {
178  $this->tpl->setCurrentBlock('ContentStyle');
179  $this->tpl->setVariable('LOCATION_CONTENT_STYLESHEET', ilObjStyleSheet::getContentStylePath(0));
180  $this->tpl->parseCurrentBlock();
181 
182  $this->tpl->setCurrentBlock('SyntaxStyle');
183  $this->tpl->setVariable('LOCATION_SYNTAX_STYLESHEET', ilObjStyleSheet::getSyntaxStylePath());
184  $this->tpl->parseCurrentBlock();
185  }
186 
187  public function executeCommand(): void
188  {
189  global $DIC;
190  $ilHelp = $DIC['ilHelp'];
191  $ilHelp->setScreenIdComponent('qpl');
192 
193  $this->tabs->setTabActive(self::TAB_ID_QUESTION);
194 
195  $this->lng->loadLanguageModule('content');
196 
197  $nextClass = $this->ctrl->getNextClass($this);
198 
199  switch ($nextClass) {
200  case 'ilassquestionhintrequestgui':
201  $gui = new ilAssQuestionHintRequestGUI(
202  $this,
203  self::CMD_SHOW,
204  $this->question_gui,
205  $this->hint_tracking,
206  $this->ctrl,
207  $this->lng,
208  $this->tpl,
209  $this->tabs,
210  $this->global_screen
211  );
212  $this->ctrl->forwardCommand($gui);
213  break;
214  case 'ilassspecfeedbackpagegui':
215  case 'ilassgenfeedbackpagegui':
216  if ($this->ctrl->getCmd() === 'displayMediaFullscreen') {
217  $this->displayMediaFullscreenCmd();
218  }
220  $this->question_obj,
221  $this->ctrl,
222  $this->tabs,
223  $this->lng
224  ))->forward();
225  break;
226  case 'ilcommentgui':
227  $comment_gui = new ilCommentGUI($this->question_obj->getObjId(), $this->question_obj->getId(), 'quest');
228  $comments_panel_html = $this->ctrl->forwardCommand($comment_gui);
229  $this->showCmd($comments_panel_html);
230  break;
231  default:
232  $cmd = $this->ctrl->getCmd(self::CMD_SHOW);
233  $this->{$cmd . 'Cmd'}();
234  }
235  }
236 
237  protected function buildPreviewFormAction(): string
238  {
239  return $this->ctrl->getFormAction($this, self::CMD_SHOW) . '#' . self::FEEDBACK_FOCUS_ANCHOR;
240  }
241 
242  protected function isCommentingRequired(): bool
243  {
244  return !$this->preview_settings->isTestRefId() &&
245  $this->rbac_system->checkAccess('read', $this->request_data_collector->getRefId());
246  }
247 
248  public function showCmd(string $commands_panel_html = ''): void
249  {
250  $tpl = new ilTemplate('tpl.qpl_question_preview.html', true, true, 'components/ILIAS/TestQuestionPool');
251  $tpl->setVariable('PREVIEW_FORMACTION', $this->buildPreviewFormAction());
252 
253  $modal = '';
254  if ($this->question_gui->isSaveCommand() && $this->question_gui->needsSyncQuery()) {
255  $modal = $this->question_gui->getQuestionSyncModal(assQuestionGUI::CMD_SYNC_QUESTION_AND_RETURN);
256  }
257 
258  if ($this->info_message !== null) {
259  $this->tpl->setOnScreenMessage('info', $this->info_message, true);
260  }
261 
262  $this->populateToolbar();
263  $this->populateQuestionOutput($tpl);
264  $this->handleInstantResponseRendering($tpl);
265 
266  if ($this->isCommentingRequired()) {
267  $this->populateCommentsPanel($tpl, $commands_panel_html);
268  }
269 
270  $this->tpl->setContent($tpl->get() . $modal);
271  }
272 
273  public function assessmentCmd()
274  {
275  $this->tabs->activateTab('statistics');
276  $this->question_gui->assessment();
277  }
278 
279  public function handleInstantResponseRendering(ilTemplate $tpl): void
280  {
281  $response_required = false;
282  $response_available = false;
283  $jump_to_response = false;
284 
285  if ($this->isShowReachedPointsRequired()) {
286  $this->populateReachedPointsOutput($tpl);
287  $response_required = true;
288  $response_available = true;
289  $jump_to_response = true;
290  }
291 
292  if ($this->isShowBestSolutionRequired()) {
293  $this->populateSolutionOutput($tpl);
294  $response_required = true;
295  $response_available = true;
296  $jump_to_response = true;
297  }
298 
300  $response_required = true;
301  if ($this->populateGenericQuestionFeedback($tpl)) {
302  $response_available = true;
303  $jump_to_response = true;
304  }
305  }
306 
308  $response_required = true;
309 
310  if ($this->question_gui->hasInlineFeedback()) {
311  // Don't jump to the feedback below the question if some feedback is shown within the question
312  $jump_to_response = false;
313  } else {
314  if ($this->populateSpecificQuestionFeedback($tpl)) {
315  $response_available = true;
316  $jump_to_response = true;
317  }
318  }
319  }
320 
321  if ($response_required) {
322  $this->populateInstantResponseHeader($tpl, $jump_to_response);
323  if (!$response_available) {
324  if ($this->question_gui->hasInlineFeedback()) {
326  $tpl,
327  $this->lng->txt('tst_feedback_is_given_inline')
328  );
329  } else {
331  $tpl,
332  $this->lng->txt('tst_feedback_not_available_for_answer')
333  );
334  }
335  }
336  }
337  }
338 
339  public function resetCmd(): void
340  {
341  $this->preview_session->setRandomizerSeed(null);
342  $this->preview_session->setParticipantsSolution(null);
343  $this->preview_session->resetRequestedHints();
344  $this->preview_session->setInstantResponseActive(false);
345 
346  $this->tpl->setOnScreenMessage('info', $this->lng->txt('qst_preview_reset_msg'), true);
347 
348  $this->ctrl->redirect($this, self::CMD_SHOW);
349  }
350 
351  public function instantResponseCmd(): void
352  {
353  if ($this->saveQuestionSolution()) {
354  $this->preview_session->setInstantResponseActive(true);
355  } else {
356  $this->preview_session->setInstantResponseActive(false);
357  }
358 
359  $this->ctrl->redirect($this, self::CMD_SHOW);
360  }
361 
362  public function handleQuestionActionCmd(): void
363  {
364  $this->question_obj->persistPreviewState($this->preview_session);
365  $this->ctrl->redirect($this, self::CMD_SHOW);
366  }
367 
368  public function displayMediaFullscreenCmd(): void
369  {
370  $page_id = $this->request_data_collector->int('pg_id');
371  if ($page_id === 0) {
372  return;
373  }
374 
375  (new ilPageObjectGUI(
376  $this->ctrl->getCmdClass() === 'ilassgenfeedbackpagegui' ? 'qfbg' : 'qfbs',
377  $page_id
378  ))->displayMediaFullscreen();
379  }
380 
381  private function populateToolbar(): void
382  {
383  $this->toolbar->setFormAction($this->ctrl->getFormAction($this, self::CMD_SHOW));
384 
385  if ($this->rbac_system->checkAccess('write', $this->parent_obj_ref_id)) {
386  if ($this->primary_cmd !== []) {
387  $this->toolbar->addComponent(
388  $this->ui_factory->button()->primary(key($this->primary_cmd), current($this->primary_cmd))
389  );
390  }
391  foreach ($this->additional_cmds as $label => $action) {
392  $this->toolbar->addComponent(
393  $this->ui_factory->button()->standard($label, $action)
394  );
395  }
396  }
397 
398  $this->toolbar->addComponent(
399  $this->ui_factory->button()->standard(
400  $this->lng->txt('qpl_reset_preview'),
401  $this->ctrl->getLinkTargetByClass(ilAssQuestionPreviewGUI::class, self::CMD_RESET)
402  )
403  );
404  }
405 
406  private function populateQuestionOutput(ilTemplate $tpl): void
407  {
408  // FOR WHAT EXACTLY IS THIS USEFUL?
409  $this->ctrl->setReturnByClass('ilAssQuestionPageGUI', 'view');
410  $this->ctrl->setReturnByClass('ilObjQuestionPoolGUI', 'questions');
411 
412  $page_gui = new ilAssQuestionPageGUI($this->question_obj->getId());
413  $page_gui->setRenderPageContainer(false);
414  $page_gui->setEditPreview(true);
415  $page_gui->setEnabledTabs(false);
416 
417  $this->question_gui->setPreviewSession($this->preview_session);
418  $question = $this->question_gui->getObject();
419  $question->setShuffler($this->getQuestionAnswerShuffler());
420  $this->question_gui->setObject($question);
421 
422  $question_html = $this->question_gui->getPreview(true, $this->isShowSpecificQuestionFeedbackRequired());
423  $this->question_gui->magicAfterTestOutput();
424 
425  $question_html .= $this->getQuestionNavigationHtml();
426 
427  $page_gui->setQuestionHTML([$this->question_obj->getId() => $question_html]);
428 
429  $page_gui->setPresentationTitle($this->question_obj->getTitleForHTMLOutput());
430 
431  $tpl->setVariable('QUESTION_OUTPUT', $page_gui->preview());
432  // \ilPageObjectGUI::preview sets an undefined tab, so the "question" tab has to be activated again
433  $this->tabs->setTabActive(self::TAB_ID_QUESTION);
434  }
435 
436  protected function populateReachedPointsOutput(ilTemplate $tpl): void
437  {
438  $reachedPoints = $this->question_obj->calculateReachedPointsFromPreviewSession($this->preview_session);
439  $maxPoints = $this->question_obj->getMaximumPoints();
440 
441  $scoreInformation = sprintf(
442  $this->lng->txt("you_received_a_of_b_points"),
443  $reachedPoints,
444  $maxPoints
445  );
446 
447  $tpl->setCurrentBlock("reached_points_feedback");
448  $tpl->setVariable("REACHED_POINTS_FEEDBACK", $scoreInformation);
449  $tpl->parseCurrentBlock();
450  }
451 
452  private function populateSolutionOutput(ilTemplate $tpl): void
453  {
454  // FOR WHAT EXACTLY IS THIS USEFUL?
455  $this->ctrl->setReturnByClass('ilAssQuestionPageGUI', 'view');
456  $this->ctrl->setReturnByClass('ilObjQuestionPoolGUI', 'questions');
457 
458  $pageGUI = new ilAssQuestionPageGUI($this->question_obj->getId());
459 
460  $pageGUI->setEditPreview(true);
461  $pageGUI->setEnabledTabs(false);
462 
463  $this->question_gui->setPreviewSession($this->preview_session);
464 
465  $pageGUI->setQuestionHTML([$this->question_obj->getId() => $this->question_gui->getSolutionOutput(0, null, false, false, true, false, true, false, false)]);
466 
467  $output = $this->question_gui->getSolutionOutput(0, null, false, false, true, false, true, false, false);
468 
469  $tpl->setCurrentBlock('solution_output');
470  $tpl->setVariable('TXT_CORRECT_SOLUTION', $this->lng->txt('tst_best_solution_is'));
471  $tpl->setVariable('SOLUTION_OUTPUT', $output);
472  $tpl->parseCurrentBlock();
473  }
474 
475  private function getQuestionNavigationHtml(): string
476  {
477  $navGUI = new ilAssQuestionRelatedNavigationBarGUI($this->ctrl, $this->lng);
478 
479  $navGUI->setInstantResponseCmd(self::CMD_INSTANT_RESPONSE);
480  $navGUI->setHintRequestCmd(self::CMD_GATEWAY_CONFIRM_HINT_REQUEST);
481  $navGUI->setHintListCmd(self::CMD_GATEWAY_SHOW_HINT_LIST);
482 
483  $navGUI->setInstantResponseEnabled($this->preview_settings->isInstantFeedbackNavigationRequired());
484  $navGUI->setHintProvidingEnabled($this->preview_settings->isHintProvidingEnabled());
485 
486  $navGUI->setHintRequestsPossible($this->hint_tracking->requestsPossible());
487  $navGUI->setHintRequestsExist($this->hint_tracking->requestsExist());
488 
489  return $this->ctrl->getHTML($navGUI);
490  }
491 
496  private function populateGenericQuestionFeedback(ilTemplate $tpl): bool
497  {
498  if ($this->question_obj->isPreviewSolutionCorrect($this->preview_session)) {
499  $feedback = $this->question_gui->getGenericFeedbackOutputForCorrectSolution();
501  } else {
502  $feedback = $this->question_gui->getGenericFeedbackOutputForIncorrectSolution();
504  }
505 
506  if ($feedback !== '') {
507  $tpl->setCurrentBlock('instant_feedback_generic');
508  $tpl->setVariable('GENERIC_FEEDBACK', $feedback);
509  $tpl->setVariable('ILC_FB_CSS_CLASS', $cssClass);
510  $tpl->parseCurrentBlock();
511  return true;
512  }
513  return false;
514  }
515 
520  private function populateSpecificQuestionFeedback(ilTemplate $tpl): bool
521  {
522  $fb = $this->question_gui->getSpecificFeedbackOutput(
523  (array) $this->preview_session->getParticipantsSolution()
524  );
525 
526  if (!empty($fb)) {
527  $tpl->setCurrentBlock('instant_feedback_specific');
528  $tpl->setVariable('ANSWER_FEEDBACK', $fb);
529  $tpl->parseCurrentBlock();
530  return true;
531  }
532  return false;
533  }
534 
535  protected function populateInstantResponseHeader(ilTemplate $tpl, $withFocusAnchor): void
536  {
537  if ($withFocusAnchor) {
538  $tpl->setCurrentBlock('inst_resp_id');
539  $tpl->setVariable('INSTANT_RESPONSE_FOCUS_ID', self::FEEDBACK_FOCUS_ANCHOR);
540  $tpl->parseCurrentBlock();
541  }
542 
543  $tpl->setCurrentBlock('instant_response_header');
544  $tpl->setVariable('INSTANT_RESPONSE_HEADER', $this->lng->txt('tst_feedback'));
545  $tpl->parseCurrentBlock();
546  }
547 
548  protected function populateInstantResponseMessage(ilTemplate $tpl, string $a_message)
549  {
550  $tpl->setCurrentBlock('instant_response_message');
551  $tpl->setVariable('INSTANT_RESPONSE_MESSAGE', $a_message);
552  $tpl->parseCurrentBlock();
553  }
554 
555  private function isShowBestSolutionRequired()
556  {
557  if (!$this->preview_settings->isBestSolutionEnabled()) {
558  return false;
559  }
560 
561  return $this->preview_session->isInstantResponseActive();
562  }
563 
565  {
566  if (!$this->preview_settings->isGenericFeedbackEnabled()) {
567  return false;
568  }
569 
570  return $this->preview_session->isInstantResponseActive();
571  }
572 
573  private function isShowSpecificQuestionFeedbackRequired(): bool
574  {
575  if (!$this->preview_settings->isSpecificFeedbackEnabled()) {
576  return false;
577  }
578 
579  return $this->preview_session->isInstantResponseActive();
580  }
581 
582  private function isShowReachedPointsRequired()
583  {
584  if (!$this->preview_settings->isReachedPointsEnabled()) {
585  return false;
586  }
587 
588  return $this->preview_session->isInstantResponseActive();
589  }
590 
591  public function saveQuestionSolution(): bool
592  {
593  return $this->question_obj->persistPreviewState($this->preview_session);
594  }
595 
596  public function gatewayConfirmHintRequestCmd(): void
597  {
598  if (!$this->saveQuestionSolution()) {
599  $this->preview_session->setInstantResponseActive(false);
600  $this->showCmd();
601  return;
602  }
603 
604  $this->ctrl->redirectByClass(
605  'ilAssQuestionHintRequestGUI',
607  );
608  }
609 
610  public function gatewayShowHintListCmd(): void
611  {
612  if (!$this->saveQuestionSolution()) {
613  $this->preview_session->setInstantResponseActive(false);
614  $this->showCmd();
615  return;
616  }
617 
618  $this->ctrl->redirectByClass(
619  'ilAssQuestionHintRequestGUI',
621  );
622  }
623 
628  {
629  if (!$this->preview_session->randomizerSeedExists()) {
630  $this->preview_session->setRandomizerSeed((new RandomSeed())->createSeed());
631  }
632  return $this->random_group->shuffleArray(new GivenSeed((int) $this->preview_session->getRandomizerSeed()));
633  }
634 
635  protected function populateCommentsPanel(ilTemplate $tpl, string $comments_panel_html): void
636  {
637  if ($comments_panel_html === '') {
638  $comments_panel_html = $this->question_gui->getCommentsPanelHTML();
639  }
640 
641  $tpl->setCurrentBlock('notes_panel');
642  $tpl->setVariable('NOTES_PANEL', $comments_panel_html);
643  $tpl->parseCurrentBlock();
644  }
645 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setEditPreview(bool $a_editpreview)
Set Display first Edit tab, then Preview tab, instead of Page and Edit.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
populateCommentsPanel(ilTemplate $tpl, string $comments_panel_html)
ilAssQuestionPreviewSettings $preview_settings
readonly RequestDataCollector $request_data_collector
__construct(private readonly ilCtrl $ctrl, private readonly ilRbacSystem $rbac_system, private ilTabsGUI $tabs, private ilToolbarGUI $toolbar, private ilGlobalTemplateInterface $tpl, private readonly UIFactory $ui_factory, private readonly ilLanguage $lng, private readonly ilDBInterface $db, private readonly RandomGroup $random_group, private readonly GlobalScreen $global_screen, private readonly HTTPServices $http, private readonly Refinery $refinery, private readonly int $parent_obj_ref_id)
ilAssQuestionPreviewSession $preview_session
Class ilPageObjectGUI.
initQuestion(assQuestionGUI $question_gui, int $parent_obj_id)
$http
Definition: deliver.php:14
initPreviewSession(int $user_id, int $question_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilAssQuestionPreviewHintTracking $hint_tracking
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
setRenderPageContainer(bool $a_val)
global $DIC
Definition: shib_login.php:25
populateGenericQuestionFeedback(ilTemplate $tpl)
Populate the block for an instant generic feedback.
populateInstantResponseHeader(ilTemplate $tpl, $withFocusAnchor)
populateInstantResponseMessage(ilTemplate $tpl, string $a_message)
static getContentStylePath(int $a_style_id, bool $add_random=true, bool $add_token=true)
get content style path static (to avoid full reading)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
Comment GUI.
global $lng
Definition: privfeed.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A transformation is a function from one datatype to another.
setPrimaryCmd(string $label, string $cmd)
showCmd(string $commands_panel_html='')
addAdditionalCmd(string $label, string $cmd)
populateSpecificQuestionFeedback(ilTemplate $tpl)
Populate the block for an instant specific feedback.