File: /var/www/html/shootinschool/wp-content/plugins/shootin-school-plugin/list_instructors.php
<?php
function render_siab_instructors()
{
class Instructor_List_Table 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(
// 'sl' => 'Sl No',
'display_name' => 'Name',
'user_email' => 'Email ID',
// 'user_registered' => 'Registration Date',
'actions' => 'Actions',
);
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;
$users_table = $wpdb->prefix . "users";
$args = array(
'role' => 'siab_instructor',
'orderby' => 'created_at',
'order' => 'DESC',
'fields' => array( 'ID' ),
'meta_query' => array(
array(
'key' => 'issupervisor',
'value' => 1,
'compare' => '!=' // You can change this if necessary (e.g., '!=' for not equal)
)
)
);
// $args = array(
// 'role' => 'siab_instructor',
// 'meta_query' => array(
// 'relation' => 'OR',
// array(
// 'key' => 'issupervisor',
// 'compare' => 'NOT EXISTS' // Check if the key doesn't exist
// ),
// array(
// 'key' => 'issupervisor',
// 'value' => '1',
// 'compare' => '!=' // Check if the value is not equal to 1
// )
// )
// );
// $instructors = get_users($args);
// // Step 2: Update metadata for each user found
// foreach ($instructors as $instructor) {
// update_user_meta($instructor->ID, 'issupervisor', 0);
// }
$userIds = get_users( $args );
$temp = array();
foreach ($userIds as $single) {
$temp[] = $single->ID;
}
if(count($temp) > 0){
$data = $wpdb->get_results("SELECT * FROM " .$users_table." WHERE ID IN (" .implode(',', $temp). ")", ARRAY_A);
} else{
$data = array();
}
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 'display_name':
case 'user_email':
// case 'user_registered':
return $item[$column_name];
default:
return print_r($item, true);
}
}
function column_actions($item)
{
$actions = array(
'view history' => sprintf('<a href="?page=instructor_history&id=' . $item['ID'] . '">View Appointments</a>'),
'edit' => sprintf('<a href="?page=edit_instructor&id=' . $item['ID'] . '">Edit</a>'),
'comments' => sprintf('<a href="admin.php?page=siab-admin-trainer-forum&id='.$item['ID'].'" >Comments</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)
{
// Set defaults
$orderby = 'directorName';
$order = 'asc';
// If orderby is set, use this as the sort column
if (!empty($_GET['orderby'])) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if (!empty($_GET['order'])) {
$order = $_GET['order'];
}
$result = strcmp($a[$orderby], $b[$orderby]);
if ($order === 'asc') {
return $result;
}
return -$result;
}
}
function delete_instructor() {
global $wpdb;
if (isset($_GET['id']) && !empty($_GET['id']) && ($_GET['action'] == 'delete')) {
$val = $_GET['id'];
$wpdb->query("DELETE FROM " . DB_INSTRUCTORS_AVAILABILITY . " WHERE instructor_id=".$val);
$wpdb->query("DELETE FROM " . DB_INSTRUCTORS_OFF_DAYS . " WHERE instructor_id=".$val);
$wpdb->query("DELETE FROM " . DB_USERS . " WHERE id='" . $val . "'");
echo '<div class="updated"><p><strong>Instructor has been deleted Successfully.</strong></p></div>';
}
}
/**
* Display the list table page
*
* @return Void
*/
$importedListTable = new Instructor_List_Table();
$importedListTable->prepare_items(); ?>
<div class="wrap">
<div class="alert alert-info" role="alert">
<h3> Instructors Management </h3>
<a class="add-new-h2" href="admin.php?page=add_instructor">Add New</a>
</div>
</div>
<div class="wrap">
<div id="icon-users" class="icon32"></div>
<h2>Instructor Listing</h2>
<?php
delete_instructor();
?>
<?php $importedListTable->display(); ?>
</div>
<?php
}