make add/remove starter buttons date sensitive

This commit is contained in:
marko
2023-01-29 19:55:33 +01:00
parent 4ff0659f90
commit ece0ef5025
9 changed files with 492 additions and 196 deletions

View File

@@ -30,24 +30,9 @@ include_once 'events.inc.php';
<?php if ($_SESSION['login']) { ?> <?php if ($_SESSION['login']) { ?>
<main> <main>
<?php <h2 id="comingStarts">Eingeschriebene Starter</h2>
$eventList = participo::getEventStarter(); <?php Starter::htmlTableComingStarts(); ?>
$htmlTable = null;
if ($eventList) {
$htmlTable = '<table>'
. '<thead><tr><th>Datum</th><th>Veranstaltung</th><th>Starter</th></tr></thead>'
. '<tbody>';
foreach ($eventList as $event) {
$eventDate = DateTime::createFromFormat('Y-m-d', $event['eventDate']);
$htmlTable .= '<tr><td>' . getHtmlSquareDate($eventDate) . '</td><td>' . $event['eventName'] . '</td><td>' . $event['userName'] . ', ' . $event['userFirstname'] . '</td></tr>';
}
$htmlTable .= '</tbody></table>';
echo('<h2 id="comingStarts">Aktuelle Einschreibungen</h2>');
echo($htmlTable);
} else {
echo($eventList ? $htmlTable : '<div>Keine Meldungen zu bevorstehenden Events');
}
?>
<h2 id="eventList">Übersicht anstehender Events</h2> <h2 id="eventList">Übersicht anstehender Events</h2>
<!-- Table with events--> <!-- Table with events-->
<?php echo(eventPlaner::getHtmlEventTable(eventPlaner::getComingWkEvents())); ?> <?php echo(eventPlaner::getHtmlEventTable(eventPlaner::getComingWkEvents())); ?>

View File

@@ -5,7 +5,7 @@
<div class="col s12"> <div class="col s12">
<a href="http://cwsvjudo.bplaced.net/participo" class="breadcrumb">cwsvJudo-Apps</a> <a href="http://cwsvjudo.bplaced.net/participo" class="breadcrumb">cwsvJudo-Apps</a>
<a href="http://cwsvjudo.bplaced.net/participo/events" class="breadcrumb"> <a href="http://cwsvjudo.bplaced.net/participo/events" class="breadcrumb">
<?php echo($meta['title']);?> <?php echo($meta['title']); ?>
</a> </a>
<a class="right top-nav sidenav-trigger waves-effect waves-light hide-on-large-only" href="#" <a class="right top-nav sidenav-trigger waves-effect waves-light hide-on-large-only" href="#"
data-target="nav-mobile"> data-target="nav-mobile">
@@ -28,14 +28,22 @@
</a> </a>
</li> </li>
<!-- brings its own li --> <!-- brings its own li -->
<?php require_once("sidenav/loginStatus.php");?> <?php require_once 'sidenav/loginStatus.php'; ?>
<li class="bold"> <li class="bold">
<a class="waves-effect waves-teal right-align" href="#eventList">Liste anstehender Events<i <a class="waves-effect waves-teal" href="/participo">zurück<i
class="material-icons">home</i></a>
</li>
<li class="bold">
<a class="waves-effect waves-teal right-align" href="#comingStarts">Liste anstehender Starts<i
class="material-icons">format_list_bulleted</i></a> class="material-icons">format_list_bulleted</i></a>
</li> </li>
<li class="bold"> <li class="bold">
<a class="waves-effect waves-teal right-align" href="#detailedEventList">Eventdetails<i <a class="waves-effect waves-teal right-align" href="#eventList">Liste anstehender Events<i
class="material-icons">view_list</i></a> class="material-icons">view_list</i></a>
</li> </li>
<li class="bold">
<a class="waves-effect waves-teal right-align" href="#detailedEventList">Eventdetails<i
class="material-icons">event</i></a>
</li>
</ul> </ul>
</div><!-- cwsvJudoApps SideNav --> </div><!-- cwsvJudoApps SideNav -->

View File

@@ -40,7 +40,6 @@ $userData = getUserData(dbConnector::getDbConnection(), $_SESSION['user']['userI
// init materialize elements // init materialize elements
initModals(); initModals();
initSidenav(); initSidenav();
initSelects();
}); });
function initSidenav() { function initSidenav() {

View File

@@ -162,7 +162,8 @@ function changePassword($db, $changerId, $changeeId, $changerPassword, $newPassw
// if( !in_array($changeeId, $changersKidsIds) ){ // if( !in_array($changeeId, $changersKidsIds) ){
if (!isUserInKidIds($changeeId, $changersKidsIds)) { if (!isUserInKidIds($changeeId, $changersKidsIds)) {
// echo("not your child: ".$changeeId." not in "); var_dump($changersKidsIds); // echo("not your child: ".$changeeId." not in ");
// var_dump($changersKidsIds);
return false; return false;
} }
} }

View File

@@ -1,9 +1,13 @@
<?php <?php
require_once 'participoLib/participo.php';
require_once 'participoLib/shiai.php';
/** Framework for a event /** Framework for a event
*/ */
class Event class Event
{ {
// members in the db
private $id = null; //< unique id of the event in the db private $id = null; //< unique id of the event in the db
private $date = null; //< date for the event (@todo ranges?) private $date = null; //< date for the event (@todo ranges?)
private $shiaiId = null; //< unique id of the shiai in the db (if appropriate) private $shiaiId = null; //< unique id of the shiai in the db (if appropriate)
@@ -19,22 +23,78 @@ class Event
* @param int $shiaiId id of the linked shiai or null if not appropriate * @param int $shiaiId id of the linked shiai or null if not appropriate
* @param string $deadline deadline for sign ins in the format "YYYY-MM-DD" * @param string $deadline deadline for sign ins in the format "YYYY-MM-DD"
* @param string $remarks (json formatted) string with meta information * @param string $remarks (json formatted) string with meta information
* @param Shiai $shiai if the shiai is loaded anyway it can be placed here.
*/ */
public function __construct($id, $date, $shiaiId, $deadline, $remarks, $shiai) public function __construct($id, $date, $shiaiId, $deadline, $remarks)
{ {
//! @todo InputValidation //! @todo InputValidation
$this->id = (int) $id; $this->id = filterId($id);
$this->date = DateTime::createFromFormat('Y-m-d', $date); $this->date = DateTime::createFromFormat('Y-m-d', $date);
$this->shiaiId = (($shiaiId != null) ? ((int)$shiaiId) : (null)); $this->shiaiId = filterId($shiaiId);
$this->deadline = DateTime::createFromFormat('Y-m-d', $deadline); $this->deadline = DateTime::createFromFormat('Y-m-d', $deadline);
$this->remarks = $remarks; $this->remarks = $remarks;
$this->shiai = $shiai; $this->shiai = $shiai;
} }
// Getter
/** Getter for the id
*
* @return (int) Id of the event
*/
public function getId()
{
return $this->id;
}
/** Getter for the date
*
* @return DateTime date of the event
*/
public function getDate()
{
return $this->date;
}
/** Getter for the shiaiId
*
* @return int>0 id for the shiai in the db
*/
public function getShiaiId()
{
return $this->shiaiId;
}
/** Getter for the deadline
*
* @return DateTime deadline for the event
*/
public function getDeadLine()
{
return $this->deadline;
}
/** Getter for the shiai
*
* @return Shiai shiai for the event
*/
public function getShiai()
{
return $this->shiai;
}
public function loadShiai()
{
if ($this->shiaiId != null) {
$this->shiai = Shiai::loadFromDb($this->shiaiId);
}
return $this->shiai;
}
public static function loadFromDb(int $id) public static function loadFromDb(int $id)
{ {
$id = filterId($id);
$query = 'SELECT * FROM `cwsvjudo`.`wkParticipo_Events` WHERE `id` = :id;'; $query = 'SELECT * FROM `cwsvjudo`.`wkParticipo_Events` WHERE `id` = :id;';
$params = [':id' => ['value' => $id, 'data_type' => PDO::PARAM_INT]]; $params = [':id' => ['value' => $id, 'data_type' => PDO::PARAM_INT]];
$response = dbConnector::query($query, $params); $response = dbConnector::query($query, $params);
@@ -43,7 +103,7 @@ class Event
if (count($response) != 1) { if (count($response) != 1) {
return null; return null;
} }
return Event::fromDbArray($response[0]); return self::fromDbArray($response[0]);
} }
/** Representation of an event as (materializeCss) card /** Representation of an event as (materializeCss) card
@@ -52,10 +112,11 @@ class Event
*/ */
public function asHtmlCard() public function asHtmlCard()
{ {
$shiai = self::loadShiai();
return return
'<div class="card blue-grey darken-1">' . '<div class="card blue-grey darken-1">' .
'<div class="card-content white-text">' . '<div class="card-content white-text">' .
'<span class="card-title">' . $this->shiai->getHtmlName() . '</span>' . '<span class="card-title">' . $shiai->getHtmlName() . '</span>' .
'<dl>' . '<dl>' .
'<dt>Datum</dt>' . '<dt>Datum</dt>' .
'<dd>' . $this->date->format('Y-m-d') . '</dd>' . '<dd>' . $this->date->format('Y-m-d') . '</dd>' .
@@ -69,11 +130,12 @@ class Event
public function htmlTableRow() public function htmlTableRow()
{ {
$shiai = $this->loadShiai();
return return
'<tr>' . '<tr>' .
'<td>' . $this->date->format('Y-m-d') . '</td>' . '<td>' . $this->date->format('Y-m-d') . '</td>' .
'<td><a href="/participo/events#' . $this->id . '" >' . $this->shiai->getHtmlName() . '</a></td>' . '<td><a href="/participo/events#' . $this->id . '" >' . $shiai->getHtmlName() . '</a></td>' .
'<td>' . $this->shiai->getAgeClasses() . '</td>' . '<td>' . $shiai->getAgeClasses() . '</td>' .
'<td><a class="waves-effect waves-light btn modal-trigger" href="#event-modal-' . $this->id . '"><i class="material-icons">zoom_in</i></a></td>' . '<td><a class="waves-effect waves-light btn modal-trigger" href="#event-modal-' . $this->id . '"><i class="material-icons">zoom_in</i></a></td>' .
'</tr>'; '</tr>';
} }
@@ -109,9 +171,12 @@ class Event
return ''; return '';
} }
$starterList .= 'Bereits eingetragen: <div class="row">'; $starterList .= 'Bereits eingetragen: <div class="row">';
foreach ($listOfStarter as $s) { foreach ($listOfStarter as $start) {
$u = $s->getUser(); $startingUser = $start->loadStarter();
$starterList .= '<div class="col s12 m6">' . $u->getName() . ', ' . $u->getFirstname() . $this->getHtmlRemoveStarterForm($s->getEventId(), $s->getUserId()) . '</div>'; $starterList .= '<div class="col s12 m6"><div class="row valign-wrapper">'
. '<div class="col s6">' . $startingUser->getName() . ', ' . $startingUser->getFirstname() . ':</div>'
. '<div class="col s6">' . $start->getHtmlFormRemove() . '</div>'
. '</div></div>';
} }
$starterList .= '</div>'; $starterList .= '</div>';
return $starterList; return $starterList;
@@ -141,7 +206,7 @@ class Event
return $starter; return $starter;
} }
public static function fromArray($member) public static function fromDbArray($member)
{ {
$shiai = json_decode($member['bemerkungen'], true); $shiai = json_decode($member['bemerkungen'], true);
@@ -151,7 +216,7 @@ class Event
$member['wkId'] ?? null, $member['wkId'] ?? null,
$member['meldefrist'] ?? null, $member['meldefrist'] ?? null,
$member['bemerkungen'] ?? null, $member['bemerkungen'] ?? null,
shiai::fromArray(($shiai != null) ? $shiai : $member) shiai::fromDbArray(($shiai != null) ? $shiai : $member)
); );
} }
@@ -172,7 +237,13 @@ class Event
} }
// @todo docu // @todo docu
public static function getHtmlRemoveStarterForm($eventId, $userId, $class = null) public function getHtmlRemoveStarterForm($starterId)
{
$html = self::getHtmlRemoveStarterFromEventForm($this->id, $starterId);
}
// returns html code for a button to remove the user from an event
public static function getHtmlRemoveStarterFromEventForm($eventId, $userId, $class = null)
{ {
$form = $form =
'<form ' . (isset($class) ? ('class="' . $class . '"') : '') . ' action="api.starter.remove.php" method="post">' '<form ' . (isset($class) ? ('class="' . $class . '"') : '') . ' action="api.starter.remove.php" method="post">'

View File

@@ -23,6 +23,15 @@ class participo
return (isset($_SESSION) && array_key_exists('login', $_SESSION) && $_SESSION['login'] == true); return (isset($_SESSION) && array_key_exists('login', $_SESSION) && $_SESSION['login'] == true);
} }
public static function getSessionUserId()
{
$userId = null;
if (isset($_SESSION) && array_key_exists('user', $_SESSION) && array_key_exists('userId', $_SESSION['user'])) {
$userId = $_SESSION['user']['userId'];
}
return $userId;
}
/** Remove all login data from the session data /** Remove all login data from the session data
* *
* @return void * @return void
@@ -541,3 +550,17 @@ function getHtmlSquareDate($date = null)
. '<div style="font-size: small">' . $year . '</div>' . '<div style="font-size: small">' . $year . '</div>'
. '</div>'; . '</div>';
} }
/**
* filter_var for a (db)id
*
* check for valid id; null is default; only values > 0 are excepted
*
* @param [type] $id
* @retval int>0 filtered id
* @retval null sth. went wrong
*/
function filterId($id)
{
return filter_var($id, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1]]);
}

