Passed
Push — feature/strategic-default-ques... ( c656ee...07f342 )
by Tristan
04:21
created

JobController::populateDefaultQuestions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 28
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Models\JobApplication;
10
use Carbon\Carbon;
11
use App\Models\JobPoster;
12
use App\Models\JobPosterQuestion;
13
use App\Models\Lookup\ApplicationStatus;
14
use App\Models\Lookup\CitizenshipDeclaration;
15
use App\Models\Lookup\JobPosterStatus;
16
use App\Models\Lookup\VeteranStatus;
17
use App\Models\Manager;
18
use App\Services\JobPosterDefaultQuestions;
19
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
20
use App\Services\Validation\JobPosterValidator;
21
use Facades\App\Services\WhichPortal;
22
use Illuminate\Support\Facades\Response;
23
use Illuminate\Support\Facades\Validator;
24
25
class JobController extends Controller
26
{
27
    /**
28
     * Display a listing of JobPosters.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function index()
33
    {
34
        $now = Carbon::now();
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
35
36
        // Find published jobs that are currently open for applications.
37
        // Eager load required relationships: Department, Province, JobTerm.
38
        // Eager load the count of submitted applications, to prevent the relationship
39
        // from being actually loaded and firing off events.
40
        $jobs = JobPoster::where('internal_only', false)
41
            ->where('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

41
            ->where('department_id', '!=', /** @scrutinizer ignore-call */ config('app.strategic_response_department_id'))
Loading history...
42
            ->where('job_poster_status_id', JobPosterStatus::where('key', 'live')->first()->id)
43
            ->with([
44
                'department',
45
                'province',
46
                'job_term',
47
            ])
48
            ->withCount([
49
                'submitted_applications',
50
            ])
51
            ->get();
52
        return view('applicant/job_index', [
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

52
        return /** @scrutinizer ignore-call */ view('applicant/job_index', [
Loading history...
53
            'job_index' => Lang::get('applicant/job_index'),
54
            'jobs' => $jobs
55
        ]);
56
    }
57
58
    /**
59
     * Display a listing of a manager's JobPosters.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function managerIndex()
64
    {
65
        $manager = Auth::user()->manager;
66
67
        $jobs = JobPoster::where('manager_id', $manager->id)
68
            ->with('classification')
69
            ->withCount('submitted_applications')
70
            ->get();
71
72
        foreach ($jobs as &$job) {
73
            $chosen_lang = $job->chosen_lang;
74
75
            // Show chosen lang title if current title is empty.
76
            if (empty($job->title)) {
77
                $job->title = $job->getTranslation('title', $chosen_lang);
78
                $job->trans_required = true;
79
            }
80
81
            // Always preview and edit in the chosen language.
82
            $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.show', $job));
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

82
            $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, /** @scrutinizer ignore-call */ route('manager.jobs.show', $job));
Loading history...
83
            $job->edit_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.edit', $job));
84
        }
85
86
87
        return view('manager/job_index', [
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

87
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
88
            // Localization Strings.
89
            'jobs_l10n' => Lang::get('manager/job_index'),
90
            // Data.
91
            'jobs' => $jobs,
92
        ]);
93
    }
94
95
    /**
96
     * Display a listing of a hr advisor's JobPosters.
97
     *
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function hrIndex(Request $request)
101
    {
102
        $hrAdvisor = $request->user()->hr_advisor;
103
        return view('hr_advisor/job_index', [
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

103
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
104
            'title' => Lang::get('hr_advisor/job_index.title'),
105
            'hr_advisor_id' => $hrAdvisor->id
106
        ]);
107
    }
108
109
110
111
112
    /**
113
     * Delete a draft Job Poster.
114
     *
115
     * @param  \Illuminate\Http\Request $request   Incoming request object.
116
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function destroy(Request $request, JobPoster $jobPoster)
120
    {
121
        $jobPoster->delete();
122
    }
123
124
    /**
125
     * Display the specified job poster.
126
     *
127
     * @param  \Illuminate\Http\Request $request   Incoming request object.
128
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
129
     * @return \Illuminate\Http\Response
130
     */
