Allow Multiple Voting Allow Multiple Voting
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Allow Multiple Voting

Started by pols1337, February 26, 2013, 11:19:23 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

pols1337

Hi, in keeping with my new skin / slideshow concept, I need to allow a user to vote for pictures multiple times.

I swear I read a thread this morning about changing something with the number 86400, but I didn't bookmark the thread and now I can't find it again  >:( 

Jeff Bailey

Are you talking about this thread?
http://forum.coppermine-gallery.net/index.php/topic,50650.msg244221.html#msg244221
Thats for 1.4.x, not sure if that still works.

Adding this to your custom themes theme.php will work.

/******************************************************************************
** Section <<<theme_html_rating_box>>> - START
******************************************************************************/
function theme_html_rating_box()
{
    global $CONFIG, $CURRENT_PIC_DATA, $CURRENT_ALBUM_DATA, $THEME_DIR, $USER_DATA, $USER, $LINEBREAK;
    global $template_image_rating, $template_image_rating_oldstyle, $lang_rate_pic;

    if (!(USER_CAN_RATE_PICTURES && $CURRENT_ALBUM_DATA['votes'] == 'YES')) {
        return '';
    } else {
        //check if the users already voted or if this user is the owner
        $user_md5_id = USER_ID ? md5(USER_ID) : $USER['ID'];
        $result = cpg_db_query("SELECT pic_id FROM {$CONFIG['TABLE_VOTES']} WHERE pic_id={$CURRENT_PIC_DATA['pid']} AND user_md5_id='$user_md5_id'");

        $user_can_vote = 'false';
        if ($CURRENT_PIC_DATA['owner_id'] == $USER_DATA['user_id'] && $USER_DATA['user_id'] != 0 && ($CONFIG['rate_own_files'] == 0 || $CONFIG['rate_own_files'] == 2 && !USER_IS_ADMIN)) {
            // user is owner
            $rate_title = $lang_rate_pic['forbidden'];
        } else {
            // user hasn't voted yet, show voting things
            $rate_title = $lang_rate_pic['rate_this_pic'];
            $user_can_vote = 'true';
        }
        /*} elseif (!mysql_num_rows($result)) {
            // user hasn't voted yet, show voting things
            $rate_title = $lang_rate_pic['rate_this_pic'];
            $user_can_vote = 'true';
        } else {
            //user has voted
            $rate_title = $lang_rate_pic['already_voted'];
        }*/
        $rating_stars_amount = ($CONFIG['old_style_rating']) ? 5 : $CONFIG['rating_stars_amount'];
        $votes = $CURRENT_PIC_DATA['votes'] ? sprintf($lang_rate_pic['rating'], round(($CURRENT_PIC_DATA['pic_rating'] / 2000) / (5/$rating_stars_amount), 1), $rating_stars_amount, $CURRENT_PIC_DATA['votes']) : $lang_rate_pic['no_votes'];
        $pid = $CURRENT_PIC_DATA['pid'];

        if (defined('THEME_HAS_RATING_GRAPHICS')) {
            $location= $THEME_DIR;
        } else {
            $location= '';
        }

        $superCage = Inspekt::makeSuperCage();

        $params = array(
            '{TITLE}'      => $rate_title,
            '{VOTES}'      => $votes,
            '{LOCATION}'   => $location,
            '{WIDTH}'      => $CONFIG['picture_table_width'] == "100%" ? $CONFIG['main_table_width'] : $CONFIG['picture_table_width'],
        );

        if ($CONFIG['old_style_rating']) {
            // use old-style rating
            $start_td = '<td class="tableb" width="17%" align="center">';
            $end_td = '</td>';
            $empty_star = '<img style="cursor:pointer" id="' . $pid . '_0" title="0" src="' . $location . 'images/rate_empty.png" alt="' . $lang_rate_pic['rubbish'] . '" onclick="rate(this)" />';
            $rating_images = $start_td . $empty_star . $empty_star . $empty_star . $empty_star . $empty_star . $end_td . $LINEBREAK;

            $empty_star = '<img style="cursor:pointer" id="' . $pid . '_1" title="1" src="' . $location . 'images/rate_empty.png" alt="' . $lang_rate_pic['poor'] . '" onclick="rate(this)" />';
            $full_star = '<img style="cursor:pointer" id="' . $pid . '_1" title="1" src="' . $location . 'images/rate_full.png" alt="' . $lang_rate_pic['poor'] . '" onclick="rate(this)" />';
            $rating_images .= $start_td . $full_star . $empty_star . $empty_star . $empty_star . $empty_star . $end_td . $LINEBREAK;

            $empty_star = '<img style="cursor:pointer" id="' . $pid . '_2" title="2" src="' . $location . 'images/rate_empty.png" alt="' . $lang_rate_pic['fair'] . '" onclick="rate(this)" />';
            $full_star = '<img style="cursor:pointer" id="' . $pid . '_2" title="2" src="' . $location . 'images/rate_full.png" alt="' . $lang_rate_pic['fair'] . '" onclick="rate(this)" />';
            $rating_images .= $start_td . $full_star . $full_star . $empty_star . $empty_star . $empty_star . $end_td . $LINEBREAK;

            $empty_star = '<img style="cursor:pointer" id="' . $pid . '_3" title="3" src="' . $location . 'images/rate_empty.png" alt="' . $lang_rate_pic['good'] . '" onclick="rate(this)" />';
            $full_star = '<img style="cursor:pointer" id="' . $pid . '_3" title="3" src="' . $location . 'images/rate_full.png" alt="' . $lang_rate_pic['good'] . '" onclick="rate(this)" />';
            $rating_images .= $start_td . $full_star . $full_star . $full_star . $empty_star . $empty_star . $end_td . $LINEBREAK;

            $empty_star = '<img style="cursor:pointer" id="' . $pid . '_4" title="4" src="' . $location . 'images/rate_empty.png" alt="' . $lang_rate_pic['excellent'] . '" onclick="rate(this)" />';
            $full_star = '<img style="cursor:pointer" id="' . $pid . '_4" title="4" src="' . $location . 'images/rate_full.png" alt="' . $lang_rate_pic['excellent'] . '" onclick="rate(this)" />';
            $rating_images .= $start_td . $full_star . $full_star . $full_star . $full_star . $empty_star . $end_td . $LINEBREAK;

            $full_star = '<img style="cursor:pointer" id="' . $pid . '_5" title="5" src="' . $location . 'images/rate_full.png" alt="' . $lang_rate_pic['great'] . '" onclick="rate(this)" />';
            $rating_images .= $start_td . $full_star . $full_star . $full_star . $full_star . $full_star . $end_td . $LINEBREAK;

            set_js_var('stars_amount', 'fallback');
            set_js_var('lang_rate_pic', $rate_title);
            $params['{RATING_IMAGES}'] = $rating_images;
            $template_rating = $template_image_rating_oldstyle;

        } else {
            //use new rating
            set_js_var('stars_amount', $rating_stars_amount);
            set_js_var('lang_rate_pic', $lang_rate_pic['rollover_to_rate']);
            $params['{JS_WARNING}'] = $lang_rate_pic['js_warning'];
            $template_rating = $template_image_rating;
        }
        set_js_var('rating', round(($CURRENT_PIC_DATA['pic_rating'] / 2000) / (5/$rating_stars_amount), 0));
        set_js_var('theme_dir', $location);
        set_js_var('can_vote', $user_can_vote);
        list($timestamp, $form_token) = getFormToken();
        set_js_var('form_token', $form_token);
        set_js_var('timestamp', $timestamp);

        return template_eval($template_rating, $params);
    }
}
/******************************************************************************
** Section <<<theme_html_rating_box>>> - END
******************************************************************************/
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

pols1337

Thanks, this works. 

/resolved

(I don't quite agree with the functionality, since it seems like the new rating is ignored in favor of the original rating.  But, this code works so I ain't complainin')

Jeff Bailey

Please mark your thread as solved.
http://forum.coppermine-gallery.net/index.php/topic,55415.msg270631.html#msg270631

Quote from: pols1337 on February 27, 2013, 05:35:06 AM
I don't quite agree with the functionality, since it seems like the new rating is ignored in favor of the original rating.  But, this code works so I ain't complainin'
I'm not sure what you are saying here? The rating stars should show an average and the database should update with the number of votes and the rating. This should also work with the old style and new style rating system.
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford

pols1337

Well, it might actually work but I can't really tell since once you vote (again), it flashes a message saying "Sorry but it seems like you have already voted."  And then it moves on to the next picture too quickly for me to tell if the vote gets updated

In any case, if it does actually record the vote a second time, that's even better! 

:)

Jeff Bailey

Strange, that doesn't happen for me but in
rateppic.php
Find

// Check if user already rated this picture
$user_md5_id = USER_ID ? md5(USER_ID) : $USER['ID'];
$sql = "SELECT null FROM {$CONFIG['TABLE_VOTES']} WHERE pic_id = $pic AND user_md5_id = '$user_md5_id'";
$result = cpg_db_query($sql);

replace with

// Check if user already rated this picture
/*$user_md5_id = USER_ID ? md5(USER_ID) : $USER['ID'];
$sql = "SELECT null FROM {$CONFIG['TABLE_VOTES']} WHERE pic_id = $pic AND user_md5_id = '$user_md5_id'";
$result = cpg_db_query($sql);*/

or just delete those lines

depending on your setup you may have to comment or delete all of this.

// Clean votes older votes
$clean_before = time() - $CONFIG['keep_votes_time'] * 86400;
$sql = "DELETE FROM {$CONFIG['TABLE_VOTES']} WHERE vote_time < $clean_before";
$result = cpg_db_query($sql);

// Check if user already rated this picture
$user_md5_id = USER_ID ? md5(USER_ID) : $USER['ID'];
$sql = "SELECT null FROM {$CONFIG['TABLE_VOTES']} WHERE pic_id = $pic AND user_md5_id = '$user_md5_id'";
$result = cpg_db_query($sql);

if (mysql_num_rows($result)) {

    // user has already rated this file
    $send_back = array(
        'status' => 'error',
        'msg'    => $lang_rate_pic_php['already_rated'],
        'a'      => $USER,
    );

    echo json_encode($send_back);
    exit;
}

mysql_free_result($result);


untested.
Thinking is the hardest work there is, which is probably the reason why so few engage in it. - Henry Ford