Return logs with farm_livestock_weight_all and farm_livestock_weight_dlwg.

This commit is contained in:
paul121 2020-07-31 13:50:17 -07:00 committed by Michael Stenta
parent 47cbbde867
commit 4015d6c4ee
2 changed files with 44 additions and 27 deletions

View File

@ -47,7 +47,7 @@ function farm_livestock_weight($asset) {
* The animal asset to get weight for. * The animal asset to get weight for.
* *
* @return array * @return array
* Returns an array of arrays with the following information from each weight log ('weight', value, units, label, timestamp) * Returns an array of arrays with the following information from each weight log ('weight', value, units, label, log)
* Newest is the first, oldest last. * Newest is the first, oldest last.
*/ */
function farm_livestock_weight_all($asset) { function farm_livestock_weight_all($asset) {
@ -66,7 +66,7 @@ function farm_livestock_weight_all($asset) {
foreach ($data as $quantity){ foreach ($data as $quantity){
if (!empty($quantity['value'])){ if (!empty($quantity['value'])){
$quantity['timestamp'] = $log->timestamp; $quantity['log'] = $log;
$log_weights[] = $quantity; $log_weights[] = $quantity;
@ -91,35 +91,47 @@ function farm_livestock_weight_all($asset) {
* *
* @return array * @return array
* Returns an array of information around the daily liveweight gain: * Returns an array of information around the daily liveweight gain:
* "most_recent" - Timestamp of most recent weight logs * "latest_log" - The latest weight log.
* "previous_record" - timestamp of the record before the most recent. * "previous_log" - The weight log recorded before the latest.
* "value" - The daily liveweight gain values * "value" - The daily liveweight gain value.
* "units" - The unit of measure (eg kg) * "units" - The unit of measure (eg kg).
*/ */
function farm_livestock_weight_dlwg($asset) { function farm_livestock_weight_dlwg($asset) {
// This function returns an array[4] of (date of most recent weight, date of previous weight, daily liveweight gain, and the units)
$logs = farm_livestock_weight_all($asset);
$dailyliveweightgain = array(); // Get weight data for the asset.
if (count($logs)>1){ $weights = farm_livestock_weight_all($asset);
if (($logs[0]['units']) == ($logs[1]['units'])){ // Build array of Daily Liveweight Gain information.
$dlwg = array();
$most_recent = $logs[0]['timestamp']; // At least 2 weights must be recorded to calculate Daily Liveweight gain.
$prevous_record = $logs[1]['timestamp']; if (count($weights)>1){
$weight_difference = $logs[0]['value']-$logs[1]['value'];
$timediff = $most_recent- $prevous_record; // Make sure logs use the same units.
if (($weights[0]['units']) == ($weights[1]['units'])){
$latest_weight = $weights[0];
$latest_log = $latest_weight['log'];
$previous_weight = $weights[1];
$previous_log = $previous_weight['log'];
// Calculate weight difference.
$weight_difference = $latest_weight['value'] - $previous_weight['value'];
// Calculate time difference.
$timediff = $latest_log->timestamp - $previous_log->timestamp;
$timediff_days = round($timediff/86400,2); $timediff_days = round($timediff/86400,2);
$dlwg = round($weight_difference/$timediff_days,3); // Calculate dlwg.
$dlwg_value = round($weight_difference/$timediff_days,3);
$dailyliveweightgain = array("most_recent" => $most_recent, "previous_record" => $prevous_record, "value" => $dlwg, "units" => $logs[0]['units']); $dlwg = array("latest_log" => $latest_log, "previous_log" => $previous_log, "value" => $dlwg_value, "units" => $latest_weight['units']);
} }
} }
return $dailyliveweightgain; return $dlwg;
} }

View File

@ -33,24 +33,29 @@ function farm_livestock_weight_individual_report(FarmAsset $farm_asset) {
}; };
// Get all 'weight' logs associated with the asset. // Get all 'weight' logs associated with the asset.
$logs = farm_livestock_weight_all($farm_asset); $weights = farm_livestock_weight_all($farm_asset);
// Store all log weight data
$log_weights = array();
// Ensure there are weight logs. // Ensure there are weight logs.
if (!empty($logs)) { if (!empty($weights)) {
// Simple html table of weights. // Simple html table of weights.
$output .= t('<h3> All Weights </h3>'); $output .= t('<h3> All Weights </h3>');
$header = array(t('Date'), t('Value'), t('Units')); $header = array(t('Date'), t('Value'), t('Units'));
$table_data = array(); $table_data = array();
foreach($logs as $log){ foreach($weights as $key => $weight){
$table_data[] = array(format_date($log['timestamp']),$log['value'],$log['units']);
// Save the log.
$log = $weight['log'];
// Save the timestamp to the weights array for graphing.
$weights[$key]['timestamp'] = $log->timestamp;
// Add to table data.
$table_data[] = array(format_date($log->timestamp), $weight['value'], $weight['units']);
} }
// Output the table.
$output .= theme('table', array('header' => $header, 'rows' => $table_data)); $output .= theme('table', array('header' => $header, 'rows' => $table_data));
// Create the div to hold report graphs. // Create the div to hold report graphs.
@ -63,7 +68,7 @@ function farm_livestock_weight_individual_report(FarmAsset $farm_asset) {
$graph = array( $graph = array(
'name' => $farm_asset->name . t(' Weight Report'), 'name' => $farm_asset->name . t(' Weight Report'),
'id' => 'farm-report-weight-graph', 'id' => 'farm-report-weight-graph',
'data' => $logs, 'data' => $weights,
); );
$graph_markup[] = '<div id="farm-report-weight-graph" class="farm-report-graph"></div>'; $graph_markup[] = '<div id="farm-report-weight-graph" class="farm-report-graph"></div>';
$graphs[] = $graph; $graphs[] = $graph;