-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.php
258 lines (136 loc) · 4.49 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
include('config.php');
class db {
////////// CREATE THE CONNECTION
private $conn = Null;
function __construct( $servername, $username, $password, $dbname ) {
// Create connection
$this->conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$this->conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
//echo("Connection Succesful");
}
}
////////// GENERAL HELPERS
public function safe_select( $sql ) {
$result = $this->conn->query( $sql );
if ($result->num_rows > 0) {
$data = [];
while($row = $result->fetch_assoc()) {
array_push($data, $row);
}
return $data;
} else {
return false;
}
}
function test() {
echo "test";
}
////////// FORM SUBMISSION FUNCTIONS
function submit_all( $form ) {
//$this->test();
$this->submit_count( $form['doc_id'] );
$this->submit_polls( $form['doc_id'], $form['google'], $form['salary'] );
$this->submit_metrics( $form['doc_id'], $form['time_spent'], $form['histo'] );
if( isset( $form['links']) ) {
$this->submit_links( $form['doc_id'], $form['links'] );
}
}
protected function submit_count( $doc_id ) {
$sql = "UPDATE resume SET submit_count = submit_count + 1 WHERE id = {$doc_id};";
$this->conn->query( $sql );
}
protected function submit_polls( $doc_id, $google, $salary ) {
$sql = "INSERT INTO resumator.polls (doc_id, question, response) VALUES";
$sql .= "(" . $doc_id . " , 'Would google hire this person?', '" . $google . "'),";
$sql .= "(" . $doc_id . " , 'What would their salary be?', '" . $salary . "');";
$this->conn->query( $sql );
}
protected function submit_metrics( $doc_id, $time_spent, $histo ) {
$sql = "INSERT INTO resumator.metrics (doc_id, time_spent, histo)";
$sql .= "VALUES (" . $doc_id . " , " . $time_spent . ", '" . $histo . "');";
$this->conn->query( $sql );
}
protected function submit_links( $doc_id, $links ) {
foreach ( array_keys($links) as $key ) {
$sql = "SELECT * FROM links WHERE name = '" . $key . "' LIMIT 1;";
$result = db::safe_select( $sql )[0];
if( is_array( $result) ) {
//print_r($result);
$clicks = $result['clicks'] + $links[$key];
$sql = "UPDATE resumator.links SET clicks = " . $clicks . " WHERE links.link_id = " . $result['link_id'] . ";";
} else {
$sql = "INSERT INTO resumator.links (doc_id, name, clicks) VALUES";
$sql .= "(" . $doc_id . ",'" . $key . "'," . $links[$key] . ");";
}
}
$this->conn->query( $sql );
}
/////////// DATABASE DATA FUNCTIONS
function select_doc( $doc_id ) {
$data = [];
$data['histo'] = $this->select_histo( $doc_id );
$data['polls'] = $this->select_polls( $doc_id );
$data['time_spent'] = $this->select_time_spent( $doc_id );
$data['links'] = $this->select_links( $doc_id );
//print_r( $data );
return $data;
}
public function select_histo( $doc_id ) {
$histo = "";
$sql = "SELECT histo FROM metrics WHERE doc_id = {$doc_id};";
$result = db::safe_select( $sql );
if( is_array($result) ) {
foreach( $result as $val) {
$histo .= $val['histo'];
}
} else {
die("histo could not be selected");
}
return $histo;
}
public function select_polls( $doc_id ) {
$polls = "";
$sql = "SELECT * FROM polls WHERE doc_id = {$doc_id};";
$result = db::safe_select( $sql );
//print_r( $result );
if( is_array($result) ) {
foreach( $result as $val ) {
$question = $val['question'];
$response = $val['response'];
if( isset($polls[ $question ][ $response ]) ) {
$polls[ $question ][ $response ] += 1;
} else {
$polls[ $question ][ $response ] = 1;
}
}
} else {
die("polls could not be selected");
}
return $polls;
}
public function select_time_spent( $doc_id ) {
$sql = "SELECT avg(time_spent) FROM metrics WHERE doc_id = {$doc_id};";
$result = db::safe_select( $sql );
return $result[0]['avg(time_spent)'];
}
public function select_links( $doc_id ) {
$links = [];
$sql = "SELECT submit_count FROM resume WHERE id = {$doc_id}";
$result = db::safe_select( $sql );
$total = $result[0]['submit_count'];
$sql = "SELECT * FROM links WHERE doc_id = {$doc_id}";
$result = db::safe_select( $sql );
if( $result != false ) {
foreach( $result as $val ) {
$links[ $val['name'] ] = $val['clicks'] / $total;
}
}
//print_r( $links );
return $links;
}
}
$db = new db( $servername, $username, $password, $dbname );