Discussion::class, 'foreign_key' => 'discussion_id', 'assoc_foreign_key' => 'discussion_id' ]; $config['belongs_to']['posting'] = [ 'class_name' => self::class, 'foreign_key' => 'parent_id', 'assoc_foreign_key' => 'posting_id' ]; $config['belongs_to']['user'] = [ 'class_name' => User::class, 'foreign_key' => 'user_id', 'assoc_foreign_key' => 'user_id' ]; $config['has_many']['reactions'] = [ 'class_name' => PostingReaction::class, 'foreign_key' => 'posting_id', 'assoc_foreign_key' => 'posting_id' ]; $config['additional_fields']['author']['get'] = 'getAuthor'; $config['registered_callbacks']['after_create'][] = 'onCreate'; $config['registered_callbacks']['after_delete'][] = 'onDelete'; parent::configure($config); } public function getAuthor(): ?MemberDTO { if ($this->anonymous && $this->user_id !== User::findCurrent()?->user_id) { return MemberDTO::fromArray(); } $user = $this->user; if ($user) { return MemberDTO::fromUser($user, $this->range_id); } return null; } public static function getRecentPosts(array|string $range_ids): array { $user = User::findCurrent(); if (!$user) { return []; } $single = is_string($range_ids); if ($single) { $range_ids = [$range_ids]; } $forum_plugin = \PluginManager::getInstance()->getPlugin(\CoreForum::class); $query = "SELECT forum_topics.range_id, forum_discussions.*, COUNT(DISTINCT forum_postings.posting_id) AS 'posts' FROM forum_topics JOIN forum_discussions USING(topic_id) JOIN forum_postings USING(discussion_id) LEFT JOIN object_user_visits AS ouv ON ouv.object_id = forum_topics.range_id AND ouv.user_id = :user_id AND ouv.plugin_id = :plugin_id WHERE forum_topics.range_id IN (:range_ids) AND forum_postings.user_id != :user_id AND forum_postings.mkdate > IF(ouv.visitdate > :threshold, ouv.visitdate, :threshold) GROUP BY forum_topics.range_id, forum_discussions.discussion_id"; $params = [ ':range_ids' => $range_ids, ':user_id' => $user->id, ':plugin_id' => $forum_plugin->getPluginId(), ':threshold' => object_get_visit_threshold(), ]; $res = \DBManager::get()->fetchAll($query, $params); $by_course = []; foreach ($res as $row) { $by_course[$row['range_id']][] = $row; } return $single ? (array_pop($by_course) ?? []) : $by_course; } public function getOpenGraphURLs(): array { $content = preg_replace("~
(.*)~si", '', $this['content']); return array_filter(OpenGraph::extract($content)->toArray(), fn($og) => $og['is_opengraph']); } public function onCreate(): void { $postingNotification = new PostingNotification($this); $postingNotification->notifySubscribers(); } public function onDelete(): void { PostingReaction::deleteBySQL("posting_id = ?", [$this->posting_id]); } }