Skip to content

Commit

Permalink
added stats for student ages (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
rono516 authored Nov 12, 2023
1 parent b4f3b83 commit 6067d35
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 74 deletions.
59 changes: 53 additions & 6 deletions app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Models\Year;
use App\Models\Config;
use App\Models\Partner;
use App\Models\Period;
use App\Models\Year;
use App\Models\Partner;
use App\Models\Student;
use App\Services\DateRange;
use Illuminate\Http\Request;
use App\Services\StatService;
use App\Traits\PeriodSelection;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ReportController extends Controller
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public function external(Request $request)
$year_data = [];
$years = [];

if (! (property_exists($request, 'period') && $request->period !== null)) {
if (!(property_exists($request, 'period') && $request->period !== null)) {
$startperiod = Period::find(Config::where('name', 'first_external_period')->first()->value ?? Period::first()->id);
} else {
$startperiod = Period::find($request->period);
Expand Down Expand Up @@ -270,6 +271,52 @@ public function genderReport(Request $request)
]);
}

public function ageReport()
{
$ages = app('config')->get('age');

$all_students = Student::all(); //Fetch all students
$ages_of_all_students = []; //Initiate an array to add all student ages

foreach ($all_students as $single_student) {
$age_of_each_student = Carbon::parse($single_student->birthdate)->age;
array_push($ages_of_all_students, $age_of_each_student); //add every age to the ages_of_all_students array

}

$labels = []; // This will contain the labels to be interpolated in the bar graph report
$data = []; // This will contain the data to be interpolated in the bar graph report

$counts = array_fill_keys(array_keys($ages), 0);
// Initialize an array to store counts for each range

foreach ($ages_of_all_students as $value) {
// Iterate through the values and count them in the specified ranges
foreach ($ages as $range => $limits) {
if ($value >= $limits[0] && $value <= $limits[1]) {
$counts[$range]++;
break; // No need to check other ranges once a match is found
}
}
}

foreach ($counts as $range => $count) {
array_push($labels, $range);
array_push($data, $count);
// Add range and count from env to the labels and data arrays
}

return view('reports.age')->with(
[
'ages' => $ages,
'ages_of_all_students' => $ages_of_all_students,
'labels' => $labels,
'data' => $data,
]

);
}

/**
* Show the enrollment numbers per rhythm.
*/
Expand Down Expand Up @@ -344,7 +391,7 @@ public function levels(Request $request)

private function getStartperiod(Request $request)
{
if (! (property_exists($request, 'period') && $request->period !== null)) {
if (!(property_exists($request, 'period') && $request->period !== null)) {
$startperiod = Period::find(Config::where('name', 'first_period')->first()->value);
} else {
$startperiod = Period::find($request->period);
Expand Down
10 changes: 10 additions & 0 deletions config/age.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'0-6' => [0, 6],
'7-12' => [7, 12],
'8-14' => [8, 14],
'15-20' => [15, 20],
'21+' => [21, PHP_INT_MAX],

];
141 changes: 141 additions & 0 deletions resources/views/reports/age.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
@extends('backpack::blank')

@section('header')
<section class="container-fluid">
<h2>
{{ __('Students by age') }}
</h2>
</section>
@endsection

@section('content')
<div class="row">



<div class="col-md-12">
<div class="card">
<div class="card-body">
<canvas id="myChart"></canvas>
</div>
</div>
</div>

<div class="col-md-12">
<div class="card">


<div class="card-body">
<table id ="reportsTable" class="table table-striped">
<thead>


@foreach ($ages as $key => $value)
<th>{{ $key }} years</th>
@endforeach


</thead>

<tbody>

<tr>


@foreach ($ages as $key => $value)
@php
$count_of_ages_in_range = 0;
for ($i = 0; $i < sizeof($ages_of_all_students); $i++) {
if ($ages_of_all_students[$i] >= $value[0] && $ages_of_all_students[$i] <= $value[1]) {
$count_of_ages_in_range++;
}
}
@endphp
<td>{{ $count_of_ages_in_range }}</td>
@endforeach

</tr>


</tbody>
</table>
</div>
</div>
</div>

</div>
@endsection


@section('before_scripts')
@endsection


@section('after_scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
var _labels = @json($labels);
var _data = @json($data);
const ctx = document.getElementById('myChart');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: _labels,
datasets: [{
label: 'Students # in ages',
data: _data,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
}
});
}, true);
</script>



<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>



<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.20/b-1.6.1/b-html5-1.6.1/b-print-1.6.1/datatables.min.css" />

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript"
src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.20/b-1.6.1/b-html5-1.6.1/b-print-1.6.1/datatables.min.js">
</script>
<script>
$(document).ready(() =>
$('#reportsTable').DataTable({
dom: 'Bfrtip',
"paging": false,
"searching": false,
"ordering": false,
buttons: [
'excel',
'pdf',
'print'
]
}));
</script>
@endsection
Loading

0 comments on commit 6067d35

Please sign in to comment.