public function saveScore(Request $request) { if (!auth() || !auth()->user()) { echo 'Kết quả chưa được lưu.'; return; } $UserID = auth()->user()->getAuthIdentifier(); $data = $request->all(); $token = $data['token']; // day by day, no. of record will increase // => maybe there are multiple record with the same value of UserID and token // => pick the newest record $record = Doexams::where('token', 'LIKE', $token)->where('UserID', '=', $UserID)->get()->last(); if (count($record->toArray()) < 1) { echo 'Kết quả chưa được lưu.'; return; } $record->Score = $request['Score'] . '/' . $request['MaxScore']; $record->update(); $oldDateTime = $record->created_at->getTimestamp(); $newDateTime = $record->updated_at->getTimestamp(); $diff = ($newDateTime - $oldDateTime) / 3600.0; $record->Time = $diff; $record->update(); if ($diff > 0) { $course = Courses::find(Posts::find($record->PostID)->CourseID); $course->TotalHours += $diff; $course->update(); } echo 'Kết quả đã được lưu lại.'; return; }
public function viewPost($postID) { if (!auth() || !auth()->user()) { $browser = get_browser(null, true); // Allow crawler && Facebook Bot view post without logging in. if ($browser['crawler'] != 1 && stripos($_SERVER["HTTP_USER_AGENT"], 'facebook') === false && stripos($_SERVER["HTTP_USER_AGENT"], 'face') === false && stripos($_SERVER["HTTP_USER_AGENT"], 'google') === false) { $redirectPath = '/post/' . $postID; return redirect('/login')->with('redirectPath', $redirectPath); } $token = md5(rand(), false); $DisplayedQuestions = ConstsAndFuncs::$FreeQuestionsForCrawler; } $post = Posts::find($postID); if (count($post) < 1) { return view('errors.404'); } $courseID = $post['CourseID']; $course = Courses::find($courseID); $post->visited++; $post->update(); $post = $post->toArray(); if (auth() && auth()->user()) { $userID = auth()->user()->getAuthIdentifier(); if (User::find($userID)['admin'] < ConstsAndFuncs::PERM_ADMIN) { if ($course['Hidden'] == 1 || $post['Hidden'] == 1) { return view('errors.404'); } } $tmp = Learnings::where('UserID', '=', $userID)->where('CourseID', '=', $courseID)->get()->toArray(); if (count($tmp) < 1) { $learnings = new Learnings(); $learnings->UserID = $userID; $learnings->CourseID = $courseID; $learnings->save(); $course->NoOfUsers++; $course->update(); } // Insert a new record into DoExams Table to mark the time user start answering questions in post. $exam = new Doexams(); $exam->UserID = $userID; $exam->PostID = $postID; $token = md5($userID . rand(), false) . md5($postID . rand(), false); $exam->token = $token; $exam->save(); // Check if user is vip or not $user = User::find(auth()->user()->getAuthIdentifier()); if ($user['vip'] == 0) { $DisplayedQuestions = $post['NoOfFreeQuestions']; } else { $DisplayedQuestions = new \DateTime($user['expire_at']) >= new \DateTime() ? -1 : $post['NoOfFreeQuestions']; } if ($user['admin'] >= ConstsAndFuncs::PERM_ADMIN) { $DisplayedQuestions = -1; } // If web hasn't provide some VIP package // every user will be able to see full post if (ConstsAndFuncs::IS_PAID == 0) { $DisplayedQuestions = -1; } } $photo = $post['Photo']; if ($DisplayedQuestions > 0) { $questions = Questions::where('PostID', '=', $postID)->take($DisplayedQuestions)->get()->toArray(); } else { $questions = Questions::where('PostID', '=', $postID)->get()->toArray(); } $AnswersFor1 = array(); $TrueAnswersFor1 = array(); $AnswersFor2 = array(); $Spaces = array(); $SetOfSpaceIDs = array(); $Subquestions = array(); $AnswersFor5 = array(); $QuestionFor5IDs = array(); $AnswersFor6 = array(); $DragDropIDs = array(); $CompleteAnswersFor6 = array(); $AnswersFor3 = array(); $AnswersFor4 = array(); $FillCharacterIDs = array(); $ArrangedIDs = array(); $maxscore = 0; foreach ($questions as $q) { switch ($q['FormatID']) { case 1: // Trắc nghiệm $answers = Answers::where('QuestionID', '=', $q['id'])->get()->toArray(); foreach ($answers as $a) { if ($a['Logical'] == 1) { $TrueAnswersFor1 += [$q['id'] => $a['id']]; break; } } $info = [$q['id'] => $answers]; if (count($answers) > 0) { $maxscore++; } $AnswersFor1 += $info; continue; case 2: // Điền từ $spaces = Spaces::where('QuestionID', '=', $q['id'])->get()->toArray(); $Spaces += [$q['id'] => $spaces]; foreach ($spaces as $s) { $SetOfSpaceIDs = array_merge($SetOfSpaceIDs, [$s['id']]); $a = Answers::where('SpaceID', '=', $s['id'])->get()->toArray(); shuffle($a); $AnswersFor2 += [$s['id'] => $a]; } $maxscore += count($spaces); continue; case 3: $answer = Answers::where('QuestionID', '=', $q['id'])->get()->toArray(); $AnswersFor3 += [$q['id'] => $answer[0]]; $ArrangedIDs = array_merge($ArrangedIDs, [$q['id']]); $maxscore++; continue; case 4: $answer = Answers::where('QuestionID', '=', $q['id'])->get()->toArray(); $AnswersFor4 += [$q['id'] => $answer[0]]; $FillCharacterIDs = array_merge($FillCharacterIDs, [$q['id']]); $maxscore++; continue; case 5: // Nối $subquestions = Subquestions::where('QuestionID', '=', $q['id'])->get()->toArray(); $answer = array(); foreach ($subquestions as $s) { $a = Answers::where('SubQuestionID', '=', $s['id'])->get()->toArray(); $answer += [$s['id'] => $a[0]]; } $AnswersFor5 += [$q['id'] => $answer]; $maxscore += count($subquestions); $Subquestions += [$q['id'] => $subquestions]; $QuestionFor5IDs = array_merge($QuestionFor5IDs, [$q['id']]); continue; case 6: // Kéo thả $DragDropIDs = array_merge($DragDropIDs, [$q['id']]); $answers = Answers::where('QuestionID', '=', $q['id'])->get()->toArray(); $AnswersFor6 += [$q['id'] => $answers]; $s = ''; foreach ($answers as $a) { $s .= $a['Detail'] . ' '; } $CompleteAnswersFor6 += [$q['id'] => $s]; // $CompleteAnswersFor6 = array_merge($CompleteAnswersFor6, [$s]); $maxscore++; continue; } } $Comments = Comments::all()->toArray(); $result = array('Comments' => json_encode($Comments), 'Questions' => $questions, 'Post' => $post, 'MaxScore' => $maxscore, 'NumOfQuestions' => count($questions = Questions::where('PostID', '=', $postID)->get()->toArray()), 'Token' => $token, 'DisplayedQuestions' => $DisplayedQuestions); if (auth() && auth()->user() && User::find(auth()->user()->getAuthIdentifier())['admin'] >= ConstsAndFuncs::PERM_ADMIN) { $nextPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '>=', $post['id'])->get()->toArray(); $previousPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '<', $post['id'])->get()->toArray(); $result += ['NextPost' => count($nextPost) > 1 ? $nextPost[1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->first()->toArray()['id']]; $result += ['PreviousPost' => count($previousPost) > 0 ? $previousPost[count($previousPost) - 1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->orderBy('created_at', 'desc')->first()->toArray()['id']]; } else { $nextPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '>=', $post['id'])->where('Hidden', '=', 0)->get()->toArray(); $previousPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '<', $post['id'])->where('Hidden', '=', 0)->get()->toArray(); $result += ['NextPost' => count($nextPost) > 1 ? $nextPost[1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->where('Hidden', '=', 0)->first()->toArray()['id']]; $result += ['PreviousPost' => count($previousPost) > 0 ? $previousPost[count($previousPost) - 1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->where('Hidden', '=', 0)->orderBy('created_at', 'desc')->first()->toArray()['id']]; } $newpost = array_merge($nextPost, $previousPost); $result += ['newpost' => $newpost]; // dd($newpost); // dd($post); $ahtk = Tags::where('PostID', '=', $postID)->get()->toArray(); $Hashtag = array(); foreach ($ahtk as $k) { $ht = Hashtags::find($k['HashtagID'])['Hashtag']; if (strlen($ht) > 0) { $Hashtag = array_merge($Hashtag, [$ht]); } } return view('viewpost')->with($result)->with(compact(['result', 'newpost', 'Hashtag', 'AnswersFor1', 'TrueAnswersFor1', 'AnswersFor3', 'ArrangedIDs', 'Spaces', 'AnswersFor2', 'SetOfSpaceIDs', 'AnswersFor4', 'FillCharacterIDs', 'subquestions', 'AnswersFor5', 'Subquestions', 'QuestionFor5IDs', 'AnswersFor6', 'DragDropIDs', 'CompleteAnswersFor6'])); }
public function viewPost($postID) { if (!auth() || !auth()->user()) { $browser = get_browser(null, true); // Allow crawler && Facebook Bot view post without logging in. if ($browser['crawler'] != 1 && stripos($_SERVER["HTTP_USER_AGENT"], 'facebook') === false && stripos($_SERVER["HTTP_USER_AGENT"], 'face') === false && stripos($_SERVER["HTTP_USER_AGENT"], 'google') === false) { $redirectPath = '/post/' . $postID; return redirect('/auth/login')->with('redirectPath', $redirectPath); } $token = md5(rand(), false); $DisplayedQuestions = ConstsAndFuncs::$FreeQuestionsForCrawler; } $post = Posts::find($postID); if (count($post) < 1) { return view('errors.404'); } $post->visited++; $post->update(); $post = $post->toArray(); $courseID = $post['CourseID']; if (auth() && auth()->user()) { $userID = auth()->user()->getAuthIdentifier(); $tmp = Learning::where('UserID', '=', $userID)->where('CourseID', '=', $courseID)->get()->toArray(); if (count($tmp) < 1) { $learning = new Learning(); $learning->UserID = $userID; $learning->CourseID = $courseID; $learning->save(); $course = Courses::find($courseID); $course->NoOfUsers++; $course->update(); } // Insert a new record into DoExams Table to mark the time user start answering questions in post. $exam = new Doexams(); $exam->UserID = $userID; $exam->PostID = $postID; $token = md5($userID . rand(), false) . md5($postID . rand(), false); $exam->token = $token; $exam->save(); // Check if user is vip or not $user = User::find(auth()->user()->getAuthIdentifier()); if ($user['vip'] == 0) { $DisplayedQuestions = $post['FreeQuestions']; } else { $DisplayedQuestions = new \DateTime($user['expire_at']) >= new \DateTime() ? -1 : $post['FreeQuestions']; } if ($user['admin'] == 1) { $DisplayedQuestions = -1; } } $photo = $post['Photo']; if ($DisplayedQuestions > 0) { $questions = Questions::where('PostID', '=', $postID)->take($DisplayedQuestions)->get()->toArray(); } else { $questions = Questions::where('PostID', '=', $postID)->get()->toArray(); } $bundle = array(); $bundleAnswer = array(); $maxscore = 0; foreach ($questions as $q) { $answer = Answers::where('QuestionID', '=', $q['id'])->get()->toArray(); $bundle += array($q['id'] => $answer); $bundleAnswer += [$q['id'] => AnswersController::getAnswer($q['id'])]; if (count($answer) > 0) { $maxscore++; } } $Comments = Comments::all()->toArray(); $result = array('Comments' => json_encode($Comments), 'Title' => $post['Title'], 'Description' => $post['Description'], 'PostID' => $postID, 'Thumbnail' => $post['ThumbnailID'], 'Questions' => $questions, 'Photo' => $photo, 'Video' => $post['Video'], 'Bundle' => $bundle, 'BundleAnswers' => $bundleAnswer, 'MaxScore' => $maxscore, 'NumOfQuestions' => count($questions = Questions::where('PostID', '=', $postID)->get()->toArray()), 'Token' => $token, 'DisplayedQuestions' => $DisplayedQuestions); $nextPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '>=', $post['id'])->get()->toArray(); $result += ['NextPost' => count($nextPost) > 1 ? $nextPost[1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->first()->toArray()['id']]; $previousPost = Posts::where('CourseID', '=', $post['CourseID'])->where('id', '<', $post['id'])->get()->toArray(); $result += ['PreviousPost' => count($previousPost) > 0 ? $previousPost[count($previousPost) - 1]['id'] : Posts::where('CourseID', '=', $post['CourseID'])->orderBy('created_at', 'desc')->first()->toArray()['id']]; $newpost = array_merge($nextPost, $previousPost); $result += ['newpost' => $newpost]; // dd($newpost); // return view('viewpost')->with(compact(['result', 'newpost'])); if ($post['FormatID'] == 1) { return view('viewpost', $result); } if ($post['FormatID'] == 2) { return view('viewfilledpost')->with($result); } }