make add/remove starter buttons date sensitive
This commit is contained in:
@@ -29,31 +29,6 @@ abstract class StartingType
|
||||
/** Frame for a start to a shiai */
|
||||
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
|
||||
*
|
||||
* @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 $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
|
||||
* @param int $startId
|
||||
* @return loaded start or null (if sth. wrong)
|
||||
*/
|
||||
public static function fromDbArray($member, $columnMappings = [])
|
||||
public static function loadFromDb($startId)
|
||||
{
|
||||
// if it isn't remapped, take default column name
|
||||
foreach (self::$dbColumns as $columnName => $columnDataType) {
|
||||
if (!array_key_exists($columnName, $columnMappings)) {
|
||||
$columnMappings[$columnName] = $columnName;
|
||||
$startId = filterId($startId);
|
||||
if (!isset($startId)) {
|
||||
return null;
|
||||
}
|
||||
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 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
|
||||
);
|
||||
return $this->event;
|
||||
}
|
||||
|
||||
/** Load a starter from the db by a column
|
||||
/** Retrieve the linked User from the database
|
||||
*
|
||||
* @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)
|
||||
* @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.
|
||||
* @return User the user in the db identified via the userId
|
||||
*/
|
||||
public static function loadFromDbBy($name, $value)
|
||||
public function loadStarter(bool $force = false)
|
||||
{
|
||||
if (!array_key_exists($name, self::$dbColumns)) {
|
||||
return null;
|
||||
if ($force || (!isset($this->user))) {
|
||||
if (isset($this->userId)) {
|
||||
$this->user = User::loadFromDb($this->userId);
|
||||
}
|
||||
}
|
||||
|
||||
$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]);
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/** Load a Starter from the db via an id
|
||||
/** Add the shiai to the database
|
||||
*
|
||||
* @param int $starterId
|
||||
* @return loaded starter or null (if sth. wrong)
|
||||
* - before adding to the db it is checked wether the currently logged in user has sufficient rights
|
||||
* - 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()
|
||||
{
|
||||
// - 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)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 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);
|
||||
$this->id = self::dbInsert($this->eventId, $this->typeId, $this->userId);
|
||||
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()
|
||||
{
|
||||
// - the logged in user must have wardship over the starter
|
||||
if (!participo::isWardOf($this->userId)) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
self::dbDelete($this->eventId, $this->userId);
|
||||
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
|
||||
public function getId()
|
||||
{
|
||||
@@ -253,4 +187,300 @@ class Starter
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user