[ Index ] |
|
Code source de Mantis 1.1.0rc3 |
1 <?php 2 # Mantis - a php based bugtracking system 3 4 # Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org 5 # Copyright (C) 2002 - 2007 Mantis Team - mantisbt-dev@lists.sourceforge.net 6 7 # Mantis is free software: you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation, either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Mantis is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Mantis. If not, see <http://www.gnu.org/licenses/>. 19 20 # -------------------------------------------------------- 21 # $Id: bugnote_api.php,v 1.46.2.1 2007-10-13 22:35:14 giallu Exp $ 22 # -------------------------------------------------------- 23 24 $t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR; 25 26 require_once ( $t_core_dir . 'current_user_api.php' ); 27 require_once ( $t_core_dir . 'email_api.php' ); 28 require_once ( $t_core_dir . 'history_api.php' ); 29 require_once ( $t_core_dir . 'bug_api.php' ); 30 31 ### Bugnote API ### 32 33 #=================================== 34 # Bugnote Data Structure Definition 35 #=================================== 36 class BugnoteData { 37 var $id; 38 var $bug_id; 39 var $reporter_id; 40 var $note; 41 var $view_state; 42 var $date_submitted; 43 var $last_modified; 44 var $note_type; 45 var $note_attr; 46 var $time_tracking; 47 } 48 49 #=================================== 50 # Boolean queries and ensures 51 #=================================== 52 53 # -------------------- 54 # Check if a bugnote with the given ID exists 55 # 56 # return true if the bugnote exists, false otherwise 57 function bugnote_exists( $p_bugnote_id ) { 58 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 59 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 60 61 $query = "SELECT COUNT(*) 62 FROM $t_bugnote_table 63 WHERE id='$c_bugnote_id'"; 64 $result = db_query( $query ); 65 66 if ( 0 == db_result( $result ) ) { 67 return false; 68 } else { 69 return true; 70 } 71 } 72 73 # -------------------- 74 # Check if a bugnote with the given ID exists 75 # 76 # return true if the bugnote exists, raise an error if not 77 function bugnote_ensure_exists( $p_bugnote_id ) { 78 if ( !bugnote_exists( $p_bugnote_id ) ) { 79 trigger_error( ERROR_BUGNOTE_NOT_FOUND, ERROR ); 80 } 81 } 82 83 # -------------------- 84 # Check if the given user is the reporter of the bugnote 85 # return true if the user is the reporter, false otherwise 86 function bugnote_is_user_reporter( $p_bugnote_id, $p_user_id ) { 87 if ( bugnote_get_field( $p_bugnote_id, 'reporter_id' ) == $p_user_id ) { 88 return true; 89 } else { 90 return false; 91 } 92 } 93 94 #=================================== 95 # Creation / Deletion / Updating 96 #=================================== 97 98 # -------------------- 99 # Add a bugnote to a bug 100 # 101 # return the ID of the new bugnote 102 function bugnote_add ( $p_bug_id, $p_bugnote_text, $p_time_tracking = '0:00', $p_private = false, $p_type = 0, $p_attr = '', $p_user_id = null ) { 103 $c_bug_id = db_prepare_int( $p_bug_id ); 104 $c_bugnote_text = db_prepare_string( $p_bugnote_text ); 105 $c_time_tracking = db_prepare_time( $p_time_tracking ); 106 $c_private = db_prepare_bool( $p_private ); 107 $c_type = db_prepare_int( $p_type ); 108 $c_attr = db_prepare_string( $p_attr ); 109 110 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 111 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 112 113 # insert bugnote text 114 $query = "INSERT INTO $t_bugnote_text_table 115 ( note ) 116 VALUES 117 ( '$c_bugnote_text' )"; 118 db_query( $query ); 119 120 # retrieve bugnote text id number 121 $t_bugnote_text_id = db_insert_id( $t_bugnote_text_table ); 122 123 # get user information 124 if ( $p_user_id === null ) { 125 $c_user_id = auth_get_current_user_id(); 126 } else { 127 $c_user_id = db_prepare_int( $p_user_id ); 128 } 129 130 # Check for private bugnotes. 131 # @@@ VB: Should we allow users to report private bugnotes, and possibly see only their own private ones 132 if ( $p_private && access_has_bug_level( config_get( 'private_bugnote_threshold' ), $p_bug_id, $c_user_id ) ) { 133 $t_view_state = VS_PRIVATE; 134 } else { 135 $t_view_state = VS_PUBLIC; 136 } 137 138 # insert bugnote info 139 $query = "INSERT INTO $t_bugnote_table 140 (bug_id, reporter_id, bugnote_text_id, view_state, date_submitted, last_modified, note_type, note_attr, time_tracking ) 141 VALUES 142 ('$c_bug_id', '$c_user_id','$t_bugnote_text_id', '$t_view_state', " . db_now() . "," . db_now() . ", '$c_type', '$c_attr', '$c_time_tracking' )"; 143 db_query( $query ); 144 145 # get bugnote id 146 $t_bugnote_id = db_insert_id( $t_bugnote_table ); 147 148 # update bug last updated 149 bug_update_date( $p_bug_id ); 150 151 # log new bug 152 history_log_event_special( $p_bug_id, BUGNOTE_ADDED, bugnote_format_id( $t_bugnote_id ) ); 153 154 return $t_bugnote_id; 155 } 156 157 # -------------------- 158 # Delete a bugnote 159 function bugnote_delete( $p_bugnote_id ) { 160 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 161 $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' ); 162 $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' ); 163 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 164 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 165 166 # Remove the bugnote 167 $query = "DELETE FROM $t_bugnote_table 168 WHERE id='$c_bugnote_id'"; 169 db_query( $query ); 170 171 # Remove the bugnote text 172 $query = "DELETE FROM $t_bugnote_text_table 173 WHERE id='$t_bugnote_text_id'"; 174 db_query( $query ); 175 176 # log deletion of bug 177 history_log_event_special( $t_bug_id, BUGNOTE_DELETED, bugnote_format_id( $p_bugnote_id ) ); 178 179 return true; 180 } 181 182 # -------------------- 183 # delete all bugnotes associated with the given bug 184 function bugnote_delete_all( $p_bug_id ) { 185 $c_bug_id = db_prepare_int( $p_bug_id ); 186 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 187 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 188 189 # Delete the bugnote text items 190 $query = "SELECT bugnote_text_id 191 FROM $t_bugnote_table 192 WHERE bug_id='$c_bug_id'"; 193 $result = db_query( $query ); 194 $bugnote_count = db_num_rows( $result ); 195 for ( $i = 0 ; $i < $bugnote_count ; $i++ ) { 196 $row = db_fetch_array( $result ); 197 $t_bugnote_text_id = $row['bugnote_text_id']; 198 199 # Delete the corresponding bugnote texts 200 $query = "DELETE FROM $t_bugnote_text_table 201 WHERE id='$t_bugnote_text_id'"; 202 db_query( $query ); 203 } 204 205 # Delete the corresponding bugnotes 206 $query = "DELETE FROM $t_bugnote_table 207 WHERE bug_id='$c_bug_id'"; 208 $result = db_query( $query ); 209 210 # db_query() errors on failure so: 211 return true; 212 } 213 214 215 #=================================== 216 # Data Access 217 #=================================== 218 219 # -------------------- 220 # Get the text associated with the bugnote 221 function bugnote_get_text( $p_bugnote_id ) { 222 $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' ); 223 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 224 225 # grab the bugnote text 226 $query = "SELECT note 227 FROM $t_bugnote_text_table 228 WHERE id='$t_bugnote_text_id'"; 229 $result = db_query( $query ); 230 231 return db_result( $result ); 232 } 233 234 # -------------------- 235 # Get a field for the given bugnote 236 function bugnote_get_field( $p_bugnote_id, $p_field_name ) { 237 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 238 $c_field_name = db_prepare_string( $p_field_name ); 239 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 240 241 $query = "SELECT $c_field_name 242 FROM $t_bugnote_table 243 WHERE id='$c_bugnote_id' "; 244 $result = db_query( $query, 1 ); 245 246 return db_result( $result ); 247 } 248 249 # -------------------- 250 # Get latest bugnote id 251 function bugnote_get_latest_id( $p_bug_id ) { 252 $c_bug_id = db_prepare_int( $p_bug_id ); 253 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 254 255 $query = "SELECT id 256 FROM $t_bugnote_table 257 WHERE bug_id='$c_bug_id' 258 ORDER by last_modified DESC"; 259 $result = db_query( $query, 1 ); 260 261 return db_result( $result ); 262 } 263 264 # -------------------- 265 # Build the bugnotes array for the given bug_id filtered by specified $p_user_access_level. 266 # Bugnotes are sorted by date_submitted according to 'bugnote_order' configuration setting. 267 # 268 # Return BugnoteData class object with raw values from the tables except the field 269 # last_modified - it is UNIX_TIMESTAMP. 270 function bugnote_get_all_visible_bugnotes( $p_bug_id, $p_user_access_level, $p_user_bugnote_order, $p_user_bugnote_limit ) { 271 $t_all_bugnotes = bugnote_get_all_bugnotes( $p_bug_id, $p_user_bugnote_order, $p_user_bugnote_limit ); 272 $t_private_bugnote_threshold = config_get( 'private_bugnote_threshold' ); 273 274 $t_private_bugnote_visible = access_compare_level( $p_user_access_level, config_get( 'private_bugnote_threshold' ) ); 275 $t_time_tracking_visible = access_compare_level( $p_user_access_level, config_get( 'time_tracking_view_threshold' ) ); 276 277 $t_bugnotes = array(); 278 foreach ( $t_all_bugnotes as $t_note_index => $t_bugnote ) { 279 if ( $t_private_bugnote_visible || ( VS_PUBLIC == $t_bugnote->view_state ) ) { 280 # If the access level specified is not enough to see time tracking information 281 # then reset it to 0. 282 if ( !$t_time_tracking_visible ) { 283 $t_bugnote->time_tracking = 0; 284 } 285 286 $t_bugnotes[$t_note_index] = $t_bugnote; 287 } 288 } 289 290 return $t_bugnotes; 291 } 292 293 # -------------------- 294 # Build the bugnotes array for the given bug_id. Bugnotes are sorted by date_submitted 295 # according to 'bugnote_order' configuration setting. 296 # Return BugnoteData class object with raw values from the tables except the field 297 # last_modified - it is UNIX_TIMESTAMP. 298 # The data is not filtered by VIEW_STATE !! 299 function bugnote_get_all_bugnotes( $p_bug_id, $p_user_bugnote_order, $p_user_bugnote_limit ) { 300 global $g_cache_bugnotes; 301 302 if ( !isset( $g_cache_bugnotes ) ) { 303 $g_cache_bugnotes = array(); 304 } 305 306 # the cache should be aware of the sorting order 307 if ( !isset( $g_cache_bugnotes[$p_bug_id][$p_user_bugnote_order] ) ) { 308 $c_bug_id = db_prepare_int( $p_bug_id ); 309 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 310 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 311 312 if ( 0 == $p_user_bugnote_limit ) { 313 ## Show all bugnotes 314 $t_bugnote_limit = -1; 315 $t_bugnote_offset = -1; 316 } else { 317 ## Use offset only if order is ASC to get the last bugnotes 318 if ( 'ASC' == $p_user_bugnote_order ) { 319 $result = db_query( "SELECT COUNT(*) AS row_count FROM $t_bugnote_table WHERE bug_id = '$c_bug_id'" ); 320 $row = db_fetch_array( $result ); 321 322 $t_bugnote_offset = $row['row_count'] - $p_user_bugnote_limit; 323 } else { 324 $t_bugnote_offset = -1; 325 } 326 327 $t_bugnote_limit = $p_user_bugnote_limit; 328 } 329 330 # sort by bugnote id which should be more accurate than submit date, since two bugnotes 331 # may be submitted at the same time if submitted using a script (eg: MantisConnect). 332 $query = "SELECT b.*, t.note 333 FROM $t_bugnote_table b 334 LEFT JOIN $t_bugnote_text_table t ON b.bugnote_text_id = t.id 335 WHERE b.bug_id = '$c_bug_id' 336 ORDER BY b.id $p_user_bugnote_order"; 337 $t_bugnotes = array(); 338 339 # BUILD bugnotes array 340 $result = db_query( $query, $t_bugnote_limit, $t_bugnote_offset ); 341 $count = db_num_rows( $result ); 342 for ( $i=0; $i < $count; $i++ ) { 343 $row = db_fetch_array( $result ); 344 345 $t_bugnote = new BugnoteData; 346 347 $t_bugnote->id = $row['id']; 348 $t_bugnote->bug_id = $row['bug_id']; 349 $t_bugnote->note = $row['note']; 350 $t_bugnote->view_state = $row['view_state']; 351 $t_bugnote->reporter_id = $row['reporter_id']; 352 $t_bugnote->date_submitted = db_unixtimestamp( $row['date_submitted'] ); 353 $t_bugnote->last_modified = db_unixtimestamp( $row['last_modified'] ); 354 $t_bugnote->note_type = $row['note_type']; 355 $t_bugnote->note_attr = $row['note_attr']; 356 $t_bugnote->time_tracking = db_minutes_to_hhmm( $row['time_tracking'] ); 357 358 $t_bugnotes[] = $t_bugnote; 359 } 360 $g_cache_bugnotes[$p_bug_id][$p_user_bugnote_order] = $t_bugnotes; 361 } 362 363 return $g_cache_bugnotes[$p_bug_id][$p_user_bugnote_order]; 364 } 365 366 #=================================== 367 # Data Modification 368 #=================================== 369 370 # -------------------- 371 # Update the time_tracking field of the bugnote 372 function bugnote_set_time_tracking( $p_bugnote_id, $p_time_tracking ) { 373 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 374 $c_bugnote_time_tracking = db_prepare_time( $p_time_tracking ); 375 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 376 377 $query = "UPDATE $t_bugnote_table 378 SET time_tracking = '$c_bugnote_time_tracking' 379 WHERE id='$c_bugnote_id'"; 380 db_query( $query ); 381 382 # db_query() errors if there was a problem so: 383 return true; 384 } 385 386 # -------------------- 387 # Update the last_modified field of the bugnote 388 function bugnote_date_update( $p_bugnote_id ) { 389 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 390 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 391 392 $query = "UPDATE $t_bugnote_table 393 SET last_modified=" . db_now() . " 394 WHERE id='$c_bugnote_id'"; 395 db_query( $query ); 396 397 # db_query() errors if there was a problem so: 398 return true; 399 } 400 401 # -------------------- 402 # Set the bugnote text 403 function bugnote_set_text( $p_bugnote_id, $p_bugnote_text ) { 404 $c_bugnote_text = db_prepare_string( $p_bugnote_text ); 405 $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' ); 406 $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' ); 407 $t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' ); 408 409 $query = "UPDATE $t_bugnote_text_table 410 SET note='$c_bugnote_text' 411 WHERE id='$t_bugnote_text_id'"; 412 db_query( $query ); 413 414 # updated the last_updated date 415 bugnote_date_update( $p_bugnote_id ); 416 417 # log new bugnote 418 history_log_event_special( $t_bug_id, BUGNOTE_UPDATED, bugnote_format_id( $p_bugnote_id ) ); 419 420 return true; 421 } 422 423 # -------------------- 424 # Set the view state of the bugnote 425 function bugnote_set_view_state( $p_bugnote_id, $p_private ) { 426 $c_bugnote_id = db_prepare_int( $p_bugnote_id ); 427 $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' ); 428 429 if ( $p_private ) { 430 $t_view_state = VS_PRIVATE; 431 } else { 432 $t_view_state = VS_PUBLIC; 433 } 434 435 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 436 437 # update view_state 438 $query = "UPDATE $t_bugnote_table 439 SET view_state='$t_view_state' 440 WHERE id='$c_bugnote_id'"; 441 db_query( $query ); 442 443 history_log_event_special( $t_bug_id, BUGNOTE_STATE_CHANGED, bugnote_format_id( $t_view_state ), $p_bugnote_id ); 444 445 return true; 446 } 447 448 449 #=================================== 450 # Other 451 #=================================== 452 453 # -------------------- 454 # Pad the bugnote id with the appropriate number of zeros for printing 455 function bugnote_format_id( $p_bugnote_id ) { 456 $t_padding = config_get( 'display_bugnote_padding' ); 457 458 return str_pad( $p_bugnote_id, $t_padding, '0', STR_PAD_LEFT ); 459 } 460 461 462 #=================================== 463 # Bugnote Stats 464 #=================================== 465 466 # -------------------- 467 # Returns an array of bugnote stats 468 # $p_from - Starting date (yyyy-mm-dd) inclusive, if blank, then ignored. 469 # $p_to - Ending date (yyyy-mm-dd) inclusive, if blank, then ignored. 470 function bugnote_stats_get_events_array( $p_bug_id, $p_from, $p_to ) { 471 $c_bug_id = db_prepare_int( $p_bug_id ); 472 $c_from = db_prepare_date( $p_from ); 473 $c_to = db_prepare_date( $p_to ); 474 475 $t_user_table = config_get( 'mantis_user_table' ); 476 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 477 478 if ( !is_blank( $c_from ) ) { 479 $t_from_where = " AND bn.date_submitted >= '$c_from 00:00:00'"; 480 } else { 481 $t_from_where = ''; 482 } 483 484 if ( !is_blank( $c_to ) ) { 485 $t_to_where = " AND bn.date_submitted <= '$c_to 23:59:59'"; 486 } else { 487 $t_to_where = ''; 488 } 489 490 $t_results = array(); 491 492 $query = "SELECT username, SUM(time_tracking) sum_time_tracking 493 FROM $t_user_table u, $t_bugnote_table bn 494 WHERE u.id = bn.reporter_id AND 495 bn.bug_id = '$c_bug_id' 496 $t_from_where $t_to_where 497 GROUP BY u.id, u.username"; 498 499 $result = db_query( $query ); 500 501 while ( $row = db_fetch_array( $result ) ) { 502 $t_results[] = $row; 503 } 504 505 return $t_results; 506 } 507 508 # -------------------- 509 # Returns an array of bugnote stats 510 # $p_from - Starting date (yyyy-mm-dd) inclusive, if blank, then ignored. 511 # $p_to - Ending date (yyyy-mm-dd) inclusive, if blank, then ignored. 512 function bugnote_stats_get_project_array( $p_project_id, $p_from, $p_to, $p_cost ) { 513 $c_project_id = db_prepare_int( $p_project_id ); 514 $c_to = db_prepare_date( $p_to ); 515 $c_from = db_prepare_date( $p_from ); 516 $c_cost = db_prepare_double( $p_cost ); 517 518 // MySQL 519 $t_bug_table = config_get( 'mantis_bug_table' ); 520 $t_user_table = config_get( 'mantis_user_table' ); 521 $t_bugnote_table = config_get( 'mantis_bugnote_table' ); 522 523 if ( !is_blank( $c_from ) ) { 524 $t_from_where = " AND bn.date_submitted >= '$c_from 00:00:00'"; 525 } else { 526 $t_from_where = ''; 527 } 528 529 if ( !is_blank( $c_to ) ) { 530 $t_to_where = " AND bn.date_submitted <= '$c_to 23:59:59'"; 531 } else { 532 $t_to_where = ''; 533 } 534 535 if ( ALL_PROJECTS != $c_project_id ) { 536 $t_project_where = " AND b.project_id = '$c_project_id' AND bn.bug_id = b.id "; 537 } else { 538 $t_project_where = ''; 539 } 540 541 $t_results = array(); 542 543 $query = "SELECT username, summary, bn.bug_id, SUM(time_tracking) sum_time_tracking 544 FROM $t_user_table u, $t_bugnote_table bn, $t_bug_table b 545 WHERE u.id = bn.reporter_id AND bn.time_tracking != 0 AND bn.bug_id = b.id 546 $t_project_where $t_from_where $t_to_where 547 GROUP BY bn.bug_id, u.id, u.username, b.summary 548 ORDER BY bn.bug_id"; 549 550 $result = db_query( $query ); 551 552 $t_cost_min = $c_cost / 60; 553 554 while ( $row = db_fetch_array( $result ) ) { 555 $t_total_cost = $t_cost_min * $row['sum_time_tracking']; 556 $row['cost'] = $t_total_cost; 557 $t_results[] = $row; 558 } 559 560 return $t_results; 561 } 562 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 09:42:17 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |