/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Requirement::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
 public function addUserRequirement()
 {
     if (!$this->validate()) {
         return false;
     }
     $user = User::findOne($this->user_id);
     if (Requirement::findOne($this->requirement_id === null) || $user === null) {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
     foreach ($user->requirements as $current_req) {
         if ($current_req->id == $this->requirement_id) {
             return false;
         }
     }
     $req = new UserRequirement();
     $req->user_id = $this->user_id;
     $req->requirement_id = $this->requirement_id;
     return $req->save();
 }
 /**
  * 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]);
 }
 /**
  * Updates an existing Shift model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if (!$model->team->event->active) {
         Yii::$app->session->addFlash('error', 'Shifts can not be updated once an event is closed');
         Yii::$app->user->setReturnUrl(Yii::$app->request->referrer);
         return $this->goBack();
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $requirements = Requirement::find()->orderBy('name ASC')->all();
         return $this->render('update', ['requirements' => $requirements, 'model' => $model]);
     }
 }
 public function importShifts($data)
 {
     //Data should be a 2D array of the following
     //[title, start_timestamp, length, min_participants, max_participants, requirement_name]
     $count = 0;
     foreach ($data as $row) {
         $timestamp = strtotime($row[1]);
         if (isset($row[5])) {
             $requirement = Requirement::findOne(['name' => $row[5]]);
         } else {
             $requirement = null;
         }
         $shift = new Shift();
         $shift->title = $row[0];
         $shift->start_time = $timestamp;
         $shift->team_id = $this->id;
         $shift->length = $row[2];
         $shift->min_needed = !empty($row[3]) ? $row[3] : null;
         $shift->max_needed = !empty($row[4]) ? $row[4] : null;
         $shift->requirement_id = isset($requirement) ? $requirement->id : null;
         $shift->active = true;
         if ($shift->save()) {
             $count++;
         }
     }
     Yii::$app->session->addFlash('success', sprintf("Imported %d of %d shifts successfully", $count, count($data)));
 }
 public function getRequirement()
 {
     return $this->hasOne(Requirement::className(), ['id' => 'requirement_id']);
 }
 public function getRequirements()
 {
     return $this->hasMany(Requirement::className(), ['id' => 'requirement_id'])->viaTable('user_requirement', ['user_id' => 'id']);
 }
 /**
  * Finds the Requirement model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Requirement the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Requirement::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }