Forums

Unfortunately no one can be told what FluxBB is - you have to see it for yourself.

You are not logged in.

#1 2016-03-13 17:47:34

xnit
Member
Registered: 2012-07-27
Posts: 14

AMP Stats plugin with pretty line chart: number of posts & topic views

Really lazy written code but it works. wink

What it will give you?
Top 10 most viewed topics
Top 10 most active topics (number of replies)
Table with number of post in every month since begining of your forum
Pretty line chart showing number of posts writen in last 12 months

Download Chart.min.js:
http://www.chartjs.org/
https://github.com/nnnick/Chart.js
and upload it to plugins directory.

Save as AMP_ForumStats.php in plugins directory:

<?php

/**
 * Plugin by xNit
 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
 */

// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
	exit;
?>
<script src="plugins/Chart.min.js"></script>
<?php
// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('PUN_PLUGIN_LOADED', 1);

generate_admin_menu($plugin);

$top_viewed = $db->query("SELECT `num_replies`, `num_views`, `id`, `subject`
FROM `topics` order by num_views desc limit 10", __FILE__, __LINE__, $db->error());

$top_posted = $db->query("SELECT `num_replies`, `num_views`, `id`, `subject`
FROM `topics` order by num_replies desc limit 10", __FILE__, __LINE__, $db->error());

$query = $db->query("Select `posted` from `posts`", __FILE__, __LINE__, $db->error());

while($result = $query->fetch_assoc()){
	$timestamps[] = $result['posted'];
}
unset($query);

?>
<h2>Most viewed topcis</h2>
<?php 
while($result = $top_viewed->fetch_assoc()){
	echo '<a href="viewtopic.php?id='.$result['id'].'">'; 
	echo htmlspecialchars($result['subject']);
	echo '</a> (' . $result['num_views'] . ' views)<br>';
}
?>

<h2>Topics with most replies</h2>
<?php 
while($result = $top_posted->fetch_assoc()){
	echo '<a href="viewtopic.php?id='.$result['id'].'">'; 
	echo htmlspecialchars($result['subject']);
	echo '</a> (' . $result['num_replies'] . ' replies)<br>';
}
?>

<h2>Number of posts writen since begining:</h2>

<div class="blocktable" style="margin-left: 200px;">
	<div>
		<div class="inbox">
<?php
		
	foreach($timestamps as $timestamp) {
		$y = gmdate('Y', $timestamp);
		$m = gmdate('n', $timestamp);
		if(isset($date[$y][$m])) {
			$date[$y][$m]++;
		}
		else {
			$date[$y][$m] = 1;
		}
		
		$ym = gmdate('Y-m', $timestamp);
		if(isset($date_2D[$ym])) {
			$date_2D[$ym]++;
		}
		else {
			$date_2D[$ym] = 1;
		}
	}

	ksort($date);
	ksort($date_2D);
	$date_2D = array_slice($date_2D, -24, 24, true);
	$labels_2D =  '"' . implode('", "', array_keys($date_2D)) . '"';
	$posts_num_2D =  implode(", ", $date_2D);
	
	foreach($date as &$cur_date) {
		ksort($cur_date);
	}

	echo '<table style="width: auto;"><tr><th></th>';
	for($i = 01; $i < 13; $i++) {
		echo '<th class="tcl">'.$i.'</th>';
	}
	echo '</tr>';

	foreach ($date as $key=>$value) {
		echo '<tr>';
		echo '<td><b>';
		echo $key;
		echo '</b></td>';
		for($i = 1; $i < 13; $i++) {
			if(!isset($value[$i])) {
				$value[$i] = '';
			}
			echo '<td>'.$value[$i].'</td>';
		}
		echo '</td>';
		echo '</tr>';
	}
	echo '</table>';
	
?>
		</div>
	</div>
</div>

<h2>Number of posts writen in last 12 months</h2>
<div style="padding-left: 200px">
		<div style="width: 100%">
			<div>
				<canvas id="canvas" height="400" width="800"></canvas>
			</div>
		</div>
	<script>
		var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
		var lineChartData = {
			labels : [<?=$labels_2D?>],
			datasets : [
				{
					//label: "",
					fillColor : "rgba(220,220,220,0.2)",
					strokeColor : "rgba(220,220,220,1)",
					pointColor : "rgba(220,220,220,1)",
					pointStrokeColor : "#fff",
					pointHighlightFill : "#fff",
					pointHighlightStroke : "rgba(220,220,220,1)",
					data : [<?=$posts_num_2D?>]
				}
			]

		}

	window.onload = function(){
		var ctx = document.getElementById("canvas").getContext("2d");
		window.myLine = new Chart(ctx).Line(lineChartData, {
			responsive: true
		});
	}


	</script>
	</div>

That's all. smile

Last edited by xnit (2016-03-13 17:48:29)

Offline

Board footer

Powered by FluxBB