Skip to content

Commit

Permalink
Merge pull request #630 from EotvosCollegium/staging
Browse files Browse the repository at this point in the history
Deploy recent changes
  • Loading branch information
BertalanD authored Aug 18, 2024
2 parents abc80bd + 1eb5f44 commit 378d0b7
Show file tree
Hide file tree
Showing 22 changed files with 776 additions and 219 deletions.
126 changes: 85 additions & 41 deletions app/Http/Controllers/Auth/AdmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use App\Mail\ApplicationFileUploaded;
use App\Mail\ApplicationNoteChanged;
use App\Models\Application;
use App\Models\ApplicationWorkshop;
use App\Models\Semester;
use App\Models\SemesterStatus;
use App\Models\User;
use App\Models\RoleUser;
use App\Models\File;
Expand All @@ -17,6 +19,7 @@
use App\Utils\HasPeriodicEvent;
use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -178,54 +181,78 @@ public function update(Request $request, Application $application): RedirectResp
return redirect()->back();
}

/**
* Show the finalize page with final names and statistics
*/
public function indexFinalize(): View
{
$this->authorize('finalize', Application::class);
if(!($this->getDeadline() < now())) {
throw new \InvalidArgumentException('The application deadline has not passed yet.');
}
[$admitted, $not_admitted, $users_to_delete] = $this->getApplications();
return view('auth.admission.finalize', [
'semester' => $this->semester(),
'admitted_applications' => $admitted,
'users_to_delete' => $users_to_delete
->with('application')
->orderBy('name')
->get()
]);
}


/**
* Accept and delete applciations.
* @return RedirectResponse
* @throws AuthorizationException
*/
public function finalize(): RedirectResponse
{
// $this->authorize('finalizeApplicationProcess', User::class);
// Cache::forget('collegists');
// $not_handled_applicants = User::query()->withoutGlobalScope('verified')
// ->where('verified', 0)
// ->whereHas('application', function ($query) {
// $query->where('submitted', true);
// })
// ->count();
// if ($not_handled_applicants > 0) {
// return redirect()->back()->with('error', 'Még vannak feldolgozatlan jelentkezések!');
// }
// DB::transaction(function () {
// User::query()->withoutGlobalScope('verified')
// ->where('verified', 0)
// ->whereHas('application', function ($query) {
// $query->where('status', Application::STATUS_ACCEPTED);
// })
// ->update(['verified' => true]);
// $usersToDelete = User::query()->withoutGlobalScope('verified')
// ->where('verified', 0)->whereHas('application');
// foreach ($usersToDelete->get() as $user) {
// if ($user->profilePicture!=null) {
// Storage::delete($user->profilePicture->path);
// $user->profilePicture()->delete();
// }
// }
// $files = File::where('application_id', '!=', null);
// foreach ($files->get() as $file) {
// Storage::delete($file->path);
// }
// $files->delete();
// Application::query()->delete();
// $usersToDelete->forceDelete();
//
// RoleUser::where('role_id', Role::get(Role::APPLICATION_COMMITTEE_MEMBER)->id)->delete();
// RoleUser::where('role_id', Role::get(Role::AGGREGATED_APPLICATION_COMMITTEE_MEMBER)->id)->delete();
// });
//
// Cache::clear();
// return back()->with('message', 'Sikeresen jóváhagyta az elfogadott jelentkezőket');
return back()->with('error', 'Még nincs implementálva.');
$this->authorize('finalize', Application::class);
if(!($this->getDeadline() < now())) {
throw new \InvalidArgumentException('The application deadline has not passed yet.');
}
if(!$this->semester()) {
throw new \InvalidArgumentException('No semester can be retrieved from the application periodic event.');
}
DB::transaction(function () {
[$admitted, $not_admitted, $users_to_delete] = $this->getApplications();
// admit users
foreach ($admitted as $application) {
$application->user->update(['verified' => true]);
if($application->admitted_for_resident_status) {
$application->user->setResident();
} else {
$application->user->setExtern();
}
$application->user->workshops()->sync($application->admittedWorkshops);
$application->user->setStatusFor($this->semester(), SemesterStatus::ACTIVE);
$application->user->internetAccess->extendInternetAccess($this->semester()->getStartDate()->addMonth());
}
// delete data for not admitted users
$files = File::query()
->whereIn('application_id', $not_admitted->pluck('id')) // application files
->orWhereIn('user_id', $not_admitted->pluck('user_id')); // profile pictures
foreach ($files->get() as $file) {
Storage::delete($file->path);
}
$files->delete();
// soft deletes application, keep them for future reference
// (see https://github.com/EotvosCollegium/mars/issues/332#issuecomment-2014058021)
Application::whereIn('id', $admitted->pluck('id'))->delete();
Application::whereNotIn('id', $admitted->pluck('id'))->forceDelete();
ApplicationWorkshop::query()->delete();

// Note: users with not submitted applications will also be deleted
$users_to_delete->forceDelete();

RoleUser::where('role_id', Role::get(Role::APPLICATION_COMMITTEE_MEMBER)->id)->delete();
RoleUser::where('role_id', Role::get(Role::AGGREGATED_APPLICATION_COMMITTEE_MEMBER)->id)->delete();
});

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


Expand All @@ -252,4 +279,21 @@ public function getAccessibleWorkshops(User $user): Collection
}
return Workshop::all();
}

/**
* Helper function to get admittted, not admitted applications and users to delete.
* @return array
*/
private function getApplications()
{
$admitted = Application::query()->with(['user', 'applicationWorkshops'])->admitted()->get()->sortBy('user.name');
$not_admitted = Application::query()->whereNotIn('id', $admitted->pluck('id'))->get();
$users_to_delete_query = User::query()
->withoutGlobalScope('verified')
->whereIn('id', $not_admitted->pluck('user_id'))
//ignore users with any existing role
->whereDoesntHave('roles');

return [$admitted, $not_admitted, $users_to_delete_query];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function show()
$this->authorize('fillOrManage', SemesterEvaluation::class);

return view('secretariat.evaluation-form.app', [
'phd' => user()->educationalInformation->studyLines()->currentlyEnrolled()->where('type', 'phd')->exists(),
'phd' => user()->educationalInformation->isSenior(),
'user' => user(),
'faculties' => Faculty::all(),
'workshops' => Workshop::all(),
Expand Down
54 changes: 54 additions & 0 deletions app/Livewire/ApplicationRoleStatusUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Livewire;

use App\Models\Application;
use App\Models\ApplicationWorkshop;
use Carbon\Carbon;
use Livewire\Component;

class ApplicationRoleStatusUpdate extends Component
{
public Application $application;
public Carbon $lastUpdated;

/**
* Mount the component
* @param Application $application
* @return void
*/
public function mount(Application $application)
{
$this->application = $application;
$this->lastUpdated = Carbon::now()->subSeconds(2);
}

/**
* Update whether the applicant has been admitted as resident or not.
* @param $workshop
*/
public function switchResidentRole()
{
$this->authorize('finalize', Application::class);
$this->application->update(['admitted_for_resident_status' => !$this->application->admitted_for_resident_status]);
$this->lastUpdated = Carbon::now();
}

/**
* $this->updated
* @return bool
*/
public function getUpdatedProperty()
{
return $this->lastUpdated > Carbon::now()->subSeconds(2);
}

/**
* Render the component
* @return \Illuminate\View\View
*/
public function render()
{
return view('auth.application.role_status_update_component');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Carbon\Carbon;
use Livewire\Component;

class ApplicationStatusUpdate extends Component
class ApplicationWorkshopStatusUpdate extends Component
{
public Application $application;
public ApplicationWorkshop $workshop;
Expand All @@ -32,6 +32,7 @@ public function mount(Application $application, ApplicationWorkshop $workshop)
*/
public function callIn($workshop)
{
$this->authorize('editStatus', [\App\Models\Application::class, $this->workshop->workshop]);
$this->application->applicationWorkshops()->where('workshop_id', $workshop)->update(['called_in' => !$this->workshop->called_in]);
$this->lastUpdated = Carbon::now();
}
Expand All @@ -42,6 +43,7 @@ public function callIn($workshop)
*/
public function admit($workshop)
{
$this->authorize('editStatus', [\App\Models\Application::class, $this->workshop->workshop]);
$this->application->applicationWorkshops()->where('workshop_id', $workshop)->update(['admitted' => !$this->workshop->admitted]);
$this->lastUpdated = Carbon::now();
}
Expand All @@ -61,6 +63,6 @@ public function getUpdatedProperty()
*/
public function render()
{
return view('auth.application.status_update_component');
return view('auth.application.workshop_status_update_component');
}
}
52 changes: 50 additions & 2 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace App\Models;

use App\Utils\DataCompresser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Matrix\Builder;

/**
* App\Models\Application
Expand All @@ -19,6 +20,8 @@
* @property Collection $files
* @property boolean $submitted
* @property string $graduation_average
* @property boolean $applied_for_resident_status
* @property boolean $admitted_for_resident_status
* @property array $semester_average
* @property array $language_exam
* @property array $competition
Expand Down Expand Up @@ -64,11 +67,13 @@
class Application extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'user_id',
'submitted',
'applied_for_resident_status',
'admitted_for_resident_status',
'graduation_average',
'semester_average',
'language_exam',
Expand All @@ -86,7 +91,8 @@ class Application extends Model

protected $casts = [
'submitted' => 'bool',
'applied_for_resident_status' => 'bool'
'applied_for_resident_status' => 'bool',
'admitted_for_resident_status' => 'bool'
];

public const QUESTION_1 = [
Expand Down Expand Up @@ -126,6 +132,7 @@ public function applicationWorkshops(): HasMany
return $this->hasMany(ApplicationWorkshop::class);
}


/**
* The Workshop models that the user applied for.
* @return HasManyThrough
Expand All @@ -142,6 +149,14 @@ public function appliedWorkshops(): HasManyThrough
);
}

/**
* The Workshop models that the user admitted to.
*/
public function admittedWorkshops(): HasManyThrough
{
return $this->appliedWorkshops()->where('application_workshops.admitted', true);
}

/**
* Uploaded files
* @return HasMany
Expand All @@ -151,12 +166,45 @@ public function files(): HasMany
return $this->hasMany('App\Models\File');
}


/*
|--------------------------------------------------------------------------
| Local scopes
|--------------------------------------------------------------------------
*/

/**
* Scope a query to only include applications admitted to any workshop.
* @param Builder $query
* @return Builder
*/
public function scopeAdmitted(Builder $query): Builder
{
return $query->whereHas('applicationWorkshops', function ($query) {
$query->where('admitted', true);
});
}


/*
|--------------------------------------------------------------------------
| Accessors & Mutators
|--------------------------------------------------------------------------
*/

/**
* Get a bool whether the applicant has been admitted to any workshops.
*
* @return Attribute
*/
protected function admitted(): Attribute
{
return Attribute::make(
get: fn () => $this->applicationWorkshops()->where('admitted', true)->exists(),
);
}


/**
* Get a bool whether the applicant has been called in by any workshops.
*
Expand Down
Loading

0 comments on commit 378d0b7

Please sign in to comment.