public function actionSchedule($id)
 {
     $team = $this->findModel($id);
     $event = $team->event;
     $start = new MDateTime($event->start, new \DateTimeZone('EST5EDT'));
     $start->subToStart('D');
     $days = [];
     while ($start->timestamp < $event->end) {
         $days[$start->timestamp] = $team->getDayDataProvider($start->timestamp);
         $start->add(new \DateInterval('P1D'));
     }
     return $this->render('schedule', ['event' => $event, 'team' => $team, 'days' => $days]);
 }
 public static function findUserEventDataByDay($event_id, $user_id)
 {
     $participants = self::find()->where(['user_id' => $user_id])->andWhere(['team.event_id' => $event_id])->joinWith(['shift', 'shift.team'])->all();
     $data = [];
     foreach ($participants as $participant) {
         $shift = $participant->shift;
         $team = $shift->team;
         $start_day = new MDateTime($shift->start_time);
         $start_day->subToStart('D');
         if (!isset($data[$start_day->timestamp])) {
             $data[$start_day->timestamp] = [];
         }
         $data[$start_day->timestamp][] = $participant;
     }
     foreach ($data as $timestamp => $day_data) {
         uasort($data[$timestamp], function ($a, $b) {
             return $a->shift->start_time > $b->shift->start_time;
         });
     }
     return $data;
 }
 /**
  * Displays a single Team model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $model = $this->findModel($id);
     $event = $model->event;
     $start = new MDateTime($event->start, new \DateTimeZone('EST5EDT'));
     $start->subToStart('D');
     $days = [];
     while ($start->timestamp < $event->end) {
         $days[$start->timestamp] = $model->getDayDataProvider($start->timestamp);
         $start->add(new \DateInterval('P1D'));
     }
     $dp = new ActiveDataProvider(['query' => Shift::find()->where(['team_id' => $id]), 'pagination' => false]);
     $shift = new Shift();
     $shift->team_id = $model->id;
     $shift->active = true;
     if ($shift->load(Yii::$app->request->post())) {
         $shift->save();
     }
     $requirements = Requirement::find()->orderBy('name ASC')->all();
     $event = $model->event;
     return $this->render('view', ['model' => $model, 'shift' => $shift, 'event' => $event, 'dataProvider' => $dp, 'days' => $days, 'requirements' => $requirements]);
 }
 public function generateReport()
 {
     $output = [];
     $participants = Participant::find()->joinWith(['shift', 'shift.team'])->where(['team.event_id' => $this->id])->orderBy(['participant.timestamp' => SORT_ASC])->all();
     $date_containers = [];
     $date_totals = [];
     $user_totals = [];
     $multi_shift_totals = [];
     $team_names = ["Total Event" => 0];
     $max_needed = ["Total Event" => $this->maxTotalShifts];
     //Get list of teams, plus event total, and their max total shifts
     foreach ($this->teams as $team) {
         $team_names[$team->name] = 0;
         $max_needed[$team->name] = $team->maxTotalShifts;
     }
     //Loop through every participant signup, group by date (week)
     foreach ($participants as $participant) {
         $dt = new MDateTime($participant->timestamp);
         $dt->subToStart('W');
         $timestamp = $dt->timestamp;
         $team_name = $participant->shift->team->name;
         if (!isset($date_containers[$timestamp])) {
             $date_containers[$timestamp] = $team_names;
             $date_totals[$timestamp] = 0;
             $user_totals[$timestamp] = 0;
         }
         if (!isset($date_containers[$timestamp][$team_name])) {
             $date_containers[$timestamp][$team_name] = 0;
         }
         $date_containers[$timestamp][$team_name]++;
         $date_containers[$timestamp]["Total Event"]++;
         $date_totals[$timestamp]++;
     }
     //Add missing dates to $date_containers
     $first_date = new MDateTime(array_keys($date_containers)[0]);
     $last_date = new MDateTime(array_pop(array_keys($date_containers)));
     $last_date->add(new DateInterval('P1W'));
     $period = new DatePeriod($first_date, new DateInterval('P1W'), $last_date);
     foreach ($period as $dt) {
         if (!isset($date_containers[$dt->getTimestamp()])) {
             $date_containers[$dt->getTimestamp()] = $team_names;
             $date_totals[$dt->getTimestamp()] = 0;
             $user_totals[$dt->getTimestamp()] = 0;
         }
         //Calculate unique user totals from the start to each different time period
         $unique_ids = [];
         foreach ($participants as $participant) {
             $end_of_week = new MDateTime($dt->getTimestamp());
             $end_of_week->addToEnd('W');
             if ($participant->timestamp <= $end_of_week->timestamp) {
             }
             $unique_ids[$participant->user_id] = 1;
         }
         $user_totals[$dt->getTimestamp()] = count($unique_ids);
     }
     ksort($date_containers);
     ksort($date_totals);
     ksort($user_totals);
     //Add previous dates totals to the next date past that one
     //to show progression over time
     $current_vals = [];
     foreach ($date_containers as $timestamp => $data) {
         foreach ($data as $team_name => $total) {
             if (isset($current_vals[$team_name])) {
                 $date_containers[$timestamp][$team_name] += $current_vals[$team_name];
             }
         }
         $current_vals = $date_containers[$timestamp];
     }
     $headers = ['Team'];
     foreach ($period as $date) {
         $headers[] = $date->format('n/j');
     }
     $raw_output = [];
     $percent_output = [];
     foreach ($team_names as $name => $v) {
         $team_raw_output = [$name];
         $team_percent_output = [$name];
         foreach ($period as $date) {
             $team_raw_output[] = $date_containers[$date->getTimestamp()][$name];
             $team_percent_output[] = sprintf("%.1f%%", $date_containers[$date->getTimestamp()][$name] / $max_needed[$name] * 100);
         }
         $raw_output[] = $team_raw_output;
         $percent_output[] = $team_percent_output;
     }
     //Total shifts per participant
     foreach ($participants as $participant) {
         if (!isset($multi_shift_totals[$participant->user_id])) {
             $multi_shift_totals[$participant->user_id] = 0;
         }
         $multi_shift_totals[$participant->user_id]++;
     }
     $multi_shift_breakdown = [];
     foreach ($multi_shift_totals as $p_total) {
         $label = $p_total == 1 ? "1 Shift" : "{$p_total} Shifts";
         if (!isset($multi_shift_breakdown[$p_total])) {
             $multi_shift_breakdown[$p_total] = [$label, 0];
         }
         $multi_shift_breakdown[$p_total][1]++;
     }
     ksort($multi_shift_breakdown);
     //Sort by team name
     $sort_func = function ($a, $b) {
         if ($a[0] === "Total Event") {
             return -1;
         }
         if ($b[0] === "Total Event") {
             return 1;
         }
         return $a[0] > $b[0];
     };
     usort($raw_output, $sort_func);
     usort($percent_output, $sort_func);
     $output[] = $headers;
     $output[] = ["Percent Totals"];
     $output = array_merge($output, $percent_output);
     $output[] = [""];
     $output[] = ["Raw Totals"];
     $output = array_merge($output, $raw_output);
     $output[] = [""];
     $output[] = ["Weekly Total"];
     $output[] = [""] + $date_totals;
     $output[] = [""];
     $output[] = ["Total Unique Users"];
     $output[] = [""] + $user_totals;
     $output[] = [""];
     $output[] = ["Number of Shifts per Volunteer"];
     $output = array_merge($output, $multi_shift_breakdown);
     return $output;
 }