[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> bugnote_api.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Bugnote API
  19   *
  20   * @package CoreAPI
  21   * @subpackage BugnoteAPI
  22   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  23   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24   * @link http://www.mantisbt.org
  25   *
  26   * @uses access_api.php
  27   * @uses authentication_api.php
  28   * @uses bug_api.php
  29   * @uses bug_revision_api.php
  30   * @uses config_api.php
  31   * @uses constant_inc.php
  32   * @uses database_api.php
  33   * @uses email_api.php
  34   * @uses error_api.php
  35   * @uses event_api.php
  36   * @uses helper_api.php
  37   * @uses history_api.php
  38   * @uses lang_api.php
  39   * @uses user_api.php
  40   * @uses utility_api.php
  41   */
  42  
  43  require_api( 'access_api.php' );
  44  require_api( 'authentication_api.php' );
  45  require_api( 'bug_api.php' );
  46  require_api( 'bug_revision_api.php' );
  47  require_api( 'config_api.php' );
  48  require_api( 'constant_inc.php' );
  49  require_api( 'database_api.php' );
  50  require_api( 'email_api.php' );
  51  require_api( 'error_api.php' );
  52  require_api( 'event_api.php' );
  53  require_api( 'helper_api.php' );
  54  require_api( 'history_api.php' );
  55  require_api( 'lang_api.php' );
  56  require_api( 'user_api.php' );
  57  require_api( 'utility_api.php' );
  58  
  59  /**
  60   * Bugnote Data Structure Definition
  61   * @package MantisBT
  62   * @subpackage classes
  63   */
  64  class BugnoteData {
  65      var $id;
  66      var $bug_id;
  67      var $reporter_id;
  68      var $note;
  69      var $view_state;
  70      var $date_submitted;
  71      var $last_modified;
  72      var $note_type;
  73      var $note_attr;
  74      var $time_tracking;
  75  }
  76  
  77  /**
  78   * Check if a bugnote with the given ID exists
  79   * return true if the bugnote exists, false otherwise
  80   * @param int $p_bugnote_id bugnote id
  81   * @return bool
  82   * @access public
  83   */
  84  function bugnote_exists( $p_bugnote_id ) {
  85      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
  86      $t_bugnote_table = db_get_table( 'bugnote' );
  87  
  88      $query = "SELECT COUNT(*)
  89                        FROM $t_bugnote_table
  90                        WHERE id=" . db_param();
  91      $result = db_query_bound( $query, Array( $c_bugnote_id ) );
  92  
  93      if( 0 == db_result( $result ) ) {
  94          return false;
  95      } else {
  96          return true;
  97      }
  98  }
  99  
 100  /**
 101   * Check if a bugnote with the given ID exists
 102   * return true if the bugnote exists, raise an error if not
 103   * @param int $p_bugnote_id bugnote id
 104   * @access public
 105   */
 106  function bugnote_ensure_exists( $p_bugnote_id ) {
 107      if( !bugnote_exists( $p_bugnote_id ) ) {
 108          trigger_error( ERROR_BUGNOTE_NOT_FOUND, ERROR );
 109      }
 110  }
 111  
 112  /**
 113   * Check if the given user is the reporter of the bugnote
 114   * return true if the user is the reporter, false otherwise
 115   * @param int $p_bugnote_id bugnote id
 116   * @param int $p_user_id user id
 117   * @return bool
 118   * @access public
 119   */
 120  function bugnote_is_user_reporter( $p_bugnote_id, $p_user_id ) {
 121      if( bugnote_get_field( $p_bugnote_id, 'reporter_id' ) == $p_user_id ) {
 122          return true;
 123      } else {
 124          return false;
 125      }
 126  }
 127  
 128  /**
 129   * Add a bugnote to a bug
 130   * return the ID of the new bugnote
 131   * @param int $p_bug_id bug id
 132   * @param string $p_bugnote_text bugnote text
 133   * @param string $p_time_tracking hh:mm string
 134   * @param bool $p_private whether bugnote is private
 135   * @param int $p_type bugnote type
 136   * @param string $p_attr
 137   * @param int $p_user_id user id
 138   * @param bool $p_send_email generate email?
 139   * @param int $p_date_submitted date submitted (defaults to now())
 140   * @param int $p_last_modified last modification date (defaults to now())
 141   * @param bool $p_skip_bug_update skip bug last modification update (useful when importing bugs/bugnotes)
 142   * @return false|int false or indicating bugnote id added
 143   * @access public
 144   */
 145  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, $p_send_email = TRUE, $p_date_submitted = 0, $p_last_modified = 0, $p_skip_bug_update = FALSE ) {
 146      $c_bug_id = db_prepare_int( $p_bug_id );
 147      $c_time_tracking = helper_duration_to_minutes( $p_time_tracking );
 148      $c_private = db_prepare_bool( $p_private );
 149      $c_type = db_prepare_int( $p_type );
 150      $c_date_submitted = $p_date_submitted <= 0 ? db_now() : db_prepare_int( $p_date_submitted );
 151      $c_last_modified = $p_last_modified <= 0 ? db_now() : db_prepare_int( $p_last_modified );
 152  
 153      $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 154      $t_bugnote_table = db_get_table( 'bugnote' );
 155  
 156      $t_time_tracking_enabled = config_get( 'time_tracking_enabled' );
 157      $t_time_tracking_without_note = config_get( 'time_tracking_without_note' );
 158  
 159      if( ON == $t_time_tracking_enabled && $c_time_tracking > 0 ) {
 160          if( is_blank( $p_bugnote_text ) && OFF == $t_time_tracking_without_note ) {
 161              error_parameters( lang_get( 'bugnote' ) );
 162              trigger_error( ERROR_EMPTY_FIELD, ERROR );
 163          }
 164          $c_type = TIME_TRACKING;
 165      } else if( is_blank( $p_bugnote_text ) ) {
 166          return false;
 167      }
 168  
 169      $t_bugnote_text = $p_bugnote_text;
 170  
 171      # Event integration
 172      $t_bugnote_text = event_signal( 'EVENT_BUGNOTE_DATA', $t_bugnote_text, $c_bug_id );
 173  
 174      # insert bugnote text
 175      $query = 'INSERT INTO ' . $t_bugnote_text_table . ' ( note ) VALUES ( ' . db_param() . ' )';
 176      db_query_bound( $query, Array( $t_bugnote_text ) );
 177  
 178      # retrieve bugnote text id number
 179      $t_bugnote_text_id = db_insert_id( $t_bugnote_text_table );
 180  
 181      # get user information
 182      if( $p_user_id === null ) {
 183          $c_user_id = auth_get_current_user_id();
 184      } else {
 185          $c_user_id = db_prepare_int( $p_user_id );
 186      }
 187  
 188      # Check for private bugnotes.
 189      if( $c_private && access_has_bug_level( config_get( 'set_view_status_threshold' ), $p_bug_id, $c_user_id ) ) {
 190          $t_view_state = VS_PRIVATE;
 191      } else {
 192          $t_view_state = VS_PUBLIC;
 193      }
 194  
 195      # insert bugnote info
 196      $query = "INSERT INTO $t_bugnote_table
 197                  (bug_id, reporter_id, bugnote_text_id, view_state, date_submitted, last_modified, note_type, note_attr, time_tracking )
 198              VALUES
 199                  (" . db_param() . ', ' . db_param() . ',' . db_param() . ', ' . db_param() . ', ' . db_param() . ',' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
 200      db_query_bound( $query, Array( $c_bug_id, $c_user_id, $t_bugnote_text_id, $t_view_state, $c_date_submitted, $c_last_modified, $c_type, $p_attr, $c_time_tracking ) );
 201  
 202      # get bugnote id
 203      $t_bugnote_id = db_insert_id( $t_bugnote_table );
 204  
 205      # update bug last updated
 206      if ( !$p_skip_bug_update ) {
 207          bug_update_date( $p_bug_id );
 208      }
 209  
 210      # log new bug
 211      history_log_event_special( $p_bug_id, BUGNOTE_ADDED, bugnote_format_id( $t_bugnote_id ) );
 212  
 213      # Event integration
 214      event_signal( 'EVENT_BUGNOTE_ADD', array( $p_bug_id, $t_bugnote_id ) );
 215  
 216      # only send email if the text is not blank, otherwise, it is just recording of time without a comment.
 217      if( TRUE == $p_send_email && !is_blank( $t_bugnote_text ) ) {
 218          email_bugnote_add( $p_bug_id );
 219      }
 220  
 221      return $t_bugnote_id;
 222  }
 223  
 224  /**
 225   * Delete a bugnote
 226   * @param int $p_bugnote_id bug note id
 227   * @return bool
 228   * @access public
 229   */
 230  function bugnote_delete( $p_bugnote_id ) {
 231      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
 232      $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
 233      $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
 234      $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 235      $t_bugnote_table = db_get_table( 'bugnote' );
 236  
 237      # Remove the bugnote
 238      $query = 'DELETE FROM ' . $t_bugnote_table . ' WHERE id=' . db_param();
 239      db_query_bound( $query, Array( $c_bugnote_id ) );
 240  
 241      # Remove the bugnote text
 242      $query = 'DELETE FROM ' . $t_bugnote_text_table . ' WHERE id=' . db_param();
 243      db_query_bound( $query, Array( $t_bugnote_text_id ) );
 244  
 245      # log deletion of bug
 246      history_log_event_special( $t_bug_id, BUGNOTE_DELETED, bugnote_format_id( $p_bugnote_id ) );
 247  
 248      return true;
 249  }
 250  
 251  /**
 252   * delete all bugnotes associated with the given bug
 253   * @param int $p_bug_id bug id
 254   * @return bool
 255   * @access public
 256   */
 257  function bugnote_delete_all( $p_bug_id ) {
 258      $c_bug_id = db_prepare_int( $p_bug_id );
 259      $t_bugnote_table = db_get_table( 'bugnote' );
 260      $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 261  
 262      # Delete the bugnote text items
 263      $query = "SELECT bugnote_text_id
 264                        FROM $t_bugnote_table
 265                        WHERE bug_id=" . db_param();
 266      $result = db_query_bound( $query, Array( $c_bug_id ) );
 267      $bugnote_count = db_num_rows( $result );
 268      for( $i = 0;$i < $bugnote_count;$i++ ) {
 269          $row = db_fetch_array( $result );
 270          $t_bugnote_text_id = $row['bugnote_text_id'];
 271  
 272          # Delete the corresponding bugnote texts
 273          $query = "DELETE FROM $t_bugnote_text_table
 274                            WHERE id=" . db_param();
 275          db_query_bound( $query, Array( $t_bugnote_text_id ) );
 276      }
 277  
 278      # Delete the corresponding bugnotes
 279      $query = "DELETE FROM $t_bugnote_table
 280                        WHERE bug_id=" . db_param();
 281      $result = db_query_bound( $query, Array( $c_bug_id ) );
 282  
 283      # db_query errors on failure so:
 284      return true;
 285  }
 286  
 287  /**
 288   * Get the text associated with the bugnote
 289   * @param int $p_bugnote_id bugnote id
 290   * @return string bugnote text
 291   * @access public
 292   */
 293  function bugnote_get_text( $p_bugnote_id ) {
 294      $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
 295      $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 296  
 297      # grab the bugnote text
 298      $query = "SELECT note
 299                        FROM $t_bugnote_text_table
 300                        WHERE id=" . db_param();
 301      $result = db_query_bound( $query, Array( $t_bugnote_text_id ) );
 302  
 303      return db_result( $result );
 304  }
 305  
 306  /**
 307   * Get a field for the given bugnote
 308   * @param int $p_bugnote_id bugnote id
 309   * @param string $p_field_name field name
 310   * @return string field value
 311   * @access public
 312   */
 313  function bugnote_get_field( $p_bugnote_id, $p_field_name ) {
 314      global $g_cache_bugnote;
 315  
 316      if( isset( $g_cache_bugnote[(int)$p_bugnote_id] ) ) {
 317          return $g_cache_bugnote[(int)$p_bugnote_id]->$p_field_name;
 318      }
 319  
 320      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
 321      $c_field_name = db_prepare_string( $p_field_name );
 322      $t_bugnote_table = db_get_table( 'bugnote' );
 323  
 324      $query = "SELECT $c_field_name
 325                        FROM $t_bugnote_table
 326                        WHERE id=" . db_param();
 327      $result = db_query_bound( $query, Array( $c_bugnote_id ), 1 );
 328  
 329      return db_result( $result );
 330  }
 331  
 332  /**
 333   * Get latest bugnote id
 334   * @param int $p_bug_id bug id
 335   * @return int latest bugnote id
 336   * @access public
 337   */
 338  function bugnote_get_latest_id( $p_bug_id ) {
 339      $c_bug_id = db_prepare_int( $p_bug_id );
 340      $t_bugnote_table = db_get_table( 'bugnote' );
 341  
 342      $query = "SELECT id
 343                        FROM $t_bugnote_table
 344                        WHERE bug_id=" . db_param() . "
 345                        ORDER by last_modified DESC";
 346      $result = db_query_bound( $query, Array( $c_bug_id ), 1 );
 347  
 348      return (int)db_result( $result );
 349  }
 350  
 351  /**
 352   * Build the bugnotes array for the given bug_id filtered by specified $p_user_access_level.
 353   * Bugnotes are sorted by date_submitted according to 'bugnote_order' configuration setting.
 354   * Return BugnoteData class object with raw values from the tables except the field
 355   * last_modified - it is UNIX_TIMESTAMP.
 356   * @param int $p_bug_id bug id
 357   * @param int $p_user_bugnote_order sort order
 358   * @param int $p_user_bugnote_limit number of bugnotes to display to user
 359   * @param int $p_user_id user id
 360   * @return array array of bugnotes
 361   * @access public
 362   */
 363  function bugnote_get_all_visible_bugnotes( $p_bug_id, $p_user_bugnote_order, $p_user_bugnote_limit, $p_user_id = null ) {
 364      if( $p_user_id === null ) {
 365          $t_user_id = auth_get_current_user_id();
 366      } else {
 367          $t_user_id = $p_user_id;
 368      }
 369  
 370      $t_project_id = bug_get_field( $p_bug_id, 'project_id' );
 371      $t_user_access_level = user_get_access_level( $t_user_id, $t_project_id );
 372  
 373      $t_all_bugnotes = bugnote_get_all_bugnotes( $p_bug_id );
 374      $t_private_bugnote_threshold = config_get( 'private_bugnote_threshold' );
 375  
 376      $t_private_bugnote_visible = access_compare_level( $t_user_access_level, config_get( 'private_bugnote_threshold' ) );
 377      $t_time_tracking_visible = access_compare_level( $t_user_access_level, config_get( 'time_tracking_view_threshold' ) );
 378  
 379      $t_bugnotes = array();
 380      $t_bugnote_count = count( $t_all_bugnotes );
 381      $t_bugnote_limit = $p_user_bugnote_limit > 0 ? $p_user_bugnote_limit : $t_bugnote_count;
 382      $t_bugnotes_found = 0;
 383  
 384      # build a list of the latest bugnotes that the user can see
 385      for ( $i = 0; ( $i < $t_bugnote_count ) && ( $t_bugnotes_found < $t_bugnote_limit ); $i++ ) {
 386          $t_bugnote = array_pop( $t_all_bugnotes );
 387  
 388          if( $t_private_bugnote_visible || $t_bugnote->reporter_id == $t_user_id || ( VS_PUBLIC == $t_bugnote->view_state ) ) {
 389  
 390              # If the access level specified is not enough to see time tracking information
 391              # then reset it to 0.
 392              if( !$t_time_tracking_visible ) {
 393                  $t_bugnote->time_tracking = 0;
 394              }
 395  
 396              $t_bugnotes[$t_bugnotes_found++] = $t_bugnote;
 397          }
 398      }
 399  
 400      # reverse the list for users with ascending view preferences
 401      if ( 'ASC' == $p_user_bugnote_order ) {
 402          $t_bugnotes = array_reverse( $t_bugnotes );
 403      }
 404  
 405      return $t_bugnotes;
 406  }
 407  
 408  /**
 409   * Build the bugnotes array for the given bug_id.
 410   * Return BugnoteData class object with raw values from the tables except the field
 411   * last_modified - it is UNIX_TIMESTAMP.
 412   * The data is not filtered by VIEW_STATE !!
 413   * @param int $p_bug_id bug id
 414   * @return array array of bugnotes
 415   * @access public
 416   */
 417  function bugnote_get_all_bugnotes( $p_bug_id ) {
 418      global $g_cache_bugnotes, $g_cache_bugnote;
 419  
 420      if( !isset( $g_cache_bugnotes ) ) {
 421          $g_cache_bugnotes = array();
 422      }
 423  
 424      if( !isset( $g_cache_bugnote ) ) {
 425          $g_cache_bugnote = array();
 426      }
 427  
 428      # the cache should be aware of the sorting order
 429      if( !isset( $g_cache_bugnotes[(int)$p_bug_id] ) ) {
 430          $t_bugnote_table = db_get_table( 'bugnote' );
 431          $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 432  
 433          # sort by bugnote id which should be more accurate than submit date, since two bugnotes
 434          # may be submitted at the same time if submitted using a script (eg: MantisConnect).
 435          $t_query = "SELECT b.*, t.note
 436                            FROM      $t_bugnote_table b
 437                            LEFT JOIN $t_bugnote_text_table t ON b.bugnote_text_id = t.id
 438                          WHERE b.bug_id=" . db_param() . '
 439                          ORDER BY b.id ASC';
 440          $t_bugnotes = array();
 441  
 442          # BUILD bugnotes array
 443          $t_result = db_query_bound( $t_query, array( $p_bug_id ) );
 444  
 445          while( $row = db_fetch_array( $t_result ) ) {
 446              $t_bugnote = new BugnoteData;
 447  
 448              $t_bugnote->id = $row['id'];
 449              $t_bugnote->bug_id = $row['bug_id'];
 450              $t_bugnote->note = $row['note'];
 451              $t_bugnote->view_state = $row['view_state'];
 452              $t_bugnote->reporter_id = $row['reporter_id'];
 453              $t_bugnote->date_submitted = $row['date_submitted'];
 454              $t_bugnote->last_modified = $row['last_modified'];
 455              $t_bugnote->note_type = $row['note_type'];
 456              $t_bugnote->note_attr = $row['note_attr'];
 457              $t_bugnote->time_tracking = $row['time_tracking'];
 458  
 459              $t_bugnotes[] = $t_bugnote;
 460              $g_cache_bugnote[(int)$t_bugnote->id] = $t_bugnote;
 461          }
 462  
 463          $g_cache_bugnotes[(int)$p_bug_id] = $t_bugnotes;
 464      }
 465  
 466      return $g_cache_bugnotes[(int)$p_bug_id];
 467  }
 468  
 469  /**
 470   * Update the time_tracking field of the bugnote
 471   * @param int $p_bugnote_id bugnote id
 472   * @param string $p_time_tracking timetracking string (hh:mm format)
 473   * @return bool
 474   * @access public
 475   */
 476  function bugnote_set_time_tracking( $p_bugnote_id, $p_time_tracking ) {
 477      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
 478      $c_bugnote_time_tracking = helper_duration_to_minutes( $p_time_tracking );
 479      $t_bugnote_table = db_get_table( 'bugnote' );
 480  
 481      $query = "UPDATE $t_bugnote_table
 482                  SET time_tracking = " . db_param() . "
 483                  WHERE id=" . db_param();
 484      db_query_bound( $query, Array( $c_bugnote_time_tracking, $c_bugnote_id ) );
 485  
 486      # db_query errors if there was a problem so:
 487      return true;
 488  }
 489  
 490  /**
 491   * Update the last_modified field of the bugnote
 492   * @param int $p_bugnote_id bugnote id
 493   * @return bool
 494   * @access public
 495   */
 496  function bugnote_date_update( $p_bugnote_id ) {
 497      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
 498      $t_bugnote_table = db_get_table( 'bugnote' );
 499  
 500      $query = "UPDATE $t_bugnote_table
 501                      SET last_modified=" . db_param() . "
 502                      WHERE id=" . db_param();
 503      db_query_bound( $query, Array( db_now(), $c_bugnote_id ) );
 504  
 505      # db_query errors if there was a problem so:
 506      return true;
 507  }
 508  
 509  /**
 510   * Set the bugnote text
 511   * @param int $p_bugnote_id bugnote id
 512   * @param string $p_bugnote_text bugnote text
 513   * @return bool
 514   * @access public
 515   */
 516  function bugnote_set_text( $p_bugnote_id, $p_bugnote_text ) {
 517      $t_old_text = bugnote_get_text( $p_bugnote_id );
 518  
 519      if ( $t_old_text == $p_bugnote_text ) {
 520          return true;
 521      }
 522  
 523      $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
 524      $t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
 525      $t_bugnote_text_table = db_get_table( 'bugnote_text' );
 526  
 527      # insert an 'original' revision if needed
 528      if ( bug_revision_count( $t_bug_id, REV_BUGNOTE, $p_bugnote_id ) < 1 ) {
 529          $t_user_id = bugnote_get_field( $p_bugnote_id, 'reporter_id' );
 530          $t_timestamp = bugnote_get_field( $p_bugnote_id, 'last_modified' );
 531          bug_revision_add( $t_bug_id, $t_user_id, REV_BUGNOTE, $t_old_text, $p_bugnote_id, $t_timestamp );
 532      }
 533  
 534      $query = "UPDATE $t_bugnote_text_table
 535              SET note=" . db_param() . " WHERE id=" . db_param();
 536      db_query_bound( $query, Array( $p_bugnote_text, $t_bugnote_text_id ) );
 537  
 538      # updated the last_updated date
 539      bugnote_date_update( $p_bugnote_id );
 540  
 541      # insert a new revision
 542      $t_user_id = auth_get_current_user_id();
 543      $t_revision_id = bug_revision_add( $t_bug_id, $t_user_id, REV_BUGNOTE, $p_bugnote_text, $p_bugnote_id );
 544  
 545      # log new bugnote
 546      history_log_event_special( $t_bug_id, BUGNOTE_UPDATED, bugnote_format_id( $p_bugnote_id ), $t_revision_id );
 547  
 548      return true;
 549  }
 550  
 551  /**
 552   * Set the view state of the bugnote
 553   * @param int $p_bugnote_id bugnote id
 554   * @param bool $p_private
 555   * @return bool
 556   * @access public
 557   */
 558  function bugnote_set_view_state( $p_bugnote_id, $p_private ) {
 559      $c_bugnote_id = db_prepare_int( $p_bugnote_id );
 560      $t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
 561  
 562      if( $p_private ) {
 563          $t_view_state = VS_PRIVATE;
 564      } else {
 565          $t_view_state = VS_PUBLIC;
 566      }
 567  
 568      $t_bugnote_table = db_get_table( 'bugnote' );
 569  
 570      $query = "UPDATE $t_bugnote_table
 571                        SET view_state=" . db_param() . "
 572                        WHERE id=" . db_param();
 573      db_query_bound( $query, Array( $t_view_state, $c_bugnote_id ) );
 574  
 575      history_log_event_special( $t_bug_id, BUGNOTE_STATE_CHANGED, $t_view_state, bugnote_format_id( $p_bugnote_id ) );
 576  
 577      return true;
 578  }
 579  
 580  /**
 581   * Pad the bugnote id with the appropriate number of zeros for printing
 582   * @param int $p_bugnote_id bugnote id
 583   * @return string
 584   * @access public
 585   */
 586  function bugnote_format_id( $p_bugnote_id ) {
 587      $t_padding = config_get( 'display_bugnote_padding' );
 588  
 589      return utf8_str_pad( $p_bugnote_id, $t_padding, '0', STR_PAD_LEFT );
 590  }
 591  
 592  /**
 593   * Returns an array of bugnote stats
 594   * @param int $p_bug_id bug id
 595   * @param string $p_from Starting date (yyyy-mm-dd) inclusive, if blank, then ignored.
 596   * @param string $p_to Ending date (yyyy-mm-dd) inclusive, if blank, then ignored.
 597   * @return array array of bugnote stats
 598   * @access public
 599   */
 600  function bugnote_stats_get_events_array( $p_bug_id, $p_from, $p_to ) {
 601      $c_bug_id = db_prepare_int( $p_bug_id );
 602      $c_to = strtotime( $p_to ) + SECONDS_PER_DAY - 1;
 603      $c_from = strtotime( $p_from );
 604  
 605      $t_user_table = db_get_table( 'user' );
 606      $t_bugnote_table = db_get_table( 'bugnote' );
 607  
 608      if( !is_blank( $c_from ) ) {
 609          $t_from_where = " AND bn.date_submitted >= $c_from ";
 610      } else {
 611          $t_from_where = '';
 612      }
 613  
 614      if( !is_blank( $c_to ) ) {
 615          $t_to_where = " AND bn.date_submitted <= $c_to ";
 616      } else {
 617          $t_to_where = '';
 618      }
 619  
 620      $t_results = array();
 621  
 622      $query = "SELECT username, SUM(time_tracking) AS sum_time_tracking
 623                  FROM $t_user_table u, $t_bugnote_table bn
 624                  WHERE u.id = bn.reporter_id AND
 625                  bn.bug_id = '$c_bug_id'
 626                  $t_from_where $t_to_where
 627              GROUP BY u.id, u.username";
 628  
 629      $result = db_query( $query );
 630  
 631      while( $row = db_fetch_array( $result ) ) {
 632          $t_results[] = $row;
 633      }
 634  
 635      return $t_results;
 636  }
 637  
 638  /**
 639   * Returns an array of bugnote stats
 640   * @param int $p_project_id project id
 641   * @param string $p_from Starting date (yyyy-mm-dd) inclusive, if blank, then ignored.
 642   * @param string $p_to Ending date (yyyy-mm-dd) inclusive, if blank, then ignored.
 643   * @param int $p_cost cost
 644   * @return array array of bugnote stats
 645   * @access public
 646   */
 647  function bugnote_stats_get_project_array( $p_project_id, $p_from, $p_to, $p_cost ) {
 648      $c_project_id = db_prepare_int( $p_project_id );
 649  
 650      $c_to = strtotime( $p_to ) + SECONDS_PER_DAY - 1;
 651      $c_from = strtotime( $p_from );
 652  
 653      if ( $c_to === false || $c_from === false ) {
 654          error_parameters( array( $p_form, $p_to ) );
 655          trigger_error( ERROR_GENERIC, ERROR );
 656      }
 657  
 658      $t_bug_table = db_get_table( 'bug' );
 659      $t_user_table = db_get_table( 'user' );
 660      $t_bugnote_table = db_get_table( 'bugnote' );
 661  
 662      if( !is_blank( $c_from ) ) {
 663          $t_from_where = " AND bn.date_submitted >= $c_from";
 664      } else {
 665          $t_from_where = '';
 666      }
 667  
 668      if( !is_blank( $c_to ) ) {
 669          $t_to_where = " AND bn.date_submitted <= $c_to";
 670      } else {
 671          $t_to_where = '';
 672      }
 673  
 674      if( ALL_PROJECTS != $c_project_id ) {
 675          $t_project_where = " AND b.project_id = '$c_project_id' AND bn.bug_id = b.id ";
 676      } else {
 677          $t_project_where = '';
 678      }
 679  
 680      $t_results = array();
 681  
 682      $query = "SELECT username, summary, bn.bug_id, SUM(time_tracking) AS sum_time_tracking
 683              FROM $t_user_table u, $t_bugnote_table bn, $t_bug_table b
 684              WHERE u.id = bn.reporter_id AND bn.time_tracking != 0 AND bn.bug_id = b.id
 685              $t_project_where $t_from_where $t_to_where
 686              GROUP BY bn.bug_id, u.id, u.username, b.summary
 687              ORDER BY bn.bug_id";
 688  
 689      $result = db_query( $query );
 690  
 691      $t_cost_min = $p_cost / 60.0;
 692  
 693      while( $row = db_fetch_array( $result ) ) {
 694          $t_total_cost = $t_cost_min * $row['sum_time_tracking'];
 695          $row['cost'] = $t_total_cost;
 696          $t_results[] = $row;
 697      }
 698  
 699      return $t_results;
 700  }
 701  
 702  /**
 703   * Clear a bugnote from the cache or all bug notes if no bugnote id specified.
 704   * @param int bugnote id to clear (optional)
 705   * @return null
 706   * @access public
 707   */
 708  function bugnote_clear_cache( $p_bugnote_id = null ) {
 709      global $g_cache_bugnote, $g_cache_bugnotes;
 710  
 711      if( null === $p_bugnote_id ) {
 712          $g_cache_bugnote = array();
 713      } else {
 714          unset( $g_cache_bugnote[(int) $p_bugnote_id] );
 715      }
 716      $g_cache_bugnotes = array();
 717  
 718      return true;
 719  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7