Inital quiz page and answer submission
This commit is contained in:
parent
9e917e0f8a
commit
22d9a331ee
28
ajax.php
Normal file
28
ajax.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
use TestProject\Quiz;
|
||||
use TestProject\Question;
|
||||
require_once 'init.php';
|
||||
|
||||
$quiz = new Quiz( $_POST['quiz'] );
|
||||
$quest = new Question( $_POST['quest'] );
|
||||
|
||||
$quest->saveAnswer( $_POST['user'], $_POST['ans'] );
|
||||
|
||||
$questID_arr = Question::getList( $quiz->getID() );
|
||||
$index_int = array_search( $quest->getID(), $questID_arr );
|
||||
|
||||
$index_int++;
|
||||
$nextQuest = new Question( $index_int );
|
||||
$ans_arr = $nextQuest->getAnswers();
|
||||
|
||||
$data_arr = array(
|
||||
'text' => $nextQuest->getText(),
|
||||
'ans' => array()
|
||||
);
|
||||
|
||||
foreach( $ans_arr as $answer ){
|
||||
$data_arr['ans'][] = array( 'id' => $answer->getID(), 'text' => $answer->getText() );
|
||||
}
|
||||
|
||||
header( "content-type: application/json" );
|
||||
echo json_encode( $data_arr );
|
|
@ -5,8 +5,8 @@ namespace TestProject;
|
|||
* Question class
|
||||
*/
|
||||
class Question extends Item {
|
||||
private const tb_str = 'question';
|
||||
private const resulTb_str = 'question_result';
|
||||
private const tb_str = 'question';
|
||||
private const resultTb_str = 'question_result';
|
||||
|
||||
private $text_str;
|
||||
private $answers_arr;
|
||||
|
@ -73,4 +73,72 @@ class Question extends Item {
|
|||
public function getAnswers(){
|
||||
return $this->answers_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calulate user's score and save to DB
|
||||
*
|
||||
* @param int $userID_int ID of user
|
||||
* @param int[] $ans_arr Array of answer IDs
|
||||
*/
|
||||
public function saveAnswer( int $userID_int, $ans_arr ){
|
||||
$maxScore_int = 0;
|
||||
$score_int = 0;
|
||||
|
||||
foreach( $this->answers_arr as $answer ){
|
||||
if ( $answer->isCorrect() ){
|
||||
$maxScore_int++;
|
||||
|
||||
// User selected correct answer
|
||||
if ( in_array( $answer->getID(), $ans_arr ) ){
|
||||
$score_int++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply penalty if more answers selected than correct options
|
||||
$diff_int = count( $ans_arr ) - $maxScore_int;
|
||||
if ( $diff_int > 0 ){
|
||||
$score_int -= $diff_int;
|
||||
}
|
||||
// Do not allow a negative score
|
||||
if ( $score_int < 0 ){
|
||||
$score_int = 0;
|
||||
}
|
||||
|
||||
$db = DB::getDB();
|
||||
|
||||
// Check for existing answer
|
||||
$row_arr = $db->select( self::resultTb_str, array( 'id' ), array( 'user' => $userID_int, 'question' => $this->id_int ) );
|
||||
if ( empty( $row_arr ) ){
|
||||
$db->insert(
|
||||
self::resultTb_str,
|
||||
array(
|
||||
'user' => $userID_int,
|
||||
'question' => $this->id_int,
|
||||
'score' => $score_int
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$db->update( self::resultTb_str, array( 'score' => $score_int ), array( 'id' => $row_arr[0]['id'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user's score for the question
|
||||
*
|
||||
* @param int $userID_int ID of user
|
||||
*
|
||||
* @return int Score
|
||||
*/
|
||||
public function getScore( int $userID_int ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$row_arr = $db->select( self::resultTb_str, array( 'score' ), array( 'user' => $userID_int, 'question' => $this->id_int ) );
|
||||
if ( empty( $row_arr ) ){
|
||||
// Question not answered
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $row_arr[0]['score'];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace TestProject;
|
|||
*/
|
||||
class Quiz extends Item {
|
||||
private const tb_str = 'quiz';
|
||||
private const resulTb_str = 'quiz_result';
|
||||
private const resultTb_str = 'quiz_result';
|
||||
|
||||
private $name_str;
|
||||
private $maxScore_int;
|
||||
|
|
22
quiz.js
Normal file
22
quiz.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
$( document ).ready( function() {
|
||||
"use strict";
|
||||
|
||||
let questNum = 1;
|
||||
|
||||
$( "#question" ).on( "submit", function( event ){
|
||||
event.preventDefault();
|
||||
let ans = [];
|
||||
$( "[name='answer']:checked" ).each( function( index, element ){
|
||||
ans[ ans.length ] = element.value;
|
||||
} );
|
||||
|
||||
$.post( 'ajax.php', {
|
||||
user : id.user,
|
||||
quiz : id.quiz,
|
||||
quest : id.quest[ questNum - 1],
|
||||
ans : ans,
|
||||
} ).done( function( data ){
|
||||
console.log( data );
|
||||
} );
|
||||
} );
|
||||
} );
|
11
quiz.php
11
quiz.php
|
@ -7,11 +7,13 @@ require_once 'init.php';
|
|||
$user = User::find( $_POST['email'], $_POST['name'] );
|
||||
$quiz = new Quiz( $_POST['quiz'] );
|
||||
|
||||
$_SESSION['user_id'] = $user->getID();
|
||||
|
||||
$questID_arr = Question::getList( $quiz->getID() );
|
||||
|
||||
$quest1 = new Question( $questID_arr[0] );
|
||||
|
||||
$_SESSION['user_id'] = $user->getID();
|
||||
$progress_num = 1 / count( $questID_arr ) * 100;
|
||||
?>
|
||||
<html lang=en_GB>
|
||||
<head>
|
||||
|
@ -59,6 +61,13 @@ $_SESSION['user_id'] = $user->getID();
|
|||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="progress">
|
||||
<div class="progress-bar" role="progressbar" style="width: <?php echo $progress_num; ?>%">1 / <?php echo count( $questID_arr ); ?></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
|
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
|
||||
|
|
Loading…
Reference in a new issue