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

feat: feedback #664

Merged
merged 9 commits into from
Oct 13, 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
71 changes: 71 additions & 0 deletions app/Http/Controllers/FeedbackController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Http\Controllers;

use anlutro\LaravelSettings\Facade as Setting;
use App\Models\Feedback;
use App\Models\Position;
use App\Models\User;
use App\Notifications\FeedbackNotification;
use Illuminate\Http\Request;

class FeedbackController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{

if (! Setting::get('feedbackEnabled')) {
return redirect()->route('dashboard')->withErrors('Feedback is currently disabled.');
}

$positions = Position::all();
$controllers = User::where('atc_active', true)->get();

return view('feedback.create', compact('positions', 'controllers'));
}

/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{

if (! Setting::get('feedbackEnabled')) {
return redirect()->route('dashboard')->withErrors('Feedback is currently disabled.');
}

$data = $request->validate([
'position' => 'nullable|exists:positions,callsign',
'controller' => 'nullable|exists:users,id',
'feedback' => 'required',
]);

$position = isset($data['position']) ? Position::where('callsign', $data['position'])->get()->first() : null;
$controller = isset($data['controller']) ? User::find($data['controller']) : null;
$feedback = $data['feedback'];

$submitter = auth()->user();

$feedback = Feedback::create([
'feedback' => $feedback,
'submitter_user_id' => $submitter->id,
'reference_user_id' => isset($controller) ? $controller->id : null,
'reference_position_id' => isset($position) ? $position->id : null,
]);

// Forward email if configured
if (Setting::get('feedbackForwardEmail')) {
$feedback->notify(new FeedbackNotification($feedback));
}

return redirect()->route('dashboard')->with('success', 'Feedback submitted!');

}
}
6 changes: 6 additions & 0 deletions app/Http/Controllers/GlobalSettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function edit(Request $request, Setting $setting)
'linkVisiting' => 'required|url',
'linkDiscord' => 'required|url',
'linkMoodle' => '',
'feedbackEnabled' => '',
'feedbackForwardEmail' => 'nullable|email',
'telemetryEnabled' => '',
]);

Expand All @@ -67,9 +69,11 @@ public function edit(Request $request, Setting $setting)
isset($data['atcActivityNotifyInactive']) ? $atcActivityNotifyInactive = true : $atcActivityNotifyInactive = false;
isset($data['atcActivityAllowReactivation']) ? $atcActivityAllowReactivation = true : $atcActivityAllowReactivation = false;
isset($data['atcActivityAllowInactiveControlling']) ? $atcActivityAllowInactiveControlling = true : $atcActivityAllowInactiveControlling = false;
isset($data['feedbackEnabled']) ? $feedbackEnabled = true : $feedbackEnabled = false;

// 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['feedbackForwardEmail']) ? $feedbackForwardEmail = $data['feedbackForwardEmail'] : $feedbackForwardEmail = false;
isset($data['trainingExamTemplate']) ? $trainingExamTemplate = $data['trainingExamTemplate'] : $trainingExamTemplate = false;

Setting::set('trainingEnabled', $trainingEnabled);
Expand All @@ -93,6 +97,8 @@ public function edit(Request $request, Setting $setting)
Setting::set('linkVisiting', $data['linkVisiting']);
Setting::set('linkDiscord', $data['linkDiscord']);
Setting::set('linkMoodle', $linkMoodle);
Setting::set('feedbackEnabled', $feedbackEnabled);
Setting::set('feedbackForwardEmail', $feedbackForwardEmail);
Setting::set('telemetryEnabled', $telemetryEnabled);
Setting::save();

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

use App\Models\Area;
use App\Models\Feedback;
use App\Models\Group;
use App\Models\ManagementReport;
use App\Models\Rating;
Expand Down Expand Up @@ -122,6 +123,18 @@ public function mentors()
return view('reports.mentors', compact('mentors', 'statuses'));
}

/**
* Index received feedback
*
* @return \Illuminate\View\View
*/
public function feedback()
{
$feedback = Feedback::all()->sortByDesc('created_at');

return view('reports.feedback', compact('feedback'));
}

/**
* Return the statistics for the cards (in queue, in training, awaiting exam, completed this year) on top of the page
*
Expand Down
12 changes: 1 addition & 11 deletions app/Mail/StaffNoticeMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
Expand All @@ -13,25 +12,16 @@ class StaffNoticeMail extends Mailable

private $mailSubject;

private $user;

private $textLines;

/**
* Create a new message instance.
*
* @param Endorsement $endorsement
* @param array $textLines an array of markdown lines to add
* @param string $contactMail optional contact e-mail to put in footer
* @param string $actionUrl optinal action button url
* @param string $actionText optional action button text
* @param string $actionColor optional bootstrap button color override
* @return void
*/
public function __construct(string $mailSubject, User $user, array $textLines)
public function __construct(string $mailSubject, array $textLines)
{
$this->mailSubject = $mailSubject;
$this->user = $user;
$this->textLines = $textLines;
}

