Total Complexity | 82 |
Total Lines | 597 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 6 | Features | 1 |
Complex classes like GroupModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GroupModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class GroupModel extends Model |
||
14 | { |
||
15 | protected $tableName='group'; |
||
16 | protected $table='group'; |
||
17 | protected $primaryKey='gid'; |
||
18 | const DELETED_AT=null; |
||
19 | const UPDATED_AT=null; |
||
20 | const CREATED_AT=null; |
||
21 | |||
22 | /* |
||
23 | join_policy: |
||
24 | 1:a user can join this group only by invitation |
||
25 | 2:a user can join this group only by application |
||
26 | 3:a user can join this group by both invitation and application |
||
27 | */ |
||
28 | public $role=[ |
||
29 | "-3"=>"None", |
||
30 | "-1"=>"Invited", |
||
31 | "0"=>"Pending", |
||
32 | "1"=>"Member", |
||
33 | "2"=>"Manager", |
||
34 | "3"=>"Leader" |
||
35 | ]; |
||
36 | public $role_color=[ |
||
37 | "-3"=>"wemd-black", |
||
38 | "-1"=>"wemd-deep-purple", |
||
39 | "0"=>"wemd-red", |
||
40 | "1"=>"wemd-grey", |
||
41 | "2"=>"wemd-light-blue", |
||
42 | "3"=>"wemd-amber" |
||
43 | ]; |
||
44 | |||
45 | public function trendingGroups() |
||
46 | { |
||
47 | return Cache::tags(['group'])->get('trending'); |
||
48 | } |
||
49 | |||
50 | public function gid($gcode) |
||
51 | { |
||
52 | return DB::table($this->tableName)->where(["gcode"=>$gcode])->first()["gid"]; |
||
53 | } |
||
54 | |||
55 | public function cacheTrendingGroups() |
||
56 | { |
||
57 | $trending_groups=DB::table($this->tableName)->where(["public"=>1])->orderBy('create_time', 'desc')->select("gid", "gcode", "img", "name", "verified")->get()->all(); |
||
58 | foreach ($trending_groups as &$t) { |
||
59 | $t["members"]=$this->countGroupMembers($t["gid"]); |
||
60 | } |
||
61 | usort($trending_groups, function ($a, $b) { |
||
62 | return $b["members"]<=>$a["members"]; |
||
63 | }); |
||
64 | Cache::tags(['group'])->put('trending', array_slice($trending_groups,0,12), 3600*24); |
||
65 | } |
||
66 | |||
67 | public function userGroups($uid) |
||
68 | { |
||
69 | $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(); |
||
70 | foreach ($user_groups as &$m) { |
||
71 | $m["members"]=$this->countGroupMembers($m["gid"]); |
||
72 | } |
||
73 | return $user_groups; |
||
74 | } |
||
75 | |||
76 | public function countGroupMembers($gid) |
||
77 | { |
||
78 | return DB::table("group_member")->where(["gid"=>$gid])->count(); |
||
79 | } |
||
80 | |||
81 | public function getGroupTags($gid) |
||
82 | { |
||
83 | return DB::table("group_tag")->where(["gid"=>$gid])->select("tag")->get()->all(); |
||
84 | } |
||
85 | |||
86 | public function countGroupContest($gid) |
||
87 | { |
||
88 | return [ |
||
89 | "contest_ahead" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", ">", DB::raw("now()"))->count(), |
||
90 | "contest_going" => DB::table("contest")->where(["gid"=>$gid])->where("begin_time", "<=", DB::raw("now()"))->where("end_time", ">=", DB::raw("now()"))->count(), |
||
91 | "contest_end" => DB::table("contest")->where(["gid"=>$gid])->where("end_time", "<", DB::raw("now()"))->count() |
||
92 | ]; |
||
93 | } |
||
94 | |||
95 | public function changeNickName($gid, $uid, $nickName) |
||
99 | ]); |
||
100 | } |
||
101 | |||
102 | public function changeGroupName($gid, $GroupName) |
||
103 | { |
||
104 | return DB::table("group")->where('gid',$gid)->update([ |
||
105 | "name"=>$GroupName |
||
106 | ]); |
||
107 | } |
||
108 | |||
109 | public function changeJoinPolicy($gid, $JoinPolicy){ |
||
112 | ]); |
||
113 | } |
||
114 | |||
115 | public function details($gcode) |
||
124 | } |
||
125 | |||
126 | public function joinPolicy($gid) |
||
127 | { |
||
128 | $ret=DB::table($this->tableName)->where(["gid"=>$gid])->first(); |
||
129 | return empty($ret) ? null : $ret["join_policy"]; |
||
130 | } |
||
131 | |||
132 | public function userProfile($uid, $gid) |
||
133 | { |
||
134 | $info=DB::table("group_member") |
||
135 | ->join('users','users.id','=','group_member.uid') |
||
136 | ->where(["gid"=>$gid, "uid"=>$uid]) |
||
137 | ->where("role", ">", 0) |
||
138 | ->select('avatar','describes','email','gid','uid','name','nick_name','professional_rate','role','sub_group') |
||
139 | ->first(); |
||
140 | if (!empty($info)) { |
||
141 | $info["role_parsed"]=$this->role[$info["role"]]; |
||
142 | $info["role_color"]=$this->role_color[$info["role"]]; |
||
143 | } |
||
144 | return $info; |
||
145 | } |
||
146 | |||
147 | public function userList($gid) |
||
148 | { |
||
149 | $user_list=DB::table("group_member")->join( |
||
150 | "users", |
||
151 | "users.id", |
||
152 | "=", |
||
153 | "group_member.uid" |
||
154 | )->where(["gid"=>$gid])->orderBy('role', 'desc')->select( |
||
155 | "role", |
||
156 | "uid", |
||
157 | "name", |
||
158 | "nick_name", |
||
159 | "avatar", |
||
160 | "sub_group", |
||
161 | "ranking" |
||
162 | )->get()->all(); |
||
163 | foreach ($user_list as &$u) { |
||
164 | $u["role_parsed"]=$this->role[$u["role"]]; |
||
165 | $u["role_color"]=$this->role_color[$u["role"]]; |
||
166 | if(is_null($u["sub_group"])) $u["sub_group"]="None"; |
||
167 | } |
||
168 | return $user_list; |
||
169 | } |
||
170 | |||
171 | public function groupNotice($gid) |
||
172 | { |
||
173 | $notice_item=DB::table("group_notice")->where(["gid"=>$gid])->first(); |
||
174 | if (empty($notice_item)) { |
||
175 | return []; |
||
176 | } |
||
177 | $notice_author=DB::table("users")->where(["id"=>$notice_item["uid"]])->first(); |
||
178 | $notice_item["name"]=$notice_author["name"]; |
||
179 | $notice_item["avatar"]=$notice_author["avatar"]; |
||
180 | $notice_item["post_date_parsed"]=$this->formatPostTime($notice_item["post_date"]); |
||
181 | $notice_item["content_parsed"]=clean(convertMarkdownToHtml($notice_item["content"])); |
||
182 | return $notice_item; |
||
183 | } |
||
184 | |||
185 | public function judgeClearance($gid, $uid) |
||
186 | { |
||
187 | $ret=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->first(); |
||
188 | return empty($ret) ? -3 : $ret["role"]; |
||
189 | } |
||
190 | |||
191 | public function changeClearance($uid, $gid, $clearance) |
||
192 | { |
||
193 | return DB::table("group_member")->where([ |
||
194 | "uid"=>$uid, |
||
195 | "gid"=>$gid |
||
196 | ])->update([ |
||
197 | "role"=>$clearance |
||
198 | ]); |
||
199 | } |
||
200 | |||
201 | public function removeClearance($uid, $gid) |
||
202 | { |
||
203 | return DB::table("group_member")->where([ |
||
204 | "uid"=>$uid, |
||
205 | "gid"=>$gid |
||
206 | ])->delete(); |
||
207 | } |
||
208 | |||
209 | public function addClearance($uid, $gid, $clearance) |
||
210 | { |
||
211 | return DB::table("group_member")->insert([ |
||
212 | "uid"=>$uid, |
||
213 | "gid"=>$gid, |
||
214 | "role"=>$clearance, |
||
215 | "join_time"=>date("Y-m-d H:i:s") |
||
216 | ]); |
||
217 | } |
||
218 | |||
219 | public function isMember($gid, $uid) |
||
220 | { |
||
221 | return DB::table("group_member")->where([ |
||
222 | "gid"=> $gid, |
||
223 | "uid"=> $uid |
||
224 | ])->where("role", ">", 0)->count(); |
||
225 | } |
||
226 | |||
227 | public function problemTags($gid,$pid = -1) |
||
251 | } |
||
252 | |||
253 | public function problems($gid) |
||
254 | { |
||
255 | $contestModel = new ContestModel(); |
||
256 | $problems = DB::table('contest_problem') |
||
257 | ->join('contest','contest_problem.cid', '=', 'contest.cid') |
||
258 | ->join('problem','contest_problem.pid', '=', 'problem.pid' ) |
||
259 | ->select('contest_problem.cid as cid', 'problem.pid as pid', 'pcode', 'title') |
||
260 | ->where('contest.gid',$gid) |
||
261 | ->where('contest.practice',1) |
||
262 | ->orderBy('contest.create_time','desc') |
||
263 | ->distinct() |
||
264 | ->get()->all(); |
||
265 | $user_id = Auth::user()->id; |
||
266 | foreach($problems as $key => $value){ |
||
267 | if($contestModel->judgeClearance($value['cid'],$user_id) != 3){ |
||
268 | unset($problems[$key]); |
||
269 | }else{ |
||
270 | $problems[$key]['tags'] = $this->problemTags($gid,$value['pid']); |
||
271 | } |
||
272 | } |
||
273 | return $problems; |
||
274 | } |
||
275 | |||
276 | public function problemAddTag($gid,$pid,$tag) |
||
277 | { |
||
278 | return DB::table("group_problem_tag")->insert([ |
||
279 | "gid"=>$gid, |
||
280 | "pid"=>$pid, |
||
281 | "tag"=>$tag, |
||
282 | ]); |
||
283 | } |
||
284 | |||
285 | public function problemRemoveTag($gid,$pid,$tag) |
||
292 | } |
||
293 | |||
294 | public function formatPostTime($date) |
||
295 | { |
||
296 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
297 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
298 | |||
299 | $now=time(); |
||
300 | $unix_date=strtotime($date); |
||
301 | |||
302 | if (empty($unix_date)) { |
||
303 | return "Bad date"; |
||
304 | } |
||
305 | |||
306 | if ($now>$unix_date) { |
||
307 | $difference=$now-$unix_date; |
||
308 | $tense="ago"; |
||
325 | } |
||
326 | |||
327 | public function judgeEmailClearance($gid, $email) |
||
328 | { |
||
329 | $user=DB::table("users")->where(["email"=>$email])->first(); |
||
330 | if(empty($user)) return -4; |
||
331 | $ret=DB::table("group_member")->where([ |
||
332 | "gid"=>$gid, |
||
333 | "uid"=>$user["id"], |
||
334 | ])->first(); |
||
335 | return empty($ret) ? -3 : $ret["role"]; |
||
336 | } |
||
337 | |||
338 | public function inviteMember($gid, $email) |
||
346 | ]); |
||
347 | } |
||
348 | |||
349 | public function changeGroup($uid, $gid, $sub) |
||
350 | { |
||
351 | return DB::table("group_member")->where([ |
||
352 | "uid"=>$uid, |
||
353 | "gid"=>$gid |
||
354 | ])->update([ |
||
355 | "sub_group"=>$sub |
||
356 | ]); |
||
357 | } |
||
358 | |||
359 | public function isUser($email) |
||
364 | } |
||
365 | |||
366 | public function isGroup($gcode) |
||
371 | } |
||
372 | |||
373 | public function createGroup($uid, $gcode, $img, $name, $public, $description, $join_policy) |
||
374 | { |
||
375 | $gid=DB::table("group")->insertGetId([ |
||
376 | "gcode"=>$gcode, |
||
377 | "img"=>$img, |
||
378 | "name"=>$name, |
||
379 | "public"=>$public, |
||
380 | "verified"=>0, |
||
381 | "description"=>$description, |
||
382 | "join_policy"=>$join_policy, |
||
383 | "custom_icon"=>null, |
||
384 | "custom_title"=>null, |
||
385 | "create_time"=>date("Y-m-d H:i:s") |
||
386 | ]); |
||
387 | return DB::table("group_member")->insert([ |
||
388 | "uid"=>$uid, |
||
389 | "gid"=>$gid, |
||
390 | "role"=>3, |
||
391 | "join_time"=>date("Y-m-d H:i:s") |
||
392 | ]); |
||
393 | } |
||
394 | |||
395 | public function detailNotice($gcode) |
||
396 | { |
||
397 | $group=DB::table("group")->where([ |
||
398 | "gcode"=>$gcode, |
||
399 | ])->first(); |
||
400 | return $group_notice=DB::table("group_notice")->where([ |
||
|
|||
401 | "gid"=>$group["gid"], |
||
402 | ])->first(); |
||
403 | } |
||
404 | |||
405 | public function createNotice($gid, $uid, $title, $content) |
||
416 | ]); |
||
417 | } |
||
418 | |||
419 | public function groupMemberPracticeContestStat($gid) |
||
504 | } |
||
505 | |||
506 | public function groupMemberPracticeTagStat($gid) |
||
565 | } |
||
566 | |||
567 | public function refreshElo($gid) |
||
598 | } |
||
599 | |||
600 | public function getEloChangeLog($gid,$uid) |
||
612 |