ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
QuestionsTableActions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Test\Questions\Properties\Repository as TestQuestionsRepository;
24use ILIAS\UI\Factory as UIFactory;
25use ILIAS\UI\Renderer as UIRenderer;
31use Psr\Http\Message\ServerRequestInterface;
32
34{
35 private const ACTION_SAVE_ORDER = 'save_order';
36 private const ACTION_DELETE = 'delete';
37 private const ACTION_DELETE_CONFIRMED = 'deletion_confirmed';
38 private const ACTION_COPY = 'copy';
39 private const ACTION_ADD_TO_POOL = 'add_qpl';
40 private const ACTION_PREVIEW = 'preview';
41 private const ACTION_ADJUST = 'correction';
42 private const ACTION_STATISTICS = 'statistics';
43 private const ACTION_EDIT_QUESTION = 'edit_question';
44 private const ACTION_EDIT_PAGE = 'edit_page';
45 private const ACTION_FEEDBACK = 'feedback';
46 private const ACTION_PRINT_QUESTIONS = 'print_questions';
47 private const ACTION_PRINT_ANSWERS = 'print_answers';
48 private const ACTION_DOWNLOAD_FILE_QUESTION_ANSWERS = 'download_files';
49
50 private string $table_id;
51
55 public function __construct(
56 private readonly UIFactory $ui_factory,
57 private readonly UIRenderer $ui_renderer,
58 private \ilGlobalTemplateInterface $tpl,
59 private readonly ServerRequestInterface $request,
60 private readonly QuestionsTableQuery $table_query,
61 private readonly Language $lng,
62 private readonly \ilCtrl $ctrl,
63 private readonly TestQuestionsRepository $questionrepository,
64 private readonly Printer $question_printer,
65 private readonly \ilObjTest $test_obj,
66 private readonly bool $is_adjusting_questions_with_results_allowed,
67 private readonly bool $is_in_test_with_results,
68 private readonly bool $is_in_test_with_random_question_set,
69 private readonly \ilTestQuestionSetConfigFactory $test_question_set_config_factory
70 ) {
71 $this->table_id = (string) $test_obj->getId();
72 }
73
74 public function setDisabledActions(
75 OrderingRow $row
76 ): OrderingRow {
77 $disable_default_actions = $this->is_in_test_with_random_question_set
78 || $this->is_in_test_with_results;
79
80 return $row->withDisabledAction(
81 self::ACTION_ADD_TO_POOL,
82 $this->is_in_test_with_random_question_set
83 )->withDisabledAction(self::ACTION_EDIT_QUESTION, $disable_default_actions)
84 ->withDisabledAction(self::ACTION_EDIT_PAGE, $disable_default_actions)
85 ->withDisabledAction(
86 self::ACTION_ADJUST,
87 !$this->is_adjusting_questions_with_results_allowed || !$this->is_in_test_with_results
88 )->withDisabledAction(self::ACTION_FEEDBACK, $disable_default_actions)
89 ->withDisabledAction(self::ACTION_PRINT_ANSWERS, !$this->is_in_test_with_results)
90 ->withDisabledAction(
91 self::ACTION_DOWNLOAD_FILE_QUESTION_ANSWERS,
92 $row->getCellContent('type_tag') !== $this->lng->txt(\assFileUpload::class)
93 || !$this->questionrepository->questionHasAnswers((int) $row->getId())
94 );
95 }
96
97 public function getOrderActionUrl(): URI
98 {
99 return $this->table_query->getActionURL(self::ACTION_SAVE_ORDER);
100 }
101
102 public function getActions(): array
103 {
104 $ag = fn(string $type, string $label, $action): TableAction => $this->
105 ui_factory->table()->action()->$type(
106 $this->lng->txt($label),
107 ...$this->table_query->getRowBoundURLBuilder($action)
108 );
109
110 $actions = [
111 self::ACTION_PREVIEW => $ag('single', 'preview', self::ACTION_PREVIEW),
112 self::ACTION_ADJUST => $ag('single', 'tst_corrections_qst_form', self::ACTION_ADJUST),
113 self::ACTION_STATISTICS => $ag('single', 'statistics', self::ACTION_STATISTICS),
114 self::ACTION_EDIT_QUESTION => $ag('single', 'edit_question', self::ACTION_EDIT_QUESTION),
115 self::ACTION_EDIT_PAGE => $ag('single', 'edit_page', self::ACTION_EDIT_PAGE),
116 self::ACTION_FEEDBACK => $ag('single', 'tst_feedback', self::ACTION_FEEDBACK),
117 self::ACTION_PRINT_ANSWERS => $ag('single', 'print_answers', self::ACTION_PRINT_ANSWERS),
118 self::ACTION_DOWNLOAD_FILE_QUESTION_ANSWERS => $ag('single', 'download_all_files', self::ACTION_DOWNLOAD_FILE_QUESTION_ANSWERS),
119 ];
120
121 if (!$this->is_in_test_with_random_question_set
122 && (!$this->is_in_test_with_results || $this->is_adjusting_questions_with_results_allowed)) {
123 $actions[self::ACTION_DELETE] = $ag('standard', 'delete', self::ACTION_DELETE)
124 ->withAsync();
125 }
126
127 if (!$this->is_in_test_with_random_question_set && !$this->is_in_test_with_results) {
128 $actions[self::ACTION_COPY] = $ag('standard', 'copy', self::ACTION_COPY);
129 }
130
131 return $actions + [
132 self::ACTION_ADD_TO_POOL => $ag('standard', 'copy_and_link_to_questionpool', self::ACTION_ADD_TO_POOL),
133 self::ACTION_PRINT_QUESTIONS => $ag('multi', 'print', self::ACTION_PRINT_QUESTIONS)
134 ];
135 }
136
137 public function getQuestionTargetLinkBuilder(): \Closure
138 {
139 return function (int $question_id): string {
140 [$url_builder, $row_id_token] = $this->table_query->getRowBoundURLBuilder(self::ACTION_PREVIEW);
141 return $url_builder->withParameter($row_id_token, (string) $question_id)
142 ->buildURI()
143 ->__toString();
144 };
145 }
146
150 public function handleCommand(
151 string $cmd,
152 array $row_ids,
153 \Closure $protect_by_write_protection,
154 \Closure $copy_and_link_to_questionpool,
155 \Closure $get_table
156 ): bool {
157 switch ($cmd) {
158 case self::ACTION_SAVE_ORDER:
159 $protect_by_write_protection();
160
161 if ($this->is_in_test_with_random_question_set || $this->is_in_test_with_results) {
162 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('questionlist_cannot_be_altered'));
163 return true;
164 }
165
166 $data = $get_table()->getTableComponent()->getData();
167 $this->test_obj->setQuestionOrder(array_flip($data), []);
168 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
169 return true;
170
171 case self::ACTION_PREVIEW:
172 $this->redirectWithQuestionParameters(
173 current($row_ids),
174 \ilAssQuestionPreviewGUI::class,
176 );
177 return false;
178
179 case self::ACTION_ADJUST:
180 $this->redirectWithQuestionParameters(
181 current($row_ids),
182 \ilTestCorrectionsGUI::class,
183 'showQuestion'
184 );
185 return false;
186
187 case self::ACTION_STATISTICS:
188 $this->redirectWithQuestionParameters(
189 current($row_ids),
190 \ilAssQuestionPreviewGUI::class,
192 );
193 return false;
194
195 case self::ACTION_EDIT_QUESTION:
196 $question_id = current($row_ids);
197 $qtype = $this->test_obj->getQuestionType($question_id);
198 $target_class = $qtype . 'GUI';
199 $this->redirectWithQuestionParameters(
200 $question_id,
201 $target_class,
202 'editQuestion'
203 );
204 return false;
205
206 case self::ACTION_EDIT_PAGE:
207 $this->redirectWithQuestionParameters(
208 current($row_ids),
209 \ilAssQuestionPageGUI::class,
210 'edit'
211 );
212 return false;
213
214 case self::ACTION_FEEDBACK:
215 $this->redirectWithQuestionParameters(
216 current($row_ids),
217 \ilAssQuestionFeedbackEditingGUI::class,
219 );
220 return false;
221
222 case self::ACTION_DELETE:
223 echo $this->ui_renderer->renderAsync(
224 $this->getDeleteConfirmation(array_filter($row_ids))
225 );
226 exit();
227
228 case self::ACTION_DELETE_CONFIRMED:
229 $row_ids = $this->request->getParsedBody()['interruptive_items'] ?? [];
230 if (array_filter($row_ids) === []) {
231
232 return true;
233 }
234
235 if ($this->is_in_test_with_random_question_set
236 || $this->is_in_test_with_results
237 && !$this->is_adjusting_questions_with_results_allowed) {
238 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('questionlist_cannot_be_altered'));
239 return true;
240 }
241
242 $protect_by_write_protection();
243 if ($this->is_in_test_with_results) {
244 $this->test_obj->removeQuestionsWithResults($row_ids);
245 } else {
246 $this->test_obj->removeQuestions($row_ids);
247 $this->test_obj->saveCompleteStatus($this->test_question_set_config_factory->getQuestionSetConfig());
248 }
249 $this->tpl->setOnScreenMessage('success', $this->lng->txt('tst_questions_removed'), true);
250 return true;
251
252 case self::ACTION_COPY:
253 if (array_filter($row_ids) === []) {
254 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_no_questions_selected'));
255 return true;
256 }
257
258 if ($this->is_in_test_with_random_question_set || $this->is_in_test_with_results) {
259 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('questionlist_cannot_be_altered'));
260 return true;
261 }
262
263 $protect_by_write_protection();
264 $this->test_obj->copyQuestions($row_ids);
265 $this->tpl->setOnScreenMessage('success', $this->lng->txt('copy_questions_success'), true);
266 return true;
267
268 case self::ACTION_ADD_TO_POOL:
269 if (array_filter($row_ids) === []) {
270 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_no_question_selected_for_moving_to_qpl'));
271 return true;
272 }
273 $protect_by_write_protection();
274 if (!$this->checkQuestionParametersForCopyToPool($row_ids)) {
275 return true;
276 }
277 $copy_and_link_to_questionpool();
278 return false;
279
280 case self::ACTION_PRINT_QUESTIONS:
281 if (array_filter($row_ids) === []) {
282 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('msg_no_questions_selected'));
283 return true;
284 }
285 $protect_by_write_protection();
286 $this->question_printer->printSelectedQuestions(
287 array_reduce(
288 Types::cases(),
289 function (array $c, Types $v): array {
290 $c[$v->getLabel($this->lng)] = $this->table_query
291 ->getPrintViewTypeURL(self::ACTION_PRINT_QUESTIONS, $v->value)->__toString();
292 return $c;
293 },
294 []
295 ),
296 Types::tryFrom($this->table_query->getPrintViewType()) ?? Types::RESULTS_VIEW_TYPE_SHOW,
297 $row_ids
298 );
299 return false;
300
301 case self::ACTION_PRINT_ANSWERS:
302 $protect_by_write_protection();
303 $this->question_printer->printAnswers(current($row_ids));
304 return false;
305
306 case self::ACTION_DOWNLOAD_FILE_QUESTION_ANSWERS:
307 $protect_by_write_protection();
308 $question_object = \assQuestion::instantiateQuestion(current($row_ids));
309 if ($question_object instanceof \ilObjFileHandlingQuestionType) {
310 $question_object->deliverFileUploadZIPFile(
311 $this->test_obj->getRefId(),
312 $this->test_obj->getTestId(),
313 $this->test_obj->getTitle()
314 );
315 }
316
317 // no break
318 default:
319 throw new \InvalidArgumentException("No such table_cmd: '$cmd'.");
320 }
321 }
322
323 private function getDeleteConfirmation(array $row_ids): Interruptive
324 {
325 $items = [];
326 foreach ($row_ids as $id) {
327 $qdata = $this->test_obj->getQuestionDataset($id);
328 $type = $this->questionrepository->getQuestionPropertiesForQuestionId($id)
329 ->getGeneralQuestionProperties()->getTypeName($this->lng);
330 $icon = $this->ui_renderer->render(
331 $this->ui_factory->symbol()->icon()->standard('ques', $type, 'small')
332 );
333 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyvalue(
334 (string) $id,
335 $icon . ' ' . $qdata->title,
336 $type
337 );
338 }
339
340 return $this->ui_factory->modal()->interruptive(
341 $this->lng->txt('remove'),
342 $this->lng->txt(
343 $this->is_in_test_with_results
344 ? 'tst_remove_questions_and_results'
345 : 'tst_remove_questions'
346 ),
347 $this->table_query->getActionURL(self::ACTION_DELETE_CONFIRMED)->__toString()
348 )
349 ->withAffectedItems($items);
350 }
351
353 int $question_id,
354 string $target_class,
355 string $cmd
356 ): void {
357
358 $this->ctrl->setParameterByClass(
359 $target_class,
360 'q_id',
361 $question_id
362 );
363
364 $this->ctrl->setParameterByClass(
365 $target_class,
366 'calling_test',
367 (string) $this->test_obj->getRefId()
368 );
369
370 $this->ctrl->redirectByClass($target_class, $cmd);
371 }
372
376 private function checkQuestionParametersForCopyToPool(array $question_ids): bool
377 {
378 $question_properties = $this->questionrepository
379 ->getQuestionPropertiesForQuestionIds($question_ids);
380 foreach ($question_ids as $q_id) {
381 if (!$this->questionrepository->originalQuestionExists($q_id)) {
382 continue;
383 }
384
387 $question_properties[$q_id]->getGeneralQuestionProperties()->getOriginalId()
388 )
389 );
390
391 if ($type !== 'tst') {
392 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('tst_link_only_unassigned'), true);
393 return false;
394 }
395 }
396 return true;
397 }
398}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
redirectWithQuestionParameters(int $question_id, string $target_class, string $cmd)
handleCommand(string $cmd, array $row_ids, \Closure $protect_by_write_protection, \Closure $copy_and_link_to_questionpool, \Closure $get_table)
__construct(private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private \ilGlobalTemplateInterface $tpl, private readonly ServerRequestInterface $request, private readonly QuestionsTableQuery $table_query, private readonly Language $lng, private readonly \ilCtrl $ctrl, private readonly TestQuestionsRepository $questionrepository, private readonly Printer $question_printer, private readonly \ilObjTest $test_obj, private readonly bool $is_adjusting_questions_with_results_allowed, private readonly bool $is_in_test_with_results, private readonly bool $is_in_test_with_random_question_set, private readonly \ilTestQuestionSetConfigFactory $test_question_set_config_factory)
static lookupParentObjId(int $question_id)
static instantiateQuestion(int $question_id)
Class ilCtrl provides processing control methods.
static _lookupType(int $id, bool $reference=false)
$c
Definition: deliver.php:25
exit
withAffectedItems(array $items)
Get a modal like this listing the given items in the content section below the message.
withDisabledAction(string $action_id, bool $disable=true)
Refer to an Action by its id and disable it for this row/record only.
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31