Feat: Activites Organised
This commit is contained in:
190
app/Http/Controllers/ActivitiesOrganisedController.php
Normal file
190
app/Http/Controllers/ActivitiesOrganisedController.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use App\Models\ActivitiesOrganised;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ActivitiesOrganisedController extends Controller
|
||||
{
|
||||
public function edit($id)
|
||||
{
|
||||
$response = ActivitiesOrganised::findOrFail($id);
|
||||
|
||||
return view('activities-organised.edit', compact('response'));
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$response = ActivitiesOrganised::findOrFail($id);
|
||||
|
||||
// Validate the request data
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string',
|
||||
'resource_person_name' => 'required|string',
|
||||
'resource_person_organization' => 'required|string',
|
||||
'target_audience' => 'required|string',
|
||||
'number_of_participants' => 'required|integer',
|
||||
'objective' => 'required|string',
|
||||
'outcomes' => 'required|string',
|
||||
'venue' => 'required|string',
|
||||
'start_date' => 'required|date',
|
||||
'start_time' => 'required|date_format:H:i',
|
||||
'end_date' => 'required|date',
|
||||
'end_time' => 'required|date_format:H:i',
|
||||
'num_days' => 'required|integer',
|
||||
'activity_type' => 'required|string',
|
||||
'category' => 'required|string',
|
||||
'level' => 'required|string',
|
||||
'proof' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip',
|
||||
]);
|
||||
|
||||
// Combine start date and time
|
||||
$startDateTime = date('Y-m-d H:i:s', strtotime("{$validated['start_date']} {$validated['start_time']}"));
|
||||
$endDateTime = date('Y-m-d H:i:s', strtotime("{$validated['end_date']} {$validated['end_time']}"));
|
||||
|
||||
// Handle the file upload if a new file is provided
|
||||
if ($request->hasFile('proof')) {
|
||||
// Delete old file if exists
|
||||
if ($response->proof && Storage::disk('public')->exists($response->proof)) {
|
||||
Storage::disk('public')->delete($response->proof);
|
||||
}
|
||||
|
||||
// Extract year from start_date
|
||||
$year = date('Y', strtotime($validated['start_date']));
|
||||
$username = $response->user->name;
|
||||
|
||||
$originalName = $request->file('proof')->getClientOriginalName();
|
||||
$fileName = $username . '_' . $originalName;
|
||||
|
||||
// Create path structure: year/faculty_name
|
||||
$folderPath = 'proofs/' . $year . '/' . $username;
|
||||
|
||||
// Store file in the specified path
|
||||
$proofPath = $request->file('proof')->storeAs($folderPath, $fileName, 'public');
|
||||
|
||||
$response->proof = $proofPath;
|
||||
}
|
||||
|
||||
// Update other fields
|
||||
$response->title = $validated['title'];
|
||||
$response->resource_person_name = $validated['resource_person_name'];
|
||||
$response->resource_person_organization = $validated['resource_person_organization'];
|
||||
$response->target_audience = $validated['target_audience'];
|
||||
$response->number_of_participants = $validated['number_of_participants'];
|
||||
$response->objective = $validated['objective'];
|
||||
$response->outcomes = $validated['outcomes'];
|
||||
$response->venue = $validated['venue'];
|
||||
$response->start_date = $startDateTime;
|
||||
$response->end_date = $endDateTime;
|
||||
$response->num_days = $validated['num_days'];
|
||||
$response->activity_type = $validated['activity_type'];
|
||||
$response->category = $validated['category'];
|
||||
$response->level = $validated['level'];
|
||||
|
||||
$response->save();
|
||||
|
||||
$userRole = auth()->user()->role->name;
|
||||
|
||||
if ($userRole === 'Admin') {
|
||||
return redirect()->route('admin.ActivitiesOrganisedResponses')
|
||||
->with('status', 'Response updated successfully');
|
||||
} elseif ($userRole === 'Coordinator') {
|
||||
return redirect()->route('coordinator.ActivitiesOrganisedResponses')
|
||||
->with('status', 'Response updated successfully');
|
||||
} else {
|
||||
// For regular users
|
||||
return redirect()->route('faculty.ActivitiesOrganisedResponses')
|
||||
->with('status', 'Response updated successfully');
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$response = ActivitiesOrganised::findOrFail($id);
|
||||
|
||||
// Delete the file if it exists
|
||||
if ($response->proof && Storage::disk('public')->exists($response->proof)) {
|
||||
Storage::disk('public')->delete($response->proof);
|
||||
}
|
||||
|
||||
$response->delete();
|
||||
|
||||
return response()->json(['success' => 'Record deleted successfully']);
|
||||
}
|
||||
|
||||
public function getActivitiesOrganisedResponses()
|
||||
{
|
||||
$user = auth()->user();
|
||||
$isAdmin = $user->role->name === 'Admin';
|
||||
$isCoordinator = $user->role->name === 'Coordinator';
|
||||
|
||||
// Query based on role
|
||||
if ($isAdmin) {
|
||||
// Admin sees all records
|
||||
$responses = ActivitiesOrganised::with('user', 'department');
|
||||
} elseif ($isCoordinator) {
|
||||
// Coordinator sees only their department's records
|
||||
$responses = ActivitiesOrganised::with('user', 'department')
|
||||
->whereHas('user', function ($query) use ($user) {
|
||||
$query->where('department_id', $user->department_id);
|
||||
});
|
||||
} else {
|
||||
// Regular users see only their own records
|
||||
$responses = ActivitiesOrganised::with('user', 'department')
|
||||
->where('faculty_id', $user->id);
|
||||
}
|
||||
|
||||
return DataTables::of($responses)
|
||||
->addColumn('user_name', function ($response) {
|
||||
return $response->user->name ?? 'Unknown';
|
||||
})
|
||||
->addColumn('department_name', function ($response) {
|
||||
return $response->department->name ?? 'Unknown';
|
||||
})
|
||||
->addColumn('start_date', function ($response) {
|
||||
return \Carbon\Carbon::parse($response->start_date)->format('d-m-Y');
|
||||
})
|
||||
->addColumn('start_time', function ($response) {
|
||||
return \Carbon\Carbon::parse($response->start_date)->format('h:i A');
|
||||
})
|
||||
->addColumn('end_date', function ($response) {
|
||||
return \Carbon\Carbon::parse($response->end_date)->format('d-m-Y');
|
||||
})
|
||||
->addColumn('end_time', function ($response) {
|
||||
return \Carbon\Carbon::parse($response->end_date)->format('h:i A');
|
||||
})
|
||||
->addColumn('action', function ($response) {
|
||||
$actions = [];
|
||||
|
||||
// View proof button for everyone
|
||||
if ($response->proof) {
|
||||
$actions[] = '<a href="' . asset('storage/' . $response->proof) . '" target="_blank" class="btn btn-sm btn-primary mr-1">View</a>';
|
||||
} else {
|
||||
$actions[] = 'No Proof';
|
||||
}
|
||||
|
||||
// Edit button with role-appropriate route
|
||||
$userRole = auth()->user()->role->name;
|
||||
// Determine the appropriate route based on user role
|
||||
if ($userRole === 'Admin') {
|
||||
$editRoute = route('admin.ActivitiesOrganised.edit', $response->id);
|
||||
} elseif ($userRole === 'Coordinator') {
|
||||
$editRoute = route('coordinator.ActivitiesOrganised.edit', $response->id);
|
||||
} else {
|
||||
$editRoute = route('faculty.ActivitiesOrganised.edit', $response->id);
|
||||
}
|
||||
|
||||
$actions[] = '<a href="' . $editRoute . '" class="btn btn-sm btn-info mx-1">Edit</a>';
|
||||
|
||||
$deleteRoute = route('activitiesOrganised.destroy', $response->id);
|
||||
$actions[] = '<button type="button" class="btn btn-sm btn-danger delete-btn" data-id="' . $response->id . '" data-url="' . $deleteRoute . '">Delete</button>';
|
||||
|
||||
return implode(' ', $actions);
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
->make(true);
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,9 @@ class AdminController extends Controller
|
||||
{
|
||||
return view('activities-attended.index');
|
||||
}
|
||||
public function viewActivitiesOrganisedResponses()
|
||||
{
|
||||
return view('activities-organised.index');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,5 +20,10 @@ class CoordinatorController extends Controller
|
||||
{
|
||||
return view('activities-attended.index');
|
||||
}
|
||||
|
||||
public function viewActivitiesOrganisedResponses()
|
||||
{
|
||||
return view('activities-organised.index');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\ActivitiesAttended;
|
||||
use App\Models\ActivitiesOrganised;
|
||||
|
||||
class FacultyController extends Controller
|
||||
{
|
||||
@@ -24,6 +25,16 @@ class FacultyController extends Controller
|
||||
{
|
||||
return view('activities-attended.index');
|
||||
}
|
||||
public function ActivitiesOrganisedForm()
|
||||
{
|
||||
// Logic to show the response form
|
||||
return view('faculty.activities-organised-form');
|
||||
}
|
||||
|
||||
public function viewActivitiesOrganisedResponses()
|
||||
{
|
||||
return view('activities-organised.index');
|
||||
}
|
||||
|
||||
public function ActivitiesAttendedFormResponse(Request $request)
|
||||
{
|
||||
@@ -94,4 +105,77 @@ class FacultyController extends Controller
|
||||
return back()->withErrors('An error occurred while submitting your response: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function ActivitiesOrganisedFormResponse(Request $request)
|
||||
{
|
||||
try {
|
||||
// Validate the request data
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string',
|
||||
'resource_person_name' => 'required|string',
|
||||
'resource_person_organization' => 'required|string',
|
||||
'target_audience' => 'required|string',
|
||||
'number_of_participants' => 'required|integer',
|
||||
'objective' => 'required|string',
|
||||
'outcomes' => 'required|string',
|
||||
'venue' => 'required|string',
|
||||
'start_date' => 'required|date',
|
||||
'start_time' => 'required|date_format:H:i',
|
||||
'end_date' => 'required|date',
|
||||
'end_time' => 'required|date_format:H:i',
|
||||
'num_days' => 'required|integer',
|
||||
'activity_type' => 'required|string',
|
||||
'category' => 'required|string',
|
||||
'level' => 'required|string',
|
||||
'proof' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip',
|
||||
]);
|
||||
|
||||
// Combine start date and time
|
||||
$startDateTime = date('Y-m-d H:i:s', strtotime("{$validated['start_date']} {$validated['start_time']}"));
|
||||
$endDateTime = date('Y-m-d H:i:s', strtotime("{$validated['end_date']} {$validated['end_time']}"));
|
||||
|
||||
// Handle the file upload
|
||||
$proofPath = null;
|
||||
if ($request->hasFile('proof')) {
|
||||
$originalName = $request->file('proof')->getClientOriginalName();
|
||||
$username = auth()->user()->name;
|
||||
$fileName = $username . '_' . $originalName;
|
||||
|
||||
// Extract year from start_date
|
||||
$year = date('Y', strtotime($validated['start_date']));
|
||||
|
||||
// Create path structure: year/faculty_name
|
||||
$folderPath = 'proofs/' . $year . '/' . $username;
|
||||
|
||||
// Store file in the specified path
|
||||
$proofPath = $request->file('proof')->storeAs($folderPath, $fileName, 'public');
|
||||
}
|
||||
|
||||
// Save the response to the database
|
||||
ActivitiesOrganised::create([
|
||||
'title' => $validated['title'],
|
||||
'resource_person_name' => $validated['resource_person_name'],
|
||||
'resource_person_organization' => $validated['resource_person_organization'],
|
||||
'target_audience' => $validated['target_audience'],
|
||||
'number_of_participants' => $validated['number_of_participants'],
|
||||
'objective' => $validated['objective'],
|
||||
'outcomes' => $validated['outcomes'],
|
||||
'department_id' => auth()->user()->department->id,
|
||||
'faculty_id' => auth()->user()->id,
|
||||
'venue' => $validated['venue'],
|
||||
'start_date' => $startDateTime,
|
||||
'end_date' => $endDateTime,
|
||||
'num_days' => $validated['num_days'],
|
||||
'activity_type' => $validated['activity_type'],
|
||||
'category' => $validated['category'],
|
||||
'level' => $validated['level'],
|
||||
'proof' => $proofPath,
|
||||
]);
|
||||
|
||||
return redirect()->route('faculty.dashboard')->with('status', 'Response submitted successfully');
|
||||
} catch (\Exception $e) {
|
||||
// Handle the exception and provide an error message
|
||||
return back()->withErrors('An error occurred while submitting your response: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
56
app/Models/ActivitiesOrganised.php
Normal file
56
app/Models/ActivitiesOrganised.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ActivitiesOrganised extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'activities_organiseds';
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'resource_person_name',
|
||||
'resource_person_organization',
|
||||
'target_audience',
|
||||
'number_of_participants',
|
||||
'objective',
|
||||
'outcomes',
|
||||
'department_id',
|
||||
'faculty_id',
|
||||
'start_date',
|
||||
'end_date',
|
||||
'num_days',
|
||||
'venue',
|
||||
'activity_type',
|
||||
'category',
|
||||
'level',
|
||||
'proof'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'start_date' => 'datetime',
|
||||
'end_date' => 'datetime',
|
||||
'number_of_participants' => 'integer',
|
||||
'num_days' => 'integer'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the department that owns the activity.
|
||||
*/
|
||||
public function department()
|
||||
{
|
||||
return $this->belongsTo(Department::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the faculty user that owns the activity.
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'faculty_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user