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

Contents of /contributions/modules/jrating/jrating.module

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


Revision 1.5 - (show annotations) (download) (as text)
Sun Dec 10 17:17:48 2006 UTC (2 years, 11 months ago) by hickory
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +1 -1 lines
File MIME type: text/x-php
missed a drupal_add_css
1 <?php
2
3 /*
4 * rating module for Drupal 5.0
5 * version 0.5
6 * 2006-12-04
7 * Copyright (c) 2006, Alf Eaton and Nature Publishing Group
8 * Released under the GPL license
9 * http://www.gnu.org/copyleft/gpl.html
10 */
11
12 /**
13 * hook_perm().
14 */
15 function jrating_perm() {
16 return array ('rate content');
17 }
18
19 /**
20 * hook_menu().
21 */
22 function jrating_menu($may_cache) {
23 $items[] = array(
24 'path' => 'admin/settings/jrating',
25 'title' => t('jRating'),
26 'description' => t('Administer jRating.'),
27 'callback' => 'drupal_get_form',
28 'callback arguments' => 'jrating_settings',
29 'access' => user_access('administer site configuration'),
30 );
31
32 return $items;
33 }
34
35 /**
36 * Configuration form.
37 */
38 function jrating_settings() {
39 $form['jrating_display_teaser'] = array(
40 '#type' => 'radios',
41 '#title' => t('Display rating form for teasers'),
42 '#default_value' => variable_get('jrating_display_teaser', 0),
43 '#options' => array ('1' => t('Yes'), '0' => t('No')),
44 '#description' => t('Should a rating form be present when only a node teaser is displayed?')
45 );
46
47 $form['jrating_display_anonymous'] = array(
48 '#type' => 'radios',
49 '#title' => t('Display rating form for anonymous users'),
50 '#default_value' => variable_get('jrating_display_anonymous', 1),
51 '#options' => array ('1' => t('Yes'), '0' => t('No')),
52 '#description' => t('Should a rating form be present when users aren\'t logged in?')
53 );
54
55 $form['jrating_display_mean'] = array(
56 '#type' => 'radios',
57 '#title' => t('Display average rating'),
58 '#default_value' => variable_get('jrating_display_mean', 1),
59 '#options' => array ('1' => t('Yes'), '0' => t('No')),
60 '#description' => t('Should the average rating for an item be displayed?')
61 );
62 return system_settings_form($form);
63 }
64
65 /**
66 * hook_form alter().
67 */
68 function jrating_form_alter($form_id, &$form) {
69 if ($form_id == 'node_type_form') {
70 $form['workflow']['jrating_nodeapi'] = array(
71 '#type' => 'radios',
72 '#title' => t('Show a rating control'),
73 '#default_value' => variable_get('jrating_nodeapi_' . $form['old_type']['#value'], 0),
74 '#options' => array (
75 1 => t('Yes: display a rating control with nodes of this type'),
76 0 => t('No: do not display a rating control with nodes of this type')
77 )
78 );
79 }
80 }
81
82 /**
83 * hook_nodeapi()
84 */
85 function jrating_nodeapi(&$node, $op, $teaser, $page) {
86 switch ($op) {
87 case 'view' :
88 if ((!$teaser || variable_get('jrating_display_teaser', 0)) && variable_get('jrating_nodeapi_' . $node->type, 0)) {
89 drupal_add_css(drupal_get_path('module', 'jrating') . '/jrating.css');
90 drupal_add_js(drupal_get_path('module', 'jrating') . '/js/form.js');
91 drupal_add_js(drupal_get_path('module', 'jrating') . '/js/rating.js');
92
93 $node->jrating_html = jrating_display_rating_form($node->nid); // needed for CCK nodes
94 $node->content['jrating_html'] = array(
95 '#value' => $node->jrating_html,
96 '#weight' => 15
97 );
98 }
99 break;
100 }
101 }
102
103 function jrating_display_rating_form($nid){
104 global $user;
105 if (user_access('rate content') || ((!$user->uid) && variable_get('jrating_display_anonymous', 1))){
106 $rating_form = drupal_get_form('rating_form_' . $nid, array('nid' => $nid));
107 }
108
109 if (variable_get('jrating_display_mean', 1)){
110 $mean_rating = theme('jrating_average_rating', $nid);
111 }
112
113 return '<div class="rating-item">' . $mean_rating . $rating_form . '</div>';
114 }
115
116 /**
117 * hook_forms()
118 */
119 function jrating_forms($args) {
120 $form_id = array_shift($args);
121 if (preg_match('/^rating_form_\d+$/', $form_id)){
122 $forms[$form_id] = array(
123 'callback' => 'jrating_rate',
124 );
125 }
126 return $forms;
127 }
128
129 /**
130 * Build rating form
131 */
132 function jrating_rate($args) {
133 $nid = $args['nid'];
134
135 $form = array();
136
137 $form['#base'] = 'jrating_rate';
138
139 $form['#attributes'] = array ('class' => 'rating');
140
141 $user_rating = jrating_get_user_rating($nid);
142 $form['#attributes']['title'] = 'Rating: ' . $user_rating['rating'];
143
144 $rating_options = array(
145 '0' => t('---'),
146 '100' => t('Excellent!'),
147 '80' => t('Great!'),
148 '60' => t('Good'),
149 '40' => t('Quite good'),
150 '20' => t('Not so great')
151 );
152
153 $form['rating'] = array(
154 '#id' => 'rating_options_' . $nid,
155 '#type' => 'select',
156 '#default_value' => round($user_rating['percent'] / 20) * 20,
157 '#options' => $rating_options,
158 '#title' => t('Rate this'),
159 '#attributes' => array('class' => 'rating-options', 'title' => t('Rate this'))
160 );
161
162 $form['nid'] = array(
163 '#type' => 'hidden',
164 '#value' => $nid
165 );
166
167 $form['submit'] = array(
168 '#type' => 'submit',
169 '#value' => t('Submit')
170 );
171 return $form;
172 }
173
174 /**
175 * Submit rating form
176 */
177 function jrating_rate_submit($form_id, $form_values) {
178 //$vote = (object) NULL;
179 $vote = new stdClass;
180
181 if (user_access('rate content')) {
182 $vote->value = max(0, min(100, $form_values['rating']));
183 $vote->value_type = 'percent';
184 $vote->tag = 'rating';
185
186 $vote->value ? votingapi_set_vote('node', $form_values['nid'], $vote) : votingapi_unset_vote('node', $form_values['nid']);
187
188 $mean_rating = jrating_get_mean_rating($form_values['nid']);
189
190 $response = array(
191 'mean' => $mean_rating['rating'],
192 'num_votes' => $mean_rating['num_votes'],
193 'response' => t('Rating saved'));
194
195 global $user;
196 db_query("REPLACE INTO {history} (uid, nid, timestamp) VALUES (%d, %d, %d)", $user->uid, $form_values['nid'], time());
197 }
198 else {
199 $error = t('Unauthorised: please !login to rate items', array('!login' => l(t('login or register'), 'user/login', NULL, 'destination=' . $_GET['q'])));
200 $response = array('error' => $error);
201 }
202
203 if ($_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest') {
204 print drupal_to_js($response);
205 exit();
206 }
207 else {
208 drupal_set_message($error);
209 }
210 }
211
212 /**
213 * Get user rating for a node.
214 */
215 function jrating_get_user_rating($nid, $uid = FALSE) {
216 if ($votes = votingapi_get_user_votes('node', $nid, $uid)) {
217 foreach ($votes as $vote) {
218 if ($vote->value_type == 'percent' && $vote->tag == 'rating') {
219 $rating = $vote;
220 break;
221 }
222 }
223 }
224
225 return array(
226 'rating' => $rating ? sprintf("%.1f", round($rating->value / 20,1)) : '0.0',
227 'percent' => $rating ? $rating->value : '0',
228 );
229 }
230
231 /**
232 * Get the mean rating for a node.
233 */
234 function jrating_get_mean_rating($nid) {
235 if ($rating = votingapi_get_voting_result('node', $nid, 'percent', 'rating', 'average')) {
236 $count = votingapi_get_voting_result('node', $nid, 'percent', 'rating', 'count');
237 $num_votes = $count->value;
238 }
239
240 return array(
241 'rating' => $rating ? sprintf("%.1f", round($rating->value / 20, 1)) : '0.0',
242 'percent' => $rating ? $rating->value : '0',
243 'num_votes' => $rating ? $num_votes : '0'
244 );
245 }
246
247 /**
248 * Theme function to produce rating stars for average ratings.
249 */
250 function theme_jrating_average_rating($nid, $vote = FALSE) {
251 if ($vote){
252 $rating['rating'] = sprintf("%.1f", round($vote->value / 20, 1));
253 $rating['num_votes'] = 0; // FIXME
254 }
255 else{
256 $rating = jrating_get_mean_rating($nid);
257 }
258
259 $mean_rating = $rating['rating'];
260
261 $stars = array();
262 $count = 0;
263 while ($mean_rating >= 1){
264 $stars[] = '<div class="star avg on"><a style="width: 100%;" onclick="return false;">&nbsp;</a></div>';
265 $mean_rating--;
266 $count++;
267 }
268 if ($mean_rating == 0.5){
269 $stars[] = '<div class="star avg on"><a style="width: 50%;" onclick="return false;">&nbsp;</a></div>';
270 $count++;
271 }
272 while ($count < 5){
273 $stars[] = '<div class="star avg"><a style="width: 100%;" onclick="return false;">&nbsp;</a></div>';
274 $count++;
275 }
276
277
278 if ($vote){
279 drupal_add_css(drupal_get_path('module', 'jrating') . '/jrating.css');
280 return '<span class="rating" id="rating_mean_' . $nid . '">' . implode("\n", $stars) . '</span>';
281 }
282
283 $num_votes_span = '<span id="rating_num_votes_' . $nid .'">';
284 $num_votes_html = format_plural($rating['num_votes'], $num_votes_span . '1</span> ' . t('vote'), $num_votes_span . '@count</span> ' . t('votes'));
285
286 return '<div class="rating" id="rating_mean_' . $nid . '">' . t('Average rating') . implode("\n", $stars) . '<div class="num-votes">(' . $num_votes_html . ')</div></div>';
287 }
288
289 /**
290 * Called by votingapi_views to format vote results.
291 */
292 function jrating_votingapi_format($vote, $op){
293 if ($op == 'value' && $vote->tag == 'rating' && $vote->value_type == 'percent' && $vote->function == 'average'){
294 return theme('jrating_average_rating', $vote->content_id, $vote);
295 }
296 }
297
298 /**
299 * Default view: table of nodes in order of descending rating, with rating field.
300 */
301 function jrating_views_default_views() {
302 $view = new stdClass();
303 $view->name = 'nodes_by_rating';
304 $view->description = 'Nodes with a rating field';
305 $view->access = array (
306 );
307 $view->view_args_php = '';
308 $view->page = TRUE;
309 $view->page_title = 'Node ratings';
310 $view->page_header = '';
311 $view->page_header_format = '1';
312 $view->page_footer = '';
313 $view->page_footer_format = '1';
314 $view->page_empty = '';
315 $view->page_empty_format = '1';
316 $view->page_type = 'table';
317 $view->url = 'rating/nodes';
318 $view->use_pager = TRUE;
319 $view->nodes_per_page = '20';
320 $view->menu = TRUE;
321 $view->menu_title = '';
322 $view->menu_tab = FALSE;
323 $view->menu_tab_default = FALSE;
324 $view->menu_tab_weight = '0';
325 $view->sort = array (
326 array (
327 'tablename' => 'votingapi_cache',
328 'field' => 'value',
329 'sortorder' => 'DESC',
330 'options' => '',
331 ),
332 );
333 $view->argument = array (
334 );
335 $view->field = array (
336 array (
337 'tablename' => 'node',
338 'field' => 'title',
339 'label' => 'Title',
340 'handler' => 'views_handler_field_nodelink',
341 'options' => 'link',
342 ),
343 array (
344 'tablename' => 'votingapi_cache',
345 'field' => 'value',
346 'label' => 'Average Rating',
347 'options' => 'a:3:{s:10:"value_type";s:7:"percent";s:3:"tag";s:6:"rating";s:8:"function";s:7:"average";}',
348 ),
349 array (
350 'tablename' => 'votingapi_vote',
351 'field' => 'currentuservoted',
352 'label' => 'Voted',
353 ),
354 );
355 $view->filter = array (
356 array (
357 'tablename' => 'node',
358 'field' => 'status',
359 'operator' => '=',
360 'options' => '',
361 'value' => '1',
362 ),
363 array (
364 'tablename' => 'node',
365 'field' => 'distinct',
366 'operator' => '=',
367 'options' => '',
368 'value' => array (
369 ),
370 ),
371 );
372 $view->exposed_filter = array (
373 );
374 $view->requires = array(votingapi_cache, node, votingapi_vote);
375 $views[$view->name] = $view;
376
377 return $views;
378 }
379
380 function jrating_footer($main = 0){
381 return '<noscript><style type="text/css">form.rating{ display: block; }</style></noscript>';
382 }
383
384 ?>

  ViewVC Help
Powered by ViewVC 1.1.2