You are here

class VotingApi_Vote in Voting API 7.3

Model class for votingapi_vote table.

Hierarchy

Expanded class hierarchy of VotingApi_Vote

1 string reference to 'VotingApi_Vote'
VotingApi_VoteStorage::selectVotes in ./votingapi.module

File

./votingapi.module, line 16
A generalized voting API for Drupal.

View source
class VotingApi_Vote {
  public $vote_id = NULL;
  public $entity_type = 'node';
  public $entity_id = NULL;
  public $value_type = 'percent';
  public $value = NULL;
  public $tag = 'vote';
  public $uid = NULL;

  // defaults to current user
  public $vote_source = NULL;

  // defaults to current IP
  public $timestamp = REQUEST_TIME;
  public function __construct($data) {
    foreach ($data as $k => $v) {
      $this->{$k} = $v;
    }
    $this->uid = is_null($this->uid) ? $GLOBALS['user']->uid : $this->uid;
    $this->vote_source = is_null($this->vote_source) ? ip_address() : $this->vote_source;
  }

  /**
   * Save a collection of votes to the database.
   *
   * This function does most of the heavy lifting for VotingAPI the main
   * votingapi_set_votes() function, but does NOT automatically triger re-tallying
   * of results. As such, it's useful for modules that must insert their votes in
   * separate batches without triggering unecessary recalculation.
   *
   * Remember that any module calling this function implicitly assumes responsibility
   * for calling votingapi_recalculate_results() when all votes have been inserted.
   *
   * @param $votes array of VotingApi_Vote instances
   * @return
   *   The same votes, with vote_id keys populated.
   *
   * @see votingapi_set_votes()
   * @see votingapi_recalculate_results()
   */
  public static function saveMultiple(&$votes) {
    if (is_object($votes)) {
      $votes = array(
        $votes,
      );
    }
    foreach ($votes as $key => $vote) {
      $vote
        ->save();
    }
    module_invoke_all('votingapi_insert', $votes);
    return $votes;
  }

  /**
   * Delete votes from the database.
   *
   * @param $votes An array of votes to delete. Each vote must have the 'vote_id' key set.
   */
  public static function deleteMultiple($votes = array()) {
    if (!empty($votes)) {
      module_invoke_all('votingapi_delete', $votes);
      $vids = array();
      foreach ($votes as $vote) {
        $vids[] = $vote->vote_id;
      }
      VotingApi_VoteStorage::get()
        ->deleteVotes($votes, $vids);
    }
  }
  public function save() {
    VotingApi_VoteStorage::get()
      ->addVote($this);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VotingApi_Vote::$entity_id public property
VotingApi_Vote::$entity_type public property
VotingApi_Vote::$tag public property
VotingApi_Vote::$timestamp public property
VotingApi_Vote::$uid public property
VotingApi_Vote::$value public property
VotingApi_Vote::$value_type public property
VotingApi_Vote::$vote_id public property
VotingApi_Vote::$vote_source public property
VotingApi_Vote::deleteMultiple public static function Delete votes from the database.
VotingApi_Vote::save public function
VotingApi_Vote::saveMultiple public static function Save a collection of votes to the database.
VotingApi_Vote::__construct public function 1