SimplePortal
Customization => Blocks and Modifications => Block Requests => Topic started by: stanyy on July 21, 2013, 11:46:56 AM
-
Very much like the TopTopics block, but instead of measuring total replies, it should only measure from last x hours.
My first thought was that this could be achieved by tweaking the toptopics block a little. I posted a topic (http://simpleportal.net/index.php?topic=12712) in feature requests and Blue says this can be achieved with a custom block. If anyone can help, I would be grateful.
-
BUMP
-
Hope the following works for you. Use it in a Custom PHP block.
global $smcFunc, $scripturl, $txt;
$limit = 5;
$hours = 2;
$request = $smcFunc['db_query']('', '
SELECT id_topic, subject, COUNT(id_msg) AS posts
FROM {db_prefix}messages
WHERE poster_time > {int:start_time}
GROUP BY id_topic
ORDER BY posts DESC
LIMIT {int:limit}',
array(
'start_time' => forum_time(false, mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N") * 3600 * $hours)),
'limit' => $limit,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$row['subject'] = str_replace('Re: ', '', $row['subject']);
$topics[] = array(
'id' => $row['id_topic'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
'replies' => comma_format($row['posts']),
);
}
$smcFunc['db_free_result']($request);
if (empty($topics))
{
echo $txt['error_sp_no_topics_found'];
return;
}
else
$topics[count($topics) - 1]['is_last'] = true;
echo '
<ul class="sp_list">';
foreach ($topics as $topic)
echo '
<li class="sp_list_top">', sp_embed_image('topic'), ' ', $topic['link'], '</li>
<li class="sp_list_indent', empty($topic['is_last']) ? ' sp_list_bottom' : '', ' smalltext">Replies in last ', $hours, ' hours: ', $topic['replies'], '</li>';
echo '
</ul>';
-
Nice code, but can you make it so threads from restricted boards are excluded from the list for guests/regular members without the right permissions?
-
Opps. Sorry about that. Please use this one:
global $smcFunc, $scripturl, $txt;
$limit = 5;
$hours = 2;
$request = $smcFunc['db_query']('', '
SELECT m.id_topic, m.subject, COUNT(m.id_msg) AS posts
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
WHERE m.poster_time > {int:start_time}
AND {query_see_board}
GROUP BY m.id_topic
ORDER BY posts DESC
LIMIT {int:limit}',
array(
'start_time' => forum_time(false, mktime(0, 0, 0, date("n"), date("j"), date("Y")) - (date("N") * 3600 * $hours)),
'limit' => $limit,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$row['subject'] = str_replace('Re: ', '', $row['subject']);
$topics[] = array(
'id' => $row['id_topic'],
'subject' => $row['subject'],
'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
'replies' => comma_format($row['posts']),
);
}
$smcFunc['db_free_result']($request);
if (empty($topics))
{
echo $txt['error_sp_no_topics_found'];
return;
}
else
$topics[count($topics) - 1]['is_last'] = true;
echo '
<ul class="sp_list">';
foreach ($topics as $topic)
echo '
<li class="sp_list_top">', sp_embed_image('topic'), ' ', $topic['link'], '</li>
<li class="sp_list_indent', empty($topic['is_last']) ? ' sp_list_bottom' : '', ' smalltext">Replies in last ', $hours, ' hours: ', $topic['replies'], '</li>';
echo '
</ul>';
-
Brilliant, thanks
-
Glad that it worked for you. ;)
-
Hi,
I added this code to my site on a block and got really strange results. I choose 24 hours and got much more than that, when i added something like 1 hour instead i could easily see it didn't work as intended. Posts written earlier was included. Not all post but more then an hour, perhaps 5-6 or so (but i'm just guessing).
For me, post marked "Today" would be enough, or ofcourse 24 hours. But i cannot get that to work correctly.
-
Could it be possible this (mentioned above) has something to do with how date is determined in the code, using forum_time and then a conversion on top of that? :)
-
I've noticed strange behaviour of this, like reply count wrong and time wrong. Does anyone have a fix? Or new code that does the same thing?
-
I am not sure. I can suggest some very simplistic code to use for the start_time:
'start_time' => forum_time(false, time() - 3600 * $hours),
I have not tested this, and am not certain it will work; please feel free to test it and see if it works for you on your forum.