diff --git a/app/controllers/resources/room_request.php b/app/controllers/resources/room_request.php index af5958aca814cb44c28d5db8351fb73eb5490f55..3ab375d7e899a42a3d359e6f63177c4657fd4bfb 100644 --- a/app/controllers/resources/room_request.php +++ b/app/controllers/resources/room_request.php @@ -173,6 +173,13 @@ class Resources_RoomRequestController extends AuthenticatedController } } + // if sorting according to table column was chosen, set the correct + // sort order (ascending vs descending) + if ($sort_value = Request::int('sorting')) { + $this->filter['sorting'] = $sort_value; + $this->filter['sort_order'] = Request::get('sort_order') === 'desc' ? 'desc' : 'asc'; + } + //At this point, only the "marked" filter is definetly set. //If more filters are set, we must show the reset button. $this->show_filter_reset_button = count($this->filter) > 2; @@ -331,7 +338,20 @@ class Resources_RoomRequestController extends AuthenticatedController $sql = 'TRUE '; } - $sql .= " GROUP BY resource_requests.id ORDER BY mkdate ASC"; + $sql .= " GROUP BY resource_requests.id "; + + // if table should be sorted by marking state + switch ($this->filter['sorting']) { + case 1: + $sql .= " ORDER BY resource_requests.marked "; + break; + case 10: + $sql .= " ORDER BY resource_requests.chdate "; + break; + default: + $sql .= " ORDER BY mkdate "; + } + $sql .= $this->filter['sort_order'] === 'desc' ? 'DESC' : 'ASC'; $requests = RoomRequest::findBySql($sql, $sql_params); $result = []; @@ -386,9 +406,122 @@ class Resources_RoomRequestController extends AuthenticatedController return SimpleCollection::createFromArray($requests)->pluck('id'); } } + // sort requests according to display table columns not in the resource request db table + if (!empty($this->filter['sorting']) && + $this->filter['sorting'] != 1 && $this->filter['sorting'] != 10) { + $result = $this->sort_request_table($result, $this->filter['sorting'], $this->filter['sort_order']); + } + return $result; } + /** + * Sorts the resource requests according to columns not belonging to the + * resource requests db table. + * + * @param Array $requests array of ResourceRequest objects + * @param int $sort_variable property according to which the requests should be sorted + * values 1 and 10 are database columns (marked state and chdate) and already dealt with + 2 = lecture number + 3 = lecture name + 4 = dozent name + 5 = room name + 6 = available seats + 7 = requesting person + 8 = type of date + 9 = priority + * @param string $order ascending ('asc') or descending ('desc') order + * + * @return sorted array of resource requests + */ + protected function sort_request_table($requests, int $sort_variable, string $order) + { + usort($requests, + function ($a, $b) use ($sort_variable, $order) { + $rangeObjA = $a->getRangeObject(); + $rangeObjB = $b->getRangeObject(); + + // lecture number + if ($sort_variable === 2) { + if ($order === 'asc') { + return strcmp($rangeObjA->veranstaltungsnummer, $rangeObjB->veranstaltungsnummer); + } else { + return strcmp($rangeObjB->veranstaltungsnummer, $rangeObjA->veranstaltungsnummer); + } + } + // lecture name + if ($sort_variable === 3) { + if ($order === 'asc') { + return strcmp($rangeObjA->name, $rangeObjB->name); + } else { + return strcmp($rangeObjB->name, $rangeObjA->name); + } + } + // dozent name + if ($sort_variable === 4) { + $a_dozent_strings = ''; + foreach ($rangeObjA->getMembersWithStatus('dozent') as $dozent) { + $a_dozent_strings .= $dozent->nachname . ', ' . $dozent->vorname; + } + + $b_dozent_strings = ''; + foreach ($rangeObjB->getMembersWithStatus('dozent') as $dozent) { + $b_dozent_strings .= $dozent->nachname . ', ' . $dozent->vorname; + } + + if ($order === 'asc') { + return strcmp($a_dozent_strings, $b_dozent_strings); + + } else { + return strcmp($b_dozent_strings, $a_dozent_strings); + } + + } + // room name + if ($sort_variable === 5) { + if ($order === 'asc') { + return strcmp($a->resource->name, $b->resource->name); + } else { + return strcmp($b->resource->name, $a->resource->name); + } + } + // available seats + if ($sort_variable === 6) { + return ($order === 'asc' ? (intval($a->getProperty('seats')) - intval($b->getProperty('seats'))) : + (intval($b->getProperty('seats')) - intval($a->getProperty('seats')))); + } + // requesting person + if ($sort_variable === 7) { + if ($order === 'asc') { + return strcmp($a->user->nachname . $a->user->vorname, $b->user->nachname . $b->user->vorname); + } else { + return strcmp($b->user->nachname . $b->user->vorname, $a->user->nachname . $a->user->vorname); + } + } + // type + if ($sort_variable === 8) { + if ($order === 'asc') { + return strcmp($a->getTypeString(true) . $a->getStartDate()->format('YnjHis'), + $b->getTypeString(true) . $b->getStartDate()->format('YnjHis')); + } else { + return strcmp($b->getTypeString(true) . $b->getStartDate()->format('YnjHis'), + $a->getTypeString(true) . $a->getStartDate()->format('YnjHis')); + } + } + // priority + if ($sort_variable === 9) { + if ($order === 'asc') { + return (($a->getPriority()) - $b->getPriority()); + } else { + return (($b->getPriority()) - $a->getPriority()); + } + } + + return 0; + }); + + return $requests; + } protected function getRoomAvailability(Room $room, $time_intervals = []) { @@ -650,7 +783,7 @@ class Resources_RoomRequestController extends AuthenticatedController $this->count_requests = count($requests); $requests = array_slice( $requests, - $this->entries_per_page * ($page - 1), + $this->entries_per_page * ($page), $this->entries_per_page ); @@ -660,6 +793,9 @@ class Resources_RoomRequestController extends AuthenticatedController $this->entries_per_page ); $this->requests = $requests; + $this->page = $page; + $this->sort_order = $this->filter['sort_order']; + $this->sort_var = $this->filter['sorting']; $this->request_status = $this->filter['request_status']; } diff --git a/app/views/resources/room_request/overview.php b/app/views/resources/room_request/overview.php index 7e089af3373010ad8ddb4fd041cfc05a068174d6..30303bf3ab5a5448ea1365a2390e98cc56b13ef2 100644 --- a/app/views/resources/room_request/overview.php +++ b/app/views/resources/room_request/overview.php @@ -1,9 +1,9 @@ <? if ($requests): ?> <form class="default" method="post" action="<?= $controller->link_for('room_request/assign') ?>"> - <table class="default sortable-table request-list" data-sortlist="[[8, 0]]"> + <table class="default request-list tablesorter"> <caption> - <? if ($request_status == 'closed') : ?> + <? if ($request_status === 'closed') : ?> <?= sprintf( ngettext( 'Anfragenliste (%d bearbeitete Anfrage)', @@ -12,7 +12,7 @@ ), $count_requests ) ?> - <? elseif ($request_status == 'denied') : ?> + <? elseif ($request_status === 'denied') : ?> <?= sprintf( ngettext( 'Anfragenliste (%d abgelehnte Anfrage)', @@ -33,23 +33,60 @@ <? endif ?> </caption> <thead> - <tr> - <th data-sort="htmldata"> - <?= Icon::create('radiobutton-checked')->asImg( - [ - 'title' => _('Markierung') - ] - ) ?> + <tr class="sortable"> + <th <? if ($sort_var === 1) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 1 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=1&sort_order=%s', $sortorder)) ?>"> + <?= Icon::create('radiobutton-checked')->asImg( + [ + 'title' => _('Markierung') + ] + ) ?></a> + </th> + <th <? if ($sort_var == 2) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 2 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=2&sort_order=%s', $sortorder)) ?>"> + <?= _('Nr.') ?></a> + </th> + <th <? if ($sort_var === 3) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 3 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=3&sort_order=%s', $sortorder)) ?>"> + <?= _('Name') ?></a> + </th> + <th <? if ($sort_var === 4) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 4 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=4&sort_order=%s', $sortorder)) ?>"> + <?= _('Lehrende Person(en)') ?></a> + </th> + <th <? if ($sort_var === 5) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 5 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=5&sort_order=%s', $sortorder)) ?>"> + <?= _('Raum') ?></a> + </th> + <th <? if ($sort_var === 6) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 6 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=6&sort_order=%s', $sortorder)) ?>"> + <?= _('Plätze') ?></a> + </th> + <th <? if ($sort_var === 7) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 7 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=7&sort_order=%s', $sortorder)) ?>"> + <?= _('Anfragende Person') ?></a> + </th> + <th <? if ($sort_var === 8) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 8 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=8&sort_order=%s', $sortorder)) ?>"> + <?= _('Art') ?></a> + </th> + <th <? if ($sort_var === 9) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 9 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=9&sort_order=%s', $sortorder)) ?>"> + <?= _('Dringlichkeit') ?></a> </th> - <th data-sort="text"><?= _('Nr.') ?></th> - <th data-sort="text"><?= _('Name') ?></th> - <th data-sort="text"><?= _('Lehrende Person(en)') ?></th> - <th data-sort="text"><?= _('Raum') ?></th> - <th data-sort="text"><?= _('Plätze') ?></th> - <th data-sort="text"><?= _('Anfragende Person') ?></th> - <th data-sort="htmldata"><?= _('Art') ?></th> - <th data-sort="htmldata"><?= _('Dringlichkeit') ?></th> - <th data-sort="num"><?= _('letzte Änderung') ?></th> + <th <? if ($sort_var === 10) printf('class="sort%s"', $sort_order) ?>> + <? $sortorder = $sort_var !== 10 ? 'desc' : ($sort_order === 'asc' ? 'desc' : 'asc') ?> + <a href="<?= URLHelper::getLink(sprintf('?sorting=10&sort_order=%s', $sortorder)) ?>"> + <?= _('letzte Änderung') ?></a> <th class="actions"><?= _('Aktionen') ?></th> </tr> </thead> diff --git a/resources/assets/javascripts/bootstrap/resources.js b/resources/assets/javascripts/bootstrap/resources.js index 96c05477fe0ae7b7faa3240556c81e2236afc906..2327611dd206d221d9961234f08ab8acc6615d9f 100644 --- a/resources/assets/javascripts/bootstrap/resources.js +++ b/resources/assets/javascripts/bootstrap/resources.js @@ -922,6 +922,7 @@ STUDIP.ready(function () { } } ); + });