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();
-
+