From 2d743e8546c4af3e993012a3891f32a1776b4a3b Mon Sep 17 00:00:00 2001 From: Mark Wane Date: Sun, 6 Oct 2019 08:53:19 +0100 Subject: [PATCH] Calculate and save score, then generate results page --- ajax.php | 26 +++++++++++----- classes/question.class.php | 2 +- classes/quiz.class.php | 60 ++++++++++++++++++++++++++++++++++++ index.php | 2 +- quiz.js | 34 +++++++++++--------- result.php | 63 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 163 insertions(+), 24 deletions(-) create mode 100644 result.php diff --git a/ajax.php b/ajax.php index 7dfcb34..9b7ef39 100644 --- a/ajax.php +++ b/ajax.php @@ -12,16 +12,26 @@ $questID_arr = Question::getList( $quiz->getID() ); $index_int = array_search( $quest->getID(), $questID_arr ); $index_int++; -$nextQuest = new Question( $questID_arr[ $index_int ] ); -$ans_arr = $nextQuest->getAnswers(); +if ( count( $questID_arr ) > $index_int ){ + $nextQuest = new Question( $questID_arr[ $index_int ] ); + $ans_arr = $nextQuest->getAnswers(); -$data_arr = array( - 'text' => $nextQuest->getText(), - 'ans' => array() -); + $data_arr = array( + 'done' => false, + 'text' => $nextQuest->getText(), + 'ans' => array() + ); -foreach( $ans_arr as $answer ){ - $data_arr['ans'][] = array( 'id' => $answer->getID(), 'text' => $answer->getText() ); + foreach( $ans_arr as $answer ){ + $data_arr['ans'][] = array( 'id' => $answer->getID(), 'text' => $answer->getText() ); + } +} else { + $accessKey_str = $quiz->saveResult( $_POST['user'] ); + + $data_arr = array( + 'done' => true, + 'result' => $accessKey_str + ); } header( "content-type: application/json" ); diff --git a/classes/question.class.php b/classes/question.class.php index b45e871..c1ec417 100644 --- a/classes/question.class.php +++ b/classes/question.class.php @@ -75,7 +75,7 @@ class Question extends Item { } /** - * Calulate user's score and save to DB + * Calculate user's score and save to DB * * @param int $userID_int ID of user * @param int[] $ans_arr Array of answer IDs diff --git a/classes/quiz.class.php b/classes/quiz.class.php index e15ce51..45eaed6 100644 --- a/classes/quiz.class.php +++ b/classes/quiz.class.php @@ -63,4 +63,64 @@ class Quiz extends Item { return $this->maxScore_int; } + /** + * Calculate and save the total score of a user + * + * @param int $userID_int $userID_int ID of user + * + * @return string Access key for results page + */ + public function saveResult( int $userID_int ){ + $db = DB::getDB(); + + $total_int = 0; + foreach( $this->questionID_arr as $questionID_int ){ + $question = new Question( $questionID_int ); + $total_int += $question->getScore( $userID_int ); + } + + $accessKey_str = hash( 'sha256', $this->id_int . $userID_int . microtime() ); + + $db->insert( + self::resultTb_str, + array( + 'quiz' => $this->id_int, + 'user' => $userID_int, + 'score' => $total_int, + 'access_key' => $accessKey_str + ) + ); + + return $accessKey_str; + } + + /** + * Get result for user and calculate average score + * + * @param string $acessKey_str Access key for result + * + * @return mixed[] Array of result data + */ + public static function getResult( string $accessKey_str ){ + $db = DB::getDB(); + + $rows_arr = $db->select( self::resultTb_str, array(), array( 'access_key' => $accessKey_str ) ); + + if ( empty( $rows_arr ) ){ + throw new \RuntimeException( 'Invalid access key' ); + } + + $result_arr = $rows_arr[0]; + + $rows_arr = $db->select( self::resultTb_str, array( 'score' ), array( 'quiz' => $result_arr['quiz'] ) ); + + $total_int = 0; + foreach ( $rows_arr as $row_arr ){ + $total_int += $row_arr['score']; + } + $result_arr['average'] = $total_int / count( $rows_arr ); + + return $result_arr; + } + } diff --git a/index.php b/index.php index 5b8402d..1bc8f64 100644 --- a/index.php +++ b/index.php @@ -56,7 +56,7 @@ $quizzes_arr = Quiz::getList(); - + diff --git a/quiz.js b/quiz.js index 5cac0df..00db78e 100644 --- a/quiz.js +++ b/quiz.js @@ -16,22 +16,28 @@ $( document ).ready( function() { quest : id.quest[ questNum - 1], ans : ans, } ).done( function( data ){ - questNum++; - $( "#question-text" ).text( data.text ); - $( "#answers" ).empty(); + if ( data.done ){ + // Send user to results page + window.location.replace( "result.php?key=" + data.result ); + } else { + // Display next question + questNum++; + $( "#question-text" ).text( data.text ); + $( "#answers" ).empty(); - $.each( data.ans, function( index, value ){ - $( "#answers" ).append( '\ -
\ - \ -
' ); - } ); + $.each( data.ans, function( index, value ){ + $( "#answers" ).append( '\ +
\ + \ +
' ); + } ); - let progress = questNum / id.quest.length * 100; - $( "#progress" ).text( questNum + ' / ' + id.quest.length ).prop( "style", "width: " + progress + "%" ); + let progress = questNum / id.quest.length * 100; + $( "#progress" ).text( questNum + ' / ' + id.quest.length ).prop( "style", "width: " + progress + "%" ); + } } ); } ); } ); diff --git a/result.php b/result.php new file mode 100644 index 0000000..b855cd5 --- /dev/null +++ b/result.php @@ -0,0 +1,63 @@ +getMaxScore(); + +$scorePer_num = $data_arr['score'] / $maxScore_int * 100; +$averagePer_num = $data_arr['average'] / $maxScore_int * 100; + +if ( $scorePer_num > $averagePer_num + 10 ){ + $barColour_str = "bg-success"; +} elseif ( $scorePer_num < $averagePer_num - 10 ){ + $barColour_str = "bg-danger"; +} else { + $barColour_str = "bg-warning"; +} + +?> + + + + + + Basic PHP Quiz + + + +
+
+
+

Results

+
+
+
+
+

Thank you, getName(); ?>

+
+
+
+
+

Your score

+
+
+
+
+
+

Average score

+
+
+
+
+
+
+ + + +