266 lines
No EOL
6.2 KiB
PHP
266 lines
No EOL
6.2 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use App\Group;
|
|
use App\Testdetail;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name', 'email', 'password',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password', 'remember_token',
|
|
];
|
|
|
|
public function answers()
|
|
{
|
|
return $this->hasMany(Answer::class);
|
|
}
|
|
|
|
public function testdetails()
|
|
{
|
|
return $this->hasMany(Testdetail::class);
|
|
}
|
|
|
|
public function group()
|
|
{
|
|
return $this->belongsTo(Group::class);
|
|
}
|
|
|
|
public function passwordHash($password)
|
|
{
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
public function passwordCompare($password1, $password2)
|
|
{
|
|
return $password1 == $password2;
|
|
}
|
|
|
|
public function passwordVerify($password, $hash)
|
|
{
|
|
return password_verify($password, $hash);
|
|
}
|
|
|
|
public function isModerator()
|
|
{
|
|
if ($this->access_level == 2) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function isAdministrator()
|
|
{
|
|
if ($this->access_level == 3) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Function for adding a user.
|
|
*
|
|
*/
|
|
public function addUser($request)
|
|
{
|
|
$this->name = $request["name"];
|
|
$this->email = $request["email"];
|
|
$this->passwordHash($request["password"]);
|
|
$this->enabled = $request["enabled"];
|
|
if (Auth::user()->isModerator()) {
|
|
Group::find(Auth::user()->group_id)->tests()->save($this);
|
|
return true;
|
|
}
|
|
$this->group_id = $request["group_id"];
|
|
$this->save();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Function for deleting user. Checks if it is the user itself,
|
|
* a moderator in the same group or an administrator.
|
|
* If this is not case, the user will not be deleted.
|
|
*
|
|
*/
|
|
public function deleteUser()
|
|
{
|
|
if ($this->id == Auth::user()->id) {
|
|
$this->delete();
|
|
return true;
|
|
}
|
|
|
|
if (Auth::user()->group_id == $this->group_id && Auth::user()->isModerator) {
|
|
$this->delete();
|
|
return true;
|
|
}
|
|
|
|
if (Auth::user()->isAdministrator()) {
|
|
$this->delete();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Functions for a user to update his/her email and password
|
|
*
|
|
*/
|
|
public function updateEmail($request)
|
|
{
|
|
$email = $request['email'];
|
|
|
|
$email = trim($email);
|
|
$this->email = $email;
|
|
|
|
$this->update();
|
|
return true;
|
|
}
|
|
|
|
public function updatePassword($request)
|
|
{
|
|
$password_old = $request['password_old'];
|
|
$password1_new = $request['password1_new'];
|
|
$password2_new = $request['password2_new'];
|
|
|
|
if (!$this->passwordVerify($password_old, $this->password)) {
|
|
return false;
|
|
}
|
|
|
|
if (!$this->passwordCompare($password1_new, $password2_new)) {
|
|
return false;
|
|
}
|
|
|
|
$this->passwordHash($password1_new);
|
|
$this->update();
|
|
return true;
|
|
}
|
|
|
|
public function testTaken($test_id)
|
|
{
|
|
if (!$this->testdetails()->where('test_id', $test_id)->first()) {
|
|
return false;
|
|
}
|
|
|
|
if (!$this->testdetails()->where('test_id', $test_id)->first()->passed >= 1) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getGeneralTests()
|
|
{
|
|
return Test::where("group_id", 1)->get();
|
|
}
|
|
|
|
public function getTests()
|
|
{
|
|
$test_general = $this->getGeneralTests();
|
|
$testdetails = $this->testdetails;
|
|
|
|
if ($this->group_id > 1) {
|
|
$test_group = $this->group->getGroupTests();
|
|
$unsorted_tests = $test_group->merge($test_general);
|
|
} else {
|
|
$unsorted_tests = $test_general;
|
|
}
|
|
|
|
$available_tests = [];
|
|
$completed_tests = [];
|
|
|
|
foreach ($unsorted_tests as $test) {
|
|
$testdetail = $testdetails->where("test_id", $test->id)->first();
|
|
if ($testdetail && $testdetail->passed) {
|
|
$completed_tests[] = $test;
|
|
} else {
|
|
$available_tests[] = $test;
|
|
}
|
|
}
|
|
|
|
$tests = [$available_tests, $completed_tests, $testdetails];
|
|
return $tests;
|
|
|
|
}
|
|
|
|
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 = session("correct_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 = session("correct_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;
|
|
}
|
|
|
|
public function getAdminPath()
|
|
{
|
|
if ($this->isModerator()) {
|
|
return "mod";
|
|
}
|
|
|
|
if ($this->isAdministrator()) {
|
|
return "admin";
|
|
}
|
|
}
|
|
} |