Custom Reports

2012-04-25 14:43:35Tags: custom-reports-how-to

Hello Support,

 

I am attempting to create a custom report using the instructions and examples in the online manual. I have tried to cobble a report together, but am having issues. When I save the code below

 

<?php

$r = new $reports[$clabel]["FileRequests"]();
$r->DisplayReport();

define('_FileRequests_PER_DAY', 'FileRequestsPerDay');
define('_CUSTOM_REPORTS', 'Custom Reports');

$reports["_FileRequests_PER_DAY"] = Array(
    "ClassName" => "FileRequestsPerDay",
    "Category" => "_VISITOR_DETAILS",
    "icon" => "images/icons/32x32/unknown.png",
    "Options" => "daterangeField,displaymode,trafficsource",
    "Order" => 3
);

class FileRequestsPerDay extends Report {
    function Settings() {
        $this->DefaultDisplay = "table";
        $this->DisplayModes = "table,linechart,barchart";
        $this->showfields = _DATE.'
                ,'._VISITORS.'
                ,'._PAGEVIEWS.'
                ,'._PAGES_PER_USER;
        $this->help = _FileRequests_PER_DAY_DESC;
    }

    function DefineQuery() {
        if (!empty($this->trafficsource)) {
            $query = "SELECT FROM_UNIXTIME(timestamp, '%d-%b-%Y %a')
                        AS days,COUNT(DISTINCT visitorid) AS visitors, COUNT(*) AS
                        pages,(COUNT(*) / COUNT(DISTINCT visitorid)) AS ppu FROM
                        {$this->profile->tablename} WHERE timestamp >= {$this->from}
                        AND timestamp <= {$this->to} AND crawl = 0 GROUP BY days
                        ORDER BY timestamp";
        } else {
            // Use the summary table.
            $query = "SELECT params
                        ppu FROM {$this->profile->urlparams} WHERE
                        timestamp >= {$this->from} AND timestamp <= {$this->to} and `params` LIKE '%.pdf%'
                        ORDER BY timestamp";
        }
        return $query;
    }
}
?>

as "FileRequests.php" in the "Reports" directory, and then refresh the Logaholic main page (profiles.php), I get a blank page.

 

Ideas?

 

Are there more examples of how to craft a Custom Report?

 

Thanks!

 

John Ridgway

Answer:

Hi John,

I changed a few things in your code.

At first I noticed that u used:

$r = new $reports[$clabel]["FileRequests"]();
$r->DisplayReport();

The code above is used to create the report you want but you do not need to add it to the class file because this will be done in the master report class. So you can leave that out.

<?php

define('_FileRequests_PER_DAY', 'FileRequestsPerDay');
@define('_CUSTOM_REPORTS', 'Custom Reports');
define('_FileRequests_PER_DAY_DESC', 'DESCRIPTION HERE!');

$reports["_FileRequests_PER_DAY"] = Array(
    "ClassName" => "FileRequestsPerDay",
    "Category" => "
_CUSTOM_REPORTS",
    "icon" => "images/icons/32x32/unknown.png",
    "Options" => "daterangeField,displaymode,trafficsource",
    "Order" => 3
);

class FileRequestsPerDay extends Report {
    function Settings() {
        $this->DefaultDisplay = "table";
        $this->DisplayModes = "table,linechart,barchart";
        $this->showfields = _DATE.'
                ,'._VISITORS.'
                ,'._PAGEVIEWS.'
                ,'._PAGES_PER_USER;
        $this->help = _FileRequests_PER_DAY_DESC;
    }

    function DefineQuery() {
        if (!empty($this->trafficsource)) {
            $query = "SELECT FROM_UNIXTIME(timestamp, '%d-%b-%Y %a')
                        AS days,COUNT(DISTINCT visitorid) AS visitors, COUNT(*) AS
                        pages,(COUNT(*) / COUNT(DISTINCT visitorid)) AS ppu FROM
                        {$this->profile->tablename} WHERE timestamp >= {$this->from}
                        AND timestamp <= {$this->to} AND crawl = 0 GROUP BY days
                        ORDER BY timestamp";
                        
                        
        } else {
            // Use the summary table.
            $query = "SELECT params
                        ppu FROM {$this->profile->urlparams} WHERE
                        timestamp >= {$this->from} AND timestamp <= {$this->to} and `params` LIKE '%.pdf%'
                        ORDER BY timestamp";
        }
        return $query;
    }
}
?>

I'd like to know what you are trying to do with your query, because the current query in your code (which I did not change) will not work.

Hope this helps you create the report you want.

kind regards,
Mark Spier

Mark, Thanks for the quick reply! I am "playing" with custom reporting and this was my first attempt to see what I can do. I expected that the report would not do what I am envisioning, but having never done this before, and needing to learn the syntax of creating one, I had to start somewhere. I will work with your suggestions and try to craft a query that will hopefully produce the output I am looking for. I will probably ask more questions as I continue, and am very appreciative of your excellent support!
Mark, What is the correct syntax for querying the 'urlparams' table? My current syntax: "SELECT params FROM {$this->profile->_urlparams} WHERE I guess this also gets to what "string" does the $this->profile contain for ANY profile? Does it contain lwa_ or is that ignored etc... Thanks!
The information on this page relates to older Logaholic versions (prior to Logaholic 6) and may be outdated.