Commit 621c3a5c authored by ZeMKI's avatar ZeMKI

Code Optmization

this update is meant to be just a code styling and optimization.
parent bcd11dc9
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
......@@ -12,13 +12,15 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/dnoegel/php-xdg-base-dir" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/annotations" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/cache" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/dbal" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/event-manager" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/lexer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/dompdf/dompdf" />
<excludeFolder url="file://$MODULE_DIR$/vendor/dragonmantank/cron-expression" />
<excludeFolder url="file://$MODULE_DIR$/vendor/egulias/email-validator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/erusev/parsedown" />
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/flare-client-php" />
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/ignition" />
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/ignition-contracts" />
......@@ -33,12 +35,14 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/intervention/image" />
<excludeFolder url="file://$MODULE_DIR$/vendor/jakub-onderka/php-console-color" />
<excludeFolder url="file://$MODULE_DIR$/vendor/jakub-onderka/php-console-highlighter" />
<excludeFolder url="file://$MODULE_DIR$/vendor/jasonmccreary/laravel-test-assertions" />
<excludeFolder url="file://$MODULE_DIR$/vendor/justinrainbow/json-schema" />
<excludeFolder url="file://$MODULE_DIR$/vendor/kkszymanowski/traitor" />
<excludeFolder url="file://$MODULE_DIR$/vendor/laravel/framework" />
<excludeFolder url="file://$MODULE_DIR$/vendor/laravel/telescope" />
<excludeFolder url="file://$MODULE_DIR$/vendor/laravel/tinker" />
<excludeFolder url="file://$MODULE_DIR$/vendor/laravelcollective/html" />
<excludeFolder url="file://$MODULE_DIR$/vendor/league/commonmark" />
<excludeFolder url="file://$MODULE_DIR$/vendor/league/container" />
<excludeFolder url="file://$MODULE_DIR$/vendor/league/flysystem" />
<excludeFolder url="file://$MODULE_DIR$/vendor/maatwebsite/excel" />
......@@ -48,6 +52,7 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/mockery/mockery" />
<excludeFolder url="file://$MODULE_DIR$/vendor/monolog/monolog" />
<excludeFolder url="file://$MODULE_DIR$/vendor/moontoast/math" />
<excludeFolder url="file://$MODULE_DIR$/vendor/mpociot/laravel-test-factory-helper" />
<excludeFolder url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/nesbot/carbon" />
<excludeFolder url="file://$MODULE_DIR$/vendor/nette/utils" />
......@@ -68,6 +73,8 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/phploc/phploc" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpoffice/phpspreadsheet" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpoption/phpoption" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpseclib/bcmath_compat" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpseclib/phpseclib" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpstan/phpdoc-parser" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
......
......@@ -85,7 +85,6 @@
<path value="$PROJECT_DIR$/vendor/psr/simple-cache" />
<path value="$PROJECT_DIR$/vendor/dragonmantank/cron-expression" />
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
<path value="$PROJECT_DIR$/vendor/erusev/parsedown" />
<path value="$PROJECT_DIR$/vendor/doctrine/instantiator" />
<path value="$PROJECT_DIR$/vendor/doctrine/inflector" />
<path value="$PROJECT_DIR$/vendor/jakub-onderka/php-console-highlighter" />
......@@ -148,6 +147,14 @@
<path value="$PROJECT_DIR$/vendor/markbaker/matrix" />
<path value="$PROJECT_DIR$/vendor/markbaker/complex" />
<path value="$PROJECT_DIR$/vendor/symfony/error-handler" />
<path value="$PROJECT_DIR$/vendor/phpseclib/phpseclib" />
<path value="$PROJECT_DIR$/vendor/phpseclib/bcmath_compat" />
<path value="$PROJECT_DIR$/vendor/league/commonmark" />
<path value="$PROJECT_DIR$/vendor/doctrine/event-manager" />
<path value="$PROJECT_DIR$/vendor/doctrine/dbal" />
<path value="$PROJECT_DIR$/vendor/doctrine/cache" />
<path value="$PROJECT_DIR$/vendor/mpociot/laravel-test-factory-helper" />
<path value="$PROJECT_DIR$/vendor/jasonmccreary/laravel-test-assertions" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.2" />
......
......@@ -3,5 +3,6 @@
<component name="Symfony2PluginSettings">
<option name="directoryToWeb" value="public" />
<option name="pluginEnabled" value="true" />
<option name="lastServiceGeneratorLanguage" value="yaml" />
</component>
</project>
\ No newline at end of file
......@@ -3,18 +3,22 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Action extends Model
{
/**
* @var array
*/
protected $fillable = [
'user_id', 'url', 'name', 'description',
];
/**
* @return BelongsTo
*/
public function user()
{
return $this->belongsTo(\App\User::class);
return $this->belongsTo(User::class);
}
}
......@@ -4,6 +4,8 @@ namespace App\Exports;
use App\InterviewTokens;
use App\Study;
use App\Token;
use Illuminate\Support\Collection;
use Image;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
......@@ -15,11 +17,18 @@ class AllInterviewTokenExport implements FromCollection, WithMapping, WithHeadin
{
use Exportable;
/**
* AllInterviewTokenExport constructor.
* @param $id
*/
public function __construct($id)
{
$this->id = $id;
}
/**
* @return array
*/
public function headings(): array
{
return [
......@@ -35,77 +44,76 @@ class AllInterviewTokenExport implements FromCollection, WithMapping, WithHeadin
}
/**
* @param mixed $study
* @return array
* @var Interview $interview
*/
public function map($study): array
{
$printArray = [];
$indexForName = 0;
$count = 0;
foreach ($study->interviews as $emptyInterview) {
$allinterviews = InterviewTokens::where('interview_id', $emptyInterview->id)->get();
array_push($printArray,[$emptyInterview->interviewed]);
array_push($printArray, [$emptyInterview->interviewed]);
foreach ($allinterviews as $interview) {
if ($this->invalidData($interview)) continue;
if (isset($interview->valutation->percentagePosition)) {
$percentagePosition = "x: " . $interview->valutation->percentagePosition->x . " y:" . $interview->valutation->percentagePosition->y;
} else {
$percentagePosition = '';
}
if (is_object($interview->valutation->position)) {
$position = "x: " . $interview->valutation->position->x . " y:" . $interview->valutation->position->y;
} else {
$position = $interview->valutation->position;
}
if (is_array($interview->valutation->classifiers)) {
$classifiers = "";
foreach ($interview->valutation->classifiers as $classifier) {
$classifiers .= $classifier->basename . " - ";
}
}
$toprint = [
$interview->token_id,
\App\Token::where('id', $interview->token_id)->first()->name,
$interview->valutation->circle,
$interview->valutation->distance,
$interview->valutation->sorting,
$position,
$percentagePosition,
$classifiers
];
$this->formatTokenValuesForExport($interview, $toprint);
array_push($printArray, $toprint);
}
//array_unshift($printArray,[$emptyInterview->interviewed]);
}
return $printArray;
}
public function collection()
/**
* @param $token
* @return bool
* if the token has position 0 or is not set, don't print it.
*/
private function invalidData($token): bool
{
return Study::where('id', $this->id)->get();
return !is_object($token->valutation->position) && $token->valutation->position == 0 && $token->valutation->distance == 0 && $token->valutation->circle == 0;
}
/**
* @param $interview
* @return bool
* @param $toprint
*/
private function invalidData($interview): bool
private function formatTokenValuesForExport($interview, &$toprint): void
{
return !is_object($interview->valutation->position) && $interview->valutation->position == 0 && $interview->valutation->distance == 0 && $interview->valutation->circle == 0;
if (isset($interview->valutation->percentagePosition)) {
$percentagePosition = "x: " . $interview->valutation->percentagePosition->x . " y:" . $interview->valutation->percentagePosition->y;
} else {
$percentagePosition = '';
}
if (is_object($interview->valutation->position)) {
$position = "x: " . $interview->valutation->position->x . " y:" . $interview->valutation->position->y;
} else {
$position = $interview->valutation->position;
}
if (is_array($interview->valutation->classifiers)) {
$classifiers = "";
foreach ($interview->valutation->classifiers as $classifier) {
$classifiers .= $classifier->basename . " - ";
}
}
$toprint = [
$interview->token_id,
Token::where('id', $interview->token_id)->first()->name,
$interview->valutation->circle,
$interview->valutation->distance,
$interview->valutation->sorting,
$position,
$percentagePosition,
$classifiers
];
}
/**
* @return Collection
*/
public function collection()
{
return Study::where('id', $this->id)->get();
}
}
......@@ -7,29 +7,19 @@ use Illuminate\Http\Request;
class Answer extends Model
{
/**
* @var array
*/
protected $fillable = ['question_id', 'answer'];
/**
* @var array
*/
protected $casts = [
'answer' => 'array',
];
public function question()
{
return $this->hasOne(\App\Question::class);
}
public function interviews()
{
return $this->belongsToMany(\App\Interview::class, 'interview_answers')->withPivot('result')->withTimestamps();
}
public function getAnswerAttribute($value)
{
return is_array($value) ? $value : (array)json_decode($value);
}
/**
* @param Request $request
* @param Request $request
* @param Interview $interview
*/
public static function saveResultQuestions(Request $request, Interview $interview): void
......@@ -43,28 +33,20 @@ class Answer extends Model
$interview->answers()->attach($key, ['result' => $value, 'question_id' => $question->id]);
}
}
if ($key === 'multi') {
foreach ($value as $multi) {
$answer = Answer::where('id', '=', $multi['id'])->first();
if ($key === 'multi' || $key === 'onechoice') {
foreach ($value as $answer) {
$answer = Answer::where('id', '=', $answer['id'])->first();
$question = Question::where('id', '=', $answer->question_id)->first();
$interview->answers()->attach($multi['id'], ['question_id' => $question->id]);
$interview->answers()->attach($answer['id'], ['question_id' => $question->id]);
}
}
if ($key === 'onechoice') {
foreach ($value as $one) {
$answer = Answer::where('id', '=', $one['id'])->first();
$question = Question::where('id', '=', $answer->question_id)->first();
$interview->answers()->attach($one['id'], ['question_id' => $question->id]);
}
}
}
}
/**
* @param \App\Interview $interview
* @param $data
* @param Interview $interview
* @param $data
*/
public static function assignAnswersToQuestion(Interview $interview, $data): void
{
......@@ -74,4 +56,23 @@ class Answer extends Model
}
}
public function question()
{
return $this->hasOne(Question::class);
}
public function interviews()
{
return $this->belongsToMany(Interview::class, 'interview_answers')->withPivot('result')->withTimestamps();
}
/**
* @param $value
* @return array
*/
public function getAnswerAttribute($value)
{
return is_array($value) ? $value : (array)json_decode($value);
}
}
......@@ -2,14 +2,12 @@
namespace App\Console\Commands;
use Helper;
use App\Permission;
use App\Role;
use App\User;
use App\Permission;
use App\Mail\VerificationEmail;
use Helper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Mail;
class CreateUserCommand extends Command
{
......@@ -45,22 +43,23 @@ class CreateUserCommand extends Command
public function handle()
{
$email = $this->ask('Enter *supervisor* email');
/* $this->info('2 -> Supervisor');*/
/* $this->info('3 -> Researcher');*/
/* $role = $this->choice('User role?', [2, 3]);*/
/* $this->info('2 -> Supervisor');*/
/* $this->info('3 -> Researcher');*/
/* $role = $this->choice('User role?', [2, 3]);*/
$password = $this->secret('Enter password');
$role = 2;
$role = 2;
if ($this->store($role, $email,$password, $user)) {
if ($this->store($role, $email, $password, $user)) {
//Mail::to($email)->send(new VerificationEmail($user, config('utilities.emailDefaultText')));
//$this->info('An email was sent to '.$user->email.' he/she needs to set the password.');
$this->info('User '.$email.' created');
$this->info('User ' . $email . ' created');
return true;
} else {
}
$this->info('There it was an error during user creation, please try again.');
return false;
}
}
public function store($roleId, $email, $password, &$user)
......
......@@ -37,21 +37,11 @@ class DeleteClosedStudies extends Command
*/
public function handle()
{
$headers = ['Study Name', 'made by'];
$studiesAndCreator = \App\Study::join('users', 'user_id', '=', 'users.id')->select('studies.name', 'users.email')->get()->toArray();
$this->table($headers, $studiesAndCreator);
$headers = ['Study Id', 'Study Name'];
$studies = \App\Study::select('studies.id', 'studies.name')->get()->toArray();
$this->table($headers, $studies);
$studiesId = \App\Study::pluck('id')->toArray();
array_unshift($studiesId, 0);
$this->warn('Press 0 to delete nothing');
$studiesId = $this->ShowCurrentStudies();
$id = $this->choice('Which study you want to delete??', $studiesId, 0);
$study = \App\Study::find($id);
if (! $study) {
if (!$study) {
$this->warn('study not found or exited with 0');
} elseif ($study->interviews->count() > 0 && $this->confirm('THIS STUDY HAS INTERVIEWS, YOU WILL DELETE THEM. ARE YOU SURE?', false)) {
foreach ($study->interviews as $interview) {
......@@ -66,4 +56,24 @@ class DeleteClosedStudies extends Command
$this->info('study deleted!');
}
}
/**
* @return mixed
*/
private function ShowCurrentStudies()
{
$headers = ['Study Name', 'made by'];
$studiesAndCreator = \App\Study::join('users', 'user_id', '=', 'users.id')->select('studies.name', 'users.email')->get()->toArray();
$this->table($headers, $studiesAndCreator);
$headers = ['Study Id', 'Study Name'];
$studies = \App\Study::select('studies.id', 'studies.name')->get()->toArray();
$this->table($headers, $studies);
$studiesId = \App\Study::pluck('id')->toArray();
array_unshift($studiesId, 0);
$this->warn('Press 0 to delete nothing');
return $studiesId;
}
}
......@@ -40,39 +40,23 @@ class DeleteUserCommand extends Command
public function handle()
{
$info = $this->choice('Email or ID?', ['email', 'id']);
if ($info === 'email') {
$email = $this->ask('Enter email');
try {
$user = User::where('email', '=', $email)->withTrashed()->firstOrFail();
} catch (ModelNotFoundException $exception) {
$this->warn('user not found!');
return false;
}
} else {
$id = $this->ask('Enter id');
try {
$user = User::where('id', '=', $id)->withTrashed()->firstOrFail();
} catch (ModelNotFoundException $exception) {
$this->warn('user not found!');
return false;
}
$userEnteredData = $this->ask('Enter ' . $info);
try {
$user = User::where($info, '=', $userEnteredData)->withTrashed()->firstOrFail();
} catch (ModelNotFoundException $exception) {
$this->warn('user not found!');
return false;
}
if ($this->confirm('ARE YOU SURE YOU WANT TO DELETE THIS USER? '.$user->email, false)) {
$this->warn("Soft deletion keeps the user in the database but it acts as deleted. This allows to retrieve some data if necessary.");
$this->error("Force deletion delete the user and everything related, forever!");
if ($this->confirm('ARE YOU SURE YOU WANT TO DELETE THIS USER? ' . $user->email, false)) {
$this->warn('Soft deletion keeps the user in the database but it acts as deleted. This allows to retrieve some data if necessary.');
$this->error('Force deletion delete the user and everything related, forever!');
$whichDelete = $this->choice('Soft or Force deletion?', ['Soft', 'Force']);
if ($whichDelete == 'Force') {
if ($whichDelete === 'Force') {
$user->forceDelete();
} else {
$user->delete();
}
$this->info("user deleted!");
}
}
}
......@@ -5,11 +5,11 @@ namespace App\Console\Commands;
use App\Permission;
use App\User;
use Illuminate\Console\Command;
use File;
use Helper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Date;
use File;
class InstallMesortCommand extends Command
{
......@@ -50,58 +50,30 @@ class InstallMesortCommand extends Command
$this->warn('--Cleaning the cache---');
Artisan::call('optimize:clear');
$this->info(Artisan::output());
$this->warn('---Creating the required folders---');
$this->error('---If this fails, check your folder permissions!---');
$path = public_path().'/images/preset_tokens';
File::makeDirectory($path, $mode = 0775, true, true);
$path = public_path().'/images/classifiers';
File::makeDirectory($path, $mode = 0775, true, true);
$path = storage_path('app/preset_tokens');
File::makeDirectory($path, $mode = 0775, true, true);
$path = storage_path('app/classifiers');
File::makeDirectory($path, $mode = 0775, true, true);
$this->CreatePaths();
$this->warn('Folders created successfully!');
$this->warn('You will now create your personal user with all access rights.');
$stored = false;
while(!$stored){
$email = $this->ask('Enter your email');
$password = $this->ask('Enter your password - minimum 6 chars.');
$stored = $this->storeUser(1, $email,$password, $user);
}
$this->CreateAdmin($email, $password, $user);
$this->info('You can now enter with the following data:');
$this->info('Email: '.$email);
$this->info('Password: '.$password);
$this->info('Email: ' . $email);
$this->info('Password: ' . $password);
}
public function storeUser($roleId, $email, $password, &$user)
/**
* @param $roleId
* @param $email
* @param $password
* @param $user
* @return bool
*/
public function storeUser($roleId, $email, $password, &$user): bool
{
if (!$this->validateUser($email, $password)) return false;
$role = Role::where('id', $roleId)->first();
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->warn('Please enter a valid email.');
return false;
}
if(strlen($password) < 6){
$this->warn('Please enter a valid password.');
return false;
}
$user = new User();
$user->email = $email;
$user->password = bcrypt($password);
$createStudyPermission = Permission::where('name', 'create-studies')
......@@ -111,9 +83,52 @@ class InstallMesortCommand extends Command
$user->save();
$user->roles()->sync($role);
$user->attachPermissions([$createStudyPermission]);
return true;
}
private function validateUser($email, $password): bool
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->warn('Please enter a valid email.');
return false;