Passed
Push — dev ( d1fc13...809716 )
by Tristan
18:39
created

ApplicationByJobController::index()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 44
ccs 0
cts 17
cp 0
rs 8.8177
c 0
b 0
f 0
cc 6
nc 12
nop 1
crap 42
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Course;
6
use App\Models\Criteria;
7
use App\Models\Degree;
8
use App\Models\JobApplication;
9
use App\Models\JobApplicationAnswer;
10
use App\Models\JobPoster;
11
use App\Models\Lookup\ApplicationStatus;
12
use App\Models\Lookup\CitizenshipDeclaration;
13
use App\Models\Lookup\PreferredLanguage;
14
use App\Models\Lookup\ReviewStatus;
15
use App\Models\Lookup\SecurityClearance;
16
use App\Models\Lookup\SkillStatus;
17
use App\Models\Lookup\VeteranStatus;
18
use App\Models\Skill;
19
use App\Models\SkillDeclaration;
20
use App\Models\WorkExperience;
21
use App\Services\Validation\ApplicationValidator;
22
use App\Services\Validation\Rules\GovernmentEmailRule;
23
use App\Services\Validation\StrategicResponseApplicationValidator;
24
use Facades\App\Services\WhichPortal;
25
use Illuminate\Http\Request;
26
use Illuminate\Http\Resources\Json\JsonResource;
27
use Illuminate\Support\Facades\Auth;
28
use Illuminate\Support\Facades\Lang;
29
use Illuminate\Support\Facades\Log;
30
31
class ApplicationByJobController extends Controller
32
{
33
    /**
34
     * Display a listing of the applications for given jobPoster.
35
     *
36
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function index(JobPoster $jobPoster)
40
    {
41
        $jobPoster->loadMissing(['criteria', 'talent_stream_category', 'job_skill_level']);
42
43
        $view = 'manager/review_applications';
44
        $jobTitle = $jobPoster->title;
45
        $disableCloneJs = false;
46
        if ($jobPoster->department_id === config('app.strategic_response_department_id')) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        if ($jobPoster->department_id === /** @scrutinizer ignore-call */ config('app.strategic_response_department_id')) {
Loading history...
47
            $view = 'response/screening/index';
48
            // Hacky workaround for Accordion JS firing on the screening page.
49
            $disableCloneJs = true;
50
            if ($jobPoster->talent_stream_category && $jobPoster->job_skill_level) {
51
                $jobTitle = $jobPoster->talent_stream_category->name . ' - ' . $jobPoster->job_skill_level->name;
52
            }
53
        }
54
        $applications = $jobPoster->submitted_applications()
55
            ->with([
56
                'veteran_status',
57
                'citizenship_declaration',
58
                'application_review',
59
                'applicant.user',
60
            ])
61
            ->get();
62
63
        $viewData = [
64
            // Localization Strings.
65
            'jobs_l10n' => Lang::get('manager/job_index'),
66
            'response' => Lang::get('response/screening'),
67
            // Data.
68
            'job' => new JsonResource($jobPoster),
69
            'response_job_title' => $jobTitle,
70
            'job_id' => $jobPoster->id,
71
            'is_hr_portal' => WhichPortal::isHrPortal(),
72
            'portal' => WhichPortal::isHrPortal() ? 'hr' : 'manager',
73
            'applications' => $applications,
74
            'review_statuses' => ReviewStatus::all(),
75
            'isHrAdvisor' => Auth::user()->isHrAdvisor(),
76
        ];
77
78
        if ($disableCloneJs) {
79
            $viewData['disable_clone_js'] = true;
80
        }
81
82
        return view($view, $viewData);
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        return /** @scrutinizer ignore-call */ view($view, $viewData);
Loading history...
83
    }
84
85
    /**
86
     * Return the current applicant's application for a given Job Poster.
87
     *
88
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
89
     * @return mixed|\App\Models\JobApplication
90
     */
91
    protected function getApplicationFromJob(JobPoster $jobPoster)
92
    {
93
        $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
94
            ->where('job_poster_id', $jobPoster->id)->first();
95
        if ($application == null) {
96
            $application = new JobApplication();
97
            $application->job_poster_id = $jobPoster->id;
98
            $application->applicant_id = Auth::user()->applicant->id;
99
            $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
100
            $application->save();
101
        }
102
        return $application;
103
    }
