@@ -72,6 +72,8 @@ class ilAssQuestionList implements ilTaxAssignedItemInfo
7272 protected array $ questions = [];
7373
7474 private ?Order $ order = null ;
75+ private ?string $ order_field = null ;
76+ private ?string $ order_direction = null ;
7577 private ?Range $ range = null ;
7678
7779 public function __construct (
@@ -86,6 +88,9 @@ public function __construct(
8688 public function setOrder (?Order $ order = null ): void
8789 {
8890 $ this ->order = $ order ;
91+ $ order_state = $ this ->getOrderFieldAndDirection ($ order );
92+ $ this ->order_field = $ order_state ['order_field ' ];
93+ $ this ->order_direction = $ order_state ['order_direction ' ];
8994 }
9095
9196 public function setRange (?Range $ range = null ): void
@@ -368,10 +373,7 @@ private function getAnswerStatusFilterExpressions(): array
368373
369374 private function getTableJoinExpression (): string
370375 {
371- $ tableJoin = '
372- INNER JOIN qpl_qst_type
373- ON qpl_qst_type.question_type_id = qpl_questions.question_type_fi
374- ' ;
376+ $ tableJoin = "INNER JOIN qpl_qst_type ON qpl_qst_type.question_type_id = qpl_questions.question_type_fi " ;
375377
376378 if ($ this ->join_obj_data ) {
377379 $ tableJoin .= '
@@ -428,7 +430,7 @@ private function getSelectFieldsExpression(): string
428430 {
429431 $ select_fields = [
430432 'qpl_questions.* ' ,
431- 'qpl_qst_type.type_tag ' ,
433+ 'qpl_qst_type.type_tag AS question_type ' ,
432434 'qpl_qst_type.plugin ' ,
433435 'qpl_qst_type.plugin_name ' ,
434436 'qpl_questions.points max_points '
@@ -512,12 +514,36 @@ private function getHavingFilterExpression(): string
512514 }
513515
514516 private function buildOrderQueryExpression (): string
517+ {
518+ return $ this ->order_field === null || $ this ->order_direction === null || $ this ->order_field === 'question_type '
519+ ? ''
520+ : " ORDER BY ` $ this ->order_field ` $ this ->order_direction " ;
521+ }
522+
523+ private function buildLimitQueryExpression (): string
515524 {
516525 $ order = $ this ->order ;
517- if ($ order === null ) {
526+ if ($ order instanceof Order && $ this -> order_field === ' question_type ' ) {
518527 return '' ;
519528 }
520529
530+ $ range = $ this ->range ;
531+ if ($ range === null ) {
532+ return '' ;
533+ }
534+
535+ $ limit = max ($ range ->getLength (), 0 );
536+ $ offset = max ($ range ->getStart (), 0 );
537+
538+ return " LIMIT $ limit OFFSET $ offset " ;
539+ }
540+
541+ private function getOrderFieldAndDirection (?Order $ order ): ?array
542+ {
543+ if ($ order === null ) {
544+ return ['order_field ' => null , 'order_direction ' => null ];
545+ }
546+
521547 [$ order_field , $ order_direction ] = $ order ->join (
522548 '' ,
523549 static fn (string $ index , string $ key , string $ value ): array => [$ key , $ value ]
@@ -528,20 +554,7 @@ private function buildOrderQueryExpression(): string
528554 $ order_direction = Order::ASC ;
529555 }
530556
531- return " ORDER BY ` $ order_field` $ order_direction " ;
532- }
533-
534- private function buildLimitQueryExpression (): string
535- {
536- $ range = $ this ->range ;
537- if ($ range === null ) {
538- return '' ;
539- }
540-
541- $ limit = max ($ range ->getLength (), 0 );
542- $ offset = max ($ range ->getStart (), 0 );
543-
544- return " LIMIT $ limit OFFSET $ offset " ;
557+ return ['order_field ' => $ order_field , 'order_direction ' => $ order_direction ];
545558 }
546559
547560 private function buildQuery (): string
@@ -573,7 +586,7 @@ public function load(): void
573586 $ row ['description ' ] = $ tags_trafo ->transform ($ row ['description ' ] ?? '' );
574587 $ row ['author ' ] = $ tags_trafo ->transform ($ row ['author ' ]);
575588 $ row ['taxonomies ' ] = $ this ->loadTaxonomyAssignmentData ($ row ['obj_fi ' ], $ row ['question_id ' ]);
576- $ row ['ttype ' ] = $ this ->lng ->txt ($ row ['type_tag ' ]);
589+ $ row ['question_type ' ] = $ this ->lng ->txt ($ row ['question_type ' ]);
577590 $ row ['feedback ' ] = $ row ['feedback ' ] === 1 ;
578591 $ row ['comments ' ] = $ this ->getNumberOfCommentsForQuestion ($ row ['question_id ' ]);
579592
@@ -586,6 +599,25 @@ public function load(): void
586599
587600 $ this ->questions [$ row ['question_id ' ]] = $ row ;
588601 }
602+
603+ if ($ this ->order_field === 'question_type ' ) {
604+ $ this ->questions = $ this ->postLimit ($ this ->postOrder ($ this ->questions ));
605+ }
606+ }
607+
608+ private function postOrder (array $ questions ): array
609+ {
610+ if ($ this ->order_field === 'question_type ' ) {
611+ usort ($ questions , fn (array $ a , array $ b ): int => strcmp ($ a [$ this ->order_field ], $ b [$ this ->order_field ]));
612+ }
613+ return $ this ->order_direction === Order::DESC ? array_reverse ($ questions ) : $ questions ;
614+ }
615+
616+ private function postLimit (array $ questions ): array
617+ {
618+ return $ this ->range instanceof Range
619+ ? array_slice ($ questions , $ this ->range ->getStart (), $ this ->range ->getLength ())
620+ : $ questions ;
589621 }
590622
591623 public function getTotalRowCount (
0 commit comments