Expand Down
30 changes: 30 additions & 0 deletions app/Models/Feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Feedback extends Model
{
use HasFactory;
use Notifiable;

protected $guarded = [];

public function submitter()
{
return $this->belongsTo(User::class, 'submitter_user_id');
}

public function referenceUser()
{
return $this->belongsTo(User::class, 'reference_user_id');
}

public function referencePosition()
{
return $this->belongsTo(Position::class, 'reference_position_id');
}
}
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public function getNameAttribute()
return $this->first_name . ' ' . $this->last_name;
}

public function submittedFeedback()
{
return $this->hasMany(Feedback::class, 'submitter_user_id');
}

public function receivedFeedback()
{
return $this->hasMany(Feedback::class, 'reference_user_id');
}

/**
* @todo: Convert to to new v9.x+ mutators https://laravel.com/docs/9.x/eloquent-mutators
*/
Expand Down
83 changes: 83 additions & 0 deletions app/Notifications/FeedbackNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Notifications;

use anlutro\LaravelSettings\Facade as Setting;
use App\Mail\StaffNoticeMail;
use App\Models\Endorsement;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class FeedbackNotification extends Notification implements ShouldQueue
{
use Queueable;

private $feedback;

/**
* Create a new notification instance.
*
* @param Endorsement $endorsement
*/
public function __construct($feedback)
{
$this->feedback = $feedback;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return EndorsementMail
*/
public function toMail($notifiable)
{

if (! Setting::get('feedbackEnabled')) {
return false;
}

$position = isset($this->feedback->referencePosition) ? $this->feedback->referencePosition->callsign : 'N/A';
$controller = isset($this->feedback->referenceUser) ? $this->feedback->referenceUser->name : 'N/A';

$textLines = [
'New feedback has been submitted by ' . $this->feedback->submitter->name . ' (' . $this->feedback->submitter->id . '). You may respond by replying to this email.',
'___',
'**Controller**: ' . $controller,
'**Position**: ' . $position,
'___',
'**Feedback**',
$this->feedback->feedback,

];

$feedbackForward = Setting::get('feedbackForwardEmail');

return (new StaffNoticeMail('Feedback submited', $textLines))
->to($feedbackForward)
->replyTo($this->feedback->submitter->email, $this->feedback->submitter->name);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [];
}
}
2 changes: 1 addition & 1 deletion app/Notifications/InactiveOnlineStaffNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function toMail($notifiable)
'All admins and moderators in area in question has been notified.',
];

return (new StaffNoticeMail('Unauthorized network logon recorded', $this->user, $textLines))
return (new StaffNoticeMail('Unauthorized network logon recorded', $textLines))
->to($this->sendTo->pluck('email'));
}

Expand Down
52 changes: 52 additions & 0 deletions database/migrations/2023_10_08_144001_add_feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('feedback', function (Blueprint $table) {
$table->id();
$table->text('feedback');
$table->unsignedBigInteger('submitter_user_id');
$table->unsignedBigInteger('reference_user_id')->nullable();
$table->unsignedBigInteger('reference_position_id')->nullable();
$table->boolean('forwarded')->default(false);
$table->timestamps();

$table->foreign('submitter_user_id')->references('id')->on('users')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign('reference_user_id')->references('id')->on('users')->onUpdate('CASCADE')->onDelete('CASCADE');
$table->foreign('reference_position_id')->references('id')->on('positions')->onUpdate('CASCADE')->onDelete('SET NULL');
});

Schema::table('settings', function (Blueprint $table) {
DB::table(Config::get('settings.table'))->insert([
['key' => 'feedbackEnable', 'value' => true],
['key' => 'feedbackForwardEmail', 'value' => false],
]);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('feedback');

Schema::table('settings', function (Blueprint $table) {
DB::table(Config::get('settings.table'))->where('key', 'feedbackEnable')->delete();
DB::table(Config::get('settings.table'))->where('key', 'feedbackForwardEmail')->delete();
});
}
};
29 changes: 29 additions & 0 deletions resources/views/admin/globalsettings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,35 @@
</div>
</div>

<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">Feedback</h6>
</div>
<div class="card-body">
<div class="row">
<div class="col-xl-12 col-md-12 mb-12">

<div class="form-check mb-4">
<input class="form-check-input @error('feedbackEnabled') is-invalid @enderror" type="checkbox" id="checkFeedback" name="feedbackEnabled" {{ Setting::get('feedbackEnabled') ? "checked" : "" }}>
<label class="form-check-label" for="checkFeedback">
Enable feedback functionality
</label>
</div>

<div class="mb-4">
<label class="form-label" for="feedbackForwardEmail">Forward feedback to e-mail</label>
<input type="email" class="form-control @error('feedbackForwardEmail') is-invalid @enderror" id="feedbackForwardEmail" name="feedbackForwardEmail" value="{{ (Setting::get("feedbackForwardEmail") != false) ? Setting::get("feedbackForwardEmail") : '' }}">
<small class="form-text">Forward feedback to the provided address. Leave blank to disable.</small>
</div>
@error('feedbackForwardEmail')
<span class="text-danger">{{ $errors->first('feedbackForwardEmail') }}</span>
@enderror

</div>
</div>
</div>
</div>

<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">Telemetry</h6>
Expand Down
Loading