users are now only showns general tests and tests from their groups and test info is now stored in testdetails or failure or completion
This commit is contained in:
parent
a3dca2142e
commit
8fb7e8576f
10 changed files with 121 additions and 13 deletions
app
database/migrations
2014_10_12_000000_create_users_table.php2016_08_30_201515_create_tests_table.php2016_08_31_145706_create_testdetails_table.php
resources/views/tests
routes
|
@ -3,6 +3,7 @@
|
|||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Test;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
|
@ -15,4 +16,9 @@ class Group extends Model
|
|||
{
|
||||
return $this->hasMany(Test::class);
|
||||
}
|
||||
|
||||
public function getGroupTests()
|
||||
{
|
||||
return $this->tests;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ class HomeController extends Controller
|
|||
*/
|
||||
public function index()
|
||||
{
|
||||
$tests = Test::all();
|
||||
$tests = Auth::user()->getTests();
|
||||
return view('home', compact('tests'));
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ use Illuminate\Http\Request;
|
|||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Requests;
|
||||
use App\Test;
|
||||
use App\Testdetail;
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
|
@ -15,12 +16,13 @@ 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]);
|
||||
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 0, 'test' => $test, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false]);
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
public function showQuestion()
|
||||
{
|
||||
session(['question_counter' => session('question_counter')+1]);
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$options = $question->options->shuffle();
|
||||
session(['options' => $options]);
|
||||
|
@ -59,7 +61,20 @@ class TestController extends Controller
|
|||
{
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$options = session('options');
|
||||
session(['question_counter' => session('question_counter')+1]);
|
||||
return view('tests.answer', compact("question"), compact("options"));
|
||||
}
|
||||
|
||||
public function testRetry(Test $test)
|
||||
{
|
||||
Auth::user()->storeTestdetails($test, session("has_failed"));
|
||||
$test->sessionPurge();
|
||||
return redirect("/test/$test->id");
|
||||
}
|
||||
|
||||
public function testEnd(Test $test)
|
||||
{
|
||||
Auth::user()->storeTestdetails($test, session("has_failed"));
|
||||
$test->sessionPurge();
|
||||
return redirect("/home");
|
||||
}
|
||||
}
|
||||
|
|
13
app/Test.php
13
app/Test.php
|
@ -19,6 +19,8 @@ class Test extends Model
|
|||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function randomizeQuestions()
|
||||
{
|
||||
return $this->questions->random($this->question_count)->shuffle();
|
||||
|
@ -72,4 +74,15 @@ class Test extends Model
|
|||
$this->delete();
|
||||
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]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
80
app/User.php
80
app/User.php
|
@ -5,6 +5,7 @@ namespace App;
|
|||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use App\Group;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
|
@ -38,9 +39,9 @@ class User extends Authenticatable
|
|||
return $this->hasMany(Testdetail::class);
|
||||
}
|
||||
|
||||
public function company()
|
||||
public function group()
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
return $this->belongsTo(Group::class);
|
||||
}
|
||||
|
||||
public function passwordHash($password)
|
||||
|
@ -164,10 +165,81 @@ class User extends Authenticatable
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!$this->testdetails()->where('test_id', $test_id)->first()->passed == 1) {
|
||||
if (!$this->testdetails()->where('test_id', $test_id)->first()->passed >= 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function getGeneralTests()
|
||||
{
|
||||
return Test::where("group_id", 0)->get();
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
$test_general = $this->getGeneralTests();
|
||||
|
||||
if (!$this->group_id) {
|
||||
return $test_general;
|
||||
}
|
||||
|
||||
$test_group = $this->group->getGroupTests();
|
||||
|
||||
if ($test_group == null) {
|
||||
return $test_general;
|
||||
}
|
||||
|
||||
return $test_group->merge($test_general);
|
||||
}
|
||||
|
||||
public function storeTestdetails($test, $has_failed)
|
||||
{
|
||||
if (empty($this->testdetails->where("test_id", $test->id)->first())) {
|
||||
$this->addTestdetails($test, $has_failed);
|
||||
} else {
|
||||
$this->updateTestdetails($test, $has_failed);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addTestdetails($test, $has_failed)
|
||||
{
|
||||
$testdetails = new Testdetail;
|
||||
$testdetails->test_id = $test->id;
|
||||
$testdetails->correct_answers = $test->correctAnswers(session("question_counter"), session("wrong_answers"));
|
||||
|
||||
if ($has_failed == true) {
|
||||
$testdetails->fails += 1;
|
||||
}
|
||||
|
||||
if ($has_failed == false) {
|
||||
$testdetails->passed = 1;
|
||||
$testdetails->passes += 1;
|
||||
$testdetails->last_passed = date("Y-m-d H:i:s");
|
||||
}
|
||||
|
||||
$this->testdetails()->save($testdetails);
|
||||
return true;
|
||||
}
|
||||
|
||||
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"));
|
||||
|
||||
if ($has_failed == true) {
|
||||
$testdetails->fails += 1;
|
||||
}
|
||||
|
||||
if ($has_failed == false) {
|
||||
$testdetails->passed = 1;
|
||||
$testdetails->passes += 1;
|
||||
$testdetails->last_passed = date("Y-m-d H:i:s");
|
||||
}
|
||||
|
||||
$testdetails->update();
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ class CreateUsersTable extends Migration
|
|||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->string('password');
|
||||
$table->integer('group_id')->unsigned()->index()->nullable();
|
||||
$table->integer('group_id')->unsigned()->index()->nullable()->default(0);
|
||||
$table->integer('enabled')->unsigned()->default(1);
|
||||
$table->integer('access_level')->unsigned()->default(1);
|
||||
$table->rememberToken();
|
||||
|
|
|
@ -16,7 +16,7 @@ class CreateTestsTable extends Migration
|
|||
Schema::create('tests', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('title');
|
||||
$table->integer('group_id')->unsigned()->index()->nullable();
|
||||
$table->integer('group_id')->unsigned()->index()->nullable()->default(0);
|
||||
$table->integer('question_count')->unsigned();
|
||||
$table->integer('question_count_to_fail')->unsigned()->nullable();
|
||||
$table->timestamps();
|
||||
|
|
|
@ -17,7 +17,7 @@ class CreateTestdetailsTable extends Migration
|
|||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned()->index();
|
||||
$table->integer('test_id')->unsigned()->index();
|
||||
$table->integer('passed')->unsigned();
|
||||
$table->integer('passed')->unsigned()->default(0);
|
||||
$table->integer('correct_answers')->unsigned();
|
||||
$table->integer('fails')->unsigned()->nullable();
|
||||
$table->integer('passes')->unsigned()->nullable();
|
||||
|
|
|
@ -21,10 +21,10 @@
|
|||
@endif
|
||||
@endforeach
|
||||
@if (session("has_failed") == true)
|
||||
<button type="submit" class="btn btn-default" formaction="/test/{{ $question->test_id }}">Retry</button>
|
||||
<button type="submit" class="btn btn-default" formaction="/home">End</button>
|
||||
<button type="submit" class="btn btn-default" formaction="/test/{{ $question->test_id }}/retry">Retry</button>
|
||||
<button type="submit" class="btn btn-default" formaction="/test/{{ $question->test_id }}/end">End</button>
|
||||
@elseif (session("last_question") == true)
|
||||
<button type="submit" class="btn btn-default" formaction="/home">End</button>
|
||||
<button type="submit" class="btn btn-default" formaction="/test/{{ $question->test_id }}/end">End</button>
|
||||
@else
|
||||
<button type="submit" class="btn btn-default" formaction="/test/question">Next</button>
|
||||
@endif
|
||||
|
|
|
@ -25,6 +25,8 @@ Route::get('/test/question', 'TestController@showQuestion');
|
|||
Route::post('/test/question', 'TestController@answerQuestion');
|
||||
Route::get('/test/answer', 'TestController@showAnswer');
|
||||
Route::get('/test/{test}', 'TestController@startTest');
|
||||
Route::get('/test/{test}/retry', 'TestController@testRetry');
|
||||
Route::get('/test/{test}/end', 'TestController@testEnd');
|
||||
|
||||
Route::get('/admin', 'AdminController@index');
|
||||
|
||||
|
|
Reference in a new issue