/**
  * Creates a new BlockTime model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new BlockTime();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         //each time a block Time is created , it fetches all the registration tokens of all the users with the course and sends them a push notification
         $UsersHasCourse = UserHasCourse::findAll(["course_id" => $model->course_id]);
         foreach ($UsersHasCourse as $user_one) {
             $User = InvokerUser::findOne(['id' => $user_one['user_id']]);
             $gcmClient = new GcmClient('YOUR_API_KEY');
             $message = new Message($gcmClient);
             $message->addRegistrationId($User->reg_token);
             $message->setData(['title' => 'New Block Time', 'message' => ['starttime' => $model->starttime, 'endtime' => $model->endtime]]);
             try {
                 $response = $message->send();
                 // The send() method returns a Response object
                 print_r($response);
                 //exit(1);
             } catch (Exception $exception) {
                 echo 'uh-oh: ' . $exception->getMessage();
                 //exit(1);
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Пример #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = UserHasCourse::find()->joinWith('course')->joinWith('user')->innerJoin('role', 'role.id = user.role_id');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->params['pageSize']]]);
     /**
      * sortin related columns
      */
     $dataProvider->sort->attributes['course.name'] = ['asc' => ['course.name' => SORT_ASC], 'desc' => ['course.name' => SORT_DESC]];
     $dataProvider->sort->attributes['user.username'] = ['asc' => ['user.username' => SORT_ASC], 'desc' => ['user.username' => SORT_DESC]];
     $dataProvider->sort->attributes['user.role.name'] = ['asc' => ['role.name' => SORT_ASC], 'desc' => ['role.name' => SORT_DESC]];
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['user_id' => $this->user_id, 'course_id' => $this->course_id]);
     $query->andFilterWhere(['LIKE', 'course.name', $this->getAttribute('course.name')]);
     $query->andFilterWhere(['LIKE', 'user.username', $this->getAttribute('user.username')]);
     $query->andFilterWhere(['LIKE', 'role.name', $this->getAttribute('user.role.name')]);
     return $dataProvider;
 }
Пример #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUserHasCourses()
 {
     return $this->hasMany(UserHasCourse::className(), ['user_id' => 'id']);
 }
Пример #4
0
 /**
  * Finds the UserHasCourse model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $user_id
  * @param integer $course_id
  * @return UserHasCourse the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($user_id, $course_id)
 {
     if (($model = UserHasCourse::findOne(['user_id' => $user_id, 'course_id' => $course_id])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }