- Records page introduced

- rearangements in the include structure
This commit is contained in:
marko
2020-11-19 12:53:28 +01:00
parent a0ef0bf40c
commit de24498f77
15 changed files with 433 additions and 365 deletions

View File

@@ -4,24 +4,34 @@ require_once('./lib/db.php');
// A series of achievements
class achievementGroup{
// data from the achievements group table
// id of the group
private $id;
// name of the group
private $name;
// Achievement, that unlocks the group
private $unlockingAchievementId;
// an image for the group
private $imageUrl;
// achievements, that belong to this group
private $achievements;
// pointer to the db that shall be used for queries
private static $db=null;
// name of the table
// private static $tableName = "`cwsvjudo`.`machs_achievementGroups`";
static function setDbConnection($dbConnection){
// set the dbConnection (just setting, no establishing)
public static function setDbConnection($dbConnection){
if($dbConnection instanceof PDO)
self::$db = $dbConnection;
else
self::$db = null;
return;
}
static function getDbConnection(){
// get the db pointer
public static function getDbConnection(){
return self::$db;
}
@@ -46,7 +56,7 @@ SQL;
return $groups;
}
//getter functions for the member variables
//getter functions for the (primitive) member variables
function getId(){return $this->id;}
function getName(){return $this->name;}
function getUnlockingAchievementId(){return $this->unlockingAchievementId;}
@@ -69,13 +79,13 @@ SQL;
return $this->achievements;
} // end getAchievements
/// Load the achievementgroup $id from the db into the
/// Load the achievementgroup $id from the db (into this)
function loadAchievementGroupFromDb($id){
$query = <<<SQL
SELECT * FROM `cwsvjudo`.`machs_achievementGroups` WHERE `id` = :id;
SQL;
$params = [':id'=>['value'=>$id, 'data_type'=>PDO::PARAM_INT]];
$result = dbQuery(self::$db, $query, $param);
$result = dbQuery(self::$db, $query, $params);
$this->setAchievementGroupData(
$result[0]['id'],
@@ -94,23 +104,6 @@ SQL;
$this->imageUrl = $imageUrl;
}
/// A simple representation of the group in html code
function html(){
$html = "";
$html .= "<div>";
$html .= "<dl>";
$html .= "<dt>Id: </dt><dd>".$this->id."</dd>";
$html .= "<dt>name: </dt><dd>".$this->name."</dd>";
$html .= "<dt>unlockingAchievementId: </dt><dd>".$this->unlockingAchievementId."</dd>";
$html .= "<dt>Achievements: </dt><dd><ul>";
foreach($this->achievements as $a)
$html .= "<li>".$a['name']."</li>";
$html .= "</ul></dd>";
$html .= "</dl>";
$html .= "</div>";
return $html;
}
/// gets all achievements of that user in that group reachedd
function getUsersAchievements($userId, $options=[]){
$getAll = $options['getAll']??false;
@@ -131,7 +124,7 @@ return $result;
///
/// @param $uId id of the user the achievements should be
function asHtmlCard($uId, $options=[]){
$noForm = $options['noForm']??true;
$noForm = $options['noForm']??true; // for deactivating the give achievement form
$retHtml = "";
$userData = record::getUserData($uId);
$usersAchievements = $this->getUsersAchievements( $uId, ['getAll'=>true] );
@@ -139,6 +132,7 @@ return $result;
foreach($usersAchievements as $a){
$usersAchievementIds[]=(int)$a['achievementId'];
}
// If the user hasn't have the needed Achievement
if(!in_array($this->getUnlockingAchievementId(), $usersAchievementIds))
return "";
$records = record::getGroupsRecords(
@@ -149,15 +143,17 @@ return $result;
$retHtml .= "<div class=\"col s12 m6 l4 xl3\">";
$retHtml .= "<div class=\"card\">";
$retHtml .= "<div class=\"card-content\">";
if($this->imageUrl != null){
$retHtml .= "<div class=\"card-image\"><img src=\"".$this->imageUrl."\">";
}
$retHtml .= "<span class=\"card-title\">".$this->name."</span>";
if($this->imageUrl != null){
$retHtml .= "<span class=\"card-title\">".$this->name."</span>";
$retHtml .= "</div>";
}
$retHtml .= "<ul>";
else
$retHtml .= "<span style=\"padding-left: 1vw;\" class=\"card-title\">".$this->name."</span>";
$retHtml .= "<div class=\"card-content\">";
$retHtml .= "<ul class=\collapsible\">";
foreach($achievements as $a){
if(in_array((int)$a['id'], $usersAchievementIds)){
$retHtml .= "<li>&#10003; ".$a['name'].": ".$a['description'];
@@ -165,34 +161,43 @@ return $result;
}
else{
$retHtml .= "<li style=\"color:gray\">".$a['name'].": ".$a['description'];
if(!$noForm){
if( canUserGetAchievementToday( $this->getDbConnection(), $uId) or isUserAdmin($this->getDbConnection(), $_SESSION['user']['userId']) ){
$retHtml .= "<form action=\".\" method=\"POST\">";
$retHtml .= "<input name=\"action\" value=\"giveUserAnAchievement\" type=\"hidden\">";
$retHtml .= "<input name=\"redirectLocation\" value=\"./#achievementList-".$uId."\" type=\"hidden\">";
$retHtml .= "<input name=\"userId\" value=\"".$uId."\" type=\"hidden\">";
$retHtml .= "<input name=\"achievementId\" value=\"".$a['id']."\" type=\"hidden\">";
$retHtml .= "<input style=\"width:100%\" name=\"submit\" type=\"submit\" value=\"Achievement ".$a['name']." geben\">";
$retHtml .= "</form>";
}
}
if( $imgUrl != null )
$retHtml .= " <div class=\"card-image\" ><img src=\"".$imgUrl."\"/></div>";
$retHtml .= "</li>";
break;
}
}
// show the current record
if( validateDate($userData[0]['gebDatum'])){
$retHtml.=record::arrayRecord2htmlCard($records[0], $userData[0], $this->getId(), "li");
}
else{
$retHtml.="<div>Rekorde können erst angezeigt werden, wenn das <a href=./setUserData.php>Geburtsdatum korrekt gesetzt</a> wurde!</div>";
}
$retHtml .= "</ul>";
$retHtml .= "</div>";// end card-content
$retHtml .= "<div class=\"card-action\">";
if(!$noForm){
if( canUserGetAchievementToday( $this->getDbConnection(), $uId) or isUserAdmin($this->getDbConnection(), $_SESSION['user']['userId']) ){
$retHtml .= "<form action=\".\" method=\"POST\">";
$retHtml .= "<input name=\"action\" value=\"giveUserAnAchievement\" type=\"hidden\" />";
$retHtml .= "<input name=\"redirectLocation\" value=\"./#achievementList-".$uId."\" type=\"hidden\" />";
$retHtml .= "<input name=\"userId\" value=\"".$uId."\" type=\"hidden\" />";
$retHtml .= "<input name=\"achievementId\" value=\"".$a['id']."\" type=\"hidden\" />";
$retHtml .= "<input name=\"submit\" type=\"submit\" value=\"".$a['name']." geben\">";
// $retHtml .= "<button class=\"btn\" name=\"submit\" type=\"submit\" >Achievement ".$a['name']." geben</button>";
$retHtml .= "</form>";
}
if( validateDate($userData[0]['gebDatum'])){
$retHtml.=record::arrayRecord2htmlCardAction($records[0], $userData[0], $this->getId(), "li");
}
else{
$retHtml.="<div>Rekorde können erst angezeigt werden, wenn das <a href=./setUserData.php>Geburtsdatum korrekt gesetzt</a> wurde!</div>";
}
}
$retHtml .= "</div>";
$retHtml .= "</div>";
$retHtml .= "</div>";
$retHtml .= "</div>";// end card
$retHtml .= "</div>";// end col
return $retHtml;
}// end asHtmlCard
@@ -228,5 +233,73 @@ return $result;
return $html;
}
static function htmlAutoAddAchievementsForm($achievementGroupId, $messageTemplate=null, $from=null, $to=null, $step=null){
$html = "";
$html .= "<form action=\".\" method=\"POST\">";
$html .= "<input name=\"action\" type=\"hidden\" value=\"autoAddAchievements\" />";
$html .= "<input name=\"redirectLocation\" type=\"hidden\" value=\"achievementBuilder.php\" />";
$html .= "<input name=\"achievementGroupId\" type=\"hidden\" value=\"".$achievementGroupId."\"/>";
$html .= "<label for=\"messageTemplate\">messageTemplate ('%value%' is Placeholder for the value that goes from fromValue to toValue in step)</label>";
$html .= "<input style=\"width:100%;display:block;\" name=\"messageTemplate\" type=\"textarea\" value=\"".$messageTemplate."\"/>";
$html .= "<label for\"from\">fromValue</label>";
$html .= "<input name=\"from\" type=\"text\" value=\"".$from."\"/>";
$html .= "<label for\"to\">toValue</label>";
$html .= "<input name=\"to\" type=\"text\" value=\"".$to."\"/>";
$html .= "<label for\"step\">step</label>";
$html .= "<input name=\"step\" type=\"text\" value=\"".$step."\"/>";
$html .= "<input type=\"submit\" value=\"Auto Add Achievements\" />";
$html .= "</form>";
return $html;
}
/// auto generate the achievements for this group
/// @todo only for empty groups?
function autoAddAchievements($messageTemplate, $from, $to, $step){
if(!empty($this->getAchievements())){
echo("Won't auto-add Achievements to non-empty AchievementGroup!");
return;
}
$level = 1;
for ($value = (int)$from; $value <= (int)$to; $value+=(int)$step) {
$name = $this->getName()." ".intToRomanRepresentation($level++);
$description = str_replace("%value%", (string)$value, $messageTemplate);
$this->addAchievement($name, $description, $level);
}
}
// adds an achievement to the group (in the db!)
function addAchievement($name, $description, $level, $rootId=null){
addAchievement(
$this->getDbConnection(),
$name,
$rootId,
$this->getId(),
$level,
$description,
null
);
}
}
/// convert an int number to roman representation
/// @param int $number
/// @return string
function intToRomanRepresentation($number){
$map = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
$returnValue = '';
while ($number > 0) {
foreach ($map as $roman => $int) {
if($number >= $int) {
$number -= $int;
$returnValue .= $roman;
break;
}
}
}
return $returnValue;
}
?>

View File

@@ -11,4 +11,8 @@ document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.sidenav');
var instances = M.Sidenav.init(elems, options);
});
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.collapsible');
var instances = M.Collapsible.init(elems, options);
});
</script>

View File

@@ -2,6 +2,7 @@
<ul id="slide-out" class="sidenav">
<li><?php echo $login_status;?></li>
<li><a href=".">Achievements</a></li>
<li><a href="records.php">Rekorde</a></li>
<li><a href="./setUserData.php">Benutzerdaten</a></li>
<?php
if(isUserAdmin($dbConnection, $_SESSION['user']['userId'])){