1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GitScrum v0.1. |
4
|
|
|
* |
5
|
|
|
* @author Renato Marinho <renato.marinho> |
6
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace GitScrum\Models; |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
13
|
|
|
use GitScrum\Scopes\GlobalScope; |
14
|
|
|
|
15
|
|
|
class UserStory extends Model |
16
|
|
|
{ |
17
|
|
|
use SoftDeletes; |
18
|
|
|
use GlobalScope; |
19
|
|
|
/** |
20
|
|
|
* The database table used by the model. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $table = 'user_stories'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Attributes that should be mass-assignable. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $fillable = ['user_id', 'product_backlog_id', 'title', 'description', 'config_priority_id', 'acceptance_criteria']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The attributes excluded from the model's JSON form. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $hidden = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The attributes that should be casted to native types. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $casts = []; |
46
|
|
|
|
47
|
|
|
protected static function boot() |
48
|
|
|
{ |
49
|
|
|
parent::boot(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function productBacklog() |
53
|
|
|
{ |
54
|
|
|
return $this->belongsTo(\GitScrum\Models\ProductBacklog::class, 'product_backlog_id', 'id'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function issues() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return $this->hasMany(\GitScrum\Models\Issue::class, 'user_story_id', 'id') |
60
|
|
|
->orderby('position', 'ASC'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function priority() |
64
|
|
|
{ |
65
|
|
|
return $this->hasOne(\GitScrum\Models\ConfigPriority::class, 'id', 'config_priority_id'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function favorite() |
69
|
|
|
{ |
70
|
|
|
return $this->morphOne(\GitScrum\Models\Favorite::class, 'favoriteable'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function comments() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return $this->morphMany(\GitScrum\Models\Comment::class, 'commentable') |
76
|
|
|
->orderby('created_at', 'DESC'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function notes() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return $this->morphMany(\GitScrum\Models\Note::class, 'noteable') |
82
|
|
|
->orderby('position', 'ASC'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function labels() |
86
|
|
|
{ |
87
|
|
|
return $this->morphToMany(\GitScrum\Models\Label::class, 'labelable'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function activities() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$activities = $this->issues() |
93
|
|
|
->with('statuses')->get()->map(function ($issue) { |
94
|
|
|
return $issue->statuses; |
95
|
|
|
})->flatten(1)->map(function ($statuses) { |
96
|
|
|
return $statuses; |
97
|
|
|
})->sortByDesc('created_at'); |
98
|
|
|
|
99
|
|
|
$activities->splice(15); |
100
|
|
|
|
101
|
|
|
return $activities->all(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function issuesHasUsers($total = 3) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$users = $this->issues->map(function ($issue) { |
|
|
|
|
107
|
|
|
return $issue->users; |
108
|
|
|
})->reject(function ($value) { |
109
|
|
|
return $value == null; |
110
|
|
|
})->flatten(1)->unique('id')->splice(0, $total); |
111
|
|
|
|
112
|
|
|
return $users->all(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function issueStatus() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$status = $this->issues->map(function ($issue) { |
|
|
|
|
118
|
|
|
return $issue->status; |
119
|
|
|
})->groupBy('slug')->all(); |
120
|
|
|
|
121
|
|
|
return $status; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.