/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new WCourseTrainee();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['WCourseTrainee'])) {
         if (isset($_POST['list_trainee'])) {
             $traineeList = explode(',', $_POST['list_trainee']);
             if (!empty($traineeList)) {
                 foreach ($traineeList as $value) {
                     if (!empty($value)) {
                         $model = new WCourseTrainee();
                         $model->attributes = $_POST['WCourseTrainee'];
                         $model->trainee_id = $value;
                         if ($model->save()) {
                         }
                     }
                 }
             }
         }
         $this->redirect(array('view', 'id' => $model->id));
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     if (empty(Yii::app()->user->id) || Yii::app()->session['group_id'] == 2) {
         $this->redirect(Yii::app()->createUrl(''));
     }
     $cs = Yii::app()->getClientScript();
     //$cs->registerScriptFile(Yii::app()->theme->baseUrl.'/js/ckeditor/ckeditor.js');
     $cs->registerCssFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.fileupload.css');
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/bootstrap-datepicker/bootstrap-datetimepicker.js', CClientScript::POS_END);
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.ui.widget.js', CClientScript::POS_END);
     $cs->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jqueryUpload/jquery.fileupload.js', CClientScript::POS_END);
     $model = WCourse::model()->find('id=' . $id . ' AND created_by=' . Yii::app()->user->id);
     $nameSession = "";
     $nameSessionTrainee = "";
     if (!empty($model->id)) {
         $nameSession = "_" . $model->id;
         $nameSessionTrainee = $model->id;
     }
     if (empty($model)) {
         $this->redirect(Yii::app()->createUrl(''));
     }
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $model->start_date = date('Y-m-d', strtotime($model->start_date));
     $model->end_date = date('Y-m-d', strtotime($model->end_date));
     if (isset($_POST['WCourse'])) {
         if (!empty($_POST['avatar'])) {
             $_POST['WCourse']['avatar'] = Common::createThumb('avatars/course', $_POST['avatar'], $_POST['WCourse']['course_name']);
             $name_avatar_old = $model->avatar;
             if (!empty($name_avatar_old) and file_exists(Yii::app()->params['upload_path'] . 'uploads/avatars/course/' . $name_avatar_old)) {
                 unlink(Yii::app()->params['upload_path'] . 'uploads/avatars/course/' . $name_avatar_old);
             }
         }
         $model->attributes = $_POST['WCourse'];
         $model->created_date = date('Y-m-d H:i:s');
         if (Yii::app()->session['group_id'] == 2) {
             $model->company_code = Yii::app()->session['company_code'];
         }
         $model->created_by = Yii::app()->user->id;
         $dataLecture = Yii::app()->session->get('dataLecture' . $nameSession) ? json_decode(Yii::app()->session->get('dataLecture' . $nameSession), true) : array();
         $listTrainee = Yii::app()->session->get('listTrainee_' . $nameSessionTrainee) ? json_decode(Yii::app()->session->get('listTrainee_' . $nameSessionTrainee), true) : array();
         $model->trainee_count = count($listTrainee);
         $model->lecture_count = count($dataLecture);
         if ($model->save()) {
             if (!empty($dataLecture)) {
                 foreach ($dataLecture as $item) {
                     $modelCourseLecture = CourseLecture::model()->find('course_id=' . $model->id . ' AND lecture_id=' . $item['id']);
                     if (empty($modelCourseLecture)) {
                         $modelCourseLecture = new CourseLecture();
                         $modelCourseLecture->course_id = $model->id;
                         $modelCourseLecture->lecture_id = $item['id'];
                         $modelCourseLecture->trainer_id = Yii::app()->user->id;
                     }
                     $modelCourseLecture->start_date = $item['start_date'];
                     $modelCourseLecture->end_date = $item['end_date'];
                     $modelCourseLecture->status = 1;
                     if ($modelCourseLecture->save()) {
                         unset($_SESSION['dataLecture' . $nameSession]);
                     }
                 }
             }
         }
         if (!empty($listTrainee)) {
             foreach ($listTrainee as $item) {
                 $modelCourseTrainee = WCourseTrainee::model()->find('course_id=' . $model->id . ' AND trainee_id=' . $item['id']);
                 if (empty($modelCourseTrainee)) {
                     $modelCourseTrainee = new WCourseTrainee();
                     $modelCourseTrainee->course_id = $model->id;
                     $modelCourseTrainee->trainee_id = $item['id'];
                     $modelCourseTrainee->status = 1;
                 }
                 if ($modelCourseTrainee->save()) {
                     unset($_SESSION['listTrainee_' . $nameSessionTrainee]);
                 }
             }
         }
         $this->redirect(array('update', 'id' => $model->id));
     }
     $dataCourseLecture = array();
     if (!empty($id)) {
         unset($_SESSION['dataLecture' . $nameSession]);
         unset($_SESSION['listTrainee_' . $nameSessionTrainee]);
         $data = CourseLecture::model()->findAll('course_id=' . $id . ' ORDER BY start_date ASC');
         $data = CJSON::decode(CJSON::encode($data));
         $dataCourseLecture = array();
         if (!empty($data)) {
             foreach ($data as $key => $item) {
                 if (!empty($item["lecture_id"])) {
                     $lectureDetails = Lecture::model()->find('id=' . $item['lecture_id']);
                     if (!empty($lectureDetails)) {
                         $lectureDetails = CJSON::decode(CJSON::encode($lectureDetails));
                         $dataCourseLecture[$item['lecture_id']]['id'] = $item['lecture_id'];
                         $dataCourseLecture[$item['lecture_id']]['start_date'] = date('Y-m-d H:i', strtotime($item['start_date']));
                         $dataCourseLecture[$item['lecture_id']]['end_date'] = date('Y-m-d H:i', strtotime($item['end_date']));
                         $dataCourseLecture[$item['lecture_id']]['lecture_name'] = $lectureDetails['lecture_name'];
                         $dataCourseLecture[$item['lecture_id']]['course_id'] = $item['course_id'];
                     }
                 }
             }
         }
         $dataTrainee = CourseTrainee::model()->findAll('course_id=' . $id . ' AND status=1');
         $dataTrainee = CJSON::decode(CJSON::encode($dataTrainee));
         $listTrainee = array();
         if (!empty($dataTrainee)) {
             foreach ($dataTrainee as $item) {
                 $traineeDetails = WUser::model()->find('id=' . $item['trainee_id']);
                 if (!empty($traineeDetails)) {
                     $traineeDetails = CJSON::decode(CJSON::encode($traineeDetails));
                     $listTrainee[$traineeDetails['id']]['id'] = $traineeDetails['id'];
                     $listTrainee[$traineeDetails['id']]['course_id'] = $id;
                     $listTrainee[$traineeDetails['id']]['fullname'] = $traineeDetails['fullname'];
                     $listTrainee[$traineeDetails['id']]['username'] = $traineeDetails['username'];
                 }
             }
         }
         Yii::app()->session->add('listTrainee_' . $nameSessionTrainee, json_encode($listTrainee));
         Yii::app()->session->add('dataLecture' . $nameSession, json_encode($dataCourseLecture));
     }
     $this->render('update', array('model' => $model, 'dataCourseLecture' => $dataCourseLecture));
 }
Пример #3
0
 public function actionRegisterCourse()
 {
     $trainee_id = $_POST['trainee_id'];
     $course_id = $_POST['course_id'];
     $model = WCourseTrainee::model()->find('trainee_id = ' . $trainee_id . ' AND course_id=' . $course_id);
     if (empty($model)) {
         $model = new WCourseTrainee();
         $model->trainee_id = $trainee_id;
         $model->course_id = $course_id;
         $model->register_date = date('Y-m-d H:i');
         $model->status = 0;
         if ($model->save()) {
             echo json_encode(array('status' => 'true', 'mess' => 'You have successfully registered. Waiting for activation'));
         } else {
             echo json_encode(array('status' => 'false', 'mess' => ''));
         }
     } else {
         echo json_encode(array('status' => 'exists', 'mess' => 'Waiting... for activation'));
     }
 }