<?php

/************************************************************************/
/* DOCEBO CORE - Framework												*/
/* ============================================							*/
/*																		*/
/* Copyright (c) 2006													*/
/* 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.		*/
/************************************************************************/

/**
 * @package course management
 * @subpackage pre-assessment
 * @category ajax server
 * @author Fabio Pirovano
 * @version $Id:$
 * @since 3.5.0
 * 
 * ( editor = Eclipse 3.2.0[phpeclipse,subclipse,WTP], tabwidth = 4 ) 
 */

if(isset($_REQUEST['GLOBALS'])) die('GLOBALS overwrite attempt detected');

if(!defined("IN_DOCEBO")) 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( 'preassessment', 'framework');
		
 		$lang_obj='{
			"_DEL_TITLE":"'.$lang->def('_DEL_TITLE').'",
			"_DEL_CONFIRM":"'.$lang->def('_DEL_CONFIRM').'",
			"_YES":"'.$lang->def('_CONFIRM').'",
			"_NO":"'.$lang->def('_UNDO').'",
			"_DEL_TITLE_RULE":"'.$lang->def('_DEL_TITLE_RULE').'",
			"_DEL_CONFIRM_RULE":"'.$lang->def('_DEL_CONFIRM_RULE').'",
			"_NEW_RULE":"'.$lang->def('_ADD_RULE').'", 
			"_CONFIRM":"'.$lang->def('_CONFIRM').'", 
			"_UNDO":"'.$lang->def('_UNDO').'"
		}';
  
  		docebo_cout($lang_obj);
	}; break;
	case "modruleform" : {
		
		require_once($GLOBALS['where_lms'].'/lib/lib.preassessment.php');
		require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
		
		$lang =& DoceboLanguage::createInstance( 'standard', 'framework');
		$lang->setGlobal();
		$lang =& DoceboLanguage::createInstance( 'preassessment', 'framework');
		
		$rule_man = new AssessmentRule();
		
		$use_default 	= importVar('usedef', false, 1);
		$id_rule 		= importVar('id_rule', true, 0 );
		
		if($id_rule != 0) {
			
			// load old data ------------------------------------
			$rule = $rule_man->getRule($id_rule);
			
			$rule_type = $rule[RULE_TYPE];
			$setting = $rule_man->parseRuleSetting($rule[RULE_TYPE], $rule[RULE_SETTING]);
			$score_type_one = ( isset($setting[0]) ? $setting[0] : '' );
			$score_type_two = ( isset($setting[1]) ? $setting[1] : '' );
		} else {
			
			$rule_type = ( $use_default  ? RULE_DEFAULT : RULE_GREATER );
			$score_type_one = '';
			$score_type_two = '';
		}
		
		$arr_question = array();
		$arr_question[RULE_GREATER] = strip_tags(str_replace('[score]', $lang->def('_SCORE'), $lang->def('_RULE_GREATER')));
		$arr_question[RULE_LESSER]  = strip_tags(str_replace('[score]', $lang->def('_SCORE'), $lang->def('_RULE_LESSER'))); 
		$arr_question[RULE_BETWEEN] = strip_tags(str_replace(array('[score_1]', '[score_2]'), array($lang->def('_SCORE'), $lang->def('_SCORE_2')), $lang->def('_RULE_BETWEEN')));
		if($use_default || $id_rule != 0) $arr_question[RULE_DEFAULT] = $lang->def('_RULE_DEFAULT');
		
		docebo_cout( Form::getHidden('id_assessment', 'id_assessment', importVar('id_assessment', true, 0)) );
		docebo_cout( Form::getHidden('id_rule', 'id_rule', $id_rule) );
		
		docebo_cout(
			'<label for="rule_type">'.$lang->def('_RULE_TEXT').'</label>'
			.Form::getInputDropdown('dropdown_nowh', 'rule_type', 
						'rule_type', 
						$arr_question, 
						$rule_type, 
						' onChange="rule_type_change(\'rule_type\', \'score_type_one\', \'score_type_two\');"')
			.'<br />'.'<br />'
		);
		
		docebo_cout( Form::getTextfield(	$lang->def('_SCORE'), 
					'score_type_one', 
					'score_type_one', 
					255,
					$score_type_one ) 
		);
		
		docebo_cout( Form::getTextfield(	$lang->def('_SCORE_2'), 
					'score_type_two', 
					'score_type_two', 
					255,
					$score_type_two ) 
		);
		docebo_cout('<script type="text/javascript">'
			.'rule_type_change(\'rule_type\', \'score_type_one\', \'score_type_two\');'
		.'</script>');
	};break;
}

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

// close database connection

mysql_close($GLOBALS['dbConn']);

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

?>