301 lines
11 KiB
PHP
301 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ActivitiesAttended;
|
|
use App\Models\ActivitiesOrganised;
|
|
use App\Models\BooksPublished;
|
|
use App\Models\Department;
|
|
use App\Models\ExternalEngagement;
|
|
use App\Models\IvOrganised;
|
|
use App\Models\OnlineCourse;
|
|
use App\Models\Patent;
|
|
use App\Models\Publication;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
// Admin dashboard (optional)
|
|
public function index()
|
|
{
|
|
$departments = Department::all();
|
|
|
|
// Collect distinct years from all relevant date fields
|
|
$years = collect([
|
|
ActivitiesAttended::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
ActivitiesOrganised::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
BooksPublished::selectRaw('YEAR(date_of_publication) as year')->distinct()->pluck('year'),
|
|
ExternalEngagement::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
IvOrganised::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
OnlineCourse::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
Patent::selectRaw('YEAR(date_of_submission) as year')->distinct()->pluck('year'),
|
|
Publication::selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),
|
|
])->flatten()->unique()->sortDesc();
|
|
|
|
return view('admin.dashboard', compact('departments', 'years'));
|
|
}
|
|
|
|
// View responses submitted by users
|
|
public function viewActivitiesAttendedResponses()
|
|
{
|
|
$departments = Department::all();
|
|
return view('pages.activities-attended.index', compact('departments'));
|
|
}
|
|
public function viewActivitiesOrganisedResponses()
|
|
{
|
|
return view('pages.activities-organised.index');
|
|
}
|
|
public function viewIvOrganisedResponses()
|
|
{
|
|
return view('pages.iv-organised.index');
|
|
}
|
|
public function viewPublicationsResponses()
|
|
{
|
|
return view('pages.publications.index');
|
|
}
|
|
public function viewBooksPublishedResponses()
|
|
{
|
|
return view('pages.booksPublished.index');
|
|
}
|
|
public function viewExternalEngagementResponses()
|
|
{
|
|
return view('pages.externalEngagement.index');
|
|
}
|
|
public function viewOnlineCoursesResponses()
|
|
{
|
|
return view('pages.onlineCourses.index');
|
|
}
|
|
public function viewPatentsResponses()
|
|
{
|
|
return view('pages.patents.index');
|
|
}
|
|
public function analyticsActivitiesAttended()
|
|
{
|
|
$data = ActivitiesAttended::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'activities_attendeds.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsActivitiesOrganised()
|
|
{
|
|
$data = ActivitiesOrganised::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'activities_organiseds.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsIvOrganised()
|
|
{
|
|
$data = IvOrganised::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'iv_organiseds.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsPaperPublished()
|
|
{
|
|
$data = Publication::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'publications.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsBooksPublished()
|
|
{
|
|
$data = BooksPublished::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'books_published.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsExternalEngagement()
|
|
{
|
|
$data = ExternalEngagement::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'external_engagements.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsOnlineCourse()
|
|
{
|
|
$data = OnlineCourse::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'online_courses.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
public function analyticsPatent()
|
|
{
|
|
$data = Patent::selectRaw('departments.name as department, COUNT(*) as count')
|
|
->join('departments', 'patents.department_id', '=', 'departments.id')
|
|
->groupBy('departments.name')
|
|
->get();
|
|
|
|
$total = $data->sum('count');
|
|
|
|
return response()->json([
|
|
'labels' => $data->pluck('department'),
|
|
'values' => $data->pluck('count'),
|
|
'total' => $total,
|
|
]);
|
|
}
|
|
|
|
public function analyticsComparison(Request $request)
|
|
{
|
|
$departmentId = $request->query('department_id');
|
|
$year = $request->query('year');
|
|
|
|
$models = [
|
|
'ActivitiesAttended' => ['class' => ActivitiesAttended::class, 'date_field' => 'end_date'],
|
|
'ActivitiesOrganised' => ['class' => ActivitiesOrganised::class, 'date_field' => 'end_date'],
|
|
'BooksPublished' => ['class' => BooksPublished::class, 'date_field' => 'date_of_publication'],
|
|
'ExternalEngagement' => ['class' => ExternalEngagement::class, 'date_field' => 'end_date'],
|
|
'IvOrganised' => ['class' => IvOrganised::class, 'date_field' => 'end_date'],
|
|
'OnlineCourse' => ['class' => OnlineCourse::class, 'date_field' => 'end_date'],
|
|
'Patent' => ['class' => Patent::class, 'date_field' => 'date_of_submission'],
|
|
'Publication' => ['class' => Publication::class, 'date_field' => 'end_date'],
|
|
];
|
|
|
|
$data = [];
|
|
|
|
foreach ($models as $label => $modelConfig) {
|
|
$query = $modelConfig['class']::where('department_id', $departmentId);
|
|
if ($year && $year !== 'all') {
|
|
$query->whereYear($modelConfig['date_field'], $year);
|
|
}
|
|
$count = $query->count();
|
|
if ($count > 0) { // Only include data with non-zero count
|
|
$data[] = [
|
|
'label' => $label,
|
|
'count' => $count,
|
|
];
|
|
}
|
|
}
|
|
|
|
$total = array_sum(array_column($data, 'count'));
|
|
|
|
return response()->json([
|
|
'labels' => array_column($data, 'label'),
|
|
'values' => array_column($data, 'count'),
|
|
'total' => $total
|
|
]);
|
|
}
|
|
|
|
public function analyticsContribution(Request $request)
|
|
{
|
|
$model = $request->query('model', 'all');
|
|
$year = $request->query('year');
|
|
|
|
$models = [
|
|
'ActivitiesAttended' => ['class' => ActivitiesAttended::class, 'date_field' => 'end_date'],
|
|
'ActivitiesOrganised' => ['class' => ActivitiesOrganised::class, 'date_field' => 'end_date'],
|
|
'BooksPublished' => ['class' => BooksPublished::class, 'date_field' => 'date_of_publication'],
|
|
'ExternalEngagement' => ['class' => ExternalEngagement::class, 'date_field' => 'end_date'],
|
|
'IvOrganised' => ['class' => IvOrganised::class, 'date_field' => 'end_date'],
|
|
'OnlineCourse' => ['class' => OnlineCourse::class, 'date_field' => 'end_date'],
|
|
'Patent' => ['class' => Patent::class, 'date_field' => 'date_of_submission'],
|
|
'Publication' => ['class' => Publication::class, 'date_field' => 'end_date'],
|
|
];
|
|
|
|
$data = [];
|
|
|
|
if ($model === 'all') {
|
|
$departments = Department::all();
|
|
|
|
foreach ($departments as $department) {
|
|
$count = 0;
|
|
foreach ($models as $modelConfig) {
|
|
$query = $modelConfig['class']::where('department_id', $department->id);
|
|
if ($year && $year !== 'all') {
|
|
$query->whereYear($modelConfig['date_field'], $year);
|
|
}
|
|
$count += $query->count();
|
|
}
|
|
if ($count > 0) { // Only include data with non-zero count
|
|
$data[] = [
|
|
'label' => $department->name,
|
|
'count' => $count,
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
$modelConfig = $models[$model];
|
|
$query = $modelConfig['class']::selectRaw('departments.name as label, COUNT(*) as count')
|
|
->join('departments', 'department_id', '=', 'departments.id')
|
|
->groupBy('departments.name');
|
|
|
|
if ($year && $year !== 'all') {
|
|
$query->whereYear($modelConfig['date_field'], $year);
|
|
}
|
|
|
|
$data = $query->get()->filter(function ($item) {
|
|
return $item['count'] > 0; // Filter out data with zero count
|
|
})->toArray();
|
|
}
|
|
|
|
$total = array_sum(array_column($data, 'count'));
|
|
|
|
return response()->json([
|
|
'labels' => array_column($data, 'label'),
|
|
'values' => array_column($data, 'count'),
|
|
'total' => $total
|
|
]);
|
|
}
|
|
}
|