reduced database queries when loading home page and added time limit and date completed to home page

This commit is contained in:
mwalbeck 2016-09-28 15:26:43 +02:00
parent 7d160134b8
commit d4d8801f8b
4 changed files with 19 additions and 6 deletions

View file

@ -90,9 +90,19 @@ class Test extends Model
public function timePassed($start_time)
{
if ($start_time+3600 < time()) {
if ($start_time+$this->time_limit < time()) {
return true;
}
return false;
}
public function timeLimitInMinutes()
{
return $this->time_limit/60;
}
public function timeLimitInHours()
{
return $this->time_limit/60/60;
}
}

View file

@ -178,6 +178,7 @@ class User extends Authenticatable
public function getTests()
{
$test_general = $this->getGeneralTests();
$testdetails = $this->testdetails;
if ($this->group_id > 1) {
$test_group = $this->group->getGroupTests();
@ -190,7 +191,7 @@ class User extends Authenticatable
$completed_tests = [];
foreach ($unsorted_tests as $test) {
$testdetail = $test->testdetails()->where("user_id", $this->id)->first();
$testdetail = $testdetails->where("test_id", $test->id)->first();
if ($testdetail && $testdetail->passed) {
$completed_tests[] = $test;
} else {
@ -198,7 +199,7 @@ class User extends Authenticatable
}
}
$tests = [$available_tests, $completed_tests];
$tests = [$available_tests, $completed_tests, $testdetails];
return $tests;
}

View file

@ -11,12 +11,14 @@
<tr>
<th>Test</th>
<th>Nr. of Questions</th>
<th>Time limit</th>
<th></th>
</tr>
@foreach ($tests[0] as $test)
<tr>
<td>{{ $test->title }}</td>
<td>{{ $test->question_count }}</td>
<td>{{ $test->timeLimitInMinutes() }} min.</td>
<td>
<form method="get" class="pull-right">
<button class="btn btn-xs btn-default pull-left" formaction="/test/{{ $test->id }}">Start</button>
@ -40,7 +42,7 @@
<tr>
<td>{{ $test->title }}</td>
<td>{{ $test->question_count }}</td>
<td>Date</td>
<td>{{ $tests[2]->where("test_id", $test->id)->first()->last_passed }}</td>
</tr>
@endforeach
</table>

View file

@ -103,9 +103,9 @@ Route::group(['prefix' => 'admin'], function () {
});
/*\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
echo "<pre>";
var_dump($query->sql);
var_dump($query->time);
echo "</pre>";
});*/
});