This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/models/Training.model.php
2023-12-11 19:54:37 +01:00

123 lines
3.5 KiB
PHP

<?php
class Training extends Model
{
protected $dbTable = "trainings";
protected $dbFields = array(
'registered' => ['type' => 'datetime', 'required', 'unique', 'autoValMethod' => 'getDateTime'],
'name' => ['type' => 'text', 'required'],
'date' => ['type' => 'datetime','required'],
'duration' => ['type' => 'int'], //in days
'text' => ['type' => 'text'],
'url' => ['type' => 'text'], //if there is one
'logo' => ['type' => 'text', 'default' => 'https://pictshare.net/prrnrk.jpg'],
'admins' => ['type'=> 'array', 'default'=>[]],
'members' => ['type'=> 'array', 'default'=>[]],
'runs' => ['type'=> 'array', 'default'=>[]],
);
function joinUser($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
if (!in_array($_SESSION['user']->id, $this->data['members']))
$this->data['members'][] = $_SESSION['user']->id;
if(!in_array($training,$_SESSION['user']->data['trainings']))
{
$_SESSION['user']->data['trainings'][] = $training;
$_SESSION['user']->save();
}
$this->save();
}
}
function removeUser($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
if (in_array($_SESSION['user']->id, $this->data['members']))
$this->data['members'] = array_diff($this->data['members'],[$_SESSION['user']->id]);
if(in_array($training,$_SESSION['user']->data['trainings']))
{
$_SESSION['user']->data['trainings'] = array_diff($_SESSION['user']->data['trainings'],[$training]);
$_SESSION['user']->save();
}
$this->save();
}
}
function isMyEvent($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
if (in_array($training, $_SESSION['user']->data['trainings']))
return true;
else return false;
}
}
function amIAdmin($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
if (in_array($_SESSION['user']->id, $this->data['admins']))
return true;
else return false;
}
}
function getAdmins($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
$admins = [];
foreach($this->data['admins'] as $admin)
{
$u = new User();
$u->load($admin);
$admins[] = $u->getDataFiltered();
}
return $admins;
}
}
function getMembers($training = false)
{
if ($training == false)
$training = $this->id;
if (!$this->exists($training))
return false;
else {
$members = [];
foreach($this->data['members'] as $member)
{
$u = new User();
$u->load($member);
$members[] = $u->getDataFiltered();
}
return $members;
}
}
}