public function actionViewSoalToeflTest($id)
 {
     //Koneksi ke Database
     $con = mysqli_connect('localhost', 'root', '', 'toefl');
     // Check connection
     if (mysqli_connect_errno()) {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
     $result = mysqli_query($con, "SELECT * FROM t_modultest where id_toefl = " . $id . " LIMIT 1");
     while ($row = mysqli_fetch_array($result)) {
         $idModulTest = $row['id_modulTest'];
         //idModulTest = 15
     }
     $hasil = Modultestsoal::model()->findAllByAttributes(array('id_modultest' => $idModulTest));
     $soalToefl = new Modultestsoal('search');
     $soalToefl->id_modultest = $idModulTest;
     //Hitung Score
     $kosong = 0;
     if ($_POST['Submit_jawaban_sementara']) {
         if (count($_POST['jawaban']) < 1) {
             echo "Anda belum menjawab soal satupun ";
         } else {
             $Jawaban = "";
             $nomor = 1;
             //var_dump($_POST['NomorSoal']);
             //echo "*";
             for ($i = 1; $i <= $_POST['NomorSoal']; $i++) {
                 if ($_POST['jawaban']["{$i}"] == NULL) {
                     $Jawaban .= "#";
                     $kosong += 1;
                 } else {
                     $Jawaban .= $_POST['jawaban']["{$i}"];
                 }
             }
             // echo $Jawaban ."<BR>";
             // echo $id."<BR>";
             $encode = Soal::model()->getJawabanToefl($idModulTest);
             //echo "<br>";
             //           var_dump($encode);
             $count = 0;
             $wrong = 0;
             for ($i = 0; $i < $_POST['NomorSoal']; $i++) {
                 //echo $_POST['jawaban'][$i];
                 //echo $encode[$i]["jawaban"];
                 //echo "*";
                 if ($_POST['jawaban'][$i + 1] == $encode[$i]["jawaban"]) {
                     //echo "<br>", $_POST['jawaban']["$i"]," dan ",$encode[$i]["jawaban"];
                     $count += 1;
                 } else {
                     $wrong += 1;
                 }
             }
             //var_dump($encode);
         }
     }
     //$nilai = (100 / $_POST['NomorSoal']) * $count;
     // echo "<br>",$nilai;
     //$name = Yii::app()->user->name;
     //$koneksi_server = mysqli_connect('localhost', 'root', '', 'toefl');
     //if (mysqli_connect_errno()) {
     //    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     //}
     //mysqli_query($koneksi_server, "INSERT INTO t_grade(username,jawaban,grade)
     //        VALUES ('$name', '$Jawaban','$nilai')");
     //mysqli_close($koneksi_server);
     //}
     //Table Nilai
     $ScoreSesi1 = array(24, 25, 26, 27, 28, 29, 30, 31, 32, 32, 33, 35, 37, 38, 39, 41, 41, 42, 43, 44, 45, 45, 46, 47, 47, 48, 48, 49, 49, 50, 51, 51, 52, 52, 53, 54, 54, 55, 56, 57, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 68);
     $ScoreSesi2 = array(20, 20, 21, 22, 23, 25, 26, 27, 29, 31, 33, 35, 36, 37, 38, 40, 40, 41, 43, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 63, 65, 67, 68);
     $ScoreSesi3 = array(21, 22, 23, 23, 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 46, 47, 48, 48, 49, 50, 51, 52, 52, 53, 54, 54, 55, 56, 57, 58, 59, 60, 61, 63, 65, 66, 67);
     $Score = Yii::app()->user->getState('Score');
     if (!isset($_POST['Submit_jawaban_sementara'])) {
         $result = mysqli_query($con, "SELECT * FROM t_modultest where id_toefl = " . $id);
         $UrutanTemp = 0;
         $UrutanModul = 0;
         $NextModul = 0;
         while ($row = mysqli_fetch_array($result)) {
             $UrutanTemp++;
             if ($UrutanModul != 0) {
                 $NextModul = $row['id_modulTest'];
                 break;
             }
             if ($row['id_modulTest'] == $idModulTest) {
                 $UrutanModul = $UrutanTemp;
             }
         }
         $this->render('ViewSoalToeflSementara', array('model' => $soalToefl, 'hasil' => $hasil, 'NextModul' => $NextModul, 'UrutanModul' => $UrutanModul));
     } else {
         if (isset($_POST['Submit_jawaban_sementara'])) {
             $NextModul = $_POST['NextModul'];
             $UrutanModul = $_POST['UrutanModul'];
             if ($UrutanModul == 2) {
                 $Score += $ScoreSesi1[$count];
                 Yii::app()->user->setState('Score', $Score);
                 $hasil = Modultestsoal::model()->findAllByAttributes(array('id_modultest' => $NextModul));
                 $soalToefl = new Modultestsoal('search');
                 $soalToefl->id_modultest = $NextModul;
                 $result = mysqli_query($con, "SELECT * FROM t_modultest where id_modulTest = " . $NextModul);
                 while ($row = mysqli_fetch_array($result)) {
                     $idToefl = $row['id_toefl'];
                 }
                 $result = mysqli_query($con, "SELECT * FROM t_modultest where id_toefl = " . $idToefl);
                 $UrutanTemp2 = 0;
                 $UrutanModul2 = 0;
                 $NextModul2 = 0;
                 while ($row = mysqli_fetch_array($result)) {
                     $UrutanTemp2++;
                     if ($UrutanModul2 != 0) {
                         $NextModul2 = $row['id_modulTest'];
                         break;
                     }
                     if ($row['id_modulTest'] == $NextModul) {
                         $UrutanModul2 = $UrutanTemp2;
                     }
                 }
                 $this->render('ViewSoalToeflSementara', array('model' => $soalToefl, 'hasil' => $hasil, 'NextModul' => $NextModul2, 'UrutanModul' => $UrutanModul2));
             } else {
                 if ($UrutanModul == 3) {
                     $Score += $ScoreSesi2[$count];
                     Yii::app()->user->setState('Score', $Score);
                     $hasil = Modultestsoal::model()->findAllByAttributes(array('id_modultest' => $NextModul));
                     $soalToefl = new Modultestsoal('search');
                     $soalToefl->id_modultest = $NextModul;
                     $result = mysqli_query($con, "SELECT * FROM t_modultest where id_modulTest = " . $NextModul);
                     while ($row = mysqli_fetch_array($result)) {
                         $idToefl = $row['id_toefl'];
                     }
                     $result = mysqli_query($con, "SELECT * FROM t_modultest where id_toefl = " . $idToefl);
                     $UrutanTemp2 = 0;
                     $UrutanModul2 = 0;
                     $NextModul2 = 0;
                     while ($row = mysqli_fetch_array($result)) {
                         $UrutanTemp2++;
                         if ($UrutanModul2 != 0) {
                             $NextModul2 = $row['id_modulTest'];
                             break;
                         }
                         if ($row['id_modulTest'] == $NextModul) {
                             $UrutanModul2 = $UrutanTemp2;
                         }
                     }
                     $this->render('ViewSoalToeflSementara', array('model' => $soalToefl, 'hasil' => $hasil, 'NextModul' => $NextModul2, 'UrutanModul' => $UrutanModul2));
                 } else {
                     $Score += $ScoreSesi3[$count];
                     $SS = $Score;
                     $Score /= 3;
                     $Score *= 10;
                     Yii::app()->user->setState('Score', $Score);
                     $name = Yii::app()->user->name;
                     mysqli_query($con, "INSERT INTO t_grade(username,jawaban,grade)\n                    VALUES ('{$name}', '{$Jawaban}','{$Score}')");
                     $result = mysqli_query($con, "SELECT * FROM t_grade WHERE username = '******'");
                     while ($row = mysqli_fetch_array($result)) {
                         $idGrade = $row['id_grade'];
                     }
                     mysqli_query($con, "INSERT INTO t_toeflgrade(id_toefl, id_grade)\n                    VALUES ({$id}, {$idGrade})");
                     $this->render('HasilToefl', array('wew' => $Score, 'wow' => $idGrade, 'waw' => $id, 'wuw' => $count, 'dat' => $result));
                 }
             }
         }
     }
     mysqli_close($con);
 }
 public function actionSoalToefl($id)
 {
     $rnd = rand(0, 9999);
     $inputSoal = new InputSoal();
     if (isset($_POST['InputSoal'])) {
         $inputSoal->attributes = $_POST['InputSoal'];
         if ($inputSoal->validate()) {
             //create a file model
             $file = new File();
             $uploadedFile = CUploadedFile::getInstance($inputSoal, 'file');
             $fileName = "{$rnd}-{$uploadedFile}";
             $file->file = $fileName;
             $file->jenis = $inputSoal->jenis;
             if ($file->save()) {
                 //if(!empty($uploadedFile))
                 //$uploadedFile=CUploadedFile::getInstance($file,'file');
                 if (isset($uploadedFile)) {
                     if ($inputSoal->jenis == 'L') {
                         $uploadedFile->saveAs(Yii::app()->basePath . '/../music/test/' . $fileName);
                     } else {
                         if ($inputSoal->jenis == 'R') {
                             $uploadedFile->saveAs(Yii::app()->basePath . '/../document/test/' . $fileName);
                         }
                     }
                 }
                 $soal = new Soal();
                 $soal->attributes = $inputSoal->attributes;
                 $soal->id_file = $file->id_file;
                 if ($soal->save()) {
                     $kuissoal = new Modultestsoal();
                     $kuissoal->id_modultest = $id;
                     $kuissoal->id_soal = $soal->id_soal;
                     if ($kuissoal->save()) {
                         //redirect to index page
                         //Koneksi ke Database
                         $con = mysqli_connect('localhost', 'root', '', 'toefl');
                         // Check connection
                         if (mysqli_connect_errno()) {
                             echo "Failed to connect to MySQL: " . mysqli_connect_error();
                         }
                         $result = mysqli_query($con, "SELECT * FROM t_modultest where id_modulTest = " . $id);
                         while ($row = mysqli_fetch_array($result)) {
                             $idToefl = $row['id_toefl'];
                             //idModulTest = 15
                         }
                         $this->redirect(array('modulTest/viewSoalToeflTest', 'id' => $idToefl));
                     } else {
                         //get the error message
                         $inputSoal->addErrors($soal->getErrors());
                     }
                 } else {
                     //get the error message
                     $inputSoal->addErrors($soal->getErrors());
                 }
             } else {
                 //get the error message
                 $inputSoal->addErrors($soal->getErrors());
             }
         } else {
             //get the error message
             $inputSoal->addErrors($file->getErrors());
         }
     }
     //display input soal-form
     $this->render('Soal', array('model' => $inputSoal));
 }