104
105
    /**
106
     * Show the form for editing Application basics for the specified job.
107
     *
108
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function editBasics(JobPoster $jobPoster)
112
    {
113
        $applicant = Auth::user()->applicant;
114
        $application = $this->getApplicationFromJob($jobPoster);
115
116
        // Ensure user has permissions to view and update application.
117
        $this->authorize('view', $application);
118
        $this->authorize('update', $application);
119
120
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
121
            ? 'applicant/strategic_response_application/application_post_01'
122
            : 'applicant/application_post_01';
123
124
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
125
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
126
            : $jobPoster->title;
127
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
128
129
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        return /** @scrutinizer ignore-call */ view(
Loading history...
130
            $viewTemplate,
131
            [
132
                // Application Template Data.
133
                'application_step' => 1,
134
                'application_template' => Lang::get('applicant/application_template'),
135
                'language_options' => PreferredLanguage::all(),
136
                'citizenship_options' => CitizenshipDeclaration::all(),
137
                'veteran_options' => VeteranStatus::all(),
138
                'security_clearance_options' => SecurityClearance::all(),
139
                'preferred_language_template' => Lang::get('common/preferred_language'),
140
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
141
                'veteran_status_template' => Lang::get('common/veteran_status'),
142
                'header' => [
143
                    'title' => $headerTitle,
144
                ],
145
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 1),
146
                // Job Data.
147
                'job' => $jobPoster,
148
                // Applicant Data.
149
                'applicant' => $applicant,
150
                'job_application' => $application,
151
                // Submission.
152
                'form_submit_action' => route('job.application.update.1', $jobPoster),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.1', $jobPoster),
Loading history...
153
                'gov_email_pattern' => GovernmentEmailRule::PATTERN
154
            ]
155
        );
156
    }
157
158
    /**
159
     * Show the form for editing Application Experience for the specified job.
160
     *
161
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
162
     * @return \Illuminate\Http\Response
163
     */
164
    public function editExperience(JobPoster $jobPoster)
165
    {
166
        $applicant = Auth::user()->applicant;
167
        $application = $this->getApplicationFromJob($jobPoster);
168
169
        // Ensure user has permissions to view and update application.
170
        $this->authorize('view', $application);
171
        $this->authorize('update', $application);
172
173
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
174
            ? 'applicant/strategic_response_application/application_post_02'
175
            : 'applicant/application_post_02';
176
177
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
178
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
179
            : $jobPoster->title;
180
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
181
182
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        return /** @scrutinizer ignore-call */ view(
Loading history...
183
            $viewTemplate,
184
            [
185
                // Application Template Data.
186
                'application_step' => 2,
187
                'application_template' => Lang::get('applicant/application_template'),
188
                'header' => [
189
                    'title' => $headerTitle,
190
                ],
191
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 2),
192
                // Job Data.
193
                'job' => $jobPoster,
194
                // Applicant Data.
195
                'applicant' => $applicant,
196
                'job_application' => $application,
197
                // Submission.
198
                'form_submit_action' => route('job.application.update.2', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.2', $jobPoster)
Loading history...
199
            ]
200
        );
201
    }
202
203
    /**
204
     * Show the form for editing Application Essential Skills for the specified job.
205
     *
206
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
207
     * @return \Illuminate\Http\Response
208
     */
209
    public function editEssentialSkills(JobPoster $jobPoster)
210
    {
211
        $applicant = Auth::user()->applicant;
212
        $application = $this->getApplicationFromJob($jobPoster);
213
214
        // Ensure user has permissions to view and update application.
215
        $this->authorize('view', $application);
216
        $this->authorize('update', $application);
217
218
        $criteria = $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

218
        $criteria = $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
            return $value->criteria_type->name == 'essential'
220
                && $value->skill->skill_type->name == 'hard';
221
        });
222
223
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
224
            ? 'applicant/strategic_response_application/application_post_03'
225
            : 'applicant/application_post_03';
226
227
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
228
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
229
            : $jobPoster->title;
