summaryrefslogtreecommitdiffstats
path: root/main/survey/app/Controllers/StatisticController.php
blob: 5a74887a2f41f82ed9967a4a9aa327c050ef4b24 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/***************************************
 * Description:
 * Autor: Robert Šmalc
 * Created date: 26.02.2016
 *****************************************/

namespace App\Controllers;

use App\Models\Model;
use SurveyInfo;

class StatisticController extends Controller
{

    /**
     * @desc poisce vprasanja, ki imajo statistiko in jih poklice
     */
    public static function displayStatistika($konec = false)
    {

        $sql = sisplet_query("SELECT vrstni_red FROM srv_grupa WHERE id = '" . get('grupa') . "'");
        $row = mysqli_fetch_array($sql);
        $vrstni_red = $row['vrstni_red'];

        if ($konec) $vrstni_red = 99999999;

        //preverimo ce gre za glasovanje ali navadno anketo
        $rowA = SurveyInfo::getInstance()->getSurveyRow();

        //statistika za glasovanje
        if ($rowA['survey_type'] == 0) {

            if (get('printPreview')) {
                $sql2 = sisplet_query("SELECT ank_id, spr_id, show_title FROM srv_glasovanje WHERE ank_id='" . get('anketa') . "'");
                $row2 = mysqli_fetch_array($sql2);

                $spremenljivka = $row2['spr_id'];

                $sqla = sisplet_query("SELECT starts, expire, statistics, db_table FROM srv_anketa WHERE id ='$row2[ank_id]'");
                $rowa = mysqli_fetch_array($sqla);

                $row = Model::select_from_srv_spremenljivka($spremenljivka);

                $text = $rowa['statistics'];

                if ($text != '')
                    echo '      <h3>' . $text . "\n";
                else
                    echo '      <h3>' . "\n";

                if ($row2['show_title'] == 0)
                    echo '      ' . $row['naslov'] . '</h3>' . "\n";
                else
                    echo '      </h3>' . "\n";
            } else {
                $sqlG = sisplet_query("SELECT spr_id FROM srv_glasovanje WHERE ank_id='" . get('anketa') . "'");
                $rowG = mysqli_fetch_array($sqlG);

                $sql = sisplet_query("SELECT id FROM srv_grupa WHERE ank_id='" . get('anketa') . "' AND vrstni_red<'$vrstni_red' ORDER BY vrstni_red DESC LIMIT 1");
                if (mysqli_num_rows($sql) > 0) {
                    $row = mysqli_fetch_array($sql);
                    $prev_grupa = $row['id'];

                    $sql = sisplet_query("SELECT id FROM srv_spremenljivka WHERE gru_id='$prev_grupa' AND stat>'0' ORDER BY vrstni_red ASC");
                    while ($row = mysqli_fetch_array($sql)) {
                        echo ' <div class="spremenljivka" id="spremenljivka_statistika">' . "\n";
                        self::displayStatGlasovanje($rowG['spr_id']);
                        echo ' </div>' . "\n";
                    }
                }
            }
        } //navadna statistika
        else {
            $sql = sisplet_query("SELECT id FROM srv_grupa WHERE ank_id='" . get('anketa') . "' AND vrstni_red<'$vrstni_red' ORDER BY vrstni_red DESC LIMIT 1");
            if (mysqli_num_rows($sql) > 0) {
                $row = mysqli_fetch_array($sql);
                $prev_grupa = $row['id'];

                $sql = sisplet_query("SELECT id FROM srv_spremenljivka WHERE gru_id='$prev_grupa' AND stat='1' ORDER BY vrstni_red ASC");
                while ($row = mysqli_fetch_array($sql)) {
                    echo ' <div class="spremenljivka" id="spremenljivka_statistika">' . "\n";
                    self::displayStat($row['id']);
                    echo ' </div>' . "\n";
                }
            }
        }

    }

    /**
     * @desc prikaze statistike za odgovor
     */
    public static function displayStat($spremenljivka, $spol = 0)
    {
        global $lang;

        $row = Model::select_from_srv_spremenljivka($spremenljivka);

        $text = $lang['results'];

        echo '      <h3>' . $text . ': ' . $row['naslov'] . '</h3>' . "\n";

        $sql = sisplet_query("SELECT COUNT(*) AS count FROM srv_vrednost v, srv_data_vrednost" . get('db_table') . " d WHERE v.spr_id='$spremenljivka' AND v.id=d.vre_id");
        $row = mysqli_fetch_array($sql);
        $total = $row['count'];

        $sql = sisplet_query("SELECT id, naslov FROM srv_vrednost WHERE spr_id='$spremenljivka' ORDER BY vrstni_red");
        while ($row = mysqli_fetch_array($sql)) {
            $sql1 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_vrednost" . get('db_table') . " WHERE vre_id='$row[id]'");
            $row1 = mysqli_fetch_array($sql1);


            if ($total > 0)
                $procent = round($row1['count'] / $total, 3) * 100;
            else
                $procent = 0;

            echo '        <p><strong>' . $row['naslov'] . ':</strong> ' . $procent . '%</p>' . "\n";
        }
    }

    /**
     * @desc prikaze statistiko za glasovanje
     */
    public function displayStatGlasovanje($spremenljivka, $spol = 0)
    {
        global $lang;
        global $site_url;
        global $global_user_id;

        $row = Model::select_from_srv_spremenljivka($spremenljivka);

        //preverimo ce gre za glasovanje
        $sql2 = sisplet_query("SELECT * FROM srv_glasovanje WHERE spr_id='$spremenljivka'");
        $row2 = mysqli_fetch_array($sql2);

        $sqla = sisplet_query("SELECT starts, expire, statistics, db_table FROM srv_anketa WHERE id ='$row2[ank_id]'");
        $rowa = mysqli_fetch_array($sqla);

        SurveyInfo::getInstance()->SurveyInit($row2['ank_id']);	
        $db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();
        save('db_table', $db_table);

        $text = $rowa['statistics'];

        if ($text != '')
            echo '      <span class="glas_settings_title">' . $text . "\n";
        else
            echo '      <span class="glas_settings_title">' . "\n";

        if ($row2['show_title'] == 0)
            echo '      ' . $row['naslov'] . '</span>' . "\n";
        else
            echo '      </span>' . "\n";

        $sql = sisplet_query("SELECT COUNT(*) AS count FROM srv_vrednost v, srv_data_vrednost" . get('db_table') . " d WHERE v.spr_id='$spremenljivka' AND v.id=d.vre_id");
        $row = mysqli_fetch_array($sql);
        $total = $row['count'];

        $sql = sisplet_query("SELECT id, naslov FROM srv_vrednost WHERE spr_id='$spremenljivka' ORDER BY vrstni_red");
        while ($row = mysqli_fetch_array($sql)) {

            //prikaz statistike za moske(pri glasovanju)
            if ($spol == 1) {
                $sql1 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_vrednost" . get('db_table') . " v, srv_data_glasovanje g WHERE v.vre_id='$row[id]' AND v.usr_id=g.usr_id AND g.spr_id='$spremenljivka' AND g.spol='1' ");
                $row1 = mysqli_fetch_array($sql1);
            } //prikaz statistike za zenske(pri glasovanju)
            elseif ($spol == 2) {
                $sql1 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_vrednost" . get('db_table') . " v, srv_data_glasovanje g WHERE v.vre_id='$row[id]' AND v.usr_id=g.usr_id AND g.spr_id='$spremenljivka' AND g.spol='2' ");
                $row1 = mysqli_fetch_array($sql1);
            } else {
                $sql1 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_vrednost" . get('db_table') . " WHERE vre_id='$row[id]'");
                $row1 = mysqli_fetch_array($sql1);
            }

            if ($total > 0)
                $procent = round($row1['count'] / $total, 3) * 100;
            else
                $procent = 0;

            echo '        <p style="margin-top: 10px;"><strong>' . $row['naslov'] . ':</strong>' . "\n";


            //ce gre za glasovanje in imamo nastavljen prikaz grafa ga izrisemo
            if ($row2['show_graph'] == 1) {

                echo '</p>';

                echo '        <div "style=" margin: -20px 0 0 25%; text-align: right; width: 25%; font-size: 12px;">' . "\n";
                if ($row2['show_results'] == 1) {
                    echo $row1['count'];
                    if ($row2['show_percent'] == 1)
                        echo ' (' . $procent . '%)';
                } elseif ($row2['show_percent'] == 1)
                    echo $procent . '%';
                echo '        </div>' . "\n";

                echo '<div id="graph"><div class="graph_fill" style="width: ' . $procent . '%;"></div></div>';
            } else {
                if ($row2['show_results'] == 1) {
                    echo $row1['count'];
                    if ($row2['show_percent'] == 1)
                        echo ' (' . $procent . '%)';
                } elseif ($row2['show_percent'] == 1)
                    echo $procent . '%';
                echo '</p>';
            }
        }

        //stevilo vseh glasov
        if ($row2['stat_count'] == 1) {
            if ($spol != 0) {
                $sql5 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_glasovanje WHERE spr_id='$spremenljivka' AND spol='1' ");
                $row5 = mysqli_fetch_array($sql5);
                $moskih_glasov = $row5['count'];

                if ($spol == 1) {
                    $percent = round($moskih_glasov / $total, 3) * 100;
                    $total = $moskih_glasov;
                } else {
                    $percent = round(($total - $moskih_glasov) / $total, 3) * 100;
                    $total = $total - $moskih_glasov;
                }
            }

            echo '<p style="margin-top: 10px;">' . $lang['glasovanja_count'] . ': ' . $total;
            if ($spol != 0)
                echo ' (' . $percent . '%)';
            echo '</p>';
        }

        //cas aktivnosti ankete
        if ($row2['stat_time'] == 1) {
            $start = strtotime($rowa['starts']);
            $end = strtotime($rowa['expire']);
            echo '<p>' . $lang['glasovanja_time'] . ' ' . date("m.d.Y", $start) . ' ' . $lang['glasovanja_time_end'] . ' ' . date("m.d.Y", $end) . '</p>';
        }

        //prikaz statistike po spolu (samo kjer je nastavljena opcija izbira spola) - gumbi za preklop
        if ($row2['spol'] == 1) {
            echo '<div class="spol_buttons">' . "\n";

            if ($spol == 0) echo '<input type="button" class="active" value="' . $lang['hour_all'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'0\');">';
            else            echo '<input type="button" value="' . $lang['hour_all'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'0\');">';

            if ($spol == 1) echo '<input type="button" class="active" value="' . $lang['glasovanja_spol_moski'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'1\');">';
            else            echo '<input type="button" value="' . $lang['glasovanja_spol_moski'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'1\');">';

            if ($spol == 2) echo '<input type="button" class="active" value="' . $lang['glasovanja_spol_zenske'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'2\');">';
            else            echo '<input type="button" value="' . $lang['glasovanja_spol_zenske'] . '" onClick="stat_spol(\'' . $spremenljivka . '\', \'2\');">';

            echo '</div>' . "\n";
        }

        //prikaz arhiva statistik anket
        if ($row2['stat_archive'] == 1) {

            echo '<div id="arhiv">';
            echo 'Ankete iz arhiva:';

            $sqlArchive = sisplet_query("SELECT ank_id FROM srv_glasovanje WHERE stat_archive = '1' AND ank_id != '$row2[ank_id]'");

            echo '<ul>';
            while ($rowArchive = mysqli_fetch_array($sqlArchive)) {
                $sql4 = sisplet_query("SELECT naslov, starts, insert_uid, hash FROM srv_anketa WHERE id = '$rowArchive[ank_id]'");
                $row4 = mysqli_fetch_array($sql4);

                if($row4['insert_uid'] == $global_user_id){
                    echo '<li>';

                    echo '<a href="' . $site_url . 'a/' . $row4['hash'] . '&glas_end=0' . get('cookie_url') . '">' . $row4['naslov'] . '</a>';
                    echo ' (' . $row4['starts'] . ')';

                    echo '</li>';
                }
            }
            echo '</ul>';
            echo '</div>';
        }
    }

}