Institution_shift::getShiftsToClone()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
c 5
b 0
f 1
nc 2
nop 3
dl 0
loc 18
rs 9.8333
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Institution_shift extends Base_Model
8
{
9
10
    /**
11
     * The database table used by the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'institution_shifts';
16
17
    /**
18
     * Attributes that should be mass-assignable.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = ['start_time', 'end_time', 'academic_period_id', 'institution_id', 'location_institution_id', 'shift_option_id', 'previous_shift_id', 'modified_user_id', 'modified', 'created_user_id', 'created'];
23
24
    /**
25
     * The attributes excluded from the model's JSON form.
26
     *
27
     * @var array
28
     */
29
    protected $hidden = [];
30
31
    /**
32
     * The attributes that should be casted to native types.
33
     *
34
     * @var array
35
     */
36
    protected $casts = [];
37
38
    /**
39
     * The attributes that should be mutated to dates.
40
     *
41
     * @var array
42
     */
43
    protected $dates = ['modified', 'created'];
44
45
    public function shiftExists($shift)
46
    {
47
        return self::query()
48
            ->where('institution_id', $shift['institution_id'])
49
            ->where('location_institution_id', $shift['location_institution_id'])
50
            ->where('shift_option_id', $shift['shift_option_id'])
51
            ->where('academic_period_id', $shift['academic_period_id'])->exists();
52
    }
53
54
    public function getShift($shift)
55
    {
56
        return self::query()
57
            ->where('institution_id', $shift['institution_id'])
58
            ->where('location_institution_id', $shift['location_institution_id'])
59
            ->where('shift_option_id', $shift['shift_option_id'])
60
            ->where('academic_period_id', $shift['academic_period_id'])->first();
61
    }
62
63
    public function getShiftsToClone(string $year, $limit, $mode)
64
    {
65
        $query = self::query()
66
            ->select('institution_shifts.*')
67
            ->join('academic_periods', 'academic_periods.id', '=', 'institution_shifts.academic_period_id')
68
            ->where('academic_periods.code', $year);
69
70
        if ($mode) {
71
            $query->whereIn('institution_shifts.cloned',['2020']);
72
        } else {
73
            $query->whereNotIn('institution_shifts.cloned',['2020','2019/2020']);
74
        }
75
        
76
        $data =    $query->groupBy('institution_shifts.id')
77
            ->limit($limit)
78
            ->get()
79
            ->toArray();
80
        return $data;
81
    }
82
83
    public function getShiftsTodelete(string $year, $academic_period_id)
84
    {
85
        return self::query()
86
            // ->select('institution_shifts.*','academic_periods.academic_period_id')
87
            ->join('academic_periods', 'academic_periods.id', '=', 'institution_shifts.academic_period_id')
88
            ->where('academic_period_id', $academic_period_id)
89
            ->where('institution_shifts.cloned', $year)
90
            ->get()->toArray();
91
    }
92
}
93