<?php

/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2004													*/
/* 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(isset($_REQUEST['GLOBALS'])) die('GLOBALS overwrite attempt detected');

define("IN_DOCEBO", true);

$path_to_root = '../..';

// prepare refer ------------------------------------------------------------------

require_once(dirname(__FILE__).'/'.$path_to_root.'/config.php');
require_once($GLOBALS['where_config'].'/config.php');

if ($GLOBALS["where_cms_relative"] != false)
	$GLOBALS["where_cms_relative"]=$path_to_root.'/'.$GLOBALS["where_cms_relative"];

if ($GLOBALS["where_kms_relative"] != false)
	$GLOBALS["where_kms_relative"]=$path_to_root.'/'.$GLOBALS["where_kms_relative"];

if ($GLOBALS["where_lms_relative"] != false)
	$GLOBALS["where_lms_relative"]=$path_to_root.'/'.$GLOBALS["where_lms_relative"];

if ($GLOBALS["where_framework_relative"] != false)
	$GLOBALS["where_framework_relative"]=$path_to_root.'/'.$GLOBALS["where_framework_relative"];

if ($GLOBALS["where_files_relative"] != false) {
	$GLOBALS["where_files_relative"]=$path_to_root.'/'.$GLOBALS["where_files_relative"];
}

ob_start();

// connect to database -------------------------------------------------------------------

$GLOBALS['dbConn'] = mysql_connect($GLOBALS['dbhost'], $GLOBALS['dbuname'], $GLOBALS['dbpass']);
if( !$GLOBALS['dbConn'] ) 
	die( "Can't connect to db. Check configurations" );

if( !mysql_select_db($dbname, $GLOBALS['dbConn']) ) 
	die( "Database not found. Check configurations" );

@mysql_query("SET NAMES '".$GLOBALS['db_conn_names']."'", $GLOBALS['dbConn']);
@mysql_query("SET CHARACTER SET '".$GLOBALS['db_conn_char_set']."'", $GLOBALS['dbConn']);

// load lms setting ------------------------------------------------------------------
require_once($GLOBALS['where_framework'].'/setting.php');
require_once($GLOBALS['where_lms'].'/setting.php');

session_name("docebo_session");
session_start();

// load regional setting --------------------------------------------------------------
require_once($GLOBALS['where_framework']."/lib/lib.regset.php");
$GLOBALS['regset'] = new RegionalSettings();

// load current user from session -----------------------------------------------------
require_once($GLOBALS['where_framework'].'/lib/lib.user.php');
$GLOBALS['current_user'] =& DoceboUser::createDoceboUserFromSession('public_area');

require_once($GLOBALS['where_framework'].'/lib/lib.lang.php');
require_once($GLOBALS['where_framework'].'/lib/lib.template.php');
require_once($GLOBALS['where_framework'].'/lib/lib.utils.php');

// security check --------------------------------------------------------------------

chkInput($_GET);
chkInput($_POST);
chkInput($_COOKIE);

$GLOBALS['operation_result'] = '';
function docebo_cout($string) { $GLOBALS['operation_result'] .= $string; }

// here all the specific code ==========================================================

$op = importVar('op');

switch($op) {
	case "getLang" : {
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance( 'welcomepage', 'lms');
		
 		$lang_obj='{
			"_BECOME_TEACHER":"'.$lang->def('_BECOME_TEACHER').'",
			"_BECOME_TEACHER_LONGTEXT":"'.$lang->def('_BECOME_TEACHER_LONGTEXT').'",
			"_YES":"'.$lang->def('_CONFIRM').'",
			"_NO":"'.$lang->def('_UNDO').'"
		}';
  
  		docebo_cout($lang_obj);
	};break;
	default : {
		
		$lang =& DoceboLanguage::createInstance( 'welcomepage', 'lms');
		require_once($GLOBALS['where_lms'].'/lib/lib.course.php');
		
		$id_course = importVar('id_course', true, 0);
		
		$query_course = "
		SELECT max_num_subscribe, course_quota, used_space, COUNT(*)
		FROM ".$GLOBALS['prefix_lms']."_course AS c JOIN ".$GLOBALS['prefix_lms']."_courseuser AS u 
		WHERE c.idCourse = u.idCourse AND c.idCourse = '".$id_course."'
		GROUP BY c.idCourse";
		$re_course = mysql_query($query_course);
		$row = mysql_fetch_row($re_course);
		
		if($row[1] == COURSE_QUOTA_INHERIT) $row[1] = $GLOBALS['lms']['course_quota'];
		
		$near_limit = false;
		if($row[1] != 0) {
			 if( ($row[2]/$row[1])*100 >= 95 ) {
			 	docebo_cout($lang->def('_DISK_SPACE_NEAR_LIMIT'));
			 	$near_limit = true;
			 }
		}
		if($row[3] != 0) 	{
			if( ($row[3]/$row[0])*100  >= 95 ) {
				if($near_limit) docebo_cout("\n");
				docebo_cout($lang->def('_USER_ISCR_NEAR_LIMIT'));
			 	$near_limit = true;
			} 
		}
		if(!$near_limit) docebo_cout($lang->def('_NO_LIMIT_REACH'));
		
	}
}

// =====================================================================================

// close database connection

mysql_close($GLOBALS['dbConn']);

ob_clean();
echo $GLOBALS['operation_result'];
ob_end_flush();

?>