Passed
Push — dependabot/composer/dev/larave... ( d8aabe )
by
unknown
05:54
created

getApplicationFromJob()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
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\SkillStatus;
16
use App\Models\Lookup\VeteranStatus;
17
use App\Models\Skill;
18
use App\Models\SkillDeclaration;
19
use App\Models\WorkExperience;
20
use App\Services\Validation\ApplicationValidator;
21
use Facades\App\Services\WhichPortal;
22
use Illuminate\Http\Request;
23
use Illuminate\Support\Facades\Auth;
24
use Illuminate\Support\Facades\Lang;
25
use Illuminate\Support\Facades\Log;
26
27
class ApplicationByJobController extends Controller
28
{
29
    /**
30
     * Display a listing of the applications for given jobPoster.
31
     *
32
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function index(JobPoster $jobPoster)
36
    {
37
        $applications = $jobPoster->submitted_applications()
38
            ->with([
39
                'veteran_status',
40
                'citizenship_declaration',
41
                'application_review',
42
                'applicant.user',
43
                'job_poster.criteria',
44
            ])
45
            ->get();
46
        return view('manager/review_applications', [
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

46
        return /** @scrutinizer ignore-call */ view('manager/review_applications', [
Loading history...
47
            // Localization Strings.
48
            'jobs_l10n' => Lang::get('manager/job_index'),
49
            // Data.
50
            'is_hr_portal' => WhichPortal::isHrPortal(),
51
            'job' => $jobPoster->toApiArray(),
52
            'applications' => $applications,
53
            'review_statuses' => ReviewStatus::all()
54
        ]);
55
    }
56
57
    /**
58
     * Return the current applicant's application for a given Job Poster.
59
     *
60
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
61
     * @return mixed|\App\Models\JobApplication
62
     */
63
    protected function getApplicationFromJob(JobPoster $jobPoster)
64
    {
65
        $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
66
            ->where('job_poster_id', $jobPoster->id)->first();
67
        if ($application == null) {
68
            $application = new JobApplication();
69
            $application->job_poster_id = $jobPoster->id;
70
            $application->applicant_id = Auth::user()->applicant->id;
71
            $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
72
            $application->save();
73
        }
74
        return $application;
75
    }
76
77
    /**
78
     * Show the form for editing Application basics for the specified job.
79
     *
80
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function editBasics(JobPoster $jobPoster)
84
    {
85
        $applicant = Auth::user()->applicant;
86
        $application = $this->getApplicationFromJob($jobPoster);
87
88
        // Ensure user has permissions to view and update application.
89
        $this->authorize('view', $application);
90
        $this->authorize('update', $application);
91
92
        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

92
        return /** @scrutinizer ignore-call */ view(
Loading history...
93
            'applicant/application_post_01',
94
            [
95
                // Application Template Data.
96
                'application_step' => 1,
97
                'application_template' => Lang::get('applicant/application_template'),
98
                'language_options' => PreferredLanguage::all(),
99
                'citizenship_options' => CitizenshipDeclaration::all(),
100
                'veteran_options' => VeteranStatus::all(),
101
                'preferred_language_template' => Lang::get('common/preferred_language'),
102
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
103
                'veteran_status_template' => Lang::get('common/veteran_status'),
104
                // Job Data.
105
                'job' => $jobPoster,
106
                // Applicant Data.
107
                'applicant' => $applicant,
108
                'job_application' => $application,
109
                // Submission.
110
                '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

110
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.1', $jobPoster)
Loading history...
111
            ]
112
        );
113
    }
114
115
    /**
116
     * Show the form for editing Application Experience for the specified job.
117
     *
118
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function editExperience(JobPoster $jobPoster)
122
    {
123
        $applicant = Auth::user()->applicant;
124
        $application = $this->getApplicationFromJob($jobPoster);
125
126
        // Ensure user has permissions to view and update application.
127
        $this->authorize('view', $application);
128
        $this->authorize('update', $application);
129
130
        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

130
        return /** @scrutinizer ignore-call */ view(
Loading history...
131
            'applicant/application_post_02',
132
            [
133
                // Application Template Data.
134
                'application_step' => 2,
135
                'application_template' => Lang::get('applicant/application_template'),
136
                // Job Data.
137
                'job' => $jobPoster,
138
                // Applicant Data.
139
                'applicant' => $applicant,
140
                'job_application' => $application,
141
                // Submission.
142
                '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

142
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.2', $jobPoster)
Loading history...
143
            ]
144
        );
145
    }
146
147
    /**
148
     * Show the form for editing Application Essential Skills for the specified job.
149
     *
150
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
151
     * @return \Illuminate\Http\Response
152
     */
153
    public function editEssentialSkills(JobPoster $jobPoster)
154
    {
155
        $applicant = Auth::user()->applicant;
156
        $application = $this->getApplicationFromJob($jobPoster);
157
158
        // Ensure user has permissions to view and update application.
159
        $this->authorize('view', $application);
160
        $this->authorize('update', $application);
161
162
        $criteria = [
163
            'essential' => $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

163
            'essential' => $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...
164
                return $value->criteria_type->name == 'essential';
165
            }),
166
            'asset' => $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

166
            'asset' => $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...
167
                return $value->criteria_type->name == 'asset';
168
            }),
169
        ];
170
171
        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

171
        return /** @scrutinizer ignore-call */ view(
Loading history...
172
            'applicant/application_post_03',
173
            [
174
                // Application Template Data.
175
                'application_step' => 3,
176
                'application_template' => Lang::get('applicant/application_template'),
177
                // Job Data.
178
                'job' => $jobPoster,
179
                // Skills Data.
180
                'skills' => Skill::all(),
181
                'skill_template' => Lang::get('common/skills'),
182
                'criteria' => $criteria,
183
                // Applicant Data.
184
                'applicant' => $applicant,
185
                'job_application' => $application,
186
                // Submission.
187
                '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

187
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.3', $jobPoster)
Loading history...
188
            ]
189
        );
190
    }
191
192
    /**
193
     * Show the form for editing Application Asset Skills for the specified job.
194
     *
195
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
196
     * @return \Illuminate\Http\Response
197
     */
198
    public function editAssetSkills(JobPoster $jobPoster)
199
    {
200
        $applicant = Auth::user()->applicant;
201
        $application = $this->getApplicationFromJob($jobPoster);
202
203
        // Ensure user has permissions to view and update application.
204
        $this->authorize('view', $application);
205
        $this->authorize('update', $application);
206
207
        $criteria = [
208
            'essential' => $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

208
            'essential' => $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...
209
                return $value->criteria_type->name == 'essential';
210
            }),
211
            'asset' => $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

211
            'asset' => $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...
212
                return $value->criteria_type->name == 'asset';
213
            }),
214
        ];
215
216
        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

216
        return /** @scrutinizer ignore-call */ view(
Loading history...
217
            'applicant/application_post_04',
218
            [
219
                // Application Template Data.
220
                'application_step' => 4,
221
                'application_template' => Lang::get('applicant/application_template'),
222
                // Job Data.
223
                'job' => $jobPoster,
224
                // Skills Data.
225
                'skills' => Skill::all(),
226
                'skill_template' => Lang::get('common/skills'),
227
                'criteria' => $criteria,
228
                // Applicant Data.
229
                'applicant' => $applicant,
230
                'job_application' => $application,
231
                // Submission.
232
                '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

232
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.4', $jobPoster)
Loading history...
233
            ]
234
        );
235
    }
236
237
    /**
238
     * Show the Application Preview for the application for the specified job.
239
     *
240
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
241
     * @return \Illuminate\Http\Response
242
     */
243
    public function preview(JobPoster $jobPoster)
244
    {
245
        $applicant = Auth::user()->applicant;
246
        $application = $this->getApplicationFromJob($jobPoster);
247
248
        $this->authorize('view', $application);
249
        $criteria = [
250
            'essential' => $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

250
            'essential' => $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...
251
                return $value->criteria_type->name == 'essential';
252
            }),
253
            'asset' => $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

253
            'asset' => $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...
254
                return $value->criteria_type->name == 'asset';
255
            }),
256
        ];
257
        $skillDeclarations = $application->isDraft()
258
            ? $applicant->skill_declarations
259
            : $application->skill_declarations;
260
        $degrees = $application->isDraft()
261
            ? $applicant->degrees
262
            : $application->degrees;
263
        $courses = $application->isDraft()
264
            ? $applicant->courses
265
            : $application->courses;
266
        $work_experiences = $application->isDraft()
267
            ? $applicant->work_experiences
268
            : $application->work_experiences;
269
270
        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

