/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new WUserAnswers(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['WUserAnswers'])) { $model->attributes = $_POST['WUserAnswers']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public function actionAutoSaveQuestionAnswer() { $user_test_id = isset($_REQUEST["user_test_id"]) ? $_REQUEST["user_test_id"] : NULL; $json = array("result" => "true"); if (!empty($user_test_id)) { $db = Yii::app()->db; $comm = $db->createCommand()->select('t.*,t5.fullname,t5.username,t4.total_point,t2.course_name,t3.lecture_name,t4.test_name, (SELECT dataquestion FROM pls_test_user WHERE user_id=t.user_id AND id_test_parent=t.test_id AND course_id=t.course_id AND lecture_id=t.lecture_id AND kind=t.test_kind) as dataquestion ')->from('pls_user_test t')->leftJoin('pls_course t2', 't2.id=t.course_id')->leftJoin('pls_lecture t3', 't3.id=t.lecture_id')->leftJoin('pls_test_parent t4', 't4.id=t.test_id')->leftJoin('pls_user t5', 't5.id=t.user_id')->where('t.id IS NOT NULL AND t.id=' . $user_test_id . ' AND t.user_id=' . Yii::app()->user->id); $result = $comm->queryRow(); if (!empty($result)) { $dataQuestion = (isset($result["dataquestion"]) and !empty($result["dataquestion"])) ? json_decode($result["dataquestion"], true) : array(); if (!empty($dataQuestion)) { foreach ($dataQuestion as $itemquestion) { $dataQuestion = WQuestionLib::model()->find("id=" . $itemquestion["idquestion"]); $model = WUserAnswers::model()->find("user_id=" . Yii::app()->user->id . " AND test_id=" . $result["test_id"] . " AND user_test_id=" . $user_test_id . " AND question_id=" . $itemquestion["idquestion"]); if (empty($model)) { $model = new WUserAnswers(); $model->test_id = $result["test_id"]; $model->user_test_id = $user_test_id; $model->question_id = $itemquestion["idquestion"]; $model->user_answer_id = ","; $model->question_type = $dataQuestion["question_type"]; $model->user_id = Yii::app()->user->id; $model->doing = 0; $model->created_date = date('Y-m-d H:i:s'); if (!$model->save()) { $json = array("result" => "false"); } } } } } } echo json_encode($json); }
public function actionDoingtest() { $per_page = 1; $page_number = isset($_POST['page']) ? $_POST['page'] : 0; //echo $page_number; $test_id = isset($_POST['test_id']) ? $_POST['test_id'] : ''; $course_id = isset($_POST['course_id']) ? $_POST['course_id'] : ''; $lecture_id = isset($_POST['lecture_id']) ? $_POST['lecture_id'] : ''; $oneUT = WUserTest::model()->find('test_id=:test_id and course_id=:course_id and lecture_id=:lecture_id and user_id=:user_id', array(':test_id' => $test_id, ':course_id' => $course_id, ':lecture_id' => $lecture_id, ':user_id' => Yii::app()->user->id)); $position = ($page_number - 1) * $per_page; $total = WQuestion::totalQuestion($test_id); $pagination = WFunction::paging_question($total, $per_page); $question = WQuestion::listQuestion($test_id, $position, $per_page); $nameButton = isset($_POST['nameButton']) ? $_POST['nameButton'] : ''; $UA = ''; $arrUA = array(); if ($nameButton == 'next') { $ques_type = isset($_POST['question_type']) ? $_POST['question_type'] : ''; $ques_id = isset($_POST['question_id']) ? $_POST['question_id'] : ''; $point = isset($_POST['point']) ? $_POST['point'] : ''; $total_point = $oneUT['pass_score_point']; if ($ques_type == 1) { $answer = $_POST['rdAns']; //echo $answer;exit(); if ($answer != '') { $modelUserAnswers = new WUserAnswers(); $modelUserAnswers->user_test_id = $oneUT['id']; $modelUserAnswers->question_id = $ques_id; $modelUserAnswers->answer_id = $answer; $modelUserAnswers->user_answer_id = $answer; $modelUserAnswers->user_answer_text = ''; $modelUserAnswers->created_date = date('Y-m-d H:i:s'); $oneAns = WUserAnswers::model()->find('user_test_id=:user_test_id and question_id=:question_id', array(':user_test_id' => $oneUT['id'], ':question_id' => $ques_id)); if (is_null($oneAns)) { $modelUserAnswers->save(); //Yii::app()->session['ans_id'.$ques_id] = $modelUserAnswers->id; } else { //echo $oneAns['id'];exit(); //$modelUserAnswers->updateByPk($oneAns['id'], array($answer)); WUserAnswers::updateUA($oneAns['id'], $answer); } if (WUserAnswers::checkAnswer($ques_id, $ques_type)) { $point_user = $point + $total_point; } else { $point_user = $total_point; } WUserTest::updatePointTrainee($test_id, $course_id, Yii::app()->user->id, $point_user, $ques_type); } else { } } elseif ($ques_type == 4) { $stranswer = $_POST['chkAns']; $answers = explode(',', $stranswer); //var_dump($answers); exit(); $modelUserAnswersM = new WUserAnswers(); if (count($answers) > 0) { $arrValues = array(); foreach ($answers as $a) { //echo $a.' '.$ques_id;exit(); //$modelUserAnswersM->user_test_id = $oneUT['id']; // $modelUserAnswersM->question_id = $ques_id; // $modelUserAnswersM->answer_id = $a; // $modelUserAnswersM->user_answer_id = $a; // $modelUserAnswersM->user_answer_text = ''; // $modelUserAnswersM->created_date = date('Y-m-d H:i:s'); // $oneAns1 = WUserAnswers::model()->find( // 'user_test_id=:user_test_id and // question_id=:question_id and // answer_id=:answer_id', // array( // ':user_test_id'=> $oneUT['id'], // ':question_id' => $ques_id, // ':answer_id'=> $a // )); // if(isset($oneAns1)){ // $modelUserAnswersM->save(); // }else{ // WUserAnswers::updateUA($oneAns1['id'], $a); // } $arrValues[] = '("", ' . $oneUT['id'] . ', ' . $ques_id . ', ' . $a . ', ' . $a . ', "", "' . date("Y-m-d H:i:s") . '")'; } $sql = "INSERT INTO {{user_answers}} (`id`, `user_test_id`, `question_id`, `answer_id`, `user_answer_id`, `user_answer_text`, `created_date`) VALUES " . implode(',', $arrValues) . ""; WUserAnswers::insertAnswerM($sql); if (WUserAnswers::checkAnswer($ques_id, $ques_type)) { $point_user = $point + $total_point; } else { $point_user = $total_point; } WUserTest::updatePointTrainee($test_id, $course_id, Yii::app()->user->id, $point_user, $ques_type); } } elseif ($ques_type == 2) { $answer = $_POST['txtFree']; if ($answer != '') { $modelUserAnswers = new WUserAnswers(); $modelUserAnswers->user_test_id = $oneUT['id']; $modelUserAnswers->question_id = $ques_id; $modelUserAnswers->answer_id = ''; $modelUserAnswers->user_answer_id = ''; $modelUserAnswers->user_answer_text = $answer; $modelUserAnswers->created_date = date('Y-m-d H:i:s'); $oneAns = WUserAnswers::model()->find('user_test_id=:user_test_id and question_id=:question_id', array(':user_test_id' => $oneUT['id'], ':question_id' => $ques_id)); if (is_null($oneAns)) { $modelUserAnswers->save(); } else { WUserAnswers::updateUA($oneAns['id'], $answer); } } } WUserTest::updateStatusTest($test_id, $course_id, 1); } elseif ($nameButton == 'previous') { $ques_id = isset($_POST['question_id']) ? $_POST['question_id'] : ''; $ques_type = isset($_POST['question_type']) ? $_POST['question_type'] : ''; if ($ques_type == 1 || $ques_type == 2) { $UA = WUserAnswers::getIDTextAnswer($ques_id, $oneUT['id'], $ques_type); //echo $UA;exit(); } else { $arrUA = WUserAnswers::getIDAnswer($ques_id, $oneUT['id']); } } elseif ($nameButton == 'finished') { $ques_type = isset($_POST['question_type']) ? $_POST['question_type'] : ''; $ques_id = isset($_POST['question_id']) ? $_POST['question_id'] : ''; $point = isset($_POST['point']) ? $_POST['point'] : ''; $total_point = $oneUT['pass_score_point']; if ($ques_type == 1) { $answer = $_POST['rdAns']; if ($answer != '') { $modelUserAnswers = new WUserAnswers(); $modelUserAnswers->user_test_id = $oneUT['id']; $modelUserAnswers->question_id = $ques_id; $modelUserAnswers->answer_id = $answer; $modelUserAnswers->user_answer_id = $answer; $modelUserAnswers->user_answer_text = ''; $modelUserAnswers->created_date = date('Y-m-d H:i:s'); $oneAns = WUserAnswers::model()->find('user_test_id=:user_test_id and question_id=:question_id', array(':user_test_id' => $oneUT['id'], ':question_id' => $ques_id)); if (is_null($oneAns)) { $modelUserAnswers->save(); //Yii::app()->session['ans_id'.$ques_id] = $modelUserAnswers->id; } else { //echo $oneAns['id'];exit(); //$modelUserAnswers->updateByPk($oneAns['id'], array($answer)); WUserAnswers::updateUA($oneAns['id'], $answer); } if (WUserAnswers::checkAnswer($ques_id, $ques_type)) { $point_user = $total_point + $point; } else { $point_user = $total_point; } WUserTest::updatePointTrainee($test_id, $course_id, Yii::app()->user->id, $point_user, $ques_type); } else { } } elseif ($ques_type == 4) { $stranswer = $_POST['chkAns']; $answermm = explode(',', $stranswer); $modelUserAnswersMM = new WUserAnswers(); if (count($answermm) > 0) { $arrValues = array(); foreach ($answermm as $a) { //$modelUserAnswersMM->user_test_id = $oneUT['id']; // $modelUserAnswersMM->question_id = $ques_id; // $modelUserAnswersMM->answer_id = $a; // $modelUserAnswersMM->user_answer_id = $a; // $modelUserAnswersMM->user_answer_text = ''; // $modelUserAnswersMM->created_date = date('Y-m-d H:i:s'); $arrValues[] = '("", ' . $oneUT['id'] . ', ' . $ques_id . ', ' . $a . ', ' . $a . ', "" , "' . date("Y-m-d H:i:s") . '")'; //$oneAns2 = WUserAnswers::model()->find('user_test_id=:user_test_id and question_id=:question_id and answer_id=:answer_id', array(':user_test_id'=> $oneUT['id'], ':question_id' => $ques_id, ':answer_id'=> $a)); // if(is_null($oneAns2)){ // // $modelUserAnswersMM->save(); // // }else{ // WUserAnswers::updateUA($oneAns2['id'], $a); // } } $sql = "INSERT INTO {{user_answers}} (`id`, `user_test_id`, `question_id`, `answer_id`, `user_answer_id`, `user_answer_text`, `created_date`) VALUES " . implode(',', $arrValues) . ""; WUserAnswers::insertAnswerM($sql); if (WUserAnswers::checkAnswer($ques_id, $ques_type)) { $point_user = $total_point + $point; } else { $point_user = $total_point; } WUserTest::updatePointTrainee($test_id, $course_id, Yii::app()->user->id, $point_user, $ques_type); } } elseif ($ques_type == 2) { $answer = $_POST['txtFree']; if ($answer != '') { $modelUserAnswers = new WUserAnswers(); $modelUserAnswers->user_test_id = $oneUT['id']; $modelUserAnswers->question_id = $ques_id; $modelUserAnswers->answer_id = ''; $modelUserAnswers->user_answer_id = ''; $modelUserAnswers->user_answer_text = $answer; $modelUserAnswers->created_date = date('Y-m-d H:i:s'); $oneAns = WUserAnswers::model()->find('user_test_id=:user_test_id and question_id=:question_id', array(':user_test_id' => $oneUT['id'], ':question_id' => $ques_id)); if (is_null($oneAns)) { $modelUserAnswers->save(); } else { WUserAnswers::updateUA($oneAns['id'], $answer); } } } $url = Yii::app()->params->url_base . '/index.php?r=wTest/result&test_id=' . $test_id . '&course_id=' . $course_id . '&lecture_id=' . $lecture_id . '&total=' . $total; echo "<script type='text/javascript'>top.location.href = '{$url}';</script>"; exit; } else { $ques1 = $question[0]; if ($ques1['question_type'] == 1 || $ques1['question_type'] == 2) { $UA = WUserAnswers::getIDTextAnswer($ques1['id'], $oneUT['id'], $ques1['question_type']); //echo $UA;exit(); } else { $arrUA = WUserAnswers::getIDAnswer($ques1['id'], $oneUT['id']); } } $html = $this->renderPartial('/wTest/trainee/_doingtest', array('question' => $question, 'ua' => $UA, 'arrUA' => $arrUA, 'page_number' => $page_number, 'total' => $total, 'test_id' => $test_id, 'course_id' => $course_id, 'lecture_id' => $lecture_id)); echo $html; exit; }