Commit b9e724ef authored by ZeMKI's avatar ZeMKI

Newsletter and code optimization

* now users require a profile.
* new column on profile: newsletter.
* using config.enums for newsletter status.
* now you can define 0 pre-defined tokens for a study.
* css optimization.
parent 6ecca000
......@@ -2,12 +2,25 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="b51d2beb-a115-4b2a-80a2-b27cfb404d6c" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/config/enums.php" afterDir="false" />
<change afterPath="$PROJECT_DIR$/database/migrations/2020_06_02_173558_add_newsletter_to_user_profiles.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/AllInterviewTokenExport.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/AllInterviewTokenExport.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/InterviewTokenExport.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/InterviewTokenExport.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/js/components/Interview/newinterview.vue" beforeDir="false" afterPath="$PROJECT_DIR$/resources/js/components/Interview/newinterview.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/js/components/Interview/sorting_preview.vue" beforeDir="false" afterPath="$PROJECT_DIR$/resources/js/components/Interview/sorting_preview.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Answer.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Answer.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Helpers/Helper.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Helpers/Helper.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/Auth/RegisterController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/Auth/RegisterController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/HomeController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/HomeController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/NotificationController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/NotificationController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Http/Controllers/StudyController.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Http/Controllers/StudyController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/Profile.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Profile.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/User.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/User.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/database/migrations/2018_10_04_145119_create_users_profiles_table.php" beforeDir="false" afterPath="$PROJECT_DIR$/database/migrations/2018_10_04_145119_create_users_profiles_table.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/js/app.js" beforeDir="false" afterPath="$PROJECT_DIR$/resources/js/app.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/js/components/newstudy.vue" beforeDir="false" afterPath="$PROJECT_DIR$/resources/js/components/newstudy.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/sass/_variables.scss" beforeDir="false" afterPath="$PROJECT_DIR$/resources/sass/_variables.scss" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/views/auth/register.blade.php" beforeDir="false" afterPath="$PROJECT_DIR$/resources/views/auth/register.blade.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/views/home.blade.php" beforeDir="false" afterPath="$PROJECT_DIR$/resources/views/home.blade.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/views/layouts/nav.blade.php" beforeDir="false" afterPath="$PROJECT_DIR$/resources/views/layouts/nav.blade.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/routes/web.php" beforeDir="false" afterPath="$PROJECT_DIR$/routes/web.php" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -268,6 +281,8 @@
<workItem from="1590484643170" duration="25829000" />
<workItem from="1590656055131" duration="4234000" />
<workItem from="1590665422138" duration="4114000" />
<workItem from="1591103340019" duration="9647000" />
<workItem from="1591616407634" duration="531000" />
</task>
<servers />
</component>
......@@ -287,47 +302,63 @@
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="770" y="559" key="ComposerInitDialogKey/0.0.2048.1280@0.0.2048.1280" timestamp="1589809796036" />
<state width="2006" height="405" key="GridCell.Tab.0.bottom" timestamp="1590665409302">
<state x="901" y="511" key="Create Database Migration" timestamp="1591112158519">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state width="2006" height="491" key="GridCell.Tab.0.bottom/0.0.2048.1280@0.0.2048.1280" timestamp="1590419278940" />
<state x="901" y="511" key="Create Database Migration/0.0.2048.1280@0.0.2048.1280" timestamp="1591112158519" />
<state x="906" y="571" key="Create Model" timestamp="1591112276657">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="906" y="571" key="Create Model/0.0.2048.1280@0.0.2048.1280" timestamp="1591112276657" />
<state width="2006" height="405" key="GridCell.Tab.0.bottom" timestamp="1591199738713">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state width="2006" height="405" key="GridCell.Tab.0.bottom/0.0.2048.1280@0.0.2048.1280" timestamp="1591199738713" />
<state width="2006" height="406" key="GridCell.Tab.0.bottom/128.-1057.1920.1057/0.23.2048.1257@0.23.2048.1257" timestamp="1590060860387" />
<state width="2006" height="406" key="GridCell.Tab.0.bottom/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590058941151" />
<state width="2006" height="405" key="GridCell.Tab.0.bottom/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665409302" />
<state width="1878" height="406" key="GridCell.Tab.0.bottom/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590168747932" />
<state width="2006" height="405" key="GridCell.Tab.0.center" timestamp="1590665409301">
<state width="2006" height="405" key="GridCell.Tab.0.center" timestamp="1591199738712">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state width="2006" height="491" key="GridCell.Tab.0.center/0.0.2048.1280@0.0.2048.1280" timestamp="1590419278939" />
<state width="2006" height="405" key="GridCell.Tab.0.center/0.0.2048.1280@0.0.2048.1280" timestamp="1591199738712" />
<state width="2006" height="406" key="GridCell.Tab.0.center/128.-1057.1920.1057/0.23.2048.1257@0.23.2048.1257" timestamp="1590060860386" />
<state width="2006" height="406" key="GridCell.Tab.0.center/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590058941150" />
<state width="2006" height="405" key="GridCell.Tab.0.center/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665409301" />
<state width="1878" height="406" key="GridCell.Tab.0.center/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590168747929" />
<state width="2006" height="405" key="GridCell.Tab.0.left" timestamp="1590665409301">
<state width="2006" height="405" key="GridCell.Tab.0.left" timestamp="1591199738709">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state width="2006" height="491" key="GridCell.Tab.0.left/0.0.2048.1280@0.0.2048.1280" timestamp="1590419278938" />
<state width="2006" height="405" key="GridCell.Tab.0.left/0.0.2048.1280@0.0.2048.1280" timestamp="1591199738709" />
<state width="2006" height="406" key="GridCell.Tab.0.left/128.-1057.1920.1057/0.23.2048.1257@0.23.2048.1257" timestamp="1590060860385" />
<state width="2006" height="406" key="GridCell.Tab.0.left/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590058941149" />
<state width="2006" height="405" key="GridCell.Tab.0.left/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665409301" />
<state width="1878" height="406" key="GridCell.Tab.0.left/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590168747922" />
<state width="2006" height="405" key="GridCell.Tab.0.right" timestamp="1590665409302">
<state width="2006" height="405" key="GridCell.Tab.0.right" timestamp="1591199738712">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state width="2006" height="491" key="GridCell.Tab.0.right/0.0.2048.1280@0.0.2048.1280" timestamp="1590419278939" />
<state width="2006" height="405" key="GridCell.Tab.0.right/0.0.2048.1280@0.0.2048.1280" timestamp="1591199738712" />
<state width="2006" height="406" key="GridCell.Tab.0.right/128.-1057.1920.1057/0.23.2048.1257@0.23.2048.1257" timestamp="1590060860386" />
<state width="2006" height="406" key="GridCell.Tab.0.right/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590058941150" />
<state width="2006" height="405" key="GridCell.Tab.0.right/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665409302" />
<state width="1878" height="406" key="GridCell.Tab.0.right/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590168747931" />
<state x="801" y="371" key="NewPhpClassDialog" timestamp="1591119868791">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="801" y="371" key="NewPhpClassDialog/0.0.2048.1280@0.0.2048.1280" timestamp="1591119868791" />
<state x="835" y="570" key="NewPhpFileDialog" timestamp="1591119861931">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="835" y="570" key="NewPhpFileDialog/0.0.2048.1280@0.0.2048.1280" timestamp="1591119861931" />
<state x="0" y="23" width="2048" height="1257" key="SettingsEditor" timestamp="1590665405159">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="0" y="23" width="2048" height="1257" key="SettingsEditor/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590056101932" />
<state x="0" y="23" key="SettingsEditor/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665405159" />
<state x="754" y="390" width="597" height="524" key="StructurePopup" timestamp="1590686482515">
<state x="754" y="390" width="597" height="524" key="StructurePopup" timestamp="1591089727978">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="754" y="390" width="597" height="524" key="StructurePopup/0.0.2048.1280@0.0.2048.1280" timestamp="1590686482515" />
<state x="754" y="390" width="597" height="524" key="StructurePopup/0.0.2048.1280@0.0.2048.1280" timestamp="1591089727978" />
<state x="754" y="390" width="597" height="524" key="StructurePopup/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590054141097" />
<state x="754" y="390" width="598" height="526" key="StructurePopup/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590666782790" />
<state x="772" y="-751" width="560" height="443" key="StructurePopup/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590168084313" />
......@@ -335,16 +366,16 @@
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="349" y="331" key="com.intellij.ide.ui.customization.CustomizableActionsPanel$EditIconDialog/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590665402287" />
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1590581191374">
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1591186286502">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.0.2048.1280@0.0.2048.1280" timestamp="1590581191374" />
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/0.0.2048.1280@0.0.2048.1280" timestamp="1591186286502" />
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590054170589" />
<state x="799" y="561" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590404098131" />
<state x="708" y="373" width="632" height="534" key="find.popup" timestamp="1590406250473">
<state x="1038" y="638" width="632" height="534" key="find.popup" timestamp="1591115078673">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="708" y="373" width="632" height="534" key="find.popup/0.0.2048.1280@0.0.2048.1280" timestamp="1589968436681" />
<state x="1038" y="638" width="632" height="534" key="find.popup/0.0.2048.1280@0.0.2048.1280" timestamp="1591115078673" />
<state x="708" y="373" width="632" height="534" key="find.popup/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590406250473" />
<state x="899" y="515" width="250" height="278" key="jetbrains.javascript.buildTools.run-task-popup" timestamp="1590568927491">
<screen x="0" y="0" width="2048" height="1280" />
......@@ -362,10 +393,11 @@
</state>
<state x="824" y="505" key="scopes/128.-1057.1920.1057/0.0.2048.1280@0.0.2048.1280" timestamp="1590055889310" />
<state x="824" y="505" width="498" height="379" key="scopes/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590055356254" />
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup" timestamp="1590689990547">
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup" timestamp="1591617063454">
<screen x="0" y="0" width="2048" height="1280" />
</state>
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup/0.0.2048.1280@0.0.2048.1280" timestamp="1590689990547" />
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup/0.0.2048.1280@0.0.2048.1280" timestamp="1591617063454" />
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup/0.23.2048.1257@0.23.2048.1257" timestamp="1591115561349" />
<state x="689" y="291" width="670" height="676" key="search.everywhere.popup/128.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590054146808" />
<state x="689" y="292" width="715" height="677" key="search.everywhere.popup/65.-1080.1920.1080/0.0.2048.1280@0.0.2048.1280" timestamp="1590667028046" />
<state x="711" y="-834" width="670" height="571" key="search.everywhere.popup/65.-1080.1920.1080/0.0.2048.1280@65.-1080.1920.1080" timestamp="1590165045513" />
......
......@@ -7,15 +7,14 @@ use Illuminate\Http\Request;
/**
* App\Answer
*
* @property int $id
* @property int|null $question_id
* @property array $answer
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $id
* @property int|null $question_id
* @property array $answer
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Interview[] $interviews
* @property-read int|null $interviews_count
* @property-read \App\Question|null $question
* @property-read int|null $interviews_count
* @property-read \App\Question|null $question
* @method static \Illuminate\Database\Eloquent\Builder|\App\Answer newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Answer newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Answer query()
......@@ -45,17 +44,22 @@ class Answer extends Model
*/
public static function saveResultQuestions(Request $request, Interview $interview): void
{
foreach ($request->input('results_questions') as $key => $value) {
if (is_numeric($key)) {
foreach ($request->input('results_questions') as $key => $value)
{
if (is_numeric($key))
{
$answer = Answer::where('id', '=', $key)->first();
$question = Question::where('id', '=', $answer->question_id)->first();
$answertype = $answer->answer['type'];
if ($answertype === 'open' || $answertype === 'scale') {
if ($answertype === 'open' || $answertype === 'scale')
{
$interview->answers()->attach($key, ['result' => $value, 'question_id' => $question->id]);
}
}
if ($key === 'multi' || $key === 'onechoice') {
foreach ($value as $answer) {
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($answer['id'], ['question_id' => $question->id]);
......@@ -70,7 +74,8 @@ class Answer extends Model
*/
public static function assignAnswersToQuestion(Interview $interview, $data): void
{
foreach ($data['questions'] as $question) {
foreach ($data['questions'] as $question)
{
$question['available_answers'] = $question->availableAnswers();
$question['answers'] = $question->answers()->having('pivot_interview_id', '=', $interview->id)->get();
}
......
......@@ -103,6 +103,7 @@ class Helper
$array = array_values($temp_array);
return $array;
}
/**
* @param $string
* @param $start
......@@ -113,12 +114,11 @@ class Helper
{
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini === 0 || $ini === false) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
if (strpos($string, $end, $ini) === false) {
if (strpos($string, $end, $ini) === false)
{
$len = strlen($string) - 1;
}
return substr($string, $ini, $len);
......
......@@ -8,6 +8,7 @@ use App\Role;
use App\User;
use Helper;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Validator;
use Spatie\WebhookServer\WebhookCall;
......@@ -65,6 +66,7 @@ class RegisterController extends Controller
*/
protected function create($data)
{
$userexist = User::where('email', '=', $data['email'])->first();
$role = Role::where('name', 'supervisor')->first();
......@@ -78,17 +80,25 @@ class RegisterController extends Controller
$user->password_token = Helper::random_str(30);
$user->save();
$profile = $user->addProfile($user);
$profile->newsletter = array_key_exists('newsletter',$data) ? config('enums.newsletter_status.SUBSCRIBED') : config('enums.newsletter_status.NOT SUBSCRIBED');
$profile->save();
$createStudyPermission = Permission::where('name', 'create-studies')
->first();
$user->supervised_by = $user->id;
$user->attachPermissions([$createStudyPermission]);
$user->attachRole($role);
WebhookCall::create()
->url('https://chat.zemki.uni-bremen.de/hooks/Jj3dDY2KzSFDS2kxZ/SvbmjdswXTASAXxC2GfgfTpFooK5Eo4kFBGPyDRrtsWmgED3')
->payload(['text' => 'User '.$data['email'].' has registered on Mesort. We have a total of '.User::all()->count().' users!'])
->useSecret('Jj3dDY2KzSFDS2kxZ/SvbmjdswXTASAXxC2GfgfTpFooK5Eo4kFBGPyDRrtsWmgED3')
->dispatch();
if (!App::environment('local')) {
WebhookCall::create()
->url('https://chat.zemki.uni-bremen.de/hooks/Jj3dDY2KzSFDS2kxZ/SvbmjdswXTASAXxC2GfgfTpFooK5Eo4kFBGPyDRrtsWmgED3')
->payload(['text' => 'User '.$data['email'].' has registered on Mesort. We have a total of '.User::all()->count().' users!'])
->useSecret('Jj3dDY2KzSFDS2kxZ/SvbmjdswXTASAXxC2GfgfTpFooK5Eo4kFBGPyDRrtsWmgED3')
->dispatch();
}
$user->save();
......
......@@ -2,8 +2,12 @@
namespace App\Http\Controllers;
use App\Helpers\NewsletterStatus;
use Auth;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Response;
use Illuminate\View\View;
use Session;
class HomeController extends Controller
......@@ -19,11 +23,12 @@ class HomeController extends Controller
/**
* Show the application dashboard.
* @return Response
* @return Application|Factory|View
*/
public function index()
{
$data['studies'] = Auth::user()->studies;
$data['newsletter'] = Auth::user()->profile->newsletter === config('enums.newsletter_status.NOT DECIDED');
return view('home', $data);
}
......
......@@ -6,6 +6,9 @@ use App\Notifications\NotificationFromStaff;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use mysql_xdevapi\Exception;
use phpDocumentor\Reflection\Types\Boolean;
use PhpParser\Node\Expr\Cast\Bool_;
class NotificationController extends Controller
{
......@@ -38,5 +41,31 @@ class NotificationController extends Controller
$user->notify(new NotificationFromStaff(['title' => $request->input('title'), 'message' => $request->input('message')]));
return view('admin.notifications', ['message' => 'Notification sent to ' . $request->input('email')]);
}
public function addToNewsletter(Request $request)
{
$subscribe = $request->input('subscribed') ? config('enums.newsletter_status.SUBSCRIBED') : config('enums.newsletter_status.NOT SUBSCRIBED');
try
{
if(auth()->user()->profile()->exists())
{
auth()->user()->profile->newsletter = $subscribe;
auth()->user()->profile->save();
}else{
$profile = auth()->user()->addProfile(auth()->user());
$profile->newsletter = $subscribe;
$profile->save();
}
return response()->json(['message' => 'Your preference was saved!','r' => $subscribe], 200);
}catch (Exception $exception)
{
return response()->json(['message' => 'A problem occurred, contact the administrator.'], 500);
}
}
}
......@@ -22,15 +22,14 @@ use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;
use Image;
use Storage;
class StudyController extends Controller
{
public function create(Request $request)
{
if (Auth::user()->hasReachMaxNumberOfStudies()) {
if (Auth::user()->hasReachMaxNumberOfStudies())
{
auth()->user()->addAction('trying to create a study', $request->url(), 'Max numbers of studies reached for user ' . auth()->user()->email);
abort(403, 'You reached the max number of studies');
}
......@@ -46,14 +45,18 @@ class StudyController extends Controller
$copy = $study->replicate();
$copy->save();
$study->load('questions', 'sortings', 'tokens');
foreach ($study->getRelations() as $relationName => $values) {
if ($relationName === "sortings") {
foreach ($study->getRelations() as $relationName => $values)
{
if ($relationName === "sortings")
{
$request = new Request();
$request->replace(['details' => $study->sortings[0]->pivot->details, 'sortingid' => $study->sortings[0]->id]);
Sorting::store($request, $copy);
} else if ($relationName === "questions") {
} else if ($relationName === "questions")
{
$this->CopyQuestionsRemoveInterviews($study, $copy);
} else if ($relationName === "tokens") {
} else if ($relationName === "tokens")
{
$this->RemoveTokensCreatedDuringInterview($values);
$copy->{$relationName}()->attach($values);
}
......@@ -69,20 +72,21 @@ class StudyController extends Controller
*/
private function CopyQuestionsRemoveInterviews(Study $study, Study $copy): void
{
foreach ($study->questions as $question) {
foreach ($study->questions as $question)
{
$copiedQuestion = $copy->questions()->create($question->toArray());
$answers = Answer::where('question_id', $question->id)->get();
foreach ($answers as $answer) {
foreach ($answers as $answer)
{
$answer->question_id = $copiedQuestion->id;
$copiedQuestion->answers()->create($answer->toArray());
}
// remove interviews
foreach ($copiedQuestion->answers as $answer) {
foreach ($copiedQuestion->answers as $answer)
{
$answer->interviews()->detach();
$answer->interviews()->delete();
}
}
}
......@@ -93,8 +97,10 @@ class StudyController extends Controller
private function RemoveTokensCreatedDuringInterview(&$values): void
{
// remove tokens created during interview
foreach ($values as $index => $token) {
if ($token->author == 0) {
foreach ($values as $index => $token)
{
if ($token->author == 0)
{
unset($values[$index]);
}
}
......@@ -117,11 +123,13 @@ class StudyController extends Controller
public function store(Request $request, $dummy = false)
{
if ($dummy != false) {
if ($dummy != false)
{
$userForStudy = User::where('id', $dummy)->first();
$request = new Request(config('utilities.dummyStudy1'));
} else $userForStudy = Auth::user();
if (!$request->has('name') || !$request->has('description')) {
if (!$request->has('name') || !$request->has('description'))
{
return response()->json('Data are not valid', 422);
}
$newStudy = new Study();
......@@ -143,32 +151,43 @@ class StudyController extends Controller
}
/**
* Save the tokens to the database, if any.
* @param Study $study
* @param Request $request
* @param $token
*/
public function saveTokens(Study $study, Request $request, &$token): void
{
foreach ($request->get('sorting')['tokens'] as $tokenToSave) {
if($request->get('sorting')['tokens']){
foreach ($request->get('sorting')['tokens'] as $tokenToSave)
{
Token::store($tokenToSave, $study);
}
}
}
/**
* Save the questions to database, if any.
* @param $questions
* @param $study
* @param bool $edit
*/
public function saveQuestionsAnswers($questions, $study, $edit = false)
{
if ($edit) {
foreach ($study->questions as $questionToReset) {
if ($edit)
{
foreach ($study->questions as $questionToReset)
{
Answer::where('question_id', '=', $questionToReset['id'])->delete();
Question::destroy($questionToReset['id']);
}
}
if (array_key_exists('question', $questions[0])) {
foreach ($questions as $questionToSave) {
$atLeastOneQuestion = array_key_exists('question', $questions[0]);
if ($atLeastOneQuestion)
{
foreach ($questions as $questionToSave)
{
$question = new Question();
$question->question = $questionToSave['question'];
$question->detail = $questions[0]['type'];
......@@ -180,13 +199,16 @@ class StudyController extends Controller
}
/**
* Save the answers for a given question.
* @param $questionToSave
* @param Question $question
*/
private function saveAnswers($questionToSave, Question $question): void
{
if ($questionToSave['ismultiple']) {
foreach ($questionToSave['answers'] as $answerToSave) {
if ($questionToSave['ismultiple'])
{
foreach ($questionToSave['answers'] as $answerToSave)
{
$answer = new Answer();
$answer->question_id = $question->id;
$answerJson = ['type' => 'multi', 'answer' => $answerToSave];
......@@ -194,8 +216,10 @@ class StudyController extends Controller
$answer->save();
}
}
if ($questionToSave['isonechoice']) {
foreach ($questionToSave['answers'] as $answerToSave) {
if ($questionToSave['isonechoice'])
{
foreach ($questionToSave['answers'] as $answerToSave)
{
$answer = new Answer();
$answer->question_id = $question->id;
$answerJson = ['type' => 'onechoice', 'answer' => $answerToSave];
......@@ -203,14 +227,16 @@ class StudyController extends Controller
$answer->save();
}
}
if ($questionToSave['isopen']) {
if ($questionToSave['isopen'])
{
$answer = new Answer();
$answerJson = ['type' => 'open', 'answer' => ''];
$answer->question_id = $question->id;
$answer->answer = $answerJson;
$answer->save();
}
if ($questionToSave['isscale']) {
if ($questionToSave['isscale'])
{
$answer = new Answer();
$answerJson = ['type' => 'scale', 'answer' => ['min' => $questionToSave['scalemin'], 'max' => $questionToSave['scalemax'], 'minlabel' => $questionToSave['minlabel'], 'maxlabel' => $questionToSave['maxlabel']]];
$answer->question_id = $question->id;
......@@ -247,11 +273,13 @@ class StudyController extends Controller
public function update(Study $study, Request $request)
{
$this->authorize($study);
if (!$request->has('name')) {
if (!$request->has('name'))
{
return response()->json('Data are not valid', 422);
}
$study = Study::where('id', '=', $request->input('id'))->with('questions')->first();
if (!$study->isEditable()) {
if (!$study->isEditable())
{
return back()->withInput();
}
// sorting
......@@ -280,7 +308,8 @@ class StudyController extends Controller
public function removeTokensFromStudy(Study $study): void
{
$available_tokens = $study->available_tokens()->pluck('tokens.id')->toArray();
foreach ($available_tokens as $tokensToDelete) {
foreach ($available_tokens as $tokensToDelete)
{
$to = Token::where('id', '=', $tokensToDelete)->first();
$study->tokens()->detach($to->id);
$to->delete();
......@@ -295,11 +324,13 @@ class StudyController extends Controller
public function destroy(Study $study, Request $request)
{
if (!auth()->user()->hasRole(['admin', 'supervisor'])) {
if (!auth()->user()->hasRole(['admin', 'supervisor']))
{
abort(403, 'You are not allowed to edit this study');
}
if (!auth()->user()->can('delete-studies', $study->id)) return response('You are not authorized to delete this study.', 403);
foreach ($study->interviews as $interview) {
foreach ($study->interviews as $interview)
{
File::delete($interview->sorting_screenshot);
$interview->answers()->detach();
$interview->answers()->delete();
......@@ -333,11 +364,11 @@ class StudyController extends Controller
public function deleteallbyuser(User $user, Request $request)
{
$studies = Study::where('user_id', $user->id)->get();
foreach ($studies as $study) {
foreach ($studies as $study)
{
$study->delete();
}
auth()->user()->addAction('delete all studies by user', $request->url(), 'user deleted studies for the user ' . $user->email);
return response()->json(['message' => 'Studies Deleted!'], 200);
}
}
......@@ -41,7 +41,7 @@ class Profile extends Model
* @var array
*/
protected $fillable = [
'name', 'address', 'workaddress', 'phonenumber1', 'phonenumber2',
'name', 'address', 'workaddress', 'phonenumber1', 'phonenumber2','newsletter',
];
public function user()
......
......@@ -2,8 +2,6 @@
namespace App;
use Auth;
use DB;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;