~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to modules/fivestar/fivestar_comment.module

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: fivestar_comment.module,v 1.1.2.5 2009/04/10 17:50:58 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
  // Comment settings.
 
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.'),
 
29
      '#weight' => 1,
 
30
    );
 
31
    $form['fivestar']['comment']['fivestar_comment'] = array(
 
32
      '#type' => 'radios',
 
33
      '#title' => t('Fivestar comment settings'),
 
34
      '#options' => array(
 
35
        FIVESTAR_COMMENT_DISABLED => t('Disabled'),
 
36
        FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'),
 
37
        FIVESTAR_COMMENT_REQUIRED => t('Required rating'),
 
38
      ),
 
39
      '#default_value' => variable_get('fivestar_comment_'. $form['#node_type']->type, FIVESTAR_COMMENT_DISABLED),
 
40
    );
 
41
    $form['fivestar']['comment']['fivestar_comment_preview'] = array(
 
42
      '#type' => 'item',
 
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),
 
45
    );
 
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');
 
48
    }
 
49
    else {
 
50
      $form['fivestar']['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['fivestar']['comment']['fivestar_comment_preview']['#value'], 'comment');
 
51
    }
 
52
  }
 
53
 
 
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.
 
59
      $new_form = array();
 
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'];
 
65
          }
 
66
          else {
 
67
            $votes = fivestar_get_votes('node', $form['nid']['#value']);
 
68
            $default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
 
69
          }
 
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()),
 
79
          );
 
80
        }
 
81
        $new_form[$key] = $element;
 
82
      }
 
83
      if ($new_form['fivestar_rating']) {
 
84
        $form = $new_form;
 
85
      }
 
86
    }
 
87
  }
 
88
}
 
89
 
 
90
/**
 
91
 * Implementation of hook_comment().
 
92
 */
 
93
function fivestar_comment(&$comment, $op) {
 
94
  if (is_array($comment) && is_numeric($comment['nid'])) {
 
95
    $nid = $comment['nid'];
 
96
  }
 
97
  elseif (is_array($comment) && is_array($comment['nid']) && is_numeric($comment['nid']['#value'])) {
 
98
    $nid = $comment['nid']['#value'];
 
99
  }
 
100
  elseif (is_object($comment) && is_numeric($comment->nid)) {
 
101
    $nid = $comment->nid;
 
102
  }
 
103
 
 
104
  if (isset($nid)) {
 
105
    $node = node_load($nid);
 
106
    $fivestar_status = variable_get('fivestar_comment_'. $node->type, FIVESTAR_COMMENT_DISABLED);
 
107
  }
 
108
 
 
109
  switch ($op) {
 
110
    case 'view':
 
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;
 
115
        }
 
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);
 
121
        }
 
122
      }
 
123
      break;
 
124
    case 'insert':
 
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);
 
129
        }
 
130
        $comment = (array)$comment;
 
131
      }
 
132
    case 'update':
 
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);
 
139
          }
 
140
          else {
 
141
            fivestar_comment_insert($comment->cid, $comment->nid, $comment->uid, $comment->fivestar_rating);
 
142
          }
 
143
        }
 
144
        elseif ($fivestar_status != FIVESTAR_COMMENT_DISABLED && isset($current_rating['value'])) {
 
145
          fivestar_comment_delete($comment->cid, $comment->nid, $comment->uid);
 
146
        }
 
147
        $comment = (array)$comment;
 
148
      }
 
149
      break;
 
150
    case 'delete':
 
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']);
 
154
      }
 
155
      break;
 
156
  }
 
157
}
 
158
 
 
159
/**
 
160
 * Get a current rating for a comment.
 
161
 */
 
162
function fivestar_comment_load($cid, $nid, $reset = FALSE) {
 
163
  global $user;
 
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));
 
167
  }
 
168
  return $cids[$cid];
 
169
}
 
170
 
 
171
/**
 
172
 * Update a fivestar comment value.
 
173
 */
 
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);
 
177
}
 
178
 
 
179
/**
 
180
 * Insert a fivestar comment value.
 
181
 */
 
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);
 
185
}
 
186
 
 
187
/**
 
188
 * Delete any value for a comment and update their vote.
 
189
 */
 
190
function fivestar_comment_delete($cid, $nid, $vote_id) {
 
191
  db_query('DELETE FROM {fivestar_comment} WHERE cid = %d', $cid);
 
192
 
 
193
  $vote = array();
 
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);
 
199
}
 
200
 
 
201
function theme_fivestar_comment_view($comment, $fivestar) {
 
202
  return $fivestar . $comment;
 
203
}