added full time limit functionality for tests
This commit is contained in:
parent
40aa77386b
commit
7d160134b8
7 changed files with 61 additions and 19 deletions
app
database
resources/views/tests
|
@ -15,7 +15,7 @@ class TestController extends Controller
|
|||
{
|
||||
public function startTest(Test $test)
|
||||
{
|
||||
if (null !== session("start_time") && session("start_time")+3600 > time()) {
|
||||
if (null !== session("start_time") && session("time_limit") !== 0 && session("start_time")+session("time_limit") > time()) {
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
|
@ -23,12 +23,16 @@ class TestController extends Controller
|
|||
//insert session flash
|
||||
return redirect('/home');
|
||||
}
|
||||
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 1, 'test' => $test, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false, 'start_time' => time()]);
|
||||
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 1, 'test' => $test, 'correct_answers' => 0, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false, 'start_time' => time(), 'time_limit' => $test->time_limit]);
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
public function showQuestion()
|
||||
{
|
||||
if (null === session("start_time") || session("time_limit") !== 0 && session("start_time")+session("time_limit") < time()) {
|
||||
return redirect()->action('HomeController@index');
|
||||
}
|
||||
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$options = $question->options->shuffle();
|
||||
session(['options' => $options]);
|
||||
|
@ -56,34 +60,47 @@ class TestController extends Controller
|
|||
}
|
||||
if ($user_answers_correct == $correct_answers) {
|
||||
session(['is_correct' => true]);
|
||||
session(['correct_answers' => session('correct_answers')+1]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$question->multiple_answers_question) {
|
||||
if ($answers[0]->correct_answer) {
|
||||
session(['is_correct' => true]);
|
||||
session(['correct_answers' => session('correct_answers')+1]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
session(['has_failed' => true]);
|
||||
}
|
||||
|
||||
if (session('test')->lastQuestion(session('question_counter'))) {
|
||||
session(['last_question' => true]);
|
||||
}
|
||||
|
||||
if (session('test')->timePassed(session('start_time'))) {
|
||||
session(['time_passed' => true]);
|
||||
session(["wrong_answers" => session("test")->question_count-session("correct_answers")]);
|
||||
}
|
||||
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
session(['has_failed' => true]);
|
||||
}
|
||||
session(['question_counter' => session('question_counter')+1]);
|
||||
return redirect()->action('TestController@showAnswer');
|
||||
}
|
||||
|
||||
public function showAnswer()
|
||||
{
|
||||
if (!session("time_passed")) {
|
||||
if (null === session("start_time") || session("time_limit") !== 0 && session("start_time")+session("time_limit") < time()) {
|
||||
return redirect()->action('HomeController@index');
|
||||
}
|
||||
}
|
||||
|
||||
$question = session('questions')->get(session('question_counter')-2);
|
||||
$options = session('options');
|
||||
return view('tests.answer', compact("question"), compact("options"));
|
||||
|
|
18
app/Test.php
18
app/Test.php
|
@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Auth;
|
|||
|
||||
class Test extends Model
|
||||
{
|
||||
protected $fillable = ["title", "question_count", "question_count_to_fail"];
|
||||
protected $fillable = ["title", "question_count", "question_count_to_fail, time_limit"];
|
||||
|
||||
public function questions()
|
||||
{
|
||||
|
@ -52,6 +52,7 @@ class Test extends Model
|
|||
$this->title = $request["title"];
|
||||
$this->question_count = $request["question_count"];
|
||||
$this->question_count_to_fail = $request["question_count_to_fail"];
|
||||
$this->time_limit = $request["time_limit"];
|
||||
if (Auth::user()->isAdministrator) {
|
||||
$this->group_id = $request["group_id"];
|
||||
$this->save();
|
||||
|
@ -81,14 +82,17 @@ class Test extends Model
|
|||
return true;
|
||||
}
|
||||
|
||||
public function correctAnswers($question_counter, $wrong_answers)
|
||||
{
|
||||
return $question_counter - $wrong_answers;
|
||||
}
|
||||
|
||||
public function sessionPurge()
|
||||
{
|
||||
session(['questions' => null, 'question_counter' => null, 'test' => null, 'wrong_answers' => null, 'is_correct' => null, 'has_failed' => null, 'last_question' => null, 'options' => null]);
|
||||
session(['questions' => null, 'question_counter' => null, 'test' => null, 'wrong_answers' => null, 'correct_answers' => null, 'is_correct' => null, 'has_failed' => null, 'last_question' => null, 'options' => null, 'start_time' => null, 'time_limit' => null]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function timePassed($start_time)
|
||||
{
|
||||
if ($start_time+3600 < time()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -217,7 +217,7 @@ class User extends Authenticatable
|
|||
{
|
||||
$testdetails = new Testdetail;
|
||||
$testdetails->test_id = $test->id;
|
||||
$testdetails->correct_answers = $test->correctAnswers(session("question_counter"), session("wrong_answers"));
|
||||
$testdetails->correct_answers = session("correct_answers");
|
||||
|
||||
if ($has_failed == true) {
|
||||
$testdetails->fails += 1;
|
||||
|
@ -236,7 +236,7 @@ class User extends Authenticatable
|
|||
public function updateTestdetails($test, $has_failed)
|
||||
{
|
||||
$testdetails = $this->testdetails->where("test_id", $test->id)->first();
|
||||
$testdetails->correct_answers = $test->correctAnswers(session("question_counter"), session("wrong_answers"));
|
||||
$testdetails->correct_answers = session("correct_answers");
|
||||
|
||||
if ($has_failed == true) {
|
||||
$testdetails->fails += 1;
|
||||
|
|
|
@ -31,8 +31,8 @@ $factory->define(App\Test::class, function (Faker\Generator $faker) {
|
|||
return [
|
||||
'title' => $faker->sentence(3, true),
|
||||
'question_count' => 6,
|
||||
'question_count_to_fail' => 2
|
||||
|
||||
'question_count_to_fail' => 2,
|
||||
'time_limit' => 3600
|
||||
];
|
||||
});
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ class CreateTestsTable extends Migration
|
|||
$table->integer('group_id')->unsigned()->index()->nullable()->default(1);
|
||||
$table->integer('question_count')->unsigned();
|
||||
$table->integer('question_count_to_fail')->unsigned()->nullable();
|
||||
$table->integer('time_limit')->unsigned;
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -20,6 +20,16 @@
|
|||
<label>Number of Wrong Questions Allowed</label>
|
||||
<input type="text" class="form-control" name="question_count_to_fail" value="{{ $test->question_count_to_fail }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Time Limit</label>
|
||||
<select class="form-control" name="time_limit">
|
||||
<option value="0">No time limit</option>
|
||||
<option value="1800">30 Min</option>
|
||||
<option value="3600">60 Min</option>
|
||||
<option value="5400">90 Min</option>
|
||||
<option value="7200">120 Min</option>
|
||||
</select>
|
||||
</div>
|
||||
@if (Auth::user()->isAdministrator())
|
||||
<div class="form-group">
|
||||
<label>Group</label>
|
||||
|
|
|
@ -20,6 +20,16 @@
|
|||
<label>Number of Wrong Questions Allowed</label>
|
||||
<input type="text" class="form-control" name="question_count_to_fail">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Time Limit</label>
|
||||
<select class="form-control" name="time_limit">
|
||||
<option value="0">No time limit</option>
|
||||
<option value="1800">30 Min</option>
|
||||
<option value="3600">60 Min</option>
|
||||
<option value="5400">90 Min</option>
|
||||
<option value="7200">120 Min</option>
|
||||
</select>
|
||||
</div>
|
||||
@if (Auth::user()->isAdministrator())
|
||||
<div class="form-group">
|
||||
<label>Group</label>
|
||||
|
|
Reference in a new issue