public function actionQuestionList() { $form = new QuestionForm(); // database related activity if (isset($_POST['action'])) { $response = new stdClass(); switch ($_POST['action']) { case 1: // print_r($_POST); exit; $form = new QuestionForm(); $form->attributes = $_POST['QuestionForm']; if ($form->validate()) { $data = $_POST['QuestionForm']; $test = RsTest::model()->findByPk($data['test_id']); if ($test) { $data['category_no'] = $test->am; } $criteria = new CDbCriteria(); $criteria->condition = 'test_id=:test_id'; $criteria->addCondition('category=:category'); $criteria->addCondition('no=:no'); $criteria->params = array(":test_id" => $data['test_id'], ":category" => $data['category'], ":no" => $data['no']); $question = RsQuestion::model()->find($criteria) ? RsQuestion::model()->find($criteria) : new RsQuestion(); foreach ($data as $key => $value) { $question->{$key} = $value; } if ($question->save()) { $response->status = 1; $response->id = $question->id; $response->message = Yii::t("admin", "question.save.success"); } else { $response->status = 0; $response->message = Yii::t("admin", "question.save.fail"); } } if ($form->hasErrors()) { $response->status = 2; $response->message = CHtml::errorSummary($form, '', '', array('class' => 'alert alert-error alert-block hide-message')); } break; case 2: // print_r($_POST); $response = new stdClass(); $response->status = 1; $response->message = Yii::t("admin", "question.saveAll.success"); $validate = true; foreach ($_POST['data'] as $data) { $form = new QuestionForm(); foreach ($data as $key => $value) { $form->{$key} = $value; } $form->validate(); // print_r($form); if ($form->hasErrors()) { $validate = false; $response->status = 2; $response->message = CHtml::errorSummary($form, '', '', array('class' => 'alert alert-error alert-block hide-message')); } } if ($validate) { foreach ($_POST['data'] as $data) { $test = RsTest::model()->findByPk($data['test_id']); if ($test) { $data['category_no'] = $test->am; } $criteria = new CDbCriteria(); $criteria->condition = 'test_id=:test_id'; $criteria->addCondition('category=:category'); $criteria->addCondition('no=:no'); $criteria->params = array(":test_id" => $data['test_id'], ":category" => $data['category'], ":no" => $data['no']); $question = RsQuestion::model()->find($criteria) ? RsQuestion::model()->find($criteria) : new RsQuestion(); foreach ($data as $key => $value) { $question->{$key} = $value; } if (!$question->save()) { $response->status = 0; $response->message = Yii::t("admin", "question.saveAll.fail"); } } } break; default: # code... break; } echo json_encode($response); exit; } // if not set t_id then return to test list if (!isset($_GET['t_id'])) { $this->redirect(array('admin/test/')); } // if not set c_id then return to category list of t_id if (!isset($_GET['c_id'])) { $this->redirect(array('admin/test/')); } $criteria = new CDbCriteria(); $criteria->condition = 'test_id=:test_id'; $criteria->addCondition('category=:category_id'); $criteria->order = 'no ASC'; $criteria->params = array(':test_id' => $_GET['t_id'], ':category_id' => $_GET['c_id']); $test = RsTest::model()->findByPk($_GET['t_id']); // whether the requested test exist if (!$test) { $this->redirect(array('admin/test/')); } $tempCat = explode(',', $test->category_am); if (!isset($tempCat[$_GET['c_id'] - 1])) { // whether user requested c_id bigger than am $this->redirect(array('admin/test/')); } $tempQuestions = RsQuestion::model()->findAll($criteria); $questions = array(); if ($tempQuestions) { foreach ($tempQuestions as $q) { $questions[$q->no] = $q; } } $data = array(); $data['numberOfQuestion'] = $tempCat[$_GET['c_id'] - 1]; $data['test'] = $test; $data['questions'] = $questions; $data['category_id'] = $_GET['c_id']; $data['model'] = $form; $this->render('questionList', $data); }
public function actionCreateTest() { $response = array(); $user_id = Yii::app()->user->id; $infoStudent = RsStudent::model()->findByAttributes(array('reg_code' => $user_id)); $currentDate = date("Y-m-d H:i:s"); //var_dump($infoStudent);exit; if ($infoStudent === null) { $this->redirect($this->createUrl('/login')); } else { $criteria = new CDbCriteria(); $criteria->order = 'id DESC'; //$criteria->addCondition("id = (SELECT MAX(id) FROM rs_test)"); $criteria->addCondition("date1 <= '{$currentDate}'"); $criteria->addCondition("date2 >= '{$currentDate}'"); $criteria->addCondition('flag = 1'); $tests = RsTest::model()->findAll($criteria); $response['status'] = 0; if ($tests) { //create test $studentTest = new RsStudentTest(); $studentTest->test_id = $tests[0]->id; $studentTest->student_id = $infoStudent->id; $studentTest->date = $currentDate; $studentTest->category = $tests[0]->category; //get number question in category $number = $tests[0]->category_am; $arr_category = explode(",", $number); $arr_random = array(); foreach ($arr_category as $category) { array_push($arr_random, rand(1, $category)); } $category_no = implode(',', $arr_random); //get random category no $studentTest->category_no = $category_no; if ($studentTest->save()) { $response['status'] = 1; } } else { } echo json_encode($response); } }