public function update($QuestionID) { if (!AuthController::checkPermission()) { return redirect('/'); } $data = Request::capture(); $count = $data['numAnswer']; // delete all old spaces with corresponding answers $oldSpaces = Spaces::where('QuestionID', '=', $QuestionID)->get()->toArray(); foreach ($oldSpaces as $value) { SpacesController::destroy($value['id']); } for ($i = 0; $i < $count; $i++) { $rawAnswer = trim(AnswersController::c2s_convert($data['answer' . ($i + 1)])); preg_match_all('/([^;]+);/', $rawAnswer, $matches, PREG_PATTERN_ORDER); $arrayOfAnswer = $matches[1]; $SpaceID = DB::table('spaces')->insertGetId(['QuestionID' => $QuestionID, 'created_at' => new \DateTime(), 'updated_at' => new \DateTime()]); $true = true; foreach ($arrayOfAnswer as $value) { $a = new Answers(); $a->Logical = $true; $a->SpaceID = $SpaceID; $a->Detail = trim($value); $a->save(); $true = false; } } return redirect(route('user.viewquestion', $QuestionID)); }
public static function destroy($id) { if (!AuthController::checkPermission()) { return redirect('/'); } // return redirect('/'); $s = Subquestions::find($id); if (count($s) < 1) { return redirect('/'); } $answers = Answers::where('SubQuestionID', '=', $id)->get()->toArray(); foreach ($answers as $a) { AnswersController::destroy($a['id']); } @unlink(public_path('images/imageSubquestion/' . $s['Photo'])); $s->delete(); }
public static function destroy($id) { if (!AuthController::checkPermission()) { return redirect('/'); } $question = Questions::find($id); @unlink(public_path('images/imageQuestion/' . $question['Photo'])); $postid = $question['PostID']; $format = $question['FormatID']; switch ($format) { case 1: case 6: $answers = Answers::where('QuestionID', '=', $id)->get()->toArray(); foreach ($answers as $answer) { AnswersController::destroy($answer['id']); } break; case 2: $spaces = Spaces::where('QuestionID', '=', $id)->get()->toArray(); foreach ($spaces as $value) { SpacesController::destroy($value['id']); } break; case 3: $answers = Answers::where('QuestionID', '=', $id)->get()->toArray(); foreach ($answers as $answer) { AnswersController::destroy($answer['id']); } break; case 4: $answers = Answers::where('QuestionID', '=', $id)->get()->toArray(); foreach ($answers as $answer) { AnswersController::destroy($answer['id']); } break; case 5: $subq = Subquestions::where('QuestionID', '=', $id)->get()->toArray(); foreach ($subq as $s) { SubquestionsController::destroy($s['id']); } } $question->delete(); return redirect(route('user.viewpost', $postid)); }
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); } }