Passed
Push — master ( 59f95b...dae6f7 )
by Thomas
12:11
created

CourseSkillEvaluationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Course;
6
use App\Models\Enrollment;
7
use App\Models\Result;
8
use App\Models\ResultType;
9
use App\Models\Skills\SkillEvaluation;
10
use App\Models\Skills\SkillScale;
11
use App\Models\Student;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Gate;
14
15
class CourseSkillEvaluationController extends Controller
16
{
17
    /**
18
     * Show the skills overview for all students in the course.
19
     */
20
    public function index(Course $course)
21
    {
22
        if (Gate::forUser(backpack_user())->denies('view-course', $course)) {
23
            abort(403);
24
        }
25
26
        $skills = $course->skills->groupBy('skill_type_id');
27
        $enrollments = $course->enrollments;
28
29
        // get skill evaluations for the course
30
        $skill_evaluations = $course->skill_evaluations;
31
32
        return view('skills.students', compact('course', 'skills', 'skill_evaluations', 'enrollments'));
33
    }
34
35
    /**
36
     * Store a skill evaluation record for a student.
37
     */
38
    public function store(Request $request)
39
    {
40
        $skill = $request->input('skill');
41
        $status = $request->input('status');
42
        $student = $request->input('student');
43
        $course = Course::findOrFail($request->input('course'));
44
45
        if (Gate::forUser(backpack_user())->denies('view-course', $course)) {
46
            abort(403);
47
        }
48
49
        $new_skill = SkillEvaluation::firstOrNew([
50
            'course_id' => $course->id,
51
            'student_id' => $student,
52
            'skill_id' => $skill,
53
        ]);
54
55
        $new_skill->skill_scale_id = $status;
56
        $new_skill->save();
57
58
        return $new_skill->skill_scale_id;
59
    }
60
61
    /**
62
     * Show the form for editing a specific student's skills for the specified course.
63
     */
64
    public function edit(Course $course, Student $student)
65
    {
66
        if (Gate::forUser(backpack_user())->denies('view-course', $course)) {
67
            abort(403);
68
        }
69
70
        $student_skills = SkillEvaluation::where('student_id', $student->id)
71
        ->where('course_id', $course->id)
72
        ->get();
73
74
        $skills = $course->skills->map(function ($skill, $key) use ($student_skills) {
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

74
        $skills = $course->skills->map(function ($skill, /** @scrutinizer ignore-unused */ $key) use ($student_skills) {

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...
75
            $skill['status'] = $student_skills->where('skill_id', $skill->id)->first()->skill_scale_id ?? null;
76
77
            return $skill;
78
        });
79
80
        $skills = $skills->groupBy('skill_type_id');
81
82
        $enrollment = Enrollment::where('student_id', $student->id)
83
        ->where('course_id', $course->id)->first();
84
85
        $result = Result::where(['enrollment_id' => $enrollment->id])->with('result_name')->first();
86
87
        $results = ResultType::all();
88
        $skillScales = SkillScale::all();
89
        $writeaccess = backpack_user()->can('enrollments.edit') ?? 0;
90
91
        return view('skills.student', compact('course', 'student', 'skills', 'skillScales', 'result', 'enrollment', 'results', 'writeaccess'));
92
    }
93
}
94