diff --git a/controllers/issues.php b/controllers/issues.php index e9d394f0eaa64826e15363e7e45a9a86a2c3dddb..1ae60c45e80ba07cf5611832b1f2e6ed52d0bf29 100644 --- a/controllers/issues.php +++ b/controllers/issues.php @@ -4,8 +4,16 @@ final class IssuesController extends TracToGitlab\EventController const SECRET_ISSUE_OPEN = 'N]<d5V/6tn/sYNMy'; const LABEL_MAPPING = [ - 'TIC' => ['1927f2b86d6b185aa6c6697810ad42f1', 'a65dd807699b7b3b3d182b674042b70e', 'TIC #%1$u: %2$s'], - 'StEP' => ['1927f2b86d6b185aa6c6697810ad42f1', '93c84607e687030b249a69cd30ca7bd9', 'StEP00%1$u: %2$s'], + 'TIC' => [ + 'course_id' => '1927f2b86d6b185aa6c6697810ad42f1', + 'subject' => 'TIC #%1$u: %2$s', + 'body' => "%1\$s\n\n----\nZum [gitlab-Issue #%2\$u](%3\$s)", + ], + 'StEP' => [ + 'course_id' => '1927f2b86d6b185aa6c6697810ad42f1', + 'subject' => 'StEP00%1$u: %2$s', + 'body' => "Zum [gitlab-Issue #%2\$u](%3\$s)", + ], ]; public function create_action() @@ -33,14 +41,14 @@ final class IssuesController extends TracToGitlab\EventController continue; } $title = sprintf( - $definition[2], + $definition['subject'], $this->getFromPayload('object_attributes', 'iid'), $this->getFromPayload('object_attributes', 'title') ); $body = Studip\Markup::markAsHtml( $this->parsedown->text( sprintf( - "%s\n\n----\nZum [gitlab-Issue #%u](%s)", + $definition['body'], $this->getFromPayload('object_attributes', 'description'), $this->getFromPayload('object_attributes', 'iid'), $this->getFromPayload('object_attributes', 'url') @@ -49,18 +57,21 @@ final class IssuesController extends TracToGitlab\EventController ); $url = $this->createForumEntry( + $label, $user, $title, $body, - $definition[0], - $definition[1] + $definition['course_id'] ); - $this->gitlab->issues()->addNote( - $this->getFromPayload('project', 'id'), - $this->getFromPayload('object_attributes', 'iid'), - "[Zum Forenbeitrag auf dem Entwicklungsserver]({$url})" - ); + if ($url) { + $this->gitlab->issues()->addNote( + $this->getFromPayload('project', 'id'), + $this->getFromPayload('object_attributes', 'iid'), + "[Zum Forenbeitrag auf dem Entwicklungsserver]({$url})" + ); + } + } } } @@ -79,29 +90,51 @@ final class IssuesController extends TracToGitlab\EventController } /** - * @return string Absolute URL to forum posting + * @return ?string Absolute URL to forum posting */ - private function createForumEntry(\User $user, string $subject, string $body, string $course_id, string $parent_id) + private function createForumEntry(string $type, \User $user, string $subject, string $body, string $course_id) { - require_once "{$GLOBALS['STUDIP_BASE_PATH']}/public/plugins_packages/core/Forum/models/ForumEntry.php"; + if (!PluginEngine::getPlugin('CoreForum')) { + return null; + } + + $query = "SELECT category_id + FROM forum_categories + WHERE seminar_id = ? + AND entry_name LIKE CONCAT('Aktive ', ?, '%')"; + $category_id = DBManager::get()->fetchColumn($query, [$course_id, $type]); - $id = md5(uniqid('forum-entry', true)); + $topic_id = md5(uniqid('CoreForum')); + ForumEntry::insert([ + 'topic_id' => $topic_id, + 'seminar_id' => $course_id, + 'user_id' => $user->id, + 'name' => $subject, + 'content' => $subject, + 'author' => $user->getFullName(), + 'author_host' => $_SERVER['REMOTE_ADDR'] + ], $course_id); + + if ($category_id) { + ForumCat::addArea($category_id, $topic_id); + } - \ForumEntry::insert([ - 'topic_id' => $id, + $topic_id2 = md5(uniqid('CoreForum')); + ForumEntry::insert([ + 'topic_id' => $topic_id2, 'seminar_id' => $course_id, 'user_id' => $user->id, 'name' => $subject, 'content' => $body, 'author' => $user->getFullName(), - 'author_host' => '', - ], $parent_id); + 'author_host' => $_SERVER['REMOTE_ADDR'] + ], $topic_id); $old_base = \URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']); $url = \PluginEngine::getURL( 'CoreForum', ['cid' => $course_id], - "index/index/{$id}#{$id}" + "index/index/{$topic_id2}#{$topic_id2}" ); \URLHelper::setBaseURL($old_base);