Пример #1
0
 /**
  * Poll creation in an existing topic
  */
 function testCreatePollInTopic()
 {
     // Required values to create the poll with.
     $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);
     // it worked, right?
     $this->assertEqual($id_poll, associatedPoll($this->id_topic));
     // get some values from it
     $pollinfo = pollInfoForTopic($this->id_topic);
     $this->assertEqual($pollinfo['id_member_started'], 1);
     $this->assertEqual($pollinfo['question'], $question);
     $this->assertEqual($pollinfo['max_votes'], 1);
     // the default value
     $this->assertEqual($pollinfo['poll_starter'], 0);
     // lets use pollStarters() and test its result
     list($topic_starter, $poll_starter) = pollStarters($this->id_topic);
     $this->assertEqual($topic_starter, 1);
     $this->assertEqual($poll_starter, 0);
 }
Пример #2
0
 /**
  * Display screen for editing or adding a poll.
  * Must be called with a topic specified in the URL.
  * If the user is adding a poll to a topic, must contain the variable
  * 'add' in the url.
  * User must have poll_edit_any/poll_add_any permission for the
  * relevant action, otherwise must be poll starter with poll_edit_own
  * permission for editing, or be topic starter with poll_add_any permission for adding.
  * Accessed via ?action=editpoll.
  *
  * @uses Post language file.
  * @uses Poll template, main sub-template.
  */
 public function action_editpoll()
 {
     global $txt, $user_info, $context, $topic, $board, $scripturl;
     if (empty($topic)) {
         fatal_lang_error('no_access', false);
     }
     // We work hard with polls.
     require_once SUBSDIR . '/Poll.subs.php';
     loadLanguage('Post');
     loadTemplate('Poll');
     loadJavascriptFile('post.js', array(), 'post_scripts');
     $context['sub_template'] = 'poll_edit';
     $context['start'] = (int) $_REQUEST['start'];
     $context['is_edit'] = isset($_REQUEST['add']) ? 0 : 1;
     $poll_errors = Error_Context::context('poll');
     $pollinfo = pollInfoForTopic($topic);
     // Assume it all exists, right?
     if (empty($pollinfo)) {
         fatal_lang_error('no_board');
     }
     // If we are adding a new poll - make sure that there isn't already a poll there.
     if (!$context['is_edit'] && !empty($pollinfo['id_poll'])) {
         fatal_lang_error('poll_already_exists');
     } elseif ($context['is_edit'] && empty($pollinfo['id_poll'])) {
         fatal_lang_error('poll_not_found');
     }
     // Can you do this?
     if ($context['is_edit'] && !allowedTo('poll_edit_any')) {
         isAllowedTo('poll_edit_' . ($user_info['id'] == $pollinfo['id_member_started'] || $pollinfo['poll_starter'] != 0 && $user_info['id'] == $pollinfo['poll_starter'] ? 'own' : 'any'));
     } elseif (!$context['is_edit'] && !allowedTo('poll_add_any')) {
         isAllowedTo('poll_add_' . ($user_info['id'] == $pollinfo['id_member_started'] ? 'own' : 'any'));
     }
     $context['can_moderate_poll'] = isset($_REQUEST['add']) ? true : allowedTo('poll_edit_' . ($user_info['id'] == $pollinfo['id_member_started'] || $pollinfo['poll_starter'] != 0 && $user_info['id'] == $pollinfo['poll_starter'] ? 'own' : 'any'));
     // Do we enable guest voting?
     require_once SUBSDIR . '/Members.subs.php';
     $groupsAllowedVote = groupsAllowedTo('poll_vote', $board);
     // Want to make sure before you actually submit?  Must be a lot of options, or something.
     if (isset($_POST['preview']) || $poll_errors->hasErrors()) {
         $question = Util::htmlspecialchars($_POST['question']);
         // Basic theme info...
         $context['poll'] = array('id' => $pollinfo['id_poll'], 'question' => $question, 'hide_results' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'], 'change_vote' => isset($_POST['poll_change_vote']), 'guest_vote' => isset($_POST['poll_guest_vote']), 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']), 'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']));
         // Start at number one with no last id to speak of.
         $number = 1;
         $last_id = 0;
         // Get all the choices - if this is an edit.
         if ($context['is_edit']) {
             $pollOptions = pollOptions($pollinfo['id_poll']);
             $context['choices'] = array();
             foreach ($pollOptions as $option) {
                 // Get the highest id so we can add more without reusing.
                 if ($option['id_choice'] >= $last_id) {
                     $last_id = $option['id_choice'] + 1;
                 }
                 // They cleared this by either omitting it or emptying it.
                 if (!isset($_POST['options'][$option['id_choice']]) || $_POST['options'][$option['id_choice']] == '') {
                     continue;
                 }
                 // Add the choice!
                 $context['choices'][$option['id_choice']] = array('id' => $option['id_choice'], 'number' => $number++, 'votes' => $option['votes'], 'label' => $option['label'], 'is_last' => false);
             }
         }
         // Work out how many options we have, so we get the 'is_last' field right...
         $totalPostOptions = 0;
         foreach ($_POST['options'] as $id => $label) {
             if ($label != '') {
                 $totalPostOptions++;
             }
         }
         $count = 1;
         // If an option exists, update it.  If it is new, add it - but don't reuse ids!
         foreach ($_POST['options'] as $id => $label) {
             $label = Util::htmlspecialchars($label);
             censorText($label);
             if (isset($context['choices'][$id])) {
                 $context['choices'][$id]['label'] = $label;
             } elseif ($label != '') {
                 $context['choices'][] = array('id' => $last_id++, 'number' => $number++, 'label' => $label, 'votes' => -1, 'is_last' => $count++ == $totalPostOptions && $totalPostOptions > 1 ? true : false);
             }
         }
         // Make sure we have two choices for sure!
         if ($totalPostOptions < 2) {
             // Need two?
             if ($totalPostOptions == 0) {
                 $context['choices'][] = array('id' => $last_id++, 'number' => $number++, 'label' => '', 'votes' => -1, 'is_last' => false);
             }
             $poll_errors->addError('poll_few');
         }
         // Always show one extra box...
         $context['choices'][] = array('id' => $last_id++, 'number' => $number++, 'label' => '', 'votes' => -1, 'is_last' => true);
         $context['last_choice_id'] = $last_id;
         if ($context['can_moderate_poll']) {
             $context['poll']['expiration'] = $_POST['poll_expire'];
         }
         // Check the question/option count for errors.
         // @todo: why !$poll_errors->hasErrors()?
         if (trim($_POST['question']) == '' && !$poll_errors->hasErrors()) {
             $poll_errors->addError('no_question');
         }
         // No check is needed, since nothing is really posted.
         checkSubmitOnce('free');
         // Take a check for any errors... assuming we haven't already done so!
         $context['poll_error'] = array('errors' => $poll_errors->prepareErrors(), 'type' => $poll_errors->getErrorType() == 0 ? 'minor' : 'serious', 'title' => $context['is_edit'] ? $txt['error_while_editing_poll'] : $txt['error_while_adding_poll']);
     } else {
         // Basic theme info...
         $context['poll'] = array('id' => $pollinfo['id_poll'], 'question' => $pollinfo['question'], 'hide_results' => $pollinfo['hide_results'], 'max_votes' => $pollinfo['max_votes'], 'change_vote' => !empty($pollinfo['change_vote']), 'guest_vote' => !empty($pollinfo['guest_vote']), 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']));
         // Poll expiration time?
         $context['poll']['expiration'] = empty($pollinfo['expire_time']) || !$context['can_moderate_poll'] ? '' : ceil($pollinfo['expire_time'] <= time() ? -1 : ($pollinfo['expire_time'] - time()) / (3600 * 24));
         // Get all the choices - if this is an edit.
         if ($context['is_edit']) {
             $context['choices'] = getPollChoices($pollinfo['id_poll']);
             $last_id = max(array_keys($context['choices'])) + 1;
             // Add an extra choice...
             $context['choices'][] = array('id' => $last_id, 'number' => $context['choices'][$last_id - 1]['number'] + 1, 'votes' => -1, 'label' => '', 'is_last' => true);
             $context['last_choice_id'] = $last_id;
         } else {
             // Setup the default poll options.
             $context['poll'] = array('id' => 0, 'question' => '', 'hide_results' => 0, 'max_votes' => 1, 'change_vote' => 0, 'guest_vote' => 0, 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']), 'expiration' => '');
             // Make all five poll choices empty.
             $context['choices'] = array(array('id' => 0, 'number' => 1, 'votes' => -1, 'label' => '', 'is_last' => false), array('id' => 1, 'number' => 2, 'votes' => -1, 'label' => '', 'is_last' => false), array('id' => 2, 'number' => 3, 'votes' => -1, 'label' => '', 'is_last' => false), array('id' => 3, 'number' => 4, 'votes' => -1, 'label' => '', 'is_last' => false), array('id' => 4, 'number' => 5, 'votes' => -1, 'label' => '', 'is_last' => true));
             $context['last_choice_id'] = 4;
         }
     }
     $context['page_title'] = $context['is_edit'] ? $txt['poll_edit'] : $txt['add_poll'];
     $context['form_url'] = $scripturl . '?action=editpoll2' . ($context['is_edit'] ? '' : ';add') . ';topic=' . $context['current_topic'] . '.' . $context['start'];
     // Build the link tree.
     censorText($pollinfo['subject']);
     $context['linktree'][] = array('url' => $scripturl . '?topic=' . $topic . '.0', 'name' => $pollinfo['subject']);
     $context['linktree'][] = array('name' => $context['page_title']);
     // Register this form in the session variables.
     checkSubmitOnce('register');
 }