Issues (350)

app/Http/Controllers/CourseController.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Course;
6
use App\Models\Level;
7
use App\Models\Period;
8
use App\Models\Rhythm;
9
use App\Models\Student;
10
use App\Traits\FiltersSearchableLevels;
11
use Illuminate\Http\Request;
12
use Spatie\QueryBuilder\AllowedFilter;
13
use Spatie\QueryBuilder\QueryBuilder;
14
15
class CourseController extends Controller
16
{
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->middleware('permission:courses.view');
21
    }
22
23
    /**
24
     * Display a listing of the resource.
25
     */
26
    public function index(Request $request)
27
    {
28
        $defaultPeriod = Period::get_default_period();
29
        $rhythms = Rhythm::all();
30
        $levels = Level::all();
31
        $isAllowedToEdit = backpack_user()->hasPermissionTo('courses.edit') ? 1 : 0;
0 ignored issues
show
The method hasPermissionTo() 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

31
        $isAllowedToEdit = backpack_user()->/** @scrutinizer ignore-call */ hasPermissionTo('courses.edit') ? 1 : 0;
Loading history...
32
        $mode = $request->mode ?? 'view';
33
        $student = Student::with('enrollments')->find($request->student_id) ?? collect(['']);
34
        $enrollment_id = $request->enrollment_id ?? 'none';
35
36
        return view('courses.list', compact('defaultPeriod', 'isAllowedToEdit', 'rhythms', 'levels', 'mode', 'student', 'enrollment_id'));
37
    }
38
39
    public function search()
40
    {
41
        return QueryBuilder::for(Course::class)->where('campus_id', 1)
42
        ->with('room')->withCount('events')->withCount('children')->withCount('enrollments')
43
        ->allowedFilters([
44
            'name',
45
            AllowedFilter::exact('period_id'),
46
            AllowedFilter::exact('rhythm_id'),
47
            AllowedFilter::custom('searchable_levels', new FiltersSearchableLevels()),
48
            AllowedFilter::exact('teacher_id'),
49
        ])
50
        ->get();
51
    }
52
53
    public function redirectToUserPreferredView()
54
    {
55
        return match (backpack_user()->preferred_course_view) {
0 ignored issues
show
Accessing preferred_course_view on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56
            'blocks' => redirect(route('get-courses-list')),
57
            default => redirect(route('course.index')),
58
        };
59
    }
60
61
    public function switchViews(Request $request)
62
    {
63
        switch ($request->view) {
64
            case 'blocks':
65
                backpack_user()->update(['preferred_course_view' => 'blocks']);
66
67
                return redirect(route('get-courses-list'));
68
69
            default:
70
                backpack_user()->update(['preferred_course_view' => 'list']);
71
72
                return redirect(route('course.index'));
73
        }
74
    }
75
}
76