Completed
Push — master ( 58b661...a53837 )
by Francesco
09:22
created

Compilation::student()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Models;
5
6
use App\Models\Traits\EloquentGetTableName;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Staudenmeir\EloquentParamLimitFix\ParamLimitFix;
12
13
class Compilation extends Model
14
{
15
16
    use SoftDeletes;
17
    use EloquentGetTableName;
18
    use ParamLimitFix;
19
20
    /**
21
     * The attributes that should be mutated to dates.
22
     *
23
     * @var array
24
     */
25
    protected $dates = ["deleted_at"];
26
27
    protected $appends = [
28
        "stage_weeks"
29
    ];
30
31
    /**
32
     * Get the student who created this compilation
33
     * @return BelongsTo
34
     */
35
    public function student(): BelongsTo
36
    {
37
        return $this->belongsTo("App\Models\Student")->withTrashed();
38
    }
39
40
    /**
41
     * Get the location where the stage of this compilation took place
42
     * @return BelongsTo
43
     */
44
    public function stageLocation(): BelongsTo
45
    {
46
        return $this->belongsTo("App\Models\Location", "stage_location_id", "id")->withTrashed();
47
    }
48
49
    /**
50
     * Get the ward where the stage of this compilation took place
51
     * @return BelongsTo
52
     */
53
    public function stageWard(): BelongsTo
54
    {
55
        return $this->belongsTo("App\Models\Ward", "stage_ward_id", "id")->withTrashed();
56
    }
57
58
    /**
59
     * Get the items of this compilation (as relationship)
60
     * @return HasMany
61
     */
62
    public function items(): HasMany
63
    {
64
        return $this->hasMany("App\Models\CompilationItem")
65
            // Compilation items are returned only once, although items related
66
            // to "multiple_choice" are stored as several records/models.
67
            ->groupBy("compilation_id")
68
            ->groupBy("question_id")
69
            // @todo item sorting must be based on section+question "position" attributes
70
            ->orderBy("question_id");
71
    }
72
73
    /**
74
     * Get the number of stage weeks.
75
     *
76
     * @return int
77
     */
78
    public function getStageWeeksAttribute(): int
79
    {
80
        return (int)round($this->getStageDays() / 7);
81
    }
82
83
    /**
84
     * Get the number of stage days.
85
     *
86
     * @return int
87
     */
88
    public function getStageDays(): int
89
    {
90
        return ((strtotime($this->stage_end_date) - strtotime($this->stage_start_date)) / 60 / 60 / 24) + 1;
91
    }
92
93
}
94