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

misc: tweaks and cleanups #640

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Console/Commands/UpdateQueueCalculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Helpers\TrainingStatus;
use App\Models\Area;
use Carbon\Carbon;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function handle()
foreach ($rating->trainings->where('area_id', $area->id)->whereNotNull('created_at')->whereNull('paused_at') as $training) {
// Include training with GRP ratings inside
if ($training->ratings->count() >= 1 && $training->ratings->first()->vatsim_rating) {
if ($training->status == 0) {
if ($training->status == TrainingStatus::IN_QUEUE->value) {
$trainingCreated = $training->created_at;

// Calculate the difference in seconds with Carbon, then subtract the paused time if any.
Expand Down
18 changes: 18 additions & 0 deletions app/Helpers/TrainingStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Helpers;

/**
* Constants for training status.
*/
enum TrainingStatus: int
{
case CLOSED_BY_SYSTEM = -4;
case CLOSED_BY_STUDENT = -3;
case CLOSED_BY_STAFF = -2;
case COMPLETED = -1;
case IN_QUEUE = 0;
case PRE_TRAINING = 1;
case ACTIVE_TRAINING = 2;
case AWAITING_EXAM = 3;
}
4 changes: 1 addition & 3 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use anlutro\LaravelSettings\Facade as Setting;
use App\Models\AtcActivity;
use App\Models\TrainingInterest;
use App\Models\TrainingReport;
use App\Models\User;
Expand Down Expand Up @@ -66,8 +65,7 @@ public function index()
// Check if there's an active vote running to advertise
$activeVote = Vote::where('closed', 0)->first();

$atcHoursDB = AtcActivity::find($user->id);
$atcHours = ($atcHoursDB == null) ? null : $atcHoursDB->hours;
$atcHours = $user->atcActivity->hours;

$studentTrainings = \Auth::user()->mentoringTrainings();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/GlobalSettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function edit(Request $request, Setting $setting)
isset($data['atcActivityNotifyInactive']) ? $atcActivityNotifyInactive = true : $atcActivityNotifyInactive = false;
isset($data['atcActivityAllowReactivation']) ? $atcActivityAllowReactivation = true : $atcActivityAllowReactivation = false;

// The setting dependecy removes keys that are empty, so this is a workaround...
// The setting dependency doesn't support null values, so we need to set it to false if it's not set
isset($data['linkMoodle']) ? $linkMoodle = $data['linkMoodle'] : $linkMoodle = false;
isset($data['trainingExamTemplate']) ? $trainingExamTemplate = $data['trainingExamTemplate'] : $trainingExamTemplate = false;

Expand Down
19 changes: 12 additions & 7 deletions app/Http/Controllers/TrainingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App;
use App\Helpers\TrainingStatus;
use App\Models\Area;
use App\Models\AtcActivity;
use App\Models\Rating;
Expand Down Expand Up @@ -209,9 +210,13 @@ public function create(Request $request, $prefillUserId = null)
$this->authorize('create', Training::class);

$students = User::all();
$ratings = Area::with('ratings')->get()->toArray();
$types = TrainingController::$types;

// Fetch all ratings and add C3 to all areas
$ratings = Area::with('ratings')->get()->each(function ($area) {
$area->ratings->push(Rating::where('name', 'C3')->first());
})->sortBy('name')->toArray();

return view('training.create', compact('students', 'ratings', 'types', 'prefillUserId'));
}

Expand Down Expand Up @@ -461,7 +466,7 @@ public function updateDetails(Training $training)
}

unset($attributes['mentors']);
} elseif (Auth::user()->isModeratorOrAbove()) { // XXX This is really hack since we don't send this attribute when mentors submit
} else {
// Detach all if no passed key, as that means the list is empty

foreach ($training->mentors as $mentor) {
Expand Down Expand Up @@ -492,7 +497,7 @@ public function updateDetails(Training $training)

// If training is closed, force to unpause
if ((int) $training->status != $oldStatus) {
if ((int) $training->status < 0) {
if ((int) $training->status < TrainingStatus::IN_QUEUE->value) {
$attributes['paused_at'] = null;
if (isset($training->paused_at)) {
TrainingActivityController::create($training->id, 'PAUSE', 0, null, Auth::user()->id);
Expand All @@ -510,12 +515,12 @@ public function updateDetails(Training $training)

// Send e-mail and store endorsements rating (non-GRP ones), if it's a new status and it goes from active to closed
if ((int) $training->status != $oldStatus) {
if ((int) $training->status < 0) {
if ((int) $training->status < TrainingStatus::IN_QUEUE->value) {
// Detach all mentors
$training->mentors()->detach();

// If the training was completed and double checked with a passed exam result, store the relevant endorsements
if ((int) $training->status == -1) {
if ((int) $training->status == TrainingStatus::COMPLETED->value) {
foreach ($training->ratings as $rating) {
if ($rating->vatsim_rating == null) {
// Revoke the old endorsement if active
Expand Down Expand Up @@ -546,7 +551,7 @@ public function updateDetails(Training $training)
}

// If training is completed with a passed exam result, let's set the user to active
if ((int) $training->status == -1) {
if ((int) $training->status == TrainingStatus::COMPLETED->value) {
// If training is [Refresh, Transfer or Fast-track] or [Standard and exam is passed]
if ($training->type <= 4) {
$training->user->atc_active = true;
Expand All @@ -571,7 +576,7 @@ public function updateDetails(Training $training)
return redirect($training->path())->withSuccess('Training successfully closed. E-mail confirmation sent to the student.');
}

if ((int) $training->status == 1) {
if ((int) $training->status == TrainingStatus::PRE_TRAINING->value) {
$training->user->notify(new TrainingPreStatusNotification($training));

return redirect($training->path())->withSuccess('Training successfully updated. E-mail confirmation of pre-training sent to the student.');
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/TrainingExaminationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Helpers\TrainingStatus;
use App\Models\OneTimeLink;
use App\Models\Position;
use App\Models\Training;
Expand All @@ -27,7 +28,7 @@ class TrainingExaminationController extends Controller
public function create(Request $request, Training $training)
{
$this->authorize('create', [TrainingExamination::class, $training]);
if ($training->status != 3) {
if ($training->status != TrainingStatus::AWAITING_EXAM->value) {
return redirect(null, 400)->to($training->path())->withSuccess('Training examination cannot be created for a training not awaiting exam.');
}

Expand Down
10 changes: 6 additions & 4 deletions app/Http/Controllers/TrainingReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Helpers\TrainingStatus;
use App\Models\OneTimeLink;
use App\Models\Position;
use App\Models\Training;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function index(Training $training)
public function create(Training $training)
{
$this->authorize('create', [TrainingReport::class, $training]);
if ($training->status < 1) {
if ($training->status < TrainingStatus::PRE_TRAINING->value) {
return redirect(null, 400)->back()->withErrors('Training report cannot be created for a training not in progress.');
}

Expand Down Expand Up @@ -73,10 +74,11 @@ public function store(Request $request, Training $training)

(isset($data['draft'])) ? $data['draft'] = true : $data['draft'] = false;

$data2 = $data; // TODO this should be refactored to something better
unset($data2['files']);
$report = TrainingReport::create($data2);
// Remove attachments , they are added in next step
unset($data['files']);
$report = TrainingReport::create($data);

// Add attachments
TrainingObjectAttachmentController::saveAttachments($request, $report);

// Notify student of new training request if it's not a draft
Expand Down
8 changes: 0 additions & 8 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ public function atcActivity()
return $this->hasOne(AtcActivity::class);
}

// TODO: decide if we should nuke me from orbit
public function atchours()
{
$atcHoursDB = AtcActivity::where('user_id', $this->id)->get()->first();

return ($atcHoursDB == null) ? null : $atcHoursDB->hours;
}

public function getNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
Expand Down
3 changes: 2 additions & 1 deletion app/Policies/OneTimeLinkPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Policies;

use App\Helpers\TrainingStatus;
use App\Models\OneTimeLink;
use App\Models\Training;
use App\Models\User;
Expand All @@ -20,7 +21,7 @@ public function create(User $user, Training $training, string $type)
{
// Only allow examination link generation if the training is awaiting exam
if ($type == OneTimeLink::TRAINING_EXAMINATION_TYPE) {
return $training->status == 3 && ($training->mentors->contains($user) || $user->isModeratorOrAbove($training->area));
return $training->status == TrainingStatus::AWAITING_EXAM->value && ($training->mentors->contains($user) || $user->isModeratorOrAbove($training->area));
}

return $training->mentors->contains($user) || $user->isModeratorOrAbove($training->area);
Expand Down
3 changes: 2 additions & 1 deletion app/Policies/TrainingActivityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class TrainingActivityPolicy
*/
public function comment(User $user, Training $training)
{
return $user->can('update', [Training::class, $training]);
return $training->mentors->contains($user) ||
$user->can('update', [Training::class, $training]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Policies/TrainingPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Policies;

use anlutro\LaravelSettings\Facade as Setting;
use App\Helpers\TrainingStatus;
use App\Models\Area;
use App\Models\Training;
use App\Models\TrainingExamination;
Expand Down Expand Up @@ -35,8 +36,7 @@ public function view(User $user, Training $training)
*/
public function update(User $user, Training $training)
{
return $training->mentors->contains($user) ||
$user->isModeratorOrAbove($training->area);
return $user->isModeratorOrAbove($training->area);
}

/**
Expand All @@ -56,7 +56,7 @@ public function delete(User $user, Training $training)
*/
public function close(User $user, Training $training)
{
return $user->is($training->user) && $training->status == 0;
return $user->is($training->user) && $training->status == TrainingStatus::IN_QUEUE->value;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Seeders;

use App\Helpers\FactoryHelper;
use App\Helpers\TrainingStatus;
use App\Models\Endorsement;
use App\Models\Group;
use App\Models\Position;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function run()
$training->ratings()->attach(Rating::where('vatsim_rating', '>', 1)->inRandomOrder()->first());

// Give all non-queued trainings a mentor
if ($training->status > 0) {
if ($training->status > TrainingStatus::IN_QUEUE->value) {
$training->mentors()->attach(
User::whereHas('groups', function ($query) {
$query->where('id', 3);
Expand All @@ -137,7 +138,7 @@ public function run()
}

// Give all exam awaiting trainings a solo endorsement
if ($training->status == 3) {
if ($training->status == TrainingStatus::AWAITING_EXAM->value) {
if (! Endorsement::where('user_id', $training->user_id)->exists()) {
$soloEndorsement = Endorsement::factory()->create([
'user_id' => $training->user_id,
Expand Down
8 changes: 4 additions & 4 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@
@endphp
<span title="{{ $reportDate->toEuropeanDate() }}">
@if($reportDate->isToday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
@elseif($reportDate->isYesterday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
@elseif($reportDate->diffInDays() <= 7)
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
@else
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
@endif

</span>
Expand Down
8 changes: 4 additions & 4 deletions resources/views/mentor/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@

<span title="{{ $reportDate->toEuropeanDate() }}">
@if($reportDate->isToday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
@elseif($reportDate->isYesterday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
@elseif($reportDate->diffInDays() <= 7)
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
@else
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
@endif

</span>
Expand Down
8 changes: 4 additions & 4 deletions resources/views/reports/mentors.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@
@endphp
<span data-bs-toggle="tooltip" data-bs-placement="top" title="{{ $reportDate->toEuropeanDate() }}">
@if($reportDate->isToday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Today</span>
@elseif($reportDate->isYesterday())
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">Yesterday</span>
@elseif($reportDate->diffInDays() <= 7)
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 1]) }}</span>
@else
<span class="{{ ($trainingIntervalExceeded && $training->status != 3 && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
<span class="{{ ($trainingIntervalExceeded && $training->status != \App\Helpers\TrainingStatus::AWAITING_EXAM->value && !$training->paused_at) ? 'text-danger' : '' }}">{{ $reportDate->diffForHumans(['parts' => 2]) }}</span>
@endif
</span>
@else
Expand Down
1 change: 0 additions & 1 deletion resources/views/training/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ class="form-control @error('student') is-invalid @enderror"
showTrainingLevels: function(event) {
const selectedTrainingArea = event.srcElement.options[event.srcElement.selectedIndex];
this.ratings = payload[selectedTrainingArea.getAttribute('data-id')].ratings;
this.ratings.push({id: 5, name: "C3"});
},
},
})
Expand Down
Loading