230
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
231
232
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
        return /** @scrutinizer ignore-call */ view(
Loading history...
233
            $viewTemplate,
234
            [
235
                // Application Template Data.
236
                'application_step' => 3,
237
                'application_template' => Lang::get('applicant/application_template'),
238
                'header' => [
239
                    'title' => $headerTitle,
240
                ],
241
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 3),
242
                // Job Data.
243
                'job' => $jobPoster,
244
                // Skills Data.
245
                'skills' => Skill::all(),
246
                'skill_template' => Lang::get('common/skills'),
247
                'criteria' => $criteria,
248
                // Applicant Data.
249
                'applicant' => $applicant,
250
                'job_application' => $application,
251
                // Submission.
252
                'form_submit_action' => route('job.application.update.3', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

252
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.3', $jobPoster)
Loading history...
253
            ]
254
        );
255
    }
256
257
    /**
258
     * Show the form for editing Application Asset Skills for the specified job.
259
     *
260
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
261
     * @return \Illuminate\Http\Response
262
     */
263
    public function editAssetSkills(JobPoster $jobPoster)
264
    {
265
        $applicant = Auth::user()->applicant;
266
        $application = $this->getApplicationFromJob($jobPoster);
267
268
        // Ensure user has permissions to view and update application.
269
        $this->authorize('view', $application);
270
        $this->authorize('update', $application);
271
272
        $criteria = $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

272
        $criteria = $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
273
            return $value->criteria_type->name == 'asset'
274
                && $value->skill->skill_type->name == 'hard';
275
        });
276
277
278
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
279
            ? 'applicant/strategic_response_application/application_post_04'
280
            : 'applicant/application_post_04';
281
282
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
283
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
284
            : $jobPoster->title;
285
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
286
287
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

287
        return /** @scrutinizer ignore-call */ view(
Loading history...
288
            $viewTemplate,
289
            [
290
                // Application Template Data.
291
                'application_step' => 4,
292
                'application_template' => Lang::get('applicant/application_template'),
293
                'header' => [
294
                    'title' => $headerTitle,
295
                ],
296
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 4),
297
                // Job Data.
298
                'job' => $jobPoster,
299
                // Skills Data.
300
                'skills' => Skill::all(),
301
                'skill_template' => Lang::get('common/skills'),
302
                'criteria' => $criteria,
303
                // Applicant Data.
304
                'applicant' => $applicant,
305
                'job_application' => $application,
306
                // Submission.
307
                'form_submit_action' => route('job.application.update.4', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.4', $jobPoster)
Loading history...
308
            ]
309
        );
310
    }
311
312
    /**
313
     * Show the Application Preview for the application for the specified job.
314
     *
315
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
316
     * @return \Illuminate\Http\Response
317
     */
318
    public function preview(JobPoster $jobPoster)
319
    {
320
        $applicant = Auth::user()->applicant;
321
        $application = $this->getApplicationFromJob($jobPoster);
322
323
        $this->authorize('view', $application);
324
325
        $essential_criteria = $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

325
        $essential_criteria = $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
326
            return $value->criteria_type->name == 'essential'
327
                && $value->skill->skill_type->name == 'hard';
328
        });
329
        $asset_criteria = $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

329
        $asset_criteria = $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
330
            return $value->criteria_type->name == 'asset'
331
                && $value->skill->skill_type->name == 'hard';
332
        });
333
334
        $skillDeclarations = $application->isDraft()
335
            ? $applicant->skill_declarations
336
            : $application->skill_declarations;
337
        $degrees = $application->isDraft()
338
            ? $applicant->degrees
339
            : $application->degrees;
340
        $courses = $application->isDraft()
341
            ? $applicant->courses
342
            : $application->courses;
343
        $work_experiences = $application->isDraft()
344
            ? $applicant->work_experiences
345
            : $application->work_experiences;
346
347
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
348
            ? 'applicant/strategic_response_application/application_post_05'
349
            : 'applicant/application_post_05';
350
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
351
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
352
            : $jobPoster->title;
353
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
354
355
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

355
        return /** @scrutinizer ignore-call */ view(
Loading history...
356
            $viewTemplate,
357
            [
358
                // Application Template Data.
359
                'application_step' => 5,
360
                'application_template' => Lang::get('applicant/application_template'),
361
                'preferred_language_template' => Lang::get('common/preferred_language'),
362
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
363
                'veteran_status_template' => Lang::get('common/veteran_status'),
364
                'header' => [
365
                    'title' => $headerTitle,
366
                ],
367
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 5),
368
                // Job Data.
369
                'job' => $jobPoster,
370
                // Skills Data.
371
                'skills' => Skill::all(),
372
                'skill_template' => Lang::get('common/skills'),
373
                'essential_criteria' => $essential_criteria,
374
                'asset_criteria' => $asset_criteria,
375
                // Applicant Data.
376
                'applicant' => $applicant,
377
                'job_application' => $application,
378
                'skill_declarations' => $skillDeclarations,
379
                'degrees' => $degrees,
380
                'courses' => $courses,
381
                'work_experiences' => $work_experiences,
382
                'is_manager_view' => WhichPortal::isManagerPortal(),
383
                'is_draft' => $application->application_status->name == 'draft',
384
            ]
385
        );
386
    }
387
388
    /**
389
     * Show the Confirm Submit page for the application for the specified job.
390
     *
391
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
392
     * @return \Illuminate\Http\Response
393
     */
394
    public function confirm(JobPoster $jobPoster)
395
    {
396
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
397
        $application = $this->getApplicationFromJob($jobPoster);
398
399
        $this->authorize('update', $application);
400
401
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
402
            ? 'applicant/strategic_response_application/application_post_06'
403
            : 'applicant/application_post_06';
404
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
405
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
406
            : $jobPoster->title;
407
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
408
409
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

409
        return /** @scrutinizer ignore-call */ view(
Loading history...
410
            $viewTemplate,
411
            [
412
                // Application Template Data.
413
                'application_step' => 6,
414
                'application_template' => Lang::get('applicant/application_template'),
415
                'header' => [
416
                    'title' => $headerTitle,
417
                ],
418
                'custom_breadcrumbs' => $this->customBreadcrumbs($jobPoster, 6),
419
                // Used by tracker partial.
420
                'job' => $jobPoster,
421
                'job_application' => $application,
422
                // Submission.
423
                'form_submit_action' => route('job.application.submit', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

423
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.submit', $jobPoster)
Loading history...
424
            ]
425
        );
426
    }
427
428
    /**
429
     * Show the application submission information.
430
     *
431
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
432
     * @return \Illuminate\Http\Response
433
     */
434
    public function complete(JobPoster $jobPoster)
435
    {
436
        // Include Applicant Data.
437
        $applicant = Auth::user()->applicant;
438
        // Include Application Data.
439
        $application = $this->getApplicationFromJob($jobPoster);
440
441
        // Ensure user has permissions to view application.
442
        $this->authorize('view', $application);
443
444
        $viewTemplate = $jobPoster->isInStrategicResponseDepartment()
445
            ? 'applicant/strategic_response_application/application_post_complete'
446
            : 'applicant/application_post_complete';
447
448
        $jobTitle = $jobPoster->isInStrategicResponseDepartment()
449
            ? "{$jobPoster->talent_stream_category->name} - {$jobPoster->job_skill_level->name}"
450
            : $jobPoster->title;
451
        $headerTitle = Lang::get('applicant/application_template')['title'] . ": {$jobTitle}";
452
453
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

453
        return /** @scrutinizer ignore-call */ view(
Loading history...
454
            $viewTemplate,
455
            [
456
                // Application Template Data.
457
                'application_template' => Lang::get('applicant/application_template'),
458
                'header' => [
459
                    'title' => $headerTitle,
460
                ],
461
                // Job Data.
462
                'job' => $jobPoster,
463
                // Applicant Data.
464
                'applicant' => $applicant,
465
                'job_application' => $application
466
            ]
467
        );
468
    }
469
470
    /**
471
     * Update the Application Basics in storage for the specified job.
472
     *
473
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
474
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
475
     * @return \Illuminate\Http\Response
476
     */
477
    public function updateBasics(Request $request, JobPoster $jobPoster)
478
    {
479
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
480
        $application = $this->getApplicationFromJob($jobPoster);
481
482
        // Ensure user has permissions to update this application.
483
        $this->authorize('update', $application);
484
485
        $application->fill([
486
            'citizenship_declaration_id' => $request->input('citizenship_declaration_id'),
487
            'veteran_status_id' => $request->input('veteran_status_id'),
488
            'preferred_language_id' => $request->input('preferred_language_id'),
489
            'language_requirement_confirmed' => $request->input('language_requirement_confirmed', false),
490
491
            // The following fields are exclusive Strategic Talent Response applications.
492
            'director_name' => $request->input('director_name'),
493
            'director_title' => $request->input('director_title'),
494
            'director_email' => $request->input('director_email'),
495
            'reference_name' => $request->input('reference_name'),
496
            'reference_title' => $request->input('reference_title'),
497
            'reference_email' => $request->input('reference_email'),
498
            'gov_email' => $request->input('gov_email'),
499
            'physical_office_willing' => $request->input('physical_office_willing', false),
500
            'security_clearance_id' => $request->input('security_clearance_id'),
501
        ]);
502
        $application->save();
503
504
        $questions = $jobPoster->job_poster_questions;
505
        $questionsInput = $request->input('questions');
506
        foreach ($questions as $question) {
507
            $answer = null;
508
            if (isset($questionsInput[$question->id])) {
509
                $answer = $questionsInput[$question->id];
510
            }
511
            $answerObj = $application->job_application_answers
512
                ->firstWhere('job_poster_question_id', $question->id);
513
            if ($answerObj == null) {
514
                $answerObj = new JobApplicationAnswer();
515
                $answerObj->job_poster_question_id = $question->id;
516
                $answerObj->job_application_id = $application->id;
517
            }
518
            $answerObj->answer = $answer;
519
            $answerObj->save();
520
        }
521
522
        // Redirect to correct page.
523
        switch ($request->input('submit')) {
524
            case 'save_and_quit':
525
            case 'save_and_return':
526
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

526
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
527
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
528
            case 'save_and_continue':
529
            case 'next':
530
                return redirect()->route('job.application.edit.2', $jobPoster);
531
                break;
532
            default:
533
                return redirect()->back()->withInput();
534
        }
535
    }
536
537
    /**
538
     * Update the Application Basics in storage for the specified job.
539
     *
540
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
541
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
542
     * @return \Illuminate\Http\Response
543
     */
544
    public function updateExperience(Request $request, JobPoster $jobPoster)
545
    {
546
        $applicant = Auth::user()->applicant;
547
        $application = $this->getApplicationFromJob($jobPoster);
548
549
        // Ensure user has permissions to update this application.
550
        $this->authorize('update', $application);
551
552
        // Record that the user has saved their experience for this application.
553
        $application->experience_saved = true;
554
        $application->save();
555
556
        $degrees = $request->input('degrees');
557
558
        $request->validate([
559
            'degrees.new.*.degree_type_id' => 'required',
560
            'degrees.new.*.area_of_study'  => 'required',
561
            'degrees.new.*.institution'    => 'required',
562
            'degrees.new.*.thesis'         => 'nullable',
563
            'degrees.new.*.start_date'     => 'required|date',
564
            'degrees.new.*.end_date'       => 'required|date',
565
            'degrees.new.*.blockcert_url'  => 'nullable|string',
566
        ]);
567
568
        // Save new degrees.
569
        if (isset($degrees['new'])) {
570
            foreach ($degrees['new'] as $degreeInput) {
571
                $degree = new Degree();
572
                $degree->fill([
573
                    'degree_type_id' => $degreeInput['degree_type_id'],
574
                    'area_of_study' => $degreeInput['area_of_study'],
575
                    'institution' => $degreeInput['institution'],
576
                    'thesis' => $degreeInput['thesis'],
577
                    'start_date' => $degreeInput['start_date'],
578
                    'end_date' => $degreeInput['end_date'],
579
                    'blockcert_url' => $degreeInput['blockcert_url'],
580
                ]);
581
                $applicant->degrees()->save($degree);
582
            }
583
        }
584
585
        // Update old degrees.
586
        if (isset($degrees['old'])) {
587
            foreach ($degrees['old'] as $id => $degreeInput) {
588
                // Ensure this degree belongs to this applicant.
589
                $degree = $applicant->degrees->firstWhere('id', $id);
590
                if ($degree != null) {
591
                    $degree->fill([
592
                        'degree_type_id' => $degreeInput['degree_type_id'],
593
                        'area_of_study' => $degreeInput['area_of_study'],
594
                        'institution' => $degreeInput['institution'],
595
                        'thesis' => $degreeInput['thesis'],
596
                        'start_date' => $degreeInput['start_date'],
597
                        'end_date' => $degreeInput['end_date'],
598
                        'blockcert_url' => $degreeInput['blockcert_url'],
599
                    ]);
600
                    $degree->save();
601
                } else {
602
                    Log::warning("Applicant $applicant->id attempted to update degree with invalid id: $id");
603
                }
604
            }
605
        }
606
607
        $courses = $request->input('courses');
608
609
        $request->validate([
610
            'courses.new.*.name'             => 'required',
611
            'courses.new.*.institution'      => 'required',
612
            'courses.new.*.course_status_id' => 'required',
613
            'courses.new.*.start_date'       => 'required|date',
614
            'courses.new.*.end_date'         => 'required|date',
615
        ]);
616
617
        // Save new courses.
618
        if (isset($courses['new'])) {
619
            foreach ($courses['new'] as $courseInput) {
620
                $course = new Course();
621
                $course->fill([
622
                    'name' => $courseInput['name'],
623
                    'institution' => $courseInput['institution'],
624
                    'course_status_id' => $courseInput['course_status_id'],
625
                    'start_date' => $courseInput['start_date'],
626
                    'end_date' => $courseInput['end_date']
627
                ]);
628
                $applicant->courses()->save($course);
629
            }
630
        }
631
632
        // Update old courses.
633
        if (isset($courses['old'])) {
634
            foreach ($courses['old'] as $id => $courseInput) {
635
                // Ensure this course belongs to this applicant.
636
                $course = $applicant->courses->firstWhere('id', $id);
637
                if ($course != null) {
638
                    $course->fill([
639
                        'name' => $courseInput['name'],
640
                        'institution' => $courseInput['institution'],
641
                        'course_status_id' => $courseInput['course_status_id'],
642
                        'start_date' => $courseInput['start_date'],
643
                        'end_date' => $courseInput['end_date']
644
                    ]);
645
                    $course->save();
646
                } else {
647
                    Log::warning("Applicant $applicant->id attempted to update course with invalid id: $id");
648
                }
649
            }
650
        }
651
652
        $work_experiences = $request->input('work_experiences');
653
654
        $request->validate([
655
            'work_experiences.new.*.role'        => 'required',
656
            'work_experiences.new.*.company'     => 'required',
657
            'work_experiences.new.*.description' => 'required',
658
            'work_experiences.new.*.start_date'  => 'required|date',
659
            'work_experiences.new.*.end_date'    => 'required|date',
660
        ]);
661
662
        // Save new work_experiences.
663
        if (isset($work_experiences['new'])) {
664
            foreach ($work_experiences['new'] as $workExperienceInput) {
665
                $workExperience = new WorkExperience();
666
                $workExperience->fill([
667
                    'role' => $workExperienceInput['role'],
668
                    'company' => $workExperienceInput['company'],
669
                    'description' => $workExperienceInput['description'],
670
                    'start_date' => $workExperienceInput['start_date'],
671
                    'end_date' => $workExperienceInput['end_date']
672
                ]);
673
                $applicant->work_experiences()->save($workExperience);
674
            }
675
        }
676
677
        // Update old work_experiences.
678
        if (isset($work_experiences['old'])) {
679
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
680
                // Ensure this work_experience belongs to this applicant.
681
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
682
                if ($workExperience != null) {
683
                    $workExperience->fill([
684
                        'role' => $workExperienceInput['role'],
685
                        'company' => $workExperienceInput['company'],
686
                        'description' => $workExperienceInput['description'],
687
                        'start_date' => $workExperienceInput['start_date'],
688
                        'end_date' => $workExperienceInput['end_date']
689
                    ]);
690
                    $workExperience->save();
691
                } else {
692
                    Log::warning("Applicant $applicant->id attempted to update work_experience with invalid id: $id");
693
                }
694
            }
695
        }
696
697
        // Redirect to correct page.
698
        switch ($request->input('submit')) {
699
            case 'save_and_quit':
700
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

700
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
701
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
702
            case 'save_and_continue':
703
            case 'next':
704
                return redirect()->route('job.application.edit.3', $jobPoster);
705
                break;
706
            case 'save_and_return':
707
                return redirect()->route('job.application.edit.1', $jobPoster);
708
                break;
709
            default:
710
                return redirect()->back()->withInput();
711
        }
712
    }
713
714
    /**
715
     * Update the Application Essential Skills in storage for the specified job.
716
     *
717
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
718
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
719
     * @return \Illuminate\Http\Response
720
     */
721
    public function updateEssentialSkills(Request $request, JobPoster $jobPoster)
722
    {
723
        $applicant = Auth::user()->applicant;
724
        $application = $this->getApplicationFromJob($jobPoster);
725
726
        // Ensure user has permissions to update this application.
727
        $this->authorize('update', $application);
728
729
        $skillDeclarations = $request->input('skill_declarations');
730
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
731
732
        // Save new skill declarations.
733
        if (isset($skillDeclarations['new'])) {
734
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
735
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
736
                    $skillDeclaration = new SkillDeclaration();
737
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
738
                    $skillDeclaration->skill_status_id = $claimedStatusId;
739
                    $skillDeclaration->fill([
740
                        'description' => $skillDeclarationInput['description'],
741
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
742
                    ]);
743
                    $applicant->skill_declarations()->save($skillDeclaration);
744
745
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
746
                    $skillDeclaration->references()->sync($referenceIds);
747
748
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
749
                    $skillDeclaration->work_samples()->sync($sampleIds);
750
                }
751
            }
752
        }
753
754
        // Update old declarations.
755
        if (isset($skillDeclarations['old'])) {
756
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
757
                foreach ($typeInput as $id => $skillDeclarationInput) {
758
                    // Ensure this declaration belongs to this applicant.
759
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
760
                    if ($skillDeclaration != null) {
761
                        // skill_id and skill_status cannot be changed.
762
                        $skillDeclaration->fill([
763
                            'description' => $skillDeclarationInput['description'],
764
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
765
                        ]);
766
                        $skillDeclaration->save();
767
768
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
769
                        $skillDeclaration->references()->sync($referenceIds);
770
771
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
772
                        $skillDeclaration->work_samples()->sync($sampleIds);
773
                    } else {
774
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
775
                    }
776
                }
777
            }
