/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Exams();
     $model_1 = new ExamGroups();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Exams'])) {
         //$model->attributes=$_POST['Exams'];
         if (isset($_REQUEST['exam_group_id'])) {
             $insert_id = $_REQUEST['exam_group_id'];
         } else {
             $model_1->attributes = $_POST['ExamGroups'];
             $model_1->batch_id = $_REQUEST['id'];
             $model_1->save();
             $insert_id = Yii::app()->db->getLastInsertID();
         }
         $posts = Subjects::model()->findAll("batch_id=:x AND no_exams=:y", array(':x' => $_REQUEST['id'], ':y' => 0));
         $list = $_POST['Exams'];
         $count = count($list['subject_id']);
         $electivecount = count($_POST['ElectiveExams']['elective_id']);
         $j = 0;
         for ($i = 0; $i < $count; $i++) {
             if ($list['maximum_marks'][$i] != NULL and $list['minimum_marks'][$i] != NULL and $list['start_time'][$i] != NULL and $list['end_time'][$i] != NULL) {
                 $model = new Exams();
                 $model->exam_group_id = $insert_id;
                 $model->subject_id = $list['subject_id'][$i];
                 $model->maximum_marks = $list['maximum_marks'][$i];
                 $model->minimum_marks = $list['minimum_marks'][$i];
                 $model->start_time = $list['start_time'][$i];
                 $model->end_time = $list['end_time'][$i];
                 if ($model->start_time) {
                     $date1 = date('Y-m-d H:i', strtotime($model->start_time));
                     $model->start_time = $date1;
                 }
                 if ($model->end_time) {
                     $date2 = date('Y-m-d H:i', strtotime($model->end_time));
                     $model->end_time = $date2;
                 }
                 $model->grading_level_id = $list['grading_level_id'];
                 $model->weightage = $list['weightage'];
                 $model->event_id = $list['event_id'];
                 $model->created_at = $list['created_at'];
                 $model->updated_at = $list['updated_at'];
                 //$model->save();
                 if ($model->save()) {
                     $subject_name = Subjects::model()->findByAttributes(array('id' => $model->subject_id));
                     $examgroup = ExamGroups::model()->findByAttributes(array('id' => $model->exam_group_id));
                     $batch = Batches::model()->findByAttributes(array('id' => $examgroup->batch_id));
                     $exam = ucfirst($subject_name->name) . ' - ' . ucfirst($examgroup->name) . ' (' . ucfirst($batch->name) . '-' . ucfirst($batch->course123->course_name) . ')';
                     //Adding activity to feed via saveFeed($initiator_id,$activity_type,$goal_id,$goal_name,$field_name,$initial_field_value,$new_field_value)
                     ActivityFeed::model()->saveFeed(Yii::app()->user->Id, '17', $model->id, $exam, NULL, NULL, NULL);
                 }
             }
         }
         $this->redirect(array('exams/create', 'id' => $_REQUEST['id'], 'exam_group_id' => $_REQUEST['exam_group_id']));
     }
     $this->render('create', array('model' => $model, 'model_1' => $model_1));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Exams();
     $model_1 = new ExamGroups();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Exams'])) {
         //$model->attributes=$_POST['Exams'];
         if (isset($_REQUEST['exam_group_id'])) {
             $insert_id = $_REQUEST['exam_group_id'];
         } else {
             $model_1->attributes = $_POST['ExamGroups'];
             $model_1->batch_id = $_REQUEST['id'];
             $model_1->save();
             $insert_id = Yii::app()->db->getLastInsertID();
         }
         $posts = Subjects::model()->findAll("batch_id=:x AND no_exams=:y", array(':x' => $_REQUEST['id'], ':y' => 0));
         $list = $_POST['Exams'];
         $count = count($list['subject_id']);
         $j = 0;
         for ($i = 0; $i < $count; $i++) {
             if ($list['maximum_marks'][$i] != NULL and $list['minimum_marks'][$i] != NULL and $list['start_time'][$i] != NULL and $list['end_time'][$i] != NULL) {
                 $model = new Exams();
                 $model->exam_group_id = $insert_id;
                 $model->subject_id = $list['subject_id'][$i];
                 $model->maximum_marks = $list['maximum_marks'][$i];
                 $model->minimum_marks = $list['minimum_marks'][$i];
                 $model->start_time = $list['start_time'][$i];
                 $model->end_time = $list['end_time'][$i];
                 if ($model->start_time) {
                     $date1 = date('Y-m-d H:i', strtotime($model->start_time));
                     $model->start_time = $date1;
                 }
                 if ($model->end_time) {
                     $date2 = date('Y-m-d H:i', strtotime($model->end_time));
                     $model->end_time = $date2;
                 }
                 $model->grading_level_id = $list['grading_level_id'];
                 $model->weightage = $list['weightage'];
                 $model->event_id = $list['event_id'];
                 $model->created_at = $list['created_at'];
                 $model->updated_at = $list['updated_at'];
                 $model->save();
             }
         }
         $this->redirect(array('exams/create', 'id' => $_REQUEST['id'], 'exam_group_id' => $_REQUEST['exam_group_id']));
     }
     $this->render('create', array('model' => $model, 'model_1' => $model_1));
 }
 public function createExam()
 {
     $rules = array();
     $rules['questioncount'] = 'required|integer|min:5|max:100';
     $rules['title'] = 'required|min:6|max:1024';
     $rules['timing'] = 'required';
     $messages = array();
     $questioncount = Input::get('questioncount');
     for ($qc = 1; $qc <= $questioncount; $qc++) {
         $rules['question_' . $qc] = 'required|min:4|max:1024';
         $rules['checkbox_' . $qc] = 'required';
         $rules['checkbox_' . $qc . '_1'] = 'required|min:1|max:1024';
         $rules['checkbox_' . $qc . '_2'] = 'required|min:1|max:1024';
         $rules['checkbox_' . $qc . '_3'] = 'required|min:1|max:1024';
         $rules['checkbox_' . $qc . '_4'] = 'required|min:1|max:1024';
     }
     for ($qc = 1; $qc <= $questioncount; $qc++) {
         $messages['question_' . $qc . '.required'] = 'Question ' . $qc . " is required";
         $messages['checkbox_' . $qc . '_1' . '.required'] = 'The value for ' . $qc . ' Checkbox 1 is missing.';
         $messages['checkbox_' . $qc . '_2' . '.required'] = 'The value for ' . $qc . ' Checkbox 2 is missing.';
         $messages['checkbox_' . $qc . '_3' . '.required'] = 'The value for ' . $qc . ' Checkbox 3 is missing.';
         $messages['checkbox_' . $qc . '_4' . '.required'] = 'The value for ' . $qc . ' Checkbox 4 is missing.';
     }
     $validator = Validator::make(Input::all(), $rules, $messages);
     if ($validator->fails()) {
         Input::flash();
         return View::make('dashboard.exams.create')->with('errors', $validator->getMessageBag())->with('id', 0);
     }
     $input = Input::all();
     $exam = new Exams();
     $exam->title = Input::get('title');
     $exam->subjectid = Input::get('subject');
     $exam->createdby = Sentry::getUser()->id;
     $exam->save();
     $newexam = DB::table('exams')->orderby('id', 'desc')->first();
     $data = array();
     $data['maxtime'] = Input::get('timing', '30');
     for ($question = 1; $question <= $questioncount;) {
         $data['questiondata']['questions'][$question] = $input['question_' . $question];
         for ($checkbox = 1; $checkbox <= 4; $checkbox++) {
             $data['questiondata']['question'][$question]['checkboxdata'][$checkbox] = $input['checkbox_' . $question . '_' . $checkbox];
         }
         if (is_array($input['checkbox_' . $question])) {
             if (isset($input['checkbox_' . $question][0]) && $input['checkbox_' . $question][0] > 0 && $input['checkbox_' . $question][0] <= 4) {
                 $data['questiondata']['question'][$question]['answers'][0] = $input['checkbox_' . $question][0];
             }
             if (isset($input['checkbox_' . $question][1])) {
                 $data['questiondata']['question'][$question]['answers'][1] = $input['checkbox_' . $question][1];
             }
             if (isset($input['checkbox_' . $question][2])) {
                 $data['questiondata']['question'][$question]['answers'][2] = $input['checkbox_' . $question][2];
             }
             if (isset($input['checkbox_' . $question][3])) {
                 $data['questiondata']['question'][$question]['answers'][3] = $input['checkbox_' . $question][3];
             }
             if (isset($input['checkbox_' . $question][4])) {
                 $data['questiondata']['question'][$question]['answers'][4] = $input['checkbox_' . $question][4];
             }
         } else {
             $data['questiondata']['question'][$question]['answers'] = $input['checkbox_' . $question][0];
         }
         $question++;
     }
     $encoded = json_encode($data);
     $encryptedpath = Crypt::encrypt('questiondata');
     $encryptedpath = substr($encryptedpath, strlen($encryptedpath) / 2);
     if (!is_dir(app_path() . '/files/exam-' . $newexam->id)) {
         File::makeDirectory(app_path() . '/files/exam-' . $newexam->id);
     }
     file_put_contents(app_path() . '/files/exam-' . $newexam->id . '/' . $encryptedpath . '.json', $encoded);
     $newexame = Exams::find($newexam->id);
     $newexame->hash = $encryptedpath;
     $newexame->totalquestions = $question - 1;
     $newexame->save();
     return Redirect::to("/exams");
 }