Extending Site Statistics with your plugin

From GeeklogWiki
Revision as of 22:01, 28 June 2004 by Tomw (talk | contribs) (=Function details and examples=)

Jump to: navigation, search

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 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.

There is just one (1) plugin function 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:

Staticpages 1.2 Reports summary and detail
FileMgmt v1.2   Reports summary and detail
Contact Manager v1.0 Reports summary only - Total number of contacts
Chatterblock 3.0 Reports summary only - Total number of chat records
The following table summarizes the function:
  Function Description of Function
1 plugin_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 paramater $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

function</b> plugin_showstats_filemgmt(</font>$showsitestats) {

      <b>       <p style="margin-top: 0; margin-bottom: 4">global</b>        $LANG_FILEMGMT,       $_FM_TABLES,       $_CONF;
             <p style="margin-top: 0; margin-bottom: 4">       $stat_templates = <b>new</b> Template($_CONF['path_layout']        . 'stats');
             <p style="margin-top: 0; margin-bottom: 4">       $stat_templates<b>-></b>set_file(array('itemstats'=>'itemstatistics.thtml',        'statrow'=>'singlestat.thtml'));
      <p style="margin-top: 0; margin-bottom: 4"><b>if</b> ($showsitestats        == 1) {
<p style="margin-top: 0; margin-bottom: 4"> $total_pages=DB_count($_FM_TABLES['filemgmt_filedetail']);

<p style="margin-top: 0; margin-bottom: 4"> $total_downloads=DB_getItem($_FM_TABLES['filemgmt_filedetail'], 'SUM(hits)',"");

<p style="margin-top: 0; margin-bottom: 4"> $retval  = "<table border = '0' width='100%' cellspacing='0' cellpadding='0'>";

<p style="margin-top: 0; margin-bottom: 4"> $retval .= "<tr><td>" . $LANG_FILEMGMT['nofiles'] . "</td>";

<p style="margin-top: 0; margin-bottom: 4"> $retval .= "<td align='right'>" . $total_pages . " (" .$total_downloads

<p style="margin-top: 0; margin-bottom: 4">     .")&nbsp;&nbsp;&nbsp;</td></tr></table>";

</blockquote> <p style="margin-top: 0; margin-bottom: 4">} <b>else</b> {

<p style="margin-top: 0; margin-bottom: 4"> $result = DB_query("SELECT lid, title, hits from {<b>$_FM_TABLES</b>['filemgmt_filedetail']} WHERE hits > 0 ORDER BY hits desc LIMIT 10");

<p style="margin-top: 0; margin-bottom: 4"> $nrows = DB_numRows($result);

<p style="margin-top: 0; margin-bottom: 4"> $retval .= COM_startBlock($LANG_FILEMGMT['StatsMsg1']);

<p style="margin-top: 0; margin-bottom: 4"><b>if</b> ($nrows > 0) {

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>set_var('item_label',"Page Title");

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>set_var('stat_name',"Hits");

<p style="margin-top: 0; margin-bottom: 4"><b>for</b> ($i = 0; $i < $nrows && $i < 10; $i++) {

<p style="margin-top: 0; margin-bottom: 4">list (</font>$lid, $title,$hits) = DB_fetchARRAY($result);

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>set_var('item_url', $_CONF[site_url]. "/filemgmt/singlefile.php?lid=".$lid);

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>set_var('item_text', $title);

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>set_var('item_stat', $hits);

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>parse('stat_row','statrow',<b>true</b>);

</blockquote> <p style="margin-top: 0; margin-bottom: 4">}

<p style="margin-top: 0; margin-bottom: 4"> $stat_templates<b>-></b>parse('output','itemstats');

<p style="margin-top: 0; margin-bottom: 4"> $retval .= $stat_templates<b>-></b>finish($stat_templates<b>-></b>get_var('output'));

</blockquote> <p style="margin-top: 0; margin-bottom: 4">} <b>else</b> {

<p style="margin-top: 0; margin-bottom: 4"> $retval .= $LANG_FILEMGMT['StatsMsg2'];

</blockquote> <p style="margin-top: 0; margin-bottom: 4">}

<p style="margin-top: 0; margin-bottom: 4"> $retval .= COM_endBlock();

</blockquote> <p style="margin-top: 0; margin-bottom: 4">}

<p style="margin-top: 0; margin-bottom: 4"><b>return</b> $retval;

<p style="margin-top: 0; margin-bottom: 4">}</td> </tr> </table>This example function is from the filemgmt plugin 1.2 release and is similar to the staticpage search function. 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 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.

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.