Exemplo n.º 1
0
/**
 * Votes for something
 * @param {string} forType the type of thing to vote for
 * @param {string} forId string uniquely identifying that thing among others of its type
 * @param {integer} value the value the user has voted for, such as a rating etc.
 */
function Users_vote_post()
{
    $user = Users::loggedInUser(true);
    $required = array('forType', 'forId');
    foreach ($required as $field) {
        if (empty($_REQUEST[$field])) {
            throw new Q_Exception_RequiredField(compact('field'));
        }
    }
    $value = Q_Config::get('Users', 'vote', $_REQUEST['forType'], 'value', null);
    if (isset($value)) {
        $_REQUEST['value'] = $value;
    } else {
        if (!isset($_REQUEST['value'])) {
            $_REQUEST['value'] = 1;
        }
    }
    if ($_REQUEST['forType'] === 'Users/hinted') {
        $hinted = Q::ifset($_SESSION, 'Users', 'hinted', array());
        if (!in_array($_REQUEST['forId'], $hinted)) {
            $_SESSION['Users']['hinted'][] = $_REQUEST['forId'];
        }
    }
    $vote = new Users_Vote();
    $vote->userId = $user->id;
    $vote->forType = $_REQUEST['forType'];
    $vote->forId = $_REQUEST['forId'];
    $vote->value = $_REQUEST['value'];
    $retrieved = $vote->retrieve();
    /**
     * @event Users/vote {before}
     * @return {string}
     */
    if (false === Q::event('Users/vote', compact('user', 'vote'), 'before')) {
        return;
    }
    if (!$retrieved) {
        $vote->save();
    }
    $vote = $vote->exportArray();
    $vote['retrieved'] = $retrieved;
    Users::$cache['vote'] = $vote;
}
Exemplo n.º 2
0
/**
 * Returns a vote or value, if one is there
 * @param {string} forType the type of thing to vote for
 * @param {string} forId string uniquely identifying that thing among others of its type
 */
function Users_vote_response()
{
    if (isset(Users::$cache['vote'])) {
        $vote = Users::$cache['vote'];
    } else {
        $required = array('forType', 'forId');
        foreach ($required as $field) {
            if (empty($_REQUEST[$field])) {
                throw new Q_Exception_RequiredField(compact('field'));
            }
        }
        $user = Users::loggedInUser(true);
        $vote = new Users_Vote();
        $vote->userId = $user->id;
        $vote->forType = $_REQUEST['forType'];
        $vote->forId = $_REQUEST['forId'];
        $retrieved = $vote->retrieve();
        $vote = $vote->exportArray();
        $vote['retrieved'] = $retrieved;
    }
    Q_Response::setSlot('vote', $vote ? $vote : false);
    Q_Response::setSlot('value', $vote ? $vote['value'] : false);
}
Exemplo n.º 3
0
 /**
  * Calculates total votes
  * @method beforeRemove
  * @param {array} $pk
  * @return {boolean}
  */
 function beforeRemove($pk)
 {
     $vote = new Users_Vote();
     $vote->userId = $this->userId;
     $vote->forType = $this->forType;
     $vote->forId = $this->forId;
     if ($vote->retrieve()) {
         $total = new Users_Total();
         $total->forType = $vote->forType;
         $total->forId = $vote->forId;
         if ($total->retrieve('*', false, array('begin' => true))) {
             $weightTotal = $total->weightTotal;
             $total->set('transaction', true);
             $total->weightTotal -= $vote->weight;
             if (!$total->weightTotal) {
                 $total->value = 0;
             } else {
                 $total->value = ($total->value * $weightTotal - $vote->value * $vote->weight) / $total->weightTotal;
             }
             $total->voteCount -= 1;
         } else {
             // something is wrong ... if there are votes, there should have been a total
             $total->weightTotal = 0;
             $total->voteCount = 0;
             $total->value = 0;
         }
         $this->set('total', $total);
     }
     return true;
 }