2011-06-02 23:45:54 +02:00
|
|
|
<?php
|
2016-09-29 10:53:17 +02:00
|
|
|
|
2019-12-03 20:09:45 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function questions_title()
|
|
|
|
{
|
2018-08-29 21:55:32 +02:00
|
|
|
return __('Ask the Heaven');
|
2013-11-25 21:04:58 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_questions()
|
|
|
|
{
|
2018-10-31 12:48:22 +01: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')) {
|
2020-11-29 20:00:15 +01:00
|
|
|
$open_questions = $user->questionsAsked()
|
|
|
|
->whereNull('answerer_id')
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
->get();
|
|
|
|
$answered_questions = $user->questionsAsked()
|
|
|
|
->whereNotNull('answerer_id')
|
|
|
|
->orderByDesc('answered_at')
|
|
|
|
->get();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-08-28 16:21:10 +02:00
|
|
|
return Questions_view(
|
2019-12-03 20:09:45 +01:00
|
|
|
$open_questions->all(),
|
|
|
|
$answered_questions->all(),
|
2017-08-28 16:21:10 +02:00
|
|
|
page_link_to('user_questions', ['action' => 'ask'])
|
|
|
|
);
|
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 'ask':
|
2018-12-28 22:34:30 +01:00
|
|
|
$question = request()->get('question');
|
|
|
|
if (!empty($question) && $request->hasPostData('submit')) {
|
2019-12-03 20:09:45 +01:00
|
|
|
Question::create([
|
2019-12-07 21:14:08 +01:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'text' => $question,
|
2019-12-03 20:09:45 +01:00
|
|
|
]);
|
2017-07-23 11:46:54 +02:00
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('You question was saved.'));
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('user_questions'));
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
|
|
|
return page_with_title(questions_title(), [
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Please enter a question!'), true)
|
2017-01-02 15:43:36 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
2018-11-20 16:02:03 +01:00
|
|
|
if (
|
|
|
|
$request->has('id')
|
|
|
|
&& preg_match('/^\d{1,11}$/', $request->input('id'))
|
|
|
|
&& $request->hasPostData('submit')
|
|
|
|
) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$question_id = $request->input('id');
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
2018-08-29 21:55:32 +02: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) && $question->user_id == $user->id) {
|
|
|
|
$question->delete();
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('user_questions'));
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
|
|
|
return page_with_title(questions_title(), [
|
2018-08-29 21:55:32 +02:00
|
|
|
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-02 23:45:54 +02:00
|
|
|
}
|