public function import_data($data) { $flag = array(); $list_id = array(); foreach ($data as $value) { try { if (in_array($value[0], $flag)) { $question = new RsQuestion(); $question->no = $value[9]; $question->answer = $value[10]; $question->exp = $value[11]; $question->q = $value[12]; $question->category = $value[13]; $question->test_id = $list_id[$value[0]]; $question->save(); } else { $flag[] = $value[0]; $test = new RsTest(); $test->title = $value[1]; $date = new DateTime(str_replace("/", "-", $value[2])); $test->date1 = $date->format('Y-m-d H:i:s'); $date = new DateTime(str_replace("/", "-", $value[3])); $test->date2 = $date->format('Y-m-d H:i:s'); $test->remark = $value[4]; $test->am = $value[5]; $test->point = $value[6]; if (empty($value[7])) { $value[7] = 0; } $test->flag = $value[7]; if (isset($value[8])) { $test->category_am = $value[8]; } if ($test->save()) { $list_id[$value[0]] = $test->id; $question = new RsQuestion(); $question->no = $value[9]; $question->answer = $value[10]; $question->exp = $value[11]; $question->q = $value[12]; $question->category = $value[13]; $question->test_id = $list_id[$value[0]]; $question->save(); } } } catch (Exception $e) { } } }
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); } }