ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilAssQuestionList.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Taxonomy/interfaces/interface.ilTaxAssignedItemInfo.php';
5 require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionType.php';
6 
17 {
23  protected $db = null;
24 
30  private $lng = null;
31 
37  private $pluginAdmin = null;
38 
45 
51  private $parentObjId = null;
52 
58  private $parentObjType = 'qpl';
59 
66 
72  private $fieldFilters = array();
73 
79  private $taxFilters = array();
80 
86  private $taxParentIds = array();
87 
93  private $taxParentTypes = array();
94 
100  private $answerStatusActiveId = null;
101 
106 
111  protected $join_obj_data = true;
112 
113 
117  const QUESTION_ANSWER_STATUS_NON_ANSWERED = 'nonAnswered';
118  const QUESTION_ANSWER_STATUS_WRONG_ANSWERED = 'wrongAnswered';
119  const QUESTION_ANSWER_STATUS_CORRECT_ANSWERED = 'correctAnswered';
120 
124  const ANSWER_STATUS_FILTER_ALL_NON_CORRECT = 'allNonCorrect';
127 
133  private $answerStatusFilter = null;
134 
135  const QUESTION_INSTANCE_TYPE_ORIGINALS = 'QST_INSTANCE_TYPE_ORIGINALS';
136  const QUESTION_INSTANCE_TYPE_DUPLICATES = 'QST_INSTANCE_TYPE_DUPLICATES';
137  private $questionInstanceTypeFilter = self::QUESTION_INSTANCE_TYPE_ORIGINALS;
138 
141 
144  const QUESTION_COMPLETION_STATUS_BOTH = 'complete/incomplete';
145  private $questionCompletionStatusFilter = self::QUESTION_COMPLETION_STATUS_BOTH;
146 
152  protected $questions = array();
153 
162  {
163  $this->db = $db;
164  $this->lng = $lng;
165  $this->pluginAdmin = $pluginAdmin;
166  }
167 
168  public function getParentObjId()
169  {
170  return $this->parentObjId;
171  }
172 
173  public function setParentObjId($parentObjId)
174  {
175  $this->parentObjId = $parentObjId;
176  }
177 
178  public function getParentObjectType()
179  {
180  return $this->parentObjType;
181  }
182 
184  {
185  $this->parentObjType = $parentObjType;
186  }
187 
191  public function getParentObjIdsFilter()
192  {
194  }
195 
200  {
201  $this->parentObjIdsFilter = $parentObjIdsFilter;
202  }
203 
205  {
206  $this->questionInstanceTypeFilter = $questionInstanceTypeFilter;
207  }
208 
210  {
212  }
213 
214  public function setIncludeQuestionIdsFilter($questionIdsFilter)
215  {
216  $this->includeQuestionIdsFilter = $questionIdsFilter;
217  }
218 
219  public function getIncludeQuestionIdsFilter()
220  {
222  }
223 
224  public function getExcludeQuestionIdsFilter()
225  {
227  }
228 
230  {
231  $this->excludeQuestionIdsFilter = $excludeQuestionIdsFilter;
232  }
233 
235  {
237  }
238 
240  {
241  $this->questionCompletionStatusFilter = $questionCompletionStatusFilter;
242  }
243 
244  public function addFieldFilter($fieldName, $fieldValue)
245  {
246  $this->fieldFilters[$fieldName] = $fieldValue;
247  }
248 
249  public function addTaxonomyFilter($taxId, $taxNodes, $parentObjId, $parentObjType)
250  {
251  $this->taxFilters[$taxId] = $taxNodes;
252  $this->taxParentIds[$taxId] = $parentObjId;
253  $this->taxParentTypes[$taxId] = $parentObjType;
254  }
255 
257  {
258  $this->availableTaxonomyIds = $availableTaxonomyIds;
259  }
260 
261  public function getAvailableTaxonomyIds()
262  {
264  }
265 
267  {
268  $this->answerStatusActiveId = $answerStatusActiveId;
269  }
270 
271  public function getAnswerStatusActiveId()
272  {
274  }
275 
277  {
278  $this->answerStatusFilter = $answerStatusFilter;
279  }
280 
281  public function getAnswerStatusFilter()
282  {
284  }
285 
291  public function setJoinObjectData($a_val)
292  {
293  $this->join_obj_data = $a_val;
294  }
295 
301  public function getJoinObjectData()
302  {
303  return $this->join_obj_data;
304  }
305 
310  {
311  $this->forcedQuestionIds = $forcedQuestionIds;
312  }
313 
317  public function getForcedQuestionIds()
318  {
320  }
321 
322  private function getParentObjFilterExpression()
323  {
324  if ($this->getParentObjId()) {
325  return 'qpl_questions.obj_fi = ' . $this->db->quote($this->getParentObjId(), 'integer');
326  }
327 
328  if (count($this->getParentObjIdsFilter())) {
329  return $this->db->in('qpl_questions.obj_fi', $this->getParentObjIdsFilter(), false, 'integer');
330  }
331 
332  return null;
333  }
334 
335  private function getFieldFilterExpressions()
336  {
337  $expressions = array();
338 
339  foreach ($this->fieldFilters as $fieldName => $fieldValue) {
340  switch ($fieldName) {
341  case 'title':
342  case 'description':
343  case 'author':
344 
345  $expressions[] = $this->db->like('qpl_questions.' . $fieldName, 'text', "%%$fieldValue%%");
346  break;
347 
348  case 'type':
349 
350  $expressions[] = "qpl_qst_type.type_tag = {$this->db->quote($fieldValue, 'text')}";
351  break;
352 
353  case 'question_id':
354  if ($fieldValue != "" && !is_array($fieldValue)) {
355  $fieldValue = array($fieldValue);
356  }
357  $expressions[] = $this->db->in("qpl_questions.question_id", $fieldValue, false, "integer");
358  break;
359 
360  case 'parent_title':
361  if ($this->join_obj_data) {
362  $expressions[] = $this->db->like('object_data.title', 'text', "%%$fieldValue%%");
363  }
364  break;
365  }
366  }
367 
368  return $expressions;
369  }
370 
371  private function getTaxonomyFilterExpressions()
372  {
373  $expressions = array();
374 
375  require_once 'Services/Taxonomy/classes/class.ilTaxonomyTree.php';
376  require_once 'Services/Taxonomy/classes/class.ilTaxNodeAssignment.php';
377 
378  foreach ($this->taxFilters as $taxId => $taxNodes) {
379  $questionIds = array();
380 
381  $forceBypass = true;
382 
383  foreach ($taxNodes as $taxNode) {
384  $forceBypass = false;
385 
386  $taxItemsByTaxParent = $this->getTaxItems(
387  $this->taxParentTypes[$taxId],
388  $this->taxParentIds[$taxId],
389  $taxId,
390  $taxNode
391  );
392 
393  $taxItemsByParent = $this->getTaxItems(
394  $this->parentObjType,
395  $this->parentObjId,
396  $taxId,
397  $taxNode
398  );
399 
400  $taxItems = array_merge($taxItemsByTaxParent, $taxItemsByParent);
401  foreach ($taxItems as $taxItem) {
402  $questionIds[$taxItem['item_id']] = $taxItem['item_id'];
403  }
404  }
405 
406  if (!$forceBypass) {
407  $expressions[] = $this->db->in('question_id', $questionIds, false, 'integer');
408  }
409  }
410 
411  return $expressions;
412  }
413 
421  protected function getTaxItems($parentType, $parentObjId, $taxId, $taxNode)
422  {
423  $taxTree = new ilTaxonomyTree($taxId);
424 
425  $taxNodeAssignment = new ilTaxNodeAssignment(
426  $parentType,
427  $parentObjId,
428  'quest',
429  $taxId
430  );
431 
432  $subNodes = $taxTree->getSubTreeIds($taxNode);
433  $subNodes[] = $taxNode;
434 
435  return $taxNodeAssignment->getAssignmentsOfNode($subNodes);
436  }
437 
439  {
440  switch ($this->getQuestionInstanceTypeFilter()) {
441  case self::QUESTION_INSTANCE_TYPE_ORIGINALS:
442 
443  return 'qpl_questions.original_id IS NULL';
444 
445  case self::QUESTION_INSTANCE_TYPE_DUPLICATES:
446 
447  return 'qpl_questions.original_id IS NOT NULL';
448  }
449 
450  return null;
451  }
452 
454  {
455  $expressions = array();
456 
457  if (is_array($this->getIncludeQuestionIdsFilter())) {
458  $expressions[] = $this->db->in(
459  'qpl_questions.question_id',
461  false,
462  'integer'
463  );
464  }
465 
466  if (is_array($this->getExcludeQuestionIdsFilter())) {
467  $IN = $this->db->in(
468  'qpl_questions.question_id',
470  true,
471  'integer'
472  );
473 
474  if ($IN == ' 1=2 ') {
475  $IN = ' 1=1 ';
476  } // required for ILIAS < 5.0
477 
478  $expressions[] = $IN;
479  }
480 
481  return $expressions;
482  }
483 
485  {
486  if ($this->parentObjId) {
487  return "qpl_questions.obj_fi = {$this->db->quote($this->parentObjId, 'integer')}";
488  }
489 
490  return null;
491  }
492 
494  {
495  $expressions = array();
496 
497  switch ($this->getAnswerStatusFilter()) {
498  case self::ANSWER_STATUS_FILTER_ALL_NON_CORRECT:
499 
500  $expressions[] = '
501  (tst_test_result.question_fi IS NULL OR tst_test_result.points < qpl_questions.points)
502  ';
503  break;
504 
505  case self::ANSWER_STATUS_FILTER_NON_ANSWERED_ONLY:
506 
507  $expressions[] = 'tst_test_result.question_fi IS NULL';
508  break;
509 
510  case self::ANSWER_STATUS_FILTER_WRONG_ANSWERED_ONLY:
511 
512  $expressions[] = 'tst_test_result.question_fi IS NOT NULL';
513  $expressions[] = 'tst_test_result.points < qpl_questions.points';
514  break;
515  }
516 
517  return $expressions;
518  }
519 
520  private function getTableJoinExpression()
521  {
522  $tableJoin = "
523  INNER JOIN qpl_qst_type
524  ON qpl_qst_type.question_type_id = qpl_questions.question_type_fi
525  ";
526 
527  if ($this->join_obj_data) {
528  $tableJoin .= "
529  INNER JOIN object_data
530  ON object_data.obj_id = qpl_questions.obj_fi
531  ";
532  }
533 
534  if ($this->getAnswerStatusActiveId()) {
535  $tableJoin .= "
536  LEFT JOIN tst_test_result
537  ON tst_test_result.question_fi = qpl_questions.question_id
538  AND tst_test_result.active_fi = {$this->db->quote($this->getAnswerStatusActiveId(), 'integer')}
539  ";
540  }
541 
542  return $tableJoin;
543  }
544 
546  {
547  $CONDITIONS = array();
548 
549  if ($this->getQuestionInstanceTypeFilterExpression() !== null) {
550  $CONDITIONS[] = $this->getQuestionInstanceTypeFilterExpression();
551  }
552 
553  if ($this->getParentObjFilterExpression() !== null) {
554  $CONDITIONS[] = $this->getParentObjFilterExpression();
555  }
556 
557  if ($this->getParentObjectIdFilterExpression() !== null) {
558  $CONDITIONS[] = $this->getParentObjectIdFilterExpression();
559  }
560 
561  $CONDITIONS = array_merge(
562  $CONDITIONS,
564  $this->getFieldFilterExpressions(),
567  );
568 
569  $CONDITIONS = implode(' AND ', $CONDITIONS);
570 
571  return strlen($CONDITIONS) ? 'AND ' . $CONDITIONS : '';
572  }
573 
574  private function getSelectFieldsExpression()
575  {
576  $selectFields = array(
577  'qpl_questions.*',
578  'qpl_qst_type.type_tag',
579  'qpl_qst_type.plugin',
580  'qpl_qst_type.plugin_name',
581  'qpl_questions.points max_points'
582  );
583 
584  if ($this->join_obj_data) {
585  $selectFields[] = 'object_data.title parent_title';
586  }
587 
588  if ($this->getAnswerStatusActiveId()) {
589  $selectFields[] = 'tst_test_result.points reached_points';
590  $selectFields[] = "CASE
591  WHEN tst_test_result.points IS NULL THEN '" . self::QUESTION_ANSWER_STATUS_NON_ANSWERED . "'
592  WHEN tst_test_result.points < qpl_questions.points THEN '" . self::QUESTION_ANSWER_STATUS_WRONG_ANSWERED . "'
593  ELSE '" . self::QUESTION_ANSWER_STATUS_CORRECT_ANSWERED . "'
594  END question_answer_status
595  ";
596  }
597 
598  $selectFields = implode(",\n\t\t\t\t", $selectFields);
599 
600  return "
601  SELECT {$selectFields}
602  ";
603  }
604 
605  private function buildBasicQuery()
606  {
607  return "
608  {$this->getSelectFieldsExpression()}
609 
610  FROM qpl_questions
611 
612  {$this->getTableJoinExpression()}
613 
614  WHERE qpl_questions.tstamp > 0
615  ";
616  }
617 
618  private function buildQuery()
619  {
620  $query = $this->buildBasicQuery() . "
621  {$this->getConditionalFilterExpression()}
622  ";
623 
624  if (count($this->getForcedQuestionIds())) {
625  $query .= "
626  UNION {$this->buildBasicQuery()}
627  AND {$this->db->in('qpl_questions.question_id', $this->getForcedQuestionIds(), false, 'integer')}
628  ";
629  }
630 
631  return $query;
632  }
633 
634  public function load()
635  {
636  $this->checkFilters();
637 
638  $query = $this->buildQuery();
639 
640  #vd($query);
641 
642  $res = $this->db->query($query);
643 
644  //echo $this->db->db->last_query;
645 
646  #vd($this->db->db->last_query);
647 
648  while ($row = $this->db->fetchAssoc($res)) {
650 
651  if (!$this->isActiveQuestionType($row)) {
652  continue;
653  }
654 
655  $row['taxonomies'] = $this->loadTaxonomyAssignmentData($row['obj_fi'], $row['question_id']);
656 
657  $row['ttype'] = $this->lng->txt($row['type_tag']);
658 
659  $this->questions[ $row['question_id'] ] = $row;
660  }
661  }
662 
663  private function loadTaxonomyAssignmentData($parentObjId, $questionId)
664  {
665  $taxAssignmentData = array();
666 
667  foreach ($this->getAvailableTaxonomyIds() as $taxId) {
668  require_once 'Services/Taxonomy/classes/class.ilTaxonomyTree.php';
669  require_once 'Services/Taxonomy/classes/class.ilTaxNodeAssignment.php';
670 
671  $taxTree = new ilTaxonomyTree($taxId);
672 
673  $taxAssignment = new ilTaxNodeAssignment('qpl', $parentObjId, 'quest', $taxId);
674 
675  $assignments = $taxAssignment->getAssignmentsOfItem($questionId);
676 
677  foreach ($assignments as $assData) {
678  if (!isset($taxAssignmentData[ $assData['tax_id'] ])) {
679  $taxAssignmentData[ $assData['tax_id'] ] = array();
680  }
681 
682  $nodeData = $taxTree->getNodeData($assData['node_id']);
683 
684  $assData['node_lft'] = $nodeData['lft'];
685 
686  $taxAssignmentData[ $assData['tax_id'] ][ $assData['node_id'] ] = $assData;
687  }
688  }
689 
690  return $taxAssignmentData;
691  }
692 
693  private function isActiveQuestionType($questionData)
694  {
695  if (!isset($questionData['plugin'])) {
696  return false;
697  }
698 
699  if (!$questionData['plugin']) {
700  return true;
701  }
702 
703  return $this->pluginAdmin->isActive(IL_COMP_MODULE, 'TestQuestionPool', 'qst', $questionData['plugin_name']);
704  }
705 
706  public function getDataArrayForQuestionId($questionId)
707  {
708  return $this->questions[$questionId];
709  }
710 
711  public function getQuestionDataArray()
712  {
713  return $this->questions;
714  }
715 
716  public function isInList($questionId)
717  {
718  return isset($this->questions[$questionId]);
719  }
720 
730  public function getTitle($a_comp_id, $a_item_type, $a_item_id)
731  {
732  if ($a_comp_id != 'qpl' || $a_item_type != 'quest' || !(int) $a_item_id) {
733  return '';
734  }
735 
736  if (!isset($this->questions[$a_item_id])) {
737  return '';
738  }
739 
740  return $this->questions[$a_item_id]['title'];
741  }
742 
743  private function checkFilters()
744  {
745  if (strlen($this->getAnswerStatusFilter()) && !$this->getAnswerStatusActiveId()) {
746  require_once 'Modules/TestQuestionPool/exceptions/class.ilTestQuestionPoolException.php';
747 
748  throw new ilTestQuestionPoolException(
749  'No active id given! You cannot use the answer status filter without giving an active id.'
750  );
751  }
752  }
753 }
setAnswerStatusFilter($answerStatusFilter)
Taxonomy node <-> item assignment.
getTaxItems($parentType, $parentObjId, $taxId, $taxNode)
__construct(ilDBInterface $db, ilLanguage $lng, ilPluginAdmin $pluginAdmin)
Constructor.
setExcludeQuestionIdsFilter($excludeQuestionIdsFilter)
getDataArrayForQuestionId($questionId)
isActiveQuestionType($questionData)
setAnswerStatusActiveId($answerStatusActiveId)
Administration class for plugins.
Interface ilDBInterface.
static completeMissingPluginName($questionTypeData)
foreach($_POST as $key=> $value) $res
setIncludeQuestionIdsFilter($questionIdsFilter)
setParentObjectType($parentObjType)
const IL_COMP_MODULE
addTaxonomyFilter($taxId, $taxNodes, $parentObjId, $parentObjType)
$query
getJoinObjectData()
Get if object data table should be joined.
Create styles array
The data for the language used.
setQuestionInstanceTypeFilter($questionInstanceTypeFilter)
const ANSWER_STATUS_FILTER_ALL_NON_CORRECT
answer status filter value domain
Interface for assigned items of taxonomies.
setForcedQuestionIds($forcedQuestionIds)
setJoinObjectData($a_val)
Set if object data table should be joined.
getTitle($a_comp_id, $a_item_type, $a_item_id)
Get title of an assigned item.
addFieldFilter($fieldName, $fieldValue)
setParentObjIdsFilter($parentObjIdsFilter)
language handling
loadTaxonomyAssignmentData($parentObjId, $questionId)
const QUESTION_ANSWER_STATUS_NON_ANSWERED
answer status domain for single questions
setQuestionCompletionStatusFilter($questionCompletionStatusFilter)
setAvailableTaxonomyIds($availableTaxonomyIds)