Passed
Push — task/add-applicants-and-applic... ( 514472...36c932 )
by Yonathan
10:54 queued 12s
created

ApplicationByJobController::index()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

939
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
940
                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...
941
            case 'submit':
942
                return redirect()->route('job.application.complete', $jobPoster);
943
                break;
944
            case 'save_and_return':
945
                return redirect()->route('job.application.edit.4', $jobPoster);
946
                break;
947
            default:
948
                return redirect()->back()->withInput();
949
        }
950
    }
951
952
    /**
953
     * Custom breadcrumbs for application process.
954
     *
955
     * @param  \App\Models\JobPoster $jobPoster        Incoming Job Poster object.
956
     * @param  string                $application_step Current step in application.
957
     * @return array
958
    */
959
    public function customBreadcrumbs(JobPoster $jobPoster, string $application_step)
960
    {
961
        $step_lang = Lang::get('applicant/application_template.tracker_label');
962
        return [
963
            '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

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