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

From GeeklogWiki
Jump to: navigation, search
(added a link to updated Stats API description)
(typos and such)
 
(5 intermediate revisions by the same user not shown)
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 page 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 (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:
+
There are two plugin functions that you need for your plugin to support reporting stats back to Geeklog. The example functions that are used for explanation in this section should provide enough details. You can also review the plugins that are bundled with Geeklog, most of which have these functions implemented. For example, the [[Static Pages Plugin]] implements both the summary and the detailed list. The [[Spam-X Plugin]] also implements both, but makes the detailed list only available to Admin users.
  
<table border="1" cellpadding="3" cellspacing="0"><tr>
+
The following table summarizes the functions:
<td width="34%">Staticpages 1.2</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%">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>
 
  
The following table summarizes the function:
+
{| border="1" cellpadding="5"
 +
!
 +
! Function
 +
! Description of Function
 +
|-
 +
|1
 +
|plugin_statssummary_{plugin}
 +
|This function returns the entry for the plugin summary on the stats page.
 +
|-
 +
|2
 +
|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 <tt>' '</tt> if there is nothing to report
 +
|}
  
<table cellPadding="2" width="100%" border="1"><tr>
 
<th width="4%">&nbsp;</th><th width="27%">Function</th>
 
<th>Description of Function</th></tr>
 
<tr><td width="4%" align="center">1</td><td width="27%">plugin_showstats_{plugin}</td><td>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</td></tr></table>
 
  
=== 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.
+
Please note that this API changed slightly in Geeklog 1.4.0. We will only describe the current implementation here. If you are interested in the details of the old API, see [[Stats Function]].
  
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.
+
Both functions should be implemented in your plugin's <tt>functions.inc</tt> file, as usual. Ensure that you use the standard function naming  convention for both. They will automatically be called by the Geeklog <tt>stats.php</tt> program when the site stats page is being created.
  
You may decide to only return a result for one of the stats features.  Chatterblock for example only returns a summary stat result.
+
You may decide to only return a result for one of the stats features.  [[Chatterblock Plugin|Chatterblock]] for example only returns a summary stat result.
  
 +
=== Summary Stats ===
  
=== Plugin database changes ===
+
For the summary stats, your <code>statssummary</code> function only needs to return an array where the first item is the label for the summary (to be displayed on the stats page) and the second entry is the number. By convention, your plugin should format the number using the <code>COM_NumberFormat</code> function.
  
There are no required database changes or plugin table requirements for this  API.  
+
This function will typically be very short. Here's an example, taken from the [[Static Pages Plugin]]:
  
=== Function details and examples ===
+
<pre>
 +
function plugin_statssummary_staticpages()
 +
{
 +
    global $LANG_STATIC;
 +
 
 +
    $total_pages = SP_countVisiblePages();
  
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&nbsp; output to stats.php.
+
    return array($LANG_STATIC['staticpages'], COM_numberFormat($total_pages));
 +
}
 +
</pre>
  
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.
+
=== Detailed Stats ===
  
If there are no stats for this plugin at this time to report - then return an  empty result &quot;&quot;.  
+
For the detailed stats list, your <code>showstats</code> function will be called with an int parameter of 2 (for historical reasons - see the note above). In this case, your plugin should return a completely formatted Geeklog block with the contents of your list.
  
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.
+
Traditionally, plugins used the template files from the current theme's <tt>stats</tt> directory for this list. For new plugins, however, we recommend that you use an [[AdminLists|Admin List]] instead, e.g. a <code>ADMIN_simpleList</code>. Sample code can again be found in the plugins that ship with Geeklog but is also shown below.
  
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.
+
== Function details and examples ==
  
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 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. If no records are found, we return a simple message (wrapped between <code>COM_startBlock</code> and <code>COM_endBlock</code>) stating just that.
 +
 
 +
If there are results, we loop over them, format them, and store the formatted date in an array (<code>$data_array</code>) that is then passed on to an <code>ADMIN_simpleList</code> which formats it for display on the stats page. Doing it this way ensures a consistent display of all detailed lists on the stats page without the need of having to deal with templates.
 +
 
 +
The detail stats section is setup to provide a link for each item in the result. This allows the user to immediately access the item from the stats report page.  
  
 
<pre>
 
<pre>
function plugin_showstats_filemgmt($showsitestats)
+
function plugin_showstats_staticpages($showsitestats)
 
{
 
{
     global $_CONF, $LANG_FILEMGMT, $_FM_TABLES;
+
     global $_CONF, $_TABLES, $LANG_STATIC;
  
 
     $retval = '';
 
     $retval = '';
     $stat_templates = new Template($_CONF['path_layout'] . 'stats');
+
 
     $stat_templates->set_file(array('itemstats'=>'itemstatistics.thtml', 'statrow'=>'singlestat.thtml'));
+
    $perms = SP_getPerms();
    if ($showsitestats == 1) {
+
    if (!empty($perms)) {
         $total_pages=DB_count($_FM_TABLES['filemgmt_filedetail']);
+
        $perms = ' AND ' . $perms;
         $total_downloads=DB_getItem($_FM_TABLES['filemgmt_filedetail'], 'SUM(hits)',"");
+
    }
        $retval  = "<table border = '0' width='100%' cellspacing='0' cellpadding='0'>";
+
     $result = DB_query("SELECT sp_id,sp_title,sp_hits FROM {$_TABLES['staticpage']} "
        $retval .= "<tr><td>" . $LANG_FILEMGMT['nofiles'] . "</td>";
+
            . "WHERE sp_hits > 0" . $perms . ' ORDER BY sp_hits DESC LIMIT 10');
         $retval .= "<td align='right'>" . $total_pages . " (" .$total_downloads .")
+
     $nrows  = DB_numRows($result);
 +
    if ($nrows > 0) {
 +
        require_once $_CONF['path_system'] . 'lib-admin.php';
 +
 
 +
        $header_arr = array(
 +
            array('text'        => $LANG_STATIC['stats_page_title'],
 +
                  'field'        => 'sid',
 +
                  'header_class' => 'stats-header-title'),
 +
            array('text'        => $LANG_STATIC['stats_hits'],
 +
                  'field'        => 'sp_hits',
 +
                  'header_class' => 'stats-header-count',
 +
                  'field_class' => 'stats-list-count'),
 +
        );
 +
        $data_arr = array();
 +
         $text_arr = array('has_menu' => false,
 +
                          'title'    => $LANG_STATIC['stats_headline']
 +
        );
 +
         for ($i = 0; $i < $nrows; $i++) {
 +
            $A = DB_fetchArray($result);
 +
            $A['sp_title'] = stripslashes($A['sp_title']);
 +
            $A['sid'] = COM_createLink($A['sp_title'],
 +
                            COM_buildUrl($_CONF['site_url']
 +
                                . "/staticpages/index.php?page={$A['sp_id']}"));
 +
            $A['sp_hits'] = COM_NumberFormat($A['sp_hits']);
 +
            $data_arr[$i] = $A;
 +
        }
 +
         $retval .= ADMIN_simpleList("", $header_arr, $text_arr, $data_arr);
 
     } else {
 
     } else {
        $result = DB_query("SELECT lid, title, hits FROM {$_FM_TABLES['filemgmt_filedetail']} WHERE hits > 0 ORDER BY hits desc LIMIT 10");
+
         $retval .= COM_startBlock($LANG_STATIC['stats_headline']);
        $nrows = DB_numRows($result);
+
         $retval .= $LANG_STATIC['stats_no_hits'];
         $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();
 
         $retval .= COM_endBlock();
 
     }
 
     }
Line 89: Line 116:
 
</pre>
 
</pre>
  
'''Note:''' This API changed slightly in Geeklog 1.4.0, see [[Stats Function]].
+
Please note that most of the code in this function is to prepare the use of the [[AdminLists|Admin List]] - only the SQL request and the <code>for</code> loop actually prepare the detailed stats data.
  
  
 
[[Category:Plugin Developers Handbook]] [[Category:Plugin Development]]
 
[[Category:Plugin Developers Handbook]] [[Category:Plugin Development]]

Latest revision as of 16:20, 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 page 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 are two plugin functions that you need for your plugin to support reporting stats back to Geeklog. The example functions that are used for explanation in this section should provide enough details. You can also review the plugins that are bundled with Geeklog, most of which have these functions implemented. For example, the Static Pages Plugin implements both the summary and the detailed list. The Spam-X Plugin also implements both, but makes the detailed list only available to Admin users.

The following table summarizes the functions:

Function Description of Function
1 plugin_statssummary_{plugin} This function returns the entry for the plugin summary on the stats page.
2 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

Please note that this API changed slightly in Geeklog 1.4.0. We will only describe the current implementation here. If you are interested in the details of the old API, see Stats Function.

Both functions should be implemented in your plugin's functions.inc file, as usual. Ensure that you use the standard function naming convention for both. They will automatically be called by the Geeklog stats.php program when the site stats page is being created.

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

Summary Stats

For the summary stats, your statssummary function only needs to return an array where the first item is the label for the summary (to be displayed on the stats page) and the second entry is the number. By convention, your plugin should format the number using the COM_NumberFormat function.

This function will typically be very short. Here's an example, taken from the Static Pages Plugin:

function plugin_statssummary_staticpages()
{
    global $LANG_STATIC;

    $total_pages = SP_countVisiblePages();

    return array($LANG_STATIC['staticpages'], COM_numberFormat($total_pages));
}

Detailed Stats

For the detailed stats list, your showstats function will be called with an int parameter of 2 (for historical reasons - see the note above). In this case, your plugin should return a completely formatted Geeklog block with the contents of your list.

Traditionally, plugins used the template files from the current theme's stats directory for this list. For new plugins, however, we recommend that you use an Admin List instead, e.g. a ADMIN_simpleList. Sample code can again be found in the plugins that ship with Geeklog but is also shown below.


Function details and examples

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. If no records are found, we return a simple message (wrapped between COM_startBlock and COM_endBlock) stating just that.

If there are results, we loop over them, format them, and store the formatted date in an array ($data_array) that is then passed on to an ADMIN_simpleList which formats it for display on the stats page. Doing it this way ensures a consistent display of all detailed lists on the stats page without the need of having to deal with templates.

The detail stats section is setup to provide a link for each item in the result. This allows the user to immediately access the item from the stats report page.

function plugin_showstats_staticpages($showsitestats)
{
    global $_CONF, $_TABLES, $LANG_STATIC;

    $retval = '';

    $perms = SP_getPerms();
    if (!empty($perms)) {
        $perms = ' AND ' . $perms;
    }
    $result = DB_query("SELECT sp_id,sp_title,sp_hits FROM {$_TABLES['staticpage']} "
            . "WHERE sp_hits > 0" . $perms . ' ORDER BY sp_hits DESC LIMIT 10');
    $nrows  = DB_numRows($result);
    if ($nrows > 0) {
        require_once $_CONF['path_system'] . 'lib-admin.php';

        $header_arr = array(
            array('text'         => $LANG_STATIC['stats_page_title'],
                  'field'        => 'sid',
                  'header_class' => 'stats-header-title'),
            array('text'         => $LANG_STATIC['stats_hits'],
                  'field'        => 'sp_hits',
                  'header_class' => 'stats-header-count',
                  'field_class'  => 'stats-list-count'),
        );
        $data_arr = array();
        $text_arr = array('has_menu' => false,
                          'title'    => $LANG_STATIC['stats_headline']
        );
        for ($i = 0; $i < $nrows; $i++) {
            $A = DB_fetchArray($result);
            $A['sp_title'] = stripslashes($A['sp_title']);
            $A['sid'] = COM_createLink($A['sp_title'],
                            COM_buildUrl($_CONF['site_url']
                                . "/staticpages/index.php?page={$A['sp_id']}"));
            $A['sp_hits'] = COM_NumberFormat($A['sp_hits']);
            $data_arr[$i] = $A;
        }
        $retval .= ADMIN_simpleList("", $header_arr, $text_arr, $data_arr);
    } else {
        $retval .= COM_startBlock($LANG_STATIC['stats_headline']);
        $retval .= $LANG_STATIC['stats_no_hits'];
        $retval .= COM_endBlock();
    }

    return $retval;
}

Please note that most of the code in this function is to prepare the use of the Admin List - only the SQL request and the for loop actually prepare the detailed stats data.