270
        return /** @scrutinizer ignore-call */ view(
Loading history...
271
            'applicant/application_post_05',
272
            [
273
                // Application Template Data.
274
                'application_step' => 5,
275
                'application_template' => Lang::get('applicant/application_template'),
276
                'preferred_language_template' => Lang::get('common/preferred_language'),
277
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
278
                'veteran_status_template' => Lang::get('common/veteran_status'),
279
                // Job Data.
280
                'job' => $jobPoster,
281
                // Skills Data.
282
                'skills' => Skill::all(),
283
                'skill_template' => Lang::get('common/skills'),
284
                'criteria' => $criteria,
285
                // Applicant Data.
286
                'applicant' => $applicant,
287
                'job_application' => $application,
288
                'skill_declarations' => $skillDeclarations,
289
                'degrees' => $degrees,
290
                'courses' => $courses,
291
                'work_experiences' => $work_experiences,
292
                'is_manager_view' => WhichPortal::isManagerPortal(),
293
            ]
294
        );
295
    }
296
297
    /**
298
     * Show the Confirm Submit page for the application for the specified job.
299
     *
300
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
301
     * @return \Illuminate\Http\Response
302
     */
303
    public function confirm(JobPoster $jobPoster)
304
    {
305
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
306
        $application = $this->getApplicationFromJob($jobPoster);
307
308
        $this->authorize('update', $application);
309
310
        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

310
        return /** @scrutinizer ignore-call */ view(
Loading history...
311
            'applicant/application_post_06',
312
            [
313
                // Application Template Data.
314
                'application_step' => 6,
315
                'application_template' => Lang::get('applicant/application_template'),
316
                // Used by tracker partial.
317
                'job' => $jobPoster,
318
                'job_application' => $application,
319
                // Submission.
320
                '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

320
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.submit', $jobPoster)
Loading history...
321
            ]
322
        );
323
    }
324
325
    /**
326
     * Show the application submission information.
327
     *
328
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
329
     * @return \Illuminate\Http\Response
330
     */
331
    public function complete(JobPoster $jobPoster)
332
    {
333
        // Include Applicant Data.
334
        $applicant = Auth::user()->applicant;
335
        // Include Application Data.
336
        $application = $this->getApplicationFromJob($jobPoster);
337
338
        // Ensure user has permissions to view application.
339
        $this->authorize('view', $application);
340
341
        // Return the Completion View.
342
        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

342
        return /** @scrutinizer ignore-call */ view(
Loading history...
343
            'applicant/application_post_complete',
344
            [
345
                // Application Template Data.
346
                'application_template' => Lang::get('applicant/application_template'),
347
                // Job Data.
348
                'job' => $jobPoster,
349
                // Applicant Data.
350
                'applicant' => $applicant,
351
                'job_application' => $application
352
            ]
353
        );
354
    }
355
356
    /**
357
     * Update the Application Basics in storage for the specified job.
358
     *
359
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
360
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
361
     * @return \Illuminate\Http\Response
362
     */
363
    public function updateBasics(Request $request, JobPoster $jobPoster)
364
    {
365
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
366
        $application = $this->getApplicationFromJob($jobPoster);
367
368
        // Ensure user has permissions to update this application.
369
        $this->authorize('update', $application);
370
371
        $application->fill([
372
            'citizenship_declaration_id' => $request->input('citizenship_declaration_id'),
373
            'veteran_status_id' => $request->input('veteran_status_id'),
374
            'preferred_language_id' => $request->input('preferred_language_id'),
375
            'language_requirement_confirmed' => $request->input('language_requirement_confirmed')
376
        ]);
377
        $application->save();
378
379
        $questions = $jobPoster->job_poster_questions;
380
        $questionsInput = $request->input('questions');
381
        foreach ($questions as $question) {
382
            $answer = null;
383
            if (isset($questionsInput[$question->id])) {
384
                $answer = $questionsInput[$question->id];
385
            }
386
            $answerObj = $application->job_application_answers
387
            ->firstWhere('job_poster_question_id', $question->id);
388
            if ($answerObj == null) {
389
                $answerObj = new JobApplicationAnswer();
390
                $answerObj->job_poster_question_id = $question->id;
391
                $answerObj->job_application_id = $application->id;
392
            }
393
            $answerObj->answer = $answer;
394
            $answerObj->save();
395
        }
396
397
        // Redirect to correct page.
398
        switch ($request->input('submit')) {
399
            case 'save_and_quit':
400
            case 'previous':
401
                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

401
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
402
                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...
403
            case 'save_and_continue':
404
            case 'next':
405
                return redirect()->route('job.application.edit.2', $jobPoster);
406
                break;
407
            default:
408
                return redirect()->back()->withInput();
409
        }
410
    }
