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 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')); } }