From e16e0b2644172f098956f6c970e86581e2e758c1 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 3 Dec 2019 20:09:45 +0100 Subject: Migrate Question usages --- includes/pages/user_questions.php | 45 +++++++++++---------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) (limited to 'includes/pages/user_questions.php') diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index 29925a4f..ab836c77 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -1,7 +1,6 @@ has('action')) { - $open_questions = DB::select( - 'SELECT * FROM `Questions` WHERE `AID` IS NULL AND `UID`=?', - [$user->id] - ); - - $answered_questions = DB::select( - 'SELECT * FROM `Questions` WHERE NOT `AID` IS NULL AND `UID`=?', - [$user->id] - ); - - foreach ($answered_questions as &$question) { - $answer_user_source = User::find($question['AID']); - $question['answer_user'] = User_Nick_render($answer_user_source); - } + $open_questions = $user->questionsAsked()->whereNull('answerer_id')->get(); + $answered_questions = $user->questionsAsked()->whereNotNull('answerer_id')->get(); return Questions_view( - $open_questions, - $answered_questions, + $open_questions->all(), + $answered_questions->all(), page_link_to('user_questions', ['action' => 'ask']) ); } else { @@ -45,12 +32,10 @@ function user_questions() case 'ask': $question = request()->get('question'); if (!empty($question) && $request->hasPostData('submit')) { - DB::insert(' - INSERT INTO `Questions` (`UID`, `Question`) - VALUES (?, ?) - ', - [$user->id, $question] - ); + Question::create([ + 'enquirer_id' => $user->id, + 'question' => $question, + ]); success(__('You question was saved.')); redirect(page_link_to('user_questions')); @@ -71,15 +56,9 @@ function user_questions() return error(__('Incomplete call, missing Question ID.'), true); } - $question = DB::selectOne( - 'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1', - [$question_id] - ); - if (!empty($question) && $question['UID'] == $user->id) { - DB::delete( - 'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', - [$question_id] - ); + $question = Question::find($question_id); + if (!empty($question) && $question->user_id == $user->id) { + $question->delete(); redirect(page_link_to('user_questions')); } else { return page_with_title(questions_title(), [ -- cgit v1.2.3-54-g00ecf