Files
gsf/app/Http/Controllers/API/ManageActivityController.php
vedant-chavan 20f55281ef save to codehub
2024-08-09 17:11:41 +05:30

546 lines
25 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ActivityMaster;
use App\Models\SubscriptionMaster;
use App\Models\ActivitySchedule;
use App\Models\Teacher;
use App\Models\ManageFaq;
use App\Models\PastSession;
use App\Models\LeaderboardMaster;
use App\Models\UserContentView;
use App\Models\LinkFaqActivityMasterIds;
use App\Models\ActivityDay;
use App\Models\User;
use Validator;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Torann\GeoIP\Facades\GeoIP;
use Spatie\TimeZone\GoogleTimeZone;
use App\Helpers\Webhelper;
class ManageActivityController extends Controller {
public function get_manage_activity_id() {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$activityId = request('activityId');
$activity = ActivityMaster::with('subscription', 'schedule')->with('faq_activity_link.faqsData')->find($activityId);
if ($activity) {
return response()->json([
'success' => true,
'message' => 'Activity fetched successfully.',
'result' => $activity
]);
} else {
return response()->json([
'success' => false,
'message' => 'Activity not found.',
]);
}
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (\Exception $e) {
\Log::error("Activity data listing failed: " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something went wrong.',
]);
}
}
public function get_manage_activity(Request $request) {
// try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
// dd($user_id);
$user_data = User::where('id',$user_id)->first();
$utm_source = $user_data->utm_source;
$subscription_data = SubscriptionMaster::where('utm_plan',$utm_source)->first();
// dd($utm_source);
$subscription_id = $subscription_data->id;
// dd($subscription_id);
$current_day = Carbon::now()->toDateString();
// dd($current_day);
$manage_activity = ActivityDay::where('date', '=', $current_day)
// ->where('')
->where('subscription_id', $subscription_id)
->with('subscription')
->with('faq_activity_link.faqsData')
->with('scheduleData', 'activityData.subscription')
->with('activityData.teacher_data')
->get();
// dd($manage_activity);
// $userIP = $request->ip(); // Get the user's IP address // commented by Pradyumn; commented on 5 july 2024; Reason: not in use
// Make a GET request to ipinfo.io
// $response = Http::get("https://ipinfo.io/{$userIP}/json"); // commented by Pradyumn; commented on 5 july 2024; Reason: not in use
// Parse the JSON response
// commented by Pradyumn; commented on 5 july 2024; Reason: not in use
// $ch = curl_init("https://ipinfo.io/{$userIP}?token=fb9b51b5a3bbd9");
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($ch);
// curl_close($ch);
// $locationData = json_decode($response); // commented by Pradyumn; commented on 5 july 2024; Reason: not in use
// dd($locationData);
// Extract the user's time zone from the location data
// $userTimeZone = $locationData->timezone; // commented by Pradyumn; commented on 5 july 2024; Reason: not in use
// dd($userTimeZone);
// Convert the time to the user's time zone
// $userTimeZone = $ca$carbonTimetz($userTimeZone);
$manage_activity->each(function ($item) {
$timestamp = $item->time;
$normalTime = Carbon::createFromFormat('H:i', $timestamp, 'Asia/Kolkata'); // Replace 'your_timezone' with the actual timezone of the 'time' column
$utcTime = $normalTime->utc();
$item->time = $utcTime->format('H:i'); // Update the 'time' value with the UTC time in 'HH:mm' format
return $item;
});
$result = $manage_activity;
// $userIP = $request->ip(); // Get the user's IP address
// // Make a GET request to ipinfo.io
// $response = Http::get("https://ipinfo.io/{$userIP}/json");
// // Parse the JSON response
// $locationData = $response->json();
// // Extract the user's time zone from the location data
// $userTimeZone = $locationData['timezone'];
// $manage_activity->each(function ($item) use ($userTimeZone) {
// $timestamp = $item->time;
// $normalTime = Carbon::createFromFormat('H:i', $timestamp, $userTimeZone); // Replace 'your_timezone' with the actual timezone of the 'time' column
// $normalTime->setTimezone($userTimeZone); // Convert to the user's time zone
// $item->time = $normalTime->format('H:i'); // Update the 'time' value with the user's time zone in 'HH:mm' format
// return $item;
// });
// $result = $manage_activity;
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $result
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
}
public function get_manage_activity_schedule() {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$user_data = User::where('id',$user_id)->first();
$utm_source = $user_data->utm_source;
$subscription_data = SubscriptionMaster::where('utm_plan',$utm_source)->first();
// dd($subscription_data);
$subscription_id = $subscription_data->id;
$current_day = Carbon::now()->toDateString();
$activity_schedule = ActivityDay::where('date', '>', $current_day)
->where('subscription_id', $subscription_id)
->with('subscription')
->with('scheduleData', 'activityData.teacher_data')
->orderBy('date', 'asc')
->get();
$activity_schedule->each(function ($item) {
$timestamp = $item->time;
$normalTime = Carbon::createFromFormat('H:i', $timestamp, 'Asia/Kolkata'); // Replace 'your_timezone' with the actual timezone of the 'time' column
$utcTime = $normalTime->utc();
$item->time = $utcTime->format('H:i'); // Update the 'time' value with the UTC time in 'HH:mm' format
return $item;
});
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $activity_schedule
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (\Exception $e) {
\Log::error("User data listing Failed : " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something Went wrong.',
]);
}
}
public function get_manage_activity_schedule_utc() {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$user_data = User::where('id',$user_id)->first();
$utm_source = $user_data->utm_source;
$subscription_data = SubscriptionMaster::where('utm_plan',$utm_source)->first();
$subscription_id = $subscription_data->id;
$current_day = Carbon::now()->toDateString();
$activity_schedule = ActivityDay::where('date', '>', $current_day)
->where('subscription_id', $subscription_id)
->with('subscription')
->with('scheduleData', 'activityData.teacher_data')
->get();
$activity_schedule->each(function ($item) {
$timestamp = $item->time;
$normalTime = Carbon::createFromFormat('H:i', $timestamp, 'Asia/Kolkata'); // Replace 'your_timezone' with the actual timezone of the 'time' column
$utcTime = $normalTime->utc();
$item->time = $utcTime->format('H:i'); // Update the 'time' value with the UTC time in 'HH:mm' format
return $item;
});
// $activity_schedule_array = $activity_schedule->toArray();
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $activity_schedule
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (\Exception $e) {
\Log::error("User data listing Failed : " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something Went wrong.',
]);
}
}
public function get_schedule_id() {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$scheduleId = request('scheduleId');
// $data = User::with('user_details')->where('id', $user_id)->first()->toArray();
$activity_schedule = ActivitySchedule::with('subscription')->find($scheduleId);
// print_r($activity_schedule);exit;
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $activity_schedule
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (\Exception $e) {
\Log::error("User data listing Failed : " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something Went wrong.',
]);
}
}
public function get_manage_past_session() {
// try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$user_data = User::where('id',$user_id)->first();
// dd($user_data);
$utm_source = $user_data->utm_source;
// dd($utm_source);
$subscription_data = SubscriptionMaster::where('utm_plan',$utm_source)->first();
$subscription_id = $subscription_data->id;
// dd($subscription_id);
$activity_data = ActivityMaster::where('subscription_id', $subscription_id)->orderBy('end_date', 'desc')->first();
// Calculate the cutoff date (end date + 7 days)
$end_date = $activity_data->end_date;
// dd($end_date);
$cutoffDate = Carbon::parse($end_date)->addDays(7);
// dd($cutoffDate);
// dd($cutoffDate,Carbon::now()->toDateString());
// $current_date = Carbon::now()->format('Y-m-d');
// $desired_date = Carbon::createFromDate(2024, 1, 2);
// $cutoffDate = $desired_date->format('Y-m-d');
// ->where('end_date','>=',$current_date)
// dd($desired_date_formatted);
if(Carbon::now()->toDateString() > $cutoffDate){
return response()->json([
'success' => false,
'message' => 'Activity has ended.',
]);
}
$past_activity = ActivityMaster::where('subscription_id', $subscription_id)->where('end_date','<',$cutoffDate)->with('teacher_data')->orderBy('start_date', 'desc')->first();
$schedule_data = ActivitySchedule::where('activity_master_id', $past_activity['id'])->with('past_data')->orderBy('start_date', 'asc')->get()->toArray();
$data_schedule = [];
foreach ($schedule_data as $k => $val){
if (empty($val['past_data'])){
continue;
}else{
$data_schedule[] = $val;
}
}
$past_activity['schedule'] = $data_schedule;
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $past_activity
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
// } catch (\Exception $e) {
// \Log::error("User data listing Failed: " . $e->getMessage());
// return response()->json([
// 'success' => false,
// 'message' => 'Something went wrong.',
// ]);
// }
}
public function get_manage_past_session_id() {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$pastSessionId = request('pastSessionId');
// Calculate the date 7 days before the current date
$seven_days_before = date('Y-m-d', strtotime('+7 days'));
// Get past sessions that occurred within 7 days before the current date
$passed_session = PastSession::where('created_at', '<=', $seven_days_before)
->find($pastSessionId);
// dd($passed_session);
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $passed_session
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
} catch (\Exception $e) {
\Log::error("User data listing Failed: " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something went wrong.',
]);
}
}
public function add_session_status(Request $request) {
try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
$oldUserRankings = LeaderboardMaster::with('user')
->get();
//get old points
$validator = Validator::make($request->all(), [
'content_id' => 'required',
'content_type' => 'required'
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors()
], 401);
}
// validation fails
$data = UserContentView::where('user_id', $user_id)
->whereDate('created_at', now()->toDateString())
->get();
if ($data->isNotEmpty()) {
return response([
'status' => "Data already exists"
], 400);
} else {
$add_content_view = new UserContentView;
$add_content_view->user_id = $user_id;
$add_content_view->content_id = $request->input('content_id');
$add_content_view->is_view = $request->input('content_type');
$add_content_view->save();
// Check if user exists in another table
$leaderboardMaster = LeaderboardMaster::where('user_id', $user_id)->first();
if ($leaderboardMaster) {
// Update points if user exists
$leaderboardMaster->total_score += 1;
$leaderboardMaster->save();
} else {
// Create a new entry with 1 point if user doesn't exist
LeaderboardMaster::create([
'user_id' => $user_id,
'total_score' => 1,
]);
}
}
$newUserRanking = LeaderboardMaster::where('user_id', $user_id)->first();
if ($oldUserRankings && $newUserRanking) {
foreach ($oldUserRankings as $ranking) {
$previousTotalScore = $ranking->total_score;
$currentTotalScore = $newUserRanking->total_score;
if ($ranking->user_id == $user_id) {
if ($previousTotalScore === null) {
$ranking->progress_bar = '0'; // Initial entry
} elseif ($currentTotalScore > $previousTotalScore) {
$ranking->progress_bar = '0'; // Total score increased
$message = "You earned 1 point!";
} elseif ($currentTotalScore < $previousTotalScore) {
$ranking->progress_bar = '1'; // Total score decreased
$message = "Your score decreased.";
} else {
$ranking->progress_bar = '2'; // Total score remained the same
$message = "Your score remains the same.";
}
$ranking->save(); // Save the updated progress_bar value
}
}
return response([
'status' => "Data added successfully.",
'progress_bar' => $message,
], 200);
}
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.'
]);
}
} catch (\Exception $e) {
\Log::error("User data listing Failed: " . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Something went wrong.'
]);
}
}
public function dummy_activity(Request $request){
// try {
$token = readHeaderToken();
if ($token) {
$user_id = $token['sub'];
// dd($user_id);
$user_data = User::where('id',$user_id)->first();
$utm_source = $user_data->utm_source;
$subscription_data = SubscriptionMaster::where('utm_plan',$utm_source)->first();
$subscription_id = $subscription_data->id;
// dd($subscription_id);
$current_day = Carbon::now()->toDateString();
$manage_activity = ActivityDay::where('date', '=', $current_day)
// ->where('')
->where('subscription_id', $subscription_id)
->with('subscription')
->with('faq_activity_link.faqsData')
->with('scheduleData', 'activityData.subscription')
->with('activityData.teacher_data')
->get();
// $userIP = $request->ip(); // Get the user's IP address
// // Make a GET request to ipinfo.io
// $response = Http::get("https://ipinfo.io/{$userIP}/json");
// // Parse the JSON response
// $ch = curl_init("https://ipinfo.io/{$userIP}/json");
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($ch);
// curl_close($ch);
// $locationData = json_decode($response);
// // Extract the user's time zone from the location data
// $userTimeZone = $locationData->timezone;
// // dd($userTimeZone);
// // Convert the time to the user's time zone
// // $userTimeZone = $ca$carbonTimetz($userTimeZone);
// $manage_activity->each(function ($item) {
// $timestamp = $item->time;
// $normalTime = Carbon::createFromFormat('H:i', $timestamp, 'Asia/Kolkata'); // Replace 'your_timezone' with the actual timezone of the 'time' column
// $utcTime = $normalTime->utc();
// $item->time = $utcTime->format('H:i'); // Update the 'time' value with the UTC time in 'HH:mm' format
// return $item;
// });
// $result = $manage_activity;
$userIP = $request->ip(); // Get the user's IP address
// Make a GET request to ipinfo.io
$response = Http::get("https://ipinfo.io/{$userIP}/json");
// Parse the JSON response
$locationData = $response->json();
// Extract the user's time zone from the location data
$userTimeZone = $locationData['timezone'];
$manage_activity->each(function ($item) use ($userTimeZone) {
$timestamp = $item->time;
$normalTime = Carbon::createFromFormat('H:i', $timestamp, $userTimeZone); // Replace 'your_timezone' with the actual timezone of the 'time' column
$normalTime->setTimezone($userTimeZone); // Convert to the user's time zone
$item->time = $normalTime->format('H:i'); // Update the 'time' value with the user's time zone in 'HH:mm' format
return $item;
});
$result = $manage_activity;
return response()->json([
'success' => true,
'message' => 'Data fetched successfully.',
'result' => $result
]);
} else {
return response()->json([
'success' => false,
'message' => 'Authentication failed.',
]);
}
}
}