summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Weimann <mail@michael-weimann.eu>2019-12-03 20:09:45 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-12-07 21:29:11 +0100
commite16e0b2644172f098956f6c970e86581e2e758c1 (patch)
tree6218bff227b508df61c7184dbbcb287bf2612e8f
parent4f63bbbaacd0db3636f1169f5ad29f1cfb21615b (diff)
Migrate Question usages
-rw-r--r--includes/pages/admin_questions.php67
-rw-r--r--includes/pages/user_questions.php45
-rw-r--r--includes/view/Questions_view.php51
-rw-r--r--src/Controllers/Metrics/Stats.php6
4 files changed, 77 insertions, 92 deletions
diff --git a/includes/pages/admin_questions.php b/includes/pages/admin_questions.php
index 04868e32..54d5509b 100644
--- a/includes/pages/admin_questions.php
+++ b/includes/pages/admin_questions.php
@@ -1,7 +1,6 @@
<?php
-use Engelsystem\Database\DB;
-use Engelsystem\Models\User\User;
+use Engelsystem\Models\Question;
/**
* @return string
@@ -20,9 +19,8 @@ function admin_new_questions()
{
if (current_page() != 'admin_questions') {
if (auth()->can('admin_questions')) {
- $new_messages = count(DB::select('SELECT `QID` FROM `Questions` WHERE `AID` IS NULL'));
-
- if ($new_messages > 0) {
+ $unanswered_questions = Question::unanswered()->count();
+ if ($unanswered_questions > 0) {
return '<a href="' . page_link_to('admin_questions') . '">'
. __('There are unanswered questions!')
. '</a>';
@@ -43,36 +41,40 @@ function admin_questions()
if (!$request->has('action')) {
$unanswered_questions_table = [];
- $questions = DB::select('SELECT * FROM `Questions` WHERE `AID` IS NULL');
- foreach ($questions as $question) {
- $user_source = User::find($question['UID']);
+ $unanswered_questions = Question::unanswered()->get();
+
+ foreach ($unanswered_questions as $question) {
+ /* @var Question $question */
+ $user_source = $question->user;
$unanswered_questions_table[] = [
'from' => User_Nick_render($user_source),
- 'question' => nl2br(htmlspecialchars($question['Question'])),
+ 'question' => nl2br(htmlspecialchars($question->text)),
'answer' => form([
form_textarea('answer', '', ''),
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([
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 = [];
- $questions = DB::select('SELECT * FROM `Questions` WHERE NOT `AID` IS NULL');
- foreach ($questions as $question) {
- $user_source = User::find($question['UID']);
- $answer_user_source = User::find($question['AID']);
+ $answered_questions = Question::answered()->get();
+
+ foreach ($answered_questions as $question) {
+ /* @var Question $question */
+ $user_source = $question->user;
+ $answer_user_source = $question->answerer;
$answered_questions_table[] = [
'from' => User_Nick_render($user_source),
- 'question' => nl2br(htmlspecialchars($question['Question'])),
+ 'question' => nl2br(htmlspecialchars($question->text)),
'answered_by' => User_Nick_render($answer_user_source),
- 'answer' => nl2br(htmlspecialchars($question['Answer'])),
+ 'answer' => nl2br(htmlspecialchars($question->answer)),
'actions' => form([
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);
}
- $question = DB::selectOne(
- 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
- [$question_id]
- );
- if (!empty($question) && empty($question['AID'])) {
+ $question = Question::find($question_id);
+ if (!empty($question) && empty($question->answerer_id)) {
$answer = trim($request->input('answer'));
if (!empty($answer)) {
- DB::update('
- UPDATE `Questions`
- SET `AID`=?, `Answer`=?
- WHERE `QID`=?
- LIMIT 1
- ',
- [
- $user->id,
- $answer,
- $question_id,
- ]
- );
+ $question->answerer_id = $user->id;
+ $question->answer = $answer;
+ $question->save();
engelsystem_log(
'Question '
. $question['Question']
@@ -151,12 +141,9 @@ function admin_questions()
return error('Incomplete call, missing Question ID.', true);
}
- $question = DB::selectOne(
- 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
- [$question_id]
- );
+ $question = Question::find($question_id);
if (!empty($question)) {
- DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]);
+ $question->delete();
engelsystem_log('Question deleted: ' . $question['Question']);
redirect(page_link_to('admin_questions'));
} else {
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 @@
<?php
-use Engelsystem\Database\DB;
-use Engelsystem\Models\User\User;
+use Engelsystem\Models\Question;
/**
* @return string
@@ -20,24 +19,12 @@ function user_questions()
$request = request();
if (!$request->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(), [
diff --git a/includes/view/Questions_view.php b/includes/view/Questions_view.php
index 4d57edf9..a4c3a452 100644
--- a/includes/view/Questions_view.php
+++ b/includes/view/Questions_view.php
@@ -1,27 +1,46 @@
<?php
+use Engelsystem\Models\Question;
+
/**
- * @param array[] $open_questions
- * @param array[] $answered_questions
+ * @param Question[] $open_questions
+ * @param Question[] $answered_questions
* @param string $ask_action
* @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) {
- $question['actions'] = form([
- form_submit('submit', __('delete'), 'btn-default btn-xs')
- ], page_link_to('user_questions', ['action' => 'delete', 'id' => $question['QID']]));
- $question['Question'] = nl2br(htmlspecialchars($question['Question']));
- }
+ $open_questions = array_map(
+ static function (Question $question): array {
+ return [
+ 'actions' => form(
+ [
+ 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) {
- $question['Question'] = nl2br(htmlspecialchars($question['Question']));
- $question['Answer'] = nl2br(htmlspecialchars($question['Answer']));
- $question['actions'] = form([
- form_submit('submit', __('delete'), 'btn-default btn-xs')
- ], page_link_to('user_questions', ['action' => 'delete', 'id' => $question['QID']]));
- }
+ $answered_questions = array_map(
+ static function (Question $question): array {
+ return [
+ 'Question' => nl2br(htmlspecialchars($question->text)),
+ 'Answer' => nl2br(htmlspecialchars($question->answer)),
+ '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(), [
msg(),
diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php
index 2d58f999..f1ef247b 100644
--- a/src/Controllers/Metrics/Stats.php
+++ b/src/Controllers/Metrics/Stats.php
@@ -226,13 +226,13 @@ class Stats
public function questions($answered = null)
{
$query = $this
- ->getQuery('Questions');
+ ->getQuery('questions');
if (!is_null($answered)) {
if ($answered) {
- $query->whereNotNull('AID');
+ $query->whereNotNull('answerer_id');
} else {
- $query->whereNull('AID');
+ $query->whereNull('answerer_id');
}
}