QuestionEditController::editAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Egzaminer\Controller;
4
5
use Egzaminer\Model\AnswersModel;
6
use Egzaminer\Model\QuestionEditModel;
7
use Egzaminer\Model\QuestionsModel;
8
9
class QuestionEditController extends AdminController
10
{
11
    /**
12
     * Edit question.
13
     *
14
     * GET /admin/exam/edit/[i:id]/question/edit/[i:qid]
15
     *
16
     * @param int $examID     Exam id
17
     * @param int $questionID Question ID
18
     *
19
     * @return string
20
     */
21
    public function editAction(int $examID, int $questionID): string
22
    {
23
        $question = (new QuestionsModel($this->get('dbh')))->getByQuestionId($questionID);
24
        $answers = (new AnswersModel($this->get('dbh')))->getAnswersByOneQuestionId($questionID);
25
26
        return $this->render('admin/question', [
27
            'title'        => 'Edycja pytania',
28
            'id'           => $questionID,
29
            'examID'       => $examID,
30
            'question'     => $question,
31
            'answers'      => $answers,
32
            'templateType' => 'edit',
33
        ]);
34
    }
35
36
    /**
37
     * Edit question post action.
38
     *
39
     * POST /admin/exam/edit/[i:id]/question/edit/[i:qid]
40
     *
41
     * @param int $examID     Exam id
42
     * @param int $questionID Question ID
43
     *
44
     * @return void
45
     */
46
    public function postEditAction(int $examID, int $questionID)
47
    {
48
        $editModel = new QuestionEditModel($this->get('dbh'));
49
50
        if ($editModel->edit(
51
                $questionID,
52
                $this->getFromRequest('post'),
53
                $this->getFromRequest('files', 'image'),
54
                $this->get('rootDir')
55
            )) {
56
            $this->setMessage('success', 'Uaktualniono pomyślnie!');
57
        } else {
58
            $this->setMessage('warning', 'Coś się zepsuło!');
59
        }
60
61
        $this->redirect('/admin/exam/edit/'.$examID.'/question/edit/'.$questionID);
62
    }
63
}
64