backend/group-training : improve delete&copy week

This commit is contained in:
Roland Schneider
2021-10-10 23:01:18 +02:00
parent 5fe59ff1d3
commit 50011a4e4f
14 changed files with 226 additions and 131 deletions

View File

@@ -19,29 +19,6 @@ use yii\db\StaleObjectException;
class EventManager
{
/** @noinspection PhpUnused */
/**
* Delete or mark deleted an event
* @param $id
* @throws Throwable
* @throws \yii\base\Exception on any error
* @throws StaleObjectException
*/
public function deleteEvent($id)
{
$event = Event::findOne($id);
if (!isset($event)) {
throw new \yii\base\Exception("Event $id not found");
}
$registrations = $event->eventRegistrations;
if (!isset($registrations) || count($registrations) === 0) {
$event->delete();
} else {
$event->deleted_at = time();
$event->save(false);
}
}
/**
@@ -91,10 +68,7 @@ class EventManager
}
// get events between active dates
$trainers = null;
if ( RoleDefinition::isTrainer() ){
$trainers = $this->getAssignedTrainerIDs(\Yii::$app->user->id);
}
$trainers = $this->getAssignedTrainerIDsForPermission();
$events = $this->getEvents($dateTimeFrom, $dateTimeTo,$trainers);
@@ -236,14 +210,62 @@ class EventManager
public function getAssignedTrainerIDs($idUser){
$trainerObjects = $this->getAssignedTrainers($idUser);
return $this->mapTrainerToId($trainerObjects);
}
$trainers = [];
foreach ($trainerObjects as $trainerObject){
$trainers[] = $trainerObject->id;
}
return $trainers;
public function getAllTrainerIDs( ){
$trainerObjects = Trainer::find()->all();
return $this->mapTrainerToId($trainerObjects);
}
public function mapTrainerToId($trainerObjects){
$trainers = [];
foreach ($trainerObjects as $trainerObject){
$trainers[] = $trainerObject->id;
}
return $trainers;
}
/**
* If the trainers are not limited by the 'trainer' role, null will be returned.
* If the user has the 'trainer' permission, but no trainer is assigned, empty array will be returned.
* Otherwise the array of assigned trainer IDs will be returned.
* @return array|null
*/
public function getAssignedTrainerIDsForPermission(){
if ( RoleDefinition::isTrainer()){
$trainers = $this->getAssignedTrainerIDs(\Yii::$app->user->id);
} else {
$trainers = $this->getAllTrainerIDs();
}
return $trainers;
}
/**
* @param $trainerIDs int[]|null the trainer IDs. Null means, all trainers are allowed
* @param $trainerId
* @return bool
*/
public function isTrainerAllowed($trainerIDs,$trainerId){
if ( !isset($trainerIDs) ){
return true;
}
return array_search($trainerId,$trainerIDs) !== false;
}
/**
* Use it with the result of getAssignedTrainerIDsForPermission().
* (
* @param $trainerIDs int[]|null the trainer IDs. Null means, all trainers are allowed
* @return bool
*/
public function hasAnyTrainerAllowed($trainerIDs ){
if ( !isset($trainerIDs)){
return true;
}
return count($trainerIDs) > 0;
}
}