131
    public function show(Request $request, JobPoster $jobPoster)
132
    {
133
        $jobPoster->load([
134
            'department',
135
            'criteria.skill.skill_type',
136
            'manager.team_culture',
137
            'manager.work_environment'
138
        ]);
139
140
        $user = Auth::user();
141
142
        // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model.
143
        $workplacePhotos = [];
144
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
145
            $workplacePhotos[] = [
146
                'description' => $photoCaption->description,
147
                'url' => '/images/user.png'
148
            ];
149
        }
150
151
        // TODO: replace route('manager.show',manager.id) in templates with link using slug.
152
        $criteria = [
153
            'essential' => $jobPoster->criteria->filter(
154
                function ($value, $key) {
155
                    return $value->criteria_type->name == 'essential';
156
                }
157
            ),
158
            'asset' => $jobPoster->criteria->filter(
159
                function ($value, $key) {
160
                    return $value->criteria_type->name == 'asset';
161
                }
162
            ),
163
        ];
164
165
        $jobLang = Lang::get('applicant/job_post');
166
167
        $applyButton = [];
168
        if (WhichPortal::isManagerPortal()) {
169
            $applyButton = [
170
                'href' => route('manager.jobs.edit', $jobPoster->id),
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

170
                'href' => /** @scrutinizer ignore-call */ route('manager.jobs.edit', $jobPoster->id),
Loading history...
171
                'title' => $jobLang['apply']['edit_link_title'],
172
                'text' => $jobLang['apply']['edit_link_label'],
173
            ];
174
        } elseif (WhichPortal::isHrPortal()) {
175
            if ($jobPoster->hr_advisors->contains('user_id', $user->id)) {
176
                $applyButton = [
177
                    'href' => route('hr_advisor.jobs.summary', $jobPoster->id),
178
                    'title' => null,
179
                    'text' => Lang::get('hr_advisor/job_summary.summary_title'),
180
                ];
181
            } else {
182
                $applyButton = [
183
                    'href' => route('hr_advisor.jobs.index'),
184
                    'title' => null,
185
                    'text' => Lang::get('hr_advisor/job_index.title'),
186
                ];
187
            }
188
        } elseif (Auth::check() && $jobPoster->isOpen()) {
189
            $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
190
                ->where('job_poster_id', $jobPoster->id)->first();
191
            // If applicants job application is not draft anymore then link to application preview page.
192
            if ($application != null && $application->application_status->name != 'draft') {
193
                $applyButton = [
194
                    'href' => route('applications.show', $application->id),
195
                    'title' => $jobLang['apply']['view_link_title'],
196
                    'text' => $jobLang['apply']['view_link_label'],
197
                ];
198
            } else {
199
                $applyButton = [
200
                    'href' => route('job.application.edit.1', $jobPoster->id),
201
                    'title' => $jobLang['apply']['apply_link_title'],
202
                    'text' => $jobLang['apply']['apply_link_label'],
203
                ];
204
            }
205
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
206
            $applyButton = [
207
                'href' => route('job.application.edit.1', $jobPoster->id),
208
                'title' => $jobLang['apply']['login_link_title'],
209
                'text' => $jobLang['apply']['login_link_label'],
210
            ];
211
        } else {
212
            $applyButton = [
213
                'href' => null,
214
                'title' => null,
215
                'text' => $jobLang['apply']['job_closed_label'],
216
            ];
217
        }
218
219
        $jpb_release_date = strtotime('2019-08-21 16:18:17');
220
        $job_created_at = strtotime($jobPoster->created_at);
221
222
        // If the job poster is created after the release of the JPB.
223
        // Then, render with updated poster template.
224
        // Else, render with old poster template.
