wip: pmelo

This commit is contained in:
marko
2024-01-19 05:12:42 +01:00
parent baf6d9d2bf
commit d665230ad5
3 changed files with 146 additions and 45 deletions

View File

@@ -248,7 +248,7 @@ SQL;
public function getConfig()
{
return $this->$config;
return $this->config;
}
// static functions
@@ -325,27 +325,4 @@ SQL;
}
return User::fromDbArray($response[0]);
}
public static function loadFromDbByAttribute(int $attributeId){
$query =
"SELECT DISTINCT" .
" `wkParticipo_Users`.* " .
" FROM `wkParticipo_Users`" .
" JOIN `wkParticipo_user<=>userAttributes`" .
" ON `wkParticipo_user<=>userAttributes`.`userId` = `wkParticipo_Users`.`id`" .
" WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = 4".
" ORDER BY `wkParticipo_Users`.`id` ASC;";
$response = dbConnector::query($query);
// Postprocessing
// - convert the comma separated list into an array
foreach ($response as &$user) {
$user["eMail"] = explode(",", $user["eMail"]);
foreach ($user["eMail"] as &$email) {
$email = trim($email);
}
}
return $response;
}
}

View File

@@ -1,16 +1,99 @@
<?php
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
set_include_path(get_include_path() . PATH_SEPARATOR . './lib/');
setlocale(LC_ALL, "de_DE@euro", "de_DE", "de", "ge");
set_include_path(get_include_path() . PATH_SEPARATOR . "./lib/");
// start a session
session_start();
// Configs
require_once 'config/participo.php';
require_once $config['basePath'] . '/config/cwsvJudo.config.php';
require_once("config/participo.php");
require_once $config['basePath'] . "/config/cwsvJudo.config.php";
// Libs
require_once 'participoLib/user.php';
require_once("participoLib/user.php");
participo::init($cwsvJudoConfig);
$trainees = User::loadFromDbByAttribute(4);
$pmelo = new PmElo();
?>
class PmElo{
function __construct()
{
// starting/continuing a session
session_start();
// create 'pmelo' section in the sessions data
if(!array_key_exists('pmelo', $_SESSION)){
$_SESSION['pmelo'] = [];
}
// - data storage for id-s of chosen fighters
if(!array_key_exists('fighterIds', $_SESSION)){
$_SESSION['pmelo']['fighterIds'] = null;
}
// check for action in post data
if(array_key_exists('pmelo', $_POST)){
if(array_key_exists('fighterIds', $_POST['pmelo'])){
$this->fighters = array_map('User::loadFromDb', $_POST['pmelo']['fighterIds']);
}
}
if(!is_file("pmeloRanking.json")){
$this->log['info'][] = "Couldn't find `pmeloRanking.json`. Create a new one!";
file_put_contents("pmeloRanking.json", json_encode([]));
}
// init members
$this->trainees = self::getTrainees();
$this->rankings = self::createRankings();
}
public static function getTrainees(){
$traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4));
$traineeAssocArray = [];
foreach($traineeList as $trainee){
$traineeAssocArray[$trainee->getId()] = $trainee;
}
return $traineeAssocArray;
}
public static function loadRankings(){
return json_decode(file_get_contents("pmeloRanking.json"));
}
public static function saveRankings(array $rankings){
file_put_contents("pmeloRankings.json", json_encode($rankings));
}
public function createRankings(){
// load the last state of the ranking
$loadedRanking = self::loadRankings();
// check if the ranked trainees still are in training
$cleanRanking = [];
$toBeInsertedTrainees = [];
foreach($loadedRanking as $rank=>$id){
if(array_key_exists($id, $this->trainees)){
$cleanRanking[] = $id;
continue;
}
// id of the not in the ranking, we have to insert
$toBeInsertedTrainees[$id] = $this->trainees[$id];
}
usort($toBeInsertedTrainees, function($lhs, $rhs){return $lhs < $rhs ? -1 : 1;});
var_dump($toBeInsertedTrainees);
// insert new trainees in the ranking
// foreach($this->trainees as $trainee){
// if(empty($this->rankings)){
// }
// }
}
public $log = ['error'=>[], 'warning'=>[], 'info'=>[]];
public $fighters = null;
public $trainees = null;
// list of id-s in the order of the current ranking
public $rankings = null;
}

View File

@@ -37,21 +37,62 @@
</header>
<main>
<h2>pmElo</h2>
<?php var_dump($_POST);?>
<form id="form" action="pmelo.php" method="post">
<div class="input-field col s12">
<select name ="selectedOptions[]" id="form-select-2" multiple>
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<label for="form-select-2">Materialize Multiple Select</label>
<input type="submit" value="submit">
</div>
</form>
<h2><a href="pmelo">pmElo</a></h2>
<form id="pmelo-form-fighters" action="pmelo.php" method="post">
<div class="input-field col s12">
<select name ="pmelo[fighterIds][]" id="pmelo-form-fighters-select" multiple>
<option value="" disabled selected>Kämpfer auswählen</option>
<?php
foreach($pmelo->trainees as $trainee){
echo("<option value=\"".$trainee->getId()."\">".$trainee->getFirstname()." ".$trainee->getName()."</option>".PHP_EOL);
}
?>
</select>
<label for="form-select-2">Kämpfer auswählen</label>
<input type="submit" value="Liste erstellen">
</div>
</form>
<table>
<thead>
<tr><th>Platz</th><th>Name</th><th>Aufstieg</th></tr>
</thead>
<tbody>
<?php if(!empty($pmelo->fighters)) foreach($pmelo->fighters as $fighter){
echo("<tr><td>#</td><td>".$fighter->getFirstname()." ".$fighter->getName()."</td><td>^</td></tr>".PHP_EOL);
}
?>
</tbody>
</table>
<ul class="collection with-header">
<li class="collection-header"><h3>Logs</h3></li>
<?php
foreach($pmelo->log as $logLevel=>$mesages){
if(!empty($messages)){
?>
<li class="collection-item">
<ul class="collection with-header">
<li class="collection-header"><?php echo($logLevel);?></li>
<?php
foreach($messages as $message){
?>
<li>
<?php echo($message); ?>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
<?php
}
?>
</ul>
</main>
</body>
</html>