<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\QuizRecord;
use App\Models\Students;
use App\Models\User;
use DB;
use Log;

class QuizRecordController extends Controller {

    public function quiz_records(Request $request) {
        try {
            $quiz_records = QuizRecord::where('date',$request->date)->get();
            return response()->json([ 'quiz_records' => $quiz_records], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }

    public function get_user_info( $id ) {
        try {
            $user = Students::where('student_id', $id)->first();
            return response()->json([ 'user' => $user], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }


    public function get_avg_time() {
        try {
            $QuizRecord = QuizRecord::get();
            $QuizRecordCount = QuizRecord::count();
            $duration = 0;
            $avg_time = 0;
            foreach ($QuizRecord as $item) {
                $duration += $item->duration;
            }
            $avg_time = $duration/$QuizRecordCount;

            response()->json(  $avg_time );
            // return response()->json([ 'avg_time' => $avg_time], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
        
        //return response()->json( $players_count );
    }


}
