/**
  * Generates columns.
  * 
  * @param CH_Contest $item Current contest.
  * @param string $column_name Current column text ID.
  */
 function column_default($item, $column_name)
 {
     switch ($column_name) {
         case 'title':
             $headline = isset($item->ch_headline) ? esc_html($item->ch_headline) : '';
             $output = '<b>' . esc_html($item->ID) . ':</b> <a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID) . '">' . $headline . '</a>';
             $actions = array();
             $actions['edit'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID) . '">' . __('Edit') . '</a>';
             $actions['delete'] = '<a class="confirm_delete" href="' . admin_url('admin.php?page=' . CH_Page_List::page_id . '&action=del&contest=' . $item->ID) . '">' . __('Delete') . '</a>';
             $actions['dashboard'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Contest::page_id . '&contest=' . $item->ID . '&ch_page=dashboard') . '">' . __('Dashboard', 'contesthopper') . '</a>';
             $actions['participants'] = '<a href="' . admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $item->ID) . '">' . __('Participants', 'contesthopper') . '</a>';
             $output .= $this->row_actions($actions);
             return $output;
         case 'participants':
             $part_num = CH_Participant::get_num($item->ID);
             $part_num_all = CH_Participant::get_num($item->ID, 'all');
             $participant_num = $part_num;
             if ($part_num_all != $part_num) {
                 $participant_num .= ' (' . $part_num_all . ')';
             }
             return $participant_num;
         case 'date_start':
             $date_start = isset($item->ch_date_start) ? $item->ch_date_start : '';
             return $date_start;
         case 'duration':
             $date_start = isset($item->ch_date_start) ? $item->ch_date_start : '';
             $date_end = isset($item->ch_date_end) ? $item->ch_date_end : '';
             $start_time = strtotime($date_start);
             $end_time = strtotime($date_end);
             $duration = $end_time - $start_time;
             $duration_text = '';
             if ($duration < 3600 * 24) {
                 $hrs = round($duration / 3600);
                 $duration_text = $hrs . ' hour(s)';
             } else {
                 $days = intval(floor($duration / 86400));
                 $hrs = round(($duration - $days * 86400) / 3600);
                 $duration_text = $days . ' day(s), ' . $hrs . ' hour(s)';
             }
             $duration_text .= ' (ends ' . $item->ch_date_end . ')';
             return $duration_text;
         case 'status':
             $status = isset($item->ch_status) ? $item->ch_status : '';
             $isexpired = $item->is_expired();
             $isstarted = $item->is_started();
             $contest_status = __('Expired', 'contesthopper');
             if (!$isstarted) {
                 $contest_status = __('Not active yet', 'contesthopper');
             } else {
                 if ($isexpired && $item->ch_status == 'expired') {
                     $contest_status = __('Expired (forced)', 'contesthopper');
                 } else {
                     if ($isexpired && $item->ch_status == 'active') {
                         $contest_status = __('Expired (automatic)', 'contesthopper');
                     } else {
                         if ($isexpired && $item->ch_status == 'winners_picked') {
                             $contest_status = __('Expired (winners picked)', 'contesthopper');
                         } else {
                             if ($isstarted && !$isexpired) {
                                 $contest_status = __('Active', 'contesthopper');
                             }
                         }
                     }
                 }
             }
             return $contest_status;
         default:
             return '';
     }
 }
 /** 
  * 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', 'contesthopper');
     if (!$isstarted) {
         $contest_status = __('Not active yet', 'contesthopper');
     } else {
         if ($isexpired && $this->contest->ch_status == 'expired') {
             $contest_status = __('Expired (forced)', 'contesthopper');
         } else {
             if ($isexpired && $this->contest->ch_status == 'active') {
                 $contest_status = __('Expired (automatic)', 'contesthopper');
             } else {
                 if ($isexpired && $this->contest->ch_status == 'winners_picked') {
                     $contest_status = __('Expired (winners picked)', 'contesthopper');
                 } else {
                     if ($isstarted && !$isexpired) {
                         $contest_status = __('Active', 'contesthopper');
                     }
                 }
             }
         }
     }
     $part_num = CH_Participant::get_num($this->contest->ID);
     $part_num_all = CH_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->ch_status == 'expired') {
         // display stop / resume contest
         $url = admin_url('admin.php?page=' . CH_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', 'contesthopper');
         if ($this->contest->ch_status == 'expired') {
             $text = __('Resume Contest', 'contesthopper');
         }
         submit_button($text, 'button-secondary action', false, false);
         echo '</form>';
     }
     if ($this->contest->is_expired() && $this->contest->ch_status == 'winners_picked') {
         // display reset contest button
         $url = admin_url('admin.php?page=' . CH_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', 'contesthopper');
         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->ch_winners_num) && is_numeric($this->contest->ch_winners_num) && $this->contest->ch_winners_num > 0) {
         $winners_num = $this->contest->ch_winners_num;
     }
     if ($current_winners < $winners_num) {
         $confirm_class = '';
         if ($current_winners == 0 && $this->contest->ch_status != 'winners_picked') {
             $confirm_class = 'confirm_setwinner';
         }
         $text = __('Pick Random Winners', 'contesthopper');
         $url = admin_url('admin.php?page=' . CH_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', 'contesthopper');
         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=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
         $text = __('Clear All Winners', 'contesthopper');
         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', 'contesthopper') . '</h3>
     <div class="inside">    
     <table class="form-table">
     <tbody><tr valign="top"><th class="ch_label">' . __('Status', 'contesthopper') . '</th>
     <td>' . $contest_status . '</td>
     </tr>
     <tr valign="top"><th class="ch_label" style="vertical-align: middle">' . __('Number of participants', 'contesthopper') . '</th>
     <td style="vertical-align: middle">' . $participant_num . ' <a href="' . admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest']) . '" class="button">' . __('View all participants', 'contesthopper') . '</a></td>
     </tr>
     </table>
     </div></div>';
     if ($current_winners > 0) {
         $winners = CH_Participant::get_all($this->contest->ID, '', '', '', '', 'winner');
         echo '<div class="postbox">
         <h3>' . __('Winners', 'contesthopper') . '</h3>
         <div class="inside">
         <table class="form-table">
         <tbody>';
         foreach ($winners as $winner) {
             $url = admin_url('admin.php?page=' . CH_Page_Participants::page_id . '&contest=' . $_GET['contest'] . '&redirect=contest');
             $text = __('Remove Winner', 'contesthopper');
             echo '<tr valign="top"><th class="ch_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>';
 }
 /**
  * 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 = CH_Participant::get_all($contest_id, $orderby, $order, $page, $perpage, 'all');
     $this->found_posts = CH_Participant::get_num($contest_id);
     return $data;
 }