ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilAssQuestionList.php
Go to the documentation of this file.
1 <?php
2 
21 
29 {
30  private array $parentObjIdsFilter = [];
31  private ?int $parentObjId = null;
32  private string $parentObjType = 'qpl';
33  private array $availableTaxonomyIds = [];
34  private array $fieldFilters = [];
35  private array $taxFilters = [];
37  private array $taxParentIds = [];
38  private array $taxParentTypes = [];
39  private ?int $answerStatusActiveId = null;
40  private array $forcedQuestionIds = [];
41  protected bool $join_obj_data = true;
42 
46  public const QUESTION_ANSWER_STATUS_NON_ANSWERED = 'nonAnswered';
47  public const QUESTION_ANSWER_STATUS_WRONG_ANSWERED = 'wrongAnswered';
48  public const QUESTION_ANSWER_STATUS_CORRECT_ANSWERED = 'correctAnswered';
49 
53  public const ANSWER_STATUS_FILTER_ALL_NON_CORRECT = 'allNonCorrect';
54  public const ANSWER_STATUS_FILTER_NON_ANSWERED_ONLY = 'nonAnswered';
55  public const ANSWER_STATUS_FILTER_WRONG_ANSWERED_ONLY = 'wrongAnswered';
56 
57  private $answerStatusFilter = null;
58 
59  public const QUESTION_INSTANCE_TYPE_ORIGINALS = 'QST_INSTANCE_TYPE_ORIGINALS';
60  public const QUESTION_INSTANCE_TYPE_DUPLICATES = 'QST_INSTANCE_TYPE_DUPLICATES';
61  public const QUESTION_INSTANCE_TYPE_ALL = 'QST_INSTANCE_TYPE_ALL';
62  private string $questionInstanceTypeFilter = self::QUESTION_INSTANCE_TYPE_ORIGINALS;
63 
64  private $includeQuestionIdsFilter = null;
65  private $excludeQuestionIdsFilter = null;
66 
67  public const QUESTION_COMPLETION_STATUS_COMPLETE = 'complete';
68  public const QUESTION_COMPLETION_STATUS_INCOMPLETE = 'incomplete';
69  public const QUESTION_COMPLETION_STATUS_BOTH = 'complete/incomplete';
70  private string $questionCompletionStatusFilter = self::QUESTION_COMPLETION_STATUS_BOTH;
71 
72  public const QUESTION_COMMENTED_ONLY = '1';
73  public const QUESTION_COMMENTED_EXCLUDED = '2';
74  protected ?string $filter_comments = null;
75 
76  protected array $questions = [];
77 
78  public function __construct(
79  private ilDBInterface $db,
80  private ilLanguage $lng,
81  private Refinery $refinery,
82  private ilComponentRepository $component_repository,
83  private ?NotesService $notes_service = null
84  ) {
85  }
86 
87  public function getParentObjId(): ?int
88  {
89  return $this->parentObjId;
90  }
91 
92  public function setParentObjId($parentObjId): void
93  {
94  $this->parentObjId = $parentObjId;
95  }
96 
97  public function getParentObjectType(): string
98  {
99  return $this->parentObjType;
100  }
101 
102  public function setParentObjectType($parentObjType): void
103  {
104  $this->parentObjType = $parentObjType;
105  }
106 
107  public function getParentObjIdsFilter(): array
108  {
110  }
111 
115  public function setParentObjIdsFilter($parentObjIdsFilter): void
116  {
117  $this->parentObjIdsFilter = $parentObjIdsFilter;
118  }
119 
120  public function setQuestionInstanceTypeFilter($questionInstanceTypeFilter): void
121  {
122  $this->questionInstanceTypeFilter = (string) $questionInstanceTypeFilter;
123  }
124 
126  {
128  }
129 
130  public function setIncludeQuestionIdsFilter($questionIdsFilter): void
131  {
132  $this->includeQuestionIdsFilter = $questionIdsFilter;
133  }
134 
135  public function getIncludeQuestionIdsFilter()
136  {
138  }
139 
140  public function getExcludeQuestionIdsFilter()
141  {
143  }
144 
146  {
147  $this->excludeQuestionIdsFilter = $excludeQuestionIdsFilter;
148  }
149 
150  public function getQuestionCompletionStatusFilter(): string
151  {
153  }
154 
155  public function setQuestionCompletionStatusFilter($questionCompletionStatusFilter): void
156  {
157  $this->questionCompletionStatusFilter = $questionCompletionStatusFilter;
158  }
159 
160  public function addFieldFilter($fieldName, $fieldValue): void
161  {
162  $this->fieldFilters[$fieldName] = $fieldValue;
163  }
164 
165  public function addTaxonomyFilter($taxId, $taxNodes, $parentObjId, $parentObjType): void
166  {
167  $this->taxFilters[$taxId] = $taxNodes;
168  $this->taxParentIds[$taxId] = $parentObjId;
169  $this->taxParentTypes[$taxId] = $parentObjType;
170  }
171 
172  public function addTaxonomyFilterNoTaxonomySet(bool $flag): void
173  {
174  $this->taxFiltersExcludeAnyObjectsWithTaxonomies = $flag;
175  }
176 
177  public function setAvailableTaxonomyIds($availableTaxonomyIds): void
178  {
179  $this->availableTaxonomyIds = $availableTaxonomyIds;
180  }
181 
182  public function getAvailableTaxonomyIds(): array
183  {
185  }
186 
187  public function setAnswerStatusActiveId($answerStatusActiveId): void
188  {
189  $this->answerStatusActiveId = $answerStatusActiveId;
190  }
191 
192  public function getAnswerStatusActiveId(): ?int
193  {
195  }
196 
198  {
199  $this->answerStatusFilter = $answerStatusFilter;
200  }
201 
202  public function getAnswerStatusFilter(): ?string
203  {
205  }
206 
212  public function setJoinObjectData($a_val): void
213  {
214  $this->join_obj_data = $a_val;
215  }
216 
222  public function getJoinObjectData(): bool
223  {
224  return $this->join_obj_data;
225  }
226 
230  public function setForcedQuestionIds($forcedQuestionIds): void
231  {
232  $this->forcedQuestionIds = $forcedQuestionIds;
233  }
234 
238  public function getForcedQuestionIds(): array
239  {
241  }
242 
243  private function getParentObjFilterExpression(): ?string
244  {
245  if ($this->getParentObjId()) {
246  return 'qpl_questions.obj_fi = ' . $this->db->quote($this->getParentObjId(), 'integer');
247  }
248 
249  if (count($this->getParentObjIdsFilter())) {
250  return $this->db->in('qpl_questions.obj_fi', $this->getParentObjIdsFilter(), false, 'integer');
251  }
252 
253  return null;
254  }
255 
256  private function getFieldFilterExpressions(): array
257  {
258  $expressions = [];
259 
260  foreach ($this->fieldFilters as $fieldName => $fieldValue) {
261  switch ($fieldName) {
262  case 'title':
263  case 'description':
264  case 'author':
265  case 'lifecycle':
266 
267  $expressions[] = $this->db->like('qpl_questions.' . $fieldName, 'text', "%%$fieldValue%%");
268  break;
269 
270  case 'type':
271 
272  $expressions[] = "qpl_qst_type.type_tag = {$this->db->quote($fieldValue, 'text')}";
273  break;
274 
275  case 'question_id':
276  if ($fieldValue != "" && !is_array($fieldValue)) {
277  $fieldValue = array($fieldValue);
278  }
279  $expressions[] = $this->db->in("qpl_questions.question_id", $fieldValue, false, "integer");
280  break;
281 
282  case 'parent_title':
283  if ($this->join_obj_data) {
284  $expressions[] = $this->db->like('object_data.title', 'text', "%%$fieldValue%%");
285  }
286  break;
287  }
288  }
289 
290  return $expressions;
291  }
292 
293  private function getTaxonomyFilterExpressions(): array
294  {
295  $expressions = [];
296  if ($this->taxFiltersExcludeAnyObjectsWithTaxonomies) {
297  $expressions[] = 'question_id NOT IN (SELECT DISTINCT item_id FROM tax_node_assignment)';
298  return $expressions;
299  }
300 
301  foreach ($this->taxFilters as $taxId => $taxNodes) {
302  $questionIds = [];
303 
304  $forceBypass = true;
305 
306  foreach ($taxNodes as $taxNode) {
307  $forceBypass = false;
308 
309  $taxItemsByTaxParent = $this->getTaxItems(
310  $this->taxParentTypes[$taxId],
311  $this->taxParentIds[$taxId],
312  $taxId,
313  $taxNode
314  );
315 
316  $taxItemsByParent = $this->getTaxItems(
317  $this->parentObjType,
318  $this->parentObjId,
319  $taxId,
320  $taxNode
321  );
322 
323  $taxItems = array_merge($taxItemsByTaxParent, $taxItemsByParent);
324  foreach ($taxItems as $taxItem) {
325  $questionIds[$taxItem['item_id']] = $taxItem['item_id'];
326  }
327  }
328 
329  if (!$forceBypass) {
330  $expressions[] = $this->db->in('question_id', $questionIds, false, 'integer');
331  }
332  }
333 
334  return $expressions;
335  }
336 
344  protected function getTaxItems($parentType, $parentObjId, $taxId, $taxNode): array
345  {
346  $taxTree = new ilTaxonomyTree($taxId);
347 
348  $taxNodeAssignment = new ilTaxNodeAssignment(
349  $parentType,
350  $parentObjId,
351  'quest',
352  $taxId
353  );
354 
355  $subNodes = $taxTree->getSubTreeIds($taxNode);
356  $subNodes[] = $taxNode;
357 
358  return $taxNodeAssignment->getAssignmentsOfNode($subNodes);
359  }
360 
361  private function getQuestionInstanceTypeFilterExpression(): ?string
362  {
363  switch ($this->getQuestionInstanceTypeFilter()) {
364  case self::QUESTION_INSTANCE_TYPE_ORIGINALS:
365  return 'qpl_questions.original_id IS NULL';
366  case self::QUESTION_INSTANCE_TYPE_DUPLICATES:
367  return 'qpl_questions.original_id IS NOT NULL';
368  case self::QUESTION_INSTANCE_TYPE_ALL:
369  default:
370  return null;
371  }
372 
373  return null;
374  }
375 
376  private function getQuestionIdsFilterExpressions(): array
377  {
378  $expressions = [];
379 
380  if (is_array($this->getIncludeQuestionIdsFilter())) {
381  $expressions[] = $this->db->in(
382  'qpl_questions.question_id',
384  false,
385  'integer'
386  );
387  }
388 
389  if (is_array($this->getExcludeQuestionIdsFilter())) {
390  $IN = $this->db->in(
391  'qpl_questions.question_id',
393  true,
394  'integer'
395  );
396 
397  if ($IN == ' 1=2 ') {
398  $IN = ' 1=1 ';
399  } // required for ILIAS < 5.0
400 
401  $expressions[] = $IN;
402  }
403 
404  return $expressions;
405  }
406 
407  private function getParentObjectIdFilterExpression(): ?string
408  {
409  if ($this->parentObjId) {
410  return "qpl_questions.obj_fi = {$this->db->quote($this->parentObjId, 'integer')}";
411  }
412 
413  return null;
414  }
415 
416  private function getAnswerStatusFilterExpressions(): array
417  {
418  $expressions = [];
419 
420  switch ($this->getAnswerStatusFilter()) {
421  case self::ANSWER_STATUS_FILTER_ALL_NON_CORRECT:
422 
423  $expressions[] = '
424  (tst_test_result.question_fi IS NULL OR tst_test_result.points < qpl_questions.points)
425  ';
426  break;
427 
428  case self::ANSWER_STATUS_FILTER_NON_ANSWERED_ONLY:
429 
430  $expressions[] = 'tst_test_result.question_fi IS NULL';
431  break;
432 
433  case self::ANSWER_STATUS_FILTER_WRONG_ANSWERED_ONLY:
434 
435  $expressions[] = 'tst_test_result.question_fi IS NOT NULL';
436  $expressions[] = 'tst_test_result.points < qpl_questions.points';
437  break;
438  }
439 
440  return $expressions;
441  }
442 
443  private function getTableJoinExpression(): string
444  {
445  $tableJoin = "
446  INNER JOIN qpl_qst_type
447  ON qpl_qst_type.question_type_id = qpl_questions.question_type_fi
448  ";
449 
450  if ($this->join_obj_data) {
451  $tableJoin .= "
452  INNER JOIN object_data
453  ON object_data.obj_id = qpl_questions.obj_fi
454  ";
455  }
456 
457  if ($this->getParentObjectType() === 'tst'
458  && $this->getQuestionInstanceTypeFilter() === self::QUESTION_INSTANCE_TYPE_ALL) {
459  $tableJoin .= "
460  INNER JOIN tst_test_question tstquest
461  ON tstquest.question_fi = qpl_questions.question_id
462  ";
463  }
464 
465  if ($this->getAnswerStatusActiveId()) {
466  $tableJoin .= "
467  LEFT JOIN tst_test_result
468  ON tst_test_result.question_fi = qpl_questions.question_id
469  AND tst_test_result.active_fi = {$this->db->quote($this->getAnswerStatusActiveId(), 'integer')}
470  ";
471  }
472 
473  return $tableJoin;
474  }
475 
476  private function getConditionalFilterExpression(): string
477  {
478  $CONDITIONS = [];
479 
480  if ($this->getQuestionInstanceTypeFilterExpression() !== null) {
481  $CONDITIONS[] = $this->getQuestionInstanceTypeFilterExpression();
482  }
483 
484  if ($this->getParentObjFilterExpression() !== null) {
485  $CONDITIONS[] = $this->getParentObjFilterExpression();
486  }
487 
488  if ($this->getParentObjectIdFilterExpression() !== null) {
489  $CONDITIONS[] = $this->getParentObjectIdFilterExpression();
490  }
491 
492  $CONDITIONS = array_merge(
493  $CONDITIONS,
495  $this->getFieldFilterExpressions(),
498  );
499 
500  $CONDITIONS = implode(' AND ', $CONDITIONS);
501 
502  return strlen($CONDITIONS) ? 'AND ' . $CONDITIONS : '';
503  }
504 
505  private function getSelectFieldsExpression(): string
506  {
507  $selectFields = array(
508  'qpl_questions.*',
509  'qpl_qst_type.type_tag',
510  'qpl_qst_type.plugin',
511  'qpl_qst_type.plugin_name',
512  'qpl_questions.points max_points'
513  );
514 
515  if ($this->join_obj_data) {
516  $selectFields[] = 'object_data.title parent_title';
517  }
518 
519  if ($this->getAnswerStatusActiveId()) {
520  $selectFields[] = 'tst_test_result.points reached_points';
521  $selectFields[] = "CASE
522  WHEN tst_test_result.points IS NULL THEN '" . self::QUESTION_ANSWER_STATUS_NON_ANSWERED . "'
523  WHEN tst_test_result.points < qpl_questions.points THEN '" . self::QUESTION_ANSWER_STATUS_WRONG_ANSWERED . "'
524  ELSE '" . self::QUESTION_ANSWER_STATUS_CORRECT_ANSWERED . "'
525  END question_answer_status
526  ";
527  }
528 
529  $selectFields = implode(",\n\t\t\t\t", $selectFields);
530 
531  return "
532  SELECT {$selectFields}
533  ";
534  }
535 
536  private function buildBasicQuery(): string
537  {
538  return "
539  {$this->getSelectFieldsExpression()}
540 
541  FROM qpl_questions
542 
543  {$this->getTableJoinExpression()}
544 
545  WHERE qpl_questions.tstamp > 0
546  ";
547  }
548 
549  private function buildQuery(): string
550  {
551  $query = $this->buildBasicQuery() . "
552  {$this->getConditionalFilterExpression()}
553  ";
554 
555  if (count($this->getForcedQuestionIds())) {
556  $query .= "
557  UNION {$this->buildBasicQuery()}
558  AND {$this->db->in('qpl_questions.question_id', $this->getForcedQuestionIds(), false, 'integer')}
559  ";
560  }
561 
562  return $query;
563  }
564 
565  public function load(): void
566  {
567  $this->checkFilters();
568 
569  $tags_trafo = $this->refinery->string()->stripTags();
570 
571  $query = $this->buildQuery();
572  $res = $this->db->query($query);
573  while ($row = $this->db->fetchAssoc($res)) {
575 
576  if (!$this->isActiveQuestionType($row)) {
577  continue;
578  }
579 
580  $row['title'] = $tags_trafo->transform($row['title'] ?? '&nbsp;');
581  $row['description'] = $tags_trafo->transform($row['description'] !== '' && $row['description'] !== null ? $row['description'] : '&nbsp;');
582  $row['author'] = $tags_trafo->transform($row['author']);
583  $row['taxonomies'] = $this->loadTaxonomyAssignmentData($row['obj_fi'], $row['question_id']);
584  $row['ttype'] = $this->lng->txt($row['type_tag']);
585  $row['feedback'] = $this->hasFeedback((int) $row['question_id']);
586  $row['hints'] = $this->hasHints((int) $row['question_id']);
587  $row['comments'] = $this->getNumberOfCommentsForQuestion($row['question_id']);
588 
589  if (
590  $this->filter_comments === self::QUESTION_COMMENTED_ONLY && $row['comments'] === 0
591  || $this->filter_comments === self::QUESTION_COMMENTED_EXCLUDED && $row['comments'] > 0
592  ) {
593  continue;
594  }
595 
596  $this->questions[ $row['question_id'] ] = $row;
597  }
598  }
599 
600  protected function getNumberOfCommentsForQuestion(int $question_id): int
601  {
602  if ($this->notes_service === null) {
603  return 0;
604  }
605  $notes_context = $this->notes_service->data()->context(
606  $this->getParentObjId(),
607  $question_id,
608  'quest'
609  );
610  return $this->notes_service->domain()->getNrOfCommentsForContext($notes_context);
611  }
612 
613  public function setCommentFilter(int $commented = null)
614  {
615  $this->filter_comments = $commented;
616  }
617 
618  protected function hasFeedback(int $question_id): bool
619  {
620  $pagetypes = [
623  ];
624  $res = $this->db->queryF(
625  "SELECT feedback, feedback_id FROM qpl_fb_generic
626  WHERE question_fi = %s
627  UNION ALL
628  SELECT feedback, feedback_id FROM qpl_fb_specific
629  WHERE question_fi = %s",
630  ['integer', 'integer', ],
631  [$question_id, $question_id]
632  );
633  while ($row = $this->db->fetchAssoc($res)) {
634  if (trim((string) $row['feedback']) !== '') {
635  return true;
636  }
637  foreach ($pagetypes as $pagetype) {
638  if (\ilPageUtil::_existsAndNotEmpty($pagetype, $row['feedback_id'])) {
639  return true;
640  }
641  }
642  }
643  return false;
644  }
645 
646  protected function hasHints(int $question_id): bool
647  {
648  $questionHintList = ilAssQuestionHintList::getListByQuestionId($question_id);
649  return iterator_count($questionHintList) > 0;
650  }
651 
652  private function loadTaxonomyAssignmentData($parentObjId, $questionId): array
653  {
654  $taxAssignmentData = [];
655 
656  foreach ($this->getAvailableTaxonomyIds() as $taxId) {
657  $taxTree = new ilTaxonomyTree($taxId);
658 
659  $taxAssignment = new ilTaxNodeAssignment('qpl', $parentObjId, 'quest', $taxId);
660 
661  $assignments = $taxAssignment->getAssignmentsOfItem($questionId);
662 
663  foreach ($assignments as $assData) {
664  if (!isset($taxAssignmentData[ $assData['tax_id'] ])) {
665  $taxAssignmentData[ $assData['tax_id'] ] = [];
666  }
667 
668  $nodeData = $taxTree->getNodeData($assData['node_id']);
669 
670  $assData['node_lft'] = $nodeData['lft'];
671 
672  $taxAssignmentData[ $assData['tax_id'] ][ $assData['node_id'] ] = $assData;
673  }
674  }
675 
676  return $taxAssignmentData;
677  }
678 
679  private function isActiveQuestionType(array $questionData): bool
680  {
681  if (!isset($questionData['plugin'])) {
682  return false;
683  }
684 
685  if (!$questionData['plugin']) {
686  return true;
687  }
688 
689  if (!$this->component_repository->getComponentByTypeAndName(
691  'TestQuestionPool'
692  )->getPluginSlotById('qst')->hasPluginName((string) $questionData['plugin_name'])) {
693  return false;
694  }
695 
696  return $this->component_repository
697  ->getComponentByTypeAndName(
699  'TestQuestionPool'
700  )
701  ->getPluginSlotById(
702  'qst'
703  )
704  ->getPluginByName(
705  (string) $questionData['plugin_name']
706  )->isActive();
707  }
708 
709  public function getDataArrayForQuestionId($questionId)
710  {
711  return $this->questions[$questionId];
712  }
713 
714  public function getQuestionDataArray(): array
715  {
716  return $this->questions;
717  }
718 
719  public function isInList($questionId): bool
720  {
721  return isset($this->questions[$questionId]);
722  }
723 
733  public function getTitle(string $a_comp_id, string $a_item_type, int $a_item_id): string
734  {
735  if ($a_comp_id != 'qpl' || $a_item_type != 'quest' || !$a_item_id) {
736  return '';
737  }
738 
739  if (!isset($this->questions[$a_item_id])) {
740  return '';
741  }
742 
743  return $this->questions[$a_item_id]['title'];
744  }
745 
746  private function checkFilters(): void
747  {
748  if ($this->getAnswerStatusFilter() !== null && !$this->getAnswerStatusActiveId()) {
749  throw new ilTestQuestionPoolException(
750  'No active id given! You cannot use the answer status filter without giving an active id.'
751  );
752  }
753  }
754 }
static getListByQuestionId($questionId)
instantiates a question hint list for the passed question id
$res
Definition: ltiservices.php:69
Readable part of repository interface to ilComponentDataDB.
setAnswerStatusFilter($answerStatusFilter)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isActiveQuestionType(array $questionData)
static completeMissingPluginName($question_type_data)
getTaxItems($parentType, $parentObjId, $taxId, $taxNode)
setExcludeQuestionIdsFilter($excludeQuestionIdsFilter)
hasFeedback(int $question_id)
getTitle(string $a_comp_id, string $a_item_type, int $a_item_id)
Get title of an assigned item.
getDataArrayForQuestionId($questionId)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addTaxonomyFilterNoTaxonomySet(bool $flag)
__construct(private ilDBInterface $db, private ilLanguage $lng, private Refinery $refinery, private ilComponentRepository $component_repository, private ?NotesService $notes_service=null)
setAnswerStatusActiveId($answerStatusActiveId)
setCommentFilter(int $commented=null)
static _existsAndNotEmpty(string $a_parent_type, int $a_id, string $a_lang="-")
checks whether page exists and is not empty (may return true on some empty pages) ...
$lng
setIncludeQuestionIdsFilter($questionIdsFilter)
setParentObjectType($parentObjType)
const PAGE_OBJECT_TYPE_GENERIC_FEEDBACK
type for generic feedback page objects
addTaxonomyFilter($taxId, $taxNodes, $parentObjId, $parentObjType)
getJoinObjectData()
Get if object data table should be joined.
getNumberOfCommentsForQuestion(int $question_id)
hasHints(int $question_id)
setQuestionInstanceTypeFilter($questionInstanceTypeFilter)
const ANSWER_STATUS_FILTER_ALL_NON_CORRECT
answer status filter value domain
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setForcedQuestionIds($forcedQuestionIds)
setJoinObjectData($a_val)
Set if object data table should be joined.
addFieldFilter($fieldName, $fieldValue)
setParentObjIdsFilter($parentObjIdsFilter)
loadTaxonomyAssignmentData($parentObjId, $questionId)
const QUESTION_ANSWER_STATUS_NON_ANSWERED
answer status domain for single questions
const PAGE_OBJECT_TYPE_SPECIFIC_FEEDBACK
type for specific feedback page objects
setQuestionCompletionStatusFilter($questionCompletionStatusFilter)
setAvailableTaxonomyIds($availableTaxonomyIds)
Refinery Factory $refinery