|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use GrahamCampbell\Markdown\Facades\Markdown; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
class GroupModel extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
protected $tableName='group'; |
|
12
|
|
|
protected $table='group'; |
|
13
|
|
|
protected $primaryKey='gid'; |
|
14
|
|
|
const DELETED_AT=null; |
|
15
|
|
|
const UPDATED_AT=null; |
|
16
|
|
|
const CREATED_AT=null; |
|
17
|
|
|
|
|
18
|
|
|
public $role=[ |
|
19
|
|
|
"-3"=>"None", |
|
20
|
|
|
"-1"=>"Invited", |
|
21
|
|
|
"0"=>"Pending", |
|
22
|
|
|
"1"=>"Member", |
|
23
|
|
|
"2"=>"Manager", |
|
24
|
|
|
"3"=>"Leader" |
|
25
|
|
|
]; |
|
26
|
|
|
public $role_color=[ |
|
27
|
|
|
"-3"=>"wemd-black", |
|
28
|
|
|
"-1"=>"wemd-deep-purple", |
|
29
|
|
|
"0"=>"wemd-red", |
|
30
|
|
|
"1"=>"wemd-grey", |
|
31
|
|
|
"2"=>"wemd-light-blue", |
|
32
|
|
|
"3"=>"wemd-amber" |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
public function tendingGroups() |
|
36
|
|
|
{ |
|
37
|
|
|
$tending_groups=DB::table($this->tableName)->where(["public"=>1])->orderBy('create_time', 'desc')->select("gid", "gcode", "img", "name", "verified")->limit(12)->get()->all(); //Fake Tending |
|
38
|
|
|
foreach ($tending_groups as &$t) { |
|
39
|
|
|
$t["members"]=$this->countGroupMembers($t["gid"]); |
|
40
|
|
|
} |
|
41
|
|
|
return $tending_groups; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function userGroups($uid) |
|
45
|
|
|
{ |
|
46
|
|
|
$user_groups=DB::table("group_member")->join("group", "group_member.gid", "=", "group.gid")->where(["uid"=>$uid])->select("group.gid as gid", "gcode", "img", "name", "verified")->limit(12)->get()->all(); |
|
47
|
|
|
foreach ($user_groups as &$m) { |
|
48
|
|
|
$m["members"]=$this->countGroupMembers($m["gid"]); |
|
49
|
|
|
} |
|
50
|
|
|
return $user_groups; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function countGroupMembers($gid) |
|
54
|
|
|
{ |
|
55
|
|
|
return DB::table("group_member")->where(["gid"=>$gid])->count(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getGroupTags($gid) |
|
59
|
|
|
{ |
|
60
|
|
|
return DB::table("group_tag")->where(["gid"=>$gid])->select("tag")->get()->all(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function countGroupContest($gid) |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
"contest_ahead" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", ">", DB::raw("now()"))->count(), |
|
67
|
|
|
"contest_going" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", "<=", DB::raw("now()"))->where("end_time", ">=", DB::raw("now()"))->count(), |
|
68
|
|
|
"contest_end" => DB::table("contest")->where(["gid"=>$gid])->where("end_time", "<", DB::raw("now()"))->count() |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function changeNickName($gid, $uid, $nickName) |
|
73
|
|
|
{ |
|
74
|
|
|
return DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->update([ |
|
75
|
|
|
"nick_name"=>$nickName |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function details($gcode) |
|
80
|
|
|
{ |
|
81
|
|
|
$basic_info=DB::table($this->tableName)->where(["gcode"=>$gcode])->first(); |
|
82
|
|
|
if(empty($basic_info)) return []; |
|
83
|
|
|
$basic_info["members"]=$this->countGroupMembers($basic_info["gid"]); |
|
84
|
|
|
$basic_info["tags"]=$this->getGroupTags($basic_info["gid"]); |
|
85
|
|
|
$basic_info["create_time_foramt"]=date_format(date_create($basic_info["create_time"]), 'M jS, Y'); |
|
86
|
|
|
$basic_info["contest_stat"]=$this->countGroupContest($basic_info["gid"]); |
|
87
|
|
|
return $basic_info; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function joinPolicy($gid) |
|
91
|
|
|
{ |
|
92
|
|
|
$ret=DB::table($this->tableName)->where(["gid"=>$gid])->first(); |
|
93
|
|
|
return empty($ret) ? null : $ret["join_policy"]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function userProfile($uid, $gid) |
|
97
|
|
|
{ |
|
98
|
|
|
$info=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->where("role", ">", 0)->first(); |
|
99
|
|
|
if (!empty($info)) { |
|
100
|
|
|
$info["role_parsed"]=$this->role[$info["role"]]; |
|
101
|
|
|
} |
|
102
|
|
|
return $info; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function userList($gid) |
|
106
|
|
|
{ |
|
107
|
|
|
$user_list=DB::table("group_member")->join( |
|
108
|
|
|
"users", |
|
109
|
|
|
"users.id", |
|
110
|
|
|
"=", |
|
111
|
|
|
"group_member.uid" |
|
112
|
|
|
)->where(["gid"=>$gid])->orderBy('role', 'desc')->select( |
|
113
|
|
|
"role", |
|
114
|
|
|
"uid", |
|
115
|
|
|
"name", |
|
116
|
|
|
"nick_name", |
|
117
|
|
|
"avatar", |
|
118
|
|
|
"sub_group" |
|
119
|
|
|
)->get()->all(); |
|
120
|
|
|
foreach ($user_list as &$u) { |
|
121
|
|
|
$u["role_parsed"]=$this->role[$u["role"]]; |
|
122
|
|
|
$u["role_color"]=$this->role_color[$u["role"]]; |
|
123
|
|
|
if(is_null($u["sub_group"])) $u["sub_group"]="None"; |
|
124
|
|
|
} |
|
125
|
|
|
return $user_list; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function groupNotice($gid) |
|
129
|
|
|
{ |
|
130
|
|
|
$notice_item=DB::table("group_notice")->where(["gid"=>$gid])->first(); |
|
131
|
|
|
if (empty($notice_item)) { |
|
132
|
|
|
return []; |
|
133
|
|
|
} |
|
134
|
|
|
$notice_author=DB::table("users")->where(["id"=>$notice_item["uid"]])->first(); |
|
135
|
|
|
$notice_item["name"]=$notice_author["name"]; |
|
136
|
|
|
$notice_item["avatar"]=$notice_author["avatar"]; |
|
137
|
|
|
$notice_item["post_date_parsed"]=$this->formatPostTime($notice_item["post_date"]); |
|
138
|
|
|
$notice_item["content_parsed"]=clean(Markdown::convertToHtml($notice_item["content"])); |
|
139
|
|
|
return $notice_item; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function judgeClearance($gid, $uid) |
|
143
|
|
|
{ |
|
144
|
|
|
$ret=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->first(); |
|
145
|
|
|
return empty($ret) ? -3 : $ret["role"]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function changeClearance($uid, $gid, $clearance) |
|
149
|
|
|
{ |
|
150
|
|
|
return DB::table("group_member")->where([ |
|
151
|
|
|
"uid"=>$uid, |
|
152
|
|
|
"gid"=>$gid |
|
153
|
|
|
])->update([ |
|
154
|
|
|
"role"=>$clearance |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function removeClearance($uid, $gid) |
|
159
|
|
|
{ |
|
160
|
|
|
return DB::table("group_member")->where([ |
|
161
|
|
|
"uid"=>$uid, |
|
162
|
|
|
"gid"=>$gid |
|
163
|
|
|
])->delete(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function addClearance($uid, $gid, $clearance) |
|
167
|
|
|
{ |
|
168
|
|
|
return DB::table("group_member")->insert([ |
|
169
|
|
|
"uid"=>$uid, |
|
170
|
|
|
"gid"=>$gid, |
|
171
|
|
|
"role"=>$clearance, |
|
172
|
|
|
"join_time"=>date("Y-m-d H:i:s") |
|
173
|
|
|
]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function isMember($gid, $uid) |
|
177
|
|
|
{ |
|
178
|
|
|
return DB::table("group_member")->where([ |
|
179
|
|
|
"gid"=> $gid, |
|
180
|
|
|
"uid"=> $uid |
|
181
|
|
|
])->where("role", ">", 0)->count(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function formatPostTime($date) |
|
185
|
|
|
{ |
|
186
|
|
|
$periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
|
187
|
|
|
$lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
|
188
|
|
|
|
|
189
|
|
|
$now=time(); |
|
190
|
|
|
$unix_date=strtotime($date); |
|
191
|
|
|
|
|
192
|
|
|
if (empty($unix_date)) { |
|
193
|
|
|
return "Bad date"; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ($now>$unix_date) { |
|
197
|
|
|
$difference=$now-$unix_date; |
|
198
|
|
|
$tense="ago"; |
|
199
|
|
|
} else { |
|
200
|
|
|
$difference=$unix_date-$now; |
|
201
|
|
|
$tense="from now"; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
|
205
|
|
|
$difference/=$lengths[$j]; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$difference=round($difference); |
|
209
|
|
|
|
|
210
|
|
|
if ($difference!=1) { |
|
211
|
|
|
$periods[$j].="s"; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return "$difference $periods[$j] {$tense}"; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|