<?php

/************************************************************************/
/* DOCEBO LMS - Learning managment system									*/
/* ============================================							*/
/*																			*/
/* Copyright (c) 2005														*/
/* http://www.docebo.com													*/
/*																			*/
/* This program 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.		*/
/************************************************************************/

if(!defined('IN_DOCEBO')) die('You cannot access this file directly');

/**
 * @version  $Id: class.report_delay.php 573 2006-08-23 09:38:54Z fabio $
 * @author	 Fabio Pirovano <fabio [at] docebo [dot] com>
 */

require_once($GLOBALS['where_lms'].'/admin/modules/report/class.report.php');
require_once($GLOBALS['where_lms'].'/lib/lib.course.php');

class Report_Delay extends Report {
	
	var $lang;
	
	function Report_Delay($id_report) {
		$this->lang =& DoceboLanguage::createInstance('ru_report');
		parent::Report($id_report);
	}
	
	function play($jump_url, $back_url, $alluser, $org_chart_subdivision, $start_time, $end_time) {
		
		require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
		require_once($GLOBALS['where_framework'].'/lib/lib.directory.php');
		require_once($GLOBALS['where_framework'].'/class.module/class.directory.php');
		require_once($GLOBALS['where_lms'].'/lib/lib.course.php');
		
		$lang =& DoceboLanguage::createInstance('report');
		
		$page_title = getTitleArea(array(
			str_replace('&', '&amp;', $back_url) => $lang->def('_REPORT'),
			$this->report_name
		), 'report', $lang->def('_ALT_REPORT'));
		
		if($alluser == 1) {
			
			$GLOBALS['page']->add(
				$page_title
				.'<div class="std_block">'
				.getBackUi(str_replace('&', '&amp;', $back_url), $lang->def('_BACK')), 'content');
			$this->show_report($jump_url, true, $org_chart_subdivision, $start_time, $end_time);
			$GLOBALS['page']->add('</div>', 'content');
			return;
		}
		if(isset($_POST['cancelselector'])) {
			
			jumpTo($back_url);
		} elseif(isset($_POST['okselector'])) {
			
			$GLOBALS['page']->add(
				$page_title
				.'<div class="std_block">'
				.getBackUi(str_replace('&', '&amp;', $back_url), $lang->def('_BACK')), 'content');
			$this->show_report($jump_url, false, $org_chart_subdivision, $start_time, $end_time);
			$GLOBALS['page']->add('</div>', 'content');
			return;
		} elseif(isset($_POST['export_cvs'])) {
			
			$this->exportAsCvs($org_chart_subdivision, $start_time, $end_time);
		} else {
			
			// first step load selector 
			require_once($GLOBALS['where_framework'].'/class.module/class.directory.php');
			
			$aclManager = new DoceboACLManager();
			$user_select = new Module_Directory();
			
			if($org_chart_subdivision == 0) {
				
				$user_select->show_user_selector = TRUE;
				$user_select->show_group_selector = TRUE;
			} else {
				
				$user_select->show_user_selector = FALSE;
				$user_select->show_group_selector = FALSE;
			}
			$user_select->show_orgchart_selector = TRUE;
			$user_select->show_orgchart_simple_selector = FALSE;
			$user_select->multi_choice = TRUE;
			
			$user_select->addFormInfo(
				Form::getHidden('start_time', 'start_time', $start_time)
				.Form::getHidden('end_time', 'end_time', $end_time)
				.Form::getHidden('org_chart_subdivision', 'org_chart_subdivision', $org_chart_subdivision)
				.Form::getHidden('alluser', 'alluser', $alluser)
			);
			$user_select->setPageTitle($page_title);
			$user_select->loadSelector(str_replace('&', '&amp;', $jump_url), 
					false, 
					$lang->def('_CHOOSE_USER_FOR_REPORT'), 
					true, 
					true );
		}
		
	}
	
	function show_report($jump_url, $alluser, $org_chart_subdivision, $start_time, $end_time) {
		
		$lang =& DoceboLanguage::createInstance('rd_report');
		
		$acl_man 		= new DoceboACLManager();
		$course_man 	= new Man_Course();
		
		if($alluser == 0) {
			$user_select 	= new Module_Directory();
			$elem_selected 	= $user_select->getSelection($_POST);
			$user_selected 	=& $acl_man->getAllUsersFromIdst($elem_selected);
			
		} else {
			
			$user_selected =& $this->getAllUserIdst();
		}
		if($org_chart_subdivision == 1) {
			
			require_once($GLOBALS['where_framework'].'/lib/lib.orgchart.php');
			$org_man 	= new OrgChartManager();
			if($alluser == 1) {
				$elem_selected = $org_man->getAllGroupIdFolder();
			}
			$org_name = $org_man->getFolderFormIdst($elem_selected);
		}
		
		if(empty($user_selected)) {
			
			$GLOBALS['page']->add($lang->def('_NULL_SELECTION'), 'content');
			return;
		}
		$users_info =& $acl_man->getUsers($user_selected);
		
		// Retrive all the course
		$course =& $course_man->getAllCoursesWithMoreInfo();
		if(empty($course)) {
			
			$GLOBALS['page']->add($lang->def('_NULL_COURSE_SELECTION'), 'content');
			return;
		}
		
		// css
		$GLOBALS['page']->add(
			"\n".'<link href="'.getPathTemplate('lms').'style/report/style_report_delay.css" rel="stylesheet" type="text/css" />'."\n"
		, 'page_head');
		
		// Export as cvs
		$GLOBALS['page']->add(
			Form::openForm('export', ereg_replace('&', '&amp;', $jump_url))
			.Form::openElementSpace('text_withborder')
			.Form::getHidden('user_selected', 'user_selected', urlencode(serialize($user_selected)))
			.form::getButton('export_cvs', 'export_cvs', $lang->def('_CSVEXPORT'), 'export_cvs')
			.Form::closeElementSpace()
			.Form::closeForm()
		, 'content');
		
		if($org_chart_subdivision === 0) {
			
			$now = time();
			$num_zeromonth = array();
			$num_onemonth = array();
			$num_twomonth = array();
			$num_iscr = array();
			
			$query_time = "
			SELECT idCourse, UNIX_TIMESTAMP(date_inscr), UNIX_TIMESTAMP(date_first_access), UNIX_TIMESTAMP(date_complete)
			FROM ".$GLOBALS['prefix_lms']."_courseuser 
			WHERE idUser IN (".implode(',', $user_selected).") ";
			if($start_time != '') $query_time .= " AND cu.date_inscr >= '".$start_time."' ";
			if($end_time != '') $query_time .= " AND cu.date_inscr <= '".$end_time."' ";
			$re_time = mysql_query($query_time);
			
			while(list($id_cp, $d_iscr, $d_first, $d_complete) = mysql_fetch_row($re_time)) {
				
				if( isset($num_iscr[$id_cp]) ) ++$num_iscr[$id_cp];
				else $num_iscr[$id_cp] = 1;
				if($d_complete === NULL) {
					//not complete
					//late is difference between iscr and now
					$day_late = (int)(($now - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
				} else {
					//complete
					//late is difference between iscr and complete
					$day_late = (int)(($d_complete - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
				}
				//if is in late
				if($day_late > 0) {
					if( $day_late < 30 ) {
						if( isset($num_zeromonth[$id_cp]) ) ++$num_zeromonth[$id_cp];
						else $num_zeromonth[$id_cp] = 1;
					} elseif( $day_late < 60 ) {
						if( isset($num_onemonth[$id_cp]) ) ++$num_onemonth[$id_cp];
						else $num_onemonth[$id_cp] = 1;
					} else {
						if( isset($num_twomonth[$id_cp]) ) ++$num_twomonth[$id_cp];
						else $num_twomonth[$id_cp] = 1;
					}
				}
			}
			$this->_printTable($course, $num_zeromonth, $num_onemonth, $num_twomonth, $num_iscr);
		} else {
			
			$date_now = $GLOBALS['regset']->databaseToRegional(date("Y-m-d H:i:s")); 
			
			reset($org_name);
			while(list($idst_group, $folder_name) = each($org_name)) {
				
				$GLOBALS['page']->add('<div class="datasummary">'
					.'<b>'.$lang->def('_FOLDER_NAME').' :</b> '.$folder_name['name']
						.( $folder_name['type_of_folder'] == ORG_CHART_WITH_DESCENDANTS ? ' ('.$lang->def('_WITH_DESCENDANTS').')' : '' ).'<br />'
					, 'content');
				if($start_time != '' || $end_time != '') {
					
					$GLOBALS['page']->add('<b>'.$lang->def('_TIME_BELT_2').' :</b> '
						.( $start_time != '' 	? ' <b>'.$lang->def('_START_TIME').' </b>'.$GLOBALS['regset']->databaseToRegional($start_time, 'date') 	: '' )
						.( $end_time != '' 		? ' <b>'.$lang->def('_END_TIME').' </b>'.$GLOBALS['regset']->databaseToRegional($end_time, 'date') 		: '' )
						.'<br />'
						, 'content');
				}
				$GLOBALS['page']->add( '<b>'.$lang->def('_ANALYSIS_DATE').' :</b> '.$date_now.'<br />'
				.'</div>', 'content');
				
				$group_user = $acl_man->getGroupAllUser($idst_group);
				
				$now = time();
				$num_zeromonth = array();
				$num_onemonth = array();
				$num_twomonth = array();
				$num_iscr = array();
				
				$query_time = "
				SELECT idCourse, UNIX_TIMESTAMP(date_inscr), UNIX_TIMESTAMP(date_first_access), UNIX_TIMESTAMP(date_complete)
				FROM ".$GLOBALS['prefix_lms']."_courseuser 
				WHERE idUser IN (".implode(',', $group_user).") ";
				if($start_time != '') $query_time .= " AND date_inscr >= '".$start_time."' ";
				if($end_time != '') $query_time .= " AND date_inscr <= '".$end_time."' ";
				$re_time = mysql_query($query_time);
				
				while(list($id_cp, $d_iscr, $d_first, $d_complete) = mysql_fetch_row($re_time)) {
					
					if( isset($num_iscr[$id_cp]) ) ++$num_iscr[$id_cp];
					else $num_iscr[$id_cp] = 1;
					if($d_complete === NULL) {
						//not complete
						//late is difference between iscr and now
						$day_late = (int)(($now - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
					} else {
						//complete
						//late is difference between iscr and complete
						$day_late = (int)(($d_complete - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
					}
					//if is in late
					if($day_late > 0) {
						if( $day_late < 30 ) {
							if( isset($num_zeromonth[$id_cp]) ) ++$num_zeromonth[$id_cp];
							else $num_zeromonth[$id_cp] = 1;
						} elseif( $day_late < 60 ) {
							if( isset($num_onemonth[$id_cp]) ) ++$num_onemonth[$id_cp];
							else $num_onemonth[$id_cp] = 1;
						} else {
							if( isset($num_twomonth[$id_cp]) ) ++$num_twomonth[$id_cp];
							else $num_twomonth[$id_cp] = 1;
						}
					}
				}
				$this->_printTable($course, $num_zeromonth, $num_onemonth, $num_twomonth, $num_iscr);
			}
		}
		
	}
	
	function _printTable(&$course, &$num_zeromonth, &$num_onemonth, &$num_twomonth, &$num_iscr) {
		
		$lang =& DoceboLanguage::createInstance('rd_report');
		// Table
		$GLOBALS['page']->add(
			'<table class="report_table" summary="'.$lang->def('_RD_SUMMAMRY_MANAGMENT').'">'
			.'<caption>'.$lang->def('_RD_CAPTION').'</caption>'
			.'<thead>'
			.'<tr>'."\n\t"
				.'<th rowspan="2" colspan="2">'.$lang->def('_TH_COURSE').'</th>'."\n\t"
				.'<th colspan="6">'.$lang->def('_TH_USER').'</th>'."\n"
			.'</tr>'
			.'<tr>'."\n\t"
				.'<th>'.$lang->def('_TH_NUMBER_SUBSCRIPTION').'</th>'."\n\t"
				.'<th colspan="2">'.$lang->def('_TEMPO_TEMPOSTD').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_LS_MONTH').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_GT_MONTH').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_GT_TWO_MONTH').'</th>'."\n\t"
			.'</tr>'
			.'<tr>'."\n\t"
				.'<th>'.$lang->def('_TH_COURSEID').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_NAME_COURSE').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_NUM').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_NUM').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_PERC').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_NUM').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_NUM').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_NUM').'</th>'."\n\t"
			.'</tr>'
			.'</thead>'."\n\n"
			.'<tbody>'
		, 'content');
		
		// find some information
		$i = 0;
		$tot_iscr = $tot_std = $tot_perc_std = $tot_zero_month = $tot_one_month = $tot_two_month = 0;
		
		while(list($id, $data) =  each($course) ) {
			
			$row = '<tr class="row'.( (++$i)%2 ? '' : '_col' ).'">'
				.'<td>'.$data['code'].'</td>'."\n\t"
				.'<td>'.$data['name'].'</td>'."\n\t";
			if( isset($num_iscr[$id]) && ($num_iscr[$id] != 0) ) {
				//num_iscr
				$row .= '<td class="image">'.$num_iscr[$id].'</td>'."\n\t";
				$tot_iscr += $num_iscr[$id];
				//in time
				$line_tot = '';
				if( isset($num_zeromonth[$id]) ) $line_tot += $num_zeromonth[$id];
				if( isset($num_onemonth[$id]) ) $line_tot += $num_onemonth[$id];
				if( isset($num_twomonth[$id]) ) $line_tot += $num_twomonth[$id];
				
				$p = number_format( (($line_tot / $num_iscr[$id])*100), 2, '.', '');
				$row .= '<td class="image">'.$line_tot.'</td>'."\n\t"
					.'<td class="image">'
					.( $p != 0 ? $p : '&nbsp;' ) 
					.'</td>'."\n\t";
				$tot_std += $line_tot;
				$tot_perc_std += (($line_tot / $num_iscr[$id])*100);
				
				// < 1 month
				$row .= '<td class="image">';
				if( isset($num_zeromonth[$id]) ) {
					$row .= $num_zeromonth[$id];
					$tot_zero_month += $num_zeromonth[$id];
				}
				else $row .= '&nbsp;';
				$row .= '</td>';
				// > 1 month
				$row .= '<td class="image">';
				if( isset($num_onemonth[$id]) ) {
					$row .= $num_onemonth[$id];
					$tot_one_month += $num_onemonth[$id];
				}
				else $row .= '&nbsp;';
				$row .= '</td>';
				// > 2 month
				$row .= '<td class="image">';
				if( isset($num_twomonth[$id]) ) {
					$row .= $num_twomonth[$id];
					$tot_two_month += $num_twomonth[$id];
				}
				else $row .= '&nbsp;';
				$row .= '</td></tr>';
			} else {
				$row .= '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>'."\n\t"
					.'<td>&nbsp;</td><td>&nbsp;</td></tr>'."\n\n";
			}
			
			$GLOBALS['page']->add( $row, 'content');
		}
		$GLOBALS['page']->add( '<tr class="total">'
			.'<td colspan="2"><b>'.$lang->def('_TOTAL').'</b></td>'
			.'<td class="image">'.$tot_iscr.'</td>'
			.'<td class="image">'.$tot_std.'</td>'
			.'<td class="image">'.( $i ? number_format( ($tot_perc_std / $i ), 2, '.', '') : '' ).'</td>'
			.'<td class="image">'.$tot_zero_month.'</td>'
			.'<td class="image">'.$tot_one_month.'</td>'
			.'<td class="image">'.$tot_two_month.'</td>'
		.'</tr>', 'content');
		$GLOBALS['page']->add( 
			'</tbody>'
			.'</table>', 'content');
	}
	
	function exportAsCvs($org_chart_subdivision, $start_time, $end_time) {
		
		ob_end_clean();
		//content type forcing dowlad
		header("Content-type: application/download\n");
		//cache control
		header("Cache-control: private");
		//sending creation time
		header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
		//content type
		header('Content-Disposition: attachment; filename="report_delay '.gmdate('d-m-Y H i s').'.csv"');
		
		$lang =& DoceboLanguage::createInstance('rd_report');
		
		$acl_man 		= new DoceboACLManager();
		$course_man 	= new Man_Course();
		
		$elem_selected = unserialize(urldecode($_POST['elem_selected']));
		$user_selected = unserialize(urldecode($_POST['user_selected']));
		
		if(empty($user_selected)) exit;

		if($org_chart_subdivision == 1) {
			
			require_once($GLOBALS['where_framework'].'/lib/lib.orgchart.php');
			$org_man 	= new OrgChartManager();
			$org_name = $org_man->getFolderFormIdst($elem_selected);
		}
		
		if(empty($user_selected)) exit();
		$users_info =& $acl_man->getUsers($user_selected);
		
		// Retrive all the course
		$course =& $course_man->getAllCoursesWithMoreInfo();
		if(empty($course)) exit;
		
		if($org_chart_subdivision === 0) {
			
			$now = time();
			$num_zeromonth = array();
			$num_onemonth = array();
			$num_twomonth = array();
			$num_iscr = array();
			
			$query_time = "
			SELECT idCourse, UNIX_TIMESTAMP(date_inscr), UNIX_TIMESTAMP(date_first_access), UNIX_TIMESTAMP(date_complete)
			FROM ".$GLOBALS['prefix_lms']."_courseuser 
			WHERE idUser IN (".implode(',', $user_selected).") ";
			if($start_time != '') $query_time .= " AND date_inscr >= '".$start_time."' ";
			if($end_time != '') $query_time .= " AND date_inscr <= '".$end_time."' ";
			$re_time = mysql_query($query_time);
			
			while(list($id_cp, $d_iscr, $d_first, $d_complete) = mysql_fetch_row($re_time)) {
				
				if( isset($num_iscr[$id_cp]) ) ++$num_iscr[$id_cp];
				else $num_iscr[$id_cp] = 1;
				if($d_complete === NULL) {
					//not complete
					//late is difference between iscr and now
					$day_late = (int)(($now - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
				} else {
					//complete
					//late is difference between iscr and complete
					$day_late = (int)(($d_complete - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
				}
				//if is in late
				if($day_late > 0) {
					if( $day_late < 30 ) {
						if( isset($num_zeromonth[$id_cp]) ) ++$num_zeromonth[$id_cp];
						else $num_zeromonth[$id_cp] = 1;
					} elseif( $day_late < 60 ) {
						if( isset($num_onemonth[$id_cp]) ) ++$num_onemonth[$id_cp];
						else $num_onemonth[$id_cp] = 1;
					} else {
						if( isset($num_twomonth[$id_cp]) ) ++$num_twomonth[$id_cp];
						else $num_twomonth[$id_cp] = 1;
					}
				}
			}
			$this->_printCsvTable($course, $num_zeromonth, $num_onemonth, $num_twomonth, $num_iscr);
		} else {
			
			$date_now = $GLOBALS['regset']->databaseToRegional(date("Y-m-d H:i:s")); 
			
			reset($org_name);
			while(list($idst_group, $folder_name) = each($org_name)) {
				
				
				echo "\r\n"
					.''.$lang->def('_FOLDER_NAME').' : '.$folder_name['name']
						.( $folder_name['type_of_folder'] == ORG_CHART_WITH_DESCENDANTS ? ' ('.$lang->def('_WITH_DESCENDANTS').')' : '' )."\r\n";
				if($start_time != '' || $end_time != '') {
					
					echo ''.$lang->def('_TIME_BELT_2').' : '
						.( $start_time != '' 	? ' '.$lang->def('_START_TIME').' '.$GLOBALS['regset']->databaseToRegional($start_time, 'date') 	: '' )
						.( $end_time != '' 		? ' '.$lang->def('_END_TIME').' '.$GLOBALS['regset']->databaseToRegional($end_time, 'date') 		: '' )
						."\r\n";
				}
				echo ''.$lang->def('_ANALYSIS_DATE').' : '.$date_now."\r\n";
				
				$group_user = $acl_man->getGroupAllUser($idst_group);
				
				$now = time();
				$num_zeromonth = array();
				$num_onemonth = array();
				$num_twomonth = array();
				$num_iscr = array();
				
				$query_time = "
				SELECT idCourse, UNIX_TIMESTAMP(date_inscr), UNIX_TIMESTAMP(date_first_access), UNIX_TIMESTAMP(date_complete)
				FROM ".$GLOBALS['prefix_lms']."_courseuser 
				WHERE idUser IN (".implode(',', $group_user).") ";
				if($start_time != '') $query_time .= " AND cu.date_inscr >= '".$start_time."' ";
				if($end_time != '') $query_time .= " AND cu.date_inscr <= '".$end_time."' ";
				$re_time = mysql_query($query_time);
				
				while(list($id_cp, $d_iscr, $d_first, $d_complete) = mysql_fetch_row($re_time)) {
					
					if( isset($num_iscr[$id_cp]) ) ++$num_iscr[$id_cp];
					else $num_iscr[$id_cp] = 1;
					if($d_complete === NULL) {
						//not complete
						//late is difference between iscr and now
						$day_late = (int)(($now - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
					} else {
						//complete
						//late is difference between iscr and complete
						$day_late = (int)(($d_complete - $d_iscr) / 86400) - $course[$id_cp]['mediumTime'];
					}
					//if is in late
					if($day_late > 0) {
						if( $day_late < 30 ) {
							if( isset($num_zeromonth[$id_cp]) ) ++$num_zeromonth[$id_cp];
							else $num_zeromonth[$id_cp] = 1;
						} elseif( $day_late < 60 ) {
							if( isset($num_onemonth[$id_cp]) ) ++$num_onemonth[$id_cp];
							else $num_onemonth[$id_cp] = 1;
						} else {
							if( isset($num_twomonth[$id_cp]) ) ++$num_twomonth[$id_cp];
							else $num_twomonth[$id_cp] = 1;
						}
					}
				}
				$this->_printCsvTable($course, $num_zeromonth, $num_onemonth, $num_twomonth, $num_iscr);
			}
		}
		exit();
	}
	
	function _printCsvTable(&$course, &$num_zeromonth, &$num_onemonth, &$num_twomonth, &$num_iscr) {
		
		$lang =& DoceboLanguage::createInstance('rd_report');
		
		// Table
		echo $lang->def('_RD_CAPTION')."\n\n";
		echo $lang->def('_TH_COURSE').';;'
				.$lang->def('_TH_USER').';;;;;'
			."\n"
				.';;'
				.$lang->def('_TH_NUMBER_SUBSCRIPTION').';'
				.$lang->def('_TEMPO_TEMPOSTD').';;'
				.$lang->def('_TH_LS_MONTH').';'
				.$lang->def('_TH_GT_MONTH').';'
				.$lang->def('_TH_GT_TWO_MONTH').''
			."\n"
				.$lang->def('_TH_COURSEID').';'
				.$lang->def('_TH_NAME_COURSE').';'
				.$lang->def('_TH_NUM').';'
				.$lang->def('_TH_NUM').';'
				.$lang->def('_TH_PERC').';'
				.$lang->def('_TH_NUM').';'
				.$lang->def('_TH_NUM').';'
				.$lang->def('_TH_NUM').''
			."\n";
		
		// find some information
		$i = 0;
		$tot_iscr = $tot_std = $tot_perc_std = $tot_zero_month = $tot_one_month = $tot_two_month = 0;
		
		while(list($id, $data) =  each($course) ) {
			
			$row = ereg_replace(';', '', $data['code']).';'
				.ereg_replace(';', '', $data['name']).';';
			if( isset($num_iscr[$id]) && ($num_iscr[$id] != 0) ) {
				//num_iscr
				$row .= $num_iscr[$id].';';
				$tot_iscr += $num_iscr[$id];
				//in time
				$line_tot = '';
				if( isset($num_zeromonth[$id]) ) $line_tot += $num_zeromonth[$id];
				if( isset($num_onemonth[$id]) ) $line_tot += $num_onemonth[$id];
				if( isset($num_twomonth[$id]) ) $line_tot += $num_twomonth[$id];
				$row .= $line_tot.';'
					.number_format( 
					(($line_tot / $num_iscr[$id])*100)
					, 2, '.', '').';';
				$tot_std += $line_tot;
				$tot_perc_std += (($line_tot / $num_iscr[$id])*100);
				
				// < 1 month
				if( isset($num_zeromonth[$id]) ) {
					$row .= $num_zeromonth[$id];
					$tot_zero_month += $num_zeromonth[$id];
				}
				$row .= ';';
				// > 1 month
				if( isset($num_onemonth[$id]) ) {
					$row .= $num_onemonth[$id];
					$tot_one_month += $num_onemonth[$id];
				}
				$row .= ';';
				// > 2 month
				if( isset($num_twomonth[$id]) ) {
					$row .= $num_twomonth[$id];
					$tot_two_month += $num_twomonth[$id];
				}
				$row .= ';'."\n";
			} else {
				$row .= ';;;;;;'."\n";
			}
			$i++;
			echo $row;
		}
		echo $lang->def('_TOTAL').';;'
			.$tot_iscr.';'
			.$tot_std.';'
			.( $i ? number_format( ($tot_perc_std / $i ), 2, '.', '') : '' ).';'
			.$tot_zero_month.';'
			.$tot_one_month.';'
			.$tot_two_month.';';
	}
}

?>