You are here

function _fivestar_field_property_values in Fivestar 7.2

Callback for getting field property values.

1 string reference to '_fivestar_field_property_values'
fivestar_property_info_callback in includes/fivestar.field.inc
Callback to alter the property info of fivestar fields.

File

includes/fivestar.field.inc, line 816
Provides CCK integration for fivestar module.

Code

function _fivestar_field_property_values($entity, array $options, $name, $entity_type, $info) {
  $field = field_info_field($name);
  $langcode = field_language($entity_type, $entity, $name, isset($options['language']) ? $options['language']->language : NULL);
  $values = array();
  if (isset($entity->{$name}[$langcode])) {
    foreach ($entity->{$name}[$langcode] as $delta => $data) {
      $values[$delta]['user_rating'] = isset($data['rating']) ? $data['rating'] : NULL;
      $result = array();
      $result = votingapi_select_results(array(
        'entity_type' => $entity_type,
        'entity_id' => entity_id($entity_type, $entity),
        'value_type' => 'percent',
        'tag' => $field['settings']['axis'],
        'function' => 'average',
      ));
      $values[$delta]['average_rating'] = isset($result[0]['value']) ? $result[0]['value'] : 0;
    }
  }

  // For an empty single-valued field, we have to return NULL.
  return $field['cardinality'] == 1 ? $values ? reset($values) : NULL : $values;
}