public static function reset_poll_stats_from_database($poll_id) { global $message; $current_poll = new YOP_POLL_Poll_Model($poll_id); $message = self::delete_result_from_db_by_poll_id($poll_id); $current_poll->poll_total_votes = 0; foreach ($current_poll->questions as &$question) { foreach ($question->answers as &$answer) { $answer->votes = 0; } } $current_poll->update_no_votes(); $current_poll->save(); }
public function add_votes() { $ok = 1; global $current_user; get_currentuserinfo(); $cheked = 0; global $message; $poll_total_votes = 0; if (isset($_POST['total_number_of_votes']) && $_POST['total_number_of_votes'] != 0) { $no_of_votes = $_REQUEST['total_number_of_votes']; } $add_to_results = ""; $current_poll = new YOP_POLL_Poll_Model($_POST['poll_id']); if (current_user_can('add_yop_poll_votes')) { $index = 0; $i = 0; $message['append_row'] = ""; $append_row = ""; while ($ok == 1) { // yop_poll_dump($answer_details); $ok = 0; $i = 0; $details = ""; $max = 100; $question_count = 0; foreach ($current_poll->questions as $question) { $bulkanswers = $_POST['yoppollresultsanswerscheck'][$question->ID]; $details .= $question->question . ": "; $answer_count = 0; foreach ($question->answers as $answer) { $answer_count++; if (isset($bulkanswers)) { foreach ($bulkanswers as $bulkanswer) { if ($bulkanswer == $answer->ID) { $cheked = 1; if ($_POST['yop_poll_no_of_votes_' . $answer->ID . '_per_answer'] != "") { if ($_POST['yop_poll_no_of_votes_' . $answer->ID . '_per_answer'] != 0) { if ($_POST['yop_poll_no_of_votes_' . $answer->ID . '_per_answer'] > $index) { $answer_details["q-" . $question->ID]['question'] = $question->question . ": "; $answer_details["q-" . $question->ID]['id'] = $question->ID; $ok = 1; $details .= $answer->answer . ","; $add_to_results .= $answer->answer; $answer->votes = $answer->votes + 1; $answer_details["q-" . $question->ID]['a'][] = $answer->ID; $answer_details["q-" . $question->ID]['answers'][] = $answer->answer; $a = new YOP_POLL_Answer_Model($answer->ID); $a->votes++; $a->update(); $poll_total_votes++; } } else { $message['error'] = "Answer input from answer " . $answer_count . " Question " . ($question_count + 1) . " is zero!"; wp_die(json_encode($message)); } } else { $message['error'] = "Answer input from answer " . $answer_count . " Question " . ($question_count + 1) . " is not set!"; wp_die(json_encode($message)); } } } } } $details .= "<br>"; $question_count++; } if ($ok == 1) { $current_poll->poll_total_votes += $poll_total_votes; $poll_total_votes = 0; $current_poll->update_no_votes(); $current_poll->save(); $result['vote_details'] = json_encode($answer_details); $result['poll_id'] = $_POST['poll_id']; $result['ip'] = yop_poll_get_ip(); $result['user_type'] = "admin"; $result['vote_id'] = uniqid('vote_id_'); $result['user_id'] = $current_user->ID; $message = insert_result_in_db($result); $index++; $append_row[$i] .= " <tr valign=" . 'middle' . " class=" . 'alternate' . "\r\n id=" . 'yop-poll-log{{log.id}}' . ">\r\n <th class=" . 'check-column' . " scope=" . 'row' . ">\r\n <input type=" . 'checkbox' . " value=" . $message['insert_id'] . "\r\n name=" . 'yoppollresultscheck[]' . ">\r\n </th>\r\n <td><strong>" . $result['vote_id'] . "</strong>\r\n <br>\r\n <div class=" . 'row-actions' . ">\r\n <span class=" . 'delete' . ">\r\n <a\tonclick=" . 'return confirm( ' . ' "You are about to delete this result" ' . ') : "Cancel" "to stop" , "OK" "to delete' . ')' . "\r\n\r\n href=" . '?page=yop-polls&action=delete_result&resultid=' . $message['insert_id'] . "\r\n\r\n class=" . 'submitdelete' . ">Delete</a></span>|<span class=" . 'delete' . ">\r\n <a\r\n onclick=" . 'show_pop_up_ban(' . ($max + 1) . ')' . "\r\n class=" . 'submitdelete' . ">Ban</a></span>\r\n </div></td>\r\n <td style=" . 'display:none;' . " class=" . 'hidden_tds' . "><input id=" . 'yop-poll-results-ip_' . ($max + 1) . " value=" . $result['ip'] . "><input></td>\r\n <td style=" . 'display: none' . " class=" . 'hidden_tds' . "><input id=" . 'yop-poll-results-userid_' . ($max + 1) . " value=" . $current_user->ID . "><input></td>\r\n <td>\r\n Admin\r\n </td>\r\n <td>\r\n </td>\r\n <td>\r\n " . $result['ip'] . "\r\n </td>\r\n <td>\r\n " . current_time('mysql') . "\r\n </td>\r\n <td class=" . 'more_details' . " style=" . '"' . 'cursor:pointer;' . '"' . ">\r\n </td>\r\n <td class=" . 'less_details' . " style=" . '"' . 'cursor:pointer;' . '"' . " >\r\n </td>\r\n </tr>\r\n <tr class=" . 'results_details' . " class=" . 'hidden_tds' . ">\r\n <td></td> <td></td>\r\n <td> Questions<br><strong>" . $details . "</strong><br></td>\r\n </tr>"; // yop_poll_dump($i); $i = $i + 1; unset($answer_details); } if ($cheked == 0) { $message['error'] = "You must select at least one answer!"; wp_die(json_encode($message)); } } } else { $message['error'] = "You don't have enough permissions do add a vote!"; wp_die(json_encode($message)); } //yop_poll_dump($append_row); foreach ($append_row as $a) { $message['append_row'] .= $a; } wp_die(json_encode($message)); // self::view_results_votes(); }