2020-04-29 17:15:54 +00:00
< ? php
namespace App\Http\Controllers ;
2020-05-08 07:10:25 +00:00
use App\Application ;
2020-06-26 23:32:33 +00:00
2020-05-08 07:10:25 +00:00
use App\Response ;
2020-05-08 05:06:24 +00:00
use App\Vacancy ;
2020-06-26 23:32:33 +00:00
use App\User ;
use App\Events\ApplicationDeniedEvent ;
use App\Notifications\NewApplicant ;
use App\Notifications\ApplicationMoved ;
2020-04-29 17:15:54 +00:00
use Illuminate\Http\Request ;
2020-05-08 07:10:25 +00:00
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Validator ;
2020-06-26 23:32:33 +00:00
use Illuminate\Support\Facades\App ;
use Illuminate\Support\Facades\Log ;
2020-04-29 17:15:54 +00:00
class ApplicationController extends Controller
{
2020-05-29 23:20:39 +00:00
private function canVote ( $votes )
{
$allvotes = collect ([]);
foreach ( $votes as $vote )
{
if ( $vote -> userID == Auth :: user () -> id )
{
$allvotes -> push ( $vote );
}
}
2020-06-26 23:32:33 +00:00
return ( $allvotes -> count () == 1 ) ? false : true ;
2020-05-29 23:20:39 +00:00
}
2020-04-30 15:38:54 +00:00
2020-06-26 23:32:33 +00:00
2020-05-11 15:44:47 +00:00
public function showUserApps ()
2020-04-30 15:38:54 +00:00
{
2020-05-11 15:44:47 +00:00
return view ( 'dashboard.user.applications' )
-> with ( 'applications' , Auth :: user () -> applications );
2020-04-30 15:55:14 +00:00
}
2020-06-26 23:32:33 +00:00
2020-05-22 02:49:16 +00:00
public function showUserApp ( Request $request , $applicationID )
{
2020-07-11 01:43:59 +00:00
// TODO: Inject it instead (do this where there is no injection, not just here)
2020-05-22 02:49:16 +00:00
$application = Application :: find ( $applicationID );
2020-06-26 23:32:33 +00:00
$this -> authorize ( 'view' , $application );
2020-05-22 02:49:16 +00:00
if ( ! is_null ( $application ))
{
return view ( 'dashboard.user.viewapp' )
-> with (
[
'application' => $application ,
2020-06-26 23:32:33 +00:00
'comments' => $application -> comments ,
2020-05-22 02:49:16 +00:00
'structuredResponses' => json_decode ( $application -> response -> responseData , true ),
'formStructure' => $application -> response -> form ,
2020-05-29 23:20:39 +00:00
'vacancy' => $application -> response -> vacancy ,
'canVote' => $this -> canVote ( $application -> votes )
2020-05-22 02:49:16 +00:00
]
);
}
else
{
$request -> session () -> flash ( 'error' , 'The application you requested could not be found.' );
}
return redirect () -> back ();
}
2020-04-30 21:53:57 +00:00
2020-06-26 23:32:33 +00:00
2020-07-11 01:43:59 +00:00
public function showAllApps ()
{
return view ( 'dashboard.appmanagement.all' )
-> with ( 'applications' , Application :: paginate ( 6 ));
}
2020-06-26 23:32:33 +00:00
2020-05-01 04:42:19 +00:00
public function showAllPendingApps ()
{
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'viewAny' , Application :: class );
2020-05-22 02:49:16 +00:00
return view ( 'dashboard.appmanagement.outstandingapps' )
-> with ( 'applications' , Application :: where ( 'applicationStatus' , 'STAGE_SUBMITTED' ) -> get ());
2020-05-01 04:42:19 +00:00
}
2020-05-22 02:49:16 +00:00
public function showPendingInterview ()
2020-05-01 05:21:44 +00:00
{
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'viewAny' , Application :: class );
2020-05-22 02:49:16 +00:00
$applications = Application :: with ( 'appointment' , 'user' ) -> get ();
$count = 0 ;
$pendingInterviews = collect ([]);
$upcomingInterviews = collect ([]);
foreach ( $applications as $application )
{
if ( ! is_null ( $application -> appointment ) && $application -> appointment -> appointmentStatus == 'CONCLUDED' )
{
$count =+ 1 ;
}
switch ( $application -> applicationStatus )
{
case 'STAGE_INTERVIEW' :
$upcomingInterviews -> push ( $application );
break ;
case 'STAGE_INTERVIEW_SCHEDULED' :
$pendingInterviews -> push ( $application );
break ;
}
}
return view ( 'dashboard.appmanagement.interview' )
-> with ([
'finishedCount' => $count ,
'applications' => $pendingInterviews ,
'upcomingApplications' => $upcomingInterviews
]);
2020-05-01 05:21:44 +00:00
}
2020-05-02 05:54:14 +00:00
2020-06-26 23:32:33 +00:00
2020-05-22 02:49:16 +00:00
public function showPeerReview ()
2020-05-02 05:54:14 +00:00
{
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'viewAny' , Application :: class );
2020-05-22 02:49:16 +00:00
return view ( 'dashboard.appmanagement.peerreview' )
-> with ( 'applications' , Application :: where ( 'applicationStatus' , 'STAGE_PEERAPPROVAL' ) -> get ());
2020-05-29 23:20:39 +00:00
2020-05-02 05:54:14 +00:00
}
2020-05-08 05:06:24 +00:00
2020-06-26 23:32:33 +00:00
2020-05-08 05:06:24 +00:00
public function renderApplicationForm ( Request $request , $vacancySlug )
{
2020-06-26 23:32:33 +00:00
// FIXME: Get rid of references to first(), this is a wonky query
2020-05-08 05:06:24 +00:00
$vacancyWithForm = Vacancy :: with ( 'forms' ) -> where ( 'vacancySlug' , $vacancySlug ) -> get ();
2020-06-26 23:32:33 +00:00
$firstVacancy = $vacancyWithForm -> first ();
if ( ! $vacancyWithForm -> isEmpty () && $firstVacancy -> vacancyCount !== 0 && $firstVacancy -> vacancyStatus == 'OPEN' )
2020-05-08 05:06:24 +00:00
{
return view ( 'dashboard.application-rendering.apply' )
-> with ([
'vacancy' => $vacancyWithForm -> first (),
'preprocessedForm' => json_decode ( $vacancyWithForm -> first () -> forms -> formStructure , true )
]);
}
else
{
2020-06-26 23:32:33 +00:00
abort ( 404 , 'The application you\'re looking for could not be found or it is currently unavailable.' );
2020-05-08 05:06:24 +00:00
}
}
2020-05-08 07:10:25 +00:00
2020-06-26 23:32:33 +00:00
2020-05-08 07:10:25 +00:00
public function saveApplicationAnswers ( Request $request , $vacancySlug )
{
$vacancy = Vacancy :: with ( 'forms' ) -> where ( 'vacancySlug' , $vacancySlug ) -> get ();
2020-06-26 23:32:33 +00:00
if ( $vacancy -> first () -> vacancyCount == 0 || $vacancy -> first () -> vacancyStatus !== 'OPEN' )
{
$request -> session () -> flash ( 'error' , 'This application is unavailable.' );
return redirect () -> back ();
}
2020-05-08 07:10:25 +00:00
Log :: info ( 'Processing new application!' );
$formStructure = json_decode ( $vacancy -> first () -> forms -> formStructure , true );
$responseStructure = [];
$excludedNames = [
'_token' ,
];
$validator = [];
foreach ( $request -> all () as $fieldName => $value )
{
if ( ! in_array ( $fieldName , $excludedNames ))
{
$validator [ $fieldName ] = 'required|string' ;
$responseStructure [ 'responses' ][ $fieldName ][ 'type' ] = $formStructure [ 'fields' ][ $fieldName ][ 'type' ] ? ? 'Unavailable' ;
$responseStructure [ 'responses' ][ $fieldName ][ 'title' ] = $formStructure [ 'fields' ][ $fieldName ][ 'title' ];
$responseStructure [ 'responses' ][ $fieldName ][ 'response' ] = $value ;
}
}
Log :: info ( 'Built response & validator structure!' );
$validation = Validator :: make ( $request -> all (), $validator );
if ( ! $validation -> fails ())
{
$response = Response :: create ([
'responseFormID' => $vacancy -> first () -> forms -> id ,
'associatedVacancyID' => $vacancy -> first () -> id , // Since a form can be used by multiple vacancies, we can only know which specific vacancy this response ties to by using a vacancy ID
'responseData' => json_encode ( $responseStructure )
]);
Log :: info ( 'Registered form response for user ' . Auth :: user () -> name . ' for vacancy ' . $vacancy -> first () -> vacancyName );
2020-06-26 23:32:33 +00:00
$application = Application :: create ([
2020-05-08 07:10:25 +00:00
'applicantUserID' => Auth :: user () -> id ,
'applicantFormResponseID' => $response -> id ,
'applicationStatus' => 'STAGE_SUBMITTED' ,
]);
Log :: info ( 'Submitted application for user ' . Auth :: user () -> name . ' with response ID' . $response -> id );
2020-06-26 23:32:33 +00:00
foreach ( User :: all () as $user )
{
if ( $user -> hasRole ( 'admin' ))
{
$user -> notify (( new NewApplicant ( $application , $vacancy -> first ())) -> delay ( now () -> addSeconds ( 10 )));
}
}
2020-05-08 07:10:25 +00:00
$request -> session () -> flash ( 'success' , 'Thank you for your application! It will be reviewed as soon as possible.' );
2020-05-22 02:49:16 +00:00
return redirect () -> to ( route ( 'showUserApps' ));
2020-05-08 07:10:25 +00:00
}
else
{
Log :: warning ( 'Application form for ' . Auth :: user () -> name . ' contained errors, resetting!' );
$request -> session () -> flash ( 'error' , 'There are one or more errors in your application. Please make sure none of your fields are empty, since they are all required.' );
}
return redirect () -> back ();
}
2020-05-22 02:49:16 +00:00
public function updateApplicationStatus ( Request $request , $applicationID , $newStatus )
{
$application = Application :: find ( $applicationID );
2020-06-27 18:15:33 +00:00
$this -> authorize ( 'update' , Application :: class );
2020-05-22 02:49:16 +00:00
if ( ! is_null ( $application ))
{
switch ( $newStatus )
{
case 'deny' :
2020-06-26 23:32:33 +00:00
event ( new ApplicationDeniedEvent ( $application ));
2020-05-22 02:49:16 +00:00
break ;
case 'interview' :
Log :: info ( 'User ' . Auth :: user () -> name . ' has moved application ID ' . $application -> id . 'to interview stage' );
$request -> session () -> flash ( 'success' , 'Application moved to interview stage! (:' );
$application -> setStatus ( 'STAGE_INTERVIEW' );
2020-06-26 23:32:33 +00:00
$application -> user -> notify ( new ApplicationMoved ());
2020-05-22 02:49:16 +00:00
break ;
default :
$request -> session () -> flash ( 'error' , 'There are no suitable statuses to update to. Do not mess with the URL.' );
}
}
else
{
$request -> session () -> flash ( 'The application you\'re trying to update does not exist.' );
}
return redirect () -> back ();
}
2020-04-29 17:15:54 +00:00
}