public static function destroy($SpaceID)
 {
     if (!AuthController::checkPermission()) {
         return redirect('/');
     }
     $space = Spaces::find($SpaceID);
     $answers = Answers::where('SpaceID', '=', $SpaceID)->get()->toArray();
     foreach ($answers as $answer) {
         Answers::destroy($answer['id']);
     }
     $space->delete();
     return;
 }
Example #2
0
 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']));
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 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 = Posts::find($postid)['FormatID'];
     if ($format == 1) {
         $answers = Answers::where('QuestionID', '=', $id)->get()->toArray();
         foreach ($answers as $answer) {
             Answers::destroy($answer['id']);
         }
     } else {
         if ($format == 2) {
             $spaces = Spaces::where('QuestionID', '=', $id)->get()->toArray();
             foreach ($spaces as $value) {
                 SpacesController::destroy($value['id']);
             }
         }
     }
     $question->delete();
     return redirect(route('user.viewpost', $postid));
 }
 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));
 }
			@if ((auth()->user()) && (auth()->user()->admin == 1))
				<a style="text-decoration: none;" href="{{route('user.viewquestion', $q['id'])}}"><h3 onmouseover="this.style.color = '#f06'" onmouseout="this.style.color = '#6600cc'" class="title" id="title_question_{!! $key + 1 !!}">Câu hỏi số <?php 
echo $count_answer++;
?>
:</h3></a>
			@else
			<h3 class="title" id="title_question_{!! $key + 1 !!}">Câu hỏi số <?php 
echo $count_answer++;
?>
:</h3>
			@endif
			<h4 class="title">
			<?php 
$subP = \App\Questions::getFilledQuestion($q['Question']);
$Answers = array();
$Spaces = \App\Spaces::where('QuestionID', '=', $q['id'])->get()->toArray();
foreach ($Spaces as $value) {
    $Answers += array($value['id'] => \App\Answers::where('SpaceID', '=', $value['id'])->get()->toArray());
}
foreach ($Spaces as $key => $value) {
    $setOfSpaces = array_merge($setOfSpaces, [$value['id']]);
}
reset($Spaces);
// don't know what's different between this view & viewfilledquestion
?>
			<div style="color:#cc0066; font-weight:bold;">
			@if (strlen($q['Description']) > 0)
				{!! nl2br($q['Description']) . ":" !!}
			@endif
			</div>
			<div>