GameSubmissionDAO   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLatestSubmissions() 0 62 2
A __construct() 0 2 1
A getGameSubmissionCount() 0 29 2
B getGameSubmissionQuery() 0 45 11
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Game/GameSubmission.php" ;
6
7
use AL\Common\Model\GameSubmission;
8
9
/**
10
 * DAO for Game Submissions
11
 */
12
class GameSubmissionDAO {
13
    private $mysqli;
14
15
    public function __construct($mysqli) {
16
        $this->mysqli = $mysqli;
17
    }
18
    
19
    private function getGameSubmissionQuery($user_id = null, $last_timestamp = null, $action = null, $done = null) {
20
            
21
        $query =  "SELECT
22
                game.game_id,
23
                game.game_name,
24
                game_submitinfo.timestamp,
25
                game_submitinfo.timestamp as date,
26
                game_submitinfo.submit_text,
27
                game_submitinfo.game_submitinfo_id,
28
                game_submitinfo.game_done,
29
                users.user_id,
30
                users.userid,
31
                users.email,
32
                users.join_date,
33
                users.karma,
34
                users.show_email,
35
                users.avatar_ext,
36
                (SELECT COUNT(*) FROM game_submitinfo 
37
                    WHERE game_submitinfo.user_id = users.user_id) AS user_subm_count,
38
                (SELECT COUNT(*) FROM comments 
39
                    WHERE comments.user_id = users.user_id) AS user_comment_count               
40
                FROM game_submitinfo
41
                LEFT JOIN game ON (game_submitinfo.game_id = game.game_id)
42
                LEFT JOIN users ON (game_submitinfo.user_id = users.user_id)";
43
44
        if (isset($done) and $done == "1") {
45
            $query .= " WHERE game_done = '1'";
46
        } elseif (isset($done) and $done == "2") {
47
            $query .= " WHERE game_done <> '1'";
48
        } else {
49
            $query .= " WHERE ( game_done <> '1' or game_done = '1')";
50
        }
51
52
        if (isset($user_id) and $user_id <> '') {
53
            $query .= " AND users.user_id = $user_id";
54
        }
55
56
        if (isset($action) and $action=="autoload") {
57
            $query .= " AND game_submitinfo.timestamp < $last_timestamp ";
58
        } elseif (isset($action) and $action=="search") {
59
            $query .= " AND game_submitinfo.timestamp <= $last_timestamp ";
60
        }
61
62
        $query .= " ORDER BY game_submitinfo.timestamp DESC LIMIT 10";
63
        return $query;
64
    }
65
    
66
    /**
67
     * Return the latest game submissions, sorted by descending date
68
     *
69
     * @return \AL\Common\Model\Game\GameSubmission[] An array of news
70
     */
71
    public function getLatestSubmissions($user_id = null, $last_timestamp = null, $action = null, $done = null) {
72
        $stmt = \AL\Db\execute_query(
73
            "GameSubmissionDAO: getLatestSubmissions",
74
            $this->mysqli,
75
            $this->getGameSubmissionQuery($user_id, $last_timestamp, $action, $done),
76
            null, null
77
        );
78
79
        \AL\Db\bind_result(
80
            "GameSubmissionDAO: getLatestSubmissions",
81
            $stmt,
82
            $game_id,
83
            $game_name,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $game_name seems to be never defined.
Loading history...
84
            $timestamp,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $timestamp does not exist. Did you maybe mean $last_timestamp?
Loading history...
85
            $date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
86
            $comment,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $comment seems to be never defined.
Loading history...
87
            $submission_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $submission_id does not exist. Did you maybe mean $submission?
Loading history...
88
            $done,
89
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid does not exist. Did you maybe mean $user_id?
Loading history...
90
            $username,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username seems to be never defined.
Loading history...
91
            $email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
92
            $join_date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join_date seems to be never defined.
Loading history...
93
            $karma,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $karma seems to be never defined.
Loading history...
94
            $show_email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $show_email seems to be never defined.
Loading history...
95
            $avatar_ext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $avatar_ext seems to be never defined.
Loading history...
96
            $user_subm_count,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_subm_count seems to be never defined.
Loading history...
97
            $user_comment_count
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_comment_count does not exist. Did you maybe mean $comment?
Loading history...
98
        );
99
100
        $submissions = [];
101
        while ($stmt->fetch()) {
102
            $comment = nl2br($comment);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $comment does not seem to be defined for all execution paths leading up to this point.
Loading history...
103
            $comment = InsertALCode($comment);
104
            $comment = trim($comment);
105
            $comment = RemoveSmillies($comment);
106
            
107
            $submission = new \AL\Common\Model\GameSubmission\GameSubmission(
108
                $game_id,
109
                $game_name,
110
                $timestamp,
111
                $date,
112
                $comment,
113
                $submission_id,
114
                $done,
115
                $userid,
116
                $username,
117
                $email,
118
                $join_date,
119
                $karma,
120
                $show_email,
121
                $avatar_ext,
122
                $user_subm_count,
123
                $user_comment_count,
124
                null
125
            );
126
            
127
            $submissions[] = $submission;
128
        }
129
130
        $stmt->close();
131
132
        return $submissions;
133
    }
134
    
135
     /**
136
    * Get the total count of submissions
137
    *
138
    * @param  integer $user_id Optional ID of a user to count comments for
139
    * @return integer Number of comments     */
140
    public function getGameSubmissionCount($user_id = null) {
141
        if (isset($user_id)) {
142
            $stmt = \AL\Db\execute_query(
143
                "GameSubmissionDAO: Get submission count for user_id $user_id",
144
                $this->mysqli,
145
                "SELECT COUNT(*) FROM game_submitinfo WHERE user_id = ?",
146
                "i",
147
                $user_id
148
            );
149
        } else {
150
            $stmt = \AL\Db\execute_query(
151
                "GameSubmissionDAO:  Get submissions count in database",
152
                $this->mysqli,
153
                "SELECT COUNT(*) FROM game_submitinfo",
154
                null,
155
                null
156
            );
157
        }
158
159
        \AL\Db\bind_result(
160
            "GameSubmissionDAO: Get submission count",
161
            $stmt,
162
            $count
163
        );
164
165
        $stmt->fetch();
166
        $stmt->close();
167
168
        return $count;
169
    }
170
    
171
    
172
    /**
173
    * Get the submission screenshots
174
    *
175
    * @param  integer $submission_id
176
    * @return screenshots links    */
177
    /*public function getGameSubmissionScreenshots($submission_id = null) {
178
        $stmt = \AL\Db\execute_query(
179
            "GameSubmissionDAO: Get the screenshots of the submission",
180
            $this->mysqli,
181
            "SELECT screenshot_id, imgext FROM screenshot_main
182
                LEFT JOIN screenshot_game_submitinfo
183
                    ON (screenshot_main.screenshot_id = screenshot_game_submitinfo.screenshot_id)
184
                WHERE screenshot_game_submitinfo.game_submitinfo_id  = ?",
185
            "i",
186
            $submission_id
187
        );
188
189
        \AL\Db\bind_result(
190
            "GameSubmissionDAO: Get the screenshots of the submission",
191
            $stmt,
192
            $screenshot_id,
193
            $imgext
194
        );
195
196
        $screenshots = [];
197
        while ($stmt->fetch()) {
198
            $new_path = $game_submit_screenshot_path;
199
            $new_path .= $screenshot_id;
200
            $new_path .= ".";
201
            $new_path .= $imgext;
202
            $screenshots[] = new \AL\Common\Model\GameSubmission\GameSubmission(
203
                null,
204
                null,
205
                null,
206
                null,
207
                null,
208
                null,
209
                null,
210
                null,
211
                null,
212
                null,
213
                null,
214
                null,
215
                null,
216
                null,
217
                null,
218
                $new_path
219
            );
220
        }
221
222
        $stmt->close();
223
224
        return $screenshots;
225
    }*/
226
}
227