array( 'arguments' => array('comment' => NULL, 'fivestar' => NULL), ), ); } /** * Form alter specification for comments. */ function fivestar_comment_form_alter(&$form, &$form_state, $form_id) { // Comment settings. if ($form_id == 'fivestar_node_type_tag_form') { $tag = $form_state['fivestar_tag']; $type_name = $form_state['fivestar_node_type']; $suffix = fivestar_get_suffix($type_name, $tag); $form['comment'] = array( '#type' => 'fieldset', '#title' => t('Comment widget'), '#description' => t('Enabling Fivestar for comments will display a rating widget when a user posts a comment. The rating of the comment will affect its parent content.'), '#weight' => 1, ); $form['comment']['fivestar_comment'] = array( '#type' => 'radios', '#title' => t('Fivestar comment settings'), '#options' => array( FIVESTAR_COMMENT_DISABLED => t('Disabled'), FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'), FIVESTAR_COMMENT_REQUIRED => t('Required rating'), ), '#default_value' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED), ); $form['comment']['fivestar_comment_preview'] = array( '#type' => 'item', '#title' => t('Comment widget preview'), '#value' => theme('fivestar_preview', 'compact', 'none', $form['fivestar_stars']['#default_value'], $form['comment']['fivestar_comment']['#default_value'] == 1 ? 1 : 0), ); if (!$form['fivestar']['#default_value'] || !$form['comment']['fivestar_comment']['#default_value']) { $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', '', 'comment'); } else { $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['comment']['fivestar_comment_preview']['#value'], 'comment'); } } // Comment form. Do not allow ratings inside of threads. if ($form_id == 'comment_form' && empty($form['pid']['#value']) && user_access('rate content')) { $node = node_load($form['nid']['#value']); // Splice in the fivestar right before the body. $new_form = array(); foreach ($form as $key => $element) { if ($key == 'comment_filter') { foreach (fivestar_get_tags() as $tag) { if ($form['cid']['#value']) { $current_rating = fivestar_comment_load($form['cid']['#value'], $form['nid']['#value']); $default_value = $current_rating[$tag]['value']; } else { $votes = fivestar_get_votes('node', $form['nid']['#value'], $tag); $default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0; } if (fivestar_validate_target('node', $node->nid, $tag)) { $suffix = fivestar_get_suffix($node->type, $tag); if (variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED)) { $new_form['fivestar_rating']['fivestar_rating_tag_'. $tag] = array( '#type' => 'fivestar', '#title' => t($tag), '#stars' => variable_get('fivestar_stars_'. $node->type, 5), '#allow_clear' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_OPTIONAL ? 1 : 0, '#content_id' => $node->nid, '#required' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_REQUIRED ? 1 : 0, '#default_value' => $default_value, '#labels' => variable_get('fivestar_labels_'. $suffix, array()), ); } } } } $new_form[$key] = $element; } if ($new_form['fivestar_rating']) { $form = $new_form; } } } /** * Implementation of hook_comment(). */ function fivestar_comment(&$comment, $op) { // Performance tweak don't do any processing on validate or publish if ($op == 'validate' || $op == 'publish') { return; } if (is_array($comment) && is_numeric($comment['nid'])) { $nid = $comment['nid']; } elseif (is_array($comment) && is_array($comment['nid']) && is_numeric($comment['nid']['#value'])) { $nid = $comment['nid']['#value']; } elseif (is_object($comment) && is_numeric($comment->nid)) { $nid = $comment->nid; } if (isset($nid)) { $node = node_load($nid); } switch ($op) { case 'view': foreach (fivestar_get_tags() as $tag) { $suffix = fivestar_get_suffix($node->type, $tag); $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED); if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) { $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag; if (!isset($comment->$fivestar_rating_tag)) { $current_rating = fivestar_comment_load($comment->cid, $comment->nid); $comment->$fivestar_rating_tag = isset($current_rating[$tag]['value']) ? $current_rating[$tag]['value'] : NULL; } $comment->$fivestar_rating_tag = $comment->$fivestar_rating_tag; $comment->fivestar_view .= theme('fivestar_static', $comment->$fivestar_rating_tag, variable_get('fivestar_stars_'. $node->type, 5)); } } $comment->comment = theme('fivestar_comment_view', $comment->comment, $comment->fivestar_view); break; case 'insert': foreach (fivestar_get_tags() as $tag) { $suffix = fivestar_get_suffix($node->type, $tag); $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED); if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) { $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag; $comment = (object)$comment; // Comment module is inconsistent about comment data structures. if ($comment->$fivestar_rating_tag) { fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag); } $comment = (array)$comment; } } case 'update': foreach (fivestar_get_tags() as $tag) { $suffix = fivestar_get_suffix($node->type, $tag); $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED); if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) { $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag; $comment = (object)$comment; // Comment module is inconsistent about comment data structures. $current_rating = fivestar_comment_load($comment->cid, $comment->nid); if ($comment->$fivestar_rating_tag) { if (isset($current_rating[$tag]['value'])) { fivestar_comment_update($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag); } else { fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag); } } elseif ($fivestar_status != FIVESTAR_COMMENT_DISABLED && isset($current_rating[$tag]['vote_id'])) { $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']); } $comment = (array)$comment; } } fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion); break; case 'delete': foreach (fivestar_get_tags() as $tag) { $current_rating = fivestar_comment_load($comment->cid, $comment->nid); if (isset($current_rating[$tag]['vote_id'])) { $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']); } } fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion); break; } } /** * Get a current rating(s) for a comment. */ function fivestar_comment_load($cid, $nid, $reset = FALSE) { global $user; static $cids = array(); if (!isset($cids[$cid]) || $reset) { $result = db_query('SELECT * FROM {fivestar_comment} WHERE cid = %d', $cid); while($data = db_fetch_array($result)) { $cids[$cid][$data['tag']] = $data; } } return $cids[$cid]; } /** * Update a fivestar comment value. */ function fivestar_comment_update($cid, $nid, $uid, $value, $tag = 'vote') { $vote = _fivestar_cast_vote('node', $nid, $value, $tag, $uid); db_query("UPDATE {fivestar_comment} SET value = %d, vote_id = %d WHERE cid = %d AND tag = '%s'", $value, $vote['user']['vote_id'], $cid, $tag); } /** * Insert a fivestar comment value. */ function fivestar_comment_insert($cid, $nid, $uid, $value, $tag = 'vote') { $vote = _fivestar_cast_vote('node', $nid, $value, $tag, $uid); if (isset($vote['user']['vote_id'])) { db_query("INSERT INTO {fivestar_comment} (cid, vote_id, value, tag) VALUES (%d, %d, %d, '%s')", $cid, $vote['user']['vote_id'], $value, $tag); } } /** * Delete any value for a comment and update their vote. */ function fivestar_comment_delete($cid, $nid, $votes_for_deletion) { if (isset($votes_for_deletion)) { db_query('DELETE FROM {fivestar_comment} WHERE cid = %d', $cid); votingapi_delete_votes($votes_for_deletion); votingapi_recalculate_results('node', $nid); } } /** * Create a vote array based on comment ID, node ID, and vote ID. */ function fivestar_comment_create_vote($cid, $nid, $vote_id) { $vote = array(); $vote['content_id'] = $nid; $vote['content_type'] = 'node'; $vote['vote_id'] = $vote_id; return $vote; } /** * Theme fivestar comment view. */ function theme_fivestar_comment_view($variables) { return $variables['fivestar'] . $variables['comment']; }