engelsystem/includes/pages/admin_questions.php

167 lines
6.0 KiB
PHP
Raw Normal View History

2011-06-03 00:22:11 +02:00
<?php
2019-10-31 15:18:27 +01:00
use Carbon\Carbon;
2019-12-03 20:09:45 +01:00
use Engelsystem\Models\Question;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_questions_title()
{
return __('Answer questions');
2013-11-25 21:04:58 +01:00
}
2016-11-15 16:28:20 +01:00
/**
* Renders a hint for new questions to answer.
2017-01-03 03:22:48 +01:00
*
* @return string|null
2016-11-15 16:28:20 +01:00
*/
2017-01-02 03:57:23 +01:00
function admin_new_questions()
{
2019-11-26 20:12:43 +01:00
if (current_page() != 'admin_questions') {
if (auth()->can('admin_questions')) {
2019-12-03 20:09:45 +01:00
$unanswered_questions = Question::unanswered()->count();
if ($unanswered_questions > 0) {
2017-12-25 23:12:52 +01:00
return '<a href="' . page_link_to('admin_questions') . '">'
. __('There are unanswered questions!')
2017-12-25 23:12:52 +01:00
. '</a>';
2017-01-02 03:57:23 +01:00
}
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return null;
2011-06-03 00:22:11 +02:00
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_questions()
{
2018-10-31 12:48:22 +01:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
if (!$request->has('action')) {
2017-01-02 03:57:23 +01:00
$unanswered_questions_table = [];
2020-11-29 20:00:15 +01:00
$unanswered_questions = Question::unanswered()->orderByDesc('created_at')->get();
2019-12-03 20:09:45 +01:00
foreach ($unanswered_questions as $question) {
/* @var Question $question */
$user_source = $question->user;
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$unanswered_questions_table[] = [
2019-10-31 15:18:27 +01:00
'from' => User_Nick_render($user_source) . User_Pronoun_render($user_source),
'question' => nl2br(htmlspecialchars($question->text)),
'created_at' => $question->created_at,
'answer' => form([
2017-01-02 15:43:36 +01:00
form_textarea('answer', '', ''),
form_submit('submit', __('Send'))
2019-12-03 20:09:45 +01:00
], page_link_to('admin_questions', ['action' => 'answer', 'id' => $question->id])),
2019-10-31 15:18:27 +01:00
'actions' => form([
form_submit('submit', __('delete'), 'btn-xs'),
2019-12-03 20:09:45 +01:00
], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question->id])),
2017-01-02 15:43:36 +01:00
];
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$answered_questions_table = [];
2020-11-29 20:00:15 +01:00
$answered_questions = Question::answered()->orderByDesc('answered_at')->get();
2019-12-03 20:09:45 +01:00
foreach ($answered_questions as $question) {
/* @var Question $question */
$user_source = $question->user;
$answer_user_source = $question->answerer;
2017-01-02 03:57:23 +01:00
$answered_questions_table[] = [
2017-01-02 15:43:36 +01:00
'from' => User_Nick_render($user_source),
2019-12-03 20:09:45 +01:00
'question' => nl2br(htmlspecialchars($question->text)),
2019-10-31 15:18:27 +01:00
'created_at' => $question->created_at,
2017-01-02 15:43:36 +01:00
'answered_by' => User_Nick_render($answer_user_source),
2019-12-03 20:09:45 +01:00
'answer' => nl2br(htmlspecialchars($question->answer)),
2019-10-31 15:18:27 +01:00
'answered_at' => $question->answered_at,
'actions' => form([
form_submit('submit', __('delete'), 'btn-xs')
2019-12-03 20:09:45 +01:00
], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question->id]))
2017-01-02 15:43:36 +01:00
];
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return page_with_title(admin_questions_title(), [
'<h2>' . __('Unanswered questions') . '</h2>',
2017-01-02 15:43:36 +01:00
table([
2019-10-31 15:18:27 +01:00
'from' => __('From'),
'question' => __('Question'),
'created_at' => __('Asked at'),
'answer' => __('Answer'),
'actions' => ''
2017-01-02 15:43:36 +01:00
], $unanswered_questions_table),
'<h2>' . __('Answered questions') . '</h2>',
2017-01-02 15:43:36 +01:00
table([
'from' => __('From'),
'question' => __('Question'),
2019-10-31 15:18:27 +01:00
'created_at' => __('Asked at'),
'answered_by' => __('Answered by'),
'answer' => __('Answer'),
2019-10-31 15:18:27 +01:00
'answered_at' => __('Answered at'),
2017-01-02 15:43:36 +01:00
'actions' => ''
], $answered_questions_table)
]);
2017-01-02 03:57:23 +01:00
} else {
switch ($request->input('action')) {
2017-01-02 15:43:36 +01:00
case 'answer':
if (
$request->has('id')
&& preg_match('/^\d{1,11}$/', $request->input('id'))
&& $request->hasPostData('submit')
) {
$question_id = $request->input('id');
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('Incomplete call, missing Question ID.', true);
2017-01-02 15:43:36 +01:00
}
2019-12-03 20:09:45 +01:00
$question = Question::find($question_id);
if (!empty($question) && empty($question->answerer_id)) {
$answer = trim($request->input('answer'));
2017-01-02 15:43:36 +01:00
if (!empty($answer)) {
2019-12-03 20:09:45 +01:00
$question->answerer_id = $user->id;
$question->answer = $answer;
2019-10-31 15:18:27 +01:00
$question->answered_at = Carbon::now();
2019-12-03 20:09:45 +01:00
$question->save();
engelsystem_log(
'Question '
2019-10-31 15:18:27 +01:00
. $question->text
. ' (' . $question->id . ')'
. ' answered: '
2019-05-31 04:03:19 +02:00
. $answer
);
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('admin_questions'));
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('Enter an answer!', true);
2017-01-02 15:43:36 +01:00
}
} else {
2017-01-03 14:12:17 +01:00
return error('No question found.', true);
2017-01-02 15:43:36 +01:00
}
break;
case 'delete':
if (
$request->has('id')
&& preg_match('/^\d{1,11}$/', $request->input('id'))
&& $request->hasPostData('submit')
) {
$question_id = $request->input('id');
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('Incomplete call, missing Question ID.', true);
2017-01-02 15:43:36 +01:00
}
2019-12-03 20:09:45 +01:00
$question = Question::find($question_id);
if (!empty($question)) {
2019-12-03 20:09:45 +01:00
$question->delete();
2019-10-31 15:18:27 +01:00
engelsystem_log('Question deleted: ' . $question->text);
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('admin_questions'));
2017-01-02 15:43:36 +01:00
} else {
2017-01-03 14:12:17 +01:00
return error('No question found.', true);
2017-01-02 15:43:36 +01:00
}
break;
}
2017-01-02 03:57:23 +01:00
}
2017-01-03 03:22:48 +01:00
return '';
2011-06-03 00:22:11 +02:00
}