CreateQuestionForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 36 1
A callbackSubmit() 0 21 1
1
<?php
2
3
namespace Alfs18\User\HTMLForm;
4
5
use Alfs18\User\Question;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreateQuestionForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Alfs18\User\HTMLForm\Psr...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di, $acronym)
20
    {
21
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id" => __CLASS__,
25
                "legend" => "Create question",
26
            ],
27
            [
28
                "acronym" => [
29
                    "type"  => "hidden",
30
                    "value" => $acronym,
31
                ],
32
33
                "question" => [
34
                    "type"  => "textarea",
35
                ],
36
37
                "tags" => [
38
                    "type"  => "text",
39
                ],
40
41
                "points" => [
42
                    "type"  => "hidden",
43
                    "value" => 0,
44
                ],
45
46
                "created" => [
47
                    "type"  => "hidden",
48
                    "value" => date("d M Y, H:i"),
49
                ],
50
51
                "submit" => [
52
                    "type" => "submit",
53
                    "value" => "Ställ fråga",
54
                    "callback" => [$this, "callbackSubmit"]
55
                ],
56
            ]
57
        );
58
    }
59
60
61
62
    /**
63
     * Callback for submit-button which should return true if it could
64
     * carry out its work and false if something failed.
65
     *
66
     * @return boolean true if okey, false if something went wrong.
67
     */
68
    public function callbackSubmit()
69
    {
70
        // Get values from the submitted form
71
        $acronym = $this->form->value("acronym");
72
        $question = $this->form->value("question");
73
        $tags = $this->form->value("tags");
74
        $points = $this->form->value("points");
75
        $created = $this->form->value("created");
76
77
        // Save to database
78
        $quest = new Question();
79
        $quest->setDb($this->di->get("dbqb"));
80
        $quest->acronym = $acronym;
81
        $quest->question = $quest->changeCharacter($question);
82
        $quest->tags = $quest->changeCharacter($tags);
83
        $quest->points = intval($points);
84
        $quest->created = $created;
85
        $quest->saveQuestion($this->di);
86
87
        $this->form->addOutput("Question was created.");
88
        return true;
89
    }
90
}
91