I am looking at a very simple PHP code that uses our billing system's API to generate a graph. As of right now, it shows average ticket rating for every staff member. I would like to change it to instead just show the average ticket rating per month for the current year.
There is a 'date' field in the same table that has values formatted for each ticket like '2010-08-23 00:48:22'.
How would I alter this code to show average ticket ratings per month for the given year?
<?php
if (!defined("WHMCS"))
die("This file cannot be accessed directly");
$description = "This graph shows average support ratings by staff member";
if ($statsonly) { return false; }
$chartdata = array();
$query = "SELECT DATE_FORMAT(date, '%m-%Y') AS theMonth, AVG(rating) AS avgRating ";
$query .= "FROM tblticketreplies WHERE admin != '' AND rating !='0' ";
$query .= "GROUP BY DATE_FORMAT(date, '%m-%Y') ";
$query .= "ORDER BY avgRating ASC";
$result = mysql_query($query);
//$query = "SELECT admin, AVG(rating) AS avgrating FROM `tblticketreplies` WHERE admin != '' AND rating!='0' GROUP BY admin ORDER BY avgrating ASC";
//$result = mysql_query($query);
while ($data = mysql_fetch_array($result))
{
$chartdata[$data[0]] = round($data[1],2);
}
$graph=new WHMCSGraph(650,400);
$graph->addData($chartdata);
$graph->setTitle("Ave开发者_开发技巧rage Support Ticket Ratings by Month");
$graph->setGradient("lime", "green");
$graph->setDataValues(true);
$graph->setXValuesHorizontal(true);
?>
You'll need to begin by altering your query to return an average rating per month, instead of an average rating per 'admin' as you have now. I don't know the name of the column in your database that has dates, but lets say it's named "MyDateColumn".
To get the monthly average, you'll need a query similar to:
$query = 'SELECT DATE_FORMAT(MyDateColumn, "%m-%Y") AS theMonth, AVG(rating) AS avgRating ';
$query .= 'FROM tblticketreplies ';
$query .= 'GROUP BY DATE_FORMAT(MyDateColumn, "%m-%Y") ';
$query .= 'ORDER BY avgRating ASC';
The DATE_FORMAT function is what's providing the magic. It's formatting 'MyDateColumn' as MM-YYYY (e.g. '06-2011'). Use the same format in the GROUP BY to complete the query.
Change the setTitle
portion of your code to label the new data coming from your query:
$graph->setTitle("Average Support Ticket Ratings per Month");
Also, notice I removd the WHERE clause from your query. Before it was excluding 'ticketreplies' with an empty 'admin' or empty rating. You should evaluate adding this back into the query depending on what data you want to include in your chart.
Hope that's helpful.
~~ Edit:
Addressing the comments below - the query above sorts by avgRating
, so your chart will always go from low to high rating value. To have it go from earliest month to most recent month, update the ORDER BY
. Use ORDER BY DATE_FORMAT(MyDateColumn, "%Y-%m")
. The %Y-%m
is reversed here to ensure the sorting works correctly. The year is the most significant element here, so it goes first. The lesser significant month element goes last.
And lastly, to restrict your graph to only the current user, add a WHERE
clause between the FROM
clause and the GROUP BY
clause. Use: WHERE YEAR(MyDateColumn) = YEAR(CURDATE())
This instructs MySQL to only consider elements from your table when the year of the date in MyDateColumn
is the same year as the year of the current date.
Do give the MySQL date-time functions a review. You'll thank yourself later. =)
精彩评论