diff --git a/modules/statistics.php b/modules/statistics.php
index 695ba543..a54b1e52 100644
--- a/modules/statistics.php
+++ b/modules/statistics.php
@@ -47,28 +47,31 @@ class statistics extends module_base
*/
public $custom_acp_tpl = '';
- /** @var \phpbb\cache */
+ /** @var \phpbb\cache\service */
protected $cache;
/** @var \phpbb\config\config */
protected $config;
- /** @var \phpbb\db\driver */
+ /** @var \phpbb\db\driver\driver_interface */
protected $db;
- /** @var \phpbb\template */
+ /** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\user */
protected $user;
+ /** @var double Board days */
+ protected $board_days;
+
/**
* Construct a search object
*
- * @param \phpbb\cache $cache phpBB cache system
+ * @param \phpbb\cache\service $cache phpBB cache system
* @param \phpbb\config\config $config phpBB config
- * @param \phpbb\db\driver $db phpBB database system
- * @param \phpbb\template $template phpBB template
+ * @param \phpbb\db\driver\driver_interface $db phpBB database system
+ * @param \phpbb\template\template $template phpBB template
* @param \phpbb\user $user phpBB user object
*/
public function __construct($cache, $config, $db, $template, $user)
@@ -85,62 +88,36 @@ class statistics extends module_base
*/
public function get_template_side($module_id)
{
- // Set some stats, get posts count from forums data if we... hum... retrieve all forums data
- $total_posts = $this->config['num_posts'];
- $total_topics = $this->config['num_topics'];
- $total_users = $this->config['num_users'];
- $total_files = $this->config['num_files'];
+ // Get totals language strings
+ $l_total_user_s = $this->get_totals_language($this->config['num_users'], 'TOTAL_USERS');
+ $l_total_post_s = $this->get_totals_language($this->config['num_posts'], 'TOTAL_POSTS', 'TOTAL_POSTS_COUNT');
+ $l_total_topic_s = $this->get_totals_language($this->config['num_topics'], 'TOTAL_TOPICS');
- $l_total_user_s = ($total_users == 0) ? sprintf($this->user->lang['TOTAL_USERS_ZERO'], $total_users) : sprintf($this->user->lang['TOTAL_USERS'][2], $total_users);
- $l_total_post_s = ($total_posts == 0) ? sprintf($this->user->lang['TOTAL_POSTS_ZERO'], $total_posts) : sprintf($this->user->lang['TOTAL_POSTS_COUNT'][2], $total_posts);
- $l_total_topic_s = ($total_topics == 0) ? sprintf($this->user->lang['TOTAL_TOPICS_ZERO'], $total_topics) : sprintf($this->user->lang['TOTAL_TOPICS'][2], $total_topics);
+ // Average statistics
+ $this->board_days = (double) ((time() - $this->config['board_startdate']) / 86400);
- // avarage stat
- $board_days = (time() - $this->config['board_startdate']) / 86400;
+ $topics_per_day = round($this->config['num_topics'] / $this->board_days, 0);
+ $posts_per_day = round($this->config['num_posts'] / $this->board_days, 0);
+ $users_per_day = round($this->config['num_users'] / $this->board_days, 0);
+ $topics_per_user = round($this->config['num_topics'] / $this->config['num_users'], 0);
+ $posts_per_user = round($this->config['num_posts'] / $this->config['num_users'], 0);
+ $posts_per_topic = ($this->config['num_topics']) ? round($this->config['num_posts'] / $this->config['num_topics'], 0) : 0;
- $topics_per_day = ($total_topics) ? round($total_topics / $board_days, 0) : 0;
- $posts_per_day = ($total_posts) ? round($total_posts / $board_days, 0) : 0;
- $users_per_day = round($total_users / $board_days, 0);
- $topics_per_user = ($total_topics) ? round($total_topics / $total_users, 0) : 0;
- $posts_per_user = ($total_posts) ? round($total_posts / $total_users, 0) : 0;
- $posts_per_topic = ($total_topics) ? round($total_posts / $total_topics, 0) : 0;
+ // Mitigate incorrect averages on first day
+ $topics_per_day = $this->get_first_day_average($topics_per_day, $this->config['num_topics']);
+ $posts_per_day = $this->get_first_day_average($posts_per_day, $this->config['num_posts']);
+ $users_per_day = $this->get_first_day_average($users_per_day, $this->config['num_users']);
+ $topics_per_user = $this->get_first_day_average($topics_per_user, $this->config['num_topics']);
+ $posts_per_user = $this->get_first_day_average($posts_per_user, $this->config['num_topics']);
+ $posts_per_topic = $this->get_first_day_average($posts_per_topic, $this->config['num_posts']);
- if ($topics_per_day > $total_topics)
- {
- $topics_per_day = $total_topics;
- }
-
- if ($posts_per_day > $total_posts)
- {
- $posts_per_day = $total_posts;
- }
-
- if ($users_per_day > $total_users)
- {
- $users_per_day = $total_users;
- }
-
- if ($topics_per_user > $total_topics)
- {
- $topics_per_user = $total_topics;
- }
-
- if ($posts_per_user > $total_posts)
- {
- $posts_per_user = $total_posts;
- }
-
- if ($posts_per_topic > $total_posts)
- {
- $posts_per_topic = $total_posts;
- }
-
- $l_topics_per_day_s = ($total_topics == 0) ? 'TOPICS_PER_DAY_ZERO' : 'TOPICS_PER_DAY_OTHER';
- $l_posts_per_day_s = ($total_posts == 0) ? 'POSTS_PER_DAY_ZERO' : 'POSTS_PER_DAY_OTHER';
- $l_users_per_day_s = ($total_users == 0) ? 'USERS_PER_DAY_ZERO' : 'USERS_PER_DAY_OTHER';
- $l_topics_per_user_s = ($total_topics == 0) ? 'TOPICS_PER_USER_ZERO' : 'TOPICS_PER_USER_OTHER';
- $l_posts_per_user_s = ($total_posts == 0) ? 'POSTS_PER_USER_ZERO' : 'POSTS_PER_USER_OTHER';
- $l_posts_per_topic_s = ($total_posts == 0) ? 'POSTS_PER_TOPIC_ZERO' : 'POSTS_PER_TOPIC_OTHER';
+ // Get language variables for averages
+ $l_topics_per_day_s = $this->get_average_language($this->config['num_topics'], 'TOPICS_PER_DAY');
+ $l_posts_per_day_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_DAY');
+ $l_users_per_day_s = $this->get_average_language($this->config['num_users'], 'USERS_PER_DAY');
+ $l_topics_per_user_s = $this->get_average_language($this->config['num_topics'], 'TOPICS_PER_USER');
+ $l_posts_per_user_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_USER');
+ $l_posts_per_topic_s = $this->get_average_language($this->config['num_posts'], 'POSTS_PER_TOPIC');
$topics_count = $this->get_topics_count();
@@ -152,7 +129,7 @@ class statistics extends module_base
'B3_NEWEST_USER' => sprintf($this->user->lang['NEWEST_USER'], get_username_string('full', $this->config['newest_user_id'], $this->config['newest_username'], $this->config['newest_user_colour'])),
'B3_ANNOUNCE_COUNT' => $topics_count[POST_ANNOUNCE],
'B3_STICKY_COUNT' => $topics_count[POST_STICKY],
- 'B3_TOTAL_ATTACH' => ($this->config['allow_attachments']) ? $total_files : 0,
+ 'B3_TOTAL_ATTACH' => ($this->config['allow_attachments']) ? $this->config['num_files'] : 0,
// average stat
'B3_TOPICS_PER_DAY' => sprintf($this->user->lang[$l_topics_per_day_s], $topics_per_day),
@@ -221,4 +198,51 @@ class statistics extends module_base
return $return_ary;
}
+
+ /**
+ * Get correct average per day on first day.
+ * The per day average will be higher than the total amount. This will
+ * result in incorrect statistics.
+ *
+ * @param int $average Average per day
+ * @param int $total Total value
+ *
+ * @return int Corrected average per day, if correction was necessary
+ */
+ protected function get_first_day_average($average, $total)
+ {
+ return ($average > $total) ? $total : $average;
+ }
+
+ /**
+ * Get language string for totals
+ *
+ * @param int $total The total value
+ * @param string $language_variable Language variable of the total
+ * @param string $count_language_variable Optional language variable for count
+ *
+ * @return string Language string for total
+ */
+ protected function get_totals_language($total, $language_variable, $count_language_variable = '')
+ {
+ if ($count_language_variable === '')
+ {
+ $count_language_variable = $language_variable;
+ }
+
+ return ($total == 0) ? sprintf($this->user->lang[$language_variable . '_ZERO'], $total) : sprintf($this->user->lang[$count_language_variable][2], $total);
+ }
+
+ /**
+ * Get language variable for averages
+ *
+ * @param int $total The total value
+ * @param string $language_variable Language variable of the total
+ *
+ * @return string Language string for total
+ */
+ protected function get_average_language($total, $language_variable)
+ {
+ return ($total == 0) ? $language_variable . '_ZERO' : $language_variable . '_OTHER';
+ }
}
diff --git a/portal/fetch_posts.php b/portal/fetch_posts.php
index 59d48903..1ec2ad01 100644
--- a/portal/fetch_posts.php
+++ b/portal/fetch_posts.php
@@ -134,8 +134,8 @@ class fetch_posts
public function get_posts($forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false)
{
$posts = $update_count = array();
- $post_time = ($time == 0) ? '' : 'AND t.topic_time > ' . (time() - $time * 86400);
- $forum_from = (strpos($forum_from, ',') !== false) ? explode(',', $forum_from) : (($forum_from != '') ? array($forum_from) : array());
+ $post_time = $this->get_setting_based_data($time == 0, '', 'AND t.topic_time > ' . (time() - $time * 86400));
+ $forum_from = $this->get_setting_based_data(strpos($forum_from, ',') !== false, explode(',', $forum_from), $this->get_setting_based_data($forum_from != '', array($forum_from), array()));
$topic_icons = array(0);
$have_icons = 0;
$this->global_id = 0;
@@ -166,40 +166,15 @@ class fetch_posts
while ($row = $this->db->sql_fetchrow($result))
{
- $attachments = array();
- if (($this->auth->acl_get('u_download') && ($this->auth->acl_get('f_download', $row['forum_id']) || $row['forum_id'] == 0)) && $this->config['allow_attachments'] && $row['post_id'] && $row['post_attachment'])
- {
- // Pull attachment data
- $sql2 = 'SELECT *
- FROM ' . ATTACHMENTS_TABLE . '
- WHERE post_msg_id = '. $row['post_id'] .'
- AND in_message = 0
- ORDER BY filetime DESC';
- $result2 = $this->db->sql_query($sql2);
-
- while ($row2 = $this->db->sql_fetchrow($result2))
- {
- $attachments[] = $row2;
- }
- $this->db->sql_freeresult($result2);
- }
+ // Get attachments
+ $attachments = $this->get_post_attachments($row);
$posts[$i]['bbcode_uid'] = $row['bbcode_uid'];
- $len_check = $row['post_text'];
- $maxlen = $text_length;
- if (($text_length != 0) && (strlen($len_check) > $text_length))
- {
- $message = str_replace(array("\n", "\r"), array('
', "\n"), $row['post_text']);
- $message = get_sub_taged_string($message, $row['bbcode_uid'], $maxlen);
- $posts[$i]['striped'] = true;
- }
- else
- {
- $message = str_replace("\n", '
', $row['post_text']);
- }
+ // Format message
+ $message = $this->format_message($row, $text_length, $posts[$i]['striped']);
- $row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) + (($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + (($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
+ $row['bbcode_options'] = $this->get_setting_based_data($row['enable_bbcode'], OPTION_FLAG_BBCODE, 0) + $this->get_setting_based_data($row['enable_smilies'], OPTION_FLAG_SMILIES, 0) + $this->get_setting_based_data($row['enable_magic_url'], OPTION_FLAG_LINKS, 0);
$message = generate_text_for_display($message, $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
if (!empty($attachments))
@@ -207,20 +182,18 @@ class fetch_posts
parse_attachments($row['forum_id'], $message, $attachments, $update_count);
}
- if ($this->global_id < 1)
- {
- $this->global_id = $row['forum_id'];
- }
+ // Get proper global ID
+ $this->global_id = $this->get_setting_based_data($this->global_id, $this->global_id, $row['forum_id']);
$topic_icons[] = $row['enable_icons'];
- $have_icons = ($row['icon_id'] > 0) ? 1 : $have_icons;
+ $have_icons = $this->get_setting_based_data($row['icon_id'], 1, $have_icons);
$posts[$i] = array_merge($posts[$i], array(
'post_text' => ap_validate($message),
'topic_id' => $row['topic_id'],
'topic_last_post_id' => $row['topic_last_post_id'],
'topic_type' => $row['topic_type'],
- 'topic_posted' => (isset($row['topic_posted']) && $row['topic_posted']) ? true : false,
+ 'topic_posted' => $this->get_setting_based_data(isset($row['topic_posted']) && $row['topic_posted'], true, false),
'icon_id' => $row['icon_id'],
'topic_status' => $row['topic_status'],
'forum_id' => $row['forum_id'],
@@ -235,18 +208,18 @@ class fetch_posts
'user_id' => $row['user_id'],
'user_type' => $row['user_type'],
'user_colour' => $row['user_colour'],
- 'poll' => ($row['poll_title']) ? true : false,
- 'attachment' => ($row['topic_attachment']) ? true : false,
+ 'poll' => $this->get_setting_based_data($row['poll_title'], true, false),
+ 'attachment' => $this->get_setting_based_data($row['topic_attachment'], true, false),
'topic_views' => $row['topic_views'],
'forum_name' => $row['forum_name'],
- 'attachments' => (!empty($attachments)) ? $attachments : array(),
+ 'attachments' => $this->get_setting_based_data($attachments, $attachments, array()),
));
$posts['global_id'] = $this->global_id;
++$i;
}
$this->db->sql_freeresult($result);
- $posts['topic_icons'] = ((max($topic_icons) > 0) && $have_icons) ? true : false;
+ $posts['topic_icons'] = $this->get_setting_based_data(max($topic_icons) > 0 && $have_icons, true, false);
$posts['topic_count'] = $i;
if ($this->global_id < 1)
@@ -398,9 +371,9 @@ class fetch_posts
{
$this->topic_type = 't.topic_type = ' . POST_NORMAL;
$this->where_string = (strlen($this->where_string) > 0) ? 'AND (' . trim(substr($this->where_string, 0, -4)) . ')' : '';
- $this->user_link = ($this->config['board3_news_style_' . $this->module_id]) ? 't.topic_poster = u.user_id' : (($this->config['board3_news_show_last_' . $this->module_id]) ? 't.topic_last_poster_id = u.user_id' : 't.topic_poster = u.user_id' ) ;
- $this->post_link = ($this->config['board3_news_style_' . $this->module_id]) ? 't.topic_first_post_id = p.post_id' : (($this->config['board3_news_show_last_' . $this->module_id]) ? 't.topic_last_post_id = p.post_id' : 't.topic_first_post_id = p.post_id' ) ;
- $this->topic_order = ($this->config['board3_news_show_last_' . $this->module_id]) ? 't.topic_last_post_time DESC' : 't.topic_time DESC' ;
+ $this->user_link = $this->get_setting_based_data($this->config['board3_news_style_' . $this->module_id], 't.topic_poster = u.user_id', $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_poster_id = u.user_id', 't.topic_poster = u.user_id')) ;
+ $this->post_link = $this->get_setting_based_data($this->config['board3_news_style_' . $this->module_id], 't.topic_first_post_id = p.post_id', $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_post_id = p.post_id', 't.topic_first_post_id = p.post_id'));
+ $this->topic_order = $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_post_time DESC', 't.topic_time DESC');
}
/**
@@ -545,4 +518,104 @@ class fetch_posts
{
$this->where_string = '';
}
+
+ /**
+ * Get valid data based on setting
+ *
+ * @param mixed $setting Setting to check
+ * @param mixed $setting_true Data if setting is 'on' (not empty)
+ * @param mixed $setting_false Data if setting is 'off' (empty or 0)
+ *
+ * @return mixed Valid data based on setting
+ */
+ protected function get_setting_based_data($setting, $setting_true, $setting_false)
+ {
+ return (!empty($setting)) ? $setting_true : $setting_false;
+ }
+
+ /**
+ * Assert that all supplied arguments evaluate to true
+ *
+ * @return bool True if all evaluate to true, false if not
+ */
+ protected function assert_all_true()
+ {
+ $args = func_get_args();
+ $return = true;
+
+ foreach ($args as $argument)
+ {
+ $return = $return && $argument;
+ }
+
+ return $return;
+ }
+
+ /**
+ * Get attachments of posts
+ *
+ * @param array $row Database row of post
+ *
+ * @return array Attachment data
+ */
+ protected function get_post_attachments($row)
+ {
+ $attachments = array();
+
+ if ($this->user_can_download($row['forum_id']) && $this->assert_all_true($this->config['allow_attachments'], $row['post_id'], $row['post_attachment']))
+ {
+ // Pull attachment data
+ $sql = 'SELECT *
+ FROM ' . ATTACHMENTS_TABLE . '
+ WHERE post_msg_id = '. (int) $row['post_id'] .'
+ AND in_message = 0
+ ORDER BY filetime DESC';
+ $result = $this->db->sql_query($sql);
+
+ while ($row = $this->db->sql_fetchrow($result))
+ {
+ $attachments[] = $row;
+ }
+ $this->db->sql_freeresult($result);
+ }
+
+ return $attachments;
+ }
+
+ /**
+ * Check if user can download a file in this forum
+ *
+ * @param int $forum_id Forum ID to check
+ *
+ * @return bool True if user can download, false if not
+ */
+ protected function user_can_download($forum_id)
+ {
+ return $this->auth->acl_get('u_download') && ($this->auth->acl_get('f_download', $forum_id) || $forum_id == 0);
+ }
+
+ /**
+ * Format message for display
+ *
+ * @param array $row Database row
+ * @param int $text_length Length of text
+ * @param bool $posts_striped Whether post is striped
+ *
+ * @return mixed|string
+ */
+ protected function format_message($row, $text_length, &$posts_striped)
+ {
+ if (($text_length !== 0) && (strlen($row['post_text']) > $text_length))
+ {
+ $message = str_replace(array("\n", "\r"), array('
', "\n"), $row['post_text']);
+ $message = get_sub_taged_string($message, $row['bbcode_uid'], $text_length);
+ $posts_striped = true;
+ }
+ else
+ {
+ $message = str_replace("\n", '
', $row['post_text']);
+ }
+
+ return $message;
+ }
}