/[drupal]/contributions/modules/fivestarextra/fivestarextra.module
ViewVC logotype

Contents of /contributions/modules/fivestarextra/fivestarextra.module

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


Revision 1.5 - (show annotations) (download) (as text)
Sat Aug 29 21:24:36 2009 UTC (2 months, 4 weeks ago) by likeless
Branch: MAIN
CVS Tags: DRUPAL-6--1-1, HEAD
Changes since 1.4: +39 -17 lines
File MIME type: text/x-php
#556578 Prevented fivestarextra forms being displayed on comment reply preview page to avoid a bug
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Enables fivestar voting on comments (ie. voting on whether it is a good or bad comment).
7 *
8 */
9
10
11 /**
12 * Implementation of hook_menu.
13 */
14 function fivestarextra_menu() {
15 $items = array();
16
17 $items['admin/settings/fivestarextra_comments'] = array(
18 'title' => 'Fivestar Comments Rating',
19 'page callback' => 'drupal_get_form',
20 'page arguments' => array('fivestarextra_comments_admin_form'),
21 'description' => 'Configure Fivestar setting for the rating of comments.',
22 'type' => MENU_NORMAL_ITEM,
23 'access callback' => 'user_access',
24 'access arguments' => array('administer site configuration'),
25 'file' => 'fivestarextra.admin.inc',
26 );
27 $items['admin/settings/fivestarextra_users'] = array(
28 'title' => 'Fivestar Users Rating',
29 'page callback' => 'drupal_get_form',
30 'page arguments' => array('fivestarextra_users_admin_form'),
31 'description' => 'Configure Fivestar setting for the rating of users.',
32 'type' => MENU_NORMAL_ITEM,
33 'access callback' => 'user_access',
34 'access arguments' => array('administer site configuration'),
35 'file' => 'fivestarextra.admin.inc',
36 );
37
38 return $items;
39 }
40
41
42 /**
43 * Implementation of hook_theme().
44 */
45 function fivestarextra_theme() {
46 return array(
47 'fivestarextra_noderestrict_enable_comment' => array(
48 'arguments' => array('element' => NULL),
49 'file' => 'fivestarextra.admin.inc',
50 ),
51 );
52 }
53
54
55 /**
56 * Implementation of hook_comment.
57 *
58 * Unfortunately hook_comment doesn't give us the ability to add and render new elements in the comment,
59 * so the Fivestar form has to be concatenated with the comment text itself.
60 */
61 function fivestarextra_comment(&$comment, $op) {
62
63 $path = arg();
64 //If we are on the comment reply page, adding a dynamic rating form causes problems, so exit
65 if ( $path[0] == 'comment' && $path[1] == 'reply' ) {
66 return;
67 }
68
69 if ($op == 'view' && $comment->preview != 'Preview') {
70 if (variable_get('fivestarextra_enable_comment', FALSE)) {
71
72 //node type restriction
73 if (variable_get('fivestarextra_noderestrict_enable_comment', 0)) {
74 $node = node_load($comment->nid);
75 $type = node_get_types('type', $node);
76 if ( !in_array($type->type, variable_get('fivestarextra_noderestrict_nodetypes', array() ))) {
77 //If the node type isn't listed, exit
78 return;
79 }
80 }
81
82 $position = variable_get('fivestarextra_position_comment', 'below');
83 switch ($position) {
84 case 'above':
85 $comment->comment = drupal_get_form('fivestarextra_form_comment_'. $comment->cid, 'comment', $comment->cid) . $comment->comment;
86 break;
87 case 'below':
88 $comment->comment .= drupal_get_form('fivestarextra_form_comment_'. $comment->cid, 'comment', $comment->cid);
89 break;
90 default:
91 // We'll do nothing.
92 break;
93 }
94
95 }
96 }
97 }
98
99
100 /**
101 * Implementation of hook_forms
102 *
103 * Maps all our form ids onto fivestarextra_widget_form. We
104 * use a lot of different form ids on the same page.
105 */
106 function fivestarextra_forms($form_id, $args) {
107 if (strpos($form_id, 'fivestarextra_form_comment_') !== FALSE) {
108 if ($form_id == 'fivestarextra_form_comment_'. $args[1]) {
109 $forms[$form_id] = array('callback' => 'fivestarextra_widget_form');
110 return $forms;
111 }
112 }
113 if (strpos($form_id, 'fivestarextra_form_user_') !== FALSE) {
114 if ($form_id == 'fivestarextra_form_user_'. $args[1]) {
115 $forms[$form_id] = array('callback' => 'fivestarextra_widget_form');
116 return $forms;
117 }
118 }
119 }
120
121
122 /**
123 * Modified version of fivestar_form for comments and users.
124 */
125 function fivestarextra_widget_form(&$form_state, $content_type, $content_id) {
126
127 global $user;
128
129 $votes = fivestar_get_votes($content_type, $content_id);
130
131 $values = array(
132 'user' => isset($votes['user']['value']) ? $votes['user']['value'] : NULL,
133 'average' => isset($votes['average']['value']) ? $votes['average']['value'] : NULL,
134 'count' => isset($votes['count']['value']) ? $votes['count']['value'] : NULL,
135 );
136
137 $settings = array(
138 'stars' => variable_get('fivestarextra_stars_'. $content_type, 5),
139 'allow_clear' => variable_get('fivestarextra_unvote_'. $content_type, FALSE),
140 'style' => variable_get('fivestarextra_style_'. $content_type, 'average'),
141 'text' => variable_get('fivestarextra_text_'. $content_type, 'dual'),
142 'content_type' => $content_type,
143 'content_id' => $content_id,
144 'tag' => 'vote',
145 'autosubmit' => TRUE,
146 'title' => variable_get('fivestarextra_title_'. $content_type, 1) ? NULL : FALSE,
147 'feedback_enable' => variable_get('fivestarextra_feedback_'. $content_type, 1),
148 'labels_enable' => variable_get('fivestarextra_labels_enable_'. $content_type, 1),
149 'labels' => variable_get('fivestarextra_labels_'. $content_type, array()),
150 );
151
152 $myform = fivestar_custom_widget($form_state, $values, $settings);
153
154 return $myform;
155 }
156
157
158 /**
159 * Implementation of hook_fivestar_access().
160 */
161 function fivestarextra_fivestar_access($type, $id, $account) {
162 global $user;
163 if ($type == 'comment') {
164 if (variable_get('fivestarextra_enable_comment', 0)) {
165 return TRUE;
166 }
167 else {
168 return FALSE;
169 }
170 }
171 if ($type == 'user') {
172 if (variable_get('fivestarextra_enable_user', 0) && $user->uid != $id ) {
173 return TRUE;
174 }
175 else {
176 return FALSE;
177 }
178 }
179 }
180
181
182 /**
183 * Implementation of hook_user.
184 */
185 function fivestarextra_user($op, &$edit, &$account, $category = NULL) {
186 global $user;
187 switch ($op) {
188 case 'view':
189 if (variable_get('fivestarextra_enable_user', FALSE)) {
190 //Return the category
191 $account->content['fivestarextra'] = array(
192 '#type' => 'user_profile_category',
193 '#title' => t('User Rating'),
194 '#weight' => variable_get('fivestarextra_position_user', '100'),
195 );
196
197 if ($user->uid == $account->uid) {
198 //If the user is viewing their own page, return a static form so they cannot vote
199 $account->content['fivestarextra']['widget'] = array(
200 '#type' => 'user_profile_item',
201 '#title' => t('Your Rating:'),
202 '#value' => fivestar_static('user', $account->uid),
203 );
204 }
205 else {
206 //If it is not their own profile page, return the normal fivestar widget
207 $account->content['fivestarextra']['widget'] = array(
208 '#type' => 'user_profile_item',
209 '#title' => t('Rate %username:', array('%username' => $account->name)),
210 '#value' => drupal_get_form('fivestarextra_form_user_'. $account->uid, 'user', $account->uid),
211 );
212 }
213 }
214 break;
215 }
216 }
217

  ViewVC Help
Powered by ViewVC 1.1.2