forked from DallasMuseumArt/OctoberFriends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity.php
209 lines (178 loc) · 5.71 KB
/
Activity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php namespace DMA\Friends\Models;
use Model;
use DateTime;
use DB;
use Smirik\PHPDateTimeAgo\DateTimeAgo as TimeAgo;
/**
* Activity Model
*
* NOTE: Time restrictions conform to the ISO-8601 numeric representation of the day of the week
* where 1 (for Monday) through 7 (for Sunday) see <a href="http://php.net/manual/en/function.date.php">PHP's Date Manual</a>
*/
class Activity extends Model
{
use \October\Rain\Database\Traits\Validation;
use \DMA\Friends\Traits\Rateable;
/**
* @const No time restriction set
*/
const TIME_RESTRICT_NONE = 0;
/**
* @const A time restriction is set by individual hours and days of the week
*/
const TIME_RESTRICT_HOURS = 1;
/**
* @const A time restriction is set by a date range
*/
const TIME_RESTRICT_DAYS = 2;
/**
* @var string The database table used by the model.
*/
public $table = 'dma_friends_activities';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = ['touch'];
protected $dates = ['date_begin', 'date_end'];
public $rules = [
'title' => 'required',
'activity_code' => 'unique:dma_friends_activities',
'complete_message' => 'max:159'
];
/**
* @var array Relations
*/
public $hasMany = [
'steps' => ['DMA\Friends\Models\Step', 'table' => 'dma_friends_activity_step'],
];
public $belongsToMany = [
'users' => ['RainLab\User\Models\User', 'table' => 'dma_friends_activity_user', 'timestamps' => true],
];
public $attachOne = [
'image' => ['System\Models\File'],
];
public $morphMany = [
'activityLogs' => ['DMA\Friends\Models\ActivityLog', 'name' => 'object'],
'bookmarks' => ['DMA\Friends\Models\Bookmark', 'name' => 'object'],
];
public $morphToMany = [
'categories' => ['DMA\Friends\Models\Category',
'name' => 'object',
'table' => 'dma_friends_object_categories',
'order' => 'name',
],
];
/**
* Mutator to ensure time_restriction_data is serialized
*/
public function setTimeRestrictionDataAttribute($value)
{
if (is_array($value)) {
$value = serialize($value);
}
$this->attributes['time_restriction_data'] = $value;
}
/**
* Accessor to unserialize time_restriction_data attribute
*/
public function getTimeRestrictionDataAttribute($value)
{
return unserialize($value);
}
/**
* Return only activities that are active
*/
public function scopeIsActive($query)
{
return $query->where('is_published', '=', 1)
->where('is_archived', '<>', 1);
}
/**
* Find an activity by its activity code
*/
public function scopefindCode($query, $code)
{
return $query->where('activity_code', $code)
->isActive();
}
/**
* Find activities by activity type
*/
public function scopeFindActivityType($query, $type)
{
return $query->where('activity_type', $type)
->isActive();
}
/**
* Find activities by a wordpress id if they where imported
*/
public function scopefindWordpress($query, $id)
{
return $query->where('wordpress_id', $id);
}
/**
* Find activities by category
* @param string|array $categories string or array of strings of category name(s)
*/
public function scopeByCategory($query, $categories) {
if (is_array($categories)) {
// Get first category in list to begin query with whereHas
$firstcategory = array_shift($categories);
$query = $query->whereHas('categories', function($q) use ($firstcategory) {
$q->where('name', $firstcategory);
});
// if any categories left in list, iterate over for orWhereHas
foreach ($categories as $category) {
$query = $query->orWhereHas('categories', function($q) use ($category) {
$q->where('name', $category);
});
}
return $query;
}
else {
// if $categories is a string, only one category to filter against
return $query->whereHas('categories', function($q) use ($categories) {
$q->where('name', $categories);
});
}
}
/**
* Find activities whose end dates have not passed (or which do not have end dates)
*/
public function scopeNotExpired($query) {
$query = $query->where(function($q) {
$q->where('time_restriction', '!=', 2)
->orWhere('date_end', '>', date('Y-m-d H:i:s'));
});
return $query;
}
/**
* Find activities whose end dates have not passed,
* AND whose begin dates have passed
* (or which do not have begin and end dates)
*/
public function scopeStartedNotExpired($query) {
$query = $query->where(function($q) {
$q->where('time_restriction', '!=', 2)
->orWhere(function($q) {
$q->where('date_end', '>', date('Y-m-d H:i:s'))
->where('date_begin', '<', date('Y-m-d H:i:s'));
});
});
return $query;
}
/**
* Mutator function to return the pivot timestamp as time ago
* @return string The time since the badge was earned
*/
public function getTimeAgoAttribute($value)
{
if (!isset($this->pivot->created_at)) return null;
$timeAgo = new TimeAgo;
return $timeAgo->get($this->pivot->created_at);
}
}