示例#1
0
/**
 * 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 = jquery_rating::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;
            }
    }
    $stars = ceil(getOption('rating_stars_count'));
    $recast = getOption('rating_recast');
    $split_stars = max(1, getOption('rating_split_stars'));
    $rating = $object->get('rating');
    $votes = $object->get('total_votes');
    $id = $object->getID();
    $unique = '_' . $table . '_' . $id;
    $ip = jquery_rating::id();
    $oldrating = jquery_rating::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) {
        $step = $split_stars;
        $split = " {split:{$step}}";
        $step = 1 / $step;
    } else {
        $split = '';
        $step = 1;
    }
    ?>
	<form name="star_rating<?php 
    echo $unique;
    ?>
" id="star_rating<?php 
    echo $unique;
    ?>
" action="submit">
		<?php 
    $j = 0;
    for ($i = $step; $i <= $stars; $i = $i + $step) {
        $v = ceil($i);
        $j++;
        ?>
			<input type="radio" class="star<?php 
        echo $split;
        ?>
" name="star_rating-value<?php 
        echo $unique;
        ?>
" value="<?php 
        echo $j;
        ?>
" title="<?php 
        printf(ngettext('%u star', '%u stars', $v), $v);
        ?>
" />
			<?php 
    }
    if (!$disable) {
        ?>
			<span id="submit_button<?php 
        echo $unique;
        ?>
">
				<input type="button" class="button buttons" value="<?php 
        echo gettext('Submit »');
        ?>
" onclick="cast<?php 
        echo $unique;
        ?>
();" />
			</span>
			<?php 
    }
    ?>
	</form>
	<span class="clearall" ></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',
					cache: false,
					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');
    ?>
');
			}
		}
		// ]]> -->
	</script>
	<?php 
}
示例#2
0
文件: update.php 项目: rb26/zenphoto
 define('OFFSET_PATH', 4);
 require_once dirname(dirname(dirname(__FILE__))) . '/template-functions.php';
 $id = sanitize_numeric($_POST['id']);
 $table = sanitize($_POST['table'], 3);
 $dbtable = prefix($table);
 $ip = jquery_rating::id();
 $unique = '_' . $table . '_' . $id;
 if (isset($_POST['star_rating-value' . $unique])) {
     $rating = ceil(sanitize_numeric($_POST['star_rating-value' . $unique]) / max(1, getOption('rating_split_stars')));
     // Make sure the incoming rating isn't higher than what is allowed
     if ($rating > getOption('rating_stars_count')) {
         $rating = getOption('rating_stars_count');
     }
     $IPlist = query_single_row("SELECT * FROM {$dbtable} WHERE id= {$id}");
     if (is_array($IPlist)) {
         $oldrating = jquery_rating::getRatingByIP($ip, $IPlist['used_ips'], $IPlist['rating']);
     } else {
         $oldrating = false;
     }
     if (!$oldrating || getOption('rating_recast')) {
         if ($rating) {
             $_rating_current_IPlist[$ip] = (double) $rating;
         } else {
             if (isset($_rating_current_IPlist[$ip])) {
                 unset($_rating_current_IPlist[$ip]);
                 // retract vote
             }
         }
         $insertip = serialize($_rating_current_IPlist);
         if ($oldrating) {
             if ($rating) {