<?php

function dashboard_approvals($from_month_display_order, $to_month_display_order, $from_date, $to_date, $users_res, $dashboard_sc_type) {
    global $systypes_array;
    
    $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($month_ranges_array);
    
    start_table(TABLESTYLE,"width=$table_width%");
        
        $th = array("Transaction Type");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Average";

            table_header($th);
            
                $k = $previous_user_id = 0;
            
                    while ($user = db_fetch($users_res)) {

                        $user_id = $user['approved_by'];
                        $tran_type = $user['tran_type'];

                            if ($previous_user_id != $user_id) {
                                $real_name = get_user($user_id);
                                start_row();
                                    label_cell(spaces(10).text_bold($real_name['real_name']), 'colspan=14');
                                end_row();
                            }

                            alt_table_row_color($k);

                                label_cell($systypes_array[$user['tran_type']]);

                                $total_delay = $count = 0;
                                foreach ($month_ranges_array as $month) {

                                    $from_date = $month['month_begin'];
                                    $to_date = $month['month_end'];

                                    $delay = dashboard_get_user_approvals_period_summary($from_date, $to_date, $user_id, $tran_type);
                                        if ($delay == 0){
                                            label_cell("");
                                            continue;
                                        }
                                        
                                        $avg_delay = dashboard_format_approval_delay_time($delay);

                                        $parameters = "uid=".$user_id."&type=".$tran_type."&from_date=$from_date&to_date=$to_date";
                                        $str_delay = get_dashboard_view_str($dashboard_sc_type, $parameters, $avg_delay);
                                        label_cell($str_delay, "align=right");
                                        
                                        $total_delay += $delay;
                                        $count++;
                                }   

                                if ($count > 0) {
                                    $total_ave_delay = $total_delay/$count;
                                    $avg_t_delay = dashboard_format_approval_delay_time($total_ave_delay);
                                } else {
                                    $avg_t_delay = 0;
                                }

                                if ($avg_t_delay != 0) {
                                    label_cell($avg_t_delay, "align=right");
                                } else {
                                    label_cell("");
                                }
                                
                            end_row();

                        $previous_user_id = $user_id;
                    }
        
    end_table(2);
}

function dashboard_format_approval_delay_time($delay) {
    $delay_text = '';
    
        if ($delay <= 59) {
            if ($delay > 30) {
                $delay_text = '1m';
            } else {
                $delay_text = '0m';
            }

            return $delay_text;
        }

            // delay days
                if ($delay >= 86400) {
                    $d = intval($delay / 86400);
                    $delay_text .= $d.'d';

                    $delay = fmod($delay, 86400);
                }

            // delay hours
                if ($delay >= 3600) {
                    $h = intval($delay / 3600);
                    $delay_text .= strlen($delay_text) > 0 ? ':'.$h.'h' : $h.'h';

                    $delay = fmod($delay, 3600);
                } else {
                    if (strlen($delay_text) > 0) {
                        $delay_text .= ':0h';
                    }
                }

            // delay minutes
                if ($delay >= 60) {
                    $m = round($delay / 60);
                    $delay_text .= strlen($delay_text) > 0 ? ':'.$m.'m' : $m.'m';
                } else {
                    if ($delay > 30) {
                        $delay_text .= strlen($delay_text) > 0 ? ':1m' : '1m';
                    } else {
                        $delay_text .= strlen($delay_text) > 0 ? ':0m' : '0m';
                    }
                }

            return $delay_text;
}

?>