コード例 #1
0
 /**
  * Updates an existing Activity 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);
     $transaction = Yii::$app->db->beginTransaction();
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             //课程开始
             if ($model->status == Activity::DOING) {
                 //获取该课程下已录取的学员
                 $ActivityUsers = ActivityUsers::getApprovedActivityUsersByActivityId($model->id);
                 if (!empty($ActivityUsers)) {
                     //根据课程id,用户id更新用户状态为正在进行
                     ActivityUsers::updateActivityUsersStatusByActivityId(ActivityUsers::DOING, $model->id);
                     foreach ($ActivityUsers as $key => $val) {
                         //创建考勤信息
                         for ($i = strtotime($model->begin_time); $i < strtotime($model->end_time); $i += 86400) {
                             $day = date('Y-m-d H:i:s', $i);
                             $isExist = ActivityProcess::findOne(['activity_id' => $model->id, 'user_id' => $val['user_id'], 'day' => $day]);
                             if (empty($isExist)) {
                                 $attendance = ['activity_id' => $model->id, 'user_id' => $val['user_id'], 'day' => $day];
                                 ActivityProcess::add($attendance);
                             }
                         }
                     }
                 } else {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('更新状态失败,原因:该培训课程下没有学员!');
                 }
             }
             $transaction->commit();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }