'), $form);
$form = preg_replace('/( method=".*?")|( action=".*?")|(
)/', '', $form);
return $form;
}
function theme_fivestar_preview_wrapper($content, $type = 'direct') {
return '
'. $content .'
';
}
/**
* Callback function for fivestar/vote.
*
* @param type
* A content-type to log the vote to. 'node' is the most common.
* @param cid
* A content id to log the vote to. This would be a node ID, a comment ID, etc.
* @param value
* A value from 1-100, representing the vote cast for the content.
* @return
* An XML chunk containing the results of the vote, for use by the client-side
* javascript code.
*/
function fivestar_vote($type, $cid, $value, $tag = 'vote') {
$result = _fivestar_cast_vote($type, $cid, $value, $tag, NULL, TRUE);
if ($type == 'node') {
$node = node_load($cid);
}
$stars = variable_get('fivestar_stars_'. (!isset($node) ? 'default' : $node->type), 5);
$feedback_enable = variable_get('fivestar_feedback_'. (!isset($node) ? 'default' : $node->type), 1);
$output = '';
$output .= '';
$output .= '
';
if (count($result)) {
foreach ($result as $data) {
if ($data['tag'] == $tag) {
$output .= '<'. $data['function'] .'>'. $data['value'] .''. $data['function'] .'>';
$summary[$data['tag']][$data['function']] = $data['value'];
}
}
}
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
$output .= ''. $value .'';
$output .= ''. $type .'';
$output .= ''. $cid .'';
$output .= '';
drupal_set_header("Content-Type: text/xml");
exit($output);
}
/**
* Internal function to handle vote casting, flood control, XSS, IP based
* voting, etc...
*/
function _fivestar_cast_vote($type, $cid, $value, $tag = NULL, $uid = NULL, $result = FALSE) {
global $user;
$tag = empty($tag) ? 'vote' : $tag;
$uid = empty($uid) ? $user->uid : $uid;
// Bail out if the user's trying to vote on an invalid object.
if (!_fivestar_validate_target($type, $cid)) {
return array();
}
// Sanity-check the incoming values.
if (is_numeric($cid) && is_numeric($value)) {
if ($value > 100) {
$value = 100;
}
// Get the user's current vote.
$criteria = array('content_type' => $type, 'content_id' => $cid, 'tag' => $tag);
$user_votes = votingapi_select_votes($criteria += array('uid' => $uid));
if ($value == 0) {
votingapi_delete_votes($user_votes);
}
else {
$votes = $criteria += array('value' => $value, 'uid' => $uid);
votingapi_set_votes($votes, $user_votes);
return fivestar_get_votes($type, $cid, $tag, $uid);
unset($votes['user']);
return $votes;
}
}
}
function fivestar_get_votes($type, $cid, $tag = 'vote', $uid = NULL) {
global $user;
if (empty($uid)) {
$uid = $user->uid;
}
$criteria = array(
'content_type' => $type,
'content_id' => $cid,
'value_type' => 'percent',
'tag' => $tag,
);
$votes = array(
'average' => array(),
'count' => array(),
'user' => array(),
);
$results = votingapi_select_results($criteria);
foreach ($results as $result) {
if ($result['function'] == 'average') {
$votes['average'] = $result;
}
if ($result['function'] == 'count') {
$votes['count'] = $result;
}
}
if ($user->uid) {
$user_vote = votingapi_select_votes($criteria += array('uid' => $user->uid));
if ($user_vote) {
$votes['user'] = $user_vote[0];
$votes['user']['function'] = 'user';
}
}
else {
// If the user is anonymous, we never bother loading their existing votes.
// Not only would it be hit-or-miss, it would break page caching. Safer to always
// show the 'fresh' version to anon users.
$votes['user'] = array('value' => 0);
}
return $votes;
}
/**
* Internal function to check that node-type accepts votes
*
* @param text $type type of target (currently only node is supported)
* @param text $id identifier within the type (in this case node-type)
*
* @return boolean
*/
function _fivestar_validate_target($type, $id) {
switch ($type) {
case 'node':
if ($node = node_load($id)) {
if (variable_get('fivestar_'. $node->type, 0)) {
return TRUE;
}
else {
return FALSE;
}
}
else {
return FALSE;
}
default:
return FALSE;
}
}
/**
* Implementation of hook_fivestar_widgets.
*
* This hook allows other modules to create additional custom widgets for
* the fivestar module.
*
* @return array
* An array of key => value pairs suitable for inclusion as the #options in a
* select or radios form element. Each key must be the location of a css
* file for a fivestar widget. Each value should be the name of the widget.
*/
function fivestar_fivestar_widgets() {
$widgets_directory = drupal_get_path('module', 'fivestar') .'/widgets';
$files = file_scan_directory($widgets_directory, '\.css$');
$widgets = array();
foreach ($files as $file) {
if (strpos($file->filename, '-rtl.css') === FALSE) {
$widgets[$file->filename] = drupal_ucfirst(str_replace('-color', '', $file->name));
}
}
return $widgets;
}
/**
* Implementation of hook_nodeapi()
*
* Adds the fievestar widget to the node view.
*/
function fivestar_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
if ($node->build_mode != NODE_BUILD_PREVIEW && !isset($node->modr8_form_teaser) && variable_get('fivestar_'. $node->type, 0)) {
if ($teaser) {
$position = variable_get('fivestar_position_teaser_'. $node->type, 'above');
}
else {
$position = variable_get('fivestar_position_'. $node->type, 'above');
}
switch ($position) {
case 'above':
case 'below':
if (user_access('rate content')) {
$node->content['fivestar_widget'] = array(
'#value' => fivestar_widget_form($node),
'#weight' => $position == 'above' ? -10 : 50,
);
break;
} // Fall through to static if not allowed to rate.
case 'above_static':
case 'below_static':
$stars = variable_get('fivestar_stars_'. $node->type, 5);
$node->content['fivestar_widget'] = array(
'#value' => fivestar_static('node', $node->nid, $node->type),
'#weight' => $position == 'above_static' ? -10 : 50,
);
break;
default:
// We'll do nothing.
break;
}
}
break;
}
}
/**
* Implementation of hook_link()
*
* Add a "rate" link to node teaser.
*/
function fivestar_link($type, $node = NULL, $teaser = FALSE) {
$links = array();
if ($type == "node" && $teaser) {
if (variable_get('fivestar_position_teaser_'. $node->type, 'above') == "link") {
$links['rate'] = array(
'title' => t('Rate'),
'href' => 'node/'. $node->nid,
'fragment' => 'fivestar-form-node-'. $node->nid,
'attributes' => array('title' => t('Rate this @type', array('@type', node_get_types('name', $node->type)))),
);
}
}
return $links;
}
function fivestar_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Fivestar: Rate this node');
return $blocks;
case 'view':
if (user_access('access content') && user_access('rate content')) {
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == '' || arg(2) == 'view')) {
$node = node_load(arg(1));
if (_fivestar_validate_target('node', $node->nid)) {
$block['subject'] = t('Rate This');
$block['content'] = fivestar_widget_form($node);
}
return $block;
}
}
break;
}
}
function fivestar_widget_form($node) {
return drupal_get_form('fivestar_form_node_'. $node->nid, 'node', $node->nid);
}
/**
* Implementation of hook_forms. This is necessary when multiple fivestar
* forms appear on the same page, each requiring a separate form_id, but all
* using the same underlying callbacks.
*/
function fivestar_forms($form_id, $args) {
if (strpos($form_id, 'fivestar_form') !== FALSE) {
if ($form_id == 'fivestar_form_'. $args[0] .'_'. $args[1]) {
$forms[$form_id] = array('callback' => 'fivestar_form');
return $forms;
}
}
}
/**
* Create the fivestar form for the current item.
* Note that this is not an implementation of hook_form(). We should probably
* change the function to reflect that.
*/
function fivestar_form(&$form_state, $content_type, $content_id) {
global $user;
if ($content_type == 'node') {
if (is_numeric($content_id)) {
$node = node_load($content_id);
}
else {
return array();
}
}
$votes = fivestar_get_votes($content_type, $content_id);
$values = array(
'user' => $votes['user']['value'],
'average' => $votes['average']['value'],
'count' => $votes['count']['value'],
);
$settings = array(
'stars' => variable_get('fivestar_stars_'. $node->type, 5),
'allow_clear' => variable_get('fivestar_unvote_'. $node->type, FALSE),
'style' => variable_get('fivestar_style_'. $node->type, 'average'),
'text' => variable_get('fivestar_text_'. $node->type, 'combo'),
'content_type' => $content_type,
'content_id' => $content_id,
'autosubmit' => TRUE,
'title' => variable_get('fivestar_title_'. $node->type, 1) ? NULL : FALSE,
'feedback_enable' => variable_get('fivestar_feedback_'. $node->type, 1),
'labels_enable' => variable_get('fivestar_labels_enable_'. $node->type, 1),
'labels' => variable_get('fivestar_labels_'. $node->type, array()),
);
return fivestar_custom_widget($form_state, $values, $settings);
}
function fivestar_static($content_type, $content_id, $node_type = NULL) {
global $user;
$criteria = array(
'content_type' => $content_type,
'content_id' => $content_id,
'value_type' => 'percent',
'tag' => 'vote',
);
$votes = fivestar_get_votes($content_type, $content_id);
if ($content_type == 'node') {
// Content type should always be passed to avoid this node load.
if (!isset($node_type)) {
$node = node_load($content_id);
$node_type = $node->type;
}
$star_display = variable_get('fivestar_style_'. $node_type, 'average');
$text_display = variable_get('fivestar_text_'. $node_type, 'dual');
$title_display = variable_get('fivestar_title_'. $node_type, 1);
$stars = variable_get('fivestar_stars_'. $node_type, 5);
switch ($star_display) {
case 'average':
case 'dual':
$star_value = $votes['average']['value'];
$title = $title_display ? t('Average') : NULL;
break;
case 'user':
$star_value = $votes['user']['value'];
$title = $title_display ? t('Your rating') : NULL;
break;
case 'smart':
$star_value = $votes['user']['value'] ? $votes['user']['value'] : $votes['average']['value'];
$title = $title_display ? $votes['user']['value'] ? t('Your rating') : t('Average') : NULL;
break;
}
// Set all text values, then unset the unnecessary ones.
$user_value = $votes['user']['value'];
$average_value = $votes['average']['value'];
$count_value = $votes['count']['value'];
switch ($text_display) {
case 'average':
$user_value = NULL;
break;
case 'user':
$average_value = NULL;
break;
case 'smart':
if ($votes['user']['value']) {
$average_value = NULL;
}
else {
$user_value = NULL;
}
break;
}
}
// Possibly add other content types here (comment, user, etc).
else {
$stars = 5;
$star_value = $votes['average']['value'];
$user_value = $votes['user']['value'];
$average_value = $votes['average']['value'];
$count_value = $votes['count']['value'];
}
$star_display = theme('fivestar_static', $star_value, $stars);
$text_display = $text_display == 'none' ? NULL : theme('fivestar_summary', $user_value, $average_value, $count_value, $stars, FALSE);
return theme('fivestar_static_element', $star_display, $title, $text_display);
}
function fivestar_custom_widget(&$form_state, $values, $settings) {
$form = array(
'#attributes' => array('class' => 'fivestar-widget'),
'#redirect' => FALSE,
'#theme' => 'fivestar_widget',
);
$form['#submit'][] = 'fivestar_form_submit';
if (isset($settings['content_type'])) {
$form['content_type'] = array(
'#type' => 'hidden',
'#value' => $settings['content_type'],
'#id' => $settings['content_id'] ? 'edit-content-type-'. $settings['content_id'] : NULL,
);
}
if (isset($settings['content_id'])) {
$form['content_id'] = array(
'#type' => 'hidden',
'#value' => $settings['content_id'],
'#id' => $settings['content_id'] ? 'edit-content-id-'. $settings['content_id'] : NULL,
);
}
$form['vote'] = array(
'#type' => 'fivestar',
'#stars' => $settings['stars'],
'#vote_count' => $values['count'],
'#vote_average' => $values['average'],
'#auto_submit' => isset($settings['autosubmit']) ? $settings['autosubmit'] : TRUE,
'#auto_submit_path' => (!isset($settings['autosubmit']) || $settings['autosubmit']) ? 'fivestar/vote/'. $settings['content_type'] .'/'. $settings['content_id'] : NULL,
'#allow_clear' => $settings['allow_clear'],
'#content_id' => isset($settings['content_id']) ? $settings['content_id'] : NULL,
'#required' => isset($settings['required']) ? $settings['required'] : FALSE,
'#feedback_enable' => isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE,
'#labels_enable' => isset($settings['labels_enable']) ? $settings['labels_enable'] : TRUE,
'#labels' => isset($settings['labels']) ? $settings['labels'] : NULL,
);
$form['destination'] = array(
'#type' => 'hidden',
'#value' => $_GET['q'],
'#id' => isset($settings['content_id']) ? 'edit-destination-'. $settings['content_id'] : NULL,
);
$form['fivestar_submit'] = array(
'#type' => 'submit',
'#value' => t('Rate'),
'#attributes' => array('class' => 'fivestar-submit'),
'#id' => isset($settings['content_id']) ? 'edit-fivestar-submit-'. $settings['content_id'] : NULL,
);
$form['vote']['#attributes']['class'] = isset($form['vote']['#attributes']['class']) ? $form['vote']['#attributes']['class'] : '';
$settings['feedback_enable'] = isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE;
switch ($settings['text']) {
case 'user':
$form['vote']['#description'] = theme('fivestar_summary', $values['user'], NULL, $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-user-text';
break;
case 'average':
$form['vote']['#description'] = $settings['style'] == 'dual' ? NULL : theme('fivestar_summary', NULL, $values['average'], $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-average-text';
break;
case 'smart':
$form['vote']['#description'] = ($settings['style'] == 'dual' && !$values['user']) ? NULL : theme('fivestar_summary', $values['user'], $values['user'] ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-smart-text '. ($values['user'] ? 'fivestar-user-text' : 'fivestar-average-text');
break;
case 'dual':
$form['vote']['#description'] = theme('fivestar_summary', $values['user'], $settings['style'] == 'dual' ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-combo-text';
break;
}
switch ($settings['style']) {
case 'average':
$form['vote']['#title'] = t('Average');
$form['vote']['#default_value'] = $values['average'];
$form['vote']['#attributes']['class'] .= ' fivestar-average-stars';
break;
case 'user':
$form['vote']['#title'] = t('Your rating');
$form['vote']['#default_value'] = $values['user'];
$form['vote']['#attributes']['class'] .= ' fivestar-user-stars';
break;
case 'smart':
$form['vote']['#title'] = $values['user'] ? t('Your rating') : t('Average');
$form['vote']['#default_value'] = $values['user'] ? $values['user'] : $values['average'];
$form['vote']['#attributes']['class'] .= ' fivestar-smart-stars '. ($values['user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
break;
case 'dual':
$form['vote']['#title'] = t('Your rating');
$form['vote']['#default_value'] = $values['user'];
$form['vote']['#attributes']['class'] .= ' fivestar-combo-stars';
$form['#attributes']['class'] .= ' fivestar-combo-stars';
$static_average = theme('fivestar_static', $values['average'], $settings['stars']);
if ($settings['text'] == 'none' && !$settings['labels_enable'] && !$settings['feedback_enable']) {
$static_description = NULL;
}
elseif ($settings['text'] != 'none') {
$static_description = theme('fivestar_summary', NULL, $settings['text'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0), isset($values['count']) ? $values['count'] : 0, $settings['stars'], FALSE);
}
else {
$static_description = ' ';
}
$form['average'] = array(
'#type' => 'markup',
'#value' => theme('fivestar_static_element', $static_average, $settings['title'] !== FALSE ? t('Average') : NULL, $static_description),
'#weight' => -1,
);
break;
}
// Set an over-ridding title if passed in.
// An empty title won't change the default, a string will set a new title,
// and title === FALSE will unset the title entirely.
if (isset($settings['title'])) {
if ($settings['title'] !== FALSE) {
$form['vote']['#title'] = $settings['title'];
}
else {
unset($form['vote']['#title']);
unset($form['average']['#title']);
}
}
return $form;
}
/**
* Submit handler for the above form (non-javascript version).
*/
function fivestar_form_submit($form, &$form_state) {
if ($form_state['values']['form_id'] == 'fivestar_form_'. $form_state['values']['content_type'] .'_'. $form_state['values']['content_id']) {
// Cast the vote.
_fivestar_cast_vote($form_state['values']['content_type'], $form_state['values']['content_id'], $form_state['values']['vote']);
// Set a message that the vote was received.
if ($form_state['values']['vote'] === '0') {
drupal_set_message(t('Your vote has been cleared.'));
}
elseif (is_numeric($form_state['values']['vote'])) {
drupal_set_message(t('Thank you for your vote.'));
}
// Regenerate the page with a drupal_goto() to update the current values.
drupal_goto();
}
}
/**
* Implementation of hook_elements
*
* Defines 'fivestar' form element type
*/
function fivestar_elements() {
$type['fivestar'] = array(
'#input' => TRUE,
'#stars' => 5,
'#widget' => 'stars',
'#allow_clear' => FALSE,
'#auto_submit' => FALSE,
'#auto_submit_path' => '',
'#labels_enable' => TRUE,
'#feedback_enable' => TRUE,
'#process' => array('fivestar_expand'),
);
return $type;
}
/**
* Theme the fivestar form element by adding necessary css and javascript.
*/
function theme_fivestar($element) {
// Add necessary CSS.
fivestar_add_css();
// Add necessary javascript.
if ($element['#widget'] == 'stars' ) {
fivestar_add_js();
}
if (empty($element['#description'])) {
if ($element['#feedback_enable']) {
$element['#description'] = '
';
}
elseif ($element['#labels_enable']) {
$element['#description'] = '
';
}
}
return theme('form_element', $element, $element['#children']);
}
/**
* Theme the straight HTML version of the fivestar select list. This is used
* to remove the wrapping 'form-item' div from the select list.
*/
function theme_fivestar_select($element) {
$select = '';
$size = $element['#size'] ? ' size="'. $element['#size'] .'"' : '';
_form_set_class($element, array('form-select'));
$multiple = isset($element['#multiple']) && $element['#multiple'];
return '
';
}
/**
* Theme an entire fivestar widget, including the submit button and the normal
* fivestar widget themed in the theme_fivestar() function.
*/
function theme_fivestar_widget($form) {
// Only print out the summary if text is being displayed or using rollover text.
if (empty($form['vote']['#description']) && strpos($form['vote']['#prefix'], 'fivestar-labels-hover') === FALSE) {
unset($form['vote']['#description']);
}
$output = '';
$output .= '
';
$output .= drupal_render($form);
$output .= '
';
return $output;
}
/**
* Display a plain HTML VIEW ONLY version of the widget
* with the specified rating
*
* @param $rating
* The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars)
* @param $stars
* The total number of stars this rating is out of
* @return
* A themed HTML string representing the star widget
*
*/
function theme_fivestar_static($rating, $stars = 5) {
// Add necessary CSS.
fivestar_add_css();
$output = '';
$output .= '
';
return $output;
}
function theme_fivestar_summary($user_rating, $average_rating, $votes, $stars = 5, $feedback = TRUE) {
$output = '';
if (isset($user_rating)) {
$div_class = isset($votes) ? 'user-count' : 'user';
$user_stars = round(($user_rating * $stars) / 100, 1);
$output .= '
'. t('Your rating: !stars', array('!stars' => $user_rating ? $user_stars : t('None'))) .'';
}
if (isset($user_rating) && isset($average_rating)) {
$output .= ' ';
}
if (isset($average_rating)) {
$div_class = isset($votes) ? 'average-count' : 'average';
$average_stars = round(($average_rating * $stars) / 100, 1);
$output .= '
'. t('Average: !stars', array('!stars' => $average_stars)) .'';
}
if (isset($user_rating) && isset($average_rating)) {
$div_class = 'combo';
}
if (isset($votes) && !(isset($user_rating) || isset($average_rating))) {
$output .= '
'. format_plural($votes, '1 vote', '@count votes') .'';
$div_class = 'count';
}
elseif (isset($votes)) {
$output .= '
('. format_plural($votes, '1 vote', '@count votes') .')';
}
if ($votes === 0) {
$output = '
'. t('No votes yet') .'';
}
$output = '
'. $output .'
';
return $output;
}
/**
* Display a static fivestar value as stars with a title and description.
*/
function theme_fivestar_static_element($value, $title = NULL, $description = NULL) {
$output = '';
$output .= '
';
$element = array(
'#type' => 'item',
'#title' => $title,
'#description' => $description,
);
$output .= theme('form_element', $element, $value);
$output .= '
';
return $output;
}
/**
* Fetch the necessary CSS files to render the fivestar widget.
*/
function fivestar_add_css($widget_css = NULL) {
// Add fivestar CSS.
drupal_add_css(drupal_get_path('module', 'fivestar') .'/css/fivestar.css');
// Add widget specific CSS.
if (!isset($widget_css)) {
$widget_css = variable_get('fivestar_widget', 'default');
}
if ($widget_css != 'default') {
drupal_add_css($widget_css);
}
}
/**
* Add necessary JS files and settings to render the fivestar widget.
*/
function fivestar_add_js() {
static $js_added = FALSE;
// Add necessary javascript only once per page.
if (!$js_added) {
$settings = array(
'titleUser' => t('Your rating') .': ',
'titleAverage' => t('Average') .': ',
'feedbackSavingVote' => t('Saving your vote...'),
'feedbackVoteSaved' => t('Your vote has been saved.'),
'feedbackDeletingVote' => t('Deleting your vote...'),
'feedbackVoteDeleted' => t('Your vote has been deleted.'),
);
drupal_add_js(drupal_get_path('module', 'fivestar') .'/js/fivestar.js');
drupal_add_js(array('fivestar' => $settings), 'setting');
$js_added = TRUE;
}
}
/**
* Process callback for fivestar_element -- see fivestar_element()
*/
function fivestar_expand($element) {
static $fivestar_id = 0;
if (isset($element['#vote_count'])) {
$element['vote_count'] = array(
'#type' => 'hidden',
'#value' => $element['#vote_count'],
'#id' => 'edit-vote-count-'. $fivestar_id,
);
}
if (isset($element['#vote_average'])) {
$element['vote_average'] = array(
'#type' => 'hidden',
'#value' => $element['#vote_average'],
'#id' => 'edit-vote-average-'. $fivestar_id,
);
}
if ($element['#auto_submit'] && !empty($element['#auto_submit_path'])) {
$element['auto_submit_path'] = array(
'#type' => 'hidden',
'#value' => url($element['#auto_submit_path']),
'#attributes' => array('class' => 'fivestar-path'),
'#id' => 'edit-auto-submit-path-'. $fivestar_id,
);
}
if (!isset($element['#default_value'])) {
$element['#default_value'] = 0;
}
$options = array('-' => t('Select rating'));
$default_value = $element['#default_value'];
for ($i = 0; $i <= $element['#stars']; $i++) {
$this_value = ceil($i * 100/$element['#stars']);
$next_value = ceil(($i+1) * 100/$element['#stars']);
// Display clear button only if enabled.
if ($element['#allow_clear'] == TRUE && $i == 0) {
$options[$this_value] = isset($element['#labels'][$i]) ? t(filter_xss_admin($element['#labels'][$i])) : t('Cancel rating');
}
// Display a normal star value.
if ($i > 0) {
if (isset($element['#labels'][$i])) {
$options[$this_value] = $element['#labels'][$i] == '' ? $i : t(filter_xss_admin($element['#labels'][$i]), array('@star' => $i, '@count' => $element['#stars']));
}
else {
$options[$this_value] = t('Give it @star/@count', array('@star' => $i, '@count' => $element['#stars']));
}
}
// Round up the default value to the next exact star value if needed.
if ($this_value < $element['#default_value'] && $next_value > $element['#default_value']) {
$default_value = $next_value;
}
}
$element['vote'] = array(
'#type' => 'select',
'#options' => $options,
'#required' => $element['#required'],
'#default_value' => $default_value,
'#parents' => $element['#parents'],
'#id' => 'edit-vote-'. $fivestar_id,
'#theme' => 'fivestar_select',
'#weight' => $element['#weight'],
);
// If a default value is not exactly on a radio value, round up to the next one
if ($element['#default_value'] > $this_value && $element['#default_value'] <= $next_value) {
$element['vote']['#default_value'] = $next_value;
}
// Set a class for the display of label text on hover.
$label_class = $element['#labels_enable'] ? ' fivestar-labels-hover' : '';
$element['#id'] = 'edit-vote-'. $fivestar_id;
$element['#prefix'] = '
';
$element['#suffix'] = '
';
// Add validation function that considers a 0 value as empty.
$element['#validate']['fivestar_validate'] = array();
$fivestar_id++;
return $element;
}
function fivestar_validate($form, &$form_state) {
if ($form['#required'] && (empty($form_state['values']['vote']) || $form_state['values']['vote'] == '-')) {
form_error($form, t('!name field is required.', array('!name' => $form['#title'])));
}
}
function fivestar_votingapi_views_formatters($details = array()) {
if ($details['value_type'] == 'percent') {
return array(
'fivestar_views_value_display_handler' => t('Fivestar Stars (display only)'),
'fivestar_views_widget_compact_handler' => t('Fivestar Stars (clickable, no text)'),
'fivestar_views_widget_normal_handler' => t('Fivestar Stars (clickable, with text)'),
);
}
}
function fivestar_views_value_display_handler($op, $filter, $value, &$query) {
// Determine number of stars to display
if (is_numeric($query->options)) {
$stars = $query->options;
}
elseif (isset($query->node_type)) {
$stars = variable_get('fivestar_stars_'. $query->node_type, 5);
}
else {
$type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $query->nid));
$stars = variable_get('fivestar_stars_'. (!isset($type) ? 'default' : $type), 5);
}
return theme('fivestar_static', $value, $stars);
}
function fivestar_views_widget_compact_handler($op, $filter, $value, &$query) {
return fivestar_views_widget_handler($op, $filter, $value, $query, FALSE);
}
function fivestar_views_widget_normal_handler($op, $filter, $value, &$query) {
return fivestar_views_widget_handler($op, $filter, $value, $query, TRUE);
}
function fivestar_views_widget_handler($op, $filter, $value, &$query, $summary) {
$content_type = 'node';
$content_id = $query->nid;
$node_type = isset($query->node_type) ? $query->node_type : db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $query->nid));
$votes = fivestar_get_votes($content_type, $content_id);
$values = array(
'user' => $votes['user']['value'],
'average' => (int)$value,
'count' => $votes['count']['value'],
);
$settings = array(
'stars' => variable_get('fivestar_stars_'. $node_type, 5),
'allow_clear' => variable_get('fivestar_unvote_'. $node_type, FALSE),
// If the user has setup this content type to use smart stars, display
// the smart version instead of just the average.
'style' => variable_get('fivestar_style_'. $node_type, 'average') != 'smart' ? 'average' : 'smart',
'text' => $summary ? variable_get('fivestar_text_'. $node_type, 'combo') : 'none',
'content_type' => $content_type,
'content_id' => $content_id,
'autosubmit' => TRUE,
'title' => FALSE,
'feedback_enable' => $summary ? variable_get('fivestar_feedback_'. $node_type, 1) : FALSE,
'labels_enable' => $summary ? variable_get('fivestar_labels_enable_'. $node_type, 1) : FALSE,
'labels' => $summary ? variable_get('fivestar_labels_'. $node_type, array()) : array(),
);
return drupal_get_form('fivestar_custom_widget', $values, $settings);
}