示例#1
0
文件: TankForm.php 项目: ninetor/23
 /**
  * @return bool
  */
 public function addParticipant()
 {
     if ($this->validate()) {
         $participant = new Participant();
         $participant->setAttributes($this->getAttributes());
         if ($participant->save()) {
             return $participant->id;
         }
     }
     return $this->getErrors();
 }
 public function addUser()
 {
     if (!$this->validate()) {
         return false;
     }
     if (Shift::findOne($this->shift_id === null) || User::findOne($this->user_id == null)) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     $participant = new Participant();
     $participant->shift_id = $this->shift_id;
     $participant->user_id = $this->user_id;
     return $participant->save();
 }
示例#3
0
 /**
  * Finds the Participant model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Participant the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Participant::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionDelete($shift_id, $user_id)
 {
     if (!$this->isEventActive($shift_id)) {
         Yii::$app->session->addFlash("error", "Closed events cannot be modified.");
         return $this->goBack();
     }
     $model = Participant::findOne(['shift_id' => $shift_id, 'user_id' => $user_id])->delete();
     return $this->redirect(['/shift/view', 'id' => $shift_id]);
 }
 public function actionShifts($id = null)
 {
     if (!isset($id)) {
         $id = Yii::$app->params['currentEvent'];
     }
     $data = Participant::findUserEventDataByDay($id, Yii::$app->user->id);
     $events = Event::find()->where(['not', ['id' => $id]])->all();
     return $this->render('shifts', ['data' => $data, 'event' => Event::findOne($id), 'events' => $events]);
 }
 /**
  * Displays a single Shift model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $model = $this->findModel($id);
     $dp = new ActiveDataProvider(['query' => Participant::find()->where(['shift_id' => $id])]);
     $form = new AddParticipantForm();
     $form->shift_id = $id;
     if ($form->load(Yii::$app->request->post())) {
         $form->addUser();
     }
     return $this->render('view', ['model' => $model, 'form' => $form, 'dp' => $dp]);
 }
示例#7
0
 /**
  * @return array|bool|null|\yii\db\ActiveRecord
  */
 public function actionGetpostdata()
 {
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $id = intval(Yii::$app->request->post('id'));
         $participant = Participant::find()->select(['date', 'name', 'text'])->where(['id' => $id])->one();
         if ($participant) {
             $participant->date = Date::DateMonth($participant->date);
             return $participant;
         }
     }
     return false;
 }
示例#8
0
 public function init()
 {
     //		$presents = (new Present())->presents;
     $presents = (new Present())->getPresentsList();
     $activity = Activity::find()->orderBy('RAND()')->one();
     $query = Participant::find()->orderBy(['id' => SORT_DESC]);
     $countQuery = clone $query;
     $p_pages = new Pagination(['totalCount' => $countQuery->count()]);
     $p_pages->pageSize = 10;
     $models = $query->offset($p_pages->offset)->limit($p_pages->limit)->all();
     $winners = Participant::find()->where(['winner' => 1])->all();
     $this->getView()->params = ['participants' => $models, 'participants_pages' => $p_pages, 'presents' => $presents, 'activity' => $activity, 'winners' => $winners];
     parent::init();
     // TODO: Change the autogenerated stub
 }
 public function addParticipant($user_id)
 {
     if ($this->hasParticipant($user_id) == true) {
         Yii::$app->session->addFlash("error", "You are already signed up for this shift.");
         return false;
     }
     if (!$this->canBeFilled()) {
         Yii::$app->session->addFlash("error", "This shift is full.");
         return false;
     }
     if (!$this->meetsRequirement($user_id)) {
         Yii::$app->session->addFlash("error", $this->requirement->errorMessageString);
         return false;
     }
     $participant = new Participant();
     $participant->shift_id = $this->id;
     $participant->user_id = $user_id;
     if ($participant->save()) {
         Yii::$app->session->addFlash("success", sprintf("You are now signed up for the %s '%s' shift on %s", $this->team->name, $this->title, date('M j \\a\\t g:i a', $this->start_time)));
         return true;
     }
     Yii::$app->session->addFlash("error", "There was an error saving: " . print_r($participant->errors, true));
     return false;
 }
 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;
 }
示例#11
0
文件: Activity.php 项目: ninetor/23
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getParticipants()
 {
     return $this->hasMany(Participant::className(), ['activity' => 'id']);
 }
 /**
  * Get the shift participation this user is a part of 
  */
 public function getParticipation()
 {
     return $this->hasMany(Participant::className(), ['user_id' => 'id']);
 }