225
        if ($job_created_at > $jpb_release_date) {
226
            // Updated job poster (JPB).
227
            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

227
            return /** @scrutinizer ignore-call */ view(
Loading history...
228
                'applicant/jpb_job_post',
229
                [
230
                    'job_post' => $jobLang,
231
                    'frequencies' => Lang::get('common/lookup/frequency'),
232
                    'skill_template' => Lang::get('common/skills'),
233
                    'job' => $jobPoster,
234
                    'manager' => $jobPoster->manager,
235
                    'criteria' => $criteria,
236
                    'apply_button' => $applyButton,
237
                ]
238
            );
239
        } else {
240
            // Old job poster.
241
            return view(
242
                'applicant/job_post',
243
                [
244
                    'job_post' => $jobLang,
245
                    'frequencies' => Lang::get('common/lookup/frequency'),
246
                    'manager' => $jobPoster->manager,
247
                    'manager_profile_photo_url' => '/images/user.png', // TODO get real photo.
248
                    'team_culture' => $jobPoster->manager->team_culture,
249
                    'work_environment' => $jobPoster->manager->work_environment,
250
                    'workplace_photos' => $workplacePhotos,
251
                    'job' => $jobPoster,
252
                    'criteria' => $criteria,
253
                    'apply_button' => $applyButton,
254
                    'skill_template' => Lang::get('common/skills'),
255
                ]
256
            );
257
        }
258
    }
259
260
    /**
261
     * Display the form for editing an existing Job Poster
262
     * Only allows editing fields that don't appear on the react-built Job Poster Builder.
263
     *
264
     * @param  \Illuminate\Http\Request $request   Incoming request object.
265
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
266
     * @return \Illuminate\Http\Response
267
     */
268
    public function edit(Request $request, JobPoster $jobPoster)
269
    {
270
        $manager = $jobPoster->manager;
271
272
        $defaultQuestionManager = new JobPosterDefaultQuestions();
273
        $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster);
274
275
        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

275
        return /** @scrutinizer ignore-call */ view(
Loading history...
276
            'manager/job_create',
277
            [
278
                // Localization Strings.
279
                'job_l10n' => Lang::get('manager/job_edit'),
280
                // Data.
281
                'manager' => $manager,
282
                'job' => $jobPoster,
283
                'form_action_url' => route('admin.jobs.update', $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

283
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
284
            ]
285
        );
286
    }
287
288
    /**
289
     * Create a blank job poster for the specified manager
290
     *
291
     * @param  \App\Models\Manager $manager Incoming Manager object.
292
     * @return \Illuminate\Http\Response Job Create view
293
     */
294
    public function createAsManager(Manager $manager)
295
    {
296
        $jobPoster = new JobPoster();
297
        $jobPoster->manager_id = $manager->id;
298
299
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB
300
        $divisionEn = $manager->getTranslation('division', 'en');
301
        $divisionFr = $manager->getTranslation('division', 'fr');
302
        $jobPoster->fill([
303
            'department_id' => $manager->user->department_id,
304
            'division' => ['en' => $divisionEn],
305
            'division' => ['fr' => $divisionFr],
306
        ]);
307
308
        $jobPoster->save();
309
310
        return redirect()->route('manager.jobs.edit', $jobPoster->id);
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

310
        return /** @scrutinizer ignore-call */ redirect()->route('manager.jobs.edit', $jobPoster->id);
Loading history...
311
    }
312
313
    /**
314
     * Update a resource in storage
315
     * NOTE: Only saves fields that are not on the react-built Job Poster Builder
316
     *
317
     * @param  \Illuminate\Http\Request $request   Incoming request object.
318
     * @param  \App\Models\JobPoster    $jobPoster Optional Job Poster object.
319
     * @return \Illuminate\Http\Response
320
     */
321
    public function store(Request $request, JobPoster $jobPoster)
