Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
SchoolStatisticsController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Api\SPWalletController; use App\Http\Controllers\Controller; use App\Libraries\Helpers; use App\Models\Admin; use Illuminate\Http\Request; use Auth; use Session; use Hash; use App\Models\User; use App\Models\Product; use App\Models\ProductCategory; use App\Models\Country; use App\Models\Goal; use App\Models\Group; use App\Models\Task; use App\Models\College; use App\Models\Course; use App\V2\Services\AttendanceService; use App\V2\Services\SPWalletService; use Carbon\Carbon; use Carbon\CarbonPeriod; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class SchoolStatisticsController extends Controller { private $attendanceService; public function __construct() { $this->attendanceService = new AttendanceService(); } public function getGroupMonthlyData(Request $request) { $monthlyData = [ 'labels' => ['January', 'February', 'March', 'April'], // Example months 'data' => [ // January [ ['group' => 'Group A', 'presentPercentage' => 70, 'absentPercentage' => 20, 'holidayPercentage' => 10], ['group' => 'Group B', 'presentPercentage' => 60, 'absentPercentage' => 30, 'holidayPercentage' => 10], ], // February [ ['group' => 'Group A', 'presentPercentage' => 65, 'absentPercentage' => 25, 'holidayPercentage' => 10], ['group' => 'Group B', 'presentPercentage' => 70, 'absentPercentage' => 20, 'holidayPercentage' => 10], ], // March [ ['group' => 'Group A', 'presentPercentage' => 80, 'absentPercentage' => 15, 'holidayPercentage' => 5], ['group' => 'Group B', 'presentPercentage' => 75, 'absentPercentage' => 20, 'holidayPercentage' => 5], ], // April [ ['group' => 'Group A', 'presentPercentage' => 75, 'absentPercentage' => 20, 'holidayPercentage' => 5], ['group' => 'Group B', 'presentPercentage' => 70, 'absentPercentage' => 25, 'holidayPercentage' => 5], ], ], ]; return response()->json($monthlyData); } public function getMonthlyData(Request $request) { $school = Helpers::getCurrentSchool($request->user()); $user_ids = User::select(['user_id']) ->where('preferred_school_id', $school->school_id) ->pluck('user_id') ->toArray(); $last12Months = collect(); for ($i = 11; $i >= 0; $i--) { $month = Carbon::today()->subMonths($i); $last12Months->push(['month' => $month->format('Y-m'), 'start' => $month->startOfMonth()->format('Y-m-d'), 'end' => $month->endOfMonth()->format('Y-m-d')]); } // Initialize arrays to hold the results $groupLabels = []; $attendanceData = []; foreach ($last12Months as $group) { // For monthly grouping, get the start and end dates for each month $startDate = $group['start']; $endDate = $group['end']; $startDateObj = Carbon::createFromFormat('Y-m-d', $startDate); $result = $this->attendanceService->getCalendarAttendancesBulk($user_ids, null, $startDate, $endDate); Log::debug("Summary: ". print_r($result['summary'], true)); $present_count = $result['summary']['present_num_for_period']; $absent_count = $result['summary']['absent_num_for_period']; $holiday_count = $result['summary']['holiday_num_for_period']; $na_count = $result['summary']['na_num_for_period']; $total_count = $result['summary']['total_num_for_period']; // Calculate the total number of users and percentages $presentPercentage = ($present_count / $total_count) * 100; $absentPercentage = ($absent_count / $total_count) * 100; $holidayPercentage = ( $holiday_count / $total_count) * 100; //NA are the ones for the dates which are still in future, or if the students have not yet registered. $naPercentage = ( $na_count / $total_count) * 100; // Add the results to the arrays // $groupLabels[] = $group['month']; // Group label (e.g., "2022-03") $groupLabels[] = $startDateObj->format('M Y'); $attendanceData[] = [ 'presentPercentage' => Helpers::formatDecimanPoints($presentPercentage, 2), 'absentPercentage' => Helpers::formatDecimanPoints($absentPercentage, 2), 'holidayPercentage' => Helpers::formatDecimanPoints($holidayPercentage, 2), 'NAPercentage'=> Helpers::formatDecimanPoints($naPercentage, 2), ]; } $response_data = []; $response_data['labels'] = $groupLabels; $response_data['data'] = $attendanceData; return response()->json($response_data); } public function getBiWeeklyData(Request $request) { $startDate = $request->get('start_date', Carbon::today()->subDays(14)->format('Y-m-d')); $endDate = $request->get('end_date', Carbon::today()->format('Y-m-d')); $school = Helpers::getCurrentSchool($request->user()); $user_ids = User::select(['user_id']) ->where('preferred_school_id', $school->school_id) ->pluck('user_id') ->toArray(); $dates = CarbonPeriod::create($startDate, $endDate); // Initialize arrays to hold the results $groupLabels = []; $attendanceData = []; foreach ($dates as $date) { // For monthly grouping, get the start and end dates for each month // $startDate = $group['start']; // $endDate = $group['end']; $startDate = $date->format('Y-m-d'); $endDate = $date->format('Y-m-d'); $startDateObj = Carbon::createFromFormat('Y-m-d', $startDate); $result = $this->attendanceService->getCalendarAttendancesBulk($user_ids, null, $startDate, $endDate); // Log::debug("Summary: ". print_r($result['summary'], true)); $present_count = $result['summary']['present_num_for_period']; $absent_count = $result['summary']['absent_num_for_period']; $holiday_count = $result['summary']['holiday_num_for_period']; $na_count = $result['summary']['na_num_for_period']; $total_count = $result['summary']['total_num_for_period']; // Calculate the total number of users and percentages $presentPercentage = ($present_count / $total_count) * 100; $absentPercentage = ($absent_count / $total_count) * 100; $holidayPercentage = ( $holiday_count / $total_count) * 100; //NA are the ones for the dates which are still in future, or if the students have not yet registered. $naPercentage = ( $na_count / $total_count) * 100; // Add the results to the arrays // $groupLabels[] = $group['month']; // Group label (e.g., "2022-03") $groupLabels[] = $startDateObj->format('Y-m-d'); $attendanceData[] = [ 'presentPercentage' => Helpers::formatDecimanPoints($presentPercentage, 2), 'absentPercentage' => Helpers::formatDecimanPoints($absentPercentage, 2), 'holidayPercentage' => Helpers::formatDecimanPoints($holidayPercentage, 2), 'NAPercentage'=> Helpers::formatDecimanPoints($naPercentage, 2), ]; } $response_data = []; $response_data['labels'] = $groupLabels; $response_data['data'] = $attendanceData; return response()->json($response_data); } public function getWeeklyData(Request $request) { $startDate = Carbon::createFromFormat('Y-m-d', $request->get('start_date', Carbon::today()->subDays(7*10)->format('Y-m-d'))); $endDate = Carbon::createFromFormat('Y-m-d', $request->get('end_date', Carbon::today()->format('Y-m-d'))); $school = Helpers::getCurrentSchool($request->user()); $user_ids = User::select(['user_id']) ->where('preferred_school_id', $school->school_id) ->pluck('user_id') ->toArray(); $groups = collect(); $currentStartDate = $startDate->copy(); // Loop through the weeks between the start and end dates while ($currentStartDate->lte($endDate)) { $currentEndDate = $currentStartDate->copy()->addDays(6); // Define the end of the week if ($currentEndDate->gt($endDate)) { // $currentEndDate = $endDate; // Ensure the last group doesn't exceed the end date break; } $groups->push([ 'period' => $currentStartDate->format('d M') . '-' . $currentEndDate->format('d M Y'), 'start' => $currentStartDate->format('Y-m-d'), 'end' => $currentEndDate->format('Y-m-d') ]); $currentStartDate->addWeek(); // Move to the next week } // Initialize arrays to hold the results $groupLabels = []; $attendanceData = []; foreach ($groups as $group) { // For monthly grouping, get the start and end dates for each month $startDate = $group['start']; $endDate = $group['end']; // $startDate = $date->format('Y-m-d'); // $endDate = $date->format('Y-m-d'); $startDateObj = Carbon::createFromFormat('Y-m-d', $startDate); $result = $this->attendanceService->getCalendarAttendancesBulk($user_ids, null, $startDate, $endDate); // Log::debug("Summary: ". print_r($result['summary'], true)); $present_count = $result['summary']['present_num_for_period']; $absent_count = $result['summary']['absent_num_for_period']; $holiday_count = $result['summary']['holiday_num_for_period']; $na_count = $result['summary']['na_num_for_period']; $total_count = $result['summary']['total_num_for_period']; // Calculate the total number of users and percentages $presentPercentage = ($present_count / $total_count) * 100; $absentPercentage = ($absent_count / $total_count) * 100; $holidayPercentage = ( $holiday_count / $total_count) * 100; //NA are the ones for the dates which are still in future, or if the students have not yet registered. $naPercentage = ( $na_count / $total_count) * 100; // Add the results to the arrays // $groupLabels[] = $group['month']; // Group label (e.g., "2022-03") $groupLabels[] = $group['period']; $attendanceData[] = [ 'presentPercentage' => Helpers::formatDecimanPoints($presentPercentage, 2), 'absentPercentage' => Helpers::formatDecimanPoints($absentPercentage, 2), 'holidayPercentage' => Helpers::formatDecimanPoints($holidayPercentage, 2), 'NAPercentage'=> Helpers::formatDecimanPoints($naPercentage, 2), ]; } $response_data = []; $response_data['labels'] = $groupLabels; $response_data['data'] = $attendanceData; return response()->json($response_data); } }