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 actionTest() { $user_id = Yii::app()->user->id; $studentInfo = RsStudent::model()->findByAttributes(array('reg_code' => $user_id)); $studentTestCri = new CDbCriteria(); $studentTestCri->condition = 'student_id=:st_id'; $studentTestCri->together = true; $studentTestCri->with = array('student' => array('select' => 'student.*', 'together' => true)); $studentTestCri->with = array('test' => array('select' => 'test.*', 'together' => true)); $studentTestCri->addCondition('date(date)=:today'); $studentTestCri->order = 't.id DESC'; $studentTestCri->params = array(':st_id' => $studentInfo->id, ':today' => date('Y/m/d')); $studentTest = RsStudentTest::model()->find($studentTestCri); if ($studentTest) { // $checkCri = new CDbCriteria; // $checkCri->condition = 'student_id=:student_id'; // $checkCri->addCondition('test_id=:test_id'); // $checkCri->params = array( // 'test_id' => $studentTest->test_id, // 'student_id' => $studentTest->student_id // ); // if (RsTestResult::model()->find($checkCri)) { // $this->redirect(array('front/test/status')); // } $questions = array(); foreach (explode(",", $studentTest->category_no) as $key => $value) { $questionsCri = new CDbCriteria(); $questionsCri->condition = 'test_id=:test_id'; $questionsCri->addCondition('category=:category'); $questionsCri->addCondition('no=:question_no'); $questionsCri->params = array('test_id' => $studentTest->test_id, 'category' => $key + 1, 'question_no' => $value); array_push($questions, RsQuestion::model()->find($questionsCri)); } if (isset($_POST['answer'])) { // print_r($_POST['answer']); exit; $modalData = array(); $i = 0; foreach ($_POST['answer'] as $key => $value) { if ($questions[$key]["answer"] == $value) { $i++; } } $result = new RsTestResult(); $result->test_id = $studentTest->test_id; $result->student_id = $studentTest->student_id; $result->date = date("Y/m/d H:i:s"); $result->point = $i; $result->am = $studentTest->test->am; $result->pof = $i >= $studentTest->test->point ? 1 : 0; $result->answer_list = join($_POST['answer'], ","); $result->test_title = $studentTest->test->title; $result->student_name = $studentTest->student->first_name . " " . $studentTest->student->last_name; $result->category = $studentTest->category; $result->category_no = $studentTest->category_no; // echo "<pre>"; var_dump($result); $result->save(); $modalData['questions'] = $questions; $modalData['answer'] = $_POST['answer']; $modalData['correct'] = $i; $modalData['pass'] = $result->pof; echo $this->renderPartial('_test', $modalData, true); } else { $data = array(); $data['questions'] = $questions; $data['test'] = $studentTest; $this->render('test', $data); } } else { $this->redirect($this->createUrl('front/test/status')); } }