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

From GeeklogWiki
Jump to: navigation, search
(=Plugin database changes=)
(typos and such)
 
(15 intermediate revisions by 3 users 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" style="border-collapse: collapse" bordercolor="#DDDDDD" width="67%" id="AutoNumber4" height="91">  <tr>    <td width="34%" height="19"><font size="2">Staticpages 1.2 </font></td>    <td width="66%" height="19"><font size="2">Reports summary and detail</font></td>  </tr>  <tr>    <td width="34%" height="16"><font size="2">FileMgmt v1.2&nbsp;&nbsp; </font>    </td>    <td width="66%" height="16"><font size="2">Reports summary and detail</font></td>  </tr>  <tr>    <td width="34%" height="16"><font size="2">Contact Manager v1.0</font></td>    <td width="66%" height="16"><font size="2">Reports summary only - Total      number of contacts</font></td>  </tr>  <tr>    <td width="34%" height="16"><font size="2">Chatterblock 3.0</font></td>    <td width="66%" height="16"><font size="2">Reports summary only - Total      number of chat records</font></td>  </tr> </table>The following table summarizes the function:
+
The following table summarizes the functions:
  
  <div align="center">  <center>  <table cellPadding="2" width="100%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellspacing="0">    <tr>      <th vAlign="top" width="4%">&nbsp;</th>      <th vAlign="top" width="27%">Function</th>      <th vAlign="top" width="99%">Description of Function</th>    </tr>    <tr>      <td class="codeheader" width="4%" align="center">      <font size="2">1</font></td>      <td class="codeheader" width="27%"><font size="2">plugin_showstats_{plugin}</font></td>      <td class="code" vAlign="top" width="99%"><font size="2">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</font></td>    </tr>    </table>  </center> </div>  === 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><font size="2">$showsitestats</font></b> which is either = 1 for summary or =  2 for detail.
+
{| 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
 +
|}
  
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.
+
== 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]].
  
=== Plugin database changes ===
+
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.
  
There are no required database changes or plugin table requirements for this API.  
+
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.
  
=== Function details and examples ===
+
=== Summary Stats ===
  
  <table border="0" cellpadding="0" cellspacing="10" style="border-collapse: collapse; float: right; position: relative; left: 0" width="62%" id="AutoNumber1" align="right" height="98">     <tr>      <td width="100%" height="98" bgcolor="#DDDDDD" style="border-style: solid; border-width: 1; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1" bordercolor="#C0C0C0">      <font SIZE="1"><b>      <p style="margin-top: 0; margin-bottom: 4">function</b>        plugin_showstats_filemgmt(</font><font SIZE="1" COLOR="#800000">$showsitestats</font><font size="1">)        {
+
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.
  
      <b>      <p style="margin-top: 0; margin-bottom: 4">global</b> </font>      <font SIZE="1" COLOR="#800000">$LANG_FILEMGMT</font><font SIZE="1">,      </font><font SIZE="1" COLOR="#800000">$_FM_TABLES</font><font SIZE="1">,      </font><font SIZE="1" COLOR="#800000">$_CONF</font><font size="1">;
+
This function will typically be very short. Here's an example, taken from the [[Static Pages Plugin]]:
  
      </font>       <p style="margin-top: 0; margin-bottom: 4"><font SIZE="1" COLOR="#800000">      $stat_templates</font><font SIZE="1"> = <b>new</b> Template(</font><font SIZE="1" COLOR="#800000">$_CONF</font><font SIZE="1">[</font><font COLOR="#0000f0" size="1">'path_layout'</font><font SIZE="1">]        . </font><font SIZE="1" COLOR="#0000f0">'stats'</font><font size="1">);
+
<pre>
 +
function plugin_statssummary_staticpages()
 +
{
 +
    global $LANG_STATIC;
  
      </font>      <p style="margin-top: 0; margin-bottom: 4"><font SIZE="1" COLOR="#800000">      $stat_templates</font><font SIZE="1"><b>-&gt;</b>set_file(array(</font><font COLOR="#0000f0" size="1">'itemstats'</font><font SIZE="1">=&gt;</font><font SIZE="1" COLOR="#0000f0">'itemstatistics.thtml'</font><font size="1">,</font><font SIZE="1" COLOR="#0000f0">        'statrow'</font><font SIZE="1">=&gt;</font><font SIZE="1" COLOR="#0000f0">'singlestat.thtml'</font><font size="1">));
+
    $total_pages = SP_countVisiblePages();
  
      <p style="margin-top: 0; margin-bottom: 4"><b>if</b> (</font><font SIZE="1" COLOR="#800000">$showsitestats</font><font SIZE="1">        == </font><font SIZE="1" COLOR="#ff0000">1</font><font size="1">) {
+
    return array($LANG_STATIC['staticpages'], COM_numberFormat($total_pages));
 +
}
 +
</pre>
  
      </font>      <blockquote>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$total_pages</font><font SIZE="1">=DB_count(</font><font SIZE="1" COLOR="#800000">$_FM_TABLES</font><font SIZE="1">[</font><font COLOR="#0000f0" size="1">'filemgmt_filedetail'</font><font size="1">]);
+
=== Detailed Stats ===
  
        </font>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$total_downloads</font><font SIZE="1">=DB_getItem(</font><font SIZE="1" COLOR="#800000">$_FM_TABLES</font><font SIZE="1">[</font><font COLOR="#0000f0" size="1">'filemgmt_filedetail'</font><font SIZE="1">],        </font><font SIZE="1" COLOR="#0000f0">'SUM(hits)'</font><font SIZE="1">,</font><font SIZE="1" COLOR="#0000f0">&quot;&quot;</font><font size="1">);
+
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.
  
        </font>        <p style="margin-top: 0; margin-bottom: 4">         <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1">&nbsp; =        </font><font SIZE="1" COLOR="#0000f0">&quot;&lt;table border = '0' width='100%'          cellspacing='0' cellpadding='0'&gt;&quot;</font><font size="1">;
+
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.
  
        </font>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1"> .= </font>        <font SIZE="1" COLOR="#0000f0">&quot;&lt;tr&gt;&lt;td&gt;&quot;</font><font SIZE="1"> . </font>        <font SIZE="1" COLOR="#800000">$LANG_FILEMGMT</font><font SIZE="1">[</font><font COLOR="#0000f0" size="1">'nofiles'</font><font SIZE="1">]          . </font><font SIZE="1" COLOR="#0000f0">&quot;&lt;/td&gt;&quot;</font><font size="1">;
 
  
        </font>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1"> .= </font>        <font SIZE="1" COLOR="#0000f0">&quot;&lt;td align='right'&gt;&quot;</font><font SIZE="1">          . </font><font SIZE="1" COLOR="#800000">$total_pages</font><font SIZE="1">          . </font><font SIZE="1" COLOR="#0000f0">&quot; (&quot;</font><font SIZE="1"> .</font><font SIZE="1" COLOR="#800000">$total_downloads</font><font SIZE="1">        </font>
+
== Function details and examples ==
  
        <p style="margin-top: 0; margin-bottom: 4"><font size="1">&nbsp;&nbsp;&nbsp;&nbsp;          .</font><font SIZE="1" COLOR="#0000f0">&quot;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;</font><font size="1">;
+
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.
  
      </blockquote>       <p style="margin-top: 0; margin-bottom: 4">} <b>else</b> {
+
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.
  
      </font>      <blockquote>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$result</font><font SIZE="1"> = DB_query(</font><font COLOR="#0000f0" size="1">&quot;SELECT          lid, title, hits from {</font><font SIZE="1"><b>$_FM_TABLES</b></font><font COLOR="#0000f0" size="1">['filemgmt_filedetail']}          WHERE hits &gt; 0 ORDER BY hits desc LIMIT 10&quot;</font><font size="1">);
+
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.
  
        </font>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$nrows</font><font SIZE="1"> =          DB_numRows(</font><font SIZE="1" COLOR="#800000">$result</font><font size="1">);
+
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.
  
        </font>         <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1"> .=          COM_startBlock(</font><font SIZE="1" COLOR="#800000">$LANG_FILEMGMT</font><font SIZE="1">[</font><font SIZE="1" COLOR="#0000f0">'StatsMsg1'</font><font size="1">]);
+
<pre>
 +
function plugin_showstats_staticpages($showsitestats)
 +
{
 +
    global $_CONF, $_TABLES, $LANG_STATIC;
  
        <p style="margin-top: 0; margin-bottom: 4"><b>if</b> (</font><font SIZE="1" COLOR="#800000">$nrows</font><font SIZE="1">          &gt; </font><font SIZE="1" COLOR="#ff0000">0</font><font size="1">) {
+
    $retval = '';
  
        </font>         <blockquote>          <p style="margin-top: 0; margin-bottom: 4">          <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>set_var(</font><font SIZE="1" COLOR="#0000f0">'item_label'</font><font SIZE="1">,</font><font COLOR="#0000f0" size="1">&quot;Page            Title&quot;</font><font size="1">);
+
    $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';
  
          </font>           <p style="margin-top: 0; margin-bottom: 4">          <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>set_var(</font><font SIZE="1" COLOR="#0000f0">'stat_name'</font><font SIZE="1">,</font><font COLOR="#0000f0" size="1">&quot;Hits&quot;</font><font size="1">);
+
        $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();
 +
    }
  
          <p style="margin-top: 0; margin-bottom: 4"><b>for</b> (</font><font SIZE="1" COLOR="#800000">$i</font><font SIZE="1">            = </font><font SIZE="1" COLOR="#ff0000">0</font><font SIZE="1">;          </font><font SIZE="1" COLOR="#800000">$i</font><font SIZE="1"> &lt;          </font><font SIZE="1" COLOR="#800000">$nrows</font><font SIZE="1"> &amp;&amp;          </font><font SIZE="1" COLOR="#800000">$i</font><font SIZE="1"> &lt;           </font><font SIZE="1" COLOR="#ff0000">10</font><font SIZE="1">; </font>          <font SIZE="1" COLOR="#800000">$i</font><font size="1">++) {
+
    return $retval;
 +
}
 +
</pre>
  
          <blockquote>            <p style="margin-top: 0; margin-bottom: 4">list (</font><font SIZE="1" COLOR="#800000">$lid</font><font SIZE="1">,            </font><font SIZE="1" COLOR="#800000">$title</font><font SIZE="1">,</font><font SIZE="1" COLOR="#800000">$hits</font><font SIZE="1">)              = DB_fetchARRAY(</font><font SIZE="1" COLOR="#800000">$result</font><font size="1">);
+
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.
  
            </font>            <p style="margin-top: 0; margin-bottom: 4">            <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>set_var(</font><font COLOR="#0000f0" size="1">'item_url'</font><font SIZE="1">,            </font><font SIZE="1" COLOR="#800000">$_CONF</font><font size="1">[site_url].            </font><font SIZE="1" COLOR="#0000f0">&quot;/filemgmt/singlefile.php?lid=&quot;</font><font SIZE="1">.</font><font SIZE="1" COLOR="#800000">$lid</font><font size="1">);
 
  
            </font>            <p style="margin-top: 0; margin-bottom: 4">            <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>set_var(</font><font COLOR="#0000f0" size="1">'item_text'</font><font SIZE="1">,            </font><font SIZE="1" COLOR="#800000">$title</font><font size="1">);
+
[[Category:Plugin Developers Handbook]] [[Category:Plugin Development]]
 
 
            </font>            <p style="margin-top: 0; margin-bottom: 4">            <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>set_var(</font><font COLOR="#0000f0" size="1">'item_stat'</font><font SIZE="1">,            </font><font SIZE="1" COLOR="#800000">$hits</font><font size="1">);
 
 
 
            </font>            <p style="margin-top: 0; margin-bottom: 4">            <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>parse(</font><font SIZE="1" COLOR="#0000f0">'stat_row'</font><font SIZE="1">,</font><font SIZE="1" COLOR="#0000f0">'statrow'</font><font SIZE="1">,</font><font SIZE="1" COLOR="#800000"><b>true</b></font><font size="1">);           
 
 
 
          </blockquote>          <p style="margin-top: 0; margin-bottom: 4">}
 
 
 
          </font>          <p style="margin-top: 0; margin-bottom: 4">          <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>parse(</font><font SIZE="1" COLOR="#0000f0">'output'</font><font SIZE="1">,</font><font COLOR="#0000f0" size="1">'itemstats'</font><font size="1">);
 
 
 
          </font>          <p style="margin-top: 0; margin-bottom: 4">          <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1"> .= </font>          <font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>finish(</font><font SIZE="1" COLOR="#800000">$stat_templates</font><font SIZE="1"><b>-&gt;</b>get_var(</font><font COLOR="#0000f0" size="1">'output'</font><font size="1">));
 
 
 
        </blockquote>        <p style="margin-top: 0; margin-bottom: 4">} <b>else</b> {
 
 
 
        </font>        <blockquote>          <p style="margin-top: 0; margin-bottom: 4">          <font SIZE="1" COLOR="#800000">$retval</font><font SIZE="1"> .= </font>          <font SIZE="1" COLOR="#800000">$LANG_FILEMGMT</font><font SIZE="1">[</font><font SIZE="1" COLOR="#0000f0">'StatsMsg2'</font><font size="1">];
 
 
 
        </blockquote>        <p style="margin-top: 0; margin-bottom: 4">}
 
 
 
        </font>        <p style="margin-top: 0; margin-bottom: 4">        <font SIZE="1" COLOR="#800000">$retval</font><font size="1"> .=          COM_endBlock();
 
 
 
      </blockquote>      <p style="margin-top: 0; margin-bottom: 4">}
 
 
 
      <p style="margin-top: 0; margin-bottom: 4"><b>return</b> </font>      <font SIZE="1" COLOR="#800000">$retval</font><font size="1">;
 
 
 
      <p style="margin-top: 0; margin-bottom: 4">}</font></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&nbsp; 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 &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.
 
 
 
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 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.
 

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.