411
412
    /**
413
     * Update the Application Basics in storage for the specified job.
414
     *
415
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
416
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
417
     * @return \Illuminate\Http\Response
418
     */
419
    public function updateExperience(Request $request, JobPoster $jobPoster)
420
    {
421
        $applicant = Auth::user()->applicant;
422
        $application = $this->getApplicationFromJob($jobPoster);
423
424
        // Ensure user has permissions to update this application.
425
        $this->authorize('update', $application);
426
427
        // Record that the user has saved their experience for this application.
428
        $application->experience_saved = true;
429
        $application->save();
430
431
        $degrees = $request->input('degrees');
432
433
        $request->validate([
434
            'degrees.new.*.degree_type_id' => 'required',
435
            'degrees.new.*.area_of_study'  => 'required',
436
            'degrees.new.*.institution'    => 'required',
437
            'degrees.new.*.thesis'         => 'nullable',
438
            'degrees.new.*.start_date'     => 'required|date',
439
            'degrees.new.*.end_date'       => 'required|date',
440
            'degrees.new.*.blockcert_url'  => 'nullable|string',
441
        ]);
442
443
        // Save new degrees.
444
        if (isset($degrees['new'])) {
445
            foreach ($degrees['new'] as $degreeInput) {
446
                $degree = new Degree();
447
                $degree->fill([
448
                    'degree_type_id' => $degreeInput['degree_type_id'],
449
                    'area_of_study' => $degreeInput['area_of_study'],
450
                    'institution' => $degreeInput['institution'],
451
                    'thesis' => $degreeInput['thesis'],
452
                    'start_date' => $degreeInput['start_date'],
453
                    'end_date' => $degreeInput['end_date'],
454
                    'blockcert_url' => $degreeInput['blockcert_url'],
455
                ]);
456
                $applicant->degrees()->save($degree);
457
            }
458
        }
459
460
        // Update old degrees.
461
        if (isset($degrees['old'])) {
462
            foreach ($degrees['old'] as $id => $degreeInput) {
463
                // Ensure this degree belongs to this applicant.
464
                $degree = $applicant->degrees->firstWhere('id', $id);
465
                if ($degree != null) {
466
                    $degree->fill([
467
                        'degree_type_id' => $degreeInput['degree_type_id'],
468
                        'area_of_study' => $degreeInput['area_of_study'],
469
                        'institution' => $degreeInput['institution'],
470
                        'thesis' => $degreeInput['thesis'],
471
                        'start_date' => $degreeInput['start_date'],
472
                        'end_date' => $degreeInput['end_date'],
473
                        'blockcert_url' => $degreeInput['blockcert_url'],
474
                    ]);
475
                    $degree->save();
476
                } else {
477
                    Log::warning("Applicant $applicant->id attempted to update degree with invalid id: $id");
478
                }
479
            }
480
        }
481
482
        $courses = $request->input('courses');
483
484
        $request->validate([
485
            'courses.new.*.name'             => 'required',
486
            'courses.new.*.institution'      => 'required',
487
            'courses.new.*.course_status_id' => 'required',
488
            'courses.new.*.start_date'       => 'required|date',
489
            'courses.new.*.end_date'         => 'required|date',
490
        ]);
491
492
        // Save new courses.
493
        if (isset($courses['new'])) {
494
            foreach ($courses['new'] as $courseInput) {
495
                $course = new Course();
496
                $course->fill([
497
                    'name' => $courseInput['name'],
498
                    'institution' => $courseInput['institution'],
499
                    'course_status_id' => $courseInput['course_status_id'],
500
                    'start_date' => $courseInput['start_date'],
501
                    'end_date' => $courseInput['end_date']
502
                ]);
503
                $applicant->courses()->save($course);
504
            }
505
        }
506
507
        // Update old courses.
508
        if (isset($courses['old'])) {
509
            foreach ($courses['old'] as $id => $courseInput) {
510
                // Ensure this course belongs to this applicant.
511
                $course = $applicant->courses->firstWhere('id', $id);
512
                if ($course != null) {
513
                    $course->fill([
514
                        'name' => $courseInput['name'],
515
                        'institution' => $courseInput['institution'],
516
                        'course_status_id' => $courseInput['course_status_id'],
517
                        'start_date' => $courseInput['start_date'],
518
                        'end_date' => $courseInput['end_date']
519
                    ]);
520
                    $course->save();
521
                } else {
522
                    Log::warning("Applicant $applicant->id attempted to update course with invalid id: $id");
523
                }
524
            }
525
        }
526
527
        $work_experiences = $request->input('work_experiences');
528
529
        $request->validate([
530
            'work_experiences.new.*.role'        => 'required',
531
            'work_experiences.new.*.company'     => 'required',
532
            'work_experiences.new.*.description' => 'required',
533
            'work_experiences.new.*.start_date'  => 'required|date',
534
            'work_experiences.new.*.end_date'    => 'required|date',
535
        ]);
536
537
        // Save new work_experiences.
538
        if (isset($work_experiences['new'])) {
539
            foreach ($work_experiences['new'] as $workExperienceInput) {
540
                $workExperience = new WorkExperience();
541
                $workExperience->fill([
542
                    'role' => $workExperienceInput['role'],
543
                    'company' => $workExperienceInput['company'],
544
                    'description' => $workExperienceInput['description'],
545
                    'start_date' => $workExperienceInput['start_date'],
546
                    'end_date' => $workExperienceInput['end_date']
547
                ]);
548
                $applicant->work_experiences()->save($workExperience);
549
            }
550
        }
551
552
        // Update old work_experiences.
553
        if (isset($work_experiences['old'])) {
554
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
555
                // Ensure this work_experience belongs to this applicant.
556
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
557
                if ($workExperience != null) {
558
                    $workExperience->fill([
559
                        'role' => $workExperienceInput['role'],
560
                        'company' => $workExperienceInput['company'],
561
                        'description' => $workExperienceInput['description'],
562
                        'start_date' => $workExperienceInput['start_date'],
563
                        'end_date' => $workExperienceInput['end_date']
564
                    ]);
565
                    $workExperience->save();
566
                } else {
567
                    Log::warning("Applicant $applicant->id attempted to update work_experience with invalid id: $id");
568
                }
569
            }
570
        }
