ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Printer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Test\Questions\Properties\Repository as TestQuestionsRepository;
28 
29 class Printer
30 {
34  public function __construct(
35  private readonly UIFactory $ui_factory,
36  private \ilGlobalTemplateInterface $tpl,
37  private TabsManager $tabs_manager,
38  private \ilToolbarGUI $toolbar,
39  private readonly Refinery $refinery,
40  private readonly Language $lng,
41  private readonly \ilCtrl $ctrl,
42  private readonly \ilObjUser $user,
43  private readonly \ilTestQuestionHeaderBlockBuilder $question_header_builder,
44  private readonly \ilObjTest $test_obj,
45  ) {
46  }
47 
51  public function printSelectedQuestions(
52  array $print_view_types,
53  ?Types $selected_print_view_type,
54  array $question_ids
55  ): void {
56  $this->tabs_manager->resetTabsAndAddBacklink(
57  $this->ctrl->getLinkTargetByClass(\ilObjTestGUI::class, \ilObjTestGUI::SHOW_QUESTIONS_CMD)
58  );
59 
60  $this->toolbar->addComponent(
61  $this->ui_factory->viewControl()->mode(
62  $print_view_types,
63  $this->lng->txt('show_hide_best_solution')
64  )->withActive($selected_print_view_type->getLabel($this->lng))
65  );
66 
67  $this->toolbar->addSeparator();
68  $this->addPrintButtonToToolbar();
69 
70  $template = new \ilTemplate('tpl.il_as_tst_print_questions_preview.html', true, true, 'components/ILIAS/Test');
71 
72  $this->tpl->addCss(\ilUtil::getStyleSheetLocation('output', 'test_print.css'), 'print');
73 
74  $max_points = 0;
75  $counter = 1;
76  $this->question_header_builder->setHeaderMode($this->test_obj->getTitleOutput());
77 
78  foreach ($question_ids as $question_id) {
79  $template->setCurrentBlock('question');
80  $question_gui = $this->test_obj->createQuestionGUI('', $question_id);
81  $question_gui->setRenderPurpose(\assQuestionGUI::RENDER_PURPOSE_PREVIEW);
82 
83  $this->question_header_builder->setQuestionTitle($question_gui->getObject()->getTitleForHTMLOutput());
84  $this->question_header_builder->setQuestionPoints($question_gui->getObject()->getMaximumPoints());
85  $this->question_header_builder->setQuestionPosition($counter);
86  $template->setVariable('QUESTION_HEADER', $this->question_header_builder->getHTML());
87 
88  if ($selected_print_view_type === Types::RESULTS_VIEW_TYPE_HIDE) {
89  $template->setVariable('SOLUTION_OUTPUT', $question_gui->getPreview(false));
90  } else {
91  $template->setVariable('TXT_QUESTION_ID', $this->lng->txt('question_id_short'));
92  $template->setVariable('QUESTION_ID', $question_gui->getObject()->getId());
93  $template->setVariable('SOLUTION_OUTPUT', $question_gui->getSolutionOutput(0, null, false, true, false, false));
94  }
95 
96  $template->parseCurrentBlock('question');
97  $counter++;
98  $max_points += $question_gui->getObject()->getMaximumPoints();
99  }
100 
101  $template->setVariable(
102  'TITLE',
103  $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform(
104  $this->test_obj->getTitle()
105  )
106  );
107  $template->setVariable('PRINT_TEST', $this->lng->txt('tst_print'));
108  $template->setVariable('TXT_PRINT_DATE', $this->lng->txt('date'));
109  $template->setVariable(
110  'VALUE_PRINT_DATE',
111  (new \DateTimeImmutable())
112  ->setTimezone(new \DateTimeZone($this->user->getTimeZone()))
113  ->format($this->user->getDateTimeFormat()->toString())
114  );
115  $template->setVariable(
116  'TXT_MAXIMUM_POINTS',
117  $this->lng->txt('tst_maximum_points')
118  );
119  $template->setVariable('VALUE_MAXIMUM_POINTS', $max_points);
120  $this->tpl->setVariable('PRINT_CONTENT', $template->get());
121  }
122 
123  public function printAnswers(int $question_id): void
124  {
125  $this->tabs_manager->resetTabsAndAddBacklink(
126  $this->ctrl->getLinkTargetByClass(\ilObjTestGUI::class, \ilObjTestGUI::SHOW_QUESTIONS_CMD)
127  );
128 
129  $this->addPrintButtonToToolbar();
130 
131  $template = new \ilTemplate('tpl.il_as_tst_print_questions_answers.html', true, true, 'components/ILIAS/Test');
132  $this->tpl->addCss(\ilUtil::getStyleSheetLocation('output', 'test_print.css'), 'print');
133 
134  $question_gui = $this->test_obj->createQuestionGUI('', $question_id);
135  $question_gui->setRenderPurpose(\assQuestionGUI::RENDER_PURPOSE_PREVIEW);
136  $template->setVariable('TITLE', $question_gui->getObject()->getTitleForHTMLOutput());
137  $template->setVariable('TXT_PRINT_DATE', $this->lng->txt('date'));
138  $template->setVariable(
139  'VALUE_PRINT_DATE',
140  (new \DateTimeImmutable())
141  ->setTimezone(new \DateTimeZone($this->user->getTimeZone()))
142  ->format($this->user->getDateTimeFormat()->toString())
143  );
144 
145  $this->tpl->setVariable(
146  'PRINT_CONTENT',
148  $template,
149  $question_gui,
150  $this->test_obj->getTestId()
151  )->get()
152  );
153  }
154 
156  \ilTemplate $template,
157  \assQuestionGUI $question_gui,
158  int $test_id
159  ): \ilTemplate {
160  $this->test_obj->setAccessFilteredParticipantList(
161  $this->test_obj->buildStatisticsAccessFilteredParticipantList()
162  );
163 
164  $foundusers = $this->test_obj->getParticipantsForTestAndQuestion(
165  $test_id,
166  $question_gui->getObject()->getId()
167  );
168 
169  foreach ($foundusers as $active_id => $passes) {
170  if (($resultpass = \ilObjTest::_getResultPass($active_id)) === null) {
171  continue;
172  }
173 
174  for ($i = 0; $i < count($passes); $i++) {
175  if ($passes[$i]['pass'] !== $resultpass) {
176  continue;
177  }
178 
179  $template->setCurrentBlock('question');
180  $template = $this->addResultUserInfoToTemplate(
181  $template,
182  $active_id,
183  $resultpass + 1
184  );
185  $template->setVariable(
186  'SOLUTION_OUTPUT',
187  $question_gui->getSolutionOutput(
188  $active_id,
189  $resultpass,
190  false,
191  false,
192  false,
193  false
194  )
195  );
196  $template->parseCurrentBlock('question');
197  }
198  }
199  return $template;
200  }
201 
202  private function addResultUserInfoToTemplate(
203  \ilTemplate $template,
204  int $active_id,
205  int $pass
206  ): \ilTemplate {
207  $user_id = $this->test_obj->_getUserIdFromActiveId($active_id);
208  if (\ilObjUser::_lookupLogin($user_id) !== '') {
209  $user = new \ilObjUser($user_id);
210  } else {
211  $user = new \ilObjUser();
212  $user->setLastname($this->lng->txt('deleted_user'));
213  }
214  if ($user->getMatriculation() !== ''
215  && $this->test_obj->getAnonymity() === false) {
216  $template->setCurrentBlock('matriculation');
217  $template->setVariable('TXT_USR_MATRIC', $this->lng->txt('matriculation'));
218  $template->setVariable('VALUE_USR_MATRIC', $user->getMatriculation());
219  $template->parseCurrentBlock();
220  }
221 
222  $template->setVariable('TXT_USR_NAME', $this->lng->txt('name'));
223  $uname = $this->test_obj->userLookupFullName($user_id, false);
224  $template->setVariable('VALUE_USR_NAME', $uname);
225  $template->setVariable('TXT_PASS', $this->lng->txt('scored_pass'));
226  $template->setVariable('VALUE_PASS', $pass);
227  return $template;
228  }
229 
230  private function addPrintButtonToToolbar(): void
231  {
232  $this->toolbar->addComponent(
233  $this->ui_factory->button()->standard($this->lng->txt('print'), '')
234  ->withOnLoadCode(fn($id) => "$('#$id').on('click', ()=>{window.print();})")
235  );
236  }
237 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
addQuestionResultForTestUsersToTemplate(\ilTemplate $template, \assQuestionGUI $question_gui, int $test_id)
Definition: Printer.php:155
static getStyleSheetLocation(string $mode="output", string $a_css_name="")
get full style sheet file name (path inclusive) of current user
printSelectedQuestions(array $print_view_types, ?Types $selected_print_view_type, array $question_ids)
Definition: Printer.php:51
addResultUserInfoToTemplate(\ilTemplate $template, int $active_id, int $pass)
Definition: Printer.php:202
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
static _getResultPass($active_id)
Retrieves the pass number that should be counted for a given user.
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(private readonly UIFactory $ui_factory, private \ilGlobalTemplateInterface $tpl, private TabsManager $tabs_manager, private \ilToolbarGUI $toolbar, private readonly Refinery $refinery, private readonly Language $lng, private readonly \ilCtrl $ctrl, private readonly \ilObjUser $user, private readonly \ilTestQuestionHeaderBlockBuilder $question_header_builder, private readonly \ilObjTest $test_obj,)
Definition: Printer.php:34
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
global $lng
Definition: privfeed.php:31
getSolutionOutput(int $active_id, ?int $pass=null, bool $graphical_output=false, bool $result_output=false, bool $show_question_only=true, bool $show_feedback=false, bool $show_correct_solution=false, bool $show_manual_scoring=false, bool $show_question_text=true, bool $show_inline_feedback=true)
static _lookupLogin(int $a_user_id)