Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
66 / 66
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
QuestionHelper
100.00% covered (success)
100.00%
66 / 66
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 generateSmartAnswer
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
2
 getOpenQuestions
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3/**
4 * Questions helper class for phpMyFAQ.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ\Helper
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2019-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2019-11-26
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Helper;
21
22use League\CommonMark\Exception\CommonMarkException;
23use phpMyFAQ\Database;
24use phpMyFAQ\Date;
25use phpMyFAQ\Language\Plurals;
26use phpMyFAQ\Link;
27use phpMyFAQ\Link\Util\TitleSlugifier;
28use phpMyFAQ\Mail;
29use phpMyFAQ\Search\SearchResultSet;
30use phpMyFAQ\Utils;
31use stdClass;
32
33/**
34 * Class QuestionHelper
35 * @package phpMyFAQ\Helper
36 */
37class QuestionHelper extends AbstractHelper
38{
39    /**
40     * @throws CommonMarkException
41     */
42    public function generateSmartAnswer(SearchResultSet $searchResultSet): string
43    {
44        $plurals = new Plurals();
45        $smartAnswer = sprintf('<h5>%s</h5>', $plurals->get(
46            key: 'plmsgSearchAmount',
47            number: $searchResultSet->getNumberOfResults(),
48        ));
49
50        $smartAnswer .= '<ul>';
51        foreach ($searchResultSet->getResultSet() as $result) {
52            $question = (string) ($result->question ?? '');
53            $url = sprintf(
54                '%scontent/%d/%d/%s/%s.html',
55                $this->configuration->getDefaultUrl(),
56                (int) ($result->category_id ?? 0),
57                (int) ($result->id ?? 0),
58                (string) ($result->lang ?? ''),
59                TitleSlugifier::slug($question),
60            );
61            $link = new Link($url, $this->configuration);
62            $link->text = Utils::chopString($question, 15);
63            $link->setTitle($question);
64
65            $faqHelper = new FaqHelper($this->configuration);
66            $smartAnswer .= sprintf(
67                '<li>%s<br><small class="pmf-search-preview">%s...</small></li>',
68                $link->toHtmlAnchor(),
69                $faqHelper->renderAnswerPreview((string) ($result->answer ?? ''), 10),
70            );
71        }
72
73        return $smartAnswer . '</ul>';
74    }
75
76    public function getOpenQuestions(): stdClass
77    {
78        $date = new Date($this->configuration);
79        $mail = new Mail($this->configuration);
80        $db = $this->configuration->getDb();
81        $language = $db->escape($this->configuration->getLanguage()->getLanguage());
82
83        $query = sprintf(
84            "SELECT COUNT(id) AS num FROM %sfaqquestions WHERE lang = '%s' AND is_visible != 'Y'",
85            Database::getTablePrefix(),
86            $language,
87        );
88
89        $result = $db->query($query);
90        $row = $db->fetchObject($result);
91
92        $openQuestions = new stdClass();
93        $openQuestions->numberInvisibleQuestions = $row instanceof \stdClass ? (int) $row->num : 0;
94
95        $query = sprintf(
96            "SELECT * FROM %sfaqquestions WHERE lang = '%s' AND is_visible = 'Y' ORDER BY created ASC",
97            Database::getTablePrefix(),
98            $language,
99        );
100
101        $result = $db->query($query);
102
103        if ($result !== false && $this->configuration->getDb()->numRows($result) > 0) {
104            $openQuestions->numberQuestions = $this->configuration->getDb()->numRows($result);
105            $questions = [];
106            while (true) {
107                $row = $this->configuration->getDb()->fetchObject($result);
108                if (!$row instanceof \stdClass) {
109                    break;
110                }
111
112                $question = new stdClass();
113                $question->id = (int) $row->id;
114                $question->lang = (string) $row->lang;
115                $question->date = $date->format(Date::createIsoDate((string) $row->created));
116                $question->email = $mail->safeEmail((string) $row->email);
117                $question->userName = (string) $row->username;
118                $question->categoryId = (int) $row->category_id;
119                $question->categoryName = $this->getCategory()->getCategoryName((int) $row->category_id);
120                $question->question = (string) $row->question;
121                $question->answerId = (int) $row->answer_id;
122                $question->slug = TitleSlugifier::slug($question->question);
123
124                $questions[] = $question;
125            }
126
127            $openQuestions->questions = $questions;
128        }
129
130        return $openQuestions;
131    }
132}