571
572
        // Redirect to correct page.
573
        switch ($request->input('submit')) {
574
            case 'save_and_quit':
575
                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

575
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
576
                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...
577
            case 'save_and_continue':
578
            case 'next':
579
                return redirect()->route('job.application.edit.3', $jobPoster);
580
                break;
581
            case 'previous':
582
                return redirect()->route('job.application.edit.1', $jobPoster);
583
                break;
584
            default:
585
                return redirect()->back()->withInput();
586
        }
587
    }
588
589
    /**
590
     * Update the Application Essential Skills in storage for the specified job.
591
     *
592
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
593
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
594
     * @return \Illuminate\Http\Response
595
     */
596
    public function updateEssentialSkills(Request $request, JobPoster $jobPoster)
597
    {
598
        $applicant = Auth::user()->applicant;
599
        $application = $this->getApplicationFromJob($jobPoster);
600
601
        // Ensure user has permissions to update this application.
602
        $this->authorize('update', $application);
603
604
        $skillDeclarations = $request->input('skill_declarations');
605
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
606
607
        // Save new skill declarations.
608
        if (isset($skillDeclarations['new'])) {
609
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
610
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
611
                    $skillDeclaration = new SkillDeclaration();
612
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
613
                    $skillDeclaration->skill_status_id = $claimedStatusId;
614
                    $skillDeclaration->fill([
615
                        'description' => $skillDeclarationInput['description'],
616
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
617
                    ]);
618
                    $applicant->skill_declarations()->save($skillDeclaration);
619
620
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
621
                    $skillDeclaration->references()->sync($referenceIds);
622
623
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
624
                    $skillDeclaration->work_samples()->sync($sampleIds);
625
                }
626
            }
627
        }
628
629
        // Update old declarations.
630
        if (isset($skillDeclarations['old'])) {
631
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
632
                foreach ($typeInput as $id => $skillDeclarationInput) {
633
                    // Ensure this declaration belongs to this applicant.
634
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
635
                    if ($skillDeclaration != null) {
636
                        // skill_id and skill_status cannot be changed.
637
                        $skillDeclaration->fill([
638
                            'description' => $skillDeclarationInput['description'],
639
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
640
                        ]);
641
                        $skillDeclaration->save();
642
643
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
644
                        $skillDeclaration->references()->sync($referenceIds);
645
646
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
647
                        $skillDeclaration->work_samples()->sync($sampleIds);
648
                    } else {
649
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
650
                    }
651
                }
652
            }
653
        }
654
655
        // Redirect to correct page.
656
        switch ($request->input('submit')) {
657
            case 'save_and_quit':
658
                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

658
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
659
                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...
660
            case 'save_and_continue':
661
            case 'next':
662
                return redirect()->route('job.application.edit.4', $jobPoster);
663
                break;
664
            case 'previous':
665
                return redirect()->route('job.application.edit.2', $jobPoster);
666
                break;
667
            default:
668
                return redirect()->back()->withInput();
669
        }
