From a63caefe3d39d08de76284991883d2aa3762c65a Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 16 May 2022 04:55:15 +0000 Subject: [PATCH] Polls are now displayed --- src/Model/Item.php | 32 +++++++++++++++++++++++++++++ view/templates/content/question.tpl | 7 +++++++ 2 files changed, 39 insertions(+) create mode 100644 view/templates/content/question.tpl diff --git a/src/Model/Item.php b/src/Model/Item.php index 13c26c245a..d08b938581 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -42,6 +42,7 @@ use Friendica\Util\Map; use Friendica\Util\Network; use Friendica\Util\Proxy; use Friendica\Util\Strings; +use Friendica\Util\Temporal; use Friendica\Worker\Delivery; use LanguageDetection\Language; @@ -95,6 +96,7 @@ class Item 'event-created', 'event-edited', 'event-start', 'event-finish', 'event-summary', 'event-desc', 'event-location', 'event-type', 'event-nofinish', 'event-ignore', 'event-id', + "question-id", "question-multiple", "question-voters", "question-end-time", 'delivery_queue_count', 'delivery_queue_done', 'delivery_queue_failed' ]; @@ -2852,6 +2854,7 @@ class Item $s = self::addVisualAttachments($attachments, $item, $s, false); $s = self::addLinkAttachment($item['uri-id'], $attachments, $body, $s, false, $shared_links); $s = self::addNonVisualAttachments($attachments, $item, $s, false); + $s = self::addQuestions($item, $s); // Map. if (strpos($s, '
') !== false && !empty($item['coord'])) { @@ -3177,6 +3180,35 @@ class Item return $content; } + private static function addQuestions(array $item, string $content) + { + DI::profiler()->startRecording('rendering'); + if (!empty($item['question-id'])) { + $question = [ + 'id' => $item['question-id'], + 'multiple' => $item['question-multiple'], + 'voters' => $item['question-voters'], + 'endtime' => $item['question-end-time'] + ]; + + $options = Post\QuestionOption::getByURIId($item['uri-id']); + foreach ($options as $key => $option) { + $percent = $question['voters'] ? ($option['replies'] / $question['voters'] * 100) : 0; + + $options[$key]['percent'] = $percent; + $options[$key]['vote'] = DI::l10n()->t('%d%s: %s (%d votes)', round($percent, 1), '%', $option['name'], $option['replies']); + } + + $content .= Renderer::replaceMacros(Renderer::getMarkupTemplate('content/question.tpl'), [ + '$question' => $question, + '$options' => $options, + '$summary' => DI::l10n()->t('%d voters. Poll end: %s', $question['voters'], Temporal::getRelativeDate($question['endtime'])), + ]); + } + DI::profiler()->stopRecording(); + return $content; + } + /** * get private link for item * diff --git a/view/templates/content/question.tpl b/view/templates/content/question.tpl new file mode 100644 index 0000000000..767cfda180 --- /dev/null +++ b/view/templates/content/question.tpl @@ -0,0 +1,7 @@ +

+{{$summary}} +

\ No newline at end of file