fivestar_expand

  1. drupal
    1. 6 fivestar.module function
    2. 7 fivestar.module function
Drupal 7 fivestar_expand($element)

Process callback for fivestar_element -- see fivestar_element()

1 call to fivestar_expand()

1 string reference to 'fivestar_expand'

File

sites/all/modules/fivestar/fivestar.module, line 470
A simple n-star voting widget, usable in other forms.

Code

function fivestar_expand($element) {
  // Add CSS and JS
  $path = drupal_get_path('module', 'fivestar');
  $element['#attached']['js'][] = $path . '/js/fivestar.js';
  $element['#attached']['css'][] = $path . '/css/fivestar.css';
  $settings = $element['#settings'];
  $values = $element['#values'];
  $class[] = 'clearfix';

  $options = array('-' => t('Select rating'));
  for ($i = 1; $i <= $element['#stars']; $i++) {
    $this_value = ceil($i * 100 / $element['#stars']);
    $options[$this_value] = t('Give it @star/@count', array('@star' => $i, '@count' => $element['#stars']));
  }
  // Display clear button only if enabled.
  if ($element['#allow_clear'] == TRUE) {
    $options[0] = t('Cancel rating');
  }

  $element['vote'] = array(
    '#type' => 'select', 
    '#options' => $options, 
    '#required' => $element['#required'], 
    '#theme' => 'fivestar_select', 
    '#default_value' => _fivestar_get_element_default_value($element),
  );

  if (isset($element['#parents'])) {
    $element['vote']['#parents'] = $element['#parents'];
  }
  if (isset($element['#weight'])) {
    $element['vote']['#weight'] = $element['#weight'];
  }

  switch ($settings['text']) {
    case 'user':
      $element['vote']['#description'] = theme('fivestar_summary', array(
        'user_rating' => $values['user'], 
        'votes' => $settings['style'] == 'dual' ? NULL : $values['count'], 
        'stars' => $settings['stars'],
      ));
      $class[] = 'fivestar-user-text';
      break;
    case 'average':
      $element['vote']['#description'] = $settings['style'] == 'dual' ? NULL : theme('fivestar_summary', array(
        'average_rating' => $values['average'], 
        'votes' => $values['count'], 
        'stars' => $settings['stars'],
      ));
      $class[] = 'fivestar-average-text';
      break;
    case 'smart':
      $element['vote']['#description'] = ($settings['style'] == 'dual' && !$values['user']) ? NULL : theme('fivestar_summary', array(
        'user_rating' => $values['user'], 
        'average_rating' => $values['user'] ? NULL : $values['average'], 
        'votes' => $settings['style'] == 'dual' ? NULL : $values['count'], 
        'stars' => $settings['stars'],
      ));
      $class[] = 'fivestar-smart-text';
      $class[] = $values['user'] ? 'fivestar-user-text' : 'fivestar-average-text';
      break;
    case 'dual':
      $element['vote']['#description'] = theme('fivestar_summary', array(
        'user_rating' => $values['user'], 
        'average_rating' => $settings['style'] == 'dual' ? NULL : $values['average'], 
        'votes' => $settings['style'] == 'dual' ? NULL : $values['count'], 
        'stars' => $settings['stars'],
      ));
      $class[] = ' fivestar-combo-text';
      break;
  }

  switch ($settings['style']) {
    case 'average':
      $class[] = 'fivestar-average-stars';
      break;
    case 'user':
      $class[] = 'fivestar-user-stars';
      break;
    case 'smart':
      $class[] = 'fivestar-smart-stars ' . ($values['user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
      break;
    case 'dual':
      $class[] = 'fivestar-combo-stars';
      $static_average = theme('fivestar_static', array(
        'rating' => $values['average'], 
        'stars' => $settings['stars'], 
        'tag' => $settings['tag'], 
        'widget' => $settings['widget'],
      ));
      if ($settings['text'] != 'none') {
        $static_description = theme('fivestar_summary', array(
          'average_rating' => $settings['text'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0), 
          'votes' => isset($values['count']) ? $values['count'] : 0, 
          'stars' => $settings['stars'],
        ));
      }
      else {
        $static_description = '&nbsp;';
      }
      $element['average'] = array(
        '#type' => 'markup', 
        '#markup' => theme('fivestar_static_element', array(
          'star_display' => $static_average, 
          'title' => '', 
          'description' => $static_description,
        )), 
        '#weight' => -1,
      );
      break;
  }
  $class[] = 'fivestar-form-item';
  $class[] = 'fivestar-' . $element['#widget']['name'];
  if ($element['#widget']['name'] != 'default') {
    $element['#attached']['css'][] = $element['#widget']['css'];
  }
  $element['#prefix'] = '<div ' . drupal_attributes(array('class' => $class)) . '>';
  $element['#suffix'] = '</div>';

  // Add AJAX handling if necessary.
  if (!empty($element['#auto_submit'])) {
    $element['vote']['#ajax'] = array(
      'callback' => 'fivestar_ajax_submit',
    );
    $element['vote']['#attached']['js'][] = $path . '/js/fivestar.ajax.js';
  }

  if (empty($element['#input'])) {
    $static_stars = theme('fivestar_static', array(
      'rating' => $element['vote']['#default_value'], 
      'stars' => $settings['stars'], 
      'tag' => $settings['tag'], 
      'widget' => $settings['widget'],
    ));

    $element['vote'] = array(
      '#type' => 'markup', 
      '#markup' => theme('fivestar_static_element', array(
        'star_display' => $static_stars, 
        'title' => '', 
        'description' => $element['vote']['#description'],
      )),
    );
  }

  // Add validation function that considers a 0 value as empty.
  $element['#element_validate'] = array('fivestar_validate');

  return $element;
}