You are here

function farm_livestock_weight_dlwg in farmOS 7

Helper function for retrieving the latest daily liveweight gain for an animal

Parameters

FarmAsset $asset: The animal asset to get daily liveweight for.

Return value

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.

2 calls to farm_livestock_weight_dlwg()
farm_livestock_weight_entity_view_alter in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module
Implements hook_entity_view_alter().
farm_livestock_weight_individual_report in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.report.inc
Asset Report view callback.

File

modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module, line 104
Farm livestock weight module.

Code

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;
}