Skip to content

Commit

Permalink
Merge pull request #278 from czqoocavatsim/JoshuaNeedsCoffee
Browse files Browse the repository at this point in the history
Add/Remove Students Manually & Training Session Updates
  • Loading branch information
JoshuaMicallefYBSU authored Aug 15, 2024
2 parents 37af685 + d8dcfe2 commit c044401
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 47 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/DiscordTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public function Shanwick()
'data' => $result,
]);
}

public function SendEmbed()
{
//New Applicant in Instructor Channel
$discord = new DiscordClient();
$discord->sendMessageWithEmbed('772787726009237524', 'Oceanic Training Cancelled!',
'Your training request with Gander Oceanic has been terminated.
If you would like to begin training again, please re-apply via the Gander Website.');
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/Training/ApplicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function apply(Request $request)

//Download via CURL
$url = 'https://api.vatsim.net/v2/members/'.auth()->id().'/stats';
// $url = 'https://api.vatsim.net/v2/members/1342084/stats';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -332,6 +333,7 @@ public function adminViewApplication($reference_id)

//Download via CURL
$url = 'https://api.vatsim.net/v2/members/'.$application->user_id.'/stats';
// $url = 'https://api.vatsim.net/v2/members/1342084/stats';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -443,7 +445,7 @@ public function adminAcceptApplication($reference_id, Request $request)
$discord->assignRole($student->user->discord_user_id, 482824058141016075);

//Create Instructor Thread
$discord->createTrainingThread(intval(config('services.discord.training_forum')), $student->user->fullName('FLC'), '<@'.$student->user->discord_user_id.'>');
$discord->createTrainingThread($student->user->fullName('FLC'), '<@'.$student->user->discord_user_id.'>');

// Notify Senior Team that the application was accepted.
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.applications')), 'Accepted Applicant', $student->user->fullName('FLC').' has just been accepted.', 'error');
Expand Down
71 changes: 54 additions & 17 deletions app/Http/Controllers/Training/InstructingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ public function addStudent(Request $request)
Session::flash('info', 'Unable to remove Discord permissions automatically.');
}

//Give not ready status label
$student->assignStatusLabel(StudentStatusLabel::whereName('Not Ready')->first());
//Give Awaiting Exam status label
$label = new StudentStatusLabelLink([
'student_status_label_id' => StudentStatusLabel::whereName('Awaiting Exam')->first()->id,
'student_id' => $student->id,
]);
$label->save();

//Create roster object
if (!RosterMember::where('cid', $student->user_id)->first()) {
Expand All @@ -272,6 +276,30 @@ public function addStudent(Request $request)
$rosterMember->active = 1;
$rosterMember->save();

//Discord Updates
if ($student->user->hasDiscord() && $student->user->member_of_czqo) {
//Get Discord client
$discord = new DiscordClient();

//Add student discord role
$discord->assignRole($student->user->discord_user_id, 482824058141016075);

//Create Instructor Thread
$discord->createTrainingThread($student->user->fullName('FLC'), '<@'.$student->user->discord_user_id.'>');

// Notify Senior Team that the application was accepted.
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.applications')), 'Manually Added Student', $student->user->fullName('FLC').' has just been added as a manual student. Their training record has been created automatically.', 'error');

} else {
Session::flash('info', 'Unable to add Discord permissions automatically, as the member is not in the Discord.');

//Get Discord client
$discord = new DiscordClient();

// Notify Senior Team that new Applicant is not a member of the Discord Server
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.applications')), 'Manually Added Student not in Discord', $student->user->fullName('FLC').' is not a member of Gander Oceanic. They will need to be contacted via email.', 'error');
}

//Notify
$student->user->notify(new AddedAsStudent());

Expand Down Expand Up @@ -372,19 +400,28 @@ public function removeStudent($cid)
//Remove role
$student->user->removeRole('Student');

//Remove role on Discord if able
try {
if ($student->user->hasDiscord() && $student->user->member_of_czqo) {
//Get Discord client
$discord = new DiscordClient();
//Discord Updates
if ($student->user->hasDiscord() && $student->user->member_of_czqo) {
//Get Discord client
$discord = new DiscordClient();

//Remove student role
$discord->removeRole($student->user->discord_user_id, 482824058141016075);
} else {
Session::flash('info', 'Unable to remove Discord permissions automatically.');
}
} catch (\Exception $e) {
Session::flash('info', 'Unable to remove Discord permissions automatically.');
//remove student discord role
$discord->removeRole($student->user->discord_user_id, 482824058141016075);

//close Instructor Thread
$discord->closeTrainingThread($student->user->fullName('FLC'), 'cancel');

// Notify Senior Team that new training has been terminated.
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.instructors')), 'Training Terminated', $student->user->fullName('FLC').' has had their training terminated.', 'error');

} else {
Session::flash('info', 'Unable to add Discord permissions automatically, as the member is not in the Discord.');

//Get Discord client
$discord = new DiscordClient();

// Notify Senior Team that training has been terminated
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.instructors')), 'Training Terminated', $student->user->fullName('FLC').' has had their training terminated.', 'error');
}

//Remove labels and instructor links and availability
Expand Down Expand Up @@ -421,8 +458,8 @@ public function certifyStudent($cid)
$student = Student::where('user_id', $cid)->firstOrFail();

// Remove Student Status & Set Controller as Active
// $student->user->removeRole('Student');
// $student->user->addRole('Certified Controller');
$student->user->removeRole('Student');
$student->user->assignRole('Certified Controller');
$student->user->removeRole('Guest');

// Update Traing Lable
Expand All @@ -441,7 +478,7 @@ public function certifyStudent($cid)

// Close Training Thread Out & Send Completion Message
$discord = new DiscordClient();
$discord->closeTrainingThread($student->user->fullName('FLC'));
$discord->closeTrainingThread($student->user->fullName('FLC'), 'certfied');

// Update Roster with Certification Status
$rosterMember = RosterMember::where('cid', $cid)->firstOrFail();
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Controllers/Training/SessionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public function createTrainingSession(Request $request)
$discord = new DiscordClient();
$discord->sendMessageWithEmbed(config('app.env') == 'local' ? intval(config('services.discord.web_logs')) : intval(config('services.discord.instructors')), 'New training session scheduled #'.$session->id, $session->instructor->user->fullName('FLC').' has scheduled a new training session with '.$session->student->user->fullName('FLC').' on '.$request->get('scheduled_time'));

//Discord Notification in Training Thread
$discord = new DiscordClient();
$discord->sendEmbedInTrainingThread($session->student->user->fullName('FLC'), 'Training Session Booked',
'Hello, '. $session->student->user->fullName('F').'!
A training session has been created for you at '.Carbon::parse($request->get('scheduled_time'))->format('l, jS F H:i\z').'.
Please reach out to your instructor in this training thread if you have any questions.');

//Return
return redirect()->route('training.admin.instructing.training-sessions.view', $session->id)->with('success', 'Session created!');
}
Expand Down Expand Up @@ -125,6 +134,15 @@ public function editTrainingSessionTime(Request $request, $session_id)
//Notify
$session->student->user->notify(new SessionScheduledTimeChanged($session, 'training'));

//Discord Notification in Training Thread
$discord = new DiscordClient();
$discord->sendEmbedInTrainingThread($session->student->user->fullName('FLC'), 'Training Session Time Changed',
'Hello, '. $session->student->user->fullName('F').'!
A training session time has been updated. The new time is now '.Carbon::parse($request->get('new_time'))->format('l, jS F H:i\z').'.
Please reach out to your instructor in this training thread if you have any questions.');

//Return
return redirect()->route('training.admin.instructing.training-sessions.view', $session)->with('success', 'Scheduled time changed!');
}
Expand Down Expand Up @@ -218,6 +236,15 @@ public function cancelTrainingSession(Request $request, $session_id)
//Notify student
$session->student->user->notify(new SessionCancelled($session, 'training'));

//Discord Notification in Training Thread
$discord = new DiscordClient();
$discord->sendEmbedInTrainingThread($session->student->user->fullName('FLC'), 'Training Session Cancelled',
'Hello, '. $session->student->user->fullName('F').'!
A training session which was planned has been cancelled.
Please contact your instructor if you believe this was a mistake.');

//Soft delete session
$session->delete();

Expand Down
49 changes: 39 additions & 10 deletions app/Services/DiscordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public function changeName($userId, $name)
}

// Function to create a user training thread
public function createTrainingThread($channelId, $name, $user)
public function createTrainingThread($name, $user)
{
try {
$response = $this->client->post("channels/{$channelId}/threads", [
$response = $this->client->post("channels/".env('DISCORD_TRAINING_FORUM')."/threads", [
'json' => [
'name' => $name,
'applied_tags' => [1271845980865695774], //Tag ID for 'New Request'
Expand Down Expand Up @@ -116,7 +116,7 @@ public function createTrainingThread($channelId, $name, $user)

}

public function closeTrainingThread($name)
public function closeTrainingThread($name, $status)
{
// Get active Discord Threads
$active_threads = $this->client->get('guilds/'.env('DISCORD_GUILD_ID').'/threads/active');
Expand All @@ -126,19 +126,30 @@ public function closeTrainingThread($name)

foreach ($threads_data['threads'] as $thread) {
if ($thread['name'] == $name) {

if($status == "certify"){
$this->sendMessageWithEmbed($thread['id'], 'Oceanic Training Completed!',
'Congratulations, you have now been certified on Gander & Shanwick Oceanic!
This training thread will now be closed due to the completion of your training.
If you have any questions, please reach out to your Instructor, or ask your question in <#836707337829089322>.
Enjoy controlling Gander & Shanwick OCA!');
} elseif($status == "cancel") {
$this->sendMessageWithEmbed($thread['id'], 'Oceanic Training Cancelled!',
'Your training request with Gander Oceanic has been terminated.
If you would like to begin training again, please re-apply via the Gander Website.');
}

// Lock and Archive the Thread
$data = $this->client->patch('channels/'.$thread['id'], [
'json' => [
'locked' => true,
'archived' => true,
]
]);

$this->sendMessageWithEmbed($thread['id'], 'Training Completed!',
'Congratulations, you have now been certified on Gander Oceanic!
This thread is now being closed due to the completion of your training.
Enjoy controlling Gander & Shanwick!');
}
}

Expand Down Expand Up @@ -183,5 +194,23 @@ public function EditThreadTag($lable, $name)
}
}

// Send Embed Message in Training Thread
public function sendEmbedInTrainingThread($name, $title, $message)
{
// Get active Discord Threads
$active_threads = $this->client->get('guilds/'.env('DISCORD_GUILD_ID').'/threads/active');

// Decode Data
$threads_data = json_decode($active_threads->getBody(), true);

// Loop through all threads to find students training record
foreach ($threads_data['threads'] as $thread) {
if ($thread['name'] == $name) {
// Send Embed Message
$this->sendMessageWithEmbed($thread['id'], $title, $message);
}
}
}

}

10 changes: 5 additions & 5 deletions resources/views/admin/training/instructing/board.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@extends('admin.training.layouts.main')
@section('title', 'Board - Instructing - ')
@section('training-content')
<h1 class="blue-text mb-4">Board</h1>
<h4 class="blue-text mb-4">Student Status</h4>
<h1 class="blue-text">Instructing Overview - Board</h1>
<h4 class="blue-text mb-4">All Student's Status</h4>
<div class="row">
@foreach($lists as $list)
<div class="col-lg-4">
<div class="card p-3 z-depth-1 shadow-none mb-3" style="min-height: 160px;">
<h5><i class="fa fa-circle fa-fw {{$list->colour}}-text"></i>&nbsp;{{$list->name}}</h5>
<h5><i class="fa fa-circle fa-fw {{$list->colour}}-text"></i>&nbsp;{{$list->name}} ({{count($list->students)}})</h5>
@if (count($list->students) > 0)
<div class="list-group mt-3">
@foreach($list->students as $student)
Expand All @@ -33,15 +33,15 @@
@endforeach
</div>

<h4 class="blue-text mt-4">View By Instructor</h4>
<div class="row">
<h4 class="blue-text mb-4">Instructor View</h4>
@foreach($instructors as $list)
<div class="col-lg-4">
<div class="card p-4 z-depth-1 shadow-none mb-3" style="min-height: 160px;">
<div class="d-flex flex-row align-items-center">
<a title="View Instructor Profile" href="{{route('training.admin.instructing.instructors.view', $list->user_id)}}"><img src="{{$list->user->avatar()}}" style="height: 30px; width:30px;margin-right: 10px; border-radius: 50%;"></a>
<div class="d-flex flex-column align-items-center h-100">
<h5 class="mb-0">{{$list->user->fullName('F')}}'s Students</h5>
<h5 class="mb-0">{{$list->user->fullName('F')}}'s Students ({{count($list->studentsAssigned)}})</h5>
</div>
</div>
@if (count($list->studentsAssigned) > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<form action="{{route('training.admin.instructing.training-sessions.edit.time', $session->id)}}" method="POST">
@csrf
<div class="modal-body">
<p>This will notify the Student in their Discord Thread</p>
@if($errors->editTimeErrors->any())
<div class="alert alert-danger">
<h4>There were errors</h4>
Expand Down Expand Up @@ -303,7 +304,7 @@
</button>
</div>
<div class="modal-body">
<p>The student will be notified by email. Please ensure proper communication with the student.</p>
<p>The student will be notified by email and discord. Please ensure proper communication with the student if you have not already explained the reason.</p>
<p>The session will also no longer be available to view.</p>
</div>
<div class="modal-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
<h5 class="blue-text">Actions</h5>
<ul class="list-unstyled mt-2">
@can('edit students')
<li class="mb-2">
<a data-target="#deleteStudentModal" data-toggle="modal" style="text-decoration:none;"><span class="red-text"><i class="fas fa-chevron-right"></i></span> &nbsp; <span class="black-text">Remove as student</span></a>
</li>
<div class="list-group z-depth-1">
<a data-target="#deleteStudentModal" data-toggle="modal" class="list-group-item list-group-item-action red-text"><i class="fas fa-dumpster-fire mr-3"></i>Remove User as Student</a>
</div>
@endcan
@if($student->user->rosterProfile->certification == "training")
<li class="mb-2">
<a data-target="#certifyStudentModal" data-toggle="modal" style="text-decoration:none;"><span class="green-text"><i class="fas fa-chevron-right"></i></span> &nbsp; <span class="black-text">Certify Controller</span></a>
</li>
<div class="list-group z-depth-1">
<a data-target="#certifyStudentModal" data-toggle="modal" class="list-group-item list-group-item-action green-text"><i class="fas fa-check mr-3"></i>Certify Controller</a>
</div>
@endif
</ul>
<h5 class="blue-text">Records</h5>
Expand Down Expand Up @@ -147,7 +147,7 @@
Expires on {{$student->soloCertification()->expires->toFormattedDateString()}}
</div>
@endif
@if(Auth::user()->hasAnyRole('Senior Staff|Administrator') || ($student->instructor() && $student->instructor()->instructor == Auth::user()->instructorProfile))
{{-- @if(Auth::user()->hasAnyRole('Senior Staff|Administrator') || ($student->instructor() && $student->instructor()->instructor == Auth::user()->instructorProfile))
<h5 class="mt-4 blue-text">Requests</h5>
<div class="list-group z-depth-1">
@if(!$student->soloCertification() && !$student->setAsReadyForAssessment())
Expand All @@ -156,7 +156,7 @@
<div class="list-group-item text-muted"><i>Already recommended for solo certification/solo certification in progress</i></div>
@endif
</div>
@endif
@endif --}}
</div>
</div>

Expand Down
Loading

0 comments on commit c044401

Please sign in to comment.