ResultCrudController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
c 0
b 0
f 0
dl 0
loc 135
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setup() 0 8 2
B setupListOperation() 0 87 1
A show() 0 19 2
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Models\Course;
6
use App\Models\Enrollment;
7
use App\Models\Period;
8
use App\Models\Result;
9
use App\Models\ResultType;
10
use Backpack\CRUD\app\Http\Controllers\CrudController;
11
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
12
use Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
13
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
14
use Illuminate\Support\Facades\Gate;
15
16
/**
17
 * Class ResultCrudController
18
 * Controller to monitor student results. No result can be added from here.
19
 */
20
class ResultCrudController extends CrudController
21
{
22
    use ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Admin\ResultCrudController: $model, $entity_name_plural
Loading history...
23
    use ShowOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ShowOperation requires some properties which are not provided by App\Http\Controllers\Admin\ResultCrudController: $route, $entity_name
Loading history...
24
25
    public function __construct()
26
    {
27
        parent::__construct();
28
        $this->middleware('permission:evaluation.view', ['except' => ['show']]);
29
    }
30
31
    public function setup()
32
    {
33
        CRUD::setModel(Enrollment::class);
34
        CRUD::setRoute(config('backpack.base.route_prefix').'/result');
35
        CRUD::setEntityNameStrings(__('result'), __('results'));
36
37
        if (backpack_user()->hasRole('admin')) {
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

37
        if (backpack_user()->/** @scrutinizer ignore-call */ hasRole('admin')) {
Loading history...
38
            CRUD::addButtonFromView('line', 'editResult', 'editResult', 'end');
39
        }
40
    }
41
42
    public function setupListOperation()
43
    {
44
        CRUD::setColumns([
45
            [
46
                'name' => 'id',
47
                'label' => 'ID',
48
            ],
49
            [
50
                // STUDENT NAME
51
                'label' => __('Student'),
52
                'type' => 'select',
53
                'name' => 'student',
54
                'attribute' => 'name',
55
                'searchLogic' => function ($query, $column, $searchTerm) {
56
                    $query->orWhereHas('student', function ($q) use ($searchTerm) {
57
                        $q->WhereHas('user', function ($q) use ($searchTerm) {
58
                            $q->where('firstname', 'like', '%'.$searchTerm.'%')
59
                            ->orWhere('lastname', 'like', '%'.$searchTerm.'%')
60
                            ->orWhere('email', 'like', '%'.$searchTerm.'%')
61
                          ->orWhere('idnumber', 'like', '%'.$searchTerm.'%');
62
                        });
63
                    });
64
                },
65
            ],
66
            [
67
                // COURSE NAME
68
                'label' => __('Course'),
69
                'type' => 'relationship',
70
                'name' => 'course',
71
                'attribute' => 'name',
72
                'model' => Course::class,
73
            ],
74
            [
75
                'name' => 'course.period',
76
                'label' => __('Period'),
77
                'type' => 'relationship',
78
                'attribute' => 'name',
79
            ],
80
            [
81
                // RESULT
82
                'label' => __('Result'),
83
                'type' => 'relationship',
84
                'name' => 'result',
85
                'attribute' => 'result_type',
86
                'model' => Result::class,
87
            ],
88
        ]);
89
90
        CRUD::addFilter(
91
            [
92
                'type' => 'simple',
93
                'name' => 'noresult',
94
                'label' => __('No Result'),
95
            ],
96
            false,
97
            function () {
98
                CRUD::addClause('noResult');
99
            }
100
        );
101
102
        CRUD::addFilter(
103
            [
104
                'type' => 'simple',
105
                'name' => 'hideparents',
106
                'label' => __('Hide Parents'),
107
            ],
108
            false,
109
            function () {
110
                CRUD::addClause('real');
111
            }
112
        );
113
114
        CRUD::addFilter([
115
            'name' => 'period_id',
116
            'type' => 'select2',
117
            'label' => __('Period'),
118
        ], fn () => Period::all()->pluck('name', 'id')->toArray(), function ($value) {
119
            CRUD::addClause('period', $value);
120
        });
121
122
        CRUD::addFilter([ // select2_multiple filter
123
            'name' => 'result',
124
            'type' => 'select2',
125
            'label' => __('Result'),
126
        ], fn () => ResultType::all()->pluck('name', 'id')->toArray(), function ($value) {
127
            $this->crud->query = $this->crud->query->whereHas('result', function ($query) use ($value) {
128
                $query->where('result_type_id', $value);
129
            });
130
        });
131
    }
132
133
    /**
134
     * Display the specified resource (result for a specific enrollment).
135
     */
136
    public function show($enrollment)
137
    {
138
        // the user is allowed to view the result if they are the student,
139
        // if they are the teacher of the course for this result
140
        // of if they have explicit permission to view any result
141
142
        $enrollment = Enrollment::findOrFail($enrollment);
143
144
        if (Gate::forUser(backpack_user())->denies('view-enrollment', $enrollment)) {
145
            abort(403);
146
        }
147
148
        $grades = $enrollment->grades->groupBy('grade_type_category');
149
        $skills = $enrollment->skill_evaluations;
150
        $result = $enrollment->result;
151
        $results = ResultType::all();
152
        $writeaccess = Gate::forUser(backpack_user())->check('edit-result', $enrollment);
153
154
        return view('results.show', compact('enrollment', 'writeaccess', 'grades', 'skills', 'result', 'results'));
155
    }
156
}
157