Passed
Push — feature/job-builder/step-03-ui ( dabf2e...dabf2e )
by Xander
20:05 queued 13s
created

AssessmentPlanController::getForJob()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 35
ccs 0
cts 24
cp 0
rs 9.2248
c 0
b 0
f 0
cc 5
nc 8
nop 1
crap 30
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
13
class AssessmentPlanController extends Controller
14
{
15
    /**
16
     * Return all the criteria, Assessments, Assessments,
17
     * RatingGuideQuestions, and RatingGuideAnswers associated with a job,
18
     * as an array.
19
     *
20
     * @param  \App\Models\JobPoster $jobPoster Job Poster to retrieve assessment plan for.
21
     * @return mixed[]
22
     */
23
    public function getForJob(JobPoster $jobPoster)
24
    {
25
        if (Gate::denies('view-assessment-plan', $jobPoster)) {
26
            abort(403);
27
        }
28
29
        $criteria = Criteria::where('job_poster_id', $jobPoster->id)->get();
30
        $criteriaTranslated = [];
31
        foreach ($criteria as $criterion) {
32
            // TODO: getTranslationsArray probably makes DB calls every loop. Find a way to profile & optimize.
33
            $criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray());
34
        }
35
        $criteriaIds = $criteria->pluck('id');
36
        $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
37
        // Check for newly created assessment plan, and initialize any empty criteria to have the
38
        // "Narrative Review" option set.
39
        $assessmentCriteriaIds = $assessments->pluck('criterion_id');
40
        $emptyAssessments = array_diff($criteriaIds->toArray(), $assessmentCriteriaIds->toArray());
41
        if (!empty($emptyAssessments)) {
42
            $narrativeReview = AssessmentType::where('key', 'narrative_assessment')->first();
43
            foreach ($emptyAssessments as $criterionId) {
44
                Assessment::create([
45
                    'criterion_id' => $criterionId,
46
                    'assessment_type_id' => $narrativeReview->id
47
                ]);
48
            }
49
            $assessments = Assessment::whereIn('criterion_id', $criteriaIds)->get();
50
        }
51
        $questions = RatingGuideQuestion::where('job_poster_id', $jobPoster->id)->get();
52
        $answers = RatingGuideAnswer::whereIn('rating_guide_question_id', $questions->pluck('id'))->get();
53
        return [
54
            'criteria' => $criteriaTranslated,
55
            'assessments' => $assessments->toArray(),
56
            'rating_guide_questions' => $questions->toArray(),
57
            'rating_guide_answers' => $answers->toArray()
58
        ];
59
    }
60
}
61