- section for adding new corona trainee - list (of the last month) can be send to an eMail
190 lines
6.1 KiB
PHP
190 lines
6.1 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($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
|
|
);
|
|
}
|
|
}
|
|
//var_dump($ret);
|
|
//var_dump($aQueryString);
|
|
//var_dump($aBindArray);
|
|
return $ret;
|
|
}
|
|
|
|
function getLastAttendances($db, $minDate=null){
|
|
if ($minDate == null){
|
|
$minDate = new DateTime;
|
|
$minDate->sub(new DateInterval("P1M")); // from the current date subtract a *P*eriod of *1* *M*onth
|
|
}
|
|
|
|
$query = <<<SQL
|
|
SELECT userId, date, vorname, name, corona_PLZ, corona_telephon, corona_eMail
|
|
FROM `cwsvjudo`.`anwesenheit`
|
|
JOIN `cwsvjudo`.`wkParticipo_Users`
|
|
ON `cwsvjudo`.`anwesenheit`.`userId` = `cwsvjudo`.`wkParticipo_Users`.`id`
|
|
WHERE :minDate <= date
|
|
ORDER BY `date` DESC, `name`;
|
|
SQL;
|
|
$params = array(
|
|
'minDate' => array('value' => $minDate->format('Y-m-d'), 'data_type' => PDO::PARAM_STR)
|
|
);
|
|
$options = array();
|
|
$ret = dbQuery($db, $query, $params, $options);
|
|
return $ret;
|
|
}
|
|
|
|
function getUsersWithAttribute($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 giveUserAnUserAttribute($dbConnection, $userId, $attributeName){
|
|
$query = <<<SQL
|
|
INSERT INTO `cwsvjudo`.`wkParticipo_user<=>userAttributes` (`userId`, `attributeId`)
|
|
SELECT :userId, `id`
|
|
FROM `cwsvjudo`.`wkParticipo_userAttributes`
|
|
WHERE `name` = :attributeName;
|
|
SQL;
|
|
$params = array(
|
|
':userId' => array('value'=>$userId, 'data_type'=>PDO::PARAM_INT),
|
|
':attributeName' => array('value'=>$attributeName, 'data_type'=>PDO::PARAM_STR)
|
|
);
|
|
return dbQuery($dbConnection, $query, $params);
|
|
}
|
|
|
|
function giveJudokasAttendence($dbConnection, $date, $ids){
|
|
$values = array();
|
|
try{
|
|
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]);
|
|
}
|
|
catch(PDOException $db_error){
|
|
print "Error!: " . $db_error->getMessage() . "<br/>";
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// updates corona data of an user
|
|
function updateCoronaData($db, $userId, $columnName, $columnValue){
|
|
$coronaColumnNames = ["corona_PLZ", "corona_telephon", "corona_eMail"];
|
|
|
|
if( !in_array( $columnName, $coronaColumnNames) ){
|
|
return;
|
|
}
|
|
$query = "UPDATE `cwsvjudo`.`wkParticipo_Users` SET `".$columnName."`=:val WHERE `id`=:id;";
|
|
$params = array(
|
|
':val' => array('value'=>$columnValue, 'data_type'=>PDO::PARAM_STR),
|
|
':id' => array('value'=>$userId, 'data_type'=>PDO::PARAM_INT)
|
|
);
|
|
dbQuery($db, $query, $params);
|
|
return;
|
|
}
|
|
|
|
function addCoronaUser($db, $name, $vorname, $corona_PLZ, $corona_telephon, $corona_eMail){
|
|
$query = <<<SQL
|
|
INSERT INTO `cwsvjudo`.`wkParticipo_Users` (name, vorname, corona_PLZ, corona_telephon, corona_eMail)
|
|
VALUES (:name, :vorname, :plz, :telephon, :email);
|
|
SQL;
|
|
$params = array(
|
|
':name' => array('value'=>$name, 'data_type'=>PDO::PARAM_STR),
|
|
':vorname' => array('value'=>$vorname, 'data_type'=>PDO::PARAM_STR),
|
|
':plz' => array('value'=>$corona_PLZ, 'data_type'=>PDO::PARAM_STR),
|
|
':telephon' => array('value'=>$corona_telephon, 'data_type'=>PDO::PARAM_STR),
|
|
':email' => array('value'=>$corona_eMail, 'data_type'=>PDO::PARAM_STR),
|
|
);
|
|
dbQuery($db, $query, $params);
|
|
|
|
$newId = $db->lastInsertId();
|
|
giveUserAnUserAttribute($db, $newId, "inTraining");
|
|
return;
|
|
}
|
|
?>
|