Files
cwsvJudo/homepage/participo/lib/participoLib/starter.php

257 lines
7.7 KiB
PHP

<?php
/** Types of starting */
abstract class StartingType
{
public const __default = null;
public const Fighter = 1;
public const Audience = 2;
public const NoParticipation = 3;
/** convert a variable into a StartingType
*
* @param [int] $type starting type candidate
* @return int representation of the StartingType if successful converted, otherwise null
*/
public static function toStartingType($type)
{
return filter_var($type, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1, 'max_range' => 3]]);
}
/** string representations of the starting type
*
* @var array array of StartingType => its string representation
*/
public static $AsString = [1 => 'Kämpfer', 2 => 'Zuschauer', 3 => 'keine Teilnahme'];
}
/** 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
*/
public function __construct($id, $eventId, $typeId, $userId, $rideId = null, $mass = null, $result = null)
{
$this->id = filter_var($id, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1]]);
$this->eventId = filter_var($eventId, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1]]);
$this->typeId = StartingType::toStartingType($typeId);
$this->userId = filter_var($userId, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1]]);
$this->rideId = filter_var($rideId, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 1]]);
$this->mass = filter_var($mass, FILTER_VALIDATE_FLOAT, ['options' => ['default' => null, 'min_range' => 0.0]]);
if (is_iterable($result)) {
$this->result = [];
foreach ($result as $r) {
$r = filter_var($r, FILTER_VAR_INT, ['options' => ['default' => null, 'min_range' => 0]]);
if ($r) {
array_push($this->result, $r);
}
}
} else {
$this->$result = filter_var($r, FILTER_VALIDATE_INT, ['options' => ['default' => null, 'min_range' => 0]]);
}
}
/** 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
*/
public 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)
*/
public 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]);
}
/** Load a Starter from the db via an id
*
* @param int $starterId
* @return loaded starter or null (if sth. wrong)
*/
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
// - the logged in user must have wardship over the 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;
}
public function removeFromDb()
{
// - the logged in user must have wardship over the starter
if (!participo::isWardOf($this->userId)) {
return null;
}
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()
{
return $this->id;
}
public function getEventId()
{
return $this->eventId;
}
public function getTypeId()
{
return $this->typeId;
}
public function getUserId()
{
return $this->userId;
}
public function getRideId()
{
return $this->rideId;
}
public function getMass()
{
return $this->mass;
}
public function getResult()
{
return $this->result;
}
public function getUser()
{
return User::loadFromDb($this->userId);
}
}