Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple periodic events from controller names #555

Open
wants to merge 14 commits into
base: development
Choose a base branch
from
25 changes: 25 additions & 0 deletions app/Events/SemesterEvaluationPeriodEnd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Events;

use App\Models\PeriodicEvent;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SemesterEvaluationPeriodEnd
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public PeriodicEvent $periodicEvent;

/**
* Create a new event instance.
*/
public function __construct(PeriodicEvent $periodicEvent)
{
$this->periodicEvent = $periodicEvent;
}
}
25 changes: 25 additions & 0 deletions app/Events/SemesterEvaluationPeriodReminder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Events;

use App\Models\PeriodicEvent;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SemesterEvaluationPeriodReminder
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public PeriodicEvent $periodicEvent;

/**
* Create a new event instance.
*/
public function __construct(PeriodicEvent $periodicEvent)
{
$this->periodicEvent = $periodicEvent;
}
}
24 changes: 24 additions & 0 deletions app/Events/SemesterEvaluationPeriodStart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Events;

use App\Models\PeriodicEvent;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SemesterEvaluationPeriodStart
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public PeriodicEvent $periodicEvent;
/**
* Create a new event instance.
*/
public function __construct(PeriodicEvent $periodicEvent)
{
$this->periodicEvent = $periodicEvent;
}
}
9 changes: 4 additions & 5 deletions app/Http/Controllers/Auth/AdmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Mail\ApplicationNoteChanged;
use App\Models\Application;
use App\Models\ApplicationWorkshop;
use App\Models\PeriodicEvent;
use App\Models\Semester;
use App\Models\SemesterStatus;
use App\Models\User;
Expand All @@ -16,7 +17,7 @@
use App\Models\Role;
use App\Models\Workshop;
use App\Utils\ApplicationHandler;
use App\Utils\HasPeriodicEvent;
use App\Utils\PeriodicEventController;
use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -33,15 +34,13 @@
/**
* Controller handling the admittance and administrative process.
*/
class AdmissionController extends Controller
class AdmissionController extends PeriodicEventController
{
use ApplicationHandler;
use HasPeriodicEvent;

public function __construct()
{
$this->underlyingControllerName =
\App\Http\Controllers\Auth\ApplicationController::class;
parent::__construct(PeriodicEvent::APPLICATION_PERIOD);
}

/**
Expand Down
49 changes: 43 additions & 6 deletions app/Http/Controllers/Auth/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Exports\ApplicantsExport;
use App\Models\Application;
use App\Models\ApplicationForm;
use App\Models\Faculty;
use App\Models\File;
use App\Models\PeriodicEvent;
use App\Models\Role;
use App\Models\RoleUser;
use App\Models\Semester;
use App\Models\User;
use App\Models\Workshop;
use App\Utils\HasPeriodicEvent;
use App\Utils\ApplicationHandler;
use Illuminate\Auth\AuthenticationException;
use App\Utils\PeriodicEventController;
use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Maatwebsite\Excel\Facades\Excel;

/**
* Controller handling the applicantion process.
*/
class ApplicationController extends Controller
class ApplicationController extends PeriodicEventController
{
use HasPeriodicEvent;
use ApplicationHandler;

private const EDUCATIONAL_ROUTE = 'educational';
Expand All @@ -28,7 +38,35 @@ class ApplicationController extends Controller
private const DELETE_FILE_ROUTE = 'files.delete';
private const SUBMIT_ROUTE = 'submit';

public function __construct()
{
parent::__construct(PeriodicEvent::APPLICATION_PERIOD);
}

/**
* Update the PeriodicEvent connected to the applications.
* @throws AuthorizationException
*/
public function updateApplicationPeriod(Request $request): RedirectResponse
{
$this->authorize('finalize', Application::class);

$request->validate([
'semester_id' => 'required|exists:semesters,id',
'start_date' => 'required|date',
'end_date' => 'required|date|after:now|after:start_date',
'extended_end_date' => 'nullable|date|after:end_date',
]);

$this->updatePeriodicEvent(
Semester::find($request->semester_id),
Carbon::parse($request->start_date),
Carbon::parse($request->end_date),
$request->extended_end_date ? Carbon::parse($request->extended_end_date) : null
);

return back()->with('message', __('general.successful_modification'));
}

/**
* Return the view based on the request's page parameter.
Expand Down Expand Up @@ -74,7 +112,6 @@ public function show(Request $request): View|RedirectResponse
/**
* @param Request $request
* @return RedirectResponse
* @throws AuthenticationException
*/
public function store(Request $request): RedirectResponse
{
Expand Down
118 changes: 17 additions & 101 deletions app/Http/Controllers/Secretariat/SemesterEvaluationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@

namespace App\Http\Controllers\Secretariat;

use App\Http\Controllers\Controller;
use App\Jobs\PeriodicEventsProcessor;
use App\Mail\EvaluationFormAvailable;
use App\Mail\EvaluationFormAvailableDetails;
use App\Mail\EvaluationFormClosed;
use App\Mail\EvaluationFormReminder;
use App\Mail\StatusDeactivated;
use App\Models\Faculty;
use App\Models\GeneralAssemblies\GeneralAssembly;
use App\Models\Question;
use App\Models\PeriodicEvent;
use App\Models\Role;
use App\Models\RoleUser;
use App\Models\Semester;
use App\Models\SemesterEvaluation;
use App\Models\SemesterStatus;
use App\Models\User;
use App\Models\Workshop;
use App\Utils\HasPeriodicEvent;
use App\Utils\PeriodicEventController;
use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class SemesterEvaluationController extends Controller
class SemesterEvaluationController extends PeriodicEventController
{
use HasPeriodicEvent;
public function __construct()
{
parent::__construct(PeriodicEvent::SEMESTER_EVALUATION_PERIOD);
}

/**
* Update the PeriodicEvent for the evaluation form.
Expand Down Expand Up @@ -66,73 +61,11 @@ public function updateEvaluationPeriod(Request $request): RedirectResponse
return back()->with('message', __('general.successful_modification'));
}

/**
* Send email that the form is available.
* @return void
*/
public function handlePeriodicEventStart(): void
{
Mail::to(config('contacts.mail_membra'))->queue(new EvaluationFormAvailable($this->getDeadline()));
if (User::secretary()) {
Mail::to(User::secretary())->queue(new EvaluationFormAvailableDetails(User::secretary()->name, $this->getDeadline()));
}
if (User::president()) {
Mail::to(User::president())->queue(new EvaluationFormAvailableDetails(User::president()->name, $this->getDeadline()));
}
}

/**
* Send reminder that the form is available.
* @param int $daysBeforeEnd
* @return void
*/
public function handlePeriodicEventReminder(int $daysBeforeEnd): void
{
if($daysBeforeEnd < 3) {
$userCount = $this->usersHaventFilledOutTheForm()->count();
Mail::to(config('contacts.mail_membra'))->queue(new EvaluationFormReminder($userCount, $this->getDeadline()));
}
}

/**
* Send email about results and deactivate collegists who did not fill out the form.
*/
public function handlePeriodicEventEnd()
{
$users = $this->usersHaventFilledOutTheForm();
$users_names = $users->pluck('name')->toArray();

if (User::secretary()) {
Mail::to(User::secretary())->queue(new EvaluationFormClosed(User::secretary()->name, $users_names));
}
if (User::president()) {
Mail::to(User::president())->queue(new EvaluationFormClosed(User::president()->name, $users_names));
}
if (User::director()) {
Mail::to(User::director())->queue(new EvaluationFormClosed(User::director()->name, $users_names));
}
foreach (User::workshopLeaders() as $user) {
Mail::to($user)->queue(new EvaluationFormClosed($user->name));
}


foreach ($users as $user) {
try {
Mail::to($user)->queue(new StatusDeactivated($user->name));
RoleUser::withoutEvents(function () use ($user) {
self::deactivateCollegist($user);
});
} catch (\Exception $e) {
Log::error('Error deactivating collegist: ' . $user->name . ' - ' . $e->getMessage());
}
}
}

/**
* Show the evaluation form.
* @throws AuthenticationException|AuthorizationException
*/
public function show()
public function show(): View
{
$this->authorize('fillOrManage', SemesterEvaluation::class);

Expand All @@ -141,20 +74,20 @@ public function show()
'user' => user(),
'faculties' => Faculty::all(),
'workshops' => Workshop::all(),
'evaluation' => user()->semesterEvaluations()->where('semester_id', Semester::current()->id)->first(),
'evaluation' => user()->semesterEvaluations()->where('semester_id', self::semester()->id)->first(),
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Static semester() Method Not Found

  • The semester() method is not defined within SemesterEvaluationController or its parent classes.
🔗 Analysis chain

Verify accessibility of self::semester() method

The use of self::semester() assumes that a static semester() method is available within the class hierarchy. Please ensure that this method exists and returns the expected Semester instance.

To confirm the existence and accessibility of the semester() method, you may run:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for the definition of the static method 'semester'.

# Test: Search for the 'static function semester()' declaration.
rg --type php -A 2 -B 2 'static function semester\(\)' app/

Length of output: 6556

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kdmnk this is indeed true; what about this?

where('semester_id', $this->periodicEvent()?->semester()->id ?? Semester::current())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

'general_assemblies' => GeneralAssembly::all()->sortByDesc('closed_at')->take(2),
'community_services' => user()->communityServiceRequests()->where('semester_id', Semester::current()->id)->get(),
'community_services' => user()->communityServiceRequests()->where('semester_id', self::semester()->id)->get(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

'position_roles' => user()->roles()->whereIn('name', Role::STUDENT_POSTION_ROLES)->get(),
'periodicEvent' => $this->periodicEvent(),
'users_havent_filled_out' => user()->can('manage', SemesterEvaluation::class) ? $this->usersHaventFilledOutTheForm() : null,
'users_havent_filled_out' => user()->can('manage', SemesterEvaluation::class) ? User::doesntHaveStatusFor(self::semester()->succ())->get() : null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here:

($this->periodicEvent()?->semester()->id ?? Semester::current())->succ()

]);
}

/**
* Update form information.
* @throws \Exception
*/
public function store(Request $request)
public function store(Request $request): RedirectResponse
{
$this->authorize('fill', SemesterEvaluation::class);

Expand Down Expand Up @@ -221,7 +154,10 @@ public function store(Request $request)
]
));
if ($request->next_status == Role::ALUMNI) {
self::deactivateCollegist($user);
RoleUser::withoutEvents(function () use ($user) {
$user->removeRole(Role::collegist());
$user->addRole(Role::alumni());
});
return redirect()->route('home')->with('message', __('general.successful_modification'));
} else {
if (!isset($request->next_status)) {
Expand All @@ -239,24 +175,4 @@ public function store(Request $request)

return back()->with('message', __('general.successful_modification'))->with('section', $request->section);
}

/**
* @return User[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function usersHaventFilledOutTheForm()
{
return User::withRole(Role::COLLEGIST)->verified()->whereDoesntHave('semesterStatuses', function ($query) {
$query->where('semester_id', $this->semester()?->succ()?->id);
})->get();
}

/**
* Deactivate a collegist and set alumni role.
* @param User $user
*/
public static function deactivateCollegist(User $user)
{
$user->removeRole(Role::collegist());
$user->addRole(Role::alumni());
}
}
Loading