File: /var/www/html/shootinschool/wp-content/plugins/shootin-school-plugin/instructor_history.php
<?php
function render_siab_instructor_history()
{
$id = $_GET['id'];
global $wpdb;
$instructor = $wpdb->get_row("SELECT display_name FROM " . DB_USERS . " WHERE ID = $id");
class Instructor_Appointment_History extends WP_List_Table
{
/**
* Prepare the items for the table to process
*
* @return Void
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
usort($data, array(&$this, 'sort_data'));
$perPage = 10;
$currentPage = $this->get_pagenum();
$totalItems = count($data);
$this->set_pagination_args(array(
'total_items' => $totalItems,
'per_page' => $perPage
));
$data = array_slice($data, (($currentPage - 1) * $perPage), $perPage);
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = array(
'id' => 'Appointment ID',
'child_id_name' => 'Child name',
'appointment_date' => 'Appointment date',
'appointment_time' => 'Appointment Time',
'status' => 'Status',
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return array('display_name' => array('display_name', false));
}
/**
* Get the table data
*
* @return Array
*/
private function table_data()
{
global $wpdb;
if ((!empty($_GET['start_date']) && !empty($_GET['end_date']))) {
$where .= ' AND ';
} else {
$where .= ' AND ';
$dd = date('d');
if ($dd > 15) {
$start_date = date('Y-m-d', strtotime(date('Y-m-16')));
$end_date = date('Y-m-d', strtotime(date('Y-m-t')));
} else {
$start_date = date('Y-m-d', strtotime(date('Y-m-01')));
$end_date = date('Y-m-d', strtotime(date('Y-m-15')));
}
}
if (!empty($_GET['start_date']) && !empty($_GET['end_date'])) {
$start_date = $_GET['start_date'];
$end_date = $_GET['end_date'];
}
$where .= DB_APPOINTMENTS . '.appointment_date >= CAST("' . $start_date . '" AS DATE) AND ' . DB_APPOINTMENTS . '.appointment_date <= CAST("' . $end_date . '" AS DATE)';
$query = "SELECT " . DB_APPOINTMENTS . ".* FROM "
. DB_APPOINTMENTS . " JOIN " . DB_USERS . "
as us ON us.ID = " . DB_APPOINTMENTS . ".customer_id JOIN " . DB_APPOINTMENT_INSTRUCTORS . " as ins ON ins.appt_id = " . DB_APPOINTMENTS . ".id WHERE
ins.instructor_id = " . $_GET['id'] . $where . " GROUP BY
" . DB_APPOINTMENTS . ".location_id," . DB_APPOINTMENTS . ".appointment_time," . DB_APPOINTMENTS . ".appointment_date," . DB_APPOINTMENTS . ".session_id ORDER BY " . DB_APPOINTMENTS . ".id DESC";
// var_dump($query);
// exit();
$data = $wpdb->get_results($query, ARRAY_A);
foreach ($data as $key1 => $item) {
global $wpdb;
$query = "SELECT child_id_name,is_cancelled,has_attended FROM " . DB_APPOINTMENTS . "
WHERE instructor_id = " . $_GET['id'] . " AND
location_id = " . $item['location_id'] . " AND appointment_date = '" . $item['appointment_date'] . "'
AND session_id = 1 AND appointment_time ='" . $item['appointment_time'] . "'";
$childrens = $wpdb->get_results($query, ARRAY_A);
$child_data = "";
$status = "";
// var_dump(count($childrens));
if (count($childrens) > 1) {
$child_data = "<div style='margin-left:8px'><ul style='list-style-type:circle'>";
$status = "<div style='margin-left:8px'><ul style='list-style-type:circle'>";
foreach ($childrens as $key2 => $child) {
$child_data .= "<li>" . $child['child_id_name'] . "</li>";
if ($child['is_cancelled'] == 1) {
$status .= "<li>Cancelled</li>";
} else {
if ($child['has_attended'] == 1) {
$status .= "<li>Attended</li>";
} else if ($child['has_attended'] != NULL) {
$status .= "<li>Not attended</li>";
} else if ($child['is_approved'] == 1) {
$status .= "<li>Approved</li>";
} else {
$status .= "<li>Assigned</li>";
}
}
}
$child_data .= "</ul></div>";
$status .= "</ul></div>";
$data[$key1]['child_id_name'] = $child_data;
$data[$key1]['status'] = $status;
// $data[$key1]['comments'] = $comment_data;
} else {
if ($item['is_cancelled'] == 1) {
$data[$key1]['status'] = $status = "Cancelled";
} else {
if ($item['has_attended'] == 1) {
$data[$key1]['status'] = $status = "Attended";
} else if ($item['has_attended'] != NULL) {
$data[$key1]['status'] = $status = "Not attended";
} else if ($item['is_approved'] == 1) {
$data[$key1]['status'] = $status = "Approved";
} else {
$data[$key1]['status'] = $status = "Assigned";
}
}
}
}
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default($item, $column_name)
{
switch ($column_name) {
case 'id':
case 'child_id_name':
return $item[$column_name];
case 'appointment_date':
return date('l, F d Y ', strtotime($item[$column_name]));
case 'appointment_time':
$cust_split = explode(' - ', $item[$column_name]);
$app_time = date('g:i A', strtotime($cust_split[0])) . " - " . date('g:i A', strtotime($cust_split[1]));
return $app_time;
case 'status':
// if($item['is_approved'] == 0 && $item['is_cancelled'] == 0 && $item['instructor_id'] != NULL ){
// return "Instructor Approval pending";
// }else if ($item['is_cancelled'] == 0){
// if($item['has_attended'] != NULL && $item['has_attended'] == 1){
// return "Attended";
// } else if($item['has_attended'] != NULL && $item['has_attended'] == 0){
// return "Not Attended";
// } else{
// return " Instructor Assigned";
// }
// }else {
// return "Cancelled";
// }
return $item['status'];
default:
return print_r($item, true);
}
}
// function column_actions($item)
// {
// $actions = array(
// 'view history' => sprintf('<a href="?page=instructor_history&id=' . $item['ID'] . '">View</a>'),
// 'edit' => sprintf('<a href="?page=edit_instructor&id=' . $item['ID'] . '">Edit</a>'),
// 'delete' => sprintf('<a href="?page=siab-instructors&action=delete&id=' . $item['ID'] . '">Delete</a>'),
// );
// return sprintf(
// // '%1$s <span style="color:silver">(Edit:%2$s)</span>%3$s',
// /*$1%s*/
// // $item['title'],
// /*$2%s*/
// // $item['id'],
// /*$3%s*/
// $this->row_actions($actions)
// );
// }
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data($a, $b)
{
$start1 = explode(' - ', $a['appointment_time']);
$start2 = explode(' - ', $b['appointment_time']);
$date1 = strtotime($a['appointment_date']);
$date2 = strtotime($b['appointment_date']);
$result = strcmp($date1, $date2);
if ($result == 0) {
$result1 = strcmp(strtotime($start1[0]), strtotime($start2[0]));
return $result1;
}
return $result;
}
}
/**
* Display the list table page
*
* @return Void
*/
$importedListTable = new Instructor_Appointment_History();
$importedListTable->prepare_items();
$dd = date('d');
if ($dd > 15) {
$start_date = date('Y-m-d', strtotime(date('Y-m-16')));
$end_date = date('Y-m-d', strtotime(date('Y-m-t')));
} else {
$start_date = date('Y-m-d', strtotime(date('Y-m-01')));
$end_date = date('Y-m-d', strtotime(date('Y-m-15')));
}
// $end_date ='';
if (!empty($_GET['start_date'])) {
$start_date = $_GET['start_date'];
}
if (!empty($_GET['end_date'])) {
$end_date = $_GET['end_date'];
}
$issupervisor = get_user_meta($id, $key = 'issupervisor');
?>
<div class="wrap">
<div class="alert alert-info" role="alert">
<?php
if ($issupervisor[0] == 1) { ?>
<h3> Supervisor Appointment Booking History </h3>
<a class="add-new-h2 btn btn-success" href="admin.php?page=siab-supervisors"> Back to Supervisor Listing</a>
<?php } else { ?>
<h3> Instructor Appointment Booking History </h3>
<a class="add-new-h2 btn btn-success" href="admin.php?page=siab-instructors"> Back to Instructors Listing</a>
<?php } ?>
</div>
</div>
<div class="container" style="margin-top:19px">
<label>Start Date</label>
<input type="date" value="<?php echo $start_date; ?>" name="start_date" id="start_date1" placeholder="Start Date">
<label>End Date</label>
<input type="date" value="<?php echo $end_date; ?>" name="end_date" id="end_date1" placeholder="End Date">
<input class="btn btn-primary btn-sm" type="button" value="Apply" onclick="ApplyDateFilter()">
<a class="btn btn-default btn-sm" href="admin.php?page=instructor_history&id=<?= $id ?>"> Reset</a>
<hr>
</div>
<div class="wrap">
<div id="icon-users" class="icon32"></div>
<h2>Bookings of <?php echo $instructor->display_name ?></h2>
<?php $importedListTable->display(); ?>
</div>
<?php
}