* @author Stephen Billard (sbillard) * @package plugins */ define('OFFSET_PATH', 4); require_once dirname(dirname(dirname(__FILE__))) . '/template-functions.php'; require_once 'functions-rating.php'; $id = sanitize_numeric($_POST['id']); $table = sanitize($_POST['table'], 3); $dbtable = prefix($table); $ip = getUserIP(); $unique = '_' . $table . '_' . $id; $split_stars = getOption('rating_split_stars') + 1; $rating = max(0, min(5, round(sanitize_numeric($_POST['star_rating-value' . $unique]) / $split_stars))); $IPlist = query_single_row("SELECT * FROM {$dbtable} WHERE id= {$id}"); if (is_array($IPlist)) { $oldrating = getRatingByIP($ip, $IPlist['used_ips'], $IPlist['rating']); } else { $oldrating = false; } if (!$oldrating || getOption('rating_recast')) { if ($rating) { $_rating_current_IPlist[$ip] = $rating; } else { if (isset($_rating_current_IPlist[$ip])) { unset($_rating_current_IPlist[$ip]); // retract vote } } $insertip = serialize($_rating_current_IPlist); if ($oldrating) { if ($rating) {
/** * Prints the rating star form and the current rating * Insert this function call in the page script where you * want the star ratings to appear. * * NOTE: * If $vote is false or the rating_recast option is false then * the stars shown will be the rating. Otherwise the stars will * show the value of the viewer's last vote. * * @param bool $vote set to false to disable voting * @param object $object optional object for the ratings target. If not set, the current page object is used * @param bool $text if false, no annotation text is displayed */ function printRating($vote = 3, $object = NULL, $text = true) { global $_zp_gallery_page; if (is_null($object)) { $object = getCurrentPageObject(); } if (!is_object($object)) { return; } $table = $object->table; $vote = min($vote, getOption('rating_status'), $object->get('rating_status')); switch ($vote) { case 1: // members only if (!zp_loggedin()) { $vote = 0; } break; case 2: // members & guests switch ($_zp_gallery_page) { case 'album.php': $album = $object; $hint = ''; if (!(zp_loggedin() || checkAlbumPassword($album->name))) { $vote = 0; } break; case 'pages.php': case 'news.php': if (!zp_loggedin()) { // no guest password $vote = 0; } break; default: $album = $object->getAlbum(); $hint = ''; if (!(zp_loggedin() || checkAlbumPassword($album->name))) { $vote = 0; } break; } } $rating = $object->get('rating'); $votes = $object->get('total_votes'); $id = $object->get('id'); $unique = '_' . $table . '_' . $id; if (getOption('rating_hash_ip')) { $ip = sha1(getUserIP()); } else { $ip = getUserIP(); } $recast = getOption('rating_recast'); $split_stars = getOption('rating_split_stars') + 1; $oldrating = getRatingByIP($ip, $object->get('used_ips'), $object->get('rating')); if ($vote && $recast == 2 && $oldrating) { $starselector = round($oldrating * $split_stars); } else { $starselector = round($rating * $split_stars); } $disable = !$vote || $oldrating && !$recast; if ($rating > 0) { $msg = sprintf(ngettext('Rating %2$.1f (%1$u vote)', 'Rating %2$.1f (%1$u votes)', $votes), $votes, $rating); } else { $msg = gettext('Not yet rated'); } if ($split_stars > 1) { $split = ' {split:2}'; } else { $split = ''; } ?> <form name="star_rating<?php echo $unique; ?> " id="star_rating<?php echo $unique; ?> " action="submit"> <input type="radio" class="star<?php echo $split; ?> " name="star_rating-value<?php echo $unique; ?> " value="1" title="<?php echo gettext('1 star'); ?> " /> <input type="radio" class="star<?php echo $split; ?> " name="star_rating-value<?php echo $unique; ?> " value="2" title="<?php echo gettext('1 star'); ?> " /> <input type="radio" class="star<?php echo $split; ?> " name="star_rating-value<?php echo $unique; ?> " value="3" title="<?php echo gettext('2 stars'); ?> " /> <input type="radio" class="star<?php echo $split; ?> " name="star_rating-value<?php echo $unique; ?> " value="4" title="<?php echo gettext('2 stars'); ?> " /> <input type="radio" class="star<?php echo $split; ?> " name="star_rating-value<?php echo $unique; ?> " value="5" title="<?php echo gettext('3 stars'); ?> " /> <?php if ($split_stars > 1) { ?> <input type="radio" class="star {split:2}" name="star_rating-value<?php echo $unique; ?> " value="6" title="<?php echo gettext('3 stars'); ?> " /> <input type="radio" class="star {split:2}" name="star_rating-value<?php echo $unique; ?> " value="7" title="<?php echo gettext('4 stars'); ?> " /> <input type="radio" class="star {split:2}" name="star_rating-value<?php echo $unique; ?> " value="8" title="<?php echo gettext('4 stars'); ?> " /> <input type="radio" class="star {split:2}" name="star_rating-value<?php echo $unique; ?> " value="9" title="<?php echo gettext('5 stars'); ?> " /> <input type="radio" class="star {split:2}" name="star_rating-value<?php echo $unique; ?> " value="10" title="<?php echo gettext('5 stars'); ?> " /> <?php } if (!$disable) { ?> <span id="submit_button<?php echo $unique; ?> "> <input type="button" value="<?php echo gettext('Submit »'); ?> " onclick="javascript:cast<?php echo $unique; ?> ();" /> </span> <?php } ?> </form> <span style="line-height: 0em;"><br clear="all" /></span> <span class="vote" id="vote<?php echo $unique; ?> " <?php if (!$text) { echo 'style="display:none;"'; } ?> > <?php echo $msg; ?> </span> <script type="text/javascript"> // <!-- <![CDATA[ var recast<?php echo $unique; ?> = <?php printf('%u', $recast && $oldrating); ?> ; $(document).ready(function() { $('#star_rating<?php echo $unique; ?> :radio.star').rating('select','<?php echo $starselector; ?> '); <?php if ($disable) { ?> $('#star_rating<?php echo $unique; ?> :radio.star').rating('disable'); <?php } ?> }); function cast<?php echo $unique; ?> () { var dataString = $('#star_rating<?php echo $unique; ?> ').serialize(); if (dataString || recast<?php echo $unique; ?> ) { <?php if ($recast) { ?> if (!dataString) { dataString = 'star_rating-value<?php echo $unique; ?> =0'; } <?php } else { ?> $('#star_rating<?php echo $unique; ?> :radio.star').rating('disable'); $('#submit_button<?php echo $unique; ?> ').hide(); <?php } ?> $.ajax({ type: 'POST', url: '<?php echo WEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/' . substr(basename(__FILE__), 0, -4); ?> /update.php', data: dataString+'&id=<?php echo $id; ?> &table=<?php echo $table; ?> ' }); recast<?php echo $unique; ?> = <?php printf('%u', $recast); ?> ; $('#vote<?php echo $unique; ?> ').html('<?php echo gettext('Vote Submitted'); ?> '); } else { $('#vote<?php echo $unique; ?> ').html('<?php echo gettext('nothing to submit'); ?> '); } } function star_click<?php echo $unique; ?> () { alert('star click'); $('#vote<?php echo $unique; ?> ').html('<?php echo gettext('Vote pending'); ?> '); } // ]]> --> </script> <?php }