Difference between revisions of "Extending Site Statistics with your plugin"

From GeeklogWiki
Jump to: navigation, search
m (added link to Plugin API)
(formatting and minor corrections)
Line 1: Line 1:
This section will describe and document how to enable your plugin to return  statistics back to Geeklog when the site stats page is requested. There are two main sections to the site stats page. The first is a block at the top of the stats pages used for the site summary. The rest of the stats page is a more detailed report for each Geeklog component. Your plugin can be written to return results for both or just one of these sections.
+
This section will describe and document how to enable your plugin to return  statistics back to Geeklog when the site stats page is requested. There are two main sections to the site stats page. The first is a block at the top of the stats pages used for the site summary. The rest of the stats page is a more detailed report for each Geeklog component. Your plugin can be written to return results for both or just one of these sections.
  
The site statistics summary block at the top may have one line for your plugin as an overall summary counter. This may be the number of contacts in your contact plugin, number of files in your FileMgmt or some other overall event counter for your plugin. This will be a straight forward database query to return the count and description.
+
The site statistics summary block at the top may have one line for your plugin as an overall summary counter. This may be the number of contacts in your contact plugin, number of files in your file repository or some other overall counter for your plugin. This will be a straight forward database query to return the count and description.
  
The detail stats section allows you to have a block on the stats page for your plugin in which you can report as many lines of detail you want. For example: the staticpage plugin and filemgmt plugin report respectfully, the top 10 pages and files.
+
The detailed stats section allows you to have a block on the stats page for your plugin in which you can report as many lines of detail as you want. For example: the [[Static Pages Plugin] and [[File Management Plugin]] report, respectively, the top 10 pages and and top 10 files.
  
There is just one plugin function (actually, there are [[Stats Function|two]]) that you need for your plugin project to support reporting stats back to Geeklog. The example function that is used for explanation in this section and included in this kit should be provide enough details. You can also  review the following plugins which have this function implemented:
+
There is just one plugin function (actually, there are [[Stats Function|two]]) that you need for your plugin to support reporting stats back to Geeklog. The example function that is used for explanation in this section and included in the [[Universal Plugin ToolKit|kit]] should provide enough details. You can also  review the following plugins which have this function implemented:
  
<table border="1" cellpadding="3" cellspacing="0"><tr>
+
<table border="1" cellpadding="3" cellspacing="0">
<td width="34%">Staticpages 1.2</td><td>Reports summary and detail</td></tr>
+
<tr><td width="34%">[[Calendar Plugin]]</td><td>Reports summary and detail</td></tr>
<tr><td width="34%">FileMgmt v1.2</td><td>Reports summary and detail</td></tr>
+
<tr><td width="34%">[[Links Plugin]]</td><td>Reports summary and detail</td></tr>
 +
<tr><td width="34%">[[Polls Plugin]]</td><td>Reports summary and detail</td></tr>
 +
<tr><td width="34%">[[Spam-X Plugin]]</td><td>Reports summary. The detailed list is only visible to Admin users</td></tr>
 +
<tr><td width="34%">[[Static Pages Plugin]]</td><td>Reports summary and detail</td></tr>
 +
<tr><td width="34%">[[File Management Plugin]]</td><td>Reports summary and detail</td></tr>
 
<tr><td width="34%">Contact Manager v1.0</td><td>Reports summary only - Total number of contacts</td></tr>
 
<tr><td width="34%">Contact Manager v1.0</td><td>Reports summary only - Total number of contacts</td></tr>
<tr><td width="34%">Chatterblock 3.0</td><td>Reports summary only - Total      number of chat records</td></tr></table>
+
<tr><td width="34%">[[Chatterblock Plugin]]</td><td>Reports summary only - Total      number of chat records</td></tr></table>
  
 
The following table summarizes the function:
 
The following table summarizes the function:
Line 23: Line 27:
 
=== How to call the Stats Function ===
 
=== How to call the Stats Function ===
  
You only need to include the completed <i>showstats</i> function in your plugin functions.inc file. Ensure that you.use the standard function naming  convention. It will automatically be called by the Geeklog stats.php program when the site stats pages is being created. The function has one <i>int </i>paramater <b>$showsitestats</b> which is either = 1 for summary or = 2 for detail.
+
You only need to include the completed <i>showstats</i> function in your plugin <tt>functions.inc</tt> file. Ensure that you use the standard function naming  convention. It will automatically be called by the Geeklog <tt>stats.php</tt> program when the site stats pages is being created. The function has one <i>int </i> parameter <b>$showsitestats</b> which is either = 1 for summary or = 2 for detail.
  
 
The [[Plugin API]] call <code>PLG_getPluginStats()</code> is called twice in <tt>stats.php</tt> and with each call, all the enabled plugins will return their statistics which are then formatted into the final page.
 
The [[Plugin API]] call <code>PLG_getPluginStats()</code> is called twice in <tt>stats.php</tt> and with each call, all the enabled plugins will return their statistics which are then formatted into the final page.
Line 32: Line 36:
 
=== Plugin database changes ===
 
=== Plugin database changes ===
  
There are no required database changes or plugin table requirements for this API.  
+
There are no required database changes or plugin table requirements for this API.  
  
 
=== Function details and examples ===
 
=== Function details and examples ===
  
This example function is from the [[File Management Plugin]] 1.2 release. It uses the Geeklog template class to return the properly formatted output to stats.php.
+
This example function is from the [[File Management Plugin]] 1.2 release. It uses the Geeklog template class to return the properly formatted output to <tt>stats.php</tt>.
  
 
If the passed int parameter is (1) then it returns a summary level metric. In  this case and in most plugins, this would be the total number of main records in  your plugin. You should use language variables for all displayed text.
 
If the passed int parameter is (1) then it returns a summary level metric. In  this case and in most plugins, this would be the total number of main records in  your plugin. You should use language variables for all displayed text.
Line 42: Line 46:
 
If there are no stats for this plugin at this time to report - then return an  empty result &quot;&quot;.  
 
If there are no stats for this plugin at this time to report - then return an  empty result &quot;&quot;.  
  
The detail level reporting is a bit more involved. For the filemgmt plugin  and the staticpage plugin as well, the detail reporting is for the top 10 accessed records. You may need to add logic to your plugin if it does not  already have the required data that you want to report on.
+
The detail level reporting is a bit more involved. For the [[File Management Plugin]] and the [[Static Pages Plugin]] as well, the detail reporting is for the top 10 accessed records. You may need to add logic to your plugin if it does not  already have the required data that you want to report on.
  
In most cases, you will have the data in the plugin records. In this example, the SQL statement does most of the work, returning the top 10 accessed records. The logic then checks for a search result &gt; 0 records and executes a loop to  format each line for the final returned output.
+
In most cases, you will have the data in the plugin records. In this example, the SQL statement does most of the work, returning the top 10 accessed records. The logic then checks for a search result &gt; 0 records and executes a loop to  format each line for the final returned output.
  
The stats template has a number of defined variables like <i>item_label,  stat_name, item_url, item_text, item_stat</i> that are being set with the results of each record - in this case the top 10 accessed files. The two templates used are under &lt;theme_dir&gt;/stats and are itemstatistics.thtml and  singlestat.thtml.
+
The stats template has a number of defined variables like <i>item_label,  stat_name, item_url, item_text, item_stat</i> that are being set with the results of each record - in this case the top 10 accessed files. The two templates used are under &lt;theme_dir&gt;/stats and are <tt>itemstatistics.thtml</tt> and  <tt>singlestat.thtml</tt>.
  
The detail stats section is setup to provide a link <i>item_url</i> for each item in the result. This allows the user to immediately access the item from the stats report page. The template <i>item_url</i> variable is set to the plugin program URL to view the item.
+
The detail stats section is setup to provide a link <i>item_url</i> for each item in the result. This allows the user to immediately access the item from the stats report page. The template <i>item_url</i> variable is set to the plugin program URL to view the item.
  
 
<pre>
 
<pre>

Revision as of 14:51, 24 May 2009

This section will describe and document how to enable your plugin to return statistics back to Geeklog when the site stats page is requested. There are two main sections to the site stats page. The first is a block at the top of the stats pages used for the site summary. The rest of the stats page is a more detailed report for each Geeklog component. Your plugin can be written to return results for both or just one of these sections.

The site statistics summary block at the top may have one line for your plugin as an overall summary counter. This may be the number of contacts in your contact plugin, number of files in your file repository or some other overall counter for your plugin. This will be a straight forward database query to return the count and description.

The detailed stats section allows you to have a block on the stats page for your plugin in which you can report as many lines of detail as you want. For example: the [[Static Pages Plugin] and File Management Plugin report, respectively, the top 10 pages and and top 10 files.

There is just one plugin function (actually, there are two) that you need for your plugin to support reporting stats back to Geeklog. The example function that is used for explanation in this section and included in the kit should provide enough details. You can also review the following plugins which have this function implemented:

Calendar PluginReports summary and detail
Links PluginReports summary and detail
Polls PluginReports summary and detail
Spam-X PluginReports summary. The detailed list is only visible to Admin users
Static Pages PluginReports summary and detail
File Management PluginReports summary and detail
Contact Manager v1.0Reports summary only - Total number of contacts
Chatterblock PluginReports summary only - Total number of chat records

The following table summarizes the function:

 Function Description of Function
1plugin_showstats_{plugin}This function expands the site stats reporting and is called with a parameter to indicate to get the plugin summary or detail stats. You can return a blank ' ' if there is nothing to report


How to call the Stats Function

You only need to include the completed showstats function in your plugin functions.inc file. Ensure that you use the standard function naming convention. It will automatically be called by the Geeklog stats.php program when the site stats pages is being created. The function has one int parameter $showsitestats which is either = 1 for summary or = 2 for detail.

The Plugin API call PLG_getPluginStats() is called twice in stats.php and with each call, all the enabled plugins will return their statistics which are then formatted into the final page.

You may decide to only return a result for one of the stats features. Chatterblock for example only returns a summary stat result.


Plugin database changes

There are no required database changes or plugin table requirements for this API.

Function details and examples

This example function is from the File Management Plugin 1.2 release. It uses the Geeklog template class to return the properly formatted output to stats.php.

If the passed int parameter is (1) then it returns a summary level metric. In this case and in most plugins, this would be the total number of main records in your plugin. You should use language variables for all displayed text.

If there are no stats for this plugin at this time to report - then return an empty result "".

The detail level reporting is a bit more involved. For the File Management Plugin and the Static Pages Plugin as well, the detail reporting is for the top 10 accessed records. You may need to add logic to your plugin if it does not already have the required data that you want to report on.

In most cases, you will have the data in the plugin records. In this example, the SQL statement does most of the work, returning the top 10 accessed records. The logic then checks for a search result > 0 records and executes a loop to format each line for the final returned output.

The stats template has a number of defined variables like item_label, stat_name, item_url, item_text, item_stat that are being set with the results of each record - in this case the top 10 accessed files. The two templates used are under <theme_dir>/stats and are itemstatistics.thtml and singlestat.thtml.

The detail stats section is setup to provide a link item_url for each item in the result. This allows the user to immediately access the item from the stats report page. The template item_url variable is set to the plugin program URL to view the item.

function plugin_showstats_filemgmt($showsitestats)
{
    global $_CONF, $LANG_FILEMGMT, $_FM_TABLES;

    $retval = '';
    $stat_templates = new Template($_CONF['path_layout'] . 'stats');
    $stat_templates->set_file(array('itemstats'=>'itemstatistics.thtml', 'statrow'=>'singlestat.thtml'));
    if ($showsitestats == 1) {
        $total_pages=DB_count($_FM_TABLES['filemgmt_filedetail']);
        $total_downloads=DB_getItem($_FM_TABLES['filemgmt_filedetail'], 'SUM(hits)',"");
        $retval  = "<table border = '0' width='100%' cellspacing='0' cellpadding='0'>";
        $retval .= "<tr><td>" . $LANG_FILEMGMT['nofiles'] . "</td>";
        $retval .= "<td align='right'>" . $total_pages . " (" .$total_downloads .")
    } else {
        $result = DB_query("SELECT lid, title, hits FROM {$_FM_TABLES['filemgmt_filedetail']} WHERE hits > 0 ORDER BY hits desc LIMIT 10");
        $nrows = DB_numRows($result);
        $retval .= COM_startBlock($LANG_FILEMGMT['StatsMsg1']);
        if ($nrows > 0) {
            $stat_templates->set_var('item_label',"Page Title");
            $stat_templates->set_var('stat_name',"Hits");
            for ($i = 0; $i < $nrows && $i < 10; $i++) {
                list ($lid, $title,$hits) = DB_fetchARRAY($result);
                $stat_templates->set_var('item_url', $_CONF[site_url]. "/filemgmt/singlefile.php?lid=".$lid);
                $stat_templates->set_var('item_text', $title);
                $stat_templates->set_var('item_stat', $hits);
                $stat_templates->parse('stat_row','statrow',true); 
            }
            $stat_templates->parse('output','itemstats');
            $retval .= $stat_templates->finish($stat_templates->get_var('output'));
        } else {
            $retval .= $LANG_FILEMGMT['StatsMsg2'];
        } 
        $retval .= COM_endBlock();
    }

    return $retval;
}

Note: This API changed slightly in Geeklog 1.4.0, see Stats Function.