/**
  * Retrieves participant data.
  */
 function get_data()
 {
     $contest_id = '';
     if (!empty($this->contest)) {
         $contest_id = $this->contest->ID;
     }
     $data = array();
     $orderby = '';
     if (isset($_GET['orderby'])) {
         $orderby = $_GET['orderby'];
     }
     $order = '';
     if (isset($_GET['order'])) {
         $order = $_GET['order'];
     }
     $page = $this->get_pagenum();
     $perpage = $this->per_page;
     $data = cf_Participant::get_all($contest_id, $orderby, $order, $page, $perpage, 'all');
     $this->found_posts = cf_Participant::get_num($contest_id);
     return $data;
 }
 /** 
  * Generates custom content for dashboard tab.
  */
 protected function generate_dashboard()
 {
     $contest_status = '';
     $isstarted = $this->contest->is_started();
     $isexpired = $this->contest->is_expired();
     $contest_status = __('Expired', 'contestfriend');
     if (!$isstarted) {
         $contest_status = __('Not active yet', 'contestfriend');
     } else {
         if ($isexpired && $this->contest->cf_status == 'expired') {
             $contest_status = __('Expired (forced)', 'contestfriend');
         } else {
             if ($isexpired && $this->contest->cf_status == 'active') {
                 $contest_status = __('Expired (automatic)', 'contestfriend');
             } else {
                 if ($isexpired && $this->contest->cf_status == 'winners_picked') {
                     $contest_status = __('Expired (winners picked)', 'contestfriend');
                 } else {
                     if ($isstarted && !$isexpired) {
                         $contest_status = __('Active', 'contestfriend');
                     }
                 }
             }
         }
     }
     $part_num = cf_Participant::get_num($this->contest->ID);
     $part_num_all = cf_Participant::get_num($this->contest->ID, 'all');
     $participant_num = $part_num;
     if ($part_num_all != $part_num) {
         $participant_num .= ' (' . $part_num_all . ')';
     }
     echo '<div style="margin-top: 5px;">';
     // Dashboard buttons
     if (!$this->contest->is_expired() || $this->contest->cf_status == 'expired') {
         // display stop / resume contest
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="setexpired" value="1" />';
         $text = __('Stop Contest', 'contestfriend');
         if ($this->contest->cf_status == 'expired') {
             $text = __('Resume Contest', 'contestfriend');
         }
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     if ($this->contest->is_expired() && $this->contest->cf_status == 'winners_picked') {
         // display reset contest button
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="resetcontest" value="1" />';
         $text = __('Reset Contest', 'contestfriend');
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     // if not all winners were picked, show pick buttons
     $winners_num = 1;
     $current_winners = $this->contest->get_current_winner_num();
     if (isset($this->contest->cf_winners_num) && is_numeric($this->contest->cf_winners_num) && $this->contest->cf_winners_num > 0) {
         $winners_num = $this->contest->cf_winners_num;
     }
     if ($current_winners < $winners_num) {
         $confirm_class = '';
         if ($current_winners == 0 && $this->contest->cf_status != 'winners_picked') {
             $confirm_class = 'confirm_setwinner';
         }
         $text = __('Pick Random Winners', 'contestfriend');
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         echo '<form class="' . $confirm_class . '" action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="pickwinners" value="1" />';
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
         $text = __('Manually Pick Winners', 'contestfriend');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px">';
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     // if there are some winners picked, show clear winners button
     if ($current_winners > 0) {
         $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         $text = __('Clear All Winners', 'contestfriend');
         echo '<form action="' . $url . '" method="post" style="float: left; margin-left: 5px"><input type="hidden" name="clearwinners" value="1" />';
         submit_button($text, 'button-secondary action', false, false);
         echo '</a></form>';
     }
     echo '</div>
     <div id="poststuff" style="clear: both;">
     <div class="postbox">
     <h3>' . __('Contest Status', 'contestfriend') . '</h3>
     <div class="inside">    
     <table class="form-table">
     <tbody><tr valign="top"><th class="cf_label">' . __('Status', 'contestfriend') . '</th>
     <td>' . $contest_status . '</td>
     </tr>
     <tr valign="top"><th class="cf_label" style="vertical-align: middle">' . __('Number of participants', 'contestfriend') . '</th>
     <td style="vertical-align: middle">' . $participant_num . ' <a href="' . admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest']) . '" class="button">' . __('View all participants', 'contestfriend') . '</a></td>
     </tr>
     </table>
     </div></div>';
     if ($current_winners > 0) {
         $winners = cf_Participant::get_all($this->contest->ID, '', '', '', '', 'winner');
         echo '<div class="postbox">
         <h3>' . __('Winners', 'contestfriend') . '</h3>
         <div class="inside">
         <table class="form-table">
         <tbody>';
         foreach ($winners as $winner) {
             $url = admin_url('admin.php?page=' . cf_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
             $text = __('Remove Winner', 'contestfriend');
             echo '<tr valign="top"><th class="cf_label">' . $winner->email . ', ' . $winner->first_name . ' ' . $winner->last_name . '</th><td><form action="' . $url . '" method="post"><input type="hidden" name="removewinner" value="' . esc_attr($winner->id) . '" />';
             submit_button($text, 'button-secondary action', false, false);
             echo '</td></tr>';
         }
         echo '
         </table>    
         </div>
         </div>
         ';
     }
     echo '</div>';
 }
 /**
  * Pick winners.
  * @param int $num Number of winners to pick.
  */
 function pick_winners($num = 1)
 {
     if (!is_numeric($num) || $num < 1) {
         return false;
     }
     if (!$this->data['_valid']) {
         return false;
     }
     $participants = cf_Participant::get_all($this->data['ID'], '', '', '', '', 'not_winner');
     if (count($participants) < 1) {
         return;
     }
     $this->set_status('winners_picked');
     // generate a big entry_list
     $entry_list = array();
     $i = 0;
     foreach ($participants as $participant) {
         $entry_list[$i] = $participant->id;
         $i++;
         if (isset($participant->referral_to)) {
             $num_referrals = $participant->referral_to;
             $num_referrals = count($num_referrals);
             $entries_for_referral = 1;
             if (isset($this->data['cf_referral_entries']) && is_numeric($this->data['cf_referral_entries']) && $this->data['cf_referral_entries'] > 0) {
                 $entries_for_referral = $this->data['cf_referral_entries'];
             }
             for ($x = 0; $x < $num_referrals * $entries_for_referral; $x++) {
                 $entry_list[$i] = $participant->id;
                 $i++;
             }
         }
     }
     $admin_email = get_option('admin_email');
     // echo '<pre>';var_dump($entry_list);echo '</pre>';
     // check for random.org quota
     $url = 'http://www.random.org/quota/?format=plain';
     $curl = curl_init();
     curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_USERAGENT => $admin_email));
     $resp = curl_exec($curl);
     curl_close($curl);
     // echo '<pre>';var_dump(intval($resp));echo '</pre>';
     $min = 0;
     $max = count($entry_list) - 1;
     if (empty($resp) || intval($resp) <= 0) {
         $resp = range($min, $max);
         $result = shuffle($resp);
         if ($result) {
             $result = $resp;
         } else {
             die('Error generating random array.');
         }
     } else {
         $url = 'http://www.random.org/sequences/?min=' . $min . '&max=' . $max . '&col=1&format=plain&rnd=new';
         $curl = curl_init();
         curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_USERAGENT => $admin_email));
         $resp = curl_exec($curl);
         curl_close($curl);
         if (empty($resp) || strpos($resp, 'Error:') !== false) {
             $resp = range($min, $max);
             $result = shuffle($resp);
             if ($result) {
                 $result = $resp;
             } else {
                 die('Error generating random array.');
             }
         } else {
             $result = explode("\n", $resp);
             if (empty($result[count($result) - 1])) {
                 unset($result[count($result) - 1]);
             }
         }
     }
     //  echo '<pre>';var_dump($result);echo '</pre>';
     $winners_num = $num;
     if ($winners_num > count($participants)) {
         $winners_num = count($participants);
     }
     // echo '<pre>';var_dump($winners_num);echo '</pre>';
     $result_helper = 0;
     $winner_helper = 1;
     $winners = array();
     while ($winner_helper <= $winners_num) {
         $tmp = $result[$result_helper];
         $result_helper += 1;
         if (!in_array($entry_list[$tmp], $winners)) {
             $winners[$winner_helper] = $entry_list[$tmp];
             $winner_helper += 1;
         }
         if ($result_helper > count($result) - 1) {
             break;
         }
     }
     // echo '<pre>';var_dump($winners);echo '</pre>';
     foreach ($winners as $winner) {
         $participant = new cf_Participant($winner);
         $participant->set_status('winner');
     }
     return true;
 }