16
16
17
17
class Poll extends Widget {
18
18
19
- /**
20
- * Model for poll results
21
- * @var type
22
- */
23
- private $ model ;
24
-
25
- /**
26
- * Define id_poll which poll will be questioned
27
- * @var integer
28
- */
29
- public $ idPoll ;
30
-
31
- /**
32
- * Define how to show results: chart or table
33
- * @var type string
34
- */
35
- public $ resultView ;
36
-
37
- /**
38
- * Contain poll for which voting will be organized
39
- * @var type Polls
40
- */
41
- private $ pollsProvider ;
42
-
43
- public function init () {
44
- parent ::init ();
45
- }
46
-
47
- /**
48
- * Creates a new PollsResult model.
49
- * If creation is successful, the browser will be redirected to the 'view' page.
50
- * @return mixed
51
- */
52
- private function createResult () {
53
- $ modelsaved = false ;
54
- $ answers = $ this ->model ->id_answer ;
55
- foreach ($ answers as $ id_answer ) {
56
- $ modelMulti = new PollsResult ();
57
- $ modelMulti ->id_user = $ this ->model ->id_user ;
58
- $ modelMulti ->id_poll = $ this ->model ->id_poll ;
59
- $ modelMulti ->num = $ this ->model ->num ;
60
- $ modelMulti ->create_at = $ this ->model ->create_at ;
61
- $ modelMulti ->ip = $ this ->model ->ip ;
62
- $ modelMulti ->host = $ this ->model ->host ;
63
- $ modelMulti ->id_answer = $ id_answer ;
64
-
65
- if (!$ modelMulti ->save ()) {
66
- $ modelsaved = false ;
67
- print_r ($ modelMulti ->errors );
68
- return false ;
69
- } else {
70
-
71
- $ modelsaved = true ;
72
- }
73
- }
74
- return $ modelsaved ;
75
- }
76
-
77
- /**
78
- * set $model properties
79
- * @param type $model
80
- * @return type $model - PollsResult
81
- */
82
- private function setModel () {
83
- $ this ->model ->id_poll = $ this ->pollsProvider ->id ;
84
- $ this ->model ->num = PollsResult::getMaxNum ($ this ->pollsProvider ->id );
85
- if (!isset ($ this ->model ->num )) {
86
- $ this ->model ->num = 1 ;
87
- } else {
88
- $ this ->model ->num ++;
89
- }
90
- $ this ->model ->create_at = date ("Y-m-d H:i:s " );
91
- $ this ->model ->ip = Yii::$ app ->request ->getUserIP ();
92
- $ this ->model ->host = Yii::$ app ->request ->getUserHost ();
93
- if ($ this ->pollsProvider ->anonymous ) {
94
- $ this ->model ->scenario = PollsResult::SCENARIO_ANONYMOUS ;
95
- $ this ->model ->id_user = 0 ;
96
- } else {
97
- if (!Yii::$ app ->user ->isGuest ) {
98
- $ this ->model ->id_user = Yii::$ app ->user ->getId ();
99
- }
100
- }
101
- }
102
-
103
- /**
104
- * Return poll for voting
105
- * @return type Polls
106
- */
107
- public function getProvider () {
108
- if (isset ($ this ->idPoll )) {
109
- return Polls::getIdPoll ($ this ->idPoll );
110
- } else {
111
- $ pollVote = Polls::getPollToday ();
112
- if (isset ($ pollVote )) {
113
- $ this ->idPoll = $ pollVote ->id ;
114
- }
115
- return $ pollVote ;
116
- }
117
- }
118
-
119
- /**
120
- *
121
- * @return type
122
- */
123
- public function run () {
124
- // Register AssetBundle
125
- PollAsset::register ($ this ->getView ());
19
+ /**
20
+ * Model for poll results
21
+ * @var type
22
+ */
23
+ private $ model ;
24
+
25
+ /**
26
+ * Define id_poll which poll will be questioned
27
+ * @var integer
28
+ */
29
+ public $ idPoll ;
30
+
31
+ /**
32
+ * Define how to show results: chart or table
33
+ * @var type string
34
+ */
35
+ public $ resultView ;
36
+
37
+ /**
38
+ * Contain poll for which voting will be organized
39
+ * @var type Polls
40
+ */
41
+ private $ pollsProvider ;
42
+
43
+ public function init () {
44
+ parent ::init ();
45
+ }
46
+
47
+ /**
48
+ * Creates a new PollsResult model.
49
+ * If creation is successful, the browser will be redirected to the 'view' page.
50
+ * @return mixed
51
+ */
52
+ private function createResult () {
53
+ $ modelsaved = false ;
54
+ $ answers = $ this ->model ->id_answer ;
55
+ foreach ( $ answers as $ id_answer ) {
56
+ $ modelMulti = new PollsResult ();
57
+ $ modelMulti ->id_user = $ this ->model ->id_user ;
58
+ $ modelMulti ->id_poll = $ this ->model ->id_poll ;
59
+ $ modelMulti ->num = $ this ->model ->num ;
60
+ $ modelMulti ->create_at = $ this ->model ->create_at ;
61
+ $ modelMulti ->ip = $ this ->model ->ip ;
62
+ $ modelMulti ->host = $ this ->model ->host ;
63
+ $ modelMulti ->id_answer = $ id_answer ;
64
+
65
+ if ( !$ modelMulti ->save () ) {
66
+ $ modelsaved = false ;
67
+ print_r ( $ modelMulti ->errors );
68
+ return false ;
69
+ } else {
70
+
71
+ $ modelsaved = true ;
72
+ }
73
+ }
74
+ return $ modelsaved ;
75
+ }
76
+
77
+ /**
78
+ * set $model properties
79
+ * @param type $model
80
+ * @return type $model - PollsResult
81
+ */
82
+ private function setModel () {
83
+ $ this ->model ->id_poll = $ this ->pollsProvider ->id ;
84
+ $ this ->model ->num = PollsResult::getMaxNum ( $ this ->pollsProvider ->id );
85
+ if ( !isset ( $ this ->model ->num ) ) {
86
+ $ this ->model ->num = 1 ;
87
+ } else {
88
+ $ this ->model ->num ++;
89
+ }
90
+ $ this ->model ->create_at = date ( "Y-m-d H:i:s " );
91
+ $ this ->model ->ip = Yii::$ app ->request ->getUserIP ();
92
+ $ this ->model ->host = Yii::$ app ->request ->getUserHost ();
93
+ if ( $ this ->pollsProvider ->anonymous ) {
94
+ $ this ->model ->scenario = PollsResult::SCENARIO_ANONYMOUS ;
95
+ $ this ->model ->id_user = 0 ;
96
+ } else {
97
+ if ( !Yii::$ app ->user ->isGuest ) {
98
+ $ this ->model ->id_user = Yii::$ app ->user ->getId ();
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Return poll for voting
105
+ * @return type Polls
106
+ */
107
+ public function getProvider () {
108
+ if ( isset ( $ this ->idPoll ) ) {
109
+ return Polls::getIdPoll ( $ this ->idPoll );
110
+ } else {
111
+ $ pollVote = Polls::getPollToday ();
112
+ if ( isset ( $ pollVote ) ) {
113
+ $ this ->idPoll = $ pollVote ->id ;
114
+ }
115
+ return $ pollVote ;
116
+ }
117
+ }
118
+
119
+ /**
120
+ *
121
+ * @return type
122
+ */
123
+ public function run () {
124
+ // Register AssetBundle
125
+ // Register AssetBundle
126
+ // PollAsset::register($this->getView());
126
127
127
128
$ this ->model = new PollsResult ();
128
129
$ this ->pollsProvider = $ this ->getProvider ();//barbaric of course
@@ -142,9 +143,10 @@ public function run() {
142
143
}
143
144
}
144
145
if (!$ modelsaved ) {
145
- return \Yii:: $ app -> controller -> renderPartial ('@vendor/lslsoft/yii2-poll/views/create ' , [
146
+ return $ this -> render ('@vendor/lslsoft/yii2-poll/views/create ' , [
146
147
'model ' => $ this ->model ,
147
148
'pollsProvider ' => $ this ->pollsProvider ,]);
149
+
148
150
}
149
151
150
152
if (!$ this ->pollsProvider ->show_vote ) {
@@ -153,15 +155,17 @@ public function run() {
153
155
$ searchModel = new PollsResultSearch ();
154
156
$ dataProvider = $ searchModel ->search (['id_poll ' => $ this ->idPoll ]);
155
157
if (!isset ($ this ->resultView )) {
156
- return \Yii:: $ app -> controller -> renderPartial ('@vendor/lslsoft/yii2-poll/views/chart ' , ['dataProvider ' => $ dataProvider ,
158
+ return $ this -> render ('@vendor/lslsoft/yii2-poll/views/chart ' , ['dataProvider ' => $ dataProvider ,
157
159
'searchModel ' => $ searchModel ,
158
160
'question ' => $ this ->pollsProvider ->question ,
159
161
'sumRes ' => $ this ->model ->num ]);
162
+
160
163
}
161
- return \Yii:: $ app -> controller -> renderPartial ('@vendor/lslsoft/yii2-poll/views/table ' , ['dataProvider ' => $ dataProvider ,
164
+ return $ this -> render ('@vendor/lslsoft/yii2-poll/views/table ' , ['dataProvider ' => $ dataProvider ,
162
165
'searchModel ' => $ searchModel ,
163
166
'question ' => $ this ->pollsProvider ->question ,
164
167
'sumRes ' => $ this ->model ->num ]);
165
- }
168
+
169
+ }
166
170
167
171
}
0 commit comments