Passed
Push — task/screening-plan-hr ( 9b78ba )
by Yonathan
04:52
created

AssessmentPlanController::show()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Gate;
6
use App\Models\JobPoster;
7
use App\Models\Criteria;
8
use App\Models\Assessment;
9
use App\Models\RatingGuideQuestion;
10
use App\Models\RatingGuideAnswer;
11
use App\Models\Lookup\AssessmentType;
12
use Illuminate\Support\Facades\Auth;
13
14
class AssessmentPlanController extends Controller
15
{
16
    /**
17
     * Return all the criteria, Assessments, Assessments,
18
     * RatingGuideQuestions, and RatingGuideAnswers associated with a job,
19
     * as an array.
20
     *
21
     * @param  \App\Models\JobPoster $jobPoster Job Poster to retrieve assessment plan for.
22
     * @return mixed[]
23
     */
24
    public function getForJob(JobPoster $jobPoster)
25
    {
26
        if (Gate::denies('view-assessment-plan', $jobPoster)) {
27
            abort(403);
0 ignored issues
show
Bug introduced by
The function abort 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

27
            /** @scrutinizer ignore-call */ 
28
            abort(403);
Loading history...
28
        }
29
30
        $criteria = Criteria::where('job_poster_id', $jobPoster->id)->get();
31
        $criteriaTranslated = [];
32
        foreach ($criteria as $criterion) {
33
            // TODO: getTranslationsArray probably makes DB calls every loop. Find a way to profile & optimize.
34
            $criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray());
35
        }
36
        $criteriaIds = $criteria->pluck('id');
37
        $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
38
        // Check for newly created assessment plan, and initialize any empty criteria to have the
39
        // "Narrative Review" option set.
40
        $assessmentCriteriaIds = $assessments->pluck('criterion_id');
41
        $emptyAssessments = array_diff($criteriaIds->toArray(), $assessmentCriteriaIds->toArray());
42
        if (!empty($emptyAssessments)) {
43
            $narrativeReview = AssessmentType::where('key', 'narrative_assessment')->first();
44
            foreach ($emptyAssessments as $criterionId) {
45
                Assessment::create([
46
                    'criterion_id' => $criterionId,
47
                    'assessment_type_id' => $narrativeReview->id
48
                ]);
49
            }
50
            $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
51
        }
52
        $questions = RatingGuideQuestion::where('job_poster_id', $jobPoster->id)->get();
53
        $answers = RatingGuideAnswer::whereIn('rating_guide_question_id', $questions->pluck('id'))->get();
54
        return [
55
            'criteria' => $criteriaTranslated,
56
            'assessments' => $assessments->toArray(),
57
            'rating_guide_questions' => $questions->toArray(),
58
            'rating_guide_answers' => $answers->toArray()
59
        ];
60
    }
61
62
    public function show(JobPoster $jobPoster)
63
    {
64
        // Show demo notification if the user is a demoManager and is not an hr-advisor that has claimed the job.
65
        $hide_demo_notification = Auth::user() !== null && Auth::user()->isDemoManager() && !$jobPoster->hr_advisors->contains('user_id', Auth::user()->id);
66
67
        return view('manager/assessment_plan', [
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

67
        return /** @scrutinizer ignore-call */ view('manager/assessment_plan', [
Loading history...
68
            'hide_demo_notification' => $hide_demo_notification,
69
        ]);
70
    }
71
}
72
73
74