You are on page 1of 18

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style>
.error {
color:red;
}
</style>

<body>

<?php
//Step 1: Determine number of questions;
// $lastRow = $objPHPExcel->getActiveSheet()->getHighestRow();
// Step 2: Determine type of question;
//Step 3: Determine answer(s) based on type of question;
//Step 4: Display answer and scores.

function IsChecked($chkname,$value)
{
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
{
if($chkval == $value)

{
return true;
}
}
}
return false;
}

//================ Declare and limit reading

/** Include path **/


//ini_set("memory_limit", "-1");

set_include_path(get_include_path() . PATH_SEPARATOR . '../../Classes/');


/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
//

$inputFileType = 'Excel2007';

$inputFileName = './englishtest/dethithu.xls';

$sheetname = 'test1';
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;

private $_endRow = 0;

private $_columns = array();

public function __construct($startRow, $endRow, $columns) {


$this->_startRow

= $startRow;

$this->_endRow

= $endRow;

$this->_columns

= $columns;

public function readCell($column, $row, $worksheetName = '') {


if ($row >= $this->_startRow && $row <= $this->_endRow) {
if (in_array($column,$this->_columns)) {
return true;
}
}
return false;
}
}
$filterSubset = new MyReadFilter(1,70,range('A','S'));//Load tu hang 1 toi 70 trong
cot A toi S

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
//$objReader->setLoadSheetsOnly(array($sheetname,$sheetname1)) neu doc
multiple sheet
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);

$lastRow = $objPHPExcel->getActiveSheet()->getHighestRow();
lastrow
$qorder=array(); //order of question
$qtype = array(); // questions type array
$rquestion = array(); // questions array

// Read

$ranswers = array(); // row-answers array


$rightanswer = array(); // right answer array
$rerror=array(); //display error or not
//Run form 2 to lastrow - 2 is first place of question. This is for row.

if (isset($_POST['submit'])){
button is set

//open curl No 0. Check whether submit

for ($i = 2; $i <= $lastRow; $i++) {

//1st open curl

//read order of question


$qorder[$i]= $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0,$i)>getValue();

//Determine type of question


$qtype[$i]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1,$i)>getValue();
//1 equal to column 2; -2 due to fist qtype is $qtype[0]=$
//Read questions
$rquestion[$i]=$ranswers[$i][0]=$objPHPExcel->getActiveSheet()>getCellByColumnAndRow(2,$i)->getValue();
//Read answers
$ranswers[$i]=array(); //column answer array (1-4)
$ranswers[$i][0]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3,$i)>getValue(); //first answer
$ranswers[$i][1]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(4,$i)>getValue(); //second answer
$ranswers[$i][2]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(5,$i)>getValue(); //third answer -Cot F
$ranswers[$i][3]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(6,$i)>getValue(); //fourth answer
//display to screen
// display question order and question in bold
if ($qtype[$i]==1){

//open 2nd curl

if (!empty($ranswers[$i][0])){ //3rd curl

echo '<b>';
echo'' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
// display answers
echo '<input type="';
echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';
echo 'value=0>';
echo ' ' .$ranswers[$i][0];
echo '<b>';
} //third curl closing
//finish first question

if (!empty($ranswers[$i][1])){//3rd curl
echo '<b>';
echo ' '.$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';

echo 'value=1>';
echo ' '.$ranswers[$i][1];
echo '<b>';
//finish 2nd question
}//3rd curl closing

if (!empty($ranswers[$i][2])){//3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';
echo 'value=2>';
echo ' ' .$ranswers[$i][2];
echo '<b>';
//finish third question
}//3rd curl closing
if (!empty($ranswers[$i][3])){//3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';
echo 'value=3>';
echo ' ' .$ranswers[$i][3];
echo '<b>';
//finish forth question
}//3rd curl closing
}//2nd curl closing

if ($qtype[$i]==2){

//open 2nd curl

if (!empty($ranswers[$i][0])){ //3rd curl


echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
// display answers
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo ' " ';

echo 'value=0>';
echo ' ' .$ranswers[$i][0];
echo '<b>';

} //third curl closing


//finish first question
if (!empty($ranswers[$i][1])){ //3rd curl
echo '<b>';
echo ' '.$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo ' " ';
echo 'value=1>';
echo ' '.$ranswers[$i][1];
echo '<b>';
//finish 2nd question
} //3rd curl closing
if (!empty($ranswers[$i][2])){ //3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';

echo ' " ';


echo 'value=2>';
echo ' ' .$ranswers[$i][2];
echo '<b>';
//finish third question
} //3rd curl closing
if (!empty($ranswers[$i][3])){ //3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo 'value=3>';
echo ' ' .$ranswers[$i][3];
echo '<b>';
//finish forth question
}//3rd curl closing
}//2nd curl closing

//Read correct answer(s)


$rightanswer[$i]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(7,
$i)->getValue();
if ($qtype[$i]==1){ //2nd curl

if (isset($_POST[' qr. $qorder[$i]'])){


if($_POST[' qr. $qorder[$i]']== $rightanswer[$i]){
echo 'Ket qua dung';
}
Else {
Echo ' Ket qua sai ';
}
}
Else {
Echo '<class= "error">';
Echo 'Ban phai dua ra dap an';
Echo '</class>';
}
if ($qtype[$i]==2){ //2nd curl
if (isset($_POST[' qc. $qorder[$i]'])){
$aDoor = $_POST[' qc. $qorder[$i]'];
$N = count($aDoor);
$ketquac="";
for($j=0; $j < $N; $j++)
{
$ketquac=$ketquac .$aDoor[$j];
}
if($ketquac ==$rightanswer[$i]){
echo 'Ket qua dung';
}
Else {
Echo ' Ket qua sai ';
}
}
Else {

Echo '<class= "error">';


Echo 'Ban phai dua ra dap an';
Echo '</class>';

}
} //close 2nd curl

} //close first curl


} //close curl No 0

//=================================================
=check if not set
else{

echo '<form method="post" action="';


echo htmlspecialchars($_SERVER["PHP_SELF"]);
echo '">';

for ($i = 2; $i <= $lastRow; $i++) {

//1st open curl

//read order of question


$qorder[$i]= $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $i)>getValue();

//Determine type of question


$qtype[$i]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, $i)>getValue();
//1 equal to column 2; -2 due to fist qtype is $qtype[0]=$
//Read questions
$rquestion[$i]=$ranswers[$i][0]=$objPHPExcel->getActiveSheet()>getCellByColumnAndRow(2,$i)->getValue();
//Read answers
$ranswers[$i]=array(); //column answer array (1-4)
$ranswers[$i][0]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3,$i)>getValue(); //first answer
$ranswers[$i][1]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(4,$i)>getValue(); //second answer
$ranswers[$i][2]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(5,$i)>getValue(); //third answer -Cot F
$ranswers[$i][3]=$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(6,$i)>getValue(); //fourth answer
//display to screen
// display question order and question in bold
if ($qtype[$i]==1){

//open 2nd curl

if (!empty($ranswers[$i][0])){ //3rd curl


echo '<b>';
echo'' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
// display answers
echo '<input type="';
echo 'radio"';
echo 'name="qr'. $qorder[$i];

echo ' " ';


echo 'value=0>';
echo ' ' .$ranswers[$i][0];
echo '<b>';
} //third curl closing
//finish first question

if (!empty($ranswers[$i][1])){//3rd curl
echo '<b>';
echo ' '.$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';

echo 'value=1>';
echo ' '.$ranswers[$i][1];
echo '<b>';
//finish 2nd question
}//3rd curl closing

if (!empty($ranswers[$i][2])){//3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';

echo ' ' .$rquestion[$i];


echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';
echo 'value=2>';
echo ' ' .$ranswers[$i][2];
echo '<b>';
//finish third question
}//3rd curl closing
if (!empty($ranswers[$i][3])){//3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';


echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';
echo 'value=3>';
echo ' ' .$ranswers[$i][3];
echo '<b>';
//finish forth question
}//3rd curl closing

}//2nd curl closing

if ($qtype[$i]==2){

//open 2nd curl

if (!empty($ranswers[$i][0])){ //3rd curl


echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
// display answers
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo ' " ';

echo 'value=0>';
echo ' ' .$ranswers[$i][0];
echo '<b>';
} //third curl closing
//finish first question
if (!empty($ranswers[$i][1])){ //3rd curl
echo '<b>';
echo ' '.$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
echo '<input type="';

echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo ' " ';
echo 'value=1>';
echo ' '.$ranswers[$i][1];
echo '<b>';
//finish 2nd question
} //3rd curl closing
if (!empty($ranswers[$i][2])){ //3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo ' " ';
echo 'value=2>';
echo ' ' .$ranswers[$i][2];
echo '<b>';
//finish third question
} //3rd curl closing
if (!empty($ranswers[$i][3])){ //3rd curl
echo '<b>';
echo ' ' .$qorder[$i];
echo '.';

echo ' ' .$rquestion[$i];


echo '</b>';
echo '<br>';
echo '<input type="';
echo 'checkbox"';
echo 'name="qc'. $qorder[$i];
echo ' [] ';
echo 'value=3>';
echo ' ' .$ranswers[$i][3];
echo '<b>';
//finish forth question
}//3rd curl closing
}//2nd curl closing
}
echo '</form>';//het form

} //het check notset

//==============danh de thu
for ($i = 2; $i <= 4; $i++) {
echo '<input type="button" value="Xem ket qua">';
echo '<b>';
echo ' '.$qorder[$i];
echo '.';
echo ' ' .$rquestion[$i];
echo '</b>';
echo '<br>';

echo '<input type="';

echo 'radio"';
echo 'name="qr'. $qorder[$i];
echo ' " ';

echo 'value=1>';
echo ' '.$ranswers[$i][1];
echo '<b>';
}

?>

</body>
</html>

You might also like