2019-06-25 05:27:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Farm livestock report page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asset Report view callback.
|
|
|
|
*
|
|
|
|
* @param FarmAsset $farm_asset
|
|
|
|
* The farm asset entity.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Returns the page content.
|
|
|
|
*/
|
2019-08-24 22:29:27 +02:00
|
|
|
function farm_livestock_weight_individual_report(FarmAsset $farm_asset) {
|
2019-06-25 05:58:12 +02:00
|
|
|
$output = '';
|
2019-06-25 05:27:01 +02:00
|
|
|
|
|
|
|
// Get the assets current weight.
|
|
|
|
$current_weight = farm_livestock_weight($farm_asset);
|
|
|
|
|
|
|
|
// Check if the asset has a weight recorded.
|
|
|
|
if (empty($current_weight)) {
|
2020-08-28 15:45:06 +02:00
|
|
|
$output .= '<p>' . t('No weight recorded for asset') . '</p>';
|
2020-08-27 14:34:07 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-28 15:45:06 +02:00
|
|
|
$output .= '<p><strong>' . t('Current weight') . ':</strong> ' . $current_weight['value'] . ' ' . $current_weight['units'] . '</p>';
|
2020-07-31 22:55:13 +02:00
|
|
|
|
|
|
|
// Load the daily live weight gain.
|
2020-06-08 20:40:59 +02:00
|
|
|
$dlwg = farm_livestock_weight_dlwg($farm_asset);
|
2020-08-27 14:34:07 +02:00
|
|
|
if (!empty($dlwg)) {
|
2020-07-31 22:55:13 +02:00
|
|
|
// Add the dlwg markup.
|
|
|
|
$output .= $dlwg['markup'];
|
2020-06-08 20:40:59 +02:00
|
|
|
}
|
2019-06-25 05:27:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get all 'weight' logs associated with the asset.
|
2020-07-31 22:50:17 +02:00
|
|
|
$weights = farm_livestock_weight_all($farm_asset);
|
2019-06-25 06:43:11 +02:00
|
|
|
|
2019-06-25 05:27:01 +02:00
|
|
|
// Ensure there are weight logs.
|
2020-07-31 22:50:17 +02:00
|
|
|
if (!empty($weights)) {
|
|
|
|
|
2019-06-25 05:58:12 +02:00
|
|
|
// Simple html table of weights.
|
2020-08-28 15:45:06 +02:00
|
|
|
$output .= '<h3>' . t('All weights') . '</h3>';
|
2020-06-08 20:40:59 +02:00
|
|
|
$header = array(t('Date'), t('Value'), t('Units'));
|
2019-07-20 06:31:57 +02:00
|
|
|
$table_data = array();
|
2019-06-25 05:58:12 +02:00
|
|
|
|
2020-08-27 14:34:07 +02:00
|
|
|
foreach($weights as $key => $weight) {
|
2020-07-31 22:50:17 +02:00
|
|
|
|
|
|
|
// Save the log.
|
|
|
|
$log = $weight['log'];
|
|
|
|
|
|
|
|
// Save the timestamp to the weights array for graphing.
|
|
|
|
$weights[$key]['timestamp'] = $log->timestamp;
|
2019-06-25 05:27:01 +02:00
|
|
|
|
2020-07-31 22:50:17 +02:00
|
|
|
// Add to table data.
|
|
|
|
$table_data[] = array(format_date($log->timestamp), $weight['value'], $weight['units']);
|
2019-06-25 05:27:01 +02:00
|
|
|
}
|
2020-06-08 20:40:59 +02:00
|
|
|
|
2020-07-31 22:50:17 +02:00
|
|
|
// Output the table.
|
2019-07-20 06:31:57 +02:00
|
|
|
$output .= theme('table', array('header' => $header, 'rows' => $table_data));
|
2019-06-25 06:43:11 +02:00
|
|
|
|
|
|
|
// Create the div to hold report graphs.
|
|
|
|
$graph_markup = array();
|
|
|
|
|
|
|
|
// Create a graph object to pass to JS.
|
|
|
|
$graphs = array();
|
|
|
|
|
|
|
|
// Create a Weight Report Graph
|
|
|
|
$graph = array(
|
2020-08-28 15:45:06 +02:00
|
|
|
'name' => htmlspecialchars(entity_label('farm_asset', $farm_asset)) . ' ' . t('Weight report'),
|
2019-06-25 06:43:11 +02:00
|
|
|
'id' => 'farm-report-weight-graph',
|
2020-07-31 22:50:17 +02:00
|
|
|
'data' => $weights,
|
2019-06-25 06:43:11 +02:00
|
|
|
);
|
|
|
|
$graph_markup[] = '<div id="farm-report-weight-graph" class="farm-report-graph"></div>';
|
|
|
|
$graphs[] = $graph;
|
|
|
|
|
|
|
|
$settings = array(
|
|
|
|
'farm_livestock_report' => array(
|
|
|
|
'graphs' => $graphs,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add graphs to output.
|
2019-07-29 18:00:15 +02:00
|
|
|
$output = '<div class="farm-report-graphs">' . implode('', $graph_markup) . '</div>' . $output;
|
2019-06-25 06:43:11 +02:00
|
|
|
|
|
|
|
// Add JS and CSS to build the graphs.
|
|
|
|
drupal_add_js($settings, 'setting');
|
|
|
|
drupal_add_js(drupal_get_path('module', 'farm_livestock_weight') . '/farm_livestock_weight.js');
|
|
|
|
drupal_add_js('https://cdn.plot.ly/plotly-latest.min.js', 'external');
|
|
|
|
drupal_add_css(drupal_get_path('module', 'farm_livestock_weight') . '/farm_livestock_weight.css');
|
2019-06-25 05:27:01 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 05:58:12 +02:00
|
|
|
return $output;
|
2019-06-25 05:27:01 +02:00
|
|
|
}
|