Passed
Push — feature/hidden-job-option ( da89d6...67bc2a )
by Yonathan
04:08 queued 10s
created

AssessmentPlanController::getForJob()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 31
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
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
use Illuminate\Support\Facades\Lang;
14
15
class AssessmentPlanController extends Controller
16
{
17
    /**
18
     * Return all the criteria, Assessments, Assessments,
19
     * RatingGuideQuestions, and RatingGuideAnswers associated with a job,
20
     * as an array.
21
     *
22
     * @param  \App\Models\JobPoster $jobPoster Job Poster to retrieve assessment plan for.
23
     * @return mixed[]
24
     */
25
    public function getForJob(JobPoster $jobPoster)
26
    {
27
        $criteria = Criteria::where('job_poster_id', $jobPoster->id)->get();
28
        $criteriaTranslated = [];
29
        foreach ($criteria as $criterion) {
30
            // TODO: getTranslationsArray probably makes DB calls every loop. Find a way to profile & optimize.
31
            $criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslations());
32
        }
33
        $criteriaIds = $criteria->pluck('id');
34
        $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
35
        // Check for newly created assessment plan, and initialize any empty criteria to have the
36
        // "Narrative Review" option set.
37
        $assessmentCriteriaIds = $assessments->pluck('criterion_id');
38
        $emptyAssessments = array_diff($criteriaIds->toArray(), $assessmentCriteriaIds->toArray());
39
        if (!empty($emptyAssessments)) {
40
            $narrativeReview = AssessmentType::where('key', 'narrative_assessment')->first();
41
            foreach ($emptyAssessments as $criterionId) {
42
                Assessment::create([
43
                    'criterion_id' => $criterionId,
44
                    'assessment_type_id' => $narrativeReview->id
45
                ]);
46
            }
47
            $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
48
        }
49
        $questions = RatingGuideQuestion::where('job_poster_id', $jobPoster->id)->get();
50
        $answers = RatingGuideAnswer::whereIn('rating_guide_question_id', $questions->pluck('id'))->get();
51
        return [
52
            'criteria' => $criteriaTranslated,
53
            'assessments' => $assessments->toArray(),
54
            'rating_guide_questions' => $questions->toArray(),
55
            'rating_guide_answers' => $answers->toArray()
56
        ];
57
    }
58
59
    public function show(JobPoster $jobPoster)
60
    {
61
        // Show demo notification if the user is a demoManager and is not an hr-advisor that has claimed the job.
62
        $display_demo_notification = Auth::user() !== null &&
63
                                  Auth::user()->isDemoManager() &&
64
                                  (!$jobPoster->hr_advisors->contains('user_id', Auth::user()->id) &&
65
                                  Auth::user()->isHrAdvisor());
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
            'title' => Lang::get('manager/assessment_plan.title'),
69
            'job_id' => $jobPoster->id,
70
            'display_demo_notification' => $display_demo_notification,
71
        ]);
72
    }
73
}
74
75
76