2011-06-03 00:22:11 +02:00
|
|
|
<?php
|
2016-09-29 10:53:17 +02:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function admin_questions_title()
|
|
|
|
{
|
2018-08-29 21:55:32 +02:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
global $privileges, $page;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
if ($page != 'admin_questions') {
|
|
|
|
if (in_array('admin_questions', $privileges)) {
|
2017-01-21 13:58:53 +01:00
|
|
|
$new_messages = count(DB::select('SELECT `QID` FROM `Questions` WHERE `AID` IS NULL'));
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($new_messages > 0) {
|
2017-12-25 23:12:52 +01:00
|
|
|
return '<a href="' . page_link_to('admin_questions') . '">'
|
2018-08-29 21:55:32 +02:00
|
|
|
. __('There are unanswered questions!')
|
2017-12-25 23:12:52 +01:00
|
|
|
. '</a>';
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 10:53:17 +02: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-08 21:15:56 +02:00
|
|
|
$user = Auth()->user();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if (!$request->has('action')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$unanswered_questions_table = [];
|
2017-01-21 13:58:53 +01:00
|
|
|
$questions = DB::select('SELECT * FROM `Questions` WHERE `AID` IS NULL');
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
$user_source = User($question['UID']);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$unanswered_questions_table[] = [
|
2017-01-02 15:43:36 +01:00
|
|
|
'from' => User_Nick_render($user_source),
|
2017-01-03 14:12:17 +01:00
|
|
|
'question' => str_replace("\n", '<br />', $question['Question']),
|
2017-01-02 15:43:36 +01:00
|
|
|
'answer' => form([
|
|
|
|
form_textarea('answer', '', ''),
|
2018-08-29 21:55:32 +02:00
|
|
|
form_submit('submit', __('Save'))
|
2017-08-28 16:21:10 +02:00
|
|
|
], page_link_to('admin_questions', ['action' => 'answer', 'id' => $question['QID']])),
|
2017-01-02 15:43:36 +01:00
|
|
|
'actions' => button(
|
2017-08-28 16:21:10 +02:00
|
|
|
page_link_to('admin_questions', ['action' => 'delete', 'id' => $question['QID']]),
|
2018-08-29 21:55:32 +02:00
|
|
|
__('delete'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'btn-xs'
|
|
|
|
)
|
|
|
|
];
|
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 = [];
|
2017-01-21 13:58:53 +01:00
|
|
|
$questions = DB::select('SELECT * FROM `Questions` WHERE NOT `AID` IS NULL');
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($questions as $question) {
|
|
|
|
$user_source = User($question['UID']);
|
|
|
|
$answer_user_source = User($question['AID']);
|
|
|
|
$answered_questions_table[] = [
|
2017-01-02 15:43:36 +01:00
|
|
|
'from' => User_Nick_render($user_source),
|
2017-01-03 14:12:17 +01:00
|
|
|
'question' => str_replace("\n", '<br />', $question['Question']),
|
2017-01-02 15:43:36 +01:00
|
|
|
'answered_by' => User_Nick_render($answer_user_source),
|
2017-01-03 14:12:17 +01:00
|
|
|
'answer' => str_replace("\n", '<br />', $question['Answer']),
|
2017-01-02 15:43:36 +01:00
|
|
|
'actions' => button(
|
2017-08-28 16:21:10 +02:00
|
|
|
page_link_to('admin_questions', ['action' => 'delete', 'id' => $question['QID']]),
|
2018-08-29 21:55:32 +02:00
|
|
|
__('delete'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'btn-xs'
|
|
|
|
)
|
|
|
|
];
|
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(), [
|
2018-08-29 21:55:32 +02:00
|
|
|
'<h2>' . __('Unanswered questions') . '</h2>',
|
2017-01-02 15:43:36 +01:00
|
|
|
table([
|
2018-08-29 21:55:32 +02:00
|
|
|
'from' => __('From'),
|
|
|
|
'question' => __('Question'),
|
|
|
|
'answer' => __('Answer'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'actions' => ''
|
|
|
|
], $unanswered_questions_table),
|
2018-08-29 21:55:32 +02:00
|
|
|
'<h2>' . __('Answered questions') . '</h2>',
|
2017-01-02 15:43:36 +01:00
|
|
|
table([
|
2018-08-29 21:55:32 +02:00
|
|
|
'from' => __('From'),
|
|
|
|
'question' => __('Question'),
|
|
|
|
'answered_by' => __('Answered by'),
|
|
|
|
'answer' => __('Answer'),
|
2017-01-02 15:43:36 +01:00
|
|
|
'actions' => ''
|
|
|
|
], $answered_questions_table)
|
|
|
|
]);
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
2017-07-18 21:38:53 +02:00
|
|
|
switch ($request->input('action')) {
|
2017-01-02 15:43:36 +01:00
|
|
|
case 'answer':
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('id') && preg_match('/^\d{1,11}$/', $request->input('id'))) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
$question = DB::selectOne(
|
2017-01-21 13:58:53 +01:00
|
|
|
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
|
|
|
|
[$question_id]
|
|
|
|
);
|
2018-01-14 17:47:26 +01:00
|
|
|
if (!empty($question) && empty($question['AID'])) {
|
2017-01-02 15:43:36 +01:00
|
|
|
$answer = trim(
|
|
|
|
preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui",
|
|
|
|
'',
|
2017-07-18 21:38:53 +02:00
|
|
|
strip_tags($request->input('answer'))
|
2017-01-02 15:43:36 +01:00
|
|
|
));
|
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
if ($answer != '') {
|
2017-01-22 01:02:52 +01:00
|
|
|
DB::update('
|
2017-01-21 13:58:53 +01:00
|
|
|
UPDATE `Questions`
|
|
|
|
SET `AID`=?, `Answer`=?
|
|
|
|
WHERE `QID`=?
|
|
|
|
LIMIT 1
|
|
|
|
',
|
|
|
|
[
|
2018-10-08 21:15:56 +02:00
|
|
|
$user->id,
|
2017-01-21 13:58:53 +01:00
|
|
|
$answer,
|
|
|
|
$question_id,
|
|
|
|
]
|
|
|
|
);
|
2017-07-28 20:11:09 +02:00
|
|
|
engelsystem_log('Question ' . $question['Question'] . ' answered: ' . $answer);
|
2017-01-03 14:12:17 +01:00
|
|
|
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':
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('id') && preg_match('/^\d{1,11}$/', $request->input('id'))) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-07-28 20:11:09 +02:00
|
|
|
$question = DB::selectOne(
|
2017-01-21 13:58:53 +01:00
|
|
|
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
|
|
|
|
[$question_id]
|
|
|
|
);
|
2017-07-28 20:11:09 +02:00
|
|
|
if (!empty($question)) {
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]);
|
2017-07-28 20:11:09 +02:00
|
|
|
engelsystem_log('Question deleted: ' . $question['Question']);
|
2017-01-03 14:12:17 +01:00
|
|
|
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;
|
2016-09-29 10:53:17 +02:00
|
|
|
}
|
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
|
|
|
}
|