diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-09-05 09:35:57 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-09-05 09:35:57 +0000 |
| commit | 7b526a48e19d0a62ea204081eb1768a2d1b7e48c (patch) | |
| tree | aaec9dba3c6f8f59f8d6e2d55ada9ebf6f00eb8e /app/controllers/course/topics.php | |
| parent | f7c6d4610f082beb72c9093a7192cfb8024f988c (diff) | |
swap priorities instead of increasing/decreasing the priorities, fixes #3031
Closes #3031
Merge request studip/studip!2035
Diffstat (limited to 'app/controllers/course/topics.php')
| -rw-r--r-- | app/controllers/course/topics.php | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/app/controllers/course/topics.php b/app/controllers/course/topics.php index d90955f..dc75276 100644 --- a/app/controllers/course/topics.php +++ b/app/controllers/course/topics.php @@ -31,6 +31,7 @@ class Course_TopicsController extends AuthenticatedController public function index_action() { $this->topics = CourseTopic::findBySeminar_id(Context::getId()); + $this->topic_links = $this->createLinksForTopics($this->topics); $this->cancelled_dates_locked = LockRules::Check(Context::getId(), 'cancelled_dates'); } @@ -107,26 +108,25 @@ class Course_TopicsController extends AuthenticatedController $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()) { throw new MethodNotAllowedException(); } - $topic->increasePriority(); - - $this->redirect($this->indexURL(['open' => $topic->id])); - } - - public function move_down_action(CourseTopic $topic) - { - if (!Request::isPost()) { - throw new MethodNotAllowedException(); + if ( + $a->seminar_id !== Context::getId() + || $b->seminar_id !== Context::getId() + ) { + throw new Exception(_('Eines oder mehrere Themen gehören nicht zur ausgewählten Veranstaltung.')); } - $topic->decreasePriority(); + [$a->priority, $b->priority] = [$b->priority, $a->priority]; - $this->redirect($this->indexURL(['open' => $topic->id])); + $a->store(); + $b->store(); + + $this->redirect($this->indexURL(['open' => $a->id])); } public function allow_public_action() @@ -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; + } } |
