Skip to content

Commit c1cd418

Browse files
matheuszychthojou
authored andcommitted
Corrects sorting of questions by question type
1 parent 7209643 commit c1cd418

3 files changed

Lines changed: 56 additions & 24 deletions

File tree

components/ILIAS/Test/src/Questions/Presentation/QuestionsBrowserTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getColumns(): array
8181
'description' => $column_factory->text(
8282
$this->lng->txt('description')
8383
)->withIsOptional(true, true),
84-
'type_tag' => $column_factory->text(
84+
'question_type' => $column_factory->text(
8585
$this->lng->txt('tst_question_type')
8686
)->withIsOptional(false, true),
8787
'points' => $column_factory->number(
@@ -153,7 +153,7 @@ public function getRows(
153153
foreach ($this->loadRecords($filter_data ?? [], $order, $range) as $record) {
154154
$question_id = $record['question_id'];
155155

156-
$record['type_tag'] = $this->lng->txt($record['type_tag']);
156+
$record['question_type'] = $record['question_type'];
157157
$record['complete'] = (bool) $record['complete'];
158158
$record['lifecycle'] = \ilAssQuestionLifecycle::getInstance($record['lifecycle'])->getTranslation($this->lng) ?? '';
159159

components/ILIAS/TestQuestionPool/classes/class.ilAssQuestionList.php

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

components/ILIAS/TestQuestionPool/src/Questions/Presentation/QuestionTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getColumns(): array
157157
return [
158158
'title' => $f->link($this->lng->txt('title')),
159159
'description' => $f->text($this->lng->txt('description'))->withIsOptional(true, true),
160-
'ttype' => $f->text($this->lng->txt('question_type'))->withIsOptional(true, true),
160+
'question_type' => $f->text($this->lng->txt('question_type'))->withIsOptional(true, true),
161161
'points' => $f->number($this->lng->txt('points'))->withDecimals(2)->withIsOptional(true, true),
162162
'author' => $f->text($this->lng->txt('author'))->withIsOptional(true, true),
163163
'lifecycle' => $f->text($this->lng->txt('qst_lifecycle'))->withIsOptional(true, true),

0 commit comments

Comments
 (0)