670
    }
671
672
    /**
673
     * Update the Application Asset Skills in storage for the specified job.
674
     *
675
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
676
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
677
     * @return \Illuminate\Http\Response
678
     */
679
    public function updateAssetSkills(Request $request, JobPoster $jobPoster)
680
    {
681
        $applicant = Auth::user()->applicant;
682
        $application = $this->getApplicationFromJob($jobPoster);
683
684
        // Ensure user has permissions to update this application.
685
        $this->authorize('update', $application);
686
687
        $skillDeclarations = $request->input('skill_declarations');
688
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
689
690
        // Save new skill declarations.
691
        if (isset($skillDeclarations['new'])) {
692
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
693
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
694
                    $skillDeclaration = new SkillDeclaration();
695
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
696
                    $skillDeclaration->skill_status_id = $claimedStatusId;
697
                    $skillDeclaration->fill([
698
                        'description' => $skillDeclarationInput['description'],
699
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
700
                    ]);
701
                    $applicant->skill_declarations()->save($skillDeclaration);
702
703
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
704
                    $skillDeclaration->references()->sync($referenceIds);
705
706
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
707
                    $skillDeclaration->work_samples()->sync($sampleIds);
708
                }
709
            }
710
        }
711
712
        // Update old declarations.
713
        if (isset($skillDeclarations['old'])) {
714
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
715
                foreach ($typeInput as $id => $skillDeclarationInput) {
716
                    // Ensure this declaration belongs to this applicant.
717
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
718
                    if ($skillDeclaration != null) {
719
                        // skill_id and skill_status cannot be changed.
720
                        $skillDeclaration->fill([
721
                            'description' => $skillDeclarationInput['description'],
722
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
723
                        ]);
724
                        $skillDeclaration->save();
725
726
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
727
                        $skillDeclaration->references()->sync($referenceIds);
728
729
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
730
                        $skillDeclaration->work_samples()->sync($sampleIds);
731
                    } else {
732
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
733
                    }
734
                }
735
            }
736
        }
737
738
        // Redirect to correct page.
739
        switch ($request->input('submit')) {
740
            case 'save_and_quit':
741
                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

741
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
742
                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...
743
            case 'save_and_continue':
744
            case 'next':
745
                return redirect()->route('job.application.edit.5', $jobPoster);
746
                break;
747
            case 'previous':
748
                return redirect()->route('job.application.edit.3', $jobPoster);
749
                break;
750
            default:
751
                return redirect()->back()->withInput();
752
        }
753
    }
754
755
    /**
756
     * Submit the Application for the specified job.
757
     *
758
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
759
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
760
     * @return \Illuminate\Http\Response
761
     */
762
    public function submit(Request $request, JobPoster $jobPoster)
763
    {
764
        $application = $this->getApplicationFromJob($jobPoster);
765
766
        // Only complete submission if submit button was pressed.
767
        if ($request->input('submit') == 'submit' && $application->application_status->name == 'draft') {
768
            // Ensure user has permissions to update this application.
769
            $this->authorize('update', $application);
770
            $request->validate([
771
                'submission_signature' => [
772
                    'required',
773
                    'string',
774
                    'max:191',
775
                ],
776
                'submission_date' => [
777
                    'required',
778
                    'string',
779
                    'max:191',
780
                ]
781
            ]);
782
783
            // Save any final info.
784
            $application->fill([
785
                'submission_signature' => $request->input('submission_signature'),
786
                'submission_date' => $request->input('submission_date'),
787
            ]);
788
789
            // Error out of this process now if application is not complete.
790
            $validator = new ApplicationValidator();
791
            $validator->validate($application);
792
793
            // Change status to 'submitted'.
794
            $application->application_status_id = ApplicationStatus::where('name', 'submitted')->firstOrFail()->id;
795
            $application->saveProfileSnapshot();
796
        }
797
798
        $application->save();
799
800
        // Redirect to correct page.
801
        switch ($request->input('submit')) {
802
            case 'save_and_quit':
803
                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

803
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
804
                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...
805
            case 'submit':
806
                return redirect()->route('job.application.complete', $jobPoster);
807
                break;
808
            case 'previous':
809
                return redirect()->route('job.application.edit.4', $jobPoster);
810
                break;
811
            default:
812
                return redirect()->back()->withInput();
813
        }
814
    }
815
}
816