322
    {
323
        // Don't allow edits for published Job Posters
324
        // Also check auth while we're at it.
325
        $this->authorize('update', $jobPoster);
326
        JobPosterValidator::validateUnpublished($jobPoster);
327
328
        $input = $request->input();
329
330
        if ($jobPoster->manager_id == null) {
331
            $jobPoster->manager_id = $request->user()->manager->id;
332
            $jobPoster->save();
333
        }
334
335
        if ($request->input('question')) {
336
            $validator = Validator::make($request->input('question'), [
337
                '*.question.*' => 'required|string',
338
            ], [
339
                'required' => Lang::get('validation.custom.job_poster_question.required'),
340
                'string' => Lang::get('validation.custom.job_poster_question.string')
341
            ]);
342
343
            if ($validator->fails()) {
344
                $request->session()->flash('errors', $validator->errors());
345
                return redirect(route('admin.jobs.edit', $jobPoster->id));
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

345
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
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

345
                return /** @scrutinizer ignore-call */ redirect(route('admin.jobs.edit', $jobPoster->id));
Loading history...
346
            }
347
        }
348
349
        $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true);
350
351
        return redirect(route('manager.jobs.show', $jobPoster->id));
352
    }
353
354
    /**
355
     * Fill Job Poster's questions and save
356
     *
357
     * @param  mixed[]               $input     Field values.
358
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
359
     * @param  boolean               $replace   Remove existing relationships.
360
     * @return void
361
     */
362
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
363
    {
364
        if ($replace) {
365
            $jobPoster->job_poster_questions()->delete();
366
        }
367
368
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
369
            return;
370
        }
371
372
        foreach ($input['question'] as $question) {
373
            $jobQuestion = new JobPosterQuestion();
374
            $jobQuestion->job_poster_id = $jobPoster->id;
375
            $jobQuestion->fill(
376
                [
377
                    'question' => [
378
                        'en' => $question['question']['en'],
379
                        'fr' => $question['question']['fr']
380
381
                    ],
382
                    'description' => [
383
                        'en' => $question['description']['en'],
384
                        'fr' => $question['description']['fr']
385
                    ]
386
                ]
387
            );
388
            $jobPoster->save();
389
            $jobQuestion->save();
390
        }
391
    }
392
393
    /**
394
     * Downloads a CSV file with the applicants who have applied to the job poster.
395
     *
396
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
397
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
398
     */
399
    protected function downloadApplicants(JobPoster $jobPoster)
400
    {
401
        $tables = [];
402
        // The first row in the array represents the names of the columns in the spreadsheet.
403
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
404
405
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
406
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
407
            ->where('application_status_id', $application_status_id)
408
            ->get();
409
410
        $index = 1;
411
        foreach ($applications as $application) {
412
            $status = '';
413
            $username = $application->user_name;
414
            $user_email = $application->user_email;
415
            $language = strtoupper($application->preferred_language->name);
416
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
417
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
418
            if ($application->veteran_status_id != $non_veteran) {
419
                $status = 'Veteran';
420
            } else {
421
                // Check if the applicant is a canadian citizen.
422
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
423
                if ($application->citizenship_declaration->id == $canadian_citizen) {
424
                    $status = 'Citizen';
425
                } else {
426
                    $status = 'Non-citizen';
427
                }
428
            }
429
            $tables[$index] = [$status, $username, $user_email, $language];
430
            $index++;
431
        }
432
433
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
434
435
        // Open file.
436
        $file = fopen($filename, 'w');
437
        // Iterate through tables and add each line to csv file.
438
        foreach ($tables as $line) {
439
            fputcsv($file, $line);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

439
            fputcsv(/** @scrutinizer ignore-type */ $file, $line);
Loading history...
440
        }
441
        // Close open file.
442
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

442
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
443
444
        $headers = [
445
            'Content-Type' => 'text/csv',
446
            'Content-Disposition' => 'attachment; filename=' . $filename,
447
        ];
448
449
        return Response::download($filename, $filename, $headers);
450
    }
451
}
452