Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#56 Vote Option/Function

open
nobody
5
2009-01-09
2009-01-09
Jan Straka
No

This workaround add your users the possibility to vote for their favorite bookmarks. It also change the sort order.

May there are some bugs or things that can be done better, please mail if you find some thing to change and improve.

Discussion

  • Jan Straka
    Jan Straka
    2009-01-09

    Scuttle Vote Function/Option - Workaround

     
  • Jan Straka
    Jan Straka
    2009-01-09

    --
    +scuttle-vote-option
    +its only a workaround and may there are still some bugs, if you got a question or a suggestion feel free to contact me +email: blog.secure@gmail.com or over my blog http://secure-blog.pytalhost.eu
    +2009-01-09
    --

    this describes a workaround to give your users the option to vote for their favorite bookmarks
    (Deutsche Anleitung im Anschluss)

    execute this query:

    --
    -- Tabellenstruktur für Tabelle `sc_votes`
    --

    CREATE TABLE IF NOT EXISTS `sc_votes` (
    `vId` int(11) NOT NULL auto_increment,
    `bId` int(11) default NULL,
    `uId` int(11) default NULL,
    `vTotal` int(11) default NULL,
    `vVotesDate` datetime default NULL,
    `vRefer` varchar(355) default NULL,
    `vIP` varchar(20) default NULL,
    PRIMARY KEY (`vId`)
    );

    ALTER TABLE `sc_bookmarks` ADD `bVotes` INT NOT NULL ;

    ####################################
    Now you have to change two files, first:
    ####################################
    /services/bookmarkservices.php
    replace:

    default:
    $query_5 = ' ORDER BY B.bVotes DESC ';

    through:

    //vote
    default:
    $query_5 = ' ORDER BY B.bVotes DESC ';

    on the end, but before this two lines:

    }
    ?>

    add this:

    /*START Vote*Scuttle*secure-blog.ru.tc**first*try************/
    //update vote total
    function updateVotings ($bId,$vTotal){
    $updates = array('bVotes' => $vTotal);

    $sql = 'UPDATE '. $GLOBALS['tableprefix'] .'bookmarks
    SET '. $this->db->sql_build_array('UPDATE', $updates) .'
    WHERE bId = '.$bId;

    $this->db->sql_transaction('begin');

    if (!($dbresult = & $this->db->sql_query($sql))) {
    $this->db->sql_transaction('rollback');
    message_die(GENERAL_ERROR, 'Could not update voting',
    '', __LINE__, __FILE__, $sql, $this->db);
    return false;
    }

    $this->db->sql_transaction('commit');
    // Everything worked out, so return true.
    return true;
    }

    //select vote per bookmark
    function getVotes($bId){
    $query = 'SELECT vTotal, COUNT(*) AS vTotal
    FROM sc_votes
    WHERE bId ='.$bId.'
    GROUP BY bID';

    if (!($dbresult =& $this->db->sql_query($query))) {
    message_die(GENERAL_ERROR, 'Could not get votes', '',
    __LINE__, __FILE__, $query, $this->db);
    return false;
    }
    $vTotal = $this->db->sql_fetchrow($dbresult);

    return $vTotal;
    }

    //check if already voted
    function checkVotes($bId) {
    $userservice = & ServiceFactory ::
    getServiceInstance('UserService');

    $sId = $userservice->getCurrentUserId();
    $query = 'SELECT uId,bId
    FROM sc_votes
    WHERE bId = '. $bId .'
    ORDER BY bId';

    if (!($dbresult =& $this->db->sql_query($query))) {
    message_die(GENERAL_ERROR, 'Could not get tags', '',
    __LINE__, __FILE__, $query, $this->db);
    return false;
    }

    $votes = array();
    $i=0;
    while ($row =& $this->db->sql_fetchrow($dbresult)) {
    $votes[] = $row;

    if (!$alreadyvoted && $votes[$i]['uId'] ==
    intval($sId) &&

    $votes[$i]['bId'] == $bId){
    $alreadyvoted = true;
    }
    $i++;
    }
    return $alreadyvoted;
    }
    //submit vote
    function voteInsert($bId) {
    $userservice = & ServiceFactory ::
    getServiceInstance('UserService');

    $sId = $userservice->getCurrentUserId();

    //datetime
    if (is_null($date))
    $time = time();
    else
    $time = strtotime($date);

    $datetime = gmdate('Y-m-d H:i:s', $time);

    //check if immediately voted
    $alreadyvoted = $this->checkVotes($bId);
    if(!$alreadyvoted){
    //insert statement
    $insert = array('vId' => null,
    'bId' => $bId,
    'uId' => intval($sId),
    'vVotesDate' => $datetime,
    'vTotal' => 1,
    'vRefer' => $_SERVER['HTTP_REFERER'],
    'vIP' => $_SERVER['REMOTE_ADDR']);

    $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .
    'votes '. $this->db->sql_build_array('INSERT', $insert);

    $this->db->sql_transaction('begin');

    if (!($dbresult = & $this->db->sql_query($sql))) {
    $this->db->sql_transaction('rollback');
    message_die(GENERAL_ERROR, 'Could not update bookmark', '',
    __LINE__, __FILE__, $sql, $this->db);

    return false;
    }

    $this->db->sql_transaction('commit');
    // Everything worked out, so return true.
    return true;
    }
    // return $alreadyvoted;
    }
    /*END Vote*Scuttle*secure-blog.ru.tc**first*try************/

    ###################################
    Now lets change the second file:
    ###################################
    /templates/bookmarks.tpl.php

    before:

    if (!isset($hash)) {

    add this:

    //////////////////////////// start vote
    if ($userservice->isLoggedOn() && isset($_POST['vote']))
    $bookmarkservice->voteInsert($_POST['vote_bId']);
    /////////////////////////// end vote

    after:

    // Output
    echo '<li class="xfolkentry'. $access .'">'."\n";

    add this:

    //////////////////////start select votes
    $votes = $bookmarkservice->getVotes($row['bId']);
    if(empty($votes['vTotal']))
    $votes['vTotal'] = 0;
    //update vote total
    $bookmarkservice->updateVotings($row['bId'],$votes['vTotal']);

    $alreadyvoted = $bookmarkservice->checkVotes($row['bId']);

    if($alreadyvoted)
    echo'
    <form method="post">
    <input name="vote_bId" type="hidden" value="'.$row['bId'].'" />
    <input class="vote_voted" name="vote" type="submit" value="'.$votes['vTotal'].'" />
    </form>';
    else
    echo'
    <form method="post">
    <input name="vote_bId" type="hidden" value="'.$row['bId'].'" />
    <input class="vote" name="vote" type="submit" value="'.$votes['vTotal'].'" />
    </form>';
    ////////////////end vote

    some changes for the stylesheet:
    scuttle.css

    /*vote for scuttle*/
    .vote,.vote_voted{
    background:url(bg_bar.png);
    height:48px;
    width: 48px;
    color: #DDD;
    font-weight: bold;
    border: 5px groove black;
    padding: 5px;
    }
    .vote:hover{
    border: 4px groove black;
    cursor: pointer;
    }
    .vote_voted {
    filter:alpha(opacity=30); /* IE */
    -moz-opacity: 0.30; /* Gecko */
    opacity: 0.30; /* Opera */
    }
    ____________________________________________________________________
    DEUTSCH ###########################################################
    Da mir bei Scuttle noch eine Voting Funktion fehlte hab ich mich einfach selbst rangemacht und habe mich im Code herumgehangelt. Hier sind die Anweisungen die eigentlich stimmen müssten, falls etwas nicht klappen sollte, einfach fragen ;)

    folgende querys in der Datenbank ausführen lassen:

    --
    -- Tabellenstruktur für Tabelle `sc_votes`
    --

    CREATE TABLE IF NOT EXISTS `sc_votes` (
    `vId` int(11) NOT NULL auto_increment,
    `bId` int(11) default NULL,
    `uId` int(11) default NULL,
    `vTotal` int(11) default NULL,
    `vVotesDate` datetime default NULL,
    `vRefer` varchar(355) default NULL,
    `vIP` varchar(20) default NULL,
    PRIMARY KEY (`vId`)
    );

    ALTER TABLE `sc_bookmarks` ADD `bVotes` INT NOT NULL ;

    ####################################
    Nun gilt es zwei Dateien zu ergänzen.
    ####################################
    /services/bookmarkservices.php
    ersetze:

    default:
    $query_5 = ' ORDER BY B.bVotes DESC ';

    durch:

    //vote
    default:
    $query_5 = ' ORDER BY B.bVotes DESC ';

    am ende vor:

    }
    ?>

    das hinzufügen:

    /*START Vote*Scuttle*secure-blog.ru.tc**first*try************/
    //update vote total
    function updateVotings ($bId,$vTotal){
    $updates = array('bVotes' => $vTotal);

    $sql = 'UPDATE '. $GLOBALS['tableprefix'] .'bookmarks
    SET '. $this->db->sql_build_array('UPDATE', $updates) .'
    WHERE bId = '.$bId;

    $this->db->sql_transaction('begin');

    if (!($dbresult = & $this->db->sql_query($sql))) {
    $this->db->sql_transaction('rollback');
    message_die(GENERAL_ERROR, 'Could not update voting',
    '', __LINE__, __FILE__, $sql, $this->db);
    return false;
    }

    $this->db->sql_transaction('commit');
    // Everything worked out, so return true.
    return true;
    }

    //select vote per bookmark
    function getVotes($bId){
    $query = 'SELECT vTotal, COUNT(*) AS vTotal
    FROM sc_votes
    WHERE bId ='.$bId.'
    GROUP BY bID';

    if (!($dbresult =& $this->db->sql_query($query))) {
    message_die(GENERAL_ERROR, 'Could not get votes', '',
    __LINE__, __FILE__, $query, $this->db);
    return false;
    }
    $vTotal = $this->db->sql_fetchrow($dbresult);

    return $vTotal;
    }

    //check if already voted
    function checkVotes($bId) {
    $userservice = & ServiceFactory ::
    getServiceInstance('UserService');

    $sId = $userservice->getCurrentUserId();
    $query = 'SELECT uId,bId
    FROM sc_votes
    WHERE bId = '. $bId .'
    ORDER BY bId';

    if (!($dbresult =& $this->db->sql_query($query))) {
    message_die(GENERAL_ERROR, 'Could not get tags', '',
    __LINE__, __FILE__, $query, $this->db);
    return false;
    }

    $votes = array();
    $i=0;
    while ($row =& $this->db->sql_fetchrow($dbresult)) {
    $votes[] = $row;

    if (!$alreadyvoted && $votes[$i]['uId'] ==
    intval($sId) &&

    $votes[$i]['bId'] == $bId){
    $alreadyvoted = true;
    }
    $i++;
    }
    return $alreadyvoted;
    }
    //submit vote
    function voteInsert($bId) {
    $userservice = & ServiceFactory ::
    getServiceInstance('UserService');

    $sId = $userservice->getCurrentUserId();

    //datetime
    if (is_null($date))
    $time = time();
    else
    $time = strtotime($date);

    $datetime = gmdate('Y-m-d H:i:s', $time);

    //check if immediately voted
    $alreadyvoted = $this->checkVotes($bId);
    if(!$alreadyvoted){
    //insert statement
    $insert = array('vId' => null,
    'bId' => $bId,
    'uId' => intval($sId),
    'vVotesDate' => $datetime,
    'vTotal' => 1,
    'vRefer' => $_SERVER['HTTP_REFERER'],
    'vIP' => $_SERVER['REMOTE_ADDR']);

    $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .
    'votes '. $this->db->sql_build_array('INSERT', $insert);

    $this->db->sql_transaction('begin');

    if (!($dbresult = & $this->db->sql_query($sql))) {
    $this->db->sql_transaction('rollback');
    message_die(GENERAL_ERROR, 'Could not update bookmark', '',
    __LINE__, __FILE__, $sql, $this->db);

    return false;
    }

    $this->db->sql_transaction('commit');
    // Everything worked out, so return true.
    return true;
    }
    // return $alreadyvoted;
    }
    /*END Vote*Scuttle*secure-blog.ru.tc**first*try************/

    ###################################
    Zur zweiten Datei:
    ###################################
    /templates/bookmarks.tpl.php

    vor:

    if (!isset($hash)) {

    das einfügen:

    //////////////////////////// start vote
    if ($userservice->isLoggedOn() && isset($_POST['vote']))
    $bookmarkservice->voteInsert($_POST['vote_bId']);
    /////////////////////////// end vote

    nach:

    // Output
    echo '<li class="xfolkentry'. $access .'">'."\n";

    das hinzufügen:

    //////////////////////start select votes
    $votes = $bookmarkservice->getVotes($row['bId']);
    if(empty($votes['vTotal']))
    $votes['vTotal'] = 0;
    //update vote total
    $bookmarkservice->updateVotings($row['bId'],$votes['vTotal']);

    $alreadyvoted = $bookmarkservice->checkVotes($row['bId']);

    if($alreadyvoted)
    echo'
    <form method="post">
    <input name="vote_bId" type="hidden" value="'.$row['bId'].'" />
    <input class="vote_voted" name="vote" type="submit" value="'.$votes['vTotal'].'" />
    </form>';
    else
    echo'
    <form method="post">
    <input name="vote_bId" type="hidden" value="'.$row['bId'].'" />
    <input class="vote" name="vote" type="submit" value="'.$votes['vTotal'].'" />
    </form>';
    ////////////////end vote

    Wer will kann auch noch den CSS ansatz übernehmen:
    scuttle.css

    /*vote for scuttle*/
    .vote,.vote_voted{
    background:url(bg_bar.png);
    height:48px;
    width: 48px;
    color: #DDD;
    font-weight: bold;
    border: 5px groove black;
    padding: 5px;
    }
    .vote:hover{
    border: 4px groove black;
    cursor: pointer;
    }
    .vote_voted {
    filter:alpha(opacity=30); /* IE */
    -moz-opacity: 0.30; /* Gecko */
    opacity: 0.30; /* Opera */
    }

     
  • Iskandar
    Iskandar
    2011-06-20

    thank you..sometimes its better than semantic vote function..