Skip to content

Commit

Permalink
GH-230 Move new surveys counter to a component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jul 11, 2022
1 parent 6bb13aa commit 78137dd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
1 change: 1 addition & 0 deletions modules/overview/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
include($includePath . './screens/Overview/components/AdminAlerts/AdminAlerts.component.php');
include($includePath . './screens/Overview/components/EmailChangeInfo/EmailChangeInfo.component.php');
include($includePath . './screens/Overview/components/NewMessagesInfo/NewMessagesInfo.component.php');
include($includePath . './screens/Overview/components/NewSurveysInfo/NewSurveysInfo.component.php');
include($includePath . './screens/Overview/components/PlanetsListElement/PlanetsListElement.component.php');
include($includePath . './screens/Overview/components/ResourcesTransport/ResourcesTransport.component.php');
include($includePath . './screens/Overview/components/StatsList/StatsList.component.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace UniEngine\Engine\Modules\Overview\Screens\Overview\Components\NewSurveysInfo;

/**
* @param array $props
* @param number $props['userId']
*/
function render($props) {
global $_Lang;

$userId = $props['userId'];

$getSurveysDataQuery = (
"SELECT " .
"`surveys`.`id`, `votes`.`id` AS `vote_id` " .
"FROM {{table}} AS `surveys` " .
"LEFT JOIN {{prefix}}poll_votes AS `votes` " .
"ON " .
"`votes`.`poll_id` = `surveys`.`id` AND " .
"`votes`.`user_id` = {$userId} " .
"WHERE " .
"`surveys`.`open` = 1 " .
"ORDER BY `surveys`.`time` DESC " .
";"
);
$surveysData = doquery($getSurveysDataQuery, 'polls');

$surveysWithoutVotes = mapQueryResults($surveysData, function ($surveyEntry) {
$hasUserVoted = ($surveyEntry['vote_id'] > 0);

return (
$hasUserVoted ?
0 :
1
);
});
$surveysWithoutVotesCount = array_sum($surveysWithoutVotes);

if ($surveysWithoutVotesCount == 0) {
return [
'componentHTML' => '',
];
}

$localTemplateLoader = createLocalTemplateLoader(__DIR__);

/**
* TODO: Use a better translation system to support more languages.
*/
$content = vsprintf(
$_Lang['PollBox_You_can_vote_in_new_polls'],
(
$surveysWithoutVotesCount > 1 ?
$_Lang['PollBox_More'] :
$_Lang['PollBox_One']
)
);

$tplBodyParams = [
'content' => $content,
];

$componentHTML = parsetemplate(
$localTemplateLoader('body'),
$tplBodyParams
);

return [
'componentHTML' => $componentHTML,
];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<tr>
<th colspan="3">
<a style="color: orange;" href="polls.php">
{content}
</a>
</th>
</tr>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
19 changes: 3 additions & 16 deletions overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,9 @@
])['componentHTML'];

// --- New Polls Information Box -------------------------------------------------------------------------
$SQLResult_GetPolls = doquery("SELECT {{table}}.`id`, `votes`.`id` AS `vote_id` FROM {{table}} LEFT JOIN {{prefix}}poll_votes AS `votes` ON `votes`.`poll_id` = {{table}}.id AND `votes`.`user_id` = {$_User['id']} WHERE {{table}}.`open` = 1 ORDER BY {{table}}.`time` DESC;", 'polls');
if($SQLResult_GetPolls->num_rows > 0)
{
$AvailablePolls = 0;
while($PollData = $SQLResult_GetPolls->fetch_assoc())
{
if($PollData['vote_id'] <= 0)
{
$AvailablePolls += 1;
}
}
if($AvailablePolls > 0)
{
$parse['NewPollsBox'] = '<tr><th colspan="3"><a style="color: orange;" href="polls.php">'.vsprintf($_Lang['PollBox_You_can_vote_in_new_polls'], ($AvailablePolls > 1) ? $_Lang['PollBox_More'] : $_Lang['PollBox_One']).'</a></th></tr>';
}
}
$parse['NewPollsBox'] = Overview\Screens\Overview\Components\NewSurveysInfo\render([
'userId' => $_User['id'],
])['componentHTML'];

// --- Get users activity informations -----------------------------------------------------------
$TodaysStartTimeStamp = mktime(0, 0, 0);
Expand Down

0 comments on commit 78137dd

Please sign in to comment.