Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save omerrciftcii/d6412593499312014399dd0f67c65fdb to your computer and use it in GitHub Desktop.

Select an option

Save omerrciftcii/d6412593499312014399dd0f67c65fdb to your computer and use it in GitHub Desktop.
public function get_experts()
{
// Set a script execution timeout
set_time_limit(60);
$headers = $this->input->request_headers();
header('Content-Type: application/json');
try {
// Start timing the execution
$start_time = microtime(true);
// Get pagination parameters
$page = $this->input->get('page') ? (int)$this->input->get('page') : 1;
$limit = $this->input->get('limit') ? (int)$this->input->get('limit') : 20;
$offset = ($page - 1) * $limit;
// Get filter parameters
$category = $this->input->get('category');
$name = $this->input->get('name');
$country = $this->input->get('country');
$experience = $this->input->get('experience');
// Build the query with specific columns instead of SELECT *
$this->db->select('id, name, email, phone, thumb, category, country, experience_year, about_me, status');
$this->db->from('users');
$this->db->where('role', 'user');
// Apply filters if provided
if (!empty($category)) {
$this->db->where('category', $category);
}
if (!empty($name)) {
$this->db->like('name', $name);
}
if (!empty($country)) {
$this->db->like('country', $country);
}
if (!empty($experience)) {
$this->db->like('experience_year', $experience);
}
// Get total count for pagination
$total_records = $this->db->count_all_results('', false);
// Apply pagination
$this->db->limit($limit, $offset);
// Order by a relevant column
$this->db->order_by('id', 'DESC');
// Execute the query
$query = $this->db->get();
$experts = $query->result();
// Log execution time
$execution_time = microtime(true) - $start_time;
log_message('info', 'get_experts execution time: ' . $execution_time . ' seconds');
// Prepare pagination info
$total_pages = ceil($total_records / $limit);
if (!empty($experts)) {
echo json_encode(array(
'status' => 'success',
'data' => $experts,
'pagination' => array(
'current_page' => $page,
'total_pages' => $total_pages,
'total_records' => $total_records,
'limit' => $limit
)
));
} else {
echo json_encode(array('status' => 'error', 'msg' => 'No experts found'));
}
} catch (Exception $e) {
log_message('error', 'Error in get_experts: ' . $e->getMessage());
echo json_encode(array('status' => 'error', 'msg' => 'Database error occurred: ' . $e->getMessage()));
}
}
public function update_booking()
{
$headers = $this->input->request_headers();
header('Content-Type: application/json');
try {
// Get JSON data from request
$data = json_decode($this->input->raw_input_stream, true);
if (empty($data['booking_id'])) {
echo json_encode(array('status' => 'error', 'msg' => 'Booking ID is required'));
exit();
}
$booking_id = $data['booking_id'];
// Get the existing booking
$booking = $this->api_model->get_appointment_by_id($booking_id);
if (empty($booking)) {
echo json_encode(array('status' => 'error', 'msg' => 'Booking not found'));
exit();
}
// Fields that can be updated
$allowed_fields = ['date', 'time', 'status', 'is_start', 'is_completed'];
$update_data = array();
// Only update fields that are provided in the request
foreach ($allowed_fields as $field) {
if (isset($data[$field])) {
$update_data[$field] = $data[$field];
}
}
if (empty($update_data)) {
echo json_encode(array('status' => 'error', 'msg' => 'No fields to update'));
exit();
}
// Update the booking
$update_data = $this->security->xss_clean($update_data);
$this->common_model->edit_option($update_data, $booking_id, 'session_booking');
// Get the updated booking
$updated_booking = $this->api_model->get_appointment_by_id($booking_id);
echo json_encode(array('status' => 'success', 'booking' => $updated_booking));
} catch (\Throwable $th) {
echo json_encode(array('status' => 'error', 'msg' => $th->getMessage()));
exit();
}
}
public function upcoming_bookings($user_id, $user_type)
{
try {
// --- This is the full, correct query logic ---
$this->db->select('
sb.id, sb.user_id, sb.customer_id, sb.date, sb.time, sb.status,
sb.join_url, sb.host_url, sb.zoom_password, sb.is_start,
u.name as customer_name, u.email as customer_email, u.phone as customer_phone, u.thumb as customer_image,
e.name as expert_name, e.thumb as expert_image,
s.name as session_title, s.details as details, s.slug as session_slug, s.price as session_price, s.duration as session_duration
');
$this->db->from('session_booking sb');
$this->db->join('customers u', 'u.id = sb.customer_id', 'left'); // Correctly joins to CUSTOMERS table
$this->db->join('users e', 'e.id = sb.user_id', 'left');
$this->db->join('sessions s', 's.id = sb.session_id', 'left');
if ($user_type === 'expert') {
$this->db->where('sb.user_id', $user_id);
} else {
$this->db->where('sb.customer_id', $user_id);
}
$this->db->where('sb.date >=', date('Y-m-d'));
$this->db->where('sb.status !=', 2);
$this->db->order_by('sb.date', 'ASC');
$this->db->order_by('sb.time', 'ASC');
$this->db->limit(10);
// --- Execute the query ---
$query = $this->db->get();
// --- FINAL DEBUGGING: Check for a database error ---
if ($this->db->error()['message']) {
header('Content-Type: text/plain');
die("DATABASE ERROR: " . $this->db->error()['message'] . "\n\nLAST QUERY:\n" . $this->db->last_query());
}
// --- If no error, proceed as normal ---
header('Content-Type: application/json');
$bookings = $query->result();
if (!empty($bookings)) {
echo json_encode(array('status' => 'success', 'bookings' => $bookings));
} else {
echo json_encode(array('status' => 'error', 'msg' => 'No upcoming bookings found'));
}
} catch (Exception $e) {
header('Content-Type: text/plain');
die('PHP EXCEPTION: ' . $e->getMessage());
}
}
public function expert_bookings_with_filter($user_id)
{
$headers = $this->input->request_headers();
header('Content-Type: application/json');
// Get bookings with detailed information
$this->db->select('b.*,
s.name as session_name,
s.details as session_details,
s.price as session_price,
s.duration as session_duration,
s.category as session_category,
s.type as session_type,
c.name as customer_name,
c.email as customer_email,
c.phone as customer_phone,
c.thumb as customer_image');
$this->db->from('session_booking as b');
$this->db->join('sessions as s', 's.id = b.session_id', 'LEFT');
$this->db->join('customers as c', 'c.id = b.customer_id', 'LEFT');
$this->db->where('b.user_id', $user_id);
// Apply filters based on GET parameters
if(!empty($_GET['search']) && $_GET['search'] == 'upcoming'){
$this->db->where('b.date < DATE_ADD(now(), INTERVAL 7 DAY) AND b.date > NOW()');
}
if(!empty($_GET['search']) && $_GET['search'] == 'pending'){
$this->db->where('b.status', 0);
}
if(!empty($_GET['search']) && $_GET['search'] == 'completed'){
$this->db->where('b.status', 3);
}
if(!empty($_GET['session']) && $_GET['session'] != 'all'){
$this->db->where('b.session_id', $_GET['session']);
}
$this->db->order_by('b.date', 'ASC');
$this->db->order_by('b.time', 'ASC');
$query = $this->db->get();
$bookings = $query->result();
if (!empty($bookings)) {
echo json_encode(array('status' => 'success', 'bookings' => $bookings));
} else {
echo json_encode(array('status' => 'error', 'msg' => 'No bookings found'));
}
}
public function get_meeting_details($booking_id)
{
$headers = $this->input->request_headers();
header('Content-Type: application/json');
// Get the booking
$booking = $this->api_model->get_appointment_by_id($booking_id);
if (empty($booking)) {
echo json_encode(array('status' => 'error', 'msg' => 'Booking not found'));
return;
}
// Check if meeting has started
if ($booking->is_start != '1') {
echo json_encode(array('status' => 'error', 'msg' => 'Meeting has not started yet'));
return;
}
// Return meeting details
echo json_encode(array(
'status' => 'success',
'meeting' => array(
'join_url' => $booking->join_url,
'password' => $booking->zoom_password,
'is_started' => $booking->is_start == '1',
'booking_id' => $booking->id,
'date' => $booking->date,
'time' => $booking->time
)
));
}
public function mentor_dashboard($expert_id)
{
$headers = $this->input->request_headers();
header('Content-Type: application/json');
// 1. Yaklaşan Randevu Sayısı (bugünden itibaren)
$upcomingAppointments = $this->db
->where('user_id', $expert_id)
->where('date >=', date('Y-m-d'))
->where('status !=', 2) // 2: iptal edilmiş
->count_all_results('session_booking');
// 2. Bu Haftaki Kazanç (bu haftanın başından itibaren onaylı randevuların toplamı)
$startOfWeek = date('Y-m-d', strtotime('monday this week'));
$endOfWeek = date('Y-m-d', strtotime('sunday this week'));
$this->db->select_sum('price');
$this->db->where('user_id', $expert_id);
$this->db->where('date >=', $startOfWeek);
$this->db->where('date <=', $endOfWeek);
$this->db->where('status', 3); // 3: tamamlandı
$weeklyEarningsRow = $this->db->get('session_booking')->row();
$weeklyEarnings = $weeklyEarningsRow ? $weeklyEarningsRow->price : 0;
// 3. Yeni Mesaj (örnek: okunmamış mesajlar)
$newMessages = $this->api_model->count_unread_messages($expert_id); // Modelde bu fonksiyonu yazmalısın
// 4. Profil Görüntülenme (örnek: bir tabloya kaydediyorsan)
$profileViews = $this->api_model->get_profile_views($expert_id); // Modelde bu fonksiyonu yazmalısın
echo json_encode([
'status' => 'success',
'data' => [
'upcomingAppointments' => $upcomingAppointments,
'weeklyEarnings' => $weeklyEarnings,
'newMessages' => $newMessages,
'profileViews' => $profileViews
]
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment