Skip to content
Snippets Groups Projects
Commit 7b526a48 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

swap priorities instead of increasing/decreasing the priorities, fixes #3031

Closes #3031

Merge request studip/studip!2035
parent f7c6d461
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,7 @@ class Course_TopicsController extends AuthenticatedController ...@@ -31,6 +31,7 @@ class Course_TopicsController extends AuthenticatedController
public function index_action() public function index_action()
{ {
$this->topics = CourseTopic::findBySeminar_id(Context::getId()); $this->topics = CourseTopic::findBySeminar_id(Context::getId());
$this->topic_links = $this->createLinksForTopics($this->topics);
$this->cancelled_dates_locked = LockRules::Check(Context::getId(), 'cancelled_dates'); $this->cancelled_dates_locked = LockRules::Check(Context::getId(), 'cancelled_dates');
} }
...@@ -107,26 +108,25 @@ class Course_TopicsController extends AuthenticatedController ...@@ -107,26 +108,25 @@ class Course_TopicsController extends AuthenticatedController
$this->redirect($this->indexURL(['open' => $topic->id])); $this->redirect($this->indexURL(['open' => $topic->id]));
} }
public function move_up_action(CourseTopic $topic) public function swap_action(CourseTopic $a, CourseTopic $b)
{ {
if (!Request::isPost()) { if (!Request::isPost()) {
throw new MethodNotAllowedException(); throw new MethodNotAllowedException();
} }
$topic->increasePriority(); if (
$a->seminar_id !== Context::getId()
$this->redirect($this->indexURL(['open' => $topic->id])); || $b->seminar_id !== Context::getId()
) {
throw new Exception(_('Eines oder mehrere Themen gehören nicht zur ausgewählten Veranstaltung.'));
} }
public function move_down_action(CourseTopic $topic) [$a->priority, $b->priority] = [$b->priority, $a->priority];
{
if (!Request::isPost()) {
throw new MethodNotAllowedException();
}
$topic->decreasePriority(); $a->store();
$b->store();
$this->redirect($this->indexURL(['open' => $topic->id])); $this->redirect($this->indexURL(['open' => $a->id]));
} }
public function allow_public_action() public function allow_public_action()
...@@ -263,4 +263,30 @@ class Course_TopicsController extends AuthenticatedController ...@@ -263,4 +263,30 @@ class Course_TopicsController extends AuthenticatedController
); );
} }
} }
private function createLinksForTopics(array $topics): array
{
$links = array_combine(
array_column($topics, 'id'),
array_fill(0, count($topics), ['previous' => null, 'next' => null])
);
$last = null;
foreach ($topics as $topic) {
if ($last !== null) {
$links[$topic->id]['previous'] = $last;
}
$last = $topic;
}
$next = null;
foreach (array_reverse($topics) as $topic) {
if ($next !== null) {
$links[$topic->id]['next'] = $next;
}
$next = $topic;
}
return $links;
}
} }
<?php
/**
* @var CourseTopic[] $topics
* @var Course_TopicsController $controller
* @var array<array{next: ?CourseTopic, previous: ?CourseTopic}> $topic_links
*/
?>
<? if (count($topics) > 0) : ?> <? if (count($topics) > 0) : ?>
<table class="default withdetails"> <table class="default withdetails">
<colgroup> <colgroup>
...@@ -102,13 +109,13 @@ ...@@ -102,13 +109,13 @@
<? endif ?> <? endif ?>
<span class="button-group"> <span class="button-group">
<? if ($key > 0) : ?> <? if ($topic_links[$topic->id]['previous']) : ?>
<form action="<?= $controller->move_up($topic) ?>" method="post" style="display: inline;"> <form action="<?= $controller->swap($topic, $topic_links[$topic->id]['previous']) ?>" method="post" style="display: inline;">
<?= Studip\Button::createMoveUp(_('nach oben verschieben')) ?> <?= Studip\Button::createMoveUp(_('nach oben verschieben')) ?>
</form> </form>
<? endif ?> <? endif ?>
<? if ($key < count($topics) - 1) : ?> <? if ($topic_links[$topic->id]['next']) : ?>
<form action="<?=$controller->move_down($topic)?>" method="post" style="display: inline;"> <form action="<?= $controller->swap($topic, $topic_links[$topic->id]['next']) ?>" method="post" style="display: inline;">
<?= Studip\Button::createMoveDown(_('nach unten verschieben')) ?> <?= Studip\Button::createMoveDown(_('nach unten verschieben')) ?>
</form> </form>
<? endif ?> <? endif ?>
......
...@@ -197,6 +197,7 @@ class CourseTopic extends SimpleORMap ...@@ -197,6 +197,7 @@ class CourseTopic extends SimpleORMap
* mean higher priority. * mean higher priority.
* *
* @return boolean * @return boolean
* @todo Deprecated, remove for Stud.IP 6.0
*/ */
public function increasePriority() public function increasePriority()
{ {
...@@ -227,6 +228,8 @@ class CourseTopic extends SimpleORMap ...@@ -227,6 +228,8 @@ class CourseTopic extends SimpleORMap
* Decreases the priority of this topic. Meaning the topic will be sorted further down. * Decreases the priority of this topic. Meaning the topic will be sorted further down.
* Be aware that this actually increases the priority property since higher numbers * Be aware that this actually increases the priority property since higher numbers
* mean lower priority. * mean lower priority.
*
* @todo Deprecated, remove for Stud.IP 6.0
*/ */
public function decreasePriority() public function decreasePriority()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment