1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
class Training extends Model |
8
|
|
|
{ |
9
|
|
|
protected $table = 'trainings'; |
10
|
|
|
public $timestamps = true; |
11
|
|
|
|
12
|
|
|
protected $fillable = ['name', 'renews_in', 'description', 'administrative', 'training_type_id']; |
13
|
|
|
protected $dates = ['created_at', 'updated_at']; |
14
|
|
|
protected $appends = ['incompleted']; |
15
|
|
|
|
16
|
|
|
public function assignedUsers() |
17
|
|
|
{ |
18
|
|
|
return $this->hasMany('SET\TrainingUser', 'training_id'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function users() |
22
|
|
|
{ |
23
|
|
|
return $this->belongsToMany('SET\User'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function groups() |
27
|
|
|
{ |
28
|
|
|
return $this->belongsToMany('SET\Group'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function attachments() |
32
|
|
|
{ |
33
|
|
|
return $this->morphMany('SET\Attachment', 'imageable'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getIncompletedAttribute() |
37
|
|
|
{ |
38
|
|
|
return $this->users() |
39
|
|
|
->whereNull('training_user.completed_date') |
40
|
|
|
->active() |
41
|
|
|
->count(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $query |
46
|
|
|
* @param $input |
47
|
|
|
*/ |
48
|
|
|
public function scopeSearchTraining($query, $input) |
49
|
|
|
{ |
50
|
|
|
return $query->where('name', 'LIKE', "%$input%"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the training type for the training. |
55
|
|
|
*/ |
56
|
|
|
public function trainingType() |
57
|
|
|
{ |
58
|
|
|
return $this->belongsTo('SET\TrainingType', 'training_type_id'); // One To Many (Inverse) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @param type $query |
64
|
|
|
* @param type $type |
65
|
|
|
* @return type Training |
66
|
|
|
* |
67
|
|
|
* Get the training for the specified training_type |
68
|
|
|
*/ |
69
|
|
|
public function scopeTrainingByType($query, $type) |
70
|
|
|
{ |
71
|
|
|
return $query->whereHas('trainingType', function ($q) use($type) { |
72
|
|
|
$q->where('name', $type); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|