View File

@@ -18,9 +18,10 @@ class eventPlaner
'wardship' => 'vormundschaft' 'wardship' => 'vormundschaft'
]; ];
// request coming starts from the db
private static function getComingStarts($sinceDate = null, $userId = null) private static function getComingStarts($sinceDate = null, $userId = null)
{ {
$userId = $userId ?? $_SESSION['user']['userId']; $userId = $userId ?? participo::getSessionUserId();
$sinceDate = $sinceDate ?? new DateTime(); $sinceDate = $sinceDate ?? new DateTime();
$params = [ $params = [
@@ -91,7 +92,7 @@ class eventPlaner
$ret = dbConnector::query($query); $ret = dbConnector::query($query);
$events = []; $events = [];
foreach ($ret as $event) { foreach ($ret as $event) {
array_push($events, event::fromArray($event)); array_push($events, event::fromDbArray($event));
} }
return $events; return $events;
} }
@@ -108,39 +109,4 @@ class eventPlaner
} }
return $ret; return $ret;
} }
// inserting html code
/** Generate the htmlCode for the list of upcoming starts of
*
* @return void
*/
public static function htmlComingStarts()
{
$comingStarts = self::getComingStarts();
$htmlTable = null;
if ($comingStarts) {
$htmlTable = '<table>'
. '<thead><tr><th>Datum</th><th>Veranstaltung</th><th>Starter</th><th></th></tr></thead>'
. '<tbody>';
foreach ($comingStarts as $s) {
$eventDeadline = DateTime::createFromFormat('Y-m-d', $s['deadline']);
$eventDate = DateTime::createFromFormat('Y-m-d', $s['eventDate']);
$today = new DateTime();
$htmlTable .= '<tr>'
. '<td>' . getHtmlSquareDate($eventDate) . '</td>'
. '<td>' . $s['eventName'] . '</td>'
. '<td>' . $s['userName'] . ', ' . $s['userFirstname'] . '</td>'
. '<td>' . ($eventDeadline >= $today ? Event::getHtmlRemoveStarterForm($s['eventId'], $s['userId']) : '') . '</td>'
// . '<td>' . $eventDeadline->format('Y-m-d') . ' <= ' . $today->format('Y-m-d') . ': ' . ($eventDeadline <= $today ? 'true' : 'false') . '</td>'
. '</tr>';
}
$htmlTable .= '</tbody></table>';
echo('<h2 id="comingStarts">Aktuelle Einschreibungen</h2>');
echo($htmlTable);
} else {
echo('<div>Keine Meldungen zu bevorstehenden Events<div>');
}
return;
}
} }

