Migrate Question usages

This commit is contained in:
Michael Weimann 2019-12-03 20:09:45 +01:00 committed by Igor Scheller
parent 4f63bbbaac
commit e16e0b2644
4 changed files with 77 additions and 92 deletions

View File

@ -1,7 +1,6 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Models\Question;
use Engelsystem\Models\User\User;
/** /**
* @return string * @return string
@ -20,9 +19,8 @@ function admin_new_questions()
{ {
if (current_page() != 'admin_questions') { if (current_page() != 'admin_questions') {
if (auth()->can('admin_questions')) { if (auth()->can('admin_questions')) {
$new_messages = count(DB::select('SELECT `QID` FROM `Questions` WHERE `AID` IS NULL')); $unanswered_questions = Question::unanswered()->count();
if ($unanswered_questions > 0) {
if ($new_messages > 0) {
return '<a href="' . page_link_to('admin_questions') . '">' return '<a href="' . page_link_to('admin_questions') . '">'
. __('There are unanswered questions!') . __('There are unanswered questions!')
. '</a>'; . '</a>';
@ -43,36 +41,40 @@ function admin_questions()
if (!$request->has('action')) { if (!$request->has('action')) {
$unanswered_questions_table = []; $unanswered_questions_table = [];
$questions = DB::select('SELECT * FROM `Questions` WHERE `AID` IS NULL'); $unanswered_questions = Question::unanswered()->get();
foreach ($questions as $question) {
$user_source = User::find($question['UID']); foreach ($unanswered_questions as $question) {
/* @var Question $question */
$user_source = $question->user;
$unanswered_questions_table[] = [ $unanswered_questions_table[] = [
'from' => User_Nick_render($user_source), 'from' => User_Nick_render($user_source),
'question' => nl2br(htmlspecialchars($question['Question'])), 'question' => nl2br(htmlspecialchars($question->text)),
'answer' => form([ 'answer' => form([
form_textarea('answer', '', ''), form_textarea('answer', '', ''),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
], page_link_to('admin_questions', ['action' => 'answer', 'id' => $question['QID']])), ], page_link_to('admin_questions', ['action' => 'answer', 'id' => $question->id])),
'actions' => form([ 'actions' => form([
form_submit('submit', __('delete'), 'btn-xs'), form_submit('submit', __('delete'), 'btn-xs'),
], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question['QID']])), ], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question->id])),
]; ];
} }
$answered_questions_table = []; $answered_questions_table = [];
$questions = DB::select('SELECT * FROM `Questions` WHERE NOT `AID` IS NULL'); $answered_questions = Question::answered()->get();
foreach ($questions as $question) {
$user_source = User::find($question['UID']); foreach ($answered_questions as $question) {
$answer_user_source = User::find($question['AID']); /* @var Question $question */
$user_source = $question->user;
$answer_user_source = $question->answerer;
$answered_questions_table[] = [ $answered_questions_table[] = [
'from' => User_Nick_render($user_source), 'from' => User_Nick_render($user_source),
'question' => nl2br(htmlspecialchars($question['Question'])), 'question' => nl2br(htmlspecialchars($question->text)),
'answered_by' => User_Nick_render($answer_user_source), 'answered_by' => User_Nick_render($answer_user_source),
'answer' => nl2br(htmlspecialchars($question['Answer'])), 'answer' => nl2br(htmlspecialchars($question->answer)),
'actions' => form([ 'actions' => form([
form_submit('submit', __('delete'), 'btn-xs') form_submit('submit', __('delete'), 'btn-xs')
], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question['QID']])) ], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question->id]))
]; ];
} }
@ -106,26 +108,14 @@ function admin_questions()
return error('Incomplete call, missing Question ID.', true); return error('Incomplete call, missing Question ID.', true);
} }
$question = DB::selectOne( $question = Question::find($question_id);
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1', if (!empty($question) && empty($question->answerer_id)) {
[$question_id]
);
if (!empty($question) && empty($question['AID'])) {
$answer = trim($request->input('answer')); $answer = trim($request->input('answer'));
if (!empty($answer)) { if (!empty($answer)) {
DB::update(' $question->answerer_id = $user->id;
UPDATE `Questions` $question->answer = $answer;
SET `AID`=?, `Answer`=? $question->save();
WHERE `QID`=?
LIMIT 1
',
[
$user->id,
$answer,
$question_id,
]
);
engelsystem_log( engelsystem_log(
'Question ' 'Question '
. $question['Question'] . $question['Question']
@ -151,12 +141,9 @@ function admin_questions()
return error('Incomplete call, missing Question ID.', true); return error('Incomplete call, missing Question ID.', true);
} }
$question = DB::selectOne( $question = Question::find($question_id);
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id]
);
if (!empty($question)) { if (!empty($question)) {
DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]); $question->delete();
engelsystem_log('Question deleted: ' . $question['Question']); engelsystem_log('Question deleted: ' . $question['Question']);
redirect(page_link_to('admin_questions')); redirect(page_link_to('admin_questions'));
} else { } else {

View File

@ -1,7 +1,6 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Models\Question;
use Engelsystem\Models\User\User;
/** /**
* @return string * @return string
@ -20,24 +19,12 @@ function user_questions()
$request = request(); $request = request();
if (!$request->has('action')) { if (!$request->has('action')) {
$open_questions = DB::select( $open_questions = $user->questionsAsked()->whereNull('answerer_id')->get();
'SELECT * FROM `Questions` WHERE `AID` IS NULL AND `UID`=?', $answered_questions = $user->questionsAsked()->whereNotNull('answerer_id')->get();
[$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);
}
return Questions_view( return Questions_view(
$open_questions, $open_questions->all(),
$answered_questions, $answered_questions->all(),
page_link_to('user_questions', ['action' => 'ask']) page_link_to('user_questions', ['action' => 'ask'])
); );
} else { } else {
@ -45,12 +32,10 @@ function user_questions()
case 'ask': case 'ask':
$question = request()->get('question'); $question = request()->get('question');
if (!empty($question) && $request->hasPostData('submit')) { if (!empty($question) && $request->hasPostData('submit')) {
DB::insert(' Question::create([
INSERT INTO `Questions` (`UID`, `Question`) 'enquirer_id' => $user->id,
VALUES (?, ?) 'question' => $question,
', ]);
[$user->id, $question]
);
success(__('You question was saved.')); success(__('You question was saved.'));
redirect(page_link_to('user_questions')); redirect(page_link_to('user_questions'));
@ -71,15 +56,9 @@ function user_questions()
return error(__('Incomplete call, missing Question ID.'), true); return error(__('Incomplete call, missing Question ID.'), true);
} }
$question = DB::selectOne( $question = Question::find($question_id);
'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1', if (!empty($question) && $question->user_id == $user->id) {
[$question_id] $question->delete();
);
if (!empty($question) && $question['UID'] == $user->id) {
DB::delete(
'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id]
);
redirect(page_link_to('user_questions')); redirect(page_link_to('user_questions'));
} else { } else {
return page_with_title(questions_title(), [ return page_with_title(questions_title(), [

View File

@ -1,27 +1,46 @@
<?php <?php
use Engelsystem\Models\Question;
/** /**
* @param array[] $open_questions * @param Question[] $open_questions
* @param array[] $answered_questions * @param Question[] $answered_questions
* @param string $ask_action * @param string $ask_action
* @return string * @return string
*/ */
function Questions_view($open_questions, $answered_questions, $ask_action) function Questions_view(array $open_questions, array $answered_questions, $ask_action)
{ {
foreach ($open_questions as &$question) { $open_questions = array_map(
$question['actions'] = form([ static function (Question $question): array {
form_submit('submit', __('delete'), 'btn-default btn-xs') return [
], page_link_to('user_questions', ['action' => 'delete', 'id' => $question['QID']])); 'actions' => form(
$question['Question'] = nl2br(htmlspecialchars($question['Question'])); [
} form_submit('submit', __('delete'), 'btn-default btn-xs')
],
page_link_to('user_questions', ['action' => 'delete', 'id' => $question->id])
),
'Question' => nl2br(htmlspecialchars($question->text)),
];
},
$open_questions
);
foreach ($answered_questions as &$question) { $answered_questions = array_map(
$question['Question'] = nl2br(htmlspecialchars($question['Question'])); static function (Question $question): array {
$question['Answer'] = nl2br(htmlspecialchars($question['Answer'])); return [
$question['actions'] = form([ 'Question' => nl2br(htmlspecialchars($question->text)),
form_submit('submit', __('delete'), 'btn-default btn-xs') 'Answer' => nl2br(htmlspecialchars($question->answer)),
], page_link_to('user_questions', ['action' => 'delete', 'id' => $question['QID']])); 'answer_user' => User_Nick_render($question->answerer),
} 'actions' => form(
[
form_submit('submit', __('delete'), 'btn-default btn-xs')
],
page_link_to('user_questions', ['action' => 'delete', 'id' => $question->id])
),
];
},
$answered_questions
);
return page_with_title(questions_title(), [ return page_with_title(questions_title(), [
msg(), msg(),

View File

@ -226,13 +226,13 @@ class Stats
public function questions($answered = null) public function questions($answered = null)
{ {
$query = $this $query = $this
->getQuery('Questions'); ->getQuery('questions');
if (!is_null($answered)) { if (!is_null($answered)) {
if ($answered) { if ($answered) {
$query->whereNotNull('AID'); $query->whereNotNull('answerer_id');
} else { } else {
$query->whereNull('AID'); $query->whereNull('answerer_id');
} }
} }