Skip to content

Commit

Permalink
refactor: extracted the getAll() method into RatingData class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 29, 2024
1 parent dbedb62 commit bf9d1b5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 111 deletions.
8 changes: 5 additions & 3 deletions phpmyfaq/admin/stat.ratings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @since 2003-02-24
*/

use phpMyFAQ\Administration\RatingData;
use phpMyFAQ\Category;
use phpMyFAQ\Configuration;
use phpMyFAQ\Enums\PermissionType;
Expand Down Expand Up @@ -45,6 +46,7 @@
$category = new Category($faqConfig, [], false);
$category->setUser($currentAdminUser);
$category->setGroups($currentAdminGroups);
$ratingData = new RatingData($faqConfig);
$ratings = new Rating($faqConfig);

if ($csrfToken && !Token::getInstance()->verifyToken('clear-statistics', $csrfToken)) {
Expand All @@ -61,8 +63,8 @@
}
}

$ratingData = $ratings->getAll();
$numberOfRatings = is_countable($ratingData) ? count($ratingData) : 0;
$data = $ratingData->getAll();
$numberOfRatings = is_countable($data) ? count($data) : 0;
$currentCategory = 0;

$templateVars = [
Expand All @@ -74,7 +76,7 @@
'msgDeleteAllVotings' => 'Statistics successfully deleted.',
'msgDeleteAllVotingsError' => 'Statistics not deleted.',
'currentCategory' => $currentCategory,
'ratingData' => $ratingData,
'ratingData' => $data,
'numberOfRatings' => $numberOfRatings,
'categoryNames' => $category->categoryName,
'green' => Translation::get('ad_rs_green'),
Expand Down
138 changes: 138 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Administration/RatingData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/**
* The Rating data class for the administration.
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-03-29
*/

namespace phpMyFAQ\Administration;

use phpMyFAQ\Configuration;
use phpMyFAQ\Database;
use phpMyFAQ\Link;
use phpMyFAQ\Strings;

readonly class RatingData
{
public function __construct(private Configuration $configuration)
{
}

/**
* Returns all ratings of FAQ records.
*
* @return array
*/
public function getAll(): array
{
$ratings = [];

$query = match (Database::getType()) {
'sqlsrv' => sprintf(
'
SELECT
fd.id AS id,
fd.lang AS lang,
fcr.category_id AS category_id,
CAST(fd.thema as char(2000)) AS question,
(fv.vote / fv.usr) AS num,
fv.usr AS usr
FROM
%sfaqvoting fv,
%sfaqdata fd
LEFT JOIN
%sfaqcategoryrelations fcr
ON
fd.id = fcr.record_id
AND
fd.lang = fcr.record_lang
WHERE
fd.id = fv.artikel
GROUP BY
fd.id,
fd.lang,
fd.active,
fcr.category_id,
CAST(fd.thema as char(2000)),
fv.vote,
fv.usr
ORDER BY
fcr.category_id',
Database::getTablePrefix(),
Database::getTablePrefix(),
Database::getTablePrefix()
),
default => sprintf(
'
SELECT
fd.id AS id,
fd.lang AS lang,
fcr.category_id AS category_id,
fd.thema AS question,
(fv.vote / fv.usr) AS num,
fv.usr AS usr
FROM
%sfaqvoting fv,
%sfaqdata fd
LEFT JOIN
%sfaqcategoryrelations fcr
ON
fd.id = fcr.record_id
AND
fd.lang = fcr.record_lang
WHERE
fd.id = fv.artikel
GROUP BY
fd.id,
fd.lang,
fd.active,
fcr.category_id,
fd.thema,
fv.vote,
fv.usr
ORDER BY
fcr.category_id',
Database::getTablePrefix(),
Database::getTablePrefix(),
Database::getTablePrefix()
),
};

$result = $this->configuration->getDb()->query($query);
while ($row = $this->configuration->getDb()->fetchObject($result)) {
$question = Strings::htmlspecialchars(trim((string) $row->question));
$url = sprintf(
'%sindex.php?action=faq&cat=%d&id=%d&artlang=%s',
$this->configuration->getDefaultUrl(),
$row->category_id,
$row->id,
$row->lang
);

$link = new Link($url, $this->configuration);
$link->itemTitle = $question;

$ratings[] = [
'id' => $row->id,
'lang' => $row->lang,
'category_id' => $row->category_id,
'question' => $question,
'url' => $link->toString(),
'number' => $row->num,
'user' => $row->usr
];
}

return $ratings;
}
}
108 changes: 0 additions & 108 deletions phpmyfaq/src/phpMyFAQ/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,114 +40,6 @@ public function __construct(private Configuration $configuration)
$this->plr = new Plurals();
}

/**
* Returns all ratings of FAQ records.
*
* @return array
*/
public function getAll(): array
{
$ratings = [];

$query = match (Database::getType()) {
'mssql', 'sqlsrv' => sprintf(
'
SELECT
fd.id AS id,
fd.lang AS lang,
fcr.category_id AS category_id,
CAST(fd.thema as char(2000)) AS question,
(fv.vote / fv.usr) AS num,
fv.usr AS usr
FROM
%sfaqvoting fv,
%sfaqdata fd
LEFT JOIN
%sfaqcategoryrelations fcr
ON
fd.id = fcr.record_id
AND
fd.lang = fcr.record_lang
WHERE
fd.id = fv.artikel
GROUP BY
fd.id,
fd.lang,
fd.active,
fcr.category_id,
CAST(fd.thema as char(2000)),
fv.vote,
fv.usr
ORDER BY
fcr.category_id',
Database::getTablePrefix(),
Database::getTablePrefix(),
Database::getTablePrefix()
),
default => sprintf(
'
SELECT
fd.id AS id,
fd.lang AS lang,
fcr.category_id AS category_id,
fd.thema AS question,
(fv.vote / fv.usr) AS num,
fv.usr AS usr
FROM
%sfaqvoting fv,
%sfaqdata fd
LEFT JOIN
%sfaqcategoryrelations fcr
ON
fd.id = fcr.record_id
AND
fd.lang = fcr.record_lang
WHERE
fd.id = fv.artikel
GROUP BY
fd.id,
fd.lang,
fd.active,
fcr.category_id,
fd.thema,
fv.vote,
fv.usr
ORDER BY
fcr.category_id',
Database::getTablePrefix(),
Database::getTablePrefix(),
Database::getTablePrefix()
),
};

$result = $this->configuration->getDb()->query($query);
while ($row = $this->configuration->getDb()->fetchObject($result)) {
$question = Strings::htmlspecialchars(trim((string) $row->question));
$url = sprintf(
'%sindex.php?action=faq&cat=%d&id=%d&artlang=%s',
$this->configuration->getDefaultUrl(),
$row->category_id,
$row->id,
$row->lang
);

$link = new Link($url, $this->configuration);
$link->itemTitle = $question;

$ratings[] = [
'id' => $row->id,
'lang' => $row->lang,
'category_id' => $row->category_id,
'question' => $question,
'url' => $link->toString(),
'number' => $row->num,
'user' => $row->usr
];
}

return $ratings;
}

/**
* Calculates the rating of the user voting.
*/
Expand Down

0 comments on commit bf9d1b5

Please sign in to comment.