778
        }
779
780
        // Redirect to correct page.
781
        switch ($request->input('submit')) {
782
            case 'save_and_quit':
783
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

783
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
784
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
785
            case 'save_and_continue':
786
            case 'next':
787
                return redirect()->route('job.application.edit.4', $jobPoster);
788
                break;
789
            case 'save_and_return':
790
                return redirect()->route('job.application.edit.2', $jobPoster);
791
                break;
792
            default:
793
                return redirect()->back()->withInput();
794
        }
795
    }
796
797
    /**
798
     * Update the Application Asset Skills in storage for the specified job.
799
     *
800
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
801
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
802
     * @return \Illuminate\Http\Response
803
     */
804
    public function updateAssetSkills(Request $request, JobPoster $jobPoster)
805
    {
806
        $applicant = Auth::user()->applicant;
807
        $application = $this->getApplicationFromJob($jobPoster);
808
809
        // Ensure user has permissions to update this application.
810
        $this->authorize('update', $application);
811
812
        $skillDeclarations = $request->input('skill_declarations');
813
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
814
815
        // Save new skill declarations.
816
        if (isset($skillDeclarations['new'])) {
817
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
818
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
819
                    $skillDeclaration = new SkillDeclaration();
820
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
821
                    $skillDeclaration->skill_status_id = $claimedStatusId;
822
                    $skillDeclaration->fill([
823
                        'description' => $skillDeclarationInput['description'],
824
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
825
                    ]);
826
                    $applicant->skill_declarations()->save($skillDeclaration);
827
828
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
829
                    $skillDeclaration->references()->sync($referenceIds);
830
831
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
832
                    $skillDeclaration->work_samples()->sync($sampleIds);
833
                }
834
            }
835
        }
836
837
        // Update old declarations.
838
        if (isset($skillDeclarations['old'])) {
839
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
840
                foreach ($typeInput as $id => $skillDeclarationInput) {
841
                    // Ensure this declaration belongs to this applicant.
842
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
843
                    if ($skillDeclaration != null) {
844
                        // skill_id and skill_status cannot be changed.
845
                        $skillDeclaration->fill([
846
                            'description' => $skillDeclarationInput['description'],
847
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
848
                        ]);
849
                        $skillDeclaration->save();
850
851
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
852
                        $skillDeclaration->references()->sync($referenceIds);
853
854
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
855
                        $skillDeclaration->work_samples()->sync($sampleIds);
856
                    } else {
857
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
858
                    }
859
                }
860
            }
861
        }
862
863
        // Redirect to correct page.
864
        switch ($request->input('submit')) {
865
            case 'save_and_quit':
866
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

866
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
867
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
868
            case 'save_and_continue':
869
            case 'next':
870
                return redirect()->route('job.application.edit.5', $jobPoster);
871
                break;
872
            case 'save_and_return':
873
                return redirect()->route('job.application.edit.3', $jobPoster);
874
                break;
875
            default:
876
                return redirect()->back()->withInput();
877
        }
878
    }
879
880
    /**
881
     * Submit the Application for the specified job.
882
     *
883
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
884
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
885
     * @return \Illuminate\Http\Response
886
     */
887
    public function submit(Request $request, JobPoster $jobPoster)
888
    {
889
        $application = $this->getApplicationFromJob($jobPoster);
890
891
        // Only complete submission if submit button was pressed.
892
        if ($request->input('submit') == 'submit' && $application->application_status->name == 'draft') {
893
            // Ensure user has permissions to update this application.
894
            $this->authorize('update', $application);
895
            $request->validate([
896
                'submission_signature' => [
897
                    'required',
898
                    'string',
899
                    'max:191',
900
                ],
901
                'submission_date' => [
902
                    'required',
903
                    'string',
904
                    'max:191',
905
                ]
906
            ]);
907
908
            // Save any final info.
909
            $application->fill([
910
                'submission_signature' => $request->input('submission_signature'),
911
                'submission_date' => $request->input('submission_date'),
912
            ]);
913
914
            // Error out of this process now if application is not complete.
915
            $validator = $jobPoster->isInStrategicResponseDepartment()
916
                ? new StrategicResponseApplicationValidator()
917
                : new ApplicationValidator();
918
919
            $validatorInstance = $validator->validator($application);
920
            if (!$validatorInstance->passes()) {
921
                $userId = $application->applicant->user_id;
922
                $msg = "Application $application->id for user $userId is invalid for submission: " .
923
                    implode('; ', $validator->detailedValidatorErrors($application));
924
                Log::info($msg);
925
            }
926
            $validator->validate($application);
927
928
            // Change status to 'submitted'.
929
            $application->application_status_id = ApplicationStatus::where('name', 'submitted')->firstOrFail()->id;
930
            $application->saveProfileSnapshot();
931
        }
932
933
        $application->save();
934
935
        // Redirect to correct page.
936
        switch ($request->input('submit')) {
937
            case 'save_and_quit':
938
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

938
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
939
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
940
            case 'submit':
941
                return redirect()->route('job.application.complete', $jobPoster);
942
                break;
943
            case 'save_and_return':
944
                return redirect()->route('job.application.edit.4', $jobPoster);
945
                break;
946
            default:
947
                return redirect()->back()->withInput();
948
        }
949
    }
950
951
    /**
952
     * Custom breadcrumbs for application process.
953
     *
954
     * @param  \App\Models\JobPoster $jobPoster        Incoming Job Poster object.
955
     * @param  string                $application_step Current step in application.
956
     * @return array
957
    */
958
    public function customBreadcrumbs(JobPoster $jobPoster, string $application_step)
959
    {
960
        $step_lang = Lang::get('applicant/application_template.tracker_label');
961
        return [
962
            'home' => route('home'),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

962
            'home' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
963
            'jobs' => route('jobs.index'),
964
            $jobPoster->title => route('jobs.summary', $jobPoster),
965
            $step_lang . ' ' . $application_step => ''
966
        ];
967
    }
968
}
969