-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoll.php
171 lines (150 loc) · 4.51 KB
/
Poll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace lslsoft\poll;
use lslsoft\poll\models\Polls;
use lslsoft\poll\models\PollsResult;
use lslsoft\poll\models\PollsResultSearch;
use Yii;
use yii\base\Widget;
class Poll extends Widget {
/**
* Model for poll results
* @var type
*/
private $model;
/**
* Define id_poll which poll will be questioned
* @var integer
*/
public $idPoll;
/**
* Define how to show results: chart or table
* @var type string
*/
public $resultView;
/**
* Contain poll for which voting will be organized
* @var type Polls
*/
private $pollsProvider;
public function init() {
parent::init();
}
/**
* Creates a new PollsResult model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
private function createResult() {
$modelsaved = false;
$answers = $this->model->id_answer;
foreach ( $answers as $id_answer ) {
$modelMulti = new PollsResult();
$modelMulti->id_user = $this->model->id_user;
$modelMulti->id_poll = $this->model->id_poll;
$modelMulti->num = $this->model->num;
$modelMulti->create_at = $this->model->create_at;
$modelMulti->ip = $this->model->ip;
$modelMulti->host = $this->model->host;
$modelMulti->id_answer = $id_answer;
if ( !$modelMulti->save() ) {
$modelsaved = false;
print_r( $modelMulti->errors );
return false;
} else {
$modelsaved = true;
}
}
return $modelsaved;
}
/**
* set $model properties
* @param type $model
* @return type $model - PollsResult
*/
private function setModel() {
$this->model->id_poll = $this->pollsProvider->id;
$this->model->num = PollsResult::getMaxNum( $this->pollsProvider->id );
if ( !isset( $this->model->num ) ) {
$this->model->num = 1;
} else {
$this->model->num++;
}
$this->model->create_at = date( "Y-m-d H:i:s" );
$this->model->ip = Yii::$app->request->getUserIP();
$this->model->host = Yii::$app->request->getUserHost();
if ( $this->pollsProvider->anonymous ) {
$this->model->scenario = PollsResult::SCENARIO_ANONYMOUS;
$this->model->id_user = 0;
} else {
if ( !Yii::$app->user->isGuest ) {
$this->model->id_user = Yii::$app->user->getId();
}
}
}
/**
* Return poll for voting
* @return type Polls
*/
public function getProvider() {
if ( isset( $this->idPoll ) ) {
return Polls::getIdPoll( $this->idPoll );
} else {
$pollVote = Polls::getPollToday();
if ( isset( $pollVote ) ) {
$this->idPoll = $pollVote->id;
}
return $pollVote;
}
}
/**
*
* @return type
*/
public function run() {
// Register AssetBundle
// Register AssetBundle
// PollAsset::register($this->getView());
$this->model = new PollsResult();
$this->pollsProvider = $this->getProvider();//barbaric of course
if (!isset($this->pollsProvider)) {
return;
}
$modelsaved = false;
if ($this->model->load(Yii::$app->request->post())) {
$this->setModel();
/**
* @todo I need do something with guest user. now i have user with ID=0;
*/
if (is_array($this->model->id_answer)) {
$modelsaved = $this->createResult();
} else {
$modelsaved = $this->model->save();
}
}
if (!$modelsaved) {
return $this->render('@vendor/lslsoft/yii2-poll/views/create', [
'model' => $this->model,
'pollsProvider' => $this->pollsProvider,]);
}
if (!$this->pollsProvider->show_vote) {
return; //if show result wasn't allowed nothing would happen
}
$searchModel = new PollsResultSearch();
$dataProvider = $searchModel->search(['id_poll' => $this->idPoll]);
if (!isset($this->resultView)) {
return $this->render('@vendor/lslsoft/yii2-poll/views/chart', ['dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'question' => $this->pollsProvider->question,
'sumRes' => $this->model->num]);
}
return $this->render('@vendor/lslsoft/yii2-poll/views/table', ['dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'question' => $this->pollsProvider->question,
'sumRes' => $this->model->num]);
}
}