/[drupal]/contributions/modules/fivestar/fivestar_comment.module
ViewVC logotype

Contents of /contributions/modules/fivestar/fivestar_comment.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Thu Aug 27 21:13:11 2009 UTC (3 months ago) by ezrag
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +2 -2 lines
File MIME type: text/x-php
bug #480640 by Kars-T:
Fivestar comments duplicating comments
1 <?php
2 // $Id: fivestar_comment.module,v 1.5 2009/05/11 15:56:23 quicksketch Exp $
3
4 define('FIVESTAR_COMMENT_DISABLED', 0);
5 define('FIVESTAR_COMMENT_OPTIONAL', 1);
6 define('FIVESTAR_COMMENT_REQUIRED', 2);
7
8 /**
9 * Implementation of hook_theme().
10 */
11 function fivestar_comment_theme() {
12 return array(
13 'fivestar_comment_view' => array(
14 'arguments' => array('comment' => NULL, 'fivestar' => NULL),
15 ),
16 );
17 }
18
19 /**
20 * Form alter specification for comments.
21 */
22 function fivestar_comment_form_alter(&$form, &$form_state, $form_id) {
23
24 // Comment settings.
25 if ($form_id == 'fivestar_node_type_tag_form') {
26 $tag = $form_state['fivestar_tag'];
27 $type_name = $form_state['fivestar_node_type'];
28 $suffix = fivestar_get_suffix($type_name, $tag);
29
30 $form['comment'] = array(
31 '#type' => 'fieldset',
32 '#title' => t('Comment widget'),
33 '#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.'),
34 '#weight' => 1,
35 );
36 $form['comment']['fivestar_comment'] = array(
37 '#type' => 'radios',
38 '#title' => t('Fivestar comment settings'),
39 '#options' => array(
40 FIVESTAR_COMMENT_DISABLED => t('Disabled'),
41 FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'),
42 FIVESTAR_COMMENT_REQUIRED => t('Required rating'),
43 ),
44 '#default_value' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED),
45
46 );
47 $form['comment']['fivestar_comment_preview'] = array(
48 '#type' => 'item',
49 '#title' => t('Comment widget preview'),
50 '#value' => theme('fivestar_preview', 'compact', 'none', $form['fivestar_stars']['#default_value'], $form['comment']['fivestar_comment']['#default_value'] == 1 ? 1 : 0),
51 );
52 if (!$form['fivestar']['#default_value'] || !$form['comment']['fivestar_comment']['#default_value']) {
53 $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', '', 'comment');
54 }
55 else {
56 $form['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['comment']['fivestar_comment_preview']['#value'], 'comment');
57 }
58 }
59
60 // Comment form. Do not allow ratings inside of threads.
61 if ($form_id == 'comment_form' && empty($form['pid']['#value']) && user_access('rate content')) {
62 $node = node_load($form['nid']['#value']);
63
64 // Splice in the fivestar right before the body.
65 $new_form = array();
66 foreach ($form as $key => $element) {
67 if ($key == 'comment_filter') {
68 foreach (fivestar_get_tags() as $tag) {
69 if ($form['cid']['#value']) {
70 $current_rating = fivestar_comment_load($form['cid']['#value'], $form['nid']['#value']);
71 $default_value = $current_rating[$tag]['value'];
72 }
73 else {
74 $votes = fivestar_get_votes('node', $form['nid']['#value'], $tag);
75 $default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
76 }
77
78 if (fivestar_validate_target('node', $node->nid, $tag)) {
79 $suffix = fivestar_get_suffix($node->type, $tag);
80
81 if (variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED)) {
82 $new_form['fivestar_rating']['fivestar_rating_tag_'. $tag] = array(
83 '#type' => 'fivestar',
84 '#title' => t($tag),
85 '#stars' => variable_get('fivestar_stars_'. $node->type, 5),
86 '#allow_clear' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_OPTIONAL ? 1 : 0,
87 '#content_id' => $node->nid,
88 '#required' => variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_REQUIRED ? 1 : 0,
89 '#default_value' => $default_value,
90 '#labels' => variable_get('fivestar_labels_'. $suffix, array()),
91 );
92 }
93 }
94 }
95 }
96 $new_form[$key] = $element;
97 }
98 if ($new_form['fivestar_rating']) {
99 $form = $new_form;
100 }
101
102 }
103 }
104
105 /**
106 * Implementation of hook_comment().
107 */
108 function fivestar_comment(&$comment, $op) {
109 // Performance tweak don't do any processing on validate or publish
110 if ($op == 'validate' || $op == 'publish') {
111 return;
112 }
113
114 if (is_array($comment) && is_numeric($comment['nid'])) {
115 $nid = $comment['nid'];
116 }
117 elseif (is_array($comment) && is_array($comment['nid']) && is_numeric($comment['nid']['#value'])) {
118 $nid = $comment['nid']['#value'];
119 }
120 elseif (is_object($comment) && is_numeric($comment->nid)) {
121 $nid = $comment->nid;
122 }
123
124 if (isset($nid)) {
125 $node = node_load($nid);
126 }
127
128 switch ($op) {
129 case 'view':
130 foreach (fivestar_get_tags() as $tag) {
131 $suffix = fivestar_get_suffix($node->type, $tag);
132 $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED);
133
134 if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
135 $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag;
136 if (!isset($comment->$fivestar_rating_tag)) {
137 $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
138 $comment->$fivestar_rating_tag = isset($current_rating[$tag]['value']) ? $current_rating[$tag]['value'] : NULL;
139 }
140 $comment->$fivestar_rating_tag = $comment->$fivestar_rating_tag;
141 $comment->fivestar_view .= theme('fivestar_static', $comment->$fivestar_rating_tag, variable_get('fivestar_stars_'. $node->type, 5));
142 }
143 }
144 $comment->comment = theme('fivestar_comment_view', $comment->comment, $comment->fivestar_view);
145 break;
146 case 'insert':
147 foreach (fivestar_get_tags() as $tag) {
148 $suffix = fivestar_get_suffix($node->type, $tag);
149 $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED);
150
151 if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
152 $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag;
153 $comment = (object)$comment; // Comment module is inconsistent about comment data structures.
154 if ($comment->$fivestar_rating_tag) {
155 fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag);
156 }
157 $comment = (array)$comment;
158 }
159 }
160 case 'update':
161 foreach (fivestar_get_tags() as $tag) {
162 $suffix = fivestar_get_suffix($node->type, $tag);
163 $fivestar_status = variable_get('fivestar_comment_'. $suffix, FIVESTAR_COMMENT_DISABLED);
164
165 if ($fivestar_status != FIVESTAR_COMMENT_DISABLED) {
166 $fivestar_rating_tag = 'fivestar_rating_tag_'. $tag;
167 $comment = (object)$comment; // Comment module is inconsistent about comment data structures.
168 $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
169 if ($comment->$fivestar_rating_tag) {
170 if (isset($current_rating[$tag]['value'])) {
171 fivestar_comment_update($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag);
172 }
173 else {
174 fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->$fivestar_rating_tag, $tag);
175 }
176 }
177 elseif ($fivestar_status != FIVESTAR_COMMENT_DISABLED && isset($current_rating[$tag]['vote_id'])) {
178 $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']);
179 }
180 $comment = (array)$comment;
181 }
182 }
183
184 fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion);
185 break;
186 case 'delete':
187 foreach (fivestar_get_tags() as $tag) {
188 $current_rating = fivestar_comment_load($comment->cid, $comment->nid);
189 if (isset($current_rating[$tag]['vote_id'])) {
190 $votes_for_deletion[] = fivestar_comment_create_vote($comment->cid, $comment->nid, $current_rating[$tag]['vote_id']);
191 }
192 }
193 fivestar_comment_delete($comment->cid, $comment->nid, $votes_for_deletion);
194 break;
195 }
196 }
197
198 /**
199 * Get a current rating(s) for a comment.
200 */
201 function fivestar_comment_load($cid, $nid, $reset = FALSE) {
202 global $user;
203 static $cids = array();
204 if (!isset($cids[$cid]) || $reset) {
205 $result = db_query('SELECT * FROM {fivestar_comment} WHERE cid = %d', $cid);
206
207 while($data = db_fetch_array($result)) {
208 $cids[$cid][$data['tag']] = $data;
209 }
210 }
211 return $cids[$cid];
212 }
213
214 /**
215 * Update a fivestar comment value.
216 */
217 function fivestar_comment_update($cid, $nid, $uid, $value, $tag = 'vote') {
218 $vote = _fivestar_cast_vote('node', $nid, $value, $tag, $uid);
219 db_query("UPDATE {fivestar_comment} SET value = %d, vote_id = %d WHERE cid = %d AND tag = '%s'", $value, $vote['user']['vote_id'], $cid, $tag);
220 }
221
222 /**
223 * Insert a fivestar comment value.
224 */
225 function fivestar_comment_insert($cid, $nid, $uid, $value, $tag = 'vote') {
226 $vote = _fivestar_cast_vote('node', $nid, $value, $tag, $uid);
227
228 if (isset($vote['user']['vote_id'])) {
229 db_query("INSERT INTO {fivestar_comment} (cid, vote_id, value, tag) VALUES (%d, %d, %d, '%s')", $cid, $vote['user']['vote_id'], $value, $tag);
230 }
231 }
232
233 /**
234 * Delete any value for a comment and update their vote.
235 */
236 function fivestar_comment_delete($cid, $nid, $votes_for_deletion) {
237 if (isset($votes_for_deletion)) {
238 db_query('DELETE FROM {fivestar_comment} WHERE cid = %d', $cid);
239 votingapi_delete_votes($votes_for_deletion);
240 votingapi_recalculate_results('node', $nid);
241 }
242 }
243
244 /**
245 * Create a vote array based on comment ID, node ID, and vote ID.
246 */
247 function fivestar_comment_create_vote($cid, $nid, $vote_id) {
248 $vote = array();
249 $vote['content_id'] = $nid;
250 $vote['content_type'] = 'node';
251 $vote['vote_id'] = $vote_id;
252 return $vote;
253 }
254
255 /**
256 * Theme fivestar comment view.
257 */
258 function theme_fivestar_comment_view($comment, $fivestar) {
259 return $fivestar . $comment;
260 }

  ViewVC Help
Powered by ViewVC 1.1.2