Skip to content

Commit

Permalink
feat: add course event relation (#720)
Browse files Browse the repository at this point in the history
* feat: add course event relation

* fix: redirect on illegal registration

* fix: use 'with' for eloquent parent/childeren models
  • Loading branch information
simonostendorf authored Sep 15, 2024
1 parent 2c4d768 commit d4bd213
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DashboardController extends Controller
public function index(): Response
{
// get events ordered by sort_order
$events = Event::orderBy('sort_order')->get();
$events = Event::orderBy('sort_order')->with('courses')->get();

// get registrations of the user
$registrations = Auth::user()->registrations;
Expand Down
26 changes: 18 additions & 8 deletions app/Http/Controllers/DashboardEventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Registration;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
Expand Down Expand Up @@ -41,7 +42,7 @@ protected function redirectToEvent(Event $event): RedirectResponse
* Redirect to the event page for the given event if the registration is not possible
*
*
* @return \Illuminate\Http\RedirectResponse|void
* @return RedirectResponse|void
*/
protected function redirectToEventIfNoRegistrationIsPossible(Event $event)
{
Expand All @@ -58,13 +59,22 @@ protected function redirectToEventIfNoRegistrationIsPossible(Event $event)

return $this->redirectToEvent($event);
}

// check if the user has the right courses to register
if ($event->courses()->exists()) {
if (! $event->courses()->where('course_id', Auth::user()->course_id)->exists()) {
Session::flash('error', 'Du kannst dich mit deinem Studiengang zu diesem Event nicht anmelden.');

return $this->redirectToEvent($event);
}
}
}

/**
* Redirect to the event page for the given event if the unregistration is not possible
*
*
* @return \Illuminate\Http\RedirectResponse|void
* @return RedirectResponse|void
*/
protected function redirectToEventIfNoUnregistrationIsPossible(Event $event)
{
Expand Down Expand Up @@ -111,14 +121,14 @@ public function index(Request $request): Response
/**
* Display the event register page
*/
public function register(Request $request): Response
public function register(Request $request): Response|RedirectResponse
{
$event = $this->getEvent($request);
if ($event instanceof \Inertia\Response) {
return $event;
}
$registrationIsPossible = $this->redirectToEventIfNoRegistrationIsPossible($event);
if ($registrationIsPossible instanceof \Illuminate\Http\RedirectResponse) {
if ($registrationIsPossible instanceof RedirectResponse) {
return $registrationIsPossible;
}
$event->slots = $event->slots()->get();
Expand All @@ -131,14 +141,14 @@ public function register(Request $request): Response
/**
* Display the event unregister page
*/
public function unregister(Request $request): Response
public function unregister(Request $request): Response|RedirectResponse
{
$event = $this->getEvent($request);
if ($event instanceof \Inertia\Response) {
return $event;
}
$unregistrationIsPossible = $this->redirectToEventIfNoUnregistrationIsPossible($event);
if ($unregistrationIsPossible instanceof \Illuminate\Http\RedirectResponse) {
if ($unregistrationIsPossible instanceof RedirectResponse) {
return $unregistrationIsPossible;
}

Expand All @@ -157,7 +167,7 @@ public function registerUser(Request $request): RedirectResponse
return $event;
}
$registrationIsPossible = $this->redirectToEventIfNoRegistrationIsPossible($event);
if ($registrationIsPossible instanceof \Illuminate\Http\RedirectResponse) {
if ($registrationIsPossible instanceof RedirectResponse) {
return $registrationIsPossible;
}

Expand Down Expand Up @@ -245,7 +255,7 @@ public function unregisterUser(Request $request): RedirectResponse
return $event;
}
$unregistrationIsPossible = $this->redirectToEventIfNoUnregistrationIsPossible($event);
if ($unregistrationIsPossible instanceof \Illuminate\Http\RedirectResponse) {
if ($unregistrationIsPossible instanceof RedirectResponse) {
return $unregistrationIsPossible;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardTutorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DashboardTutorController extends Controller
public function index(): Response
{
// get events ordered by sort_order
$events = Event::orderBy('sort_order')->with('registrations')->get();
$events = Event::orderBy('sort_order')->with('registrations')->with('courses')->get();

return Inertia::render('Dashboard/Tutor/Index', [
'events' => $events,
Expand Down
37 changes: 37 additions & 0 deletions app/Models/CourseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;
use OwenIt\Auditing\Contracts\Auditable;

class CourseEvent extends Pivot implements Auditable
{
use HasFactory;
use \OwenIt\Auditing\Auditable;

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* Get course for the course_event.
*/
public function course(): BelongsTo
{
return $this->belongsTo(Course::class);
}

/**
* Get event for the course_event.
*/
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
}
9 changes: 9 additions & 0 deletions app/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use OwenIt\Auditing\Contracts\Auditable;

Expand Down Expand Up @@ -63,4 +64,12 @@ public function stations(): HasMany
{
return $this->hasMany(Station::class);
}

/**
* Get courses for the group.
*/
public function courses(): BelongsToMany
{
return $this->belongsToMany(Course::class, 'course_event')->using(CourseEvent::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('course_event', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('course_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
$table->foreignId('event_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('course_event');
}
};
91 changes: 90 additions & 1 deletion database/seeders/EventsDemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Seeders;

use App\Models\Course;
use App\Models\CourseEvent;
use App\Models\CourseGroup;
use App\Models\Event;
use App\Models\Group;
Expand All @@ -20,6 +21,7 @@ class EventsDemoSeeder extends Seeder
public function run(): void
{
$this->runGroupPhase();
$this->runGroupPhaseLimitedCourses();
$this->runGroupPhaseByCourse();
$this->runEventRegistration();
$this->runSlotBooking();
Expand Down Expand Up @@ -86,6 +88,93 @@ public function runGroupPhase(): void
}
}

/**
* Run the "groupPhaseLimitedCourses" event seeds.
*/
public function runGroupPhaseLimitedCourses(): void
{
// check if event with name "group_phase-limited_courses" exists
$event = Event::where('name', 'group_phase-limited_courses')->first();
if ($event) {
return;
}

// set registration_from and registration_to
$registration_from = new DateTime(NOW());
$registration_to = new DateTime(NOW());
$registration_to->modify('+7 days');

// create a new event
$event = new Event;
$event->name = 'group_phase-limited_courses';
$event->description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae eros vitae nisl ultrices ult.';
$event->type = 'group_phase';
$event->registration_from = $registration_from;
$event->registration_to = $registration_to;
$event->has_requirements = false;
$event->consider_alcohol = true;
$event->sort_order = 110;

// save the event
$event->save();

// create event groups
$groups = [];
for ($i = 1; $i <= 14; $i++) {
$groups[] = [
'name' => "Gruppe $i",
'telegram_group_link' => "https://example.com/invalid-telegram-link",
];
}

// save groups
foreach ($groups as $groupData) {
$group = new Group;
$group->name = $groupData['name'];
$group->event_id = $event->id;
$group->telegram_group_link = $groupData['telegram_group_link'];
$group->save();
}

// get all courses
$courses = Course::all();

// map courses by abbreviation
$coursesByAbbreviation = [];
foreach ($courses as $course) {
$coursesByAbbreviation[$course->abbreviation] = $course;
}

// allowed courses
$allowedCourses = [
'ISE-Master',
'IS-Master',
'INF-Master',
'ET-Master',
];

// save courses
foreach ($allowedCourses as $courseAbbreviation) {
$course = $coursesByAbbreviation[$courseAbbreviation];
$course_event = new CourseEvent;
$course_event->course_id = $course->id;
$course_event->event_id = $event->id;
$course_event->save();
}

// get all users without roles
$users = User::doesntHave('roles')->get();

// register users to event
foreach ($users as $user) {
// create registration
$event->registrations()->create([
'user_id' => $user->id,
'drinks_alcohol' => rand(0, 1) == 1,
]);
}
}

/**
* Run the "groupPhaseByCourse" event seeds.
*/
Expand Down Expand Up @@ -168,7 +257,7 @@ public function runGroupPhaseByCourse(): void
$group->event_id = $event->id;
$group->save();

foreach ( $coursesCollection[$groupData['collection']] as $course){
foreach ($coursesCollection[$groupData['collection']] as $course) {
$coursegroup = new CourseGroup;
$coursegroup->course_id = $coursesByAbbreviation[$course]->id;
$coursegroup->group_id = $group->id;
Expand Down
Loading

0 comments on commit d4bd213

Please sign in to comment.