View source
<?php
define('APPS_VOTING_PATH', '/apps/vote/');
function apps_vote_for_app($server, $app, $vote) {
apps_include('manifest');
$id = apps_client_id();
$server = apps_servers($server);
$url = $server['manifest'];
$parts = parse_url($url);
if ($parts) {
$port = isset($parts['port']) ? ':' . $parts['port'] : '';
$url = $parts['scheme'] . '://' . $parts['host'] . $port;
$url .= APPS_VOTING_PATH . $app . '/' . $vote;
$result = drupal_http_request($url, array(
'data' => 'client_id=' . $id,
));
if ($result->code == '200') {
$output = json_decode($result->data);
if (lock_acquire('apps_voting_config')) {
$user_votes = variable_get('apps_user_ratings', array());
$user_votes[$server['name']][$app] = $vote;
variable_set('apps_user_ratings', $user_votes);
lock_release('apps_voting_config');
}
}
else {
$output = array(
'error' => 'Server connection failed, vote could not be saved.',
);
}
}
else {
$output = array(
'error' => 'Server URL failed to parse: ' . $url,
);
}
drupal_json_output($output);
}
function apps_theme_voting_widget($vars) {
if (!empty($vars['app']['rating'])) {
$ratings = $vars['app']['rating'];
}
else {
$ratings = array(
'count' => 0,
'average' => 100,
);
}
$teaser = !empty($vars['app']['teaser']);
$server = $vars['app']['server']['name'];
$mname = $vars['app']['machine_name'];
$app_ratings = variable_get('apps_user_ratings', array());
$user_rating = isset($app_ratings[$server][$mname]) ? $app_ratings[$server][$mname] : FALSE;
$stars = t('@num stars', array(
'@num' => round($ratings['average'] / 20, 1),
));
$js_settings = array(
'apps' => array(
$mname => array(
'url' => '/apps/vote/' . $server . '/' . $mname . '/',
'rating' => array(
'average' => $ratings['average'],
'count' => $ratings['count'],
'stars' => $stars,
'user' => $user_rating,
),
),
),
);
$hasvote = $user_rating === FALSE ? 'no-vote' : '';
$render_arr = array(
'#prefix' => '<div class="app-rating clearfix" id="app-rating-' . $vars['app']['machine_name'] . '">',
'rating' => array(
'#prefix' => '<div class="app-stars-holder" title="' . $stars . '">',
'#markup' => '<span class="app-stars" style="width:' . $ratings['average'] . '%"></span>',
'#suffix' => '</div>',
),
'numratings' => array(
'#prefix' => '<div class="app-rating-count">',
'#markup' => '(' . t('!count ratings', array(
'!count' => '<span class="rating-count">' . $ratings['count'] . '</span>',
)) . ')',
'#suffix' => '</div>',
),
'#suffix' => '</div>',
);
if (!$teaser && variable_get('apps_allow_voting', FALSE)) {
$render_arr['totalrating'] = array(
'#prefix' => '<div class="app-average-rating">',
'#markup' => t('Average rating: !stars stars', array(
'!stars' => '<span class="stars-count">' . round($ratings['average'] / 20, 1) . '</span>',
)),
'#suffix' => '</div>',
);
$render_arr['userrating'] = array(
'#prefix' => '<div class="app-user-rating ' . $hasvote . '">',
'#markup' => t('Your rating: !stars stars', array(
'!stars' => '<span class="stars-count">' . round($user_rating / 20, 1) . '</span>',
)),
'#suffix' => '</div>',
);
$render_arr['#attached'] = array(
'js' => array(
drupal_get_path('module', 'apps') . '/theme/js/apps-voting.js',
array(
'type' => 'setting',
'data' => $js_settings,
),
),
);
}
return $render_arr;
}