View File

@@ -57,7 +57,20 @@ class Shiai
return ($this->place != null ? $this->place : '-'); return ($this->place != null ? $this->place : '-');
} }
public static function fromArray($member) public static function loadFromDb(int $id)
{
$query = 'SELECT * FROM `cwsvjudo`.`wettkampfkalender` WHERE `lfdeNr` = :id;';
$params = [':id' => ['value' => $id, 'data_type' => PDO::PARAM_INT]];
$response = dbConnector::query($query, $params);
// ids are considered unique. so every other count then 1 is treated as error to prevent unprivileged access
if (count($response) != 1) {
return null;
}
return self::fromDbArray($response[0]);
}
public static function fromDbArray($member)
{ {
return new shiai( return new shiai(
$member['lfdeNr'] ?? null, $member['lfdeNr'] ?? null,

View File

@@ -29,31 +29,6 @@ abstract class StartingType
/** Frame for a start to a shiai */ /** Frame for a start to a shiai */
class Starter class Starter
{ {
private $id = null; //< id of the event in the database
private $eventId = null; //< dbId of the event one is starting
private $typeId = null; //< type(id) of the starter
private $userId = null; //< id of the starting user
private $rideId = null; //< id of the ride where the starter can car pool
private $mass = null; //< mass in kg on the scale
private $result = null; //< result of the start (array of places if multi start)
private $shiai = null; //< to store the shiai if needed
private $user = null;
/** columns in the table (in the database) with their type
*
* @var array
*/
private static $dbColumns = [
'id' => PDO::PARAM_INT,
'eventId' => PDO::PARAM_INT,
'type' => PDO::PARAM_INT,
'userId' => PDO::PARAM_INT,
'fahrtId' => PDO::PARAM_INT,
'masse' => PDO::PARAM_STR,
'platz' => PDO::PARAM_STR,
];
/** Constructor /** Constructor
* *
* @todo Document * @todo Document
@@ -80,101 +55,64 @@ class Starter
} }
} }
/** Create a Starter from an assoziative array like it is returned from db requests ////
// dbInterface
///
/** Load a Start from the db via an id
* *
* @param array $member associative array with the UserData from the dbRequest * @param int $startId
* @param $columnMappings renaming of columnNames, e.g., if the id isn't under id in the array, add 'id'=>'starterId' to the mappings * @return loaded start or null (if sth. wrong)
* @return User initialized user
*/ */
public static function fromDbArray($member, $columnMappings = []) public static function loadFromDb($startId)
{ {
// if it isn't remapped, take default column name $startId = filterId($startId);
foreach (self::$dbColumns as $columnName => $columnDataType) { if (!isset($startId)) {
if (!array_key_exists($columnName, $columnMappings)) { return null;
$columnMappings[$columnName] = $columnName; }
return self::loadFromDbBy('id', $startId);
}
/** Retrieve the linked Event from the database
*
* @param bool $force By default, if there already is an event in the buffer it is reused. Set this parameter to true to enforce the loading.
* @return Event the event in the db identified via the eventId
*/
public function loadEvent(bool $force = false)
{
if ($force || (!isset($this->event))) {
if (isset($this->eventId)) {
$this->event = Event::loadFromDb($this->eventId);
} }
} }
return $this->event;
return new Starter(
$member[$columnMappings['id']] ?? null,
$member[$columnMappings['eventId']] ?? null,
$member[$columnMappings['type']] ?? null,
$member[$columnMappings['userId']] ?? null,
$member[$columnMappings['fahrtId']] ?? null,
$member[$columnMappings['masse']] ?? null,
$member[$columnMappings['platz']] ?? null
);
} }
/** Load a starter from the db by a column /** Retrieve the linked User from the database
* *
* @param [string] $name name of the column * @param bool $force By default, if there already is an user in the buffer it is reused. Set this parameter to true to enforce the loading.
* @param [mixed] $value value to look for * @return User the user in the db identified via the userId
* @param [bool] $unique if the value is unique (true->return single value) or not (false->return array)
* @return loaded user or null (if sth. wrong)
*/ */
public static function loadFromDbBy($name, $value) public function loadStarter(bool $force = false)
{ {
if (!array_key_exists($name, self::$dbColumns)) { if ($force || (!isset($this->user))) {
return null; if (isset($this->userId)) {
$this->user = User::loadFromDb($this->userId);
}
} }
return $this->user;
$response = dbConnector::query(
'SELECT * FROM `wkParticipo_Starter` WHERE `' . $name . '` = :' . $name,
[$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]]
);
if (count($response) != 1) {
return null;
}
return self::fromDbArray($response[0]);
} }
/** Load a Starter from the db via an id /** Add the shiai to the database
* *
* @param int $starterId * - before adding to the db it is checked wether the currently logged in user has sufficient rights
* @return loaded starter or null (if sth. wrong) * - all starts of the same user to the same event get deleted before adding the start (again)
*
* @todo an admin should also be allowed
* @todo the deadline of the event should be checked as well
*
* @return int id under which the
*/ */
public static function loadFromDb($starterId)
{
return self::loadFromDbBy('id', $starterId);
}
/** Add a Start to the db
*
* - backend function no input validation/sanitation is done
*
* @param [int] $eventId eventId for the start
* @param [int] $typeId type(Id) for the start
* @param [int] $userId id of the starting user
* @return [int] lastInserted id of the Start
*/
private static function dbInsert($eventId, $typeId, $userId)
{
$query = 'INSERT INTO `cwsvjudo`.`wkParticipo_Starter` (eventId, type, userId) values (:eventId, :typeId, :userId);';
$params = [
':eventId' => ['value' => $eventId, 'data_type' => PDO::PARAM_INT],
':typeId' => ['value' => $typeId, 'data_type' => PDO::PARAM_INT],
':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]
];
$response = dbConnector::query($query, $params);
return dbConnector::getLastInsertId();
}
private static function dbDelete($eventId, $userId)
{
$query = 'DELETE FROM `cwsvjudo`.`wkParticipo_Starter` WHERE eventId = :eventId AND userId = :userId;';
$params = [
':eventId' => ['value' => $eventId, 'data_type' => PDO::PARAM_INT],
':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]
];
$response = dbConnector::query($query, $params);
return;
}
public function addToDb() public function addToDb()
{ {
// - if the id is already set it *has* to be already in the DB hence we don't add it // - if the id is already set it *has* to be already in the DB hence we don't add it
@@ -182,37 +120,33 @@ class Starter
if (isset($this->id) && !participo::isWardOf($this->userId)) { if (isset($this->id) && !participo::isWardOf($this->userId)) {
return null; return null;
} }
// You can only start once to an event so delete *all* other starts of this user to this event // You can only start once to an event so delete *all* other starts of this user to this event
self::dbDelete($this->eventId, $this->userId); self::dbDelete($this->eventId, $this->userId);
$this->id = self::dbInsert($this->eventId, $this->typeId, $this->userId); $this->id = self::dbInsert($this->eventId, $this->typeId, $this->userId);
return $this->id; return $this->id;
} }
/** Remove the start from the db
*
* - check if the currently logged in user is allowed to remove the start first
*
* @todo an admin should also be allowed
* @todo the deadline of the event should be checked as well
*
* @return void
*/
public function removeFromDb() public function removeFromDb()
{ {
// - the logged in user must have wardship over the starter // - the logged in user must have wardship over the starter
if (!participo::isWardOf($this->userId)) { if (!participo::isWardOf($this->userId)) {
return null; return;
} }
self::dbDelete($this->eventId, $this->userId); self::dbDelete($this->eventId, $this->userId);
return; return;
} }
// create starter from assoc array
public static function create($parameter)
{
$id = $parameter['id'] ?? null; //< id of the event in the database
$eventId = $parameter['eventId'] ?? null; //< dbId of the event one is starting
$typeId = $parameter['typeId'] ?? null; //< type(id) of the starter
$userId = $parameter['userId'] ?? null; //< id of the starting user
$rideId = $parameter['rideId'] ?? null; //< id of the ride where the starter can car pool
$mass = $parameter['mass'] ?? null; //< mass in kg on the scale
$result = $parameter['result'] ?? null; //< result of the start (array of places if multi start)
return new Starter($id, $eventId, $typeId, $userId, $rideId, $mass, $result);
}
// Getter for the member // Getter for the member
public function getId() public function getId()
{ {
@@ -253,4 +187,300 @@ class Starter
{ {
return User::loadFromDb($this->userId); return User::loadFromDb($this->userId);
} }
public function getEvent()
{
return $this->event;
}
public function getHtmlFormRemove($class = null)
{
$today = new DateTime();
$event = $this->loadEvent();
$eventDeadline = $event->getDeadline();
if ($today > $eventDeadline) {
return self::getHtmlModalToLate($this->id);
}
return self::getHtmlFormRemoveStarterFromEvent($this->id, $this->eventId, $class);
}
// inject html code of table with coming starts for the session user
public static function htmlTableComingStarts()
{
$userId = participo::getSessionUserId();
echo(self::getHtmlTableComingStarts($userId));
}
// Member
// - static member
/** Name of the table with all the starts
*
* @var string
*/
private static $tableName = 'wkParticipo_Starter';
/** Names of the columns in the table (in the database) with their type
*
* @var array
*/
private static $dbColumns = [
'id' => PDO::PARAM_INT,
'eventId' => PDO::PARAM_INT,
'type' => PDO::PARAM_INT,
'userId' => PDO::PARAM_INT,
'fahrtId' => PDO::PARAM_INT,
'masse' => PDO::PARAM_STR,
'platz' => PDO::PARAM_STR,
];
// - non-static member
// data that's stored in the start table
/** unique database id for the start
*
* @var int/null
*/
private $id = null;
/** unique database id of the event one is starting
*
* @var int/null
*/
private $eventId = null;
/** type(id) of the starter
*
* @var int/null
*/
private $typeId = null;
/** unique database id of the starting user
*
* @var int/null
*/
private $userId = null;
/** unique database id of the ride where the starter can car pool
*
* @var int/null
*/
private $rideId = null;
/** the mass(-class) the starter had at the event, comma separated list if multiple
*
* @var string
*/
private $mass = null;
/** place result of the starter at the event, comma separated list if multiple
*
* @var string
*/
private $result = null;
/** Create a Starter from an assoziative array like it is returned from db requests
*
* @param array $member associative array with the UserData from the dbRequest
* @param $columnMappings renaming of columnNames, e.g., if the id isn't under id in the array, add 'id'=>'starterId' to the mappings
* @return User initialized user
*/
private static function fromDbArray($member, $columnMappings = [])
{
// if it isn't remapped, take default column name
foreach (self::$dbColumns as $columnName => $columnDataType) {
if (!array_key_exists($columnName, $columnMappings)) {
$columnMappings[$columnName] = $columnName;
}
}
return new Starter(
$member[$columnMappings['id']] ?? null,
$member[$columnMappings['eventId']] ?? null,
$member[$columnMappings['type']] ?? null,
$member[$columnMappings['userId']] ?? null,
$member[$columnMappings['fahrtId']] ?? null,
$member[$columnMappings['masse']] ?? null,
$member[$columnMappings['platz']] ?? null
);
}
/** Load a starter from the db by a column
*
* @param [string] $name name of the column
* @param [mixed] $value value to look for
* @param [bool] $unique if the value is unique (true->return single value) or not (false->return array)
* @return loaded user or null (if sth. wrong)
*/
private static function loadFromDbBy($name, $value)
{
if (!array_key_exists($name, self::$dbColumns)) {
return null;
}
$response = dbConnector::query(
'SELECT * FROM `wkParticipo_Starter` WHERE `' . $name . '` = :' . $name,
[$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]]
);
if (count($response) != 1) {
return null;
}
return self::fromDbArray($response[0]);
}
/** Add a Start to the db
*
* - backend function no input validation/sanitation is done
*
* @param [int] $eventId eventId for the start
* @param [int] $typeId type(Id) for the start
* @param [int] $userId id of the starting user
* @return [int] lastInserted id of the Start
*/
private static function dbInsert($eventId, $typeId, $userId)
{
$query = 'INSERT INTO `cwsvjudo`.`wkParticipo_Starter` (eventId, type, userId) values (:eventId, :typeId, :userId);';
$params = [
':eventId' => ['value' => $eventId, 'data_type' => PDO::PARAM_INT],
':typeId' => ['value' => $typeId, 'data_type' => PDO::PARAM_INT],
':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]
];
$response = dbConnector::query($query, $params);
return dbConnector::getLastInsertId();
}
private static function dbDelete($eventId, $userId)
{
$query = 'DELETE FROM `cwsvjudo`.`wkParticipo_Starter` WHERE eventId = :eventId AND userId = :userId;';
$params = [
':eventId' => ['value' => $eventId, 'data_type' => PDO::PARAM_INT],
':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]
];
$response = dbConnector::query($query, $params);
return;
}
// request coming starts of a user from the db
private static function getComingStarts($userId = null, $sinceDate = null)
{
// if no user is given, take it from the session data
$userId = $userId ?? participo::getSessionUserId();
// If no data is given, we take today
if (!$sinceDate) {
$sinceDate = 'CURDATE()';
} else {
$sinceDate = 'DATE("' . $sinceDate . '")';
}
// Query all interesting starts
$query =
'SELECT '
. '`wkParticipo_Starter`.`id` as startId, '
. '`wkParticipo_Starter`.`userId` as starterId, '
. '`wkParticipo_Starter`.`eventId` as eventId, '
. '`wkParticipo_Events`.`wkId` as shiaiId '
. 'FROM `wkParticipo_Starter` '
// link to the event (to get the date)
. 'LEFT JOIN `wkParticipo_Events` ON `wkParticipo_Starter`.`eventId` = `wkParticipo_Events`.`id` '
// link to all wardships for the starter
. 'LEFT JOIN `vormundschaft` ON `wkParticipo_Starter`.`userId` = `vormundschaft`.`kidId` '
// link to the shiai of the event
. 'LEFT JOIN `wettkampfkalender` on `wettkampfkalender`.`lfdeNr` = `wkParticipo_Events`.`wkId` '
. 'WHERE '
// only events after
. '`wkParticipo_Events`.`date` >= ' . $sinceDate . ' AND '
// only if the current user is warden of the starte
. '`vormundschaft`.`userId` = :userId '
. 'ORDER BY `wkParticipo_Events`.`date` ASC;';
$params = [':userId' => ['value' => $userId, 'data_type' => PDO::PARAM_INT]];
$response = dbConnector::query($query, $params);
$comingStarts = [];
foreach ($response as $r) {
$start = Starter::loadFromDb($r['startId']);
$comingStarts[] = $start;
}
return $comingStarts;
}
// get html code of a list of starts
private static function getHtmlTable($starts)
{
$html = '<table>'
. '<thead><tr><th>Datum</th><th>Veranstaltung</th><th>Starter</th><th></th></tr></thead>'
. '<tbody>';
foreach ($starts as $start) {
$today = new DateTime();
$startingUser = User::loadFromDb($start->getUserId());
$event = Event::loadFromDb($start->getEventId());
$shiai = Shiai::loadFromDb($event->getShiaiId());
$eventDeadline = $event->getDeadline();
$eventDate = $event->getDate();
$html .= '<tr>'
. '<td>' . getHtmlSquareDate($eventDate) . '</td>'
. '<td>' . $shiai->getHtmlName() . '</td>'
. '<td>' . $startingUser->getName() . ', ' . $startingUser->getFirstName() . '</td>'
. '<td>' . $start->getHtmlFormRemove() . '</td>'
. '</tr>';
}
$html .= '</tbody></table>';
return $html;
}
private static function getHtmlModalToLate($startId, $caption = 'Austragen')
{
$modal = $html = '<a class="btn grey waves-effect waves-light modal-trigger" href="#modal-remove-starter-' . $startId . '">' . $caption . '</a>'
. '<div id="modal-remove-starter-' . $startId . '" class="modal">'
. '<div class="modal-content">'
. '<p>Das Fenster zum Ein- und Austragen ist bereits geschlossen.</p>'
. '</div>'
. '<div class="modal-footer">'
. '<a href="#!" class="modal-close waves-effect waves-green btn-flat">OK</a>'
. '</div>'
. '</div>';
return $modal;
}
private static function getHtmlFormRemoveStarterFromEvent($starterId, $eventId, $class = null)
{
$form =
'<form ' . (isset($class) ? ('class="' . $class . '"') : '') . ' action="api.starter.remove.php" method="post">'
. '<input type="hidden" name="returnToUrl" id="returnToUrl" value="' . urlencode(getCurPagesUrl()) . '" >'
. '<input type="hidden" name="eventId" id="eventId" value="' . $eventId . '">'
. '<input type="hidden" name="userId" id="userId" value="' . $starterId . '">'
. '<button class="btn red" type="submit" name="submit">austragen</button>'
. '</form>';
return $form;
}
/** Generate the htmlCode for the list of upcoming starts of wards of the current user
*
* @return void
*/
private static function getHtmlTableComingStarts($userId = null)
{
$userId = $userId ?? participo::getSessionUserId();
$starts = self::getComingStarts($userId);
if (!(count($starts) > 0)) {
return '<div>Keine Meldungen zu bevorstehenden Events<div>';
}
return self::getHtmlTable($starts);
}
// buffers to reduce db access
/** Event of the start
*
* @var Event/null
*/
private $event = null;
/** Starting user of the start
*
* @var User/null
*/
private $user = null;
} }