CreateAnswerForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 37 1
A callbackSubmit() 0 23 1
1
<?php
2
3
namespace Alfs18\User\HTMLForm;
4
5
use Alfs18\User\Answers;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreateAnswerForm 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
     * @param $acronym the name of the one who posted the answer.
0 ignored issues
show
Bug introduced by
The type Alfs18\User\HTMLForm\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     * @param $id the id of the question.
20
     */
21
    public function __construct(ContainerInterface $di, $acronym, $id, $text)
22
    {
23
        parent::__construct($di);
24
        $this->form->create(
25
            [
26
                "id" => __CLASS__,
27
                "legend" => "Svar",
28
            ],
29
            [
30
                "acronym" => [
31
                    "type"  => "hidden",
32
                    "value" => "$acronym",
33
                ],
34
35
                "questionId" => [
36
                    "type"  => "hidden",
37
                    "value" => "$id",
38
                ],
39
40
                "answer" => [
41
                    "type"  => "$text",
42
                ],
43
44
                "points" => [
45
                    "type"  => "hidden",
46
                    "value" => 0,
47
                ],
48
49
                "created" => [
50
                    "type"  => "hidden",
51
                    "value" => date("Y-m-d, H:i"),
52
                ],
53
54
                "submit" => [
55
                    "type" => "submit",
56
                    "value" => "Ställ fråga",
57
                    "callback" => [$this, "callbackSubmit"]
58
                ],
59
            ]
60
        );
61
    }
62
63
64
65
    /**
66
     * Callback for submit-button which should return true if it could
67
     * carry out its work and false if something failed.
68
     *
69
     * @return boolean true if okey, false if something went wrong.
70
     */
71
    public function callbackSubmit()
72
    {
73
        // Get values from the submitted form
74
        $acronym = $this->form->value("acronym");
75
        $questionId = $this->form->value("questionId");
76
        $answer = $this->form->value("answer");
77
        $points = $this->form->value("points");
78
        $created = $this->form->value("created");
79
80
        // spara info om tid för skapande...
81
82
        // Save to database
83
        $res = new Answers();
84
        $res->setDb($this->di->get("dbqb"));
85
        $res->acronym = $acronym;
86
        $res->questionId = $questionId;
87
        $res->answer = $res->changeCharacter($answer);
88
        $res->points = intval($points);
89
        $res->created = $created;
90
        $res->saveAnswer($this->di);
91
92
        $this->form->addOutput("Answer was created.");
93
        return true;
94
    }
95
}
96