-
Notifications
You must be signed in to change notification settings - Fork 0
/
remind_feedback_and_open_bugs.php
417 lines (347 loc) · 16.7 KB
/
remind_feedback_and_open_bugs.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
function compose_remind_msg($conn, $row, $sql_21_days, $sql_14_days, $msg_begin,
$msg_end, $msg_open_bugs_warning_begin, $url){
$comb_of_bug_results = 0;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result_21_days=$conn->query($sql_21_days);
$result_14_days=$conn->query($sql_14_days);
if ( $result_21_days->num_rows > 0 && $result_14_days->num_rows > 0 ){
$comb_of_bug_results = 3;
}
else if( $result_14_days->num_rows > 0 ){
$comb_of_bug_results = 2;
}
else if( $result_21_days->num_rows > 0 ){
$comb_of_bug_results = 1;
}
if ( $comb_of_bug_results > 0 ) {
$list_of_bugs = "";
$number_of_bugs = 0;
switch ($comb_of_bug_results){
//no bugs at all;
case 0: return "";
//bugs over 21 days only
case 1:
while($row_bug=$result_21_days->fetch_assoc()){
$list_of_bugs = $list_of_bugs . $url . $row_bug["id"] ."\n";
//count core project bugs only
if( $row_bug["project_id"]==1) {
$number_of_bugs++;
}
}
$message = "Hello " . $row["realname"] . "," . $msg_begin . $list_of_bugs . $msg_end;
break;
//bugs over 14 days only
case 2:
$list_of_bugs_14 = "";
while($row_bug=$result_14_days->fetch_assoc()){
$list_of_bugs_14 = $list_of_bugs_14 . $url . $row_bug["id"] . "\n";
}
$message = "Hello " . $row["realname"] . "," . $msg_open_bugs_warning_begin . $list_of_bugs_14 . $msg_end;
break;
//bugs over 21 days and over 14 days old
case 3:
$list_of_bugs_14 = "";
while($row_bug=$result_21_days->fetch_assoc()){
$list_of_bugs = $list_of_bugs . $url . $row_bug["id"] . "\n";
//count core project bugs only
if( $row_bug["project_id"]==1) {
$number_of_bugs++;
}
}
while($row_bug=$result_14_days->fetch_assoc()){
$list_of_bugs_14 = $list_of_bugs_14 . $url . $row_bug["id"] . "\n";
}
$message = "Hello " . $row["realname"] . "," . $msg_begin . $list_of_bugs . $msg_open_bugs_warning_begin . $list_of_bugs_14 . $msg_end;
break;
}
return $message;
}
}
function count_votes($result_users_bugs, $field_id, $prefix, $conn){
$votings_array;
$votings = "";
if( $result_users_bugs->num_rows > 0 ){
while($row_bug=$result_users_bugs->fetch_assoc()){
$sql_custom_field = "SELECT * FROM mantis_custom_field_string_table WHERE bug_id='". $row_bug["id"] . "' AND field_id='". $field_id . "'";
$result_custom_field = $conn->query($sql_custom_field);
if( $result_custom_field->num_rows > 0 ){
$row_custom_field = $result_custom_field->fetch_assoc();
if(!isset($votings_array[$row_custom_field["value"]])){
$votings_array[$row_custom_field["value"]] = 0;
}
$votings_array[$row_custom_field["value"]] = $votings_array[$row_custom_field["value"]] + 1;
}
}
}
if( isset($votings_array)){
krsort($votings_array);
foreach ($votings_array as $key => $value) {
if( is_numeric($key)){
if( intval($key) != 0 ){
if($votings==""){
$votings = $prefix;
}
$votings = $votings . " " . $value . "x" . intval($key) . "v";
}
}
}
}
return $votings;
}
function get_resolved_votings( $user_id, $conn, $start_date, $twentyonedaysago ){
$field_name = "fixing_priority";
$field_id = 0;
$votings = "";
$votings_array;
$sql = "SELECT * FROM mantis_custom_field_table "
. "WHERE name='" . $field_name . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
$field_id=$row["id"];
}
}
//select bugs with status equal to 'closed' or 'resolved'
$sql_users_bugs = "SELECT bg.id FROM mantis_bug_table as bg, mantis_bug_history_table as hist WHERE
handler_id='". $user_id . "'"
. " AND ( bg.status='80' OR bg.status='90' )"
. " AND ( hist.bug_id = bg.id )"
. " AND hist.field_name = 'status'"
. " AND ( hist.new_value = '80' OR hist.new_value = '90')"
. " AND hist.date_modified>" . $twentyonedaysago . " GROUP BY bg.id";
$result_users_bugs = $conn->query($sql_users_bugs);
return count_votes($result_users_bugs, $field_id, "\nResolved:", $conn);
}
function get_new_votings( $user_id, $conn, $start_date, $twentyonedaysago ){
$field_name = "fixing_priority";
$field_id = 0;
$votings = "";
$votings_array;
$sql = "SELECT * FROM mantis_custom_field_table "
. "WHERE name='" . $field_name . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
$field_id=$row["id"];
}
}
//select bugs with status not equal to 'closed' or 'resolved'
$sql_users_bugs = "SELECT id FROM mantis_bug_table WHERE
handler_id='". $user_id . "'"
. " AND id NOT IN (
SELECT bg.id FROM mantis_bug_table as bg, mantis_bug_history_table as hist WHERE
bg.handler_id='". $user_id . "'"
. " AND hist.bug_id = bg.id "
. " AND hist.new_value = '001'"
. " AND hist.date_modified<=" . $twentyonedaysago
. " AND NOT ( bg.status='80' OR bg.status='90' )
)"
. " AND NOT ( status='80' OR status='90' )";
$result_users_bugs = $conn->query($sql_users_bugs);
return count_votes($result_users_bugs, $field_id, "\nNew:", $conn);
}
function get_old_votings( $user_id, $conn, $start_date, $twentyonedaysago ){
$field_name = "fixing_priority";
$field_id = 0;
$votings = "";
$votings_array;
$sql = "SELECT * FROM mantis_custom_field_table "
. "WHERE name='" . $field_name . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
$field_id=$row["id"];
}
}
//select bugs with status not equal to 'closed' or 'resolved'
$sql_users_bugs = "SELECT bg.id FROM mantis_bug_table as bg, mantis_bug_history_table as hist WHERE
bg.handler_id='". $user_id . "'"
. " AND hist.bug_id = bg.id "
. " AND hist.new_value = '001'"
. " AND hist.date_modified<=" . $twentyonedaysago
. " AND NOT ( bg.status='80' OR bg.status='90' ) GROUP BY bg.id";
$result_users_bugs = $conn->query($sql_users_bugs);
return count_votes($result_users_bugs, $field_id, "\nOld:", $conn);
}
function count_bug_votings( $user_id, $conn, $start_date, $twentyonedaysago ){
$field_name = "fixing_priority";
$field_id = 0;
$votings = "";
$votings_array;
$sql = "SELECT * FROM mantis_custom_field_table "
. "WHERE name='" . $field_name . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
$field_id=$row["id"];
}
}
//select bugs with status not equal to 'closed' or 'resolved'
$sql_users_bugs = "SELECT * FROM mantis_bug_table WHERE
handler_id='". $user_id . "' " . "AND NOT ( status='80' OR status='90' )";
$result_users_bugs = $conn->query($sql_users_bugs);
return count_votes($result_users_bugs, $field_id, "\nVotes:", $conn);
}
function compose_summary_report_msg($summary_report){
$message = "<pre>\r\n <b>Open bugs and required feedbacks in core project:</b> \r\n ";
foreach ($summary_report as $key => $value) {
$voting_values = "";
if( isset($value["bug_votings"]) ){
if(!empty($value["bug_votings"])){
$voting_values = $value["bug_votings"] . $value["new_votings"] . $value["old_votings"] . $value["resolved_votings"];
}
}
if($value["open_bugs"] > 0){
$vname = $value["name"];
if($vname == ""){
$vname = $value["email"];
}
$name = "<a href='https://www.ilias.de/mantis/view_user_page.php?id=" . $key
. "' target='_blank'>" . $vname . "</a>";
if($value["feedback_bugs"] > 0){
$line = "\n" . $name . ": \n" . $value["open_bugs"] . " open bugs("
. $value["mean_age_open"] . " days)" . ", " . $value["feedback_bugs"]
." feedbacks required(" . $value["mean_age_feedback"] . " days)"
. $voting_values;
$message = $message . "\n" . $line;
}
else{
$line = "\n" . $name . ": \n" . $value["open_bugs"] . " open bugs("
. $value["mean_age_open"] . " days)" . $voting_values;
$message = $message . "\n" . $line;
}
}
else if($value["feedback_bugs"] > 0){
$vname = $value["name"];
if($vname == ""){
$vname = $value["email"];
}
$name = "<a href='https://www.ilias.de/mantis/view_user_page.php?id=" . $key
. "' target='_blank'>" . $vname . "</a>";
$line = "\n" . $name . ": \n" . $value["feedback_bugs"] ." feedbacks required("
. $value["mean_age_feedback"] . " days)" . $voting_values;
$message = $message . "\n" . $line;
}
else if( isset($value["bug_votings"]) ){ #$summary_report[$row["id"]]["bug_votings"]
//if( count($value["bug_votings"]) > 0 ){
if( $value["bug_votings"] != "" ){
$vname = $value["name"];
//echo "" . $vname . ": " . count($value["bug_votings"]) . "\n";
if($vname == ""){
$vname = $value["email"];
}
$name = "<a href='https://www.ilias.de/mantis/view_user_page.php?id=" . $key
. "' target='_blank'>" . $vname . "</a>";
$line = "\n" . $name . ": " . str_replace(", ", "", $value["bug_votings"])
. $value["new_votings"] . $value["old_votings"] . $value["resolved_votings"];
$message = $message . "\n" . $line;
}
}
}
$message = $message . "\r\n\r\n</pre>";
return $message;
}
include("/etc/mantis/config_db.php");
include("message_values.php");
$datenow = getdate();
$twentyonedaysago = $datenow[0] - $twentyonedays;
$fourteendaysago = $datenow[0] - $fourteendays;
// Create connection
$conn = new mysqli($dbserver, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/*
* Open bug message
*/
//Select reporter(25), updater(40), developers(55), manager(70), and administrators(90)
$sql = "SELECT * FROM mantis_user_table WHERE access_level='25' OR access_level='40' OR access_level='55' OR access_level='70' OR access_level='90'";
$result = $conn->query($sql);
$summary_report = null;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//over 21 days open bugs
$subject = '[MantisBT] Reminder about open bugs';
//count bugs of current user with project nr. 1
if(( $row["access_level"] == 55 || $row["access_level"] == 70 || $row["access_level"] == 90 )){
$summary_report[$row["id"]]["open_bugs"] = 0;
$summary_report[$row["id"]]["feedback_bugs"] = 0;
$summary_report[$row["id"]]["mean_age_open"] = 0;
$summary_report[$row["id"]]["mean_age_feedback"] = 0;
$summary_report[$row["id"]]["bug_votings"] = "";
$summary_report[$row["id"]]["old_votings"] = "";
$summary_report[$row["id"]]["new_votings"] = "";
$summary_report[$row["id"]]["resolved_votings"] = "";
$summary_report[$row["id"]]["bug_votings"] = count_bug_votings( $row["id"], $conn, $start_date, $twentyonedaysago );
$summary_report[$row["id"]]["new_votings"] = get_new_votings( $row["id"], $conn, $start_date, $twentyonedaysago );
$summary_report[$row["id"]]["old_votings"] = get_old_votings( $row["id"], $conn, $start_date, $twentyonedaysago );
$summary_report[$row["id"]]["resolved_votings"] = get_resolved_votings( $row["id"], $conn, $start_date, $twentyonedaysago );
$sql_21_days_open = "SELECT COUNT(id) AS open_num FROM mantis_bug_table WHERE handler_id='". $row["id"] . "' AND status='10' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "' AND project_id = 1";
$result2 = $conn->query($sql_21_days_open);
$row_count_open = $result2->fetch_assoc();
$summary_report[$row["id"]]["email"] = $row["email"];
$summary_report[$row["id"]]["name"] = $row["realname"];
if( $row_count_open["open_num"] > 0 ){
$summary_report[$row["id"]]["open_bugs"] = $row_count_open["open_num"];
$sql_sum_mean_age = "SELECT handler_id, SUM( " . $datenow[0] . " - last_updated ) as 'total_time_passed' FROM mantis_bug_table WHERE handler_id='". $row["id"] . "' AND status='10' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "' AND project_id = 1 GROUP BY handler_id";
$result_mean_age = $conn->query($sql_sum_mean_age);
$row_mean_age = $result_mean_age->fetch_assoc();
$mean_age_open = $row_mean_age["total_time_passed"] / $row_count_open["open_num"];
$mean_age_in_days = round( $mean_age_open / (24*60*60) );
$summary_report[$row["id"]]["mean_age_open"] = $mean_age_in_days;
}
$sql_21_days_feedback = "SELECT COUNT(id) AS feedback_num FROM mantis_bug_table WHERE reporter_id='". $row["id"] . "' AND status='20' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "' AND project_id = 1";
$result3 = $conn->query($sql_21_days_feedback);
$row_count_feedback = $result3->fetch_assoc();
if( $row_count_feedback["feedback_num"] > 0 ){
$summary_report[$row["id"]]["feedback_bugs"] = $row_count_feedback["feedback_num"];
$sql_sum_mean_age = "SELECT reporter_id, SUM( " . $datenow[0] . " - last_updated ) as 'total_time_passed' FROM mantis_bug_table WHERE reporter_id='". $row["id"] . "' AND status='20' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "' AND project_id = 1 GROUP BY reporter_id";
$result_mean_age = $conn->query($sql_sum_mean_age);
$row_mean_age = $result_mean_age->fetch_assoc();
$mean_age_feedback = $row_mean_age["total_time_passed"] / $row_count_feedback["feedback_num"];
$mean_age_in_days = round( $mean_age_feedback / (24*60*60) );
$summary_report[$row["id"]]["mean_age_feedback"] = $mean_age_in_days;
}
}
$addressee = $row["email"];
$header = 'From: Mantis Bug Tracker <[email protected]>' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = '[MantisBT] Reminder about open bugs';
$sql_21_days = "SELECT * FROM mantis_bug_table WHERE handler_id='". $row["id"] . "' AND status='10' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "'";
$sql_14_days = "SELECT * FROM mantis_bug_table WHERE handler_id='". $row["id"] . "' AND status='10' AND date_submitted>'" . $start_date . "' AND last_updated<'". $fourteendaysago . "' AND last_updated>'". $twentyonedaysago . "'";
$message_open = compose_remind_msg($conn, $row, $sql_21_days, $sql_14_days, $msg_open_bugs_begin, $msg_open_bugs_end, $msg_open_bugs_warning_begin, $url);
if( $message_open != ""){
mail($addressee, $subject, $message_open, $header);
}
$subject = '[MantisBT] Reminder about required feedbacks';
//over 21 days needed feedbacks
$sql_21_days = "SELECT * FROM mantis_bug_table WHERE reporter_id='". $row["id"] . "' AND status='20' AND date_submitted>'" . $start_date . "' AND last_updated<'". $twentyonedaysago . "'";
//over 14 but less than 21 days needed feedbacks
$sql_14_days = "SELECT * FROM mantis_bug_table WHERE reporter_id='". $row["id"] . "' AND status='20' AND date_submitted>'" . $start_date . "' AND last_updated<'". $fourteendaysago . "' AND last_updated>'". $twentyonedaysago . "'";
$message_feedback = compose_remind_msg($conn, $row, $sql_21_days, $sql_14_days, $msg_feedback_begin, $msg_feedback_end, $msg_feedback_warning_begin, $url);
if( $message_feedback != ""){
mail($addressee, $subject, $message_feedback, $header);
}
}
} else {
echo "0 results";
}
$conn->close();
//send summary report
$addressee = "[email protected]";
$subject = '[MantisBT] Summary report: open bugs & required feedbacks in core project';
$tmp = compose_summary_report_msg($summary_report);
$header = 'From: Mantis Bug Tracker <[email protected]>' . "\r\n" .
"Reply-To: [email protected]" . "\r\n" .
"X-Mailer: PHP/" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/html; charset=utf-8" . "\r\n" .
"Content-Transfer-Encoding: quoted-printable";
$message = quoted_printable_encode(utf8_encode($tmp));
mail($addressee, $subject, $message, $header);
?>