forked from mantisbt/mantisbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbugnote_api.php
740 lines (627 loc) · 22.9 KB
/
bugnote_api.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Bugnote API
*
* @package CoreAPI
* @subpackage BugnoteAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - [email protected]
* @copyright Copyright 2002 MantisBT Team - [email protected]
* @link http://www.mantisbt.org
*
* @uses access_api.php
* @uses authentication_api.php
* @uses bug_api.php
* @uses bug_revision_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses database_api.php
* @uses email_api.php
* @uses error_api.php
* @uses event_api.php
* @uses helper_api.php
* @uses history_api.php
* @uses lang_api.php
* @uses user_api.php
* @uses utility_api.php
*/
require_api( 'access_api.php' );
require_api( 'authentication_api.php' );
require_api( 'bug_api.php' );
require_api( 'bug_revision_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'email_api.php' );
require_api( 'error_api.php' );
require_api( 'event_api.php' );
require_api( 'helper_api.php' );
require_api( 'history_api.php' );
require_api( 'lang_api.php' );
require_api( 'user_api.php' );
require_api( 'utility_api.php' );
/**
* Bugnote Data Structure Definition
*/
class BugnoteData {
/**
* Bugnote ID
*/
public $id;
/**
* Bug ID
*/
public $bug_id;
/**
* Reporter ID
*/
public $reporter_id;
/**
* Note text
*/
public $note;
/**
* View State
*/
public $view_state;
/**
* Date submitted
*/
public $date_submitted;
/**
* Last Modified
*/
public $last_modified;
/**
* Bugnote type
*/
public $note_type;
/**
* ???
*/
public $note_attr;
/**
* Time tracking information
*/
public $time_tracking;
/**
* Bugnote Text id
*/
public $bugnote_text_id;
}
/**
* Check if a bugnote with the given ID exists
* return true if the bugnote exists, false otherwise
* @param integer $p_bugnote_id A bugnote identifier.
* @return boolean
* @access public
*/
function bugnote_exists( $p_bugnote_id ) {
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT COUNT(*) FROM $t_bugnote_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $p_bugnote_id ) );
if( 0 == db_result( $t_result ) ) {
return false;
} else {
return true;
}
}
/**
* Check if a bugnote with the given ID exists
* return true if the bugnote exists, raise an error if not
* @param integer $p_bugnote_id A bugnote identifier.
* @access public
* @return void
*/
function bugnote_ensure_exists( $p_bugnote_id ) {
if( !bugnote_exists( $p_bugnote_id ) ) {
trigger_error( ERROR_BUGNOTE_NOT_FOUND, ERROR );
}
}
/**
* Check if the given user is the reporter of the bugnote
* return true if the user is the reporter, false otherwise
* @param integer $p_bugnote_id A bugnote identifier.
* @param integer $p_user_id An user identifier.
* @return boolean
* @access public
*/
function bugnote_is_user_reporter( $p_bugnote_id, $p_user_id ) {
if( bugnote_get_field( $p_bugnote_id, 'reporter_id' ) == $p_user_id ) {
return true;
} else {
return false;
}
}
/**
* Add a bugnote to a bug
* return the ID of the new bugnote
* @param integer $p_bug_id A bug identifier.
* @param string $p_bugnote_text The bugnote text to add.
* @param string $p_time_tracking Time tracking value - hh:mm string.
* @param boolean $p_private Whether bugnote is private.
* @param integer $p_type The bugnote type.
* @param string $p_attr Bugnote Attribute.
* @param integer $p_user_id A user identifier.
* @param boolean $p_send_email Whether to generate email.
* @param integer $p_date_submitted Date submitted (defaults to now()).
* @param integer $p_last_modified Last modification date (defaults to now()).
* @param boolean $p_skip_bug_update Skip bug last modification update (useful when importing bugs/bugnotes).
* @param boolean $p_log_history Log changes to bugnote history (defaults to true).
* @return boolean|integer false or indicating bugnote id added
* @access public
*/
function bugnote_add( $p_bug_id, $p_bugnote_text, $p_time_tracking = '0:00', $p_private = false, $p_type = BUGNOTE, $p_attr = '', $p_user_id = null, $p_send_email = true, $p_date_submitted = 0, $p_last_modified = 0, $p_skip_bug_update = false, $p_log_history = true ) {
$c_bug_id = (int)$p_bug_id;
$c_time_tracking = helper_duration_to_minutes( $p_time_tracking );
$c_type = (int)$p_type;
$c_date_submitted = $p_date_submitted <= 0 ? db_now() : (int)$p_date_submitted;
$c_last_modified = $p_last_modified <= 0 ? db_now() : (int)$p_last_modified;
if( REMINDER !== $p_type ) {
# Check if this is a time-tracking note
$t_time_tracking_enabled = config_get( 'time_tracking_enabled' );
if( ON == $t_time_tracking_enabled && $c_time_tracking > 0 ) {
$t_time_tracking_without_note = config_get( 'time_tracking_without_note' );
if( is_blank( $p_bugnote_text ) && OFF == $t_time_tracking_without_note ) {
error_parameters( lang_get( 'bugnote' ) );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
$c_type = TIME_TRACKING;
} else if( is_blank( $p_bugnote_text ) ) {
# This is not time tracking (i.e. it's a normal bugnote)
# @todo should we not trigger an error in this case ?
return false;
}
}
# Event integration
$t_bugnote_text = event_signal( 'EVENT_BUGNOTE_DATA', $p_bugnote_text, $c_bug_id );
# insert bugnote text
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
$t_query = 'INSERT INTO ' . $t_bugnote_text_table . ' ( note ) VALUES ( ' . db_param() . ' )';
db_query_bound( $t_query, array( $t_bugnote_text ) );
# retrieve bugnote text id number
$t_bugnote_text_id = db_insert_id( $t_bugnote_text_table );
# get user information
if( $p_user_id === null ) {
$p_user_id = auth_get_current_user_id();
}
# Check for private bugnotes.
if( $p_private && access_has_bug_level( config_get( 'set_view_status_threshold' ), $p_bug_id, $p_user_id ) ) {
$t_view_state = VS_PRIVATE;
} else {
$t_view_state = VS_PUBLIC;
}
# insert bugnote info
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "INSERT INTO $t_bugnote_table
(bug_id, reporter_id, bugnote_text_id, view_state, date_submitted, last_modified, note_type, note_attr, time_tracking)
VALUES ("
. db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', '
. db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', '
. db_param() . ' )';
$t_params = array(
$c_bug_id,
$p_user_id,
$t_bugnote_text_id,
$t_view_state,
$c_date_submitted,
$c_last_modified,
$c_type,
$p_attr,
$c_time_tracking
);
db_query_bound( $t_query, $t_params );
# get bugnote id
$t_bugnote_id = db_insert_id( $t_bugnote_table );
# update bug last updated
if( !$p_skip_bug_update ) {
bug_update_date( $p_bug_id );
}
# log new bug
if( true == $p_log_history ) {
history_log_event_special( $p_bug_id, BUGNOTE_ADDED, bugnote_format_id( $t_bugnote_id ) );
}
# Event integration
event_signal( 'EVENT_BUGNOTE_ADD', array( $p_bug_id, $t_bugnote_id ) );
# only send email if the text is not blank, otherwise, it is just recording of time without a comment.
if( true == $p_send_email && !is_blank( $t_bugnote_text ) ) {
email_generic( $p_bug_id, 'bugnote', 'email_notification_title_for_action_bugnote_submitted' );
}
return $t_bugnote_id;
}
/**
* Delete a bugnote
* @param integer $p_bugnote_id A bug note identifier.
* @return boolean
* @access public
*/
function bugnote_delete( $p_bugnote_id ) {
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
$t_bugnote_table = db_get_table( 'bugnote' );
# Remove the bugnote
$t_query = 'DELETE FROM ' . $t_bugnote_table . ' WHERE id=' . db_param();
db_query_bound( $t_query, array( $p_bugnote_id ) );
# Remove the bugnote text
$t_query = 'DELETE FROM ' . $t_bugnote_text_table . ' WHERE id=' . db_param();
db_query_bound( $t_query, array( $t_bugnote_text_id ) );
# log deletion of bug
history_log_event_special( $t_bug_id, BUGNOTE_DELETED, bugnote_format_id( $p_bugnote_id ) );
return true;
}
/**
* delete all bugnotes associated with the given bug
* @param integer $p_bug_id A bug identifier.
* @return void
* @access public
*/
function bugnote_delete_all( $p_bug_id ) {
# Delete the bugnote text items
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT bugnote_text_id FROM $t_bugnote_table WHERE bug_id=" . db_param();
$t_result = db_query_bound( $t_query, array( (int)$p_bug_id ) );
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_bugnote_text_id = $t_row['bugnote_text_id'];
# Delete the corresponding bugnote texts
$t_query = "DELETE FROM $t_bugnote_text_table WHERE id=" . db_param();
db_query_bound( $t_query, array( $t_bugnote_text_id ) );
}
# Delete the corresponding bugnotes
$t_query = "DELETE FROM $t_bugnote_table WHERE bug_id=" . db_param();
db_query_bound( $t_query, array( (int)$p_bug_id ) );
}
/**
* Get the text associated with the bugnote
* @param integer $p_bugnote_id A bugnote identifier.
* @return string bugnote text
* @access public
*/
function bugnote_get_text( $p_bugnote_id ) {
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
# grab the bugnote text
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
$t_query = "SELECT note FROM $t_bugnote_text_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $t_bugnote_text_id ) );
return db_result( $t_result );
}
/**
* Get a field for the given bugnote
* @param integer $p_bugnote_id A bugnote identifier.
* @param string $p_field_name Field name to retrieve.
* @return string field value
* @access public
*/
function bugnote_get_field( $p_bugnote_id, $p_field_name ) {
static $s_vars;
global $g_cache_bugnote;
if( isset( $g_cache_bugnote[(int)$p_bugnote_id] ) ) {
return $g_cache_bugnote[(int)$p_bugnote_id]->$p_field_name;
}
if( $s_vars == null ) {
$s_vars = getClassProperties( 'BugnoteData', 'public' );
}
if( !array_key_exists( $p_field_name, $s_vars ) ) {
error_parameters( $p_field_name );
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
}
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT $p_field_name FROM $t_bugnote_table WHERE id=" . db_param();
$t_result = db_query_bound( $t_query, array( $p_bugnote_id ), 1 );
return db_result( $t_result );
}
/**
* Get latest bugnote id
* @param integer $p_bug_id A bug identifier.
* @return int latest bugnote id
* @access public
*/
function bugnote_get_latest_id( $p_bug_id ) {
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT id FROM $t_bugnote_table WHERE bug_id=" . db_param() . " ORDER by last_modified DESC";
$t_result = db_query_bound( $t_query, array( (int)$p_bug_id ), 1 );
return (int)db_result( $t_result );
}
/**
* Build the bugnotes array for the given bug_id filtered by specified $p_user_access_level.
* Bugnotes are sorted by date_submitted according to 'bugnote_order' configuration setting.
* Return BugnoteData class object with raw values from the tables except the field
* last_modified - it is UNIX_TIMESTAMP.
* @param integer $p_bug_id A bug identifier.
* @param integer $p_user_bugnote_order Sort order.
* @param integer $p_user_bugnote_limit Number of bugnotes to display to user.
* @param integer $p_user_id An user identifier.
* @return array array of bugnotes
* @access public
*/
function bugnote_get_all_visible_bugnotes( $p_bug_id, $p_user_bugnote_order, $p_user_bugnote_limit, $p_user_id = null ) {
if( $p_user_id === null ) {
$t_user_id = auth_get_current_user_id();
} else {
$t_user_id = $p_user_id;
}
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
$t_user_access_level = user_get_access_level( $t_user_id, $t_project_id );
$t_all_bugnotes = bugnote_get_all_bugnotes( $p_bug_id );
$t_private_bugnote_threshold = config_get( 'private_bugnote_threshold' );
$t_private_bugnote_visible = access_compare_level( $t_user_access_level, config_get( 'private_bugnote_threshold' ) );
$t_time_tracking_visible = access_compare_level( $t_user_access_level, config_get( 'time_tracking_view_threshold' ) );
$t_bugnotes = array();
$t_bugnote_count = count( $t_all_bugnotes );
$t_bugnote_limit = $p_user_bugnote_limit > 0 ? $p_user_bugnote_limit : $t_bugnote_count;
$t_bugnotes_found = 0;
# build a list of the latest bugnotes that the user can see
for ( $i = 0; ( $i < $t_bugnote_count ) && ( $t_bugnotes_found < $t_bugnote_limit ); $i++ ) {
$t_bugnote = array_pop( $t_all_bugnotes );
if( $t_private_bugnote_visible || $t_bugnote->reporter_id == $t_user_id || ( VS_PUBLIC == $t_bugnote->view_state ) ) {
# If the access level specified is not enough to see time tracking information
# then reset it to 0.
if( !$t_time_tracking_visible ) {
$t_bugnote->time_tracking = 0;
}
$t_bugnotes[$t_bugnotes_found++] = $t_bugnote;
}
}
# reverse the list for users with ascending view preferences
if( 'ASC' == $p_user_bugnote_order ) {
$t_bugnotes = array_reverse( $t_bugnotes );
}
return $t_bugnotes;
}
/**
* Build the bugnotes array for the given bug_id.
* Return BugnoteData class object with raw values from the tables except the field
* last_modified - it is UNIX_TIMESTAMP.
* The data is not filtered by VIEW_STATE !!
* @param integer $p_bug_id A bug identifier.
* @return array array of bugnotes
* @access public
*/
function bugnote_get_all_bugnotes( $p_bug_id ) {
global $g_cache_bugnotes, $g_cache_bugnote;
if( !isset( $g_cache_bugnotes ) ) {
$g_cache_bugnotes = array();
}
if( !isset( $g_cache_bugnote ) ) {
$g_cache_bugnote = array();
}
# the cache should be aware of the sorting order
if( !isset( $g_cache_bugnotes[(int)$p_bug_id] ) ) {
$t_bugnote_table = db_get_table( 'bugnote' );
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
# sort by bugnote id which should be more accurate than submit date, since two bugnotes
# may be submitted at the same time if submitted using a script (eg: MantisConnect).
$t_query = "SELECT b.*, t.note
FROM $t_bugnote_table b
LEFT JOIN $t_bugnote_text_table t ON b.bugnote_text_id = t.id
WHERE b.bug_id=" . db_param() . '
ORDER BY b.id ASC';
$t_bugnotes = array();
# BUILD bugnotes array
$t_result = db_query_bound( $t_query, array( $p_bug_id ) );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_bugnote = new BugnoteData;
$t_bugnote->id = $t_row['id'];
$t_bugnote->bug_id = $t_row['bug_id'];
$t_bugnote->bugnote_text_id = $t_row['bugnote_text_id'];
$t_bugnote->note = $t_row['note'];
$t_bugnote->view_state = $t_row['view_state'];
$t_bugnote->reporter_id = $t_row['reporter_id'];
$t_bugnote->date_submitted = $t_row['date_submitted'];
$t_bugnote->last_modified = $t_row['last_modified'];
$t_bugnote->note_type = $t_row['note_type'];
$t_bugnote->note_attr = $t_row['note_attr'];
$t_bugnote->time_tracking = $t_row['time_tracking'];
$t_bugnotes[] = $t_bugnote;
$g_cache_bugnote[(int)$t_bugnote->id] = $t_bugnote;
}
$g_cache_bugnotes[(int)$p_bug_id] = $t_bugnotes;
}
return $g_cache_bugnotes[(int)$p_bug_id];
}
/**
* Update the time_tracking field of the bugnote
* @param integer $p_bugnote_id A bugnote identifier.
* @param string $p_time_tracking Timetracking string (hh:mm format).
* @return void
* @access public
*/
function bugnote_set_time_tracking( $p_bugnote_id, $p_time_tracking ) {
$c_bugnote_time_tracking = helper_duration_to_minutes( $p_time_tracking );
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "UPDATE $t_bugnote_table SET time_tracking = " . db_param() . " WHERE id=" . db_param();
db_query_bound( $t_query, array( $c_bugnote_time_tracking, $p_bugnote_id ) );
}
/**
* Update the last_modified field of the bugnote
* @param integer $p_bugnote_id A bugnote identifier.
* @return void
* @access public
*/
function bugnote_date_update( $p_bugnote_id ) {
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "UPDATE $t_bugnote_table SET last_modified=" . db_param() . " WHERE id=" . db_param();
db_query_bound( $t_query, array( db_now(), $p_bugnote_id ) );
}
/**
* Set the bugnote text
* @param integer $p_bugnote_id A bugnote identifier.
* @param string $p_bugnote_text The bugnote text to set.
* @return boolean
* @access public
*/
function bugnote_set_text( $p_bugnote_id, $p_bugnote_text ) {
$t_old_text = bugnote_get_text( $p_bugnote_id );
if( $t_old_text == $p_bugnote_text ) {
return true;
}
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
$t_bugnote_text_table = db_get_table( 'bugnote_text' );
# insert an 'original' revision if needed
if( bug_revision_count( $t_bug_id, REV_BUGNOTE, $p_bugnote_id ) < 1 ) {
$t_user_id = bugnote_get_field( $p_bugnote_id, 'reporter_id' );
$t_timestamp = bugnote_get_field( $p_bugnote_id, 'last_modified' );
bug_revision_add( $t_bug_id, $t_user_id, REV_BUGNOTE, $t_old_text, $p_bugnote_id, $t_timestamp );
}
$t_query = "UPDATE $t_bugnote_text_table SET note=" . db_param() . " WHERE id=" . db_param();
db_query_bound( $t_query, array( $p_bugnote_text, $t_bugnote_text_id ) );
# updated the last_updated date
bugnote_date_update( $p_bugnote_id );
bug_update_date( $t_bug_id );
# insert a new revision
$t_user_id = auth_get_current_user_id();
$t_revision_id = bug_revision_add( $t_bug_id, $t_user_id, REV_BUGNOTE, $p_bugnote_text, $p_bugnote_id );
# log new bugnote
history_log_event_special( $t_bug_id, BUGNOTE_UPDATED, bugnote_format_id( $p_bugnote_id ), $t_revision_id );
return true;
}
/**
* Set the view state of the bugnote
* @param integer $p_bugnote_id A bugnote identifier.
* @param boolean $p_private Whether bugnote should be set to private status.
* @return boolean
* @access public
*/
function bugnote_set_view_state( $p_bugnote_id, $p_private ) {
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
if( $p_private ) {
$t_view_state = VS_PRIVATE;
} else {
$t_view_state = VS_PUBLIC;
}
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "UPDATE $t_bugnote_table SET view_state=" . db_param() . " WHERE id=" . db_param();
db_query_bound( $t_query, array( $t_view_state, $p_bugnote_id ) );
history_log_event_special( $t_bug_id, BUGNOTE_STATE_CHANGED, $t_view_state, bugnote_format_id( $p_bugnote_id ) );
return true;
}
/**
* Pad the bugnote id with the appropriate number of zeros for printing
* @param integer $p_bugnote_id A bugnote identifier.
* @return string
* @access public
*/
function bugnote_format_id( $p_bugnote_id ) {
$t_padding = config_get( 'display_bugnote_padding' );
return utf8_str_pad( $p_bugnote_id, $t_padding, '0', STR_PAD_LEFT );
}
/**
* Returns an array of bugnote stats
* @param integer $p_bug_id A bug identifier.
* @param string $p_from Starting date (yyyy-mm-dd) inclusive, if blank, then ignored.
* @param string $p_to Ending date (yyyy-mm-dd) inclusive, if blank, then ignored.
* @return array array of bugnote stats
* @access public
*/
function bugnote_stats_get_events_array( $p_bug_id, $p_from, $p_to ) {
$c_to = strtotime( $p_to ) + SECONDS_PER_DAY - 1;
$c_from = strtotime( $p_from );
if( !is_blank( $c_from ) ) {
$t_from_where = " AND bn.date_submitted >= $c_from ";
} else {
$t_from_where = '';
}
if( !is_blank( $c_to ) ) {
$t_to_where = " AND bn.date_submitted <= $c_to ";
} else {
$t_to_where = '';
}
$t_results = array();
$t_user_table = db_get_table( 'user' );
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT username, realname, SUM(time_tracking) AS sum_time_tracking
FROM $t_user_table u, $t_bugnote_table bn
WHERE u.id = bn.reporter_id AND bn.time_tracking != 0 AND
bn.bug_id = " . db_param() . "
$t_from_where $t_to_where
GROUP BY u.username, u.realname";
$t_result = db_query_bound( $t_query, array( $p_bug_id ) );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_results[] = $t_row;
}
return $t_results;
}
/**
* Returns an array of bugnote stats
* @param integer $p_project_id A project identifier.
* @param string $p_from Starting date (yyyy-mm-dd) inclusive, if blank, then ignored.
* @param string $p_to Ending date (yyyy-mm-dd) inclusive, if blank, then ignored.
* @param integer $p_cost Cost.
* @return array array of bugnote stats
* @access public
*/
function bugnote_stats_get_project_array( $p_project_id, $p_from, $p_to, $p_cost ) {
$t_params = array();
$c_to = strtotime( $p_to ) + SECONDS_PER_DAY - 1;
$c_from = strtotime( $p_from );
if( $c_to === false || $c_from === false ) {
error_parameters( array( $p_from, $p_to ) );
trigger_error( ERROR_GENERIC, ERROR );
}
if( ALL_PROJECTS != $p_project_id ) {
$t_project_where = ' AND b.project_id = ' . db_param() . ' AND bn.bug_id = b.id ';
$t_params[] = $p_project_id;
} else {
$t_project_where = '';
}
if( !is_blank( $c_from ) ) {
$t_from_where = ' AND bn.date_submitted >= ' . db_param();
$t_params[] = $c_from;
} else {
$t_from_where = '';
}
if( !is_blank( $c_to ) ) {
$t_to_where = ' AND bn.date_submitted <= ' . db_param();
$t_params[] = $c_to;
} else {
$t_to_where = '';
}
$t_results = array();
$t_bug_table = db_get_table( 'bug' );
$t_user_table = db_get_table( 'user' );
$t_bugnote_table = db_get_table( 'bugnote' );
$t_query = "SELECT username, realname, summary, bn.bug_id, SUM(time_tracking) AS sum_time_tracking
FROM $t_user_table u, $t_bugnote_table bn, $t_bug_table b
WHERE u.id = bn.reporter_id AND bn.time_tracking != 0 AND bn.bug_id = b.id
$t_project_where $t_from_where $t_to_where
GROUP BY bn.bug_id, u.username, u.realname, b.summary
ORDER BY bn.bug_id";
$t_result = db_query_bound( $t_query, $t_params );
$t_cost_min = $p_cost / 60.0;
while( $t_row = db_fetch_array( $t_result ) ) {
$t_total_cost = $t_cost_min * $t_row['sum_time_tracking'];
$t_row['cost'] = $t_total_cost;
$t_results[] = $t_row;
}
return $t_results;
}
/**
* Clear a bugnote from the cache or all bug notes if no bugnote id specified.
* @param integer $p_bugnote_id Identifier to clear (optional).
* @return boolean
* @access public
*/
function bugnote_clear_cache( $p_bugnote_id = null ) {
global $g_cache_bugnote, $g_cache_bugnotes;
if( null === $p_bugnote_id ) {
$g_cache_bugnote = array();
} else {
unset( $g_cache_bugnote[(int)$p_bugnote_id] );
}
$g_cache_bugnotes = array();
return true;
}