Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RatingController
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * The Ratings Controller
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
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2024-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     2024-11-24
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration;
21
22use phpMyFAQ\Administration\RatingStatistics;
23use phpMyFAQ\Category;
24use phpMyFAQ\Core\Exception;
25use phpMyFAQ\Enums\PermissionType;
26use phpMyFAQ\Session\Token;
27use phpMyFAQ\Translation;
28use phpMyFAQ\User\CurrentUser;
29use Symfony\Component\HttpFoundation\Request;
30use Symfony\Component\HttpFoundation\Response;
31use Symfony\Component\Routing\Attribute\Route;
32use Twig\Error\LoaderError;
33
34final class RatingController extends AbstractAdministrationController
35{
36    public function __construct(
37        private readonly RatingStatistics $ratingStatistics,
38    ) {
39        parent::__construct();
40    }
41
42    /**
43     * @throws Exception
44     * @throws LoaderError
45     * @throws \Exception
46     */
47    #[Route(path: '/statistics/ratings', name: 'admin.statistics.ratings', methods: ['GET'])]
48    public function index(Request $request): Response
49    {
50        $this->userHasPermission(PermissionType::STATISTICS_VIEWLOGS);
51
52        [$currentUser, $currentGroups] = CurrentUser::getCurrentUserGroupId($this->currentUser);
53
54        $category = new Category($this->configuration, [], false);
55        $category->setUser($currentUser);
56        $category->setGroups($currentGroups);
57
58        $data = $this->ratingStatistics->getAll();
59        $numberOfRatings = is_countable($data) ? count($data) : 0;
60        $currentCategory = 0;
61
62        return $this->render('@admin/statistics/ratings.twig', [
63            ...$this->getHeader($request),
64            ...$this->getFooter(),
65            'csrfToken' => Token::getInstance($this->session)->getTokenString('clear-statistics'),
66            'currentCategory' => $currentCategory,
67            'ratingData' => $data,
68            'numberOfRatings' => $numberOfRatings,
69            'categoryNames' => $category->getCategoryNames(),
70            'adminHeaderRatings' => Translation::get(key: 'ad_rs'),
71            'buttonDeleteAllVotings' => Translation::get(key: 'ad_delete_all_votings'),
72            'green' => Translation::get(key: 'ad_rs_green'),
73            'greenNote' => Translation::get(key: 'ad_rs_ahtf'),
74            'red' => Translation::get(key: 'ad_rs_red'),
75            'redNote' => Translation::get(key: 'ad_rs_altt'),
76            'msgNoRatings' => Translation::get(key: 'ad_rs_no'),
77        ]);
78    }
79}