Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates how to show a custom summary value for a Participants Database Participant Log
<?php
/**
* Plugin Name: PDB Custom Summary
* Description: provides a customized sum value for the "Work Log" log
* Version: 1.0
*
*/
class pdb_work_log_summary {
/**
* @var string name of the log
*/
private $log_name = 'work_log';
/**
* @var string name of the project we want to get the total hours for
*/
private $project_name = 'Mailer Assembly';
/**
* sets up the filter handler
*/
public function __construct()
{
add_filter( 'pdblog-' . $this->log_name . '_sum', array( $this, 'sum_value' ), 10, 2 );
}
/**
* provides the custom sum value
*
* @param int $sum
* @param array $log_data
* @return int sum
*/
public function sum_value( $sum, $log_data )
{
return $this->calc_sum( $log_data );
}
/**
* calculates the sum
*
* @param array $log_data
* @return int sum value
*/
private function calc_sum( $log_data )
{
/*
* the $log_data argument will come in as an array of std objects,
* each object is a log entry
*
* for your application, you would calculate your sum based on the values in
* this array
*
* In this example, we will iterate through the array to build our sum, which
* is all the hours logged while working on the "Mailer Assembly" project
*/
$sum = 0;
// check each entry, if they logged time on the mailer project, add the hours
foreach ( $log_data as $entry )
{
if ( $entry->project === $this->project_name ) {
$sum += $entry->hours;
}
}
return $sum;
}
}
new pdb_work_log_summary();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment