2
// $Id: fivestar_comment.module,v 1.1.2.5 2009/04/10 17:50:58 quicksketch Exp $
4
define('FIVESTAR_COMMENT_DISABLED', 0);
5
define('FIVESTAR_COMMENT_OPTIONAL', 1);
6
define('FIVESTAR_COMMENT_REQUIRED', 2);
9
* Implementation of hook_theme().
11
function fivestar_comment_theme() {
13
'fivestar_comment_view' => array(
14
'arguments' => array('comment' => NULL, 'fivestar' => NULL),
20
* Form alter specification for comments.
22
function fivestar_comment_form_alter(&$form, &$form_state, $form_id) {
24
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
25
$form['fivestar']['comment'] = array(
26
'#type' => 'fieldset',
27
'#title' => t('Comment widget'),
28
'#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.'),
31
$form['fivestar']['comment']['fivestar_comment'] = array(
33
'#title' => t('Fivestar comment settings'),
35
FIVESTAR_COMMENT_DISABLED => t('Disabled'),
36
FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'),
37
FIVESTAR_COMMENT_REQUIRED => t('Required rating'),
39
'#default_value' => variable_get('fivestar_comment_'. $form['#node_type']->type, FIVESTAR_COMMENT_DISABLED),
41
$form['fivestar']['comment']['fivestar_comment_preview'] = array(
43
'#title' => t('Comment widget preview'),
44
'#value' => theme('fivestar_preview', 'compact', 'none', $form['fivestar']['fivestar_stars']['#default_value'], $form['fivestar']['comment']['fivestar_comment']['#default_value'] == 1 ? 1 : 0),
46
if (!$form['fivestar']['fivestar']['#default_value'] || !$form['fivestar']['comment']['fivestar_comment']['#default_value']) {
47
$form['fivestar']['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', '', 'comment');
50
$form['fivestar']['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['fivestar']['comment']['fivestar_comment_preview']['#value'], 'comment');
54
// Comment form. Do not allow ratings inside of threads.
55
if ($form_id == 'comment_form' && empty($form['pid']['#value']) && user_access('rate content')) {
56
$node = node_load($form['nid']['#value']);
57
if (variable_get('fivestar_comment_'. $node->type, FIVESTAR_COMMENT_DISABLED)) {
58
// Splice in the fivestar right before the body.
60
foreach ($form as $key => $element) {
61
if ($key == 'comment_filter') {
62
if ($form['cid']['#value']) {
63
$current_rating = fivestar_comment_load($form['cid']['#value'], $form['nid']['#value']);
64
$default_value = $current_rating['value'];
67
$votes = fivestar_get_votes('node', $form['nid']['#value']);
68
$default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
70
$new_form['fivestar_rating'] = array(
71
'#type' => 'fivestar',
72
'#title' => t('Rating'),
73
'#stars' => variable_get('fivestar_stars_'. $node->type, 5),
74
'#allow_clear' => variable_get('fivestar_comment_'. $node->type, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_OPTIONAL ? 1 : 0,
75
'#content_id' => $node->nid,
76
'#required' => variable_get('fivestar_comment_'. $node->type, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_REQUIRED ? 1 : 0,
77
'#default_value' => $default_value,
78
'#labels' => variable_get('fivestar_labels_'. $node->type, array()),
81
$new_form[$key] = $element;
83
if ($new_form['fivestar_rating']) {
91
* Implementation of hook_comment().
93
function fivestar_comment(&$comment, $op) {
94
if (is_array($comment) && is_numeric($comment['nid'])) {
95
$nid = $comment['nid'];
97
elseif (is_array($comment) && is_array($comment['nid']) && is_numeric($comment['nid']['#value'])) {
98
$nid = $comment['nid']['#value'];
100
elseif (is_object($comment) && is_numeric($comment->nid)) {
101
$nid = $comment->nid;
105
$node = node_load($nid);
106
$fivestar_status = variable_get('fivestar_comment_'. $node->type, FIVESTAR_COMMENT_DISABLED);
111
if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
112
if (!isset($comment->fivestar_rating)) {
113
$current_rating = fivestar_comment_load($comment->cid, $comment->nid);
114
$comment->fivestar_rating = isset($current_rating['value']) ? $current_rating['value'] : NULL;
116
$comment->fivestar_rating = $comment->fivestar_rating;
117
$comment->fivestar_view = theme('fivestar_static', $comment->fivestar_rating, variable_get('fivestar_stars_'. $node->type, 5));
118
if ($comment->fivestar_rating) {
119
// Implement the theme in template.php to change the order:
120
$comment->comment = theme('fivestar_comment_view', $comment->comment, $comment->fivestar_view);
125
if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
126
$comment = (object)$comment; // Comment module is inconsistent about comment data structures.
127
if ($comment->fivestar_rating) {
128
fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->fivestar_rating);
130
$comment = (array)$comment;
133
if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
134
$comment = (object)$comment; // Comment module is inconsistent about comment data structures.
135
$current_rating = fivestar_comment_load($comment->cid, $comment->nid);
136
if ($comment->fivestar_rating) {
137
if (isset($current_rating['value'])) {
138
fivestar_comment_update($comment->cid, $comment->nid, $comment->uid, $comment->fivestar_rating);
141
fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->fivestar_rating);
144
elseif ($fivestar_status != FIVESTAR_COMMENT_DISABLED && isset($current_rating['value'])) {
145
fivestar_comment_delete($comment->cid, $comment->nid, $comment->uid);
147
$comment = (array)$comment;
151
$current_rating = fivestar_comment_load($comment->cid, $comment->nid);
152
if (isset($current_rating['value'])) {
153
fivestar_comment_delete($comment->cid, $comment->nid, $current_rating['vote_id']);
160
* Get a current rating for a comment.
162
function fivestar_comment_load($cid, $nid, $reset = FALSE) {
164
static $cids = array();
165
if (!isset($cids[$cid]) || $reset) {
166
$cids[$cid] = db_fetch_array(db_query('SELECT * FROM {fivestar_comment} WHERE cid = %d', $cid));
172
* Update a fivestar comment value.
174
function fivestar_comment_update($cid, $nid, $uid, $value) {
175
$vote = _fivestar_cast_vote('node', $nid, $value, 'vote', $uid);
176
db_query('UPDATE {fivestar_comment} SET value = %d, vote_id = %d WHERE cid = %d', $value, $vote['user']['vote_id'], $cid);
180
* Insert a fivestar comment value.
182
function fivestar_comment_insert($cid, $nid, $uid, $value) {
183
$vote = _fivestar_cast_vote('node', $nid, $value, 'vote', $uid);
184
db_query('INSERT INTO {fivestar_comment} (cid, value) VALUES (%d, %d)', $cid, $vote['user']['vote_id'], $value);
188
* Delete any value for a comment and update their vote.
190
function fivestar_comment_delete($cid, $nid, $vote_id) {
191
db_query('DELETE FROM {fivestar_comment} WHERE cid = %d', $cid);
194
$vote['content_id'] = $nid;
195
$vote['content_type'] = 'node';
196
$vote['vote_id'] = $vote_id;
197
votingapi_delete_votes(array($vote));
198
votingapi_recalculate_results('node', $nid);
201
function theme_fivestar_comment_view($comment, $fivestar) {
202
return $fivestar . $comment;