Added timestamps for questions

main
Allen Taylor 5 years ago committed by Igor Scheller
parent f4f3487c93
commit 6c177d5173

@ -0,0 +1,33 @@
<?php
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddTimestampsToQuestions extends Migration
{
use ChangesReferences;
/**
* Run the migration
*/
public function up(): void
{
$this->schema->table('questions', function (Blueprint $table) {
$table->timestamp('answered_at')->after('answerer_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migration
*/
public function down(): void
{
$this->schema->table('questions', function (Blueprint $table) {
$table->dropColumn('answered_at');
$table->dropTimestamps();
});
}
}

@ -1,5 +1,6 @@
<?php <?php
use Carbon\Carbon;
use Engelsystem\Models\Question; use Engelsystem\Models\Question;
/** /**
@ -50,6 +51,7 @@ function admin_questions()
$unanswered_questions_table[] = [ $unanswered_questions_table[] = [
'from' => User_Nick_render($user_source) . User_Pronoun_render($user_source), 'from' => User_Nick_render($user_source) . User_Pronoun_render($user_source),
'question' => nl2br(htmlspecialchars($question->text)), 'question' => nl2br(htmlspecialchars($question->text)),
'created_at' => $question->created_at,
'answer' => form([ 'answer' => form([
form_textarea('answer', '', ''), form_textarea('answer', '', ''),
form_submit('submit', __('Send')) form_submit('submit', __('Send'))
@ -70,8 +72,10 @@ function admin_questions()
$answered_questions_table[] = [ $answered_questions_table[] = [
'from' => User_Nick_render($user_source), 'from' => User_Nick_render($user_source),
'question' => nl2br(htmlspecialchars($question->text)), 'question' => nl2br(htmlspecialchars($question->text)),
'created_at' => $question->created_at,
'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)),
'answered_at' => $question->answered_at,
'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->id])) ], page_link_to('admin_questions', ['action' => 'delete', 'id' => $question->id]))
@ -83,6 +87,7 @@ function admin_questions()
table([ table([
'from' => __('From'), 'from' => __('From'),
'question' => __('Question'), 'question' => __('Question'),
'created_at' => __('Asked at'),
'answer' => __('Answer'), 'answer' => __('Answer'),
'actions' => '' 'actions' => ''
], $unanswered_questions_table), ], $unanswered_questions_table),
@ -90,8 +95,10 @@ function admin_questions()
table([ table([
'from' => __('From'), 'from' => __('From'),
'question' => __('Question'), 'question' => __('Question'),
'created_at' => __('Asked at'),
'answered_by' => __('Answered by'), 'answered_by' => __('Answered by'),
'answer' => __('Answer'), 'answer' => __('Answer'),
'answered_at' => __('Answered at'),
'actions' => '' 'actions' => ''
], $answered_questions_table) ], $answered_questions_table)
]); ]);
@ -115,10 +122,12 @@ function admin_questions()
if (!empty($answer)) { if (!empty($answer)) {
$question->answerer_id = $user->id; $question->answerer_id = $user->id;
$question->answer = $answer; $question->answer = $answer;
$question->answered_at = Carbon::now();
$question->save(); $question->save();
engelsystem_log( engelsystem_log(
'Question ' 'Question '
. $question['Question'] . $question->text
. ' (' . $question->id . ')'
. ' answered: ' . ' answered: '
. $answer . $answer
); );
@ -144,7 +153,7 @@ function admin_questions()
$question = Question::find($question_id); $question = Question::find($question_id);
if (!empty($question)) { if (!empty($question)) {
$question->delete(); $question->delete();
engelsystem_log('Question deleted: ' . $question['Question']); engelsystem_log('Question deleted: ' . $question->text);
throw_redirect(page_link_to('admin_questions')); throw_redirect(page_link_to('admin_questions'));
} else { } else {
return error('No question found.', true); return error('No question found.', true);

@ -20,6 +20,7 @@ function Questions_view(array $open_questions, array $answered_questions, $ask_a
page_link_to('user_questions', ['action' => 'delete', 'id' => $question->id]) page_link_to('user_questions', ['action' => 'delete', 'id' => $question->id])
), ),
'Question' => nl2br(htmlspecialchars($question->text)), 'Question' => nl2br(htmlspecialchars($question->text)),
'created_at' => $question->created_at,
]; ];
}, },
$open_questions $open_questions
@ -29,8 +30,10 @@ function Questions_view(array $open_questions, array $answered_questions, $ask_a
static function (Question $question): array { static function (Question $question): array {
return [ return [
'Question' => nl2br(htmlspecialchars($question->text)), 'Question' => nl2br(htmlspecialchars($question->text)),
'created_at' => $question->created_at,
'Answer' => nl2br(htmlspecialchars($question->answer)), 'Answer' => nl2br(htmlspecialchars($question->answer)),
'answer_user' => User_Nick_render($question->answerer), 'answer_user' => User_Nick_render($question->answerer),
'answered_at' => $question->answered_at,
'actions' => form( 'actions' => form(
[ [
form_submit('submit', __('delete'), 'btn-default btn-xs') form_submit('submit', __('delete'), 'btn-default btn-xs')
@ -47,13 +50,16 @@ function Questions_view(array $open_questions, array $answered_questions, $ask_a
heading(__('Open questions'), 2), heading(__('Open questions'), 2),
table([ table([
'Question' => __('Question'), 'Question' => __('Question'),
'created_at' => __('Asked at'),
'actions' => '' 'actions' => ''
], $open_questions), ], $open_questions),
heading(__('Answered questions'), 2), heading(__('Answered questions'), 2),
table([ table([
'Question' => __('Question'), 'Question' => __('Question'),
'created_at' => __('Asked at'),
'answer_user' => __('Answered by'), 'answer_user' => __('Answered by'),
'Answer' => __('Answer'), 'Answer' => __('Answer'),
'answered_at' => __('Answered at'),
'actions' => '' 'actions' => ''
], $answered_questions), ], $answered_questions),
heading(__('Ask the Heaven'), 2), heading(__('Ask the Heaven'), 2),

@ -1336,6 +1336,14 @@ msgstr "Beantwortete Fragen"
msgid "Answered by" msgid "Answered by"
msgstr "Antwort von" msgstr "Antwort von"
#: includes/pages/admin_questions.php:102 includes/view/Questions_view.php:40
msgid "Answered at"
msgstr "Beantwortet am"
#: includes/pages/admin_questions.php:91 includes/view/Questions_view.php:37
msgid "Asked at"
msgstr "Gefragt am"
#: includes/pages/admin_rooms.php:7 includes/pages/user_shifts.php:230 #: includes/pages/admin_rooms.php:7 includes/pages/user_shifts.php:230
#: includes/sys_menu.php:118 includes/sys_menu.php:172 #: includes/sys_menu.php:118 includes/sys_menu.php:172
msgid "Rooms" msgid "Rooms"

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Engelsystem\Models; namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\Models\User\UsesUserModel; use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@ -15,7 +16,12 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
* @property string $text * @property string $text
* @property string $answer * @property string $answer
* @property int $answerer_id * @property int $answerer_id
* @property Carbon|null $answered_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property-read User $answerer * @property-read User $answerer
*
* @method static Builder|Question whereAnswer($value) * @method static Builder|Question whereAnswer($value)
* @method static Builder|Question whereAnswererId($value) * @method static Builder|Question whereAnswererId($value)
* @method static Builder|Question whereId($value) * @method static Builder|Question whereId($value)
@ -25,12 +31,21 @@ class Question extends BaseModel
{ {
use UsesUserModel; use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var string[] */
protected $dates = [
'answered_at'
];
/** @var string[] */ /** @var string[] */
protected $fillable = [ protected $fillable = [
'user_id', 'user_id',
'text', 'text',
'answerer_id', 'answerer_id',
'answer', 'answer',
'answered_at',
]; ];
/** @var string[] */ /** @var string[] */

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Engelsystem\Test\Unit\Models; namespace Engelsystem\Test\Unit\Models;
use Carbon\Carbon;
use Engelsystem\Models\Question; use Engelsystem\Models\Question;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -77,6 +78,8 @@ class QuestionTest extends ModelTest
$this->assertCount(2, $unAnsweredQuestionIds); $this->assertCount(2, $unAnsweredQuestionIds);
$this->assertContains($question1->id, $unAnsweredQuestionIds); $this->assertContains($question1->id, $unAnsweredQuestionIds);
$this->assertContains($question2->id, $unAnsweredQuestionIds); $this->assertContains($question2->id, $unAnsweredQuestionIds);
$this->assertNull(Question::find($question1->id)->answered_at);
} }
/** /**
@ -94,6 +97,8 @@ class QuestionTest extends ModelTest
$this->assertCount(2, $answeredQuestionIds); $this->assertCount(2, $answeredQuestionIds);
$this->assertContains($question1->id, $answeredQuestionIds); $this->assertContains($question1->id, $answeredQuestionIds);
$this->assertContains($question2->id, $answeredQuestionIds); $this->assertContains($question2->id, $answeredQuestionIds);
$this->assertInstanceOf(Carbon::class, Question::find($question1->id)->answered_at);
} }
/** /**
@ -112,6 +117,7 @@ class QuestionTest extends ModelTest
$data += [ $data += [
'answerer_id' => $answerer->id, 'answerer_id' => $answerer->id,
'answer' => Str::random(), 'answer' => Str::random(),
'answered_at' => Carbon::now(),
]; ];
} }

Loading…
Cancel
Save