1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Provides the theming functions for fivestar
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Show a preview of a widget using a custom CSS file.
|
10 |
|
|
*/
|
11 |
|
|
function theme_fivestar_preview_widget($variables) {
|
12 |
|
|
$path = drupal_get_path('module', 'fivestar');
|
13 |
|
|
$form = array(
|
14 |
|
|
'#post' => array(),
|
15 |
|
|
'#programmed' => FALSE,
|
16 |
|
|
'#tree' => FALSE,
|
17 |
|
|
'#parents' => array(),
|
18 |
|
|
'#array_parents' => array(),
|
19 |
|
|
'#required' => FALSE,
|
20 |
|
|
'#attributes' => array(),
|
21 |
|
|
'#title_display' => 'before',
|
22 |
|
|
);
|
23 |
|
|
$form_state = form_state_defaults();
|
24 |
|
|
$form_state['values'] = array();
|
25 |
|
|
$form_state['process_input'] = array();
|
26 |
|
|
$form_state['complete form'] = array();
|
27 |
|
|
|
28 |
|
|
$form['vote'] = array(
|
29 |
|
|
'#type' => 'fivestar',
|
30 |
|
|
'#stars' => 5,
|
31 |
|
|
'#auto_submit' => FALSE,
|
32 |
|
|
'#allow_clear' => TRUE,
|
33 |
3461d8cb
|
Assos Assos
|
'#allow_revote' => TRUE,
|
34 |
|
|
'#allow_ownvote' => TRUE,
|
35 |
85ad3d82
|
Assos Assos
|
'#widget' => array(
|
36 |
|
|
'name' => isset($variables['name']) ? $variables['name'] : 'default',
|
37 |
|
|
'css' => isset($variables['css']) && $variables['css'] != 'default' ? $variables['css'] : FALSE,
|
38 |
|
|
),
|
39 |
|
|
);
|
40 |
3cb08e71
|
Assos Assos
|
|
41 |
85ad3d82
|
Assos Assos
|
// Attach necessary JS settings
|
42 |
|
|
$settings = array(
|
43 |
4853591f
|
Assos Assos
|
'titleUser' => t('Your rating') . ': ',
|
44 |
|
|
'titleAverage' => t('Average') . ': ',
|
45 |
85ad3d82
|
Assos Assos
|
'feedbackSavingVote' => t('Saving your vote...'),
|
46 |
|
|
'feedbackVoteSaved' => t('Your vote has been saved.'),
|
47 |
|
|
'feedbackDeletingVote' => t('Deleting your vote...'),
|
48 |
|
|
'feedbackVoteDeleted' => t('Your vote has been deleted.'),
|
49 |
|
|
);
|
50 |
|
|
|
51 |
|
|
drupal_add_js(array('fivestar' => $settings), 'setting');
|
52 |
|
|
|
53 |
|
|
$form = form_builder('fivestar_preview', $form, $form_state);
|
54 |
|
|
|
55 |
|
|
$output = '<div class="fivestar-star-preview fivestar-' . $form['vote']['#widget']['name'] . '">';
|
56 |
|
|
$output .= drupal_render_children($form);
|
57 |
|
|
$output .= '</div>';
|
58 |
|
|
|
59 |
|
|
return $output;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
function theme_fivestar_preview($variables) {
|
63 |
|
|
extract($variables, EXTR_SKIP);
|
64 |
|
|
$values = array(
|
65 |
|
|
'average' => 50,
|
66 |
|
|
'user' => 80,
|
67 |
|
|
'count' => 20,
|
68 |
|
|
);
|
69 |
|
|
$settings = array(
|
70 |
|
|
'stars' => $stars,
|
71 |
|
|
'allow_clear' => $unvote,
|
72 |
3461d8cb
|
Assos Assos
|
'allow_revote' => $revote,
|
73 |
|
|
'allow_ownvote' => $ownvote,
|
74 |
85ad3d82
|
Assos Assos
|
'style' => $style,
|
75 |
|
|
'text' => $text,
|
76 |
|
|
'title' => $title,
|
77 |
|
|
'autosubmit' => FALSE,
|
78 |
|
|
'tag' => 'vote',
|
79 |
|
|
);
|
80 |
|
|
|
81 |
|
|
$form = drupal_get_form('fivestar_custom_widget', $values, $settings);
|
82 |
|
|
$form = drupal_render($form);
|
83 |
|
|
// This regex is sadly necessary because having duplicate form_tokens or
|
84 |
|
|
// form_id elements can cause the content type form to choke. Forms inside of
|
85 |
|
|
// forms is also frowned upon, so this removes the wrapping form tag as well.
|
86 |
|
|
$form = str_replace(array('<form', '</form>'), array('<div', '</div>'), $form);
|
87 |
|
|
$form = preg_replace('/( method=".*?")|( action=".*?")|(<input.*?name="(form_token|form_id|destination|form_build_id)".*?\/>)/', '', $form);
|
88 |
|
|
return $form;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
function theme_fivestar_preview_wrapper($variables) {
|
92 |
4853591f
|
Assos Assos
|
return '<div class="fivestar-preview fivestar-preview-' . $variables['type'] . '">' . $variables['content'] . '</div>';
|
93 |
85ad3d82
|
Assos Assos
|
}
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Theme function for 'default' fivestar field formatter.
|
97 |
3cb08e71
|
Assos Assos
|
*
|
98 |
|
|
* This themes static stars. That is, pairs of stars where neither set
|
99 |
|
|
* of stars is "exposed". Exposed stars are clickable and displayed in a
|
100 |
|
|
* form. Theming of exposed stars is handled by the form array (and calls
|
101 |
|
|
* the same theme functions called at the end of this function).
|
102 |
85ad3d82
|
Assos Assos
|
*/
|
103 |
|
|
function theme_fivestar_formatter_default($variables) {
|
104 |
|
|
$element = $variables['element'];
|
105 |
|
|
if (empty($element['#instance_settings']['stars'])) {
|
106 |
|
|
$element['#instance_settings']['stars'] = 5;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
// Add CSS and JS
|
110 |
|
|
$path = drupal_get_path('module', 'fivestar');
|
111 |
|
|
drupal_add_js($path . '/js/fivestar.js');
|
112 |
|
|
drupal_add_css($path . '/css/fivestar.css');
|
113 |
|
|
|
114 |
|
|
$variables = array(
|
115 |
|
|
'rating' => $element['#rating'],
|
116 |
|
|
'stars' => $element['#instance_settings']['stars'],
|
117 |
|
|
'widget' => $element['#widget'],
|
118 |
|
|
);
|
119 |
|
|
$star_display = theme('fivestar_static', $variables);
|
120 |
3cb08e71
|
Assos Assos
|
return theme('fivestar_static_element', array('description' => $element['#description'], 'star_display' => $star_display, 'is_form' => FALSE));
|
121 |
85ad3d82
|
Assos Assos
|
}
|
122 |
|
|
|
123 |
|
|
/**
|
124 |
|
|
* Theme function for 'rating' fivestar field formatter.
|
125 |
|
|
*/
|
126 |
|
|
function theme_fivestar_formatter_rating($variables) {
|
127 |
|
|
$element = $variables['element'];
|
128 |
|
|
|
129 |
|
|
if (empty($element['#item']['average'])) {
|
130 |
|
|
$element['#item']['average'] = 0;
|
131 |
|
|
}
|
132 |
|
|
// Get number of stars.
|
133 |
|
|
$stars = (empty($element['#instance_settings']['stars'])) ? 5 : $element['#instance_settings']['stars'];
|
134 |
|
|
$average = $element['#item']['average'];
|
135 |
|
|
// Rating is X out of Y stars.
|
136 |
3cb08e71
|
Assos Assos
|
$rating = round(($average/100) * $stars, 1);
|
137 |
85ad3d82
|
Assos Assos
|
$output = $rating . '/' . $stars;
|
138 |
|
|
|
139 |
|
|
return $output;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Theme function for 'percentage' fivestar field formatter.
|
144 |
|
|
*/
|
145 |
|
|
function theme_fivestar_formatter_percentage($variables) {
|
146 |
|
|
$element = $variables['element'];
|
147 |
|
|
|
148 |
|
|
if (empty($element['#item']['average'])) {
|
149 |
|
|
$element['#item']['average'] = 0;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
return round($element['#item']['average'], 1) . '%';
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
/**
|
156 |
|
|
* Theme the fivestar form element by adding necessary css and javascript.
|
157 |
|
|
*/
|
158 |
|
|
function theme_fivestar($variables) {
|
159 |
|
|
$element = $variables['element'];
|
160 |
|
|
|
161 |
|
|
return theme('form_element', array('element' => $element));
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Theme the straight HTML version of the fivestar select list. This is used
|
166 |
|
|
* to remove the wrapping 'form-item' div from the select list.
|
167 |
|
|
*/
|
168 |
|
|
function theme_fivestar_select($variables) {
|
169 |
|
|
$element = $variables['element'];
|
170 |
|
|
element_set_attributes($element, array('id', 'name', 'size'));
|
171 |
|
|
_form_set_class($element, array('form-select'));
|
172 |
|
|
return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
|
173 |
|
|
}
|
174 |
|
|
/**
|
175 |
|
|
* Display a plain HTML view-only version of the widget with a specified rating.
|
176 |
|
|
*
|
177 |
|
|
* @param $rating
|
178 |
|
|
* The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars).
|
179 |
|
|
* @param $stars
|
180 |
|
|
* The total number of stars this rating is out of.
|
181 |
|
|
* @param $tag
|
182 |
|
|
* Allows multiple ratings per node.
|
183 |
|
|
* @return
|
184 |
|
|
* A themed HTML string representing the star widget.
|
185 |
|
|
*/
|
186 |
|
|
function theme_fivestar_static($variables) {
|
187 |
|
|
$rating = $variables['rating'];
|
188 |
|
|
$stars = $variables['stars'];
|
189 |
|
|
$tag = $variables['tag'];
|
190 |
|
|
$widget = $variables['widget'];
|
191 |
4853591f
|
Assos Assos
|
if ($widget['name'] != 'default') {
|
192 |
|
|
$path = drupal_get_path('module', 'fivestar');
|
193 |
|
|
drupal_add_css($path . '/css/fivestar.css');
|
194 |
85ad3d82
|
Assos Assos
|
drupal_add_css($widget['css']);
|
195 |
|
|
}
|
196 |
3cb08e71
|
Assos Assos
|
|
197 |
85ad3d82
|
Assos Assos
|
$output = '<div class="fivestar-' . $widget['name'] . '">';
|
198 |
4853591f
|
Assos Assos
|
$output .= '<div class="fivestar-widget-static fivestar-widget-static-' . $tag . ' fivestar-widget-static-' . $stars . ' clearfix">';
|
199 |
85ad3d82
|
Assos Assos
|
if (empty($stars)) {
|
200 |
|
|
$stars = 5;
|
201 |
|
|
}
|
202 |
|
|
$numeric_rating = $rating/(100/$stars);
|
203 |
|
|
for ($n=1; $n <= $stars; $n++) {
|
204 |
|
|
$star_value = ceil((100/$stars) * $n);
|
205 |
|
|
$prev_star_value = ceil((100/$stars) * ($n-1));
|
206 |
|
|
$zebra = ($n % 2 == 0) ? 'even' : 'odd';
|
207 |
|
|
$first = $n == 1 ? ' star-first' : '';
|
208 |
|
|
$last = $n == $stars ? ' star-last' : '';
|
209 |
4853591f
|
Assos Assos
|
$output .= '<div class="star star-' . $n . ' star-' . $zebra . $first . $last . '">';
|
210 |
85ad3d82
|
Assos Assos
|
if ($rating < $star_value && $rating > $prev_star_value) {
|
211 |
|
|
$percent = (($rating - $prev_star_value) / ($star_value - $prev_star_value)) * 100;
|
212 |
4853591f
|
Assos Assos
|
$output .= '<span class="on" style="width: ' . $percent . '%">';
|
213 |
85ad3d82
|
Assos Assos
|
}
|
214 |
|
|
elseif ($rating >= $star_value) {
|
215 |
|
|
$output .= '<span class="on">';
|
216 |
|
|
}
|
217 |
|
|
else {
|
218 |
|
|
$output .= '<span class="off">';
|
219 |
|
|
}
|
220 |
|
|
if ($n == 1)$output .= $numeric_rating;
|
221 |
|
|
$output .= '</span></div>';
|
222 |
|
|
}
|
223 |
|
|
$output .= '</div></div>';
|
224 |
|
|
return $output;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
|
|
* Display the text associated with a static star display.
|
229 |
|
|
*
|
230 |
|
|
* Note that passing in explicit data types is extremely important when using
|
231 |
|
|
* this function. A NULL value will exclude the value entirely from display,
|
232 |
|
|
* while a 0 value indicates that the text should be shown but it has no value
|
233 |
|
|
* yet.
|
234 |
|
|
*
|
235 |
|
|
* All ratings are from 0 to 100.
|
236 |
|
|
*
|
237 |
|
|
* @param $user_rating
|
238 |
|
|
* The current user's rating.
|
239 |
|
|
* @param $average
|
240 |
|
|
* The average rating.
|
241 |
|
|
* @param $votes
|
242 |
|
|
* The total number of votes.
|
243 |
|
|
* @param $stars
|
244 |
|
|
* The number of stars being displayed.
|
245 |
|
|
* @return
|
246 |
|
|
* A themed HTML string representing the star widget.
|
247 |
|
|
*/
|
248 |
|
|
function theme_fivestar_summary($variables) {
|
249 |
|
|
$microdata = $variables['microdata'];
|
250 |
|
|
extract($variables, EXTR_SKIP);
|
251 |
|
|
$output = '';
|
252 |
|
|
$div_class = '';
|
253 |
|
|
$average_rating_microdata = '';
|
254 |
3461d8cb
|
Assos Assos
|
$rating_count_microdata = '';
|
255 |
85ad3d82
|
Assos Assos
|
if (isset($user_rating)) {
|
256 |
|
|
$div_class = isset($votes) ? 'user-count' : 'user';
|
257 |
|
|
$user_stars = round(($user_rating * $stars) / 100, 1);
|
258 |
4853591f
|
Assos Assos
|
$output .= '<span class="user-rating">' . t('Your rating: <span>!stars</span>', array('!stars' => $user_rating ? $user_stars : t('None'))) . '</span>';
|
259 |
85ad3d82
|
Assos Assos
|
}
|
260 |
|
|
if (isset($user_rating) && isset($average_rating)) {
|
261 |
|
|
$output .= ' ';
|
262 |
|
|
}
|
263 |
|
|
if (isset($average_rating)) {
|
264 |
3461d8cb
|
Assos Assos
|
if (isset($user_rating)) {
|
265 |
|
|
$div_class = 'combo';
|
266 |
|
|
}
|
267 |
|
|
else {
|
268 |
|
|
$div_class = isset($votes) ? 'average-count' : 'average';
|
269 |
|
|
}
|
270 |
|
|
|
271 |
85ad3d82
|
Assos Assos
|
$average_stars = round(($average_rating * $stars) / 100, 1);
|
272 |
|
|
if (!empty($microdata['average_rating']['#attributes'])) {
|
273 |
|
|
$average_rating_microdata = drupal_attributes($microdata['average_rating']['#attributes']);
|
274 |
|
|
}
|
275 |
4853591f
|
Assos Assos
|
$output .= '<span class="average-rating">' . t('Average: !stars',
|
276 |
|
|
array('!stars' => "<span $average_rating_microdata>$average_stars</span>")) . '</span>';
|
277 |
85ad3d82
|
Assos Assos
|
}
|
278 |
|
|
|
279 |
3461d8cb
|
Assos Assos
|
if (isset($votes)) {
|
280 |
|
|
if (!isset($user_rating) && !isset($average_rating)) {
|
281 |
|
|
$div_class = 'count';
|
282 |
|
|
}
|
283 |
|
|
if ($votes === 0) {
|
284 |
|
|
$output = '<span class="empty">'. t('No votes yet') .'</span>';
|
285 |
|
|
}
|
286 |
|
|
else {
|
287 |
|
|
if (!empty($microdata['rating_count']['#attributes'])) {
|
288 |
|
|
$rating_count_microdata = drupal_attributes($microdata['rating_count']['#attributes']);
|
289 |
|
|
}
|
290 |
|
|
// We don't directly substitute $votes (i.e. use '@count') in format_plural,
|
291 |
|
|
// because it has a span around it which is not translatable.
|
292 |
|
|
$votes_str = format_plural($votes, '!cnt vote', '!cnt votes', array(
|
293 |
|
|
'!cnt' => '<span ' . $rating_count_microdata . '>' . intval($votes) . '</span>'));
|
294 |
|
|
if (isset($user_rating) || isset($average_rating)) {
|
295 |
|
|
$output .= ' <span class="total-votes">(' . $votes_str . ')</span>';
|
296 |
|
|
}
|
297 |
|
|
else {
|
298 |
|
|
$output .= ' <span class="total-votes">' . $votes_str . '</span>';
|
299 |
|
|
}
|
300 |
|
|
}
|
301 |
85ad3d82
|
Assos Assos
|
}
|
302 |
|
|
|
303 |
|
|
|
304 |
4853591f
|
Assos Assos
|
$output = '<div class="fivestar-summary fivestar-summary-' . $div_class . '">' . $output . '</div>';
|
305 |
85ad3d82
|
Assos Assos
|
return $output;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/**
|
309 |
|
|
* Display a static fivestar value as stars with a title and description.
|
310 |
|
|
*/
|
311 |
|
|
function theme_fivestar_static_element($variables) {
|
312 |
|
|
$output = '';
|
313 |
3cb08e71
|
Assos Assos
|
if (isset($variables['is_form']) && !$variables['is_form']) {
|
314 |
|
|
$output .= '<div class="fivestar-static-item">';
|
315 |
|
|
}
|
316 |
|
|
else {
|
317 |
|
|
$output .= '<div class="fivestar-static-form-item">';
|
318 |
|
|
}
|
319 |
85ad3d82
|
Assos Assos
|
$element = array(
|
320 |
|
|
'#type' => 'item',
|
321 |
|
|
'#title' => $variables['title'],
|
322 |
|
|
'#description' => $variables['description'],
|
323 |
|
|
'#children' => $variables['star_display'],
|
324 |
|
|
);
|
325 |
|
|
|
326 |
|
|
$output .= theme('form_element', array('element' => $element));
|
327 |
|
|
$output .= '</div>';
|
328 |
|
|
return $output;
|
329 |
|
|
} |