Daily Liveweight Gain statistic for weights #309

This commit is contained in:
Michael Stenta 2020-11-26 06:33:42 -05:00
commit 08d369a4b3
2 changed files with 182 additions and 39 deletions

View File

@ -39,6 +39,140 @@ function farm_livestock_weight($asset) {
return array();
}
/**
* Helper function for retrieving all weight logs of an animal.
*
* @param FarmAsset $asset
* The animal asset to get weight for.
*
* @return array
* Returns an array of arrays with the following information from each weight log:
* weight, value, units, label, log
* Newest is the first, oldest last.
*/
function farm_livestock_weight_all($asset) {
// Load the logs with a 'weight' quantity measurement for this asset.
$logs = farm_quantity_log_asset($asset, 'weight', NULL, REQUEST_TIME, TRUE, 'farm_observation', FALSE);
// If only a single log was returned, wrap it in an array.
if (!is_array($logs)) {
$logs = array($logs);
}
// Start array of log weights.
$log_weights = array();
// Check that there are some logs!
if (!empty($logs)) {
foreach ($logs as $log) {
// Get weight quantity data from log.
$data = farm_quantity_log_data($log, 'weight');
foreach ($data as $quantity) {
if (!empty($quantity['value'])) {
// Save the log with the quantity info.
$quantity['log'] = $log;
// Add quantity to log weights.
$log_weights[] = $quantity;
}
}
}
}
// Return log weights.
return $log_weights;
}
/**
* Helper function for retrieving the latest daily liveweight gain for an animal
*
* @param FarmAsset $asset
* The animal asset to get daily liveweight for.
*
* @return array
* Returns an array of information around the daily liveweight gain:
* "latest_log" - The latest weight log.
* "previous_log" - The weight log recorded before the latest.
* "value" - The daily liveweight gain value.
* "units" - The unit of measure (eg kg).
* "markup" - HTML markup of the dlwg.
*/
function farm_livestock_weight_dlwg($asset) {
// Get weight data for the asset.
$weights = farm_livestock_weight_all($asset);
// Build array of Daily Liveweight Gain information.
$dlwg = array();
// At least 2 weights must be recorded to calculate Daily Liveweight gain.
if (count($weights) > 1) {
// Make sure logs use the same units.
if ($weights[0]['units'] == $weights[1]['units']) {
// Save latest weight info.
$latest_weight = $weights[0];
$latest_log = $latest_weight['log'];
// Save previous weight info.
$previous_weight = $weights[1];
$previous_log = $previous_weight['log'];
// Save units.
$units = $latest_weight['units'];
// 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);
// Calculate dlwg.
$dlwg_value = round($weight_difference/$timediff_days,3);
// Generate markup commonly output by users of this function.
// Date format
$date_format = 'M j Y';
// Build links to weight logs.
// Latest log.
$latest_log_uri = entity_uri('log', $latest_log);
$latest_log_date = date($date_format, $latest_log->timestamp);
// Previous log.
$previous_log_uri = entity_uri('log', $previous_log);
$previous_log_date = date($date_format, $previous_log->timestamp);
// Build value + units text.
$dlwg_text = $dlwg_value . ' ' . $units . '/' . t('day');
// Build "observed between" text.
$observed_text = t(
'observed between <a href="!previous_log_link">@previous_log_date</a> and <a href="!latest_log_link">@latest_log_date</a>',
array(
'!previous_log_link' => url($previous_log_uri['path']),
'@previous_log_date' => $previous_log_date,
'!latest_log_link' => url($latest_log_uri['path']),
'@latest_log_date' => $latest_log_date)
);
// Assemble markup.
$markup = '<p><strong>' . t('Daily liveweight gain') . ':</strong> ' . $dlwg_text . ' (' . $observed_text . ')</p>';
// Build array to return.
$dlwg = array("latest_log" => $latest_log, "previous_log" => $previous_log, "value" => $dlwg_value, "units" => $units, 'markup' => $markup);
}
}
return $dlwg;
}
/**
* Create a weight measurement log associated with an animal.
*
@ -100,7 +234,7 @@ function farm_livestock_weight_menu() {
// Animal asset report tab.
$items[$farm_asset_uri . '/weight'] = array(
'title' => 'Weight',
'title' => t('Weight'),
'page callback' => 'farm_livestock_weight_individual_report',
'page arguments' => array($farm_asset_uri_argument_position),
'access callback' => 'farm_livestock_weight_individual_report_access',
@ -112,7 +246,7 @@ function farm_livestock_weight_menu() {
// Animal group report form.
$items['farm/report/weight'] = array(
'title' => 'Animal Weights',
'title' => t('Animal Weights'),
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_livestock_weight_group_report_form'),
'access arguments' => array('view farm reports'),
@ -148,7 +282,7 @@ function farm_livestock_weight_individual_report_access($farm_asset) {
function farm_livestock_weight_group_report_form($form, &$form_state) {
// Set the page title.
drupal_set_title('Animal Weights');
drupal_set_title(t('Animal Weights'));
// Build an entity field query of group assets.
$query = new EntityFieldQuery();
@ -477,13 +611,13 @@ function farm_livestock_weight_group_report(&$form_state) {
sort($all_log_dates);
// Create a header for CSV and HTML Table
$header = array('Asset ID', 'Asset Name', 'Group');
$header = array(t('Asset ID'), t('Asset Name'), t('Group'));
// Add columns for each date collected.
foreach ($all_log_dates as $date) {
$header[] = 'Date - ' . $date ;
$header[] = 'Weight';
$header[] = 'Units';
$header[] = t('Date - ') . $date ;
$header[] = t('Weight');
$header[] = t('Units');
}
// Add the CSV header.
@ -640,7 +774,7 @@ function farm_livestock_weight_group_report(&$form_state) {
// Create Average Weight Graph
$graph = array(
'name' => 'Average Weight - ' . $group_name,
'name' => t('Average Weight - ') . $group_name,
'id' => 'farm-livestock-average-weight-' . $group_name . '-graph',
'data' => $group['dates'],
);
@ -689,6 +823,7 @@ function farm_livestock_weight_entity_view_alter(&$build, $type) {
// If it's not a farm_asset, or if the entity object is not available, bail.
if ($type != 'farm_asset' || empty($build['#entity'])) {
return;
}
@ -713,15 +848,23 @@ function farm_livestock_weight_entity_view_alter(&$build, $type) {
$value = !empty($weight['value']) ? $weight['value'] : '';
$units = !empty($weight['units']) ? $weight['units'] : '';
// Build the weight display.
$output = '<strong>' . t('Weight') . ':</strong> ' . $value . ' ' . $units . '<a href="' . url($asset_uri['path'] . '/weight') . '"> (weight report) </a>';
// If the animal has an inventory greater than 1, add "(average)".
$inventory = farm_inventory($asset);
$average_text = '';
if ($inventory > 1) {
$output .= ' (' . t('average') . ')';
$average_text .= ' (' . t('average') . ')';
}
// Build the weight display.
$output = '<strong>' . t('Weight') . ':</strong> ' . $value . ' ' . $units . $average_text . '<a href="' . url($asset_uri['path'] . '/weight') . t('"> (weight report) </a>');
// Load the daily live weight gain.
$dlwg = farm_livestock_weight_dlwg($asset);
if (!empty($dlwg)){
// Add the dlwg markup.
$output .= $dlwg['markup'];
}
// Add it to the build array.
$build['weight'] = array(
'#markup' => $output,

View File

@ -22,43 +22,43 @@ function farm_livestock_weight_individual_report(FarmAsset $farm_asset) {
// Check if the asset has a weight recorded.
if (empty($current_weight)) {
$output .= '<p>No weight recorded for asset</p>';
} else {
$output .= '<p><strong>Current Weight:</strong> ' . $current_weight['value'] . ' ' . $current_weight['units'] . '</p>';
$output .= '<p>' . t('No weight recorded for asset') . '</p>';
}
else {
$output .= '<p><strong>' . t('Current weight') . ':</strong> ' . $current_weight['value'] . ' ' . $current_weight['units'] . '</p>';
// Load the daily live weight gain.
$dlwg = farm_livestock_weight_dlwg($farm_asset);
if (!empty($dlwg)) {
// Add the dlwg markup.
$output .= $dlwg['markup'];
}
};
// Get all 'weight' logs associated with the asset.
$logs = farm_quantity_log_asset($farm_asset, 'weight', $label = NULL, $time = REQUEST_TIME, $done = TRUE, $type = 'farm_observation', $single = FALSE);
// Store all log weight data
$log_weights = array();
$weights = farm_livestock_weight_all($farm_asset);
// Ensure there are weight logs.
if (!empty($logs)) {
if (!empty($weights)) {
// Simple html table of weights.
$output .= '<h3> All Weights </h3>';
$header = array('Date', 'Value', 'Units');
$output .= '<h3>' . t('All weights') . '</h3>';
$header = array(t('Date'), t('Value'), t('Units'));
$table_data = array();
foreach ($logs as $log) {
// Extract quantity data from the log.
$data = farm_quantity_log_data($log, 'weight');
foreach($weights as $key => $weight) {
// Iterate through the data and return the first one with a value.
foreach ($data as $quantity) {
if (!empty($quantity['value'])) {
$value = $quantity['value'];
$units = $quantity['units'];
// Save the log.
$log = $weight['log'];
// Save the timestamp with data for the graph.
$quantity['timestamp'] = $log->timestamp;
// Save the timestamp to the weights array for graphing.
$weights[$key]['timestamp'] = $log->timestamp;
// Add to $log_weights array.
$log_weights[] = $quantity;
$table_data[] = array(format_date($log->timestamp), $value, $units);
}
}
// 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));
// Create the div to hold report graphs.
@ -69,9 +69,9 @@ function farm_livestock_weight_individual_report(FarmAsset $farm_asset) {
// Create a Weight Report Graph
$graph = array(
'name' => $farm_asset->name . ' Weight Report',
'name' => htmlspecialchars(entity_label('farm_asset', $farm_asset)) . ' ' . t('Weight report'),
'id' => 'farm-report-weight-graph',
'data' => $log_weights,
'data' => $weights,
);
$graph_markup[] = '<div id="farm-report-weight-graph" class="farm-report-graph"></div>';
$graphs[] = $graph;