SimplePortal

Customization => Blocks and Modifications => Block Requests => Topic started by: stanyy on July 21, 2013, 11:46:56 AM

Title: Block For Most Replies in X Hours
Post 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.
Title: Re: Block For Most Replies in X Hours
Post by: stanyy on August 01, 2013, 04:16:50 AM
BUMP
Title: Re: Block For Most Replies in X Hours
Post by: [SiNaN] on August 01, 2013, 06:14:45 AM
Hope the following works for you. Use it in a Custom PHP block.

Code: [Select]
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>';
Title: Re: Block For Most Replies in X Hours
Post by: Vaughn on August 01, 2013, 06:34:47 AM
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?
Title: Re: Block For Most Replies in X Hours
Post by: [SiNaN] on August 01, 2013, 01:04:19 PM
Opps. Sorry about that. Please use this one:

Code: [Select]
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>';
Title: Re: Block For Most Replies in X Hours
Post by: Vaughn on August 02, 2013, 06:55:45 AM
Brilliant, thanks
Title: Re: Block For Most Replies in X Hours
Post by: [SiNaN] on August 14, 2013, 11:31:06 AM
Glad that it worked for you. ;)
Title: Re: Block For Most Replies in X Hours
Post by: Tarista on October 20, 2013, 05:39:08 AM
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.
Title: Re: Block For Most Replies in X Hours
Post by: Tarista on October 30, 2013, 10:23:57 AM
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? :)
Title: Re: Block For Most Replies in X Hours
Post by: Vaughn on May 18, 2014, 03:34:11 AM
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?
Title: Re: Block For Most Replies in X Hours
Post by: AngelinaBelle on May 19, 2014, 12:20:17 PM
I am not sure.  I can suggest some very simplistic code to use for the start_time:
Code: [Select]
'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.
SimplePortal 2.3.8 © 2008-2024, SimplePortal