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 |
|
|
|
|
18
|
|
|
* @param $acronym the name of the one who posted the answer. |
|
|
|
|
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
|
|
|
|