Skip to content

Commit

Permalink
feat: Modularisation of types
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Oct 4, 2023
1 parent ca545c1 commit 2a83d25
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 99 deletions.
14 changes: 0 additions & 14 deletions app/Helpers/TaskType.php

This file was deleted.

17 changes: 3 additions & 14 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
class TaskController extends Controller
{

/**
* A list of possible types
*/
public static $types = [
1 => ['text' => 'Theoretical Exam Access', 'icon' => 'fas fa-key'],
2 => ['text' => 'Solo Endorsement', 'icon' => 'fas fa-clock'],
3 => ['text' => 'Rating Upgrade', 'icon' => 'fas fa-circle-arrow-up'],
4 => ['text' => 'Custom Memo', 'icon' => 'fas fa-message'],
];

/**
*
* Show the application task dashboard.
Expand All @@ -27,10 +17,9 @@ class TaskController extends Controller
public function index()
{
$user = auth()->user();
$tasks = Task::where('recipient_user_id', $user->id)->get()->sortByDesc('created_at');
$taskTypes = self::$types;

return view('tasks.index', compact('tasks', 'taskTypes'));
$tasks = Task::where('recipient_user_id', $user->id)->get()->sortBy('created_at');

return view('tasks.index', compact('tasks'));
}

}
9 changes: 9 additions & 0 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ public function sender()
{
return $this->belongsTo(User::class, 'sender_user_id');
}

public function type(){
if($this->type) {
return app($this->type);
} else {
throw new \Exception('Invalid task type: ' . $this->type);
}
}

}
35 changes: 35 additions & 0 deletions app/Tasks/TaskTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Tasks;

use App\Models\Task;

abstract class TaskTypes
{
protected $name;
protected $icon;

public function __construct()
{
$this->name = $this->getName();
$this->icon = $this->getIcon();
}

public function onCreated(){
// Default logic for creating a task
}

public function onCompleted(){
// Default logic for completing a task
}

public function onDeclined(){
// Default logic for declining a task
}

abstract public function getName();
abstract public function getIcon();
abstract public function getText(Task $model);
abstract public function getLink(Task $model);

}
26 changes: 26 additions & 0 deletions app/Tasks/Types/CustomRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Tasks\Types;

use App\Tasks\TaskTypes;
use App\Models\Task;

class CustomRequest extends TaskTypes
{
public function getName() {
return 'Custom Request';
}

public function getIcon() {
return 'fa-message';
}

public function getText(Task $model) {
return $model->message;
}

public function getLink(Task $model){
return false;
}

}
34 changes: 34 additions & 0 deletions app/Tasks/Types/RatingUpgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Tasks\Types;

use App\Models\Task;
use App\Models\User;
use App\Models\Training;
use App\Tasks\TaskTypes;

class RatingUpgrade extends TaskTypes
{
public function getName() {
return 'Rating Upgrade';
}

public function getIcon() {
return 'fa-circle-arrow-up';
}

public function getText(Task $model) {
return 'Upgrade rating to ' . Training::find($model->reference_training_id)->getInlineRatings();
}

public function getLink(Task $model){
$user = User::find($model->reference_user_id);
$userEud = $user->division == 'EUD';

if($userEud){
return 'https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid=' . $model->reference_user_id;
}

return false;
}
}
27 changes: 27 additions & 0 deletions app/Tasks/Types/SoloEndorsement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Tasks\Types;

use App\Tasks\TaskTypes;
use App\Models\Task;
use App\Models\User;
use App\Models\Training;

class SoloEndorsement extends TaskTypes
{
public function getName() {
return 'Solo Endorsement';
}

public function getIcon() {
return 'fa-clock';
}

public function getText(Task $model) {
return 'Grant solo endorsement';
}

public function getLink(Task $model){
return route('endorsements.create.id', $model->reference_user_id);
}
}
34 changes: 34 additions & 0 deletions app/Tasks/Types/TheoreticalExam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Tasks\Types;

use App\Tasks\TaskTypes;
use App\Models\Task;
use App\Models\User;
use App\Models\Training;

class TheoreticalExam extends TaskTypes
{
public function getName() {
return 'Theoretical Exam Access';
}

public function getIcon() {
return 'fa-key';
}

public function getText(Task $model) {
return 'Grant theoretical exam access';
}

public function getLink(Task $model){
$user = User::find($model->reference_user_id);
$userEud = $user->division == 'EUD';

if($userEud){
return 'https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid=' . $model->reference_user_id;
}

return false;
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"psr-4": {
"App\\": "app/",
"App\\Helpers\\": "app/helpers",
"App\\Tasks\\": "app/Tasks/",
"App\\Tasks\\Types\\": "app/Tasks/Types/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/2023_10_04_085425_tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->tinyInteger('type');
$table->string('type');
$table->tinyInteger('status')->default(0);
$table->string('status_comment', 256)->nullable();
$table->string('message', 256)->nullable();
Expand Down
101 changes: 31 additions & 70 deletions resources/views/tasks/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,43 @@
<div class="row">
<div class="col-xl-12 col-md-12 mb-12">
<div class="card shadow mb-4">
<div class="card-header bg-primary py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 fw-bold text-white">Tasks</h6>
</div>
@if($tasks->count())
<div class="card-header bg-primary py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 fw-bold text-white">Tasks</h6>
</div>
@endif
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-striped table-sm table-hover table-leftpadded mb-0" width="100%" cellspacing="0">
<thead class="table-light">
<tr>
<th>Task #</th>
<th>Received</th>
<th>Type</th>
<th>Regarding</th>
<th>Request</th>
<th>From</th>
<th>Actions</th>
</tr>
</thead>
<tbody>

@foreach($tasks as $task)
@if($tasks->count())

<table class="table table-striped table-sm table-hover table-leftpadded mb-0" width="100%" cellspacing="0">
<thead class="table-light">
<tr>
<td>{{ $task->id }}</td>
<td><span data-bs-toggle="tooltip" data-bs-placement="top" title="{{ $task->created_at->toEuropeanDateTime() }}">{{ $task->created_at->diffForHumans() }}</span></td>
<td><i class="fas {{ $taskTypes[$task->type]["icon"] }}"></i> {{ $taskTypes[$task->type]["text"] }}</td>
<td><a href="{{ route('user.show', $task->reference_user_id) }}">{{ \App\Models\User::find($task->reference_user_id)->name }} ({{ $task->reference_user_id }})</a></td>
<td>
@if($task->type == \App\Helpers\TaskType::THEORY_EXAM->value)

@if(\App\Models\User::find($task->reference_user_id)->division == 'EUD')
<i class="fas fa-bolt"></i>
<a href="https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid={{ $task->reference_user_id }}" target="_blank" class="link-offset-1 dotted-underline">Grant theoretical exam access</a>
@else
Grant theoretical exam access
@endif

@elseif($task->type == \App\Helpers\TaskType::SOLO_ENDORSEMENT->value)

<i class="fas fa-bolt"></i>
<a href="{{ route('endorsements.create.id', $task->reference_user_id) }}" target="_blank" class="link-offset-1 dotted-underline">Grant solo endorsement</a>

@elseif($task->type == \App\Helpers\TaskType::RATING_UPGRADE->value)

@if(\App\Models\User::find($task->reference_user_id)->division == 'EUD')
<i class="fas fa-bolt"></i>
<a href="https://www.atsimtest.com/index.php?cmd=admin&sub=memberdetail&memberid={{ $task->reference_user_id }}" target="_blank" class="link-offset-1 dotted-underline">Upgrade rating to {{ \App\Models\Training::find($task->reference_training_id)->getInlineRatings() }}</a>
@else
Upgrade rating to {{ \App\Models\Training::find($task->reference_training_id)->getInlineRatings() }}
@endif

@if(\App\Models\Training::find($task->reference_training_id)->status == \App\Helpers\TrainingStatus::COMPLETED->value)
<span class="fw-light">(Examination completed)</span>
@endif

@elseif($task->type == \App\Helpers\TaskType::MEMO->value)
{{ $task->message }}
@endif
</td>
<td>
@isset($task->sender_user_id)
<a href="{{ route('user.show', $task->sender_user_id) }}">{{ \App\Models\User::find($task->sender_user_id)->name }} ({{ $task->sender_user_id }})</a>
@else
System
@endisset
</td>

<td>
<a href="" class="btn btn-sm btn-outline-success"><i class="fas fa-check"></i> Complete</a>
<a href="" class="btn btn-sm btn-outline-danger"><i class="fas fa-xmark"></i> Decline</a>
</td>
<th>Task #</th>
<th>Received</th>
<th>Regarding</th>
<th>Request</th>
<th>From</th>
<th>Actions</th>
</tr>

@endforeach

</tbody>
</table>
</thead>
<tbody>
@foreach($tasks as $task)
@include('tasks.parts.row', ['task' => $task])
@endforeach
</tbody>
</table>
@endif

@if($tasks->count() == 0)
<div class="text-center pt-4 pb-4">
<i class="fas fa-umbrella-beach" style="font-size: 5rem;"></i>
<p class="pt-4 fs-5">
You have no tasks
</p>
</div>
@endif
</div>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions resources/views/tasks/parts/row.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<tr>
<td>{{ $task->id }}</td>
<td><span data-bs-toggle="tooltip" data-bs-placement="top" title="{{ $task->created_at->toEuropeanDateTime() }}">{{ $task->created_at->diffForHumans() }}</span></td>
<td><a href="{{ route('user.show', $task->reference_user_id) }}">{{ \App\Models\User::find($task->reference_user_id)->name }} ({{ $task->reference_user_id }})</a></td>
<td>
<i class="fas {{ $task->type()->getIcon() }}" data-bs-toggle="tooltip" data-bs-placement="top" title="{{ $task->type()->getName() }}"></i>

@if($task->type()->getLink($task))
<a href="{{ $task->type()->getLink($task) }}" target="_blank" class="link-offset-1 dotted-underline">{{ $task->type()->getText($task) }}</a>
@else
{{ $task->type()->getText($task) }}
@endif
</td>
<td>
@isset($task->sender_user_id)
<a href="{{ route('user.show', $task->sender_user_id) }}">{{ \App\Models\User::find($task->sender_user_id)->name }} ({{ $task->sender_user_id }})</a>
@else
System
@endisset
</td>

<td>
<a href="" class="btn btn-sm btn-outline-success"><i class="fas fa-check"></i> Complete</a>
<a href="" class="btn btn-sm btn-outline-danger"><i class="fas fa-xmark"></i> Decline</a>
</td>
</tr>

0 comments on commit 2a83d25

Please sign in to comment.