Moved poll functions to Poll class (#739)

This commit is contained in:
Roman Kelesidis 2023-05-29 22:57:10 +07:00 committed by GitHub
commit d2588855fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 43 deletions

View file

@ -1574,45 +1574,6 @@ function build_topic_pagination($url, $replies, $per_page)
return $pg;
}
//
// Poll
//
function get_poll_data_items_js($topic_id)
{
if (!$topic_id_csv = get_id_csv($topic_id)) {
return is_array($topic_id) ? array() : false;
}
$items = array();
if (!$poll_data = CACHE('bb_poll_data')->get("poll_$topic_id")) {
$poll_data = DB()->fetch_rowset("
SELECT topic_id, vote_id, vote_text, vote_result
FROM " . BB_POLL_VOTES . "
WHERE topic_id IN($topic_id_csv)
ORDER BY topic_id, vote_id
");
CACHE('bb_poll_data')->set("poll_$topic_id", $poll_data);
}
foreach ($poll_data as $row) {
$opt_text_for_js = htmlCHR($row['vote_text']);
$opt_result_for_js = (int)$row['vote_result'];
$items[$row['topic_id']][$row['vote_id']] = array($opt_text_for_js, $opt_result_for_js);
}
foreach ($items as $k => $v) {
$items[$k] = json_encode($v, JSON_THROW_ON_ERROR);
}
return is_array($topic_id) ? $items : $items[$topic_id];
}
function poll_is_active($t_data)
{
global $bb_cfg;
return ($t_data['topic_vote'] == 1 && $t_data['topic_time'] > TIMENOW - $bb_cfg['poll_max_days'] * 86400);
}
function print_confirmation($tpl_vars)
{
global $template, $lang;

View file

@ -60,7 +60,7 @@ switch ($mode) {
if ($t_data['topic_status'] == TOPIC_LOCKED) {
bb_die($lang['TOPIC_LOCKED_SHORT']);
}
if (!poll_is_active($t_data)) {
if (!\TorrentPier\Legacy\Poll::poll_is_active($t_data)) {
bb_die($lang['NEW_POLL_ENDED']);
}
if (!$vote_id) {
@ -128,7 +128,7 @@ switch ($mode) {
bb_die($lang['NEW_POLL_ADDED']);
break;
// редакторование
// редактирование
case 'poll_edit':
if (!$t_data['topic_vote']) {
bb_die($lang['POST_HAS_NO_POLL']);

View file

@ -104,4 +104,53 @@ class Poll
DB()->query("DELETE FROM " . BB_POLL_USERS . " WHERE topic_id = $topic_id");
CACHE('bb_poll_data')->rm("poll_$topic_id");
}
/**
* Get poll items
*
* @param $topic_id
* @return array|false|mixed|string
* @throws \JsonException
*/
public static function get_poll_data_items_js($topic_id)
{
if (!$topic_id_csv = get_id_csv($topic_id)) {
return is_array($topic_id) ? array() : false;
}
$items = array();
if (!$poll_data = CACHE('bb_poll_data')->get("poll_$topic_id")) {
$poll_data = DB()->fetch_rowset("
SELECT topic_id, vote_id, vote_text, vote_result
FROM " . BB_POLL_VOTES . "
WHERE topic_id IN($topic_id_csv)
ORDER BY topic_id, vote_id
");
CACHE('bb_poll_data')->set("poll_$topic_id", $poll_data);
}
foreach ($poll_data as $row) {
$opt_text_for_js = htmlCHR($row['vote_text']);
$opt_result_for_js = (int)$row['vote_result'];
$items[$row['topic_id']][$row['vote_id']] = array($opt_text_for_js, $opt_result_for_js);
}
foreach ($items as $k => $v) {
$items[$k] = json_encode($v, JSON_THROW_ON_ERROR);
}
return is_array($topic_id) ? $items : $items[$topic_id];
}
/**
* Check whether poll is active
*
* @param array $t_data
* @return bool
*/
public static function poll_is_active(array $t_data): bool
{
global $bb_cfg;
return ($t_data['topic_vote'] == 1 && $t_data['topic_time'] > TIMENOW - $bb_cfg['poll_max_days'] * 86400);
}
}

View file

@ -549,7 +549,7 @@ if (!DB()->sql_query($sql)) {
// Does this topic contain a poll?
//
if ($topic_has_poll) {
$poll_votes_js = get_poll_data_items_js($topic_id);
$poll_votes_js = \TorrentPier\Legacy\Poll::get_poll_data_items_js($topic_id);
if (!$poll_votes_js) {
$template->assign_vars(array(
@ -557,7 +557,7 @@ if ($topic_has_poll) {
));
} else {
$template->assign_vars(array(
'SHOW_VOTE_BTN' => poll_is_active($t_data),
'SHOW_VOTE_BTN' => \TorrentPier\Legacy\Poll::poll_is_active($t_data),
'POLL_VOTES_JS' => $poll_votes_js,
));
}