Completed
Branch develop-3.0 (48060b)
by Mohamed
02:47
created

Project::updater()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Illuminate\Database\Eloquent\Collection;
15
use URL;
16
17
/**
18
 * Project is model class for projects.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @property int $id
23
 * @property string $name
24
 * @property int $status
25
 * @property int $default_assignee
26
 * @property int $private
27
 * @property int $openIssuesCount
28
 * @property int $closedIssuesCount
29
 * @property Collection $issues
30
 * @property Collection $issuesByUser
31
 * @property Collection $users
32
 * @property Collection $projectUsers
33
 * @property Collection $activities
34
 * @property Collection $notes
35
 * @property Collection $kanbanTags
36
 *
37
 * @method  Collection getActiveProjects()
38
 * @method  Collection getNotes()
39
 * @method  Collection getPublicProjects()
40
 * @method  Collection getNotMembers()
41
 * @method  Collection getIssues($status = Project\Issue::STATUS_OPEN, array $filter = [])
42
 * @method  Collection getIssuesForLoggedUser($status = Project\Issue::STATUS_OPEN, array $filter = [])
43
 * @method  Collection getAssignedOrCreatedIssues(User $user)
44
 * @method  Collection getCreatedIssues(User $user)
45
 * @method  Collection getAssignedIssues(User $user)
46
 * @method  Collection getRecentActivities(User $user = null, $limit = 10)
47
 * @method  Collection getPublicProjectsWithRecentIssues()
48
 * @method  Collection getKanbanTagsForUser(User $user)
49
 * @method  Collection getKanbanTags()
50
 * @method  Collection getUsersCanFixIssue()
51
 * @method  Collection getUsers()
52
 * @method  Collection getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES)
53
 * @method  Collection getProjectsWithCountIssues(array $projectIds)
54
 * @method  int countOpenIssues(User $forUser)
55
 * @method  int countClosedIssues(User $forUser)
56
 * @method  int countNotes()
57
 * @method  int countAssignedIssues(User $forUser)
58
 * @method  int countCreatedIssues(User $forUser)
59
 * @method  int countPrivateProjects()
60
 * @method  int countActiveProjects()
61
 * @method  int countArchivedProjects()
62
 * @method  int countProjectsByStatus($status)
63
 * @method  $this status($status = Project::STATUS_OPEN)
64
 * @method  $this active()
65
 * @method  $this archived()
66
 * @method  $this public()
67
 * @method  $this notPublic()
68
 */
69
class Project extends ModelAbstract
70
{
71
    use ProjectRelations,
72
        ProjectScopes;
73
74
    /**
75
     * Project private & user role can see their own issues only.
76
     *
77
     * @var int
78
     */
79
    const INTERNAL_YES = 2;
80
81
    /**
82
     * Project not public to view and create issue.
83
     *
84
     * @var int
85
     */
86
    const PRIVATE_YES = 1;
87
88
    /**
89
     * Project public to view and create issue.
90
     *
91
     * @var int
92
     */
93
    const PRIVATE_NO = 0;
94
95
    /**
96
     * All projects.
97
     *
98
     * @var int
99
     */
100
    const PRIVATE_ALL = -1;
101
102
    /**
103
     * Project status Open.
104
     *
105
     * @var int
106
     */
107
    const STATUS_OPEN = 1;
108
109
    /**
110
     * Project status Archived.
111
     *
112
     * @var int
113
     */
114
    const STATUS_ARCHIVED = 0;
115
116
    /**
117
     * Timestamp enabled.
118
     *
119
     * @var bool
120
     */
121
    public $timestamps = true;
122
123
    /**
124
     * Name of database table.
125
     *
126
     * @var string
127
     */
128
    protected $table = 'projects';
129
130
    /**
131
     * List of allowed columns to be used in $this->fill().
132
     *
133
     * @var array
134
     */
135
    protected $fillable = ['name', 'default_assignee', 'status', 'private'];
136
137
    /**
138
     * List of HTML classes for each status.
139
     *
140
     * @var array
141
     */
142
    protected $attrClassNames = [
143
        self::PRIVATE_NO   => 'note',
144
        self::PRIVATE_YES  => 'info',
145
        self::INTERNAL_YES => 'primary',
146
    ];
147
148
    /**
149
     * List of statuses names.
150
     *
151
     * @var array
152
     */
153
    protected $statusesNames = [
154
        self::PRIVATE_NO   => 'public',
155
        self::PRIVATE_YES  => 'private',
156
        self::INTERNAL_YES => 'internal',
157
    ];
158
159
    /**
160
     * @param User|null $user
161
     *
162
     * @return \Tinyissue\Repository\Project\Updater
163
     */
164
    public function updater(User $user = null)
165
    {
166
        return parent::updater($user);
167
    }
168
169
    /**
170
     * Generate a URL for the active project.
171
     *
172
     * @param string $url
173
     *
174
     * @return string
175
     */
176
    public function to($url = '')
177
    {
178
        return URL::to('project/' . $this->id . (($url) ? '/' . $url : ''));
179
    }
180
181
    /**
182
     * Returns the aggregate value of number of open issues in the project.
183
     *
184
     * @return int
185
     */
186
    public function getOpenIssuesCountAttribute()
187
    {
188
        return $this->getCountAttribute('openIssuesCount');
189
    }
190
191
    /**
192
     * Returns the aggregate value of number of closed issues in the project.
193
     *
194
     * @return int
195
     */
196
    public function getClosedIssuesCountAttribute()
197
    {
198
        return $this->getCountAttribute('closedIssuesCount');
199
    }
200
201
    /**
202
     * Set default assignee attribute.
203
     *
204
     * @param int $value
205
     *
206
     * @return $this
207
     */
208
    public function setDefaultAssigneeAttribute($value)
209
    {
210
        if (!empty($value)) {
211
            $this->attributes['default_assignee'] = (int) $value;
212
        }
213
214
        return $this;
215
    }
216
217
    /**
218
     * Returns the aggregate value of number of issues in the project.
219
     *
220
     * @return int
221
     */
222
    public function getIssuesCountAttribute()
223
    {
224
        return $this->getCountAttribute('issuesCount');
225
    }
226
227
    /**
228
     * Get total issues total quote time.
229
     *
230
     * @return int
231
     */
232
    public function getTotalQuote()
233
    {
234
        $total = 0;
235
        foreach ($this->issues as $issue) {
236
            $total += $issue->time_quote;
237
        }
238
239
        return $total;
240
    }
241
242
    /**
243
     * Calculate the progress (open & closed issues).
244
     *
245
     * @return float|int
246
     */
247
    public function getProgress()
248
    {
249
        $total    = $this->openIssuesCount + $this->closedIssuesCount;
250
        $progress = 100;
251
        if ($total > 0) {
252
            $progress = (float) ($this->closedIssuesCount / $total) * 100;
253
        }
254
        $progressInt = (int) $progress;
255
        if ($progressInt > 0) {
256
            $progress = number_format($progress, 2);
257
            $fraction = $progress - $progressInt;
258
            if ($fraction === 0.0) {
259
                $progress = $progressInt;
260
            }
261
        }
262
263
        return $progress;
264
    }
265
266
    /**
267
     * Whether or not a user is member of the project.
268
     *
269
     * @param int $userId
270
     *
271
     * @return bool
272
     */
273
    public function isMember($userId)
274
    {
275
        return $this->user($userId)->count() > 0;
276
    }
277
278
    /**
279
     * Whether or not the project is private.
280
     *
281
     * @return bool
282
     */
283
    public function isPrivate()
284
    {
285
        return (int) $this->private === self::PRIVATE_YES;
286
    }
287
288
    /**
289
     * Whether or not the project is public.
290
     *
291
     * @return bool
292
     */
293
    public function isPublic()
294
    {
295
        return (int) $this->private === self::PRIVATE_NO;
296
    }
297
298
    /**
299
     * Whether or not the project is private internal.
300
     *
301
     * @return bool
302
     */
303
    public function isPrivateInternal()
304
    {
305
        return (int) $this->private === self::INTERNAL_YES;
306
    }
307
308
    /**
309
     * Returns project status as string name.
310
     *
311
     * @return string
312
     */
313
    public function getStatusAsName()
314
    {
315
        if (array_key_exists((int) $this->private, $this->statusesNames)) {
316
            return $this->statusesNames[(int) $this->private];
317
        }
318
319
        return '';
320
    }
321
322
    /**
323
     * Returns the class name to be used for project status.
324
     *
325
     * @return string
326
     */
327
    public function getStatusClass()
328
    {
329
        if (array_key_exists((int) $this->private, $this->attrClassNames)) {
330
            return $this->attrClassNames[(int) $this->private];
331
        }
332
333
        return '';
334
    }
335
336
    /**
337
     * Get user preferred messaging type.
338
     *
339
     * @param int $userId
340
     *
341
     * @return mixed
342
     */
343
    public function getPreferredMessageIdForUser($userId)
344
    {
345
        return $this->projectUsers()
346
            ->where('user_id', '=', $userId)
347
            ->first()
348
            ->message_id;
349
    }
350
}
351