Completed
Push — master ( 922be9...285e65 )
by Nick
41s queued 31s
created

COMMENT   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Test Coverage

Coverage 38.37%

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 217
ccs 33
cts 86
cp 0.3837
rs 9.92
c 0
b 0
f 0
wmc 31

18 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 1 1
A user_id() 0 1 1
A epobject_id() 0 1 1
A comments_enabled() 0 1 1
A modflagged() 0 1 1
A firstname() 0 1 1
A lastname() 0 1 1
A visible() 0 1 1
A url() 0 1 1
A comment_id() 0 1 1
A posted() 0 1 1
A body() 0 1 1
A display() 0 18 1
A __construct() 0 42 4
A _set_username() 0 13 4
A delete() 0 26 3
A _set_url() 0 24 3
A set_modflag() 0 34 4
1
<?php
2
3
/* A class for doing things with single comments.
4
5
    To access stuff about an existing comment you can do something like:
6
        $COMMENT = new COMMENT(37);
7
        $COMMENT->display();
8
    Where '37' is the comment_id.
9
10
    To create a new comment you should get a $data array prepared of
11
    the key/value pairs needed to create a new comment and do:
12
        $COMMENT = new COMMENT;
13
        $COMMENT->create ($data);
14
15
    You can delete a comment by doing $COMMENT->delete() (it isn't actually
16
    deleted from the database, just set to invisible.
17
18
    You can also do $COMMENT->set_modflag() which happens when a user
19
    posts a report about a comment. The flag is unset when/if the report is
20
    rejected.
21
22
*/
23
24
25
26
class COMMENT {
27
28
    public $comment_id = '';
29
    public $user_id = '';
30
    public $epobject_id = '';
31
    public $body = '';
32
    public $posted = '';
33
    public $visible = false;
34
    public $modflagged = NULL;	// Is a datetime when set.
35
    public $firstname = '';	// Of the person who posted it.
36
    public $lastname = '';
37
    public $url = '';
38
39
    // So that after trying to init a comment, we can test for
40
    // if it exists in the DB.
41
    public $exists = false;
42
43
44 3
    public function __construct($comment_id='') {
45
46 3
        $this->db = new ParlDB;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
48
        // Set in init.php
49 3
        if (ALLOWCOMMENTS == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition ALLOWCOMMENTS == true is always true.
Loading history...
50 3
            $this->comments_enabled = true;
0 ignored issues
show
Bug Best Practice introduced by
The property comments_enabled does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
        } else {
52
            $this->comments_enabled = false;
53
        }
54
55
56 3
        if (is_numeric($comment_id)) {
57
            // We're getting the data for an existing comment from the DB.
58
59 3
            $q = $this->db->query("SELECT user_id,
60
                                    epobject_id,
61
                                    body,
62
                                    posted,
63
                                    visible,
64
                                    modflagged
65
                            FROM	comments
66
                            WHERE 	comment_id=:comment_id",
67 3
                    array(':comment_id' => $comment_id))->first();
68
69 3
            if ($q) {
70
71 3
                $this->comment_id 	= $comment_id;
72 3
                $this->user_id		= $q['user_id'];
73 3
                $this->epobject_id	= $q['epobject_id'];
74 3
                $this->body			= $q['body'];
75 3
                $this->posted		= $q['posted'];
76 3
                $this->visible		= $q['visible'];
77 3
                $this->modflagged	= $q['modflagged'];
78
79
                // Sets the URL and username for this comment. Duh.
80 3
                $this->_set_url();
81 3
                $this->_set_username();
82
83 3
                $this->exists = true;
84
            } else {
85
                $this->exists = false;
86
            }
87
        }
88 3
    }
89
90
91
    // Use these for accessing the object's variables externally.
92
    public function comment_id() { return $this->comment_id; }
93
    public function user_id() { return $this->user_id; }
94
    public function epobject_id() { return $this->epobject_id; }
95
    public function body() { return $this->body; }
96
    public function posted() { return $this->posted; }
97
    public function visible() { return $this->visible; }
98
    public function modflagged() { return $this->modflagged; }
99
    public function exists() { return $this->exists; }
100
    public function firstname() { return $this->firstname; }
101
    public function lastname() { return $this->lastname; }
102
    public function url() { return $this->url; }
103
104
    public function comments_enabled() { return $this->comments_enabled; }
105
106
107
    public function display($format='html', $template='comments') {
108
109
        $data['comments'][0] = array (
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
110
            'comment_id'	=> $this->comment_id,
111
            'user_id'		=> $this->user_id,
112
            'epobject_id'	=> $this->epobject_id,
113
            'body'			=> $this->body,
114
            'posted'		=> $this->posted,
115
            'modflagged'	=> $this->modflagged,
116
            'url'			=> $this->url,
117
            'firstname'		=> $this->firstname,
118
            'lastname'		=> $this->lastname,
119
            'visible'		=> $this->visible,
120
        );
121
122
        // Use the same renderer as the COMMENTLIST class.
123
        $COMMENTLIST = new COMMENTLIST();
124
        $COMMENTLIST->render($data, $format, $template);
125
126
    }
127
128
129
    public function set_modflag($switch) {
130
        // $switch is either 'on' or 'off'.
131
        // The comment's modflag goes to on when someone reports the comment.
132
        // It goes to off when a commentreport has been resolved but the
133
        // comment HASN'T been deleted.
134
        global $PAGE;
135
136
        if ($switch == 'on') {
137
            $date = gmdate("Y-m-d H:i:s");
138
            $flag = "'$date'";
139
140
        } elseif ($switch == 'off') {
141
            $date = NULL;
142
            $flag = 'NULL';
143
144
        } else {
145
            $PAGE->error_message ("Why are you trying to switch this comment's modflag to '" . _htmlentities($switch) . "'!");
146
        }
147
148
        $q = $this->db->query("UPDATE comments
149
                        SET		modflagged = $flag
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $flag does not seem to be defined for all execution paths leading up to this point.
Loading history...
150
                        WHERE 	comment_id = '" . $this->comment_id . "'
151
                        ");
152
153
        if ($q->success()) {
154
            $this->modflagged = $date;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date does not seem to be defined for all execution paths leading up to this point.
Loading history...
155
            return true;
156
        } else {
157
            $message = array (
158
                'title' => 'Sorry',
159
                'text' => "We couldn't update the annotation's modflag."
160
            );
161
            $PAGE->error_message($message);
162
            return false;
163
        }
164
165
    }
166
167
168
    public function delete() {
169
        // Mark the comment as invisible.
170
171
        global $THEUSER, $PAGE;
172
173
        if ($THEUSER->is_able_to('deletecomment')) {
174
            $q = $this->db->query("UPDATE comments SET visible = '0' WHERE comment_id = '" . $this->comment_id . "'");
175
176
            if ($q->success()) {
177
                return true;
178
            } else {
179
                $message = array (
180
                    'title' => 'Sorry',
181
                    'text' => "We were unable to delete the annotation."
182
                );
183
                $PAGE->error_message($message);
184
                return false;
185
            }
186
187
        } else {
188
            $message = array (
189
                'title' => 'Sorry',
190
                'text' => "You are not authorised to delete annotations."
191
            );
192
            $PAGE->error_message($message);
193
            return false;
194
        }
195
196
    }
197
198
199
200 3
    public function _set_url() {
201 3
        global $hansardmajors;
202
        // Creates and sets the URL for the comment.
203
204 3
        if ($this->url == '') {
205
206 3
            $q = $this->db->query("SELECT major,
207
                                    gid
208
                            FROM	hansard
209
                            WHERE	epobject_id = :epobject_id",
210 3
                            array(':epobject_id' => $this->epobject_id))->first();
211
212 3
            if ($q) {
213
                 // If you change stuff here, you might have to change it in
214
                 // $COMMENTLIST->_get_comment_data() too...
215
216
                $gid = fix_gid_from_db($q['gid']); // In includes/utility.php
217
218
                $major = $q['major'];
219
                $page = $hansardmajors[$major]['page'];
220
221
                $URL = new \MySociety\TheyWorkForYou\Url($page);
222
                $URL->insert(array('id'=>$gid));
223
                $this->url = $URL->generate() . '#c' . $this->comment_id;
224
            }
225
        }
226 3
    }
227
228
229
230 3
    public function _set_username() {
231
        // Gets and sets the user's name who posted the comment.
232
233 3
        if ($this->firstname == '' && $this->lastname == '') {
234 3
            $q = $this->db->query("SELECT firstname,
235
                                    lastname
236
                            FROM	users
237
                            WHERE	user_id = :user_id",
238 3
                            array(':user_id' => $this->user_id))->first();
239
240 3
            if ($q) {
241 3
                $this->firstname = $q['firstname'];
242 3
                $this->lastname = $q['lastname'];
243
            }
244
        }
245 3
    }
246
247
248
249
250
}
251