function revalidate($id, TestValidator $testvalidator) { $archive = Archive::findOrFail($id); $archive->data = json_decode($archive->data, true); $old_data = $archive->data; $data = $testvalidator->WithAnswers($archive->test, $archive->data['given_answers']); if (@$old_data['feedback']) { $data['feedback'] = $old_data['feedback']; } $archive->data = json_encode($data); $archive->save(); return ['success' => true, 'data' => $data]; }
public function check($id, Request $request, TestValidator $testvalidator) { $test = Test::findOrFail($id); $validation = $testvalidator->WithRequest($test, $request); $hasPassed = $validation['num_correct'] / $validation['total'] >= 0.5; $hasPassedFull = $validation['num_correct'] == $validation['total']; $minimumToPass = ceil($validation['total'] * 0.5); $customErrors = new MessageBag(); $errors = []; if (!Auth::check()) { $authenticationType = $request->input('authentication_type'); switch ($authenticationType) { case 0: $authvalidator = \Validator::make($request->all(), ['user-name' => 'required', 'user-email' => 'required|email|unique:users,email', 'user-password' => 'required|min:8|confirmed'], ['user-name.required' => 'Nimi on pakollinen.', 'user-email.required' => 'Sähköpostiosoite on pakollinen.', 'user-email.email' => 'Annettu sähköpostiosoite ei ole pätevä.', 'user-email.unique' => 'Tunnus samalla sähköpostiosoitteella on jo olemassa.', 'user-password.required' => 'Salasana on pakollinen.', 'user-password.min' => 'Salasanan pitää olla ainakin :min merkkiä pitkä.', 'user-password.confirmed' => 'Salasanat eivät täsmää.']); break; case 1: $authvalidator = \Validator::make($request->all(), ['user-login-email' => 'required|email', 'user-login-password' => 'required'], ['user-login-email.required' => 'Sähköpostiosoite on pakollinen.', 'user-login-email.email' => 'Annettu sähköpostiosoite ei ole pätevä.', 'user-login-password.required' => 'Salasana on pakollinen.']); break; } $group = false; $shouldJoinGroup = false; if (strlen($request->input('group-code')) > 0) { $group = Group::where('code', $request->input('group-code')); if (!$group->exists()) { $customErrors->add('group-code', 'Annettua ryhmää ei löytynyt. Syötä oikea ryhmäkoodi.'); } else { $shouldJoinGroup = true; } } if ($authvalidator->passes() && $customErrors->isEmpty()) { switch ($authenticationType) { case 0: $user = new User(); $user->name = $request->input('user-name'); $user->email = $request->input('user-email'); $user->password = Hash::make($request->input('user-password')); $user->access_level = 'USER'; $user->save(); Mail::send('email.user_account_created', ['user' => $user], function ($m) use($user) { $m->to($user->email, $user->name)->subject('Käyttäjätunnus luotu Media7 raamattuopistoon'); }); Auth::login($user); if ($shouldJoinGroup) { $group->first()->join(); } break; case 1: $credentials = ['email' => $request->input('user-login-email'), 'password' => $request->input('user-login-password')]; $remember = $request->input('remember_me'); if (!Auth::attempt($credentials, $remember)) { $customErrors->add('user-login-email', 'Kirjautuminen annetuilla tunnuksilla ei onnistunut!'); } else { if ($shouldJoinGroup) { $group->first()->join(); } return redirect('/test/' . $test->id); } break; } } $errors = array_merge($authvalidator->errors()->all(), $customErrors->all()); } if (Auth::check() && $customErrors->isEmpty()) { $archive = Auth::user()->archives()->where('test_id', $id)->first(); $data = $validation; if (!$archive) { $archive = new Archive(); $archive->user_id = Auth::user()->id; $archive->test_id = $test->id; } else { $old_data = json_decode($archive->data, true); if (@$old_data['feedback']) { $data['feedback'] = $old_data['feedback']; } } $archive->data = json_encode($data); $archive->save(); } return view('test.show')->with(array_merge($validation, ['test' => $test, 'feedback' => @$data['feedback'], 'hasPassed' => @$hasPassed, 'hasPassedFull' => @$hasPassedFull, 'minimumToPass' => @$minimumToPass, 'authentication_type' => @$authenticationType, 'isMaterialPage' => false, 'authFailed' => !Auth::check() && !$authvalidator->passes(), 'old' => $request->all()]))->withInput($request)->withErrors(@$errors); }