/** * Create a poll * * @param string $question The title/question of the poll * @param int $id_member = false The id of the creator * @param string $poster_name The name of the poll creator * @param int $max_votes = 1 The maximum number of votes you can do * @param int $hide_results = 1 If the results should be hidden * @param int $expire = 0 The time in days that this poll will expire * @param int $can_change_vote = 0 If you can change your vote * @param int $can_guest_vote = 0 If guests can vote * @param mixed[] $options = array() The poll options * @return int the id of the created poll */ function createPoll($question, $id_member, $poster_name, $max_votes = 1, $hide_results = 1, $expire = 0, $can_change_vote = 0, $can_guest_vote = 0, array $options = array()) { $expire = empty($expire) ? 0 : time() + $expire * 3600 * 24; $db = database(); $db->insert('', '{db_prefix}polls', array('question' => 'string-255', 'hide_results' => 'int', 'max_votes' => 'int', 'expire_time' => 'int', 'id_member' => 'int', 'poster_name' => 'string-255', 'change_vote' => 'int', 'guest_vote' => 'int'), array($question, $hide_results, $max_votes, $expire, $id_member, $poster_name, $can_change_vote, $can_guest_vote), array('id_poll')); $id_poll = $db->insert_id('{db_prefix}polls', 'id_poll'); if (!empty($options)) { addPollOptions($id_poll, $options); } call_integration_hook('integrate_poll_add_edit', array($id_poll, false)); return $id_poll; }
/** * Add options (choices) to a poll created with none. */ function testAddingOptionsToPoll() { // Values to create it first $question = 'Who is the next best contender for Grudge award?'; $id_member = 0; $poster_name = 'test'; // Create the poll. $id_poll = createPoll($question, $id_member, $poster_name); // Link the poll to the topic. associatedPoll($this->id_topic, $id_poll); $this->assertTrue($this->id_topic > 2); // extra-test just to double-check $id_poll = associatedPoll($this->id_topic); $this->assertTrue(!empty($id_poll)); // extra-test, just in case. $options = array('Ema, is that even a question?', 'Ant. He broke error log. (no, he didn\'t, but we\'ll say he did.)', 'No one this year'); addPollOptions($id_poll, $options); // Ok, what do we have now. $pollOptions = pollOptions($id_poll); $this->assertEqual($pollOptions[0]['label'], $options[0]); $this->assertEqual($pollOptions[1]['label'], $options[1]); $this->assertEqual($pollOptions[0]['id_choice'], 0); $this->assertEqual($pollOptions[1]['id_choice'], 1); $this->assertEqual($pollOptions[0]['votes'], 0); $this->assertEqual($pollOptions[1]['votes'], 0); }