wip: pmelo

This commit is contained in:
marko
2024-01-20 12:40:00 +01:00
parent d665230ad5
commit 62bd7cb14b
3 changed files with 73 additions and 14 deletions

View File

@@ -241,14 +241,18 @@ SQL;
return $this->name; return $this->name;
} }
public function getFirstname() public function getFirstName()
{ {
return $this->firstName; return $this->firstName;
} }
public function getConfig() public function getConfig()
{ {
return $this->config; return $this->$config;
}
public function getStrBirthday(){
return $this->dateOfBirth->format("Y-m-d");
} }
// static functions // static functions
@@ -325,4 +329,34 @@ SQL;
} }
return User::fromDbArray($response[0]); 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` = :attributeId".
" ORDER BY `wkParticipo_Users`.`id` ASC;";
$response = dbConnector::query(
$query
, [
'attributeId'=>[
'value'=>filterId($attributeId)
, 'data_type'=>PDO::PARAM_INT]
]
);
// 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

@@ -38,17 +38,20 @@ class PmElo{
} }
} }
if(!is_file("pmeloRanking.json")){ // load/recreate ranking data
$this->log['info'][] = "Couldn't find `pmeloRanking.json`. Create a new one!"; if(!is_file(self::$pmeloJson)){
file_put_contents("pmeloRanking.json", json_encode([])); $this->log['info'][] = "Couldn't find `".self::$pmeloJson."`. Create a new one!";
file_put_contents(self::$pmeloJson, json_encode([]));
} }
// init members // init members
$this->trainees = self::getTrainees(); $this->trainees = self::getTrainees();
$this->var_log(array_map(function($user){return $user->getFirstName();},$this->trainees), "trainees from db");
$this->rankings = self::createRankings(); $this->rankings = self::createRankings();
} }
// load all active trainees into a id=>User assoc array
public static function getTrainees(){ public static function getTrainees(){
$traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4)); $traineeList = array_map('User::fromDbArray', User::dbSelectWithAttribute(4));
$traineeAssocArray = []; $traineeAssocArray = [];
@@ -57,31 +60,51 @@ class PmElo{
} }
return $traineeAssocArray; return $traineeAssocArray;
} }
// load ranking from json file
public static function loadRankings(){ public static function loadRankings(){
return json_decode(file_get_contents("pmeloRanking.json")); return json_decode(file_get_contents(self::$pmeloJson));
} }
// save a ranking to json file
public static function saveRankings(array $rankings){ public static function saveRankings(array $rankings){
file_put_contents("pmeloRankings.json", json_encode($rankings)); file_put_contents(self::$pmeloJson, json_encode($rankings));
}
function var_log($variable, string $prefix=null, string $logChannel='info'){
if(!in_array($logChannel, ['info', 'warning', 'error'])){
$logChannel='info';
}
$prefix = (!empty($prefix) ? $prefix.": " : "");
$this->log[$logChannel][]=$prefix.var_export($variable, true);
} }
public function createRankings(){ public function createRankings(){
// load the last state of the ranking // load the last state of the ranking
$loadedRanking = self::loadRankings(); $loadedRanking = self::loadRankings();
$this->var_log($loadedRanking,"ranking");
// check if the ranked trainees still are in training // check if the ranked trainees still are in training
$cleanRanking = []; $cleanRanking = [];
$toBeInsertedTrainees = []; $toBeInsertedTrainees = $this->trainees;
foreach($loadedRanking as $rank=>$id){ foreach($loadedRanking as $rank=>$id){
if(array_key_exists($id, $this->trainees)){ if(array_key_exists($id, $this->trainees)){
$cleanRanking[] = $id; $cleanRanking[] = $id;
continue; // id of the not in the ranking, we have to insert
unset($toBeInsertedTrainees[$id]);
} }
// 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;}); $newTraineesIds = array_map(function($u){return $u->getId();},$toBeInsertedTrainees);
var_dump($toBeInsertedTrainees); usort(
$newTraineesIds
, function($lhs, $rhs){
return $this->trainees[$lhs]->getStrBirthday() < $this->trainees[$rhs]->getStrBirthday() ? -1 : ($this->trainees[$lhs]->getStrBirthday() > $this->trainees[$rhs]->getStrBirthday() ? 1 : 0);
}
);
$this->var_log(array_map(function($id){return $this->trainees[$id]->getStrBirthday();},$newTraineesIds), "newTrainieesIdsByAge");
$this->var_log($newTraineesIds, "newTrainieesIdsByAge");
$this->var_log(array_map(function($user){return $user->getFirstName();}, $toBeInsertedTrainees), "newTrainees");
// insert new trainees in the ranking // insert new trainees in the ranking
// foreach($this->trainees as $trainee){ // foreach($this->trainees as $trainee){
@@ -96,4 +119,6 @@ class PmElo{
public $trainees = null; public $trainees = null;
// list of id-s in the order of the current ranking // list of id-s in the order of the current ranking
public $rankings = null; public $rankings = null;
static $pmeloJson="pmeloRanking.json";
} }

View File

@@ -69,7 +69,7 @@
<ul class="collection with-header"> <ul class="collection with-header">
<li class="collection-header"><h3>Logs</h3></li> <li class="collection-header"><h3>Logs</h3></li>
<?php <?php
foreach($pmelo->log as $logLevel=>$mesages){ foreach($pmelo->log as $logLevel=>$messages){
if(!empty($messages)){ if(!empty($messages)){
?> ?>
<li class="collection-item"> <li class="collection-item">