admin/content/types and edit the type you would like to rate.', array('!types' => url('admin/content/types'))),
t('On the settings page for the content type, a set of options is available for fivestar, where you can enable rating for that type and set rating options.'),
);
$output .= theme('item_list', $steps, NULL, 'ol');
}
return $output;
}
/**
* Implementation of hook_menu().
*
* Provides a callback url where votes can be submitted by the client-side
* javascript.
*/
function fivestar_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/fivestar',
'title' => t('Fivestar'),
'description' => t('Configure site-wide widgets used for Fivestar rating.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('fivestar_settings'),
'type' => MENU_NORMAL_ITEM,
'access' => user_access('administer site configuration'),
);
$items[] = array(
'path' => 'fivestar/preview/color',
'callback' => 'fivestar_preview_color',
'type' => MENU_CALLBACK,
'access' => user_access('administer site configuration'),
);
$items[] = array(
'path' => 'fivestar/preview/node',
'callback' => 'fivestar_preview',
'type' => MENU_CALLBACK,
'access' => user_access('administer content types'),
);
$items[] = array(
'path' => 'fivestar/vote',
'callback' => 'fivestar_vote',
'type' => MENU_CALLBACK,
'access' => user_access('rate content'),
);
}
return $items;
}
/**
* Implementation of hook_init().
* Not that this will cause Drupal to post a warning on the admin screen
* when agressive caching is activated. Like CCK, Fivestar's use of hook_init
* IS compatible with agressive caching, we just need a way to annouce that.
*/
function fivestar_init() {
// Ensure we are not serving a cached page.
if (function_exists('drupal_set_content')) {
if (module_exists('content')) {
include_once(drupal_get_path('module', 'fivestar') .'/fivestar_field.inc');
}
}
// Add necessary CSS and JS.
// TODO: These shouldn't be loaded on every page, but block caching omits
// CSS and JS files that would be otherwise added.
fivestar_add_js();
fivestar_add_css();
}
/**
* Implementation of hook_perm().
*
* Exposes permissions for rating content, viewing aggregate ratings, and using PHP
* snippets when configuring fivestar CCK fields.
*/
function fivestar_perm() {
return array('rate content', 'use PHP for fivestar target');
}
/**
* Implementation of hook_form_alter().
*
* Adds fivestar enaable and position to the node-type configuration form.
*/
function fivestar_form_alter($form_id, &$form) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
// Goofy hack to get the buttons at the end of the array.
$form['workflow']['#weight'] = isset($form['workflow']['#weight']) ? $form['workflow']['#weight'] + 1 : 1;
$form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
$form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1;
$form['fivestar'] = array(
'#type' => 'fieldset',
'#title' => t('Fivestar ratings'),
'#collapsible' => TRUE,
'#collapsed' => !variable_get('fivestar_'. $form['#node_type']->type, 0),
'#description' => t('To rate this content, enable Fivestar rating below. These settings will be used for both comments (if available) and direct rating.'),
'#theme' => 'fivestar_node_type_form',
'#attributes' => array('id' => 'fivestar-node-type-form'),
);
$form['fivestar']['fivestar'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Fivestar rating'),
'#default_value' => variable_get('fivestar_'. $form['#node_type']->type, 0),
'#return_value' => 1,
'#weight' => -5,
);
$form['fivestar']['fivestar_stars'] = array(
'#type' => 'select',
'#title' => t('Number of stars'),
'#options' => drupal_map_assoc(range(1, 10)),
'#default_value' => variable_get('fivestar_stars_'. $form['#node_type']->type, 5),
'#weight' => -4,
);
$form['fivestar']['labels'] = array(
'#type' => 'fieldset',
'#title' => t('Star Labels'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('These star labels appear as the link title when javascript is enabled as well as the select list options when javascript is disabled.'),
);
$form['fivestar']['labels']['fivestar_labels_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Display labels on mouse over'),
'#default_value' => variable_get('fivestar_labels_enable_'. $form['#node_type']->type, 1),
'#return_value' => 1,
'#weight' => -5,
'#description' => t('When enabled, the star labels will dynamically appear underneath the stars as the user hovers over each star to provide a more descriptive qualitative meaning for each star value.'),
);
// Create the Mouseover text forms for each of the rating options
// This form depends on the number of stars, and these extra textfields will be hidden with javascript
$star_count = variable_get('fivestar_stars_'. $form['#node_type']->type, 5);
$labels = variable_get('fivestar_labels_'. $form['#node_type']->type, array());
for ($n = 0; $n <= 10; $n++) {
if ($star_count == 5 && $n <= 5) {
// If the default 5 stars are chosen, then use these five default label values.
$default_labels = array(t('Cancel rating'), t('Poor'), t('Okay'), t('Good'), t('Great'), t('Awesome'));
}
elseif ($n == 0) {
$default_labels[$n] = t('Cancel rating');
}
else {
$default_labels[$n] = t('Give it @star/@count');
}
$form['fivestar']['labels']['fivestar_label_'. $n] = array(
'#type' => 'textfield',
'#title' => $n > 0 ? t('Star @star label', array('@star' => $n)) : t('Cancel label'),
'#default_value' => isset($labels[$n]) ? $labels[$n] : $default_labels[$n],
'#prefix' => '
',
'#suffix' => '
',
'#size' => 30,
);
}
$form['fivestar']['direct'] = array(
'#type' => 'fieldset',
'#title' => t('Direct rating widget'),
'#collapsible' => FALSE,
'#description' => t('These settings allow you to display a rating widget to your users while they are viewing content of this type. Rating will immediately register a vote for that piece of content.'),
'#weight' => 2,
);
$form['fivestar']['direct']['fivestar_style'] = array(
'#type' => 'select',
'#title' => t('Star display style'),
'#default_value' => variable_get('fivestar_style_'. $form['#node_type']->type, 'average'),
'#options' => array(
'average' => t('Display average vote value'),
'user' => t('Display user vote value'),
'smart' => t('User vote if available, average otherwise'),
'dual' => t('Both user and average vote'),
),
);
$form['fivestar']['direct']['fivestar_text'] = array(
'#type' => 'select',
'#title' => t('Text display style'),
'#default_value' => variable_get('fivestar_text_'. $form['#node_type']->type, 'dual'),
'#options' => array(
'none' => t('Display no text beneath stars'),
'average' => t('Current average in text'),
'user' => t('User current vote in text'),
'smart' => t('User vote if available, average otherwise'),
'dual' => t('Both user and average vote'),
),
);
$form['fivestar']['direct']['fivestar_title'] = array(
'#type' => 'checkbox',
'#title' => t('Show widget title'),
'#default_value' => variable_get('fivestar_title_'. $form['#node_type']->type, 1),
'#return_value' => 1,
);
$form['fivestar']['direct']['fivestar_feedback'] = array(
'#type' => 'checkbox',
'#title' => t('Enable confirmations to inform a vote was saved or deleted.'),
'#default_value' => variable_get('fivestar_feedback_'. $form['#node_type']->type, 1),
'#return_value' => 1
);
$form['fivestar']['direct']['fivestar_unvote'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to undo their votes'),
'#default_value' => variable_get('fivestar_unvote_'. $form['#node_type']->type, 0),
'#return_value' => 1,
);
$form['fivestar']['direct']['fivestar_position_teaser'] = array(
'#type' => 'select',
'#title' => t('Teaser display'),
'#default_value' => variable_get('fivestar_position_teaser_'. $form['#node_type']->type, 'hidden'),
'#options' => array(
'above' => t('Clickable widget above teaser'),
'below' => t('Clickable widget below teaser'),
'above_static' => t('Static display above teaser'),
'below_static' => t('Static display below teaser'),
'link' => t('Teaser link to full node widget'),
'hidden' => '<'. t('hidden') .'>',
),
);
$form['fivestar']['direct']['fivestar_position'] = array(
'#type' => 'select',
'#title' => t('Full node display'),
'#default_value' => variable_get('fivestar_position_'. $form['#node_type']->type, 'below'),
'#options' => array(
'above' => t('Clickable widget above node body'),
'below' => t('Clickable widget below node body'),
'above_static' => t('Static display above node body'),
'below_static' => t('Static display below node body'),
'hidden' => '<'. t('hidden') .'>',
),
);
$form['fivestar']['direct']['fivestar_direct_preview'] = array(
'#type' => 'item',
'#title' => t('Direct rating widget preview'),
'#value' => theme(
'fivestar_preview',
$form['fivestar']['direct']['fivestar_style']['#default_value'],
$form['fivestar']['direct']['fivestar_text']['#default_value'],
$form['fivestar']['fivestar_stars']['#default_value'],
$form['fivestar']['direct']['fivestar_unvote']['#default_value'],
$form['fivestar']['direct']['fivestar_title']['#default_value'] ? NULL : FALSE,
$form['fivestar']['labels']['fivestar_labels_enable']['#default_value'],
variable_get('fivestar_labels_'. $form['#node_type']->type, array())
),
);
if (!$form['fivestar']['fivestar']['#default_value']) {
$form['fivestar']['direct']['fivestar_direct_preview']['#value'] = theme('fivestar_preview_wrapper', '');
}
else {
$form['fivestar']['direct']['fivestar_direct_preview']['#value'] = theme('fivestar_preview_wrapper', $form['fivestar']['direct']['fivestar_direct_preview']['#value']);
}
$form['#submit']['fivestar_node_type_form_submit'] = array();
}
}
/**
* Additional submit handler for the node type form.
*/
function fivestar_node_type_form_submit($form_id, &$form_values) {
// Do not save any fivestar variables if fivestar is disabled.
if (isset($form_values['fivestar']) && $form_values['fivestar'] === 0) {
foreach ($form_values as $key => $value) {
if (strpos($key, 'fivestar') === 0) {
variable_del($key .'_'. $form_values['type']);
}
}
}
// Merge labels into a single variable.
$labels = array();
for ($n = 0; $n <= 10; $n++) {
$labels[] = $form_values['fivestar_label_'. $n];
variable_del('fivestar_label_'. $n .'_'. $form_values['type']);
}
variable_del('fivestar_labels_'. $form_values['type']);
if ($form_values['fivestar_labels_enable']) {
variable_set('fivestar_labels_'. $form_values['type'], $labels);
}
}
/**
* Theme function to add the Fivestar preview to the node type form.
*/
function theme_fivestar_node_type_form($form) {
drupal_add_js(drupal_get_path('module', 'fivestar') .'/js/fivestar-admin.js');
drupal_add_js(array('fivestar' => array('preview_url' => url('fivestar/preview/node'))), 'setting');
drupal_add_css(drupal_get_path('module', 'fivestar') .'/css/fivestar-admin.css', 'module', 'all', FALSE);
$output = '';
$output .= drupal_render($form['fivestar']);
$output .= drupal_render($form['fivestar_stars']);
// Star labels.
$output .= drupal_render($form['labels']);
// Direct rating settings form.
$direct = '';
$direct .= '