copy only events with assigned trainers in timetable copy-week

This commit is contained in:
Roland Schneider 2021-10-10 10:08:31 +02:00
parent 5e06348996
commit 5fe59ff1d3

View File

@ -2,8 +2,11 @@
namespace common\modules\event\manager; namespace common\modules\event\manager;
use common\components\RoleDefinition;
use common\models\Event; use common\models\Event;
use common\models\EventRegistration; use common\models\EventRegistration;
use common\models\Trainer;
use common\models\User;
use common\modules\event\models\timetable\TimeTableMonth; use common\modules\event\models\timetable\TimeTableMonth;
use common\modules\event\models\timetable\TimeTableMonthDay; use common\modules\event\models\timetable\TimeTableMonthDay;
use common\modules\event\models\timetable\TimeTableMonthWeek; use common\modules\event\models\timetable\TimeTableMonthWeek;
@ -88,7 +91,12 @@ class EventManager
} }
// get events between active dates // get events between active dates
$events = $this->getEvents($dateTimeFrom, $dateTimeTo); $trainers = null;
if ( RoleDefinition::isTrainer() ){
$trainers = $this->getAssignedTrainerIDs(\Yii::$app->user->id);
}
$events = $this->getEvents($dateTimeFrom, $dateTimeTo,$trainers);
// set events per day // set events per day
/** @var Event $event */ /** @var Event $event */
@ -113,9 +121,12 @@ class EventManager
* Get all active events between the to dates * Get all active events between the to dates
* @param DateTime $fromInc from date inclusive (>=) * @param DateTime $fromInc from date inclusive (>=)
* @param DateTime $toExcl to date exclusive (<) * @param DateTime $toExcl to date exclusive (<)
* @param int[] trainers if set, load only events assigend to the given trainers
* if the array is set, but emtpy,
* then no sql will be executed, empty array will be returned
* @return Event[] * @return Event[]
*/ */
public function getEvents($fromInc, $toExcl) public function getEvents($fromInc, $toExcl,$trainers = null)
{ {
$query = Event::find(); $query = Event::find();
@ -129,6 +140,12 @@ class EventManager
->orderBy([ ->orderBy([
'event.start' => SORT_ASC 'event.start' => SORT_ASC
]); ]);
if ( isset($trainers)){
if ( count($trainers) == 0 ){
return [];
}
$query->andWhere(['in','id_trainer',$trainers]);
}
$query = $this->withEventConditions($query); $query = $this->withEventConditions($query);
return $query return $query
->all(); ->all();
@ -210,4 +227,23 @@ class EventManager
->andWhere(['event.active' => '1'])->all(); ->andWhere(['event.active' => '1'])->all();
} }
public function getAssignedTrainers($idUser){
return Trainer::find()
->innerJoinWith('userTrainerAssignments')
->andWhere(['user_trainer_assignment.id_user' => $idUser])
->all();
}
public function getAssignedTrainerIDs($idUser){
$trainerObjects = $this->getAssignedTrainers($idUser);
$trainers = [];
foreach ($trainerObjects as $trainerObject){
$trainers[] = $trainerObject->id;
}
return $trainers;
}
} }