You are being redirected, if page does not change, click here.
*/ class Poll { var $num_polls = 0; var $current_poll = 0; var $last_poll_id = 0; var $errstr = ""; var $ids = array(); var $enabled_ids = array(); var $is_connected = false; var $dbh = NULL; var $poll = NULL; var $results = NULL; var $uid = NULL; var $store = NULL; var $fetch_mode = POLL_FETCHMODE_RANDOM; /** {{{ }}} Constructor */ function Poll() { $this->dbh = DB::connect(sprintf("%s://%s:%s@%s/%s", POLL_DB_DRIVER, POLL_DB_USER, POLL_DB_PASSWD, defined("POLL_DB_PORTNUM") ? POLL_DB_HOST . ":" . POLL_DB_PORTNUM : POLL_DB_HOST, POLL_DB_NAME)); if (DB::isError($this->dbh)) { die($this->dbh->getMessage()); } $this->is_connected = true; $this->refresh(); } /** * If the connection is still alive, refresh() will refresh all of * this instances' poll id information. * * @boolean true for success, false otherwise. */ function refresh() { if (!DB::isError($this->dbh)) { $this->enabled_ids = array(); $this->ids = array(); if (DB::isError($res = $this->dbh->query("SELECT id,enabled FROM " . POLL_TBL_POLLS))) { $this->errstr = $res->getMessage(); die($res->getMessage()); } while ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) { array_push($this->ids, $row[0]); if ($row[1] == 'Y') array_push($this->enabled_ids, $row[0]); } $this->num_polls = sizeof($this->ids); $this->last_poll_id = $this->ids[sizeof($this->ids)-1]; $res->free(); return true; } return false; } /** * Sets the user associated with this poll if there is one. * * @param string|int $uid the uid of the associated user. */ function setUser($uid) { $this->uid = $uid; } /** * Sets the type of fetchmode for retrieving poll data. * * @param int $mode the type of fetchmode. */ function setPollFetchMode($mode) { $this->fetch_mode = $mode; } /** * Gets a poll. * * @param optional int $fetch_mode the type of fetchmode. * @param optional int $poll_id the polls id. * * @return mixed an array containing the poll data for success, false on failure */ function getPoll($fetch_mode = NULL, $pollid = NULL) { if (!DB::isError($this->dbh)) { if ($fetch_mode == NULL) $fetch_mode = $this->fetch_mode; $row_num = NULL; $sql = ""; switch ($fetch_mode) { case POLL_FETCHMODE_RANDOM: if (POLL_BUSERWATCH && $this->uid != NULL) { $done = false; $i = 0; while (!$done && $i < $this->numPollsEnabled()) { $row_num = rand(0, $this->numPollsEnabled() - 1); $res = $this->dbh->query(sprintf("SELECT FROM %s WHERE uid != %d AND poll_id=%d", POLL_TBL_USERWATCH, $this->uid, $this->enabled_ids[$row_num])); if (!DB::isError($res) || $res != NULL) { $done = true; } $i++; if ($i == $this->numPollsEnabled() && !done) { die("User has done all polls"); } } } else { $row_num = rand(0, $this->numPollsEnabled() - 1); } $res = $this->dbh->query(sprintf("SELECT * FROM %s WHERE id='%d'", POLL_TBL_POLLS, $this->enabled_ids[$row_num])); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } $this->poll = $res->fetchRow(DB_FETCHMODE_ASSOC); return $this->poll; case POLL_FETCHMODE_ALL: if ($this->current_poll > $this->num_polls) { $this->current_poll = 0; return false; } $row_num = $this->current_poll++; break; case POLL_FETCHMODE_ONE: // fall through default: break; } if ($pollid != NULL) { $res = $this->dbh->query(sprintf("SELECT * FROM %s WHERE id='%d'", POLL_TBL_POLLS, $pollid)); } else { $res = $this->dbh->query(sprintf("SELECT * FROM %s", POLL_TBL_POLLS)); } if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } $this->poll = $res->fetchRow(DB_FETCHMODE_ASSOC, $row_num); if ($mode != POLL_FETCHMODE_ALL || ($mode == POLL_FETCHMODE_ALL && $this->current_poll > $this->num_polls)) $res->free(); return $this->poll; } return false; } function numPolls() { return sizeof($this->ids); } function numPollsEnabled() { return sizeof($this->enabled_ids); } /** * Sets a poll. * * @param string $question the poll question. * @param array $options an array containing the options for the poll * @param optional int $pollid the poll id number * * @return boolean true for success, false otherwise */ function setPoll($question, $options, $pollid = NULL) { if (!DB::isError($this->dbh)) { $sql = ""; if ($pollid != NULL) { $sql = sprintf("UPDATE %s SET question='%s',", POLL_TBL_POLLS, $question); foreach ($options as $key => $val) { $sql .= sprintf(" %s='%s',", $key, $val); } $sql .= sprintf(" enabled='Y' WHERE id=%d", $pollid); } else { $sql = sprintf("INSERT INTO %s VALUES(NULL, '%s', %s, %s, %s, %s, 'Y')", POLL_TBL_POLLS, $question, $options['option1'] ? sprintf("'%s'", $options['option1']) : "NULL", $options['option2'] ? sprintf("'%s'", $options['option2']) : "NULL", $options['option3'] ? sprintf("'%s'", $options['option3']) : "NULL", $options['option4'] ? sprintf("'%s'", $options['option4']) : "NULL"); } if (DB::isError($res = $this->dbh->query($sql))) { $this->errstr = $res->getMessage(); die($res->getMessage()); } else { $sql = sprintf("INSERT INTO %s VALUES (NULL, '0', '0', '0', '0')", POLL_TBL_RESULTS); $res = $this->dbh->query($sql); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } $this->refresh(); return true; } } return false; } /** * Gets the results of a poll. * * @param int $pollid the poll id number * @return mixed an array containing the poll data for success, false on failure */ function getResults($pollid) { if (!DB::isError($this->dbh)) { $res = $this->dbh->query(sprintf("SELECT *, SUM(option1+option2+option3+option4) AS sum FROM %s WHERE poll_id=%d GROUP BY option4", POLL_TBL_RESULTS, $pollid)); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } $this->results = $res->fetchRow(DB_FETCHMODE_ASSOC); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } $res->free(); $results = array('percent' => array( 'option1' => $this->results['sum'] != 0 ? sprintf("%01.2f", $this->results['option1'] / $this->results['sum'] * 100) : 0, 'option2' => $this->results['sum'] != 0 ? sprintf("%01.2f", $this->results['option2'] / $this->results['sum'] * 100) : 0, 'option3' => $this->results['sum'] != 0 ? sprintf("%01.2f", $this->results['option3'] / $this->results['sum'] * 100) : 0, 'option4' => $this->results['sum'] != 0 ? sprintf("%01.2f", $this->results['option4'] / $this->results['sum'] * 100) : 0, ), 'votes' => array( $this->results['option1'], $this->results['option2'], $this->results['option3'], $this->results['option4'], $this->results['sum'] ) ); return $results; } return false; } /** * Adds a vote to the poll. * * @param int $pollid the poll id number * @param int $vote the users vote * @return boolean true for success, false otherwise */ function addResult($pollid, $vote) { if (!DB::isError($this->dbh)) { if (POLL_BUSERWATCH && $this->uid != NULL) { $this->dbh->query(sprintf("INSERT INTO %s VALUES(%d, %d, NULL, '%s')", POLL_TBL_USERWATCH, $this->uid, $pollid, $vote)); } $sql = sprintf("UPDATE %s SET option%d=option%d+1 WHERE poll_id='%d'", POLL_TBL_RESULTS, $vote, $vote, $pollid); if (DB::isError($res = $this->dbh->query($sql))) { $this->errstr = $res->getMessage(); die($res->getMessage()); } return true; } return false; } /** * Delets a poll. * * @param int $pollid the poll id number * @return boolean true for success, false otherwise */ function delete($pollid) { if (!DB::isError($this->dbh)) { $res = $this->dbh->query(sprintf("DELETE FROM %s WHERE id='%d'", POLL_TBL_POLLS, $pollid)); $res = $this->dbh->query(sprintf("DELETE FROM %s WHERE poll_id='%d'", POLL_TBL_RESULTS, $pollid)); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } //$this->refresh(); return true; } return false; } /** * Sets a polls enable/disable property. * * @param int $pollid the poll id number * @param boolean $enabled whether or not this poll is enabled * @return boolean true for success, false otherwise */ function setEnabled($pollid, $enabled) { if (!DB::isError($this->dbh)) { $res = $this->dbh->query(sprintf("UPDATE %s SET enabled='%s' WHERE id=%d", POLL_TBL_POLLS, $enabled ? 'Y' : 'N', $pollid)); if (DB::isError($res)) { $this->errstr = $res->getMessage(); die($res->getMessage()); } return true; } return false; } }; ?>