127 lines
3.9 KiB
PHP
127 lines
3.9 KiB
PHP
<?php
|
|
// get a Connection to the database
|
|
function getPdoDbConnection($hostname, $dbName, $user, $password){
|
|
try{
|
|
$dbConnection = new PDO(
|
|
'mysql:host='.$hostname.';dbname='.$dbName,
|
|
$user,
|
|
$password
|
|
);
|
|
}
|
|
catch(PDOException $dbError){
|
|
echo( "Error whilst getting a dbConnection!: " . $dbError->getMessage() );
|
|
}
|
|
return $dbConnection;
|
|
}
|
|
|
|
function createDb($dbConnection){
|
|
<<<SQL
|
|
CREATE TABLE `cwsvjudo`.`anwesenheit` (
|
|
`id` INT UNSIGNED NOT NULL ,
|
|
`userId` INT UNSIGNED NOT NULL ,
|
|
`date` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP ,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE = InnoDB;
|
|
ALTER TABLE `cwsvjudo`.`anwesenheit` ADD UNIQUE `attandence` (`userId`, `id`);
|
|
SQL;
|
|
}
|
|
|
|
/// perform a pdo-query
|
|
///
|
|
/// @param aDbConnection
|
|
/// @param $aQueryString
|
|
/// @param $aBindArray e.g. array(
|
|
/// ':userId' => array('value'=>$anUserId, 'data_type'=>PDO::PARAM_INT),
|
|
/// ':attributeId'=> array('value'=>$anAttributeId, 'data_type'=>PDO::PARAM_INT) )
|
|
/// @param $someOption
|
|
function dbQuery($aDbConnection, $aQueryString, $aBindArray = array(), $someOptions = array()){
|
|
// Standardbelegungen
|
|
if( empty($someOptions['dbCharset' ]) ) $someOptions['dbCharset' ] = "ISO-8859-1";
|
|
if( empty($someOptions['outCharset']) ) $someOptions['outCharset'] = "UTF-8";
|
|
if( empty($someOptions['dontFetch' ]) ) $someOptions['dontFetch' ] = false;
|
|
/// @toDo: Bisher wird nur die Rückgabe konvertiert. Eigentlich muss
|
|
/// doch auch die Eingabe konvertiert werden. Aber das jetzt
|
|
/// umzustellen wird schwer! Die User m Wettkampfplaner sind ja z.B.
|
|
/// als UTF8 in latin1(?) gespeichert.
|
|
/// @toDo: Die Standardwerte sollten vielleicht aus einer config
|
|
/// kommen, nicht hardcoded
|
|
try{
|
|
$pdoStatement = $aDbConnection->prepare( $aQueryString );
|
|
foreach( $aBindArray as $bindName => $bind ){
|
|
$pdoStatement->bindValue(
|
|
$bindName,
|
|
$bind['value'],
|
|
(isset($bind['data_type'])?$bind['data_type']:PDO::PARAM_STR)
|
|
);
|
|
}
|
|
$pdoResult = $pdoStatement->execute();
|
|
|
|
//if(!$pdoResult)
|
|
//echo("Strange! \"".$aQueryString."\" failed without exception!");
|
|
|
|
if($someOptions['dontFetch']){
|
|
$ret = NULL;
|
|
}
|
|
else{
|
|
$ret = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
}
|
|
catch(PDOException $db_error){
|
|
print "Error!: " . $db_error->getMessage() . "<br/>";
|
|
return null;
|
|
}
|
|
// Zeichensatzkonvertierung
|
|
if( is_array($ret) ){
|
|
foreach($ret as &$entry){
|
|
array_walk(
|
|
$entry,
|
|
function (&$value, $key, $someOptions) {
|
|
$value = iconv($someOptions['dbCharset'], $someOptions['outCharset'], $value);
|
|
},
|
|
$someOptions
|
|
);
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
function getLastAttendances($db){
|
|
$query = <<<SQL
|
|
SELECT date, vorname, name, corona_PLZ, corona_telephon, corona_eMail
|
|
FROM `cwsvjudo`.`anwesenheit`
|
|
JOIN `cwsvjudo`.`wkParticipo_Users`
|
|
ON `cwsvjudo`.`anwesenheit`.`userId` = `cwsvjudo`.`wkParticipo_Users`.`id`
|
|
ORDER BY `date` DESC, `name`;
|
|
SQL;
|
|
$params = array();
|
|
$options = array();
|
|
$ret = dbQuery($db, $query, $params, $options);
|
|
return $ret;
|
|
}
|
|
|
|
function getJudokasInTraining($dbConnection, $attributeName){
|
|
$query = <<<SQL
|
|
SELECT userId, name, vorname
|
|
FROM `cwsvjudo`.`wkParticipo_Users`
|
|
JOIN `cwsvjudo`.`wkParticipo_user<=>userAttributes`
|
|
ON `cwsvjudo`.`wkParticipo_Users`.`id` =`cwsvjudo`.`wkParticipo_user<=>userAttributes`.`userId`
|
|
WHERE `cwsvjudo`.`wkParticipo_user<=>userAttributes`.`attributeId` IN (
|
|
SELECT `id` FROM `cwsvjudo`.`wkParticipo_userAttributes` WHERE `name` = :attributeName
|
|
);
|
|
SQL;
|
|
$params = array(
|
|
':attributeName' => array('value'=>$attributeName, 'data_type'=>PDO::PARAM_STR)
|
|
);
|
|
return dbQuery($dbConnection, $query, $params);
|
|
}
|
|
|
|
function giveJudokasAttendence($dbConnection, $date, $ids){
|
|
$values = array();
|
|
foreach( $ids as $id){
|
|
array_push( $values, "(\"".$date."\", ".$id.")");;
|
|
}
|
|
$query = "INSERT INTO `cwsvjudo`.`anwesenheit` (`date`, `userId`) VALUES ".join(",", $values).";";
|
|
dbQuery($dbConnection, $query, array(), ['dontFetch' => true]);
|
|
}
|
|
?>
|