<?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_user.php 1002 2007-03-24 11:55:51Z 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_User extends Report {
	
	var $lang;
	
	var $status_c;
	
	function Report_User($id_report) {
		$this->lang =& DoceboLanguage::createInstance('ru_report');
		parent::Report($id_report);
		
		$lang =& DoceboLanguage::CreateInstance('admin_course_managment', 'lms');
		$this->status_c = array(
			CST_PREPARATION => $lang->def('_CST_PREPARATION', 'admin_course_managment', 'lms'), 
			CST_AVAILABLE 	=> $lang->def('_CST_AVAILABLE', 'admin_course_managment', 'lms'), 
			CST_EFFECTIVE 	=> $lang->def('_CST_CONFIRMED', 'admin_course_managment', 'lms'), 
			CST_CONCLUDED 	=> $lang->def('_CST_CONCLUDED', 'admin_course_managment', 'lms'), 
			CST_CANCELLED 	=> $lang->def('_CST_CANCELLED', 'admin_course_managment', 'lms') );
			
		$lang =& DoceboLanguage::CreateInstance('subscribe', 'lms');
		$this->status_u = array(
			_CUS_CONFIRMED 		=> $lang->def('_USER_STATUS_CONFIRMED', 'subscribe', 'lms'),
			
			_CUS_SUBSCRIBED 	=> $lang->def('_USER_STATUS_INSCR', 'subscribe', 'lms'),
			_CUS_BEGIN 			=> $lang->def('_USER_STATUS_BEGIN', 'subscribe', 'lms'),
			_CUS_END 			=> $lang->def('_USER_STATUS_END', 'subscribe', 'lms'),
			_CUS_SUSPEND 		=> $lang->def('_USER_STATUS_SUSPEND', 'subscribe', 'lms') );
	}
	
	function _converStatusCourse($status) {
		
		return $this->status_c[$status];
	}
	
	function _convertStatusUser($status) {
		
		return $this->status_u[$status];
	}
	
	function play($jump_url, $back_url) {
		
		$time_range 			= importVar('time_belt', true, 0);
		$start_time 			= importVar('start_time', false, '');
		$end_time 				= importVar('end_time', false, '');
		$org_chart_subdivision 	= importVar('org_chart_subdivision', true, 0);
		
		if(isset($_POST['report_alluser'])) {
			if($_POST['report_alluser'] == '0') $alluser = 0;
			else $alluser = 1;
		} else $alluser = 0;
	
		if($time_range != 0) {
			$start_time = date("Y-m-d H:i:s", time() - $time_range*24*3600);
			$end_time = date("Y-m-d H:i:s");
		} elseif(isset($_POST['report_alluser']) || isset($_POST['report_seluser'])) {
	
			$start_time = $GLOBALS['regset']->regionalToDatabase($start_time, 'date');
			$end_time 	= $GLOBALS['regset']->regionalToDatabase($end_time, 'date');
		}
	
		if($start_time == ' 00:00:00') $start_time = '';
		if($end_time == ' 00:00:00') $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('ru_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);
			
			$userlevelid = $GLOBALS['current_user']->getUserLevelId();
			if( $userlevelid != ADMIN_GROUP_GODADMIN ) {
				require_once($GLOBALS['where_framework'].'/lib/lib.adminmanager.php');
				$adminManager = new AdminManager();
				$user_filter = $adminManager->getAdminTree($GLOBALS['current_user']->getIdSt());
				$user_filter = array_flip($user_filter);
				
				$org_name_temp = $org_name;
				$org_name  = array();
				foreach($org_name_temp as $id => $value) {
					
					if(isset($user_filter[$id])) $org_name[$id] = $value;
				}
			}
		} else {
			$elem_selected = array();
		}
		
		if(empty($user_selected)) {
			
			$GLOBALS['page']->add($lang->def('_NULL_SELECTION'), 'content');
			return;
		}
		$users_info =& $acl_man->getUsers($user_selected);
		
		// Retrive all the course
		$id_courses = $course_man->getAllCourses();
		if(empty($id_courses)) {
			
			$GLOBALS['page']->add($lang->def('_NULL_COURSE_SELECTION'), 'content');
			return;
		}
		
		// css -----------------------------------------------------------
		$GLOBALS['page']->add(
			"\n".'<link href="'.getPathTemplate('lms').'style/report/style_report_user.css" rel="stylesheet" type="text/css" />'."\n"
		, 'page_head');
		
		// Export as cvs ------------------------------------------------
		$GLOBALS['page']->add(
			Form::openForm('export', $jump_url)
			.Form::openElementSpace('')
			.Form::getHidden('org_chart_subdivision', 'org_chart_subdivision', ( $org_chart_subdivision ?  1 : 0 ))
			.Form::getHidden('start_time', 'start_time', $start_time)
			.Form::getHidden('end_time', 'end_time', $end_time)
			.Form::getHidden('elem_selected', 'elem_selected', urlencode(serialize($elem_selected)))
			.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');
		
		$re_category = mysql_query("
		SELECT idCategory, path 
		FROM ".$GLOBALS['prefix_lms']."_category");
		$category_list = array(0 => $lang->def('_ROOT_CATEGORY'));
		while(list($id_cat, $name_cat) = mysql_fetch_row($re_category)) {
			$category_list[$id_cat] = substr($name_cat, strrpos($name_cat, '/') + 1 );
		}
		
		$time_list = array();
		$session_list = array();
		$lastaccess_list = array();
		
		$query = "
		SELECT idUser, idCourse, COUNT(*), SUM(UNIX_TIMESTAMP(lastTime) - UNIX_TIMESTAMP(enterTime)), MAX(lastTime) 
		FROM ".$GLOBALS['prefix_lms']."_tracksession 
		WHERE 1 ";
		if($start_time != '') $query .= " AND enterTime >= '".$start_time."' ";
		if($end_time != '') $query .= " AND lastTime <= '".$end_time."' ";
		$query .= "GROUP BY idUser, idCourse ";
		$re_time = mysql_query($query);
		while(list($id_u, $id_c, $session_num, $time_num, $last_num) = mysql_fetch_row($re_time)) {
			
			$session_list[$id_u.'_'.$id_c] 		= $session_num;
			$time_list[$id_u.'_'.$id_c] 		= $time_num;
			$lastaccess_list[$id_u.'_'.$id_c] 	= $last_num;
		}
		//recover start and final score
		require_once($GLOBALS['where_lms'].'/lib/lib.orgchart.php');
		$org_man = new OrganizationManagement(false);
		
		$score_start = $org_man->getStartObjectScore($user_selected, array_keys($id_courses));
		$score_final = $org_man->getFinalObjectScore($user_selected, array_keys($id_courses));
		
		
		require_once($GLOBALS['where_lms'].'/lib/lib.coursereport.php');
		$rep_man = new CourseReportManager();
		
		$score_course = $rep_man->getUserFinalScore($user_selected, array_keys($id_courses));
		
		
		if($org_chart_subdivision === 0) {
			
			// find some information
			$query_course_user = "
			SELECT cu.idUser, 
				c.code, c.idCourse, c.idCategory, c.name, c.status,
				cu.status, cu.date_inscr, cu.date_first_access, cu.date_complete, c.mediumTime
			FROM  ".$GLOBALS['prefix_lms']."_courseuser AS cu 
				JOIN ".$GLOBALS['prefix_lms']."_course AS c
			WHERE cu.idCourse = c.idCourse AND cu.idUser IN ( ".implode(',', $user_selected)." )";
			if($start_time != '') $query_course_user .= " AND cu.date_inscr >= '".$start_time."' ";
			if($end_time != '') $query_course_user .= " AND cu.date_inscr <= '".$end_time."' AND cu.level='3' ";
			
			$this->_printTable($acl_man, $query_course_user, $users_info, $category_list, $session_list, $lastaccess_list, $time_list, $score_start, $score_final, $score_course);
		} 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);
				
				// find some information
				$query_course_user = "
				SELECT cu.idUser, 
					c.code, c.idCourse, c.idCategory, c.name, c.status,
					cu.status, cu.date_inscr, cu.date_first_access, cu.date_complete, c.mediumTime 
				FROM  ".$GLOBALS['prefix_lms']."_courseuser AS cu 
					JOIN ".$GLOBALS['prefix_lms']."_course AS c
				WHERE cu.idCourse = c.idCourse AND cu.idUser IN ( ".implode(',', $group_user)." )";
				if($start_time != '') $query_course_user .= " AND cu.date_inscr >= '".$start_time."' ";
				if($end_time != '') $query_course_user .= " AND cu.date_inscr <= '".$end_time."'  AND cu.level='3'";
				
				$this->_printTable($acl_man, $query_course_user, $users_info, $category_list, $session_list, $lastaccess_list, $time_list, $score_start, $score_final, $score_course);	
			}
		}
	}
	
	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_user '.gmdate('d-m-Y H i s').'.csv"');
		
		$lang =& DoceboLanguage::createInstance('ru_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);
			
			$userlevelid = $GLOBALS['current_user']->getUserLevelId();
			if( $userlevelid != ADMIN_GROUP_GODADMIN ) {
				require_once($GLOBALS['where_framework'].'/lib/lib.adminmanager.php');
				$adminManager = new AdminManager();
				$user_filter = $adminManager->getAdminTree($GLOBALS['current_user']->getIdSt());
				$user_filter = array_flip($user_filter);
				
				$org_name_temp = $org_name;
				$org_name  = array();
				foreach($org_name_temp as $id => $value) {
					
					if(isset($user_filter[$id])) $org_name[$id] = $value;
				}
			}
		}
		
		$users_info =& $acl_man->getUsers($user_selected);
		
		// Retrive all the course
		$id_courses = $course_man->getAllCourses();
		if(empty($id_courses)) exit;
		
		$re_category = mysql_query("
		SELECT idCategory, path 
		FROM ".$GLOBALS['prefix_lms']."_category");
		$category_list = array(0 => $lang->def('_ROOT_CATEGORY'));
		while(list($id_cat, $name_cat) = mysql_fetch_row($re_category)) {
			$category_list[$id_cat] = substr($name_cat, strrpos($name_cat, '/') + 1 );
		}
	
		$time_list = array();
		$session_list = array();
		$lastaccess_list = array();
		
		$query = "
		SELECT idUser, idCourse, COUNT(*), SUM(UNIX_TIMESTAMP(lastTime) - UNIX_TIMESTAMP(enterTime)), MAX(lastTime) 
		FROM ".$GLOBALS['prefix_lms']."_tracksession 
		WHERE 1 ";
		if($start_time != '') $query .= " AND enterTime >= '".$start_time."' ";
		if($end_time != '') $query .= " AND lastTime <= '".$end_time."' ";
		$query .= "GROUP BY idUser, idCourse ";
		$re_time = mysql_query($query);
		while(list($id_u, $id_c, $session_num, $time_num, $last_num) = mysql_fetch_row($re_time)) {
			
			$session_list[$id_u.'_'.$id_c] 		= $session_num;
			$time_list[$id_u.'_'.$id_c] 		= $time_num;
			$lastaccess_list[$id_u.'_'.$id_c] 	= $last_num;
		}
		
		//recover start and final score
		require_once($GLOBALS['where_lms'].'/lib/lib.orgchart.php');
		$org_man = new OrganizationManagement(false);
		
		$score_start = $org_man->getStartObjectScore($user_selected, array_keys($id_courses));
		$score_final = $org_man->getFinalObjectScore($user_selected, array_keys($id_courses));
		
		
		require_once($GLOBALS['where_lms'].'/lib/lib.coursereport.php');
		$rep_man = new CourseReportManager();
		
		$score_course = $rep_man->getUserFinalScore($user_selected, array_keys($id_courses));
		
		if($org_chart_subdivision === 0) {
			
			// find some information
			$query_course_user = "
			SELECT cu.idUser, 
				c.code, c.idCourse, c.idCategory, c.name, c.status,
				cu.status, cu.date_inscr, cu.date_first_access, cu.date_complete, mediumTime
			FROM  ".$GLOBALS['prefix_lms']."_courseuser AS cu 
				JOIN ".$GLOBALS['prefix_lms']."_course AS c
			WHERE cu.idCourse = c.idCourse AND cu.idUser IN ( ".implode(',', $user_selected)." )";
			if($start_time != '') $query_course_user .= " AND cu.date_inscr >= '".$start_time."' ";
			if($end_time != '') $query_course_user .= " AND cu.date_inscr <= '".$end_time."'  AND cu.level='3'";
			
			$this->_printCsvTable($acl_man, $query_course_user, $users_info, $category_list, $session_list, $lastaccess_list, $time_list, $score_start, $score_final, $score_course);
		} 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);
				
				// find some information
				$query_course_user = "
				SELECT cu.idUser, 
					c.code, c.idCourse, c.idCategory, c.name, c.status,
					cu.status, cu.date_inscr, cu.date_first_access, cu.date_complete, c.mediumTime
				FROM  ".$GLOBALS['prefix_lms']."_courseuser AS cu 
					JOIN ".$GLOBALS['prefix_lms']."_course AS c
				WHERE cu.idCourse = c.idCourse AND cu.idUser IN ( ".implode(',', $group_user)." )";
				if($start_time != '') $query_course_user .= " AND cu.date_inscr >= '".$start_time."' ";
				if($end_time != '') $query_course_user .= " AND cu.date_inscr <= '".$end_time."'  AND cu.level='3'";
				
				$this->_printCsvTable($acl_man, $query_course_user, $users_info, $category_list, $session_list, $lastaccess_list, $time_list, $score_start, $score_final, $score_course);	
			}
		}
		die();
	}
	
	function _printTable(&$acl_man, $query_course_user, &$users_info, $category_list, &$session_list, &$lastaccess_list, &$time_list, &$score_start, &$score_final, &$score_course) {
		
		$lang =& DoceboLanguage::createInstance('ru_report');
		
		// Table
		$GLOBALS['page']->add(
			'<table class="report_table" summary="'.$lang->def('_RU_SUMMAMRY_MANAGMENT').'">'
			.'<caption>'.$lang->def('_RU_CAPTION').'</caption>'
			.'<thead>'
			.'<tr>'."\n\t"
				.'<th>&nbsp;</th>'."\n\t"
				.'<th colspan="3">'.$lang->def('_TH_COURSE').'</th>'."\n\t"
				.'<th colspan="10">'.$lang->def('_TH_STATUS').'</th>'."\n"
			.'</tr>'
			.'<tr>'."\n\t"
				//.'<th>'.$lang->def('_TH_USERCODE').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_USERNAME').'</th>'."\n\t"
				//.'<th>'.$lang->def('_TH_COURSEID').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_CAT').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_COURSENAME').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_COURSESTATUS').'</th>'."\n\t"
		
				.'<th>'.$lang->def('_TH_USER_INSCRIPTION_DATE').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_USER_START_DATE').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_USER_END_DATE').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_LAST_ACCESS_DATE').'</th>'."\n\t"
				.'<th>'.$lang->def('_TH_USER_STATUS').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_USER_START_SCORE').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_USER_FINAL_SCORE').'</th>'."\n"
				.'<th class="image">'.$lang->def('_TH_USER_COURSE_SCORE').'</th>'."\n"
				.'<th class="image">'.$lang->def('_TH_USER_NUMBER_SESSION').'</th>'."\n\t"
				.'<th class="image">'.$lang->def('_TH_USER_ELAPSED_TIME').'</th>'."\n"
				.'<th class="image">'.$lang->def('_ESTIMATED_TIME').'</th>'."\n"
			.'</tr>'
			.'<thead>'."\n\n"
			.'<tbody>'
		, 'content');
		
		$re_course_user = mysql_query($query_course_user);
		$i = 0;
		while(list($id_user, $code, $id_course, $id_category, $name, $status,
			$status_user, $date_inscr, $date_first_access, $date_complete, $medium_time) =  mysql_fetch_row($re_course_user) ) {
			
			$username = ( $users_info[$id_user][ACL_INFO_LASTNAME].$users_info[$id_user][ACL_INFO_FIRSTNAME]
				? $users_info[$id_user][ACL_INFO_LASTNAME].' '.$users_info[$id_user][ACL_INFO_FIRSTNAME]
				: $acl_man->relativeId($users_info[$id_user][ACL_INFO_USERID]) );
			
			$row = '<tr class="row'.( ++$i%2 ? '' : '-col' ).'">'."\n\t"
				//.'<td class="align_center">'.$id_user.'</td>'."\n\t"
				.'<td>'.$username.'</td>'."\n\t"
				//.'<td class="align_center">'.$code.'</td>'."\n\t"
				.'<td>'.$category_list[$id_category].'</td>'."\n\t"
				.'<td>'.$name.'</td>'."\n\t"
				.'<td class="align_center">'.$this->_converStatusCourse($status).'</td>'."\n\t"
				
				.'<td class="align_center">'.$date_inscr.'</td>'."\n\t"
				.'<td class="align_center">'.( $date_first_access !== NULL ? $GLOBALS['regset']->databaseToRegional($date_first_access) : '&nbsp;').'</td>'."\n\t"
				.'<td class="align_center">'.( $date_complete !== NULL ? $GLOBALS['regset']->databaseToRegional($date_complete) : '&nbsp;').'</td>'."\n\t"
				.'<td class="align_center">'
					.( isset($lastaccess_list[$id_user.'_'.$id_course]) ? $lastaccess_list[$id_user.'_'.$id_course] : '' )
				.'</td>'."\n\t"
				.'<td class="align_center">'.$this->_convertStatusUser($status_user).'</td>'."\n\t"
				
				.'<td class="align_center">'
					.( isset($score_start[$id_course][$id_user]) 
						? $score_start[$id_course][$id_user]['score'].' / '.$score_start[$id_course][$id_user]['max_score'] 
						: '' )
				.'</td>'."\n\t".'<td class="align_center">'
					.( isset($score_final[$id_course][$id_user]) 
						? $score_final[$id_course][$id_user]['score'].' / '.$score_final[$id_course][$id_user]['max_score'] 
						: '' )
				.'</td>'."\n\t".'<td class="align_center">'
					.( isset($score_course[$id_course][$id_user]) 
						? $score_course[$id_course][$id_user]['score'].' / '.$score_course[$id_course][$id_user]['max_score'] 
						: '' )
				.'</td>'."\n\t"
				
				.'<td class="align_center">'
					.( isset($session_list[$id_user.'_'.$id_course]) ? $session_list[$id_user.'_'.$id_course] : '' )
				.'</td>'."\n\t"
				.'<td class="align_center">'
					.( isset($time_list[$id_user.'_'.$id_course]) ? 
					substr('0'.((int)($time_list[$id_user.'_'.$id_course]/3600)),-2).'h '
					.substr('0'.((int)(($time_list[$id_user.'_'.$id_course]%3600)/60)),-2).'m '
					.substr('0'.((int)($time_list[$id_user.'_'.$id_course]%60)),-2).'s ' : '&nbsp;' )
				.'</td>'."\n"
				.'<td class="align_center">'
					.$medium_time.'h'
				.'</td>'."\n\t"
				
				.'</tr>'."\n\n";
			$GLOBALS['page']->add( $row, 'content');
		}
		$GLOBALS['page']->add( 
			'</tbody>'
			.'</table>', 'content');
	}
	
	function _printCsvTable(&$acl_man, $query_course_user, &$users_info, $category_list, &$session_list, &$lastaccess_list, &$time_list, &$score_start, &$score_final, &$score_course) {
		
		$lang =& DoceboLanguage::createInstance('ru_report');
		
		// Table
		echo $this->report_name."\n"
		.','.$lang->def('_TH_COURSE').',,'.$lang->def('_TH_STATUS').',,,,,,,'."\n"		
		//.$lang->def('_TH_USERCODE').','
		.$lang->def('_TH_USERNAME').','
		//.$lang->def('_TH_COURSEID').','
		.$lang->def('_TH_CAT').','
		.$lang->def('_TH_COURSENAME').','
		.$lang->def('_TH_COURSESTATUS').','
		.$lang->def('_TH_USER_INSCRIPTION_DATE').','
		.$lang->def('_TH_USER_START_DATE').','
		.$lang->def('_TH_USER_END_DATE').','
		.$lang->def('_TH_LAST_ACCESS_DATE').','
		.$lang->def('_TH_USER_STATUS').','
		.$lang->def('_TH_USER_START_SCORE').','
		.$lang->def('_TH_USER_FINAL_SCORE').','
		.$lang->def('_TH_USER_COURSE_SCORE').','
		.$lang->def('_TH_USER_NUMBER_SESSION').','
		.$lang->def('_TH_USER_ELAPSED_TIME')."\n";
				
		$re_course_user = mysql_query($query_course_user);
		while(list($id_user, $code, $id_course, $id_category, $name, $status,
			$status_user, $date_inscr, $date_first_access, $date_complete, $medium_time) =  mysql_fetch_row($re_course_user) ) {
			
			$username = ( $users_info[$id_user][ACL_INFO_LASTNAME].$users_info[$id_user][ACL_INFO_FIRSTNAME]
				? $users_info[$id_user][ACL_INFO_LASTNAME].' '.$users_info[$id_user][ACL_INFO_FIRSTNAME]
				: $acl_man->relativeId($users_info[$id_user][ACL_INFO_USERID]) );
			
			echo ''.$username.','
				//.''.$code.','
				.''.$category_list[$id_category].','
				.''.$name.','
				.''.$this->_converStatusCourse($status).','
				.''.$date_inscr.','
				.''.( $date_first_access !== NULL ? $date_first_access : '&nbsp;').','
				.''.( $date_complete !== NULL ? $date_complete : '&nbsp;').','
				.''.( isset($lastaccess_list[$id_user.'_'.$id_course]) ? $lastaccess_list[$id_user.'_'.$id_course] : '' ).','
				.''.$this->_convertStatusUser($status_user).','
				
				.''.( isset($score_start[$id_course][$id_user]) 
						? $score_start[$id_course][$id_user]['score'].' / '.$score_start[$id_course][$id_user]['max_score'] 
						: '' ).','
				.''.( isset($score_final[$id_course][$id_user]) 
						? $score_final[$id_course][$id_user]['score'].' / '.$score_final[$id_course][$id_user]['max_score'] 
						: '' ).','
				.''.( isset($score_course[$id_course][$id_user]) 
						? $score_course[$id_course][$id_user]['score'].' / '.$score_course[$id_course][$id_user]['max_score'] 
						: '' ).','
						
				.''.( isset($session_list[$id_user.'_'.$id_course]) ? $session_list[$id_user.'_'.$id_course] : '' ).','
				.( isset($time_list[$id_user.'_'.$id_course]) ? 
					substr('0'.((int)($time_list[$id_user.'_'.$id_course]/3600)),-2).'h '
					.substr('0'.((int)(($time_list[$id_user.'_'.$id_course]%3600)/60)),-2).'m '
					.substr('0'.((int)($time_list[$id_user.'_'.$id_course]%60)),-2).'s ' : '' )
				.','.$medium_time.'h'
				.''."\n";
		}
	}
}

?>