<?php

/************************************************************************/
/* DOCEBO LMS - Learning Managment System                               */
/* ============================================                         */
/*                                                                      */
/* 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.       */
/************************************************************************/

/**
 * @return param_value
 **/
function getLOParam( $idParam, $param_name ) {
	$query = "SELECT param_value FROM ".$GLOBALS['prefix_lms']."_lo_param "
			."WHERE idParam = '".(int)$idParam."'"
			."  AND param_name = '".$param_name."'";
	$rs = mysql_query($query) or 
			errorCommunication( 'getLOParam' );
	if( mysql_num_rows( $rs ) == 1 ) {
		list( $param_value ) = mysql_fetch_row( $rs );
		return $param_value;
	} else 
		return FALSE;	
}

/**
 * @return array of param
 **/
function getLOParamArray( $idParam ) {
	$query = "SELECT param_name, param_value FROM ".$GLOBALS['prefix_lms']."_lo_param "
			."WHERE idParam = '".(int)$idParam."'";
	$rs = mysql_query($query) or 
			errorCommunication( 'getLOParam' );
	$result = array();
	while( list( $param_name, $param_value ) = mysql_fetch_row($rs) )
		$result[$param_name] = $param_value;
	return $result;	
}

/**
 * @return idParam if $idParam == NULL
 * else bool
 **/
function setLOParam( $idParam, $param_name, $param_value) {
	
	if( $idParam === NULL ) {
		$query = "INSERT INTO ".$GLOBALS['prefix_lms']."_lo_param "
				."( idParam, param_name, param_value )"
				." VALUES "
				."( '0', '".$param_name."', '".$param_value."' )";
		if(mysql_query( $query )) {
			
			$idParam = mysql_insert_id();
			$query = "UPDATE ".$GLOBALS['prefix_lms']."_lo_param SET "
					." idParam='".(int)$idParam."'"
					."WHERE id = '".(int)$idParam."'";
		} else errorCommunication( 'setLOParam' );
	} else {
		$val = getLOParam( $idParam, $param_name );
		if( $val === FALSE ) {
			$query = "INSERT INTO ".$GLOBALS['prefix_lms']."_lo_param "
					."( idParam, param_name, param_value )"
					." VALUES "
					."( '".(int)$idParam."', '".$param_name."', '".$param_value."' )";
		} else {
			$query = "UPDATE ".$GLOBALS['prefix_lms']."_lo_param SET "
					." param_value='".$param_value."'"
					."WHERE idParam = '".(int)$idParam."'"
					."  AND param_name = '".$param_name."'";
		}
	}
	mysql_query( $query ) or 
		errorCommunication( 'setLOParam' );
	return $idParam;
}

function delLOParam( $idParam, $param_name ) {
	$query = "DELETE FROM ".$GLOBALS['prefix_lms']."_lo_param "
			."WHERE idParam = '".(int)$idParam."'"
			."  AND param_name = '".$param_name."'";
	$rs = mysql_query($query) or 
			errorCommunication( 'delLOParam' );	
}

function delAllLOParam( $idParam ) {
	$query = "DELETE FROM ".$GLOBALS['prefix_lms']."_lo_param "
			."WHERE idParam = '".(int)$idParam."'";
	$rs = mysql_query($query) or 
			errorCommunication( 'delLOParam' );	
}

?>