public function actionAjax_Create()
 {
     if (isset($_POST['StudentElectives'])) {
         $model = new StudentElectives();
         //set the submitted values
         $model->attributes = $_POST['StudentElectives'];
         //return the JSON result to provide feedback.
         if ($model->save(false)) {
             echo json_encode(array('success' => true, 'id' => $model->primaryKey));
             exit;
         } else {
             echo json_encode(array('success' => false));
             exit;
         }
     }
 }
 public function actionElective()
 {
     if (isset($_POST['elective'])) {
         if (isset($_POST['sid'])) {
             if (isset($_POST['elective_id']) and $_POST['elective_id'] != NULL) {
                 foreach ($_POST['sid'] as $sid) {
                     $Student = Students::model()->findByAttributes(array('id' => $sid));
                     $student_elective = StudentElectives::model()->findByAttributes(array('student_id' => $sid, 'elective_group_id' => $_POST['elective_group_id']));
                     if ($_POST['elective_id'] != NULL and $_POST['elective_id'] != 0) {
                         // new record
                         if ($student_elective == NULL) {
                             $electives = new StudentElectives();
                             $electives->student_id = $sid;
                             $electives->batch_id = $Student->batch_id;
                             $electives->elective_id = $_POST['elective_id'];
                             $electives->elective_group_id = $_POST['elective_group_id'];
                             $electives->status = 1;
                             $electives->created = date('Y-m-d h:i:s');
                             $electives->save();
                             Yii::app()->user->setFlash('success', "Elective added to the student");
                         } else {
                             Yii::app()->user->setFlash('error', "Elective is already assigned");
                             $this->redirect(array('elective', 'id' => $Student->batch_id));
                         }
                     } else {
                         Yii::app()->user->setFlash('error', "Select  a subject");
                         $this->redirect(array('elective', 'id' => $Student->batch_id));
                     }
                 }
                 $this->redirect(array('elective', 'id' => $Student->batch_id));
             } else {
                 Yii::app()->user->setFlash('bid', "Select a Subject!");
                 $this->redirect(array('elective', 'id' => $_REQUEST['id']));
             }
         } else {
             if (isset($_POST['elective_id']) and $_POST['elective_id'] != NULL) {
                 Yii::app()->user->setFlash('sid', "Select atleast one student!");
             } else {
                 Yii::app()->user->setFlash('sid', "* Select atleast one student!");
                 Yii::app()->user->setFlash('bid', "* Select a subject!");
             }
             $this->redirect(array('elective', 'id' => $_REQUEST['id']));
         }
     }
     $this->render('elective');
 }