Passed
Push — master ( d35d45...a409d2 )
by
unknown
09:26
created

GroupModel::userGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 changeGroupName($gid, $GroupName)
80
    {
81
        return DB::table("group")->where('gid',$gid)->update([
82
            "name"=>$GroupName
83
        ]);
84
    }
85
86
    public function changeJoinPolicy($gid, $JoinPolicy){
87
        return DB::table("group")->where('gid',$gid)->update([
88
            "join_policy"=>$JoinPolicy
89
        ]);
90
    }
91
92
    public function details($gcode)
93
    {
94
        $basic_info=DB::table($this->tableName)->where(["gcode"=>$gcode])->first();
95
        if(empty($basic_info)) return [];
96
        $basic_info["members"]=$this->countGroupMembers($basic_info["gid"]);
97
        $basic_info["tags"]=$this->getGroupTags($basic_info["gid"]);
98
        $basic_info["create_time_foramt"]=date_format(date_create($basic_info["create_time"]), 'M jS, Y');
99
        $basic_info["contest_stat"]=$this->countGroupContest($basic_info["gid"]);
100
        return $basic_info;
101
    }
102
103
    public function joinPolicy($gid)
104
    {
105
        $ret=DB::table($this->tableName)->where(["gid"=>$gid])->first();
106
        return empty($ret) ? null : $ret["join_policy"];
107
    }
108
109
    public function userProfile($uid, $gid)
110
    {
111
        $info=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->where("role", ">", 0)->first();
112
        if (!empty($info)) {
113
            $info["role_parsed"]=$this->role[$info["role"]];
114
        }
115
        return $info;
116
    }
117
118
    public function userList($gid)
119
    {
120
        $user_list=DB::table("group_member")->join(
121
            "users",
122
            "users.id",
123
            "=",
124
            "group_member.uid"
125
        )->where(["gid"=>$gid])->orderBy('role', 'desc')->select(
126
            "role",
127
            "uid",
128
            "name",
129
            "nick_name",
130
            "avatar",
131
            "sub_group"
132
        )->get()->all();
133
        foreach ($user_list as &$u) {
134
            $u["role_parsed"]=$this->role[$u["role"]];
135
            $u["role_color"]=$this->role_color[$u["role"]];
136
            if(is_null($u["sub_group"])) $u["sub_group"]="None";
137
        }
138
        return $user_list;
139
    }
140
141
    public function groupNotice($gid)
142
    {
143
        $notice_item=DB::table("group_notice")->where(["gid"=>$gid])->first();
144
        if (empty($notice_item)) {
145
            return [];
146
        }
147
        $notice_author=DB::table("users")->where(["id"=>$notice_item["uid"]])->first();
148
        $notice_item["name"]=$notice_author["name"];
149
        $notice_item["avatar"]=$notice_author["avatar"];
150
        $notice_item["post_date_parsed"]=$this->formatPostTime($notice_item["post_date"]);
151
        $notice_item["content_parsed"]=clean(Markdown::convertToHtml($notice_item["content"]));
152
        return $notice_item;
153
    }
154
155
    public function judgeClearance($gid, $uid)
156
    {
157
        $ret=DB::table("group_member")->where(["gid"=>$gid, "uid"=>$uid])->first();
158
        return empty($ret) ? -3 : $ret["role"];
159
    }
160
161
    public function changeClearance($uid, $gid, $clearance)
162
    {
163
        return DB::table("group_member")->where([
164
            "uid"=>$uid,
165
            "gid"=>$gid
166
        ])->update([
167
            "role"=>$clearance
168
        ]);
169
    }
170
171
    public function removeClearance($uid, $gid)
172
    {
173
        return DB::table("group_member")->where([
174
            "uid"=>$uid,
175
            "gid"=>$gid
176
        ])->delete();
177
    }
178
179
    public function addClearance($uid, $gid, $clearance)
180
    {
181
        return DB::table("group_member")->insert([
182
            "uid"=>$uid,
183
            "gid"=>$gid,
184
            "role"=>$clearance,
185
            "join_time"=>date("Y-m-d H:i:s")
186
        ]);
187
    }
188
189
    public function isMember($gid, $uid)
190
    {
191
        return DB::table("group_member")->where([
192
            "gid"=> $gid,
193
            "uid"=> $uid
194
        ])->where("role", ">", 0)->count();
195
    }
196
197
    public function formatPostTime($date)
198
    {
199
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
200
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
201
202
        $now=time();
203
        $unix_date=strtotime($date);
204
205
        if (empty($unix_date)) {
206
            return "Bad date";
207
        }
208
209
        if ($now>$unix_date) {
210
            $difference=$now-$unix_date;
211
            $tense="ago";
212
        } else {
213
            $difference=$unix_date-$now;
214
            $tense="from now";
215
        }
216
217
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
218
            $difference/=$lengths[$j];
219
        }
220
221
        $difference=round($difference);
222
223
        if ($difference!=1) {
224
            $periods[$j].="s";
225
        }
226
227
        return "$difference $periods[$j] {$tense}";
228
    }
229
}
230