- added simple achievement editor
- added records
This commit is contained in:
232
homepage/machs/lib/machs/achievementGroup.php
Normal file
232
homepage/machs/lib/machs/achievementGroup.php
Normal file
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
require_once('./lib/db.php');
|
||||
|
||||
|
||||
// A series of achievements
|
||||
class achievementGroup{
|
||||
private $id;
|
||||
private $name;
|
||||
private $unlockingAchievementId;
|
||||
private $imageUrl;
|
||||
|
||||
private $achievements;
|
||||
|
||||
private static $db=null;
|
||||
// private static $tableName = "`cwsvjudo`.`machs_achievementGroups`";
|
||||
|
||||
static function setDbConnection($dbConnection){
|
||||
if($dbConnection instanceof PDO)
|
||||
self::$db = $dbConnection;
|
||||
else
|
||||
self::$db = null;
|
||||
return;
|
||||
}
|
||||
static function getDbConnection(){
|
||||
return self::$db;
|
||||
}
|
||||
|
||||
/// Returns an array of all Achievementgroups
|
||||
static function getAllAchievementGroups(){
|
||||
$query = <<<SQL
|
||||
SELECT * FROM `cwsvjudo`.`machs_achievementGroups`;
|
||||
SQL;
|
||||
$result = dbQuery(self::$db, $query);
|
||||
$groups = [];
|
||||
foreach($result as $r){
|
||||
$gid = (int)$r['id'];
|
||||
$groups[$gid] = new achievementGroup;
|
||||
$groups[$gid]->setAchievementGroupData(
|
||||
$r['id'],
|
||||
$r['name'],
|
||||
$r['unlockingAchievementId'],
|
||||
$r['imageUrl']
|
||||
);
|
||||
$groups[$gid]->getAchievements(['force'=>true]);
|
||||
}
|
||||
return $groups;
|
||||
}
|
||||
|
||||
//getter functions for the member variables
|
||||
function getId(){return $this->id;}
|
||||
function getName(){return $this->name;}
|
||||
function getUnlockingAchievementId(){return $this->unlockingAchievementId;}
|
||||
function getImageUrl(){return $this->imageUrl;}
|
||||
|
||||
/// returns list of achievements
|
||||
/// - returns the previously loaded achievements
|
||||
/// - reloads them if null or forced
|
||||
function getAchievements($options=[]){
|
||||
// standards for Options:
|
||||
$force = $options['force']??false; // load the achievements from the db even if ther already are some in the group->achievements member
|
||||
if(($this->achievements == null) or ($force)){
|
||||
$query = <<<SQL
|
||||
SELECT * FROM `achievements`
|
||||
WHERE `achievementGroupId`=:groupId;
|
||||
SQL;
|
||||
$params = [':groupId'=>['value'=>$this->id, 'data_type'=>PDO::PARAM_INT]];
|
||||
$this->achievements = dbQuery(self::$db, $query, $params);
|
||||
}
|
||||
return $this->achievements;
|
||||
} // end getAchievements
|
||||
|
||||
/// Load the achievementgroup $id from the db into the
|
||||
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);
|
||||
|
||||
$this->setAchievementGroupData(
|
||||
$result[0]['id'],
|
||||
$result[0]['name'],
|
||||
$result[0]['unlockingAchievementId'],
|
||||
$result[0]['imageUrl']
|
||||
);
|
||||
$this->getAchievements(['force'=>true]);
|
||||
}
|
||||
|
||||
/// Set the member data of the group
|
||||
function setAchievementGroupData($id, $name, $unlockingAchievementId=null, $imageUrl=null){
|
||||
$this->id = (int)$id;
|
||||
$this->name = $name;
|
||||
$this->unlockingAchievementId = ($unlockingAchievementId == null ? null : (int)$unlockingAchievementId);
|
||||
$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;
|
||||
$query = "";
|
||||
$query.= "SELECT * FROM `cwsvjudo`.`achievements<=>user` ";
|
||||
$query.= "WHERE `userId` = :userId";
|
||||
if(!$getAll)
|
||||
$query.=" AND `achievementId` = :achievementId";
|
||||
$query.= ";";
|
||||
$params[':userId'] = array('value'=>$userId, 'data_type'=>PDO::PARAM_INT);
|
||||
if(!$getAll)
|
||||
$params[':achievementId'] = array('value'=>$this->getId(), 'data_type'=>PDO::PARAM_INT);
|
||||
$result = dbQuery($this->getDbConnection(), $query, $params);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/// returns the materialize card html code of the Achievementgroup
|
||||
///
|
||||
/// @param $uId id of the user the achievements should be
|
||||
function asHtmlCard($uId, $options=[]){
|
||||
$noForm = $options['noForm']??true;
|
||||
$retHtml = "";
|
||||
$userData = record::getUserData($uId);
|
||||
$usersAchievements = $this->getUsersAchievements( $uId, ['getAll'=>true] );
|
||||
$usersAchievementIds=[null];
|
||||
foreach($usersAchievements as $a){
|
||||
$usersAchievementIds[]=(int)$a['achievementId'];
|
||||
}
|
||||
if(!in_array($this->getUnlockingAchievementId(), $usersAchievementIds))
|
||||
return "";
|
||||
$records = record::getGroupsRecords(
|
||||
$this->getId(),
|
||||
record::birthday2ageClass($userData[0]['gebDatum'])
|
||||
);
|
||||
$achievements = $this->getAchievements();
|
||||
|
||||
$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 .= "</div>";
|
||||
}
|
||||
$retHtml .= "<ul>";
|
||||
foreach($achievements as $a){
|
||||
if(in_array((int)$a['id'], $usersAchievementIds)){
|
||||
$retHtml .= "<li>✓ ".$a['name'].": ".$a['description'];
|
||||
$retHtml .= "</li>";
|
||||
}
|
||||
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>";
|
||||
$retHtml .= "</div>";
|
||||
$retHtml .= "</div>";
|
||||
return $retHtml;
|
||||
}// end asHtmlCard
|
||||
|
||||
/// create html code for a for to add an achievementGroup
|
||||
static function htmlAddAchievementGroupForm(){
|
||||
$html = "";
|
||||
$html .= "<form action=\".\" method=\"POST\">";
|
||||
$html .= "<input name=\"action\" type=\"hidden\" value=\"addAchievementGroup\" />";
|
||||
$html .= "<input name=\"redirectLocation\" type=\"hidden\" value=\"achievementBuilder.php\" />";
|
||||
$html .= "<input name=\"name\" type=\"text\" placeholder=\"name\"/>";
|
||||
$html .= "<input name=\"unlockingAchievementId\" type=\"text\" placeholder=\"unlockingAchievementId\"/>";
|
||||
$html .= "<input name=\"imageUrl\" type=\"text\" placeholder=\"imageUrl\"/>";
|
||||
$html .= "<input type=\"submit\"/>";
|
||||
$html .= "</form>";
|
||||
return $html;
|
||||
}
|
||||
/// create html code for a for to edit an achievementGroup
|
||||
function htmlEditAchievementGroupForm(){
|
||||
$html = "";
|
||||
$html .= "Edit ".$this->getName();
|
||||
$html .= "<form action=\".\" method=\"POST\">";
|
||||
$html .= "<input name=\"action\" type=\"hidden\" value=\"editAchievementGroup\" />";
|
||||
$html .= "<input name=\"redirectLocation\" type=\"hidden\" value=\"achievementBuilder.php\" />";
|
||||
$html .= "<input name=\"id\" type=\"hidden\" value=\"".$this->getId()."\"/>";
|
||||
$html .= "<label for\"name\">name</label>";
|
||||
$html .= "<input name=\"name\" type=\"text\" value=\"".$this->getName()."\"/>";
|
||||
$html .= "<label for\"name\">unlockingAchievementId</label>";
|
||||
$html .= "<input name=\"unlockingAchievementId\" type=\"text\" value=\"".$this->getUnlockingAchievementId()."\"/>";
|
||||
$html .= "<label for\"name\">imageUrl</label>";
|
||||
$html .= "<input name=\"imageUrl\" type=\"text\" value=\"".$this->getImageUrl()."\"/>";
|
||||
$html .= "<input type=\"submit\"/>";
|
||||
$html .= "</form>";
|
||||
return $html;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
14
homepage/machs/lib/machs/materializeInit.php
Normal file
14
homepage/machs/lib/machs/materializeInit.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<!--
|
||||
Some Inits for the materializeCss
|
||||
-->
|
||||
<script>
|
||||
options={};
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var elems = document.querySelectorAll('.modal');
|
||||
var instances = M.Modal.init(elems, options);
|
||||
});
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var elems = document.querySelectorAll('.sidenav');
|
||||
var instances = M.Sidenav.init(elems, options);
|
||||
});
|
||||
</script>
|
||||
13
homepage/machs/lib/machs/sidenav.php
Normal file
13
homepage/machs/lib/machs/sidenav.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<!-- sidenav -->
|
||||
<ul id="slide-out" class="sidenav">
|
||||
<li><?php echo $login_status;?></li>
|
||||
<li><a href=".">Achievements</a></li>
|
||||
<li><a href="./setUserData.php">Benutzerdaten</a></li>
|
||||
<?php
|
||||
if(isUserAdmin($dbConnection, $_SESSION['user']['userId'])){
|
||||
echo("<li><a href=\"./achievementBuilder.php\">achievementBuilder</a></li>");
|
||||
}
|
||||
?>
|
||||
</ul>
|
||||
<a href="#" data-target="slide-out" class="sidenav-trigger show-on-large" style="font-size: 2em;">☰ M<small>ein</small>ACH<small>ievement</small>S<small>ystem</small></a>
|
||||
<!-- end sidenav -->
|
||||
Reference in New Issue
Block a user