@@ -38,24 +38,34 @@ class Polls
3838 * @param $id Poll-ID to display
3939 * @global object $db Globales Class-Object mit allen MySQL-Methoden
4040 * @global object $user Globales Class-Object mit den User-Methoden & Variablen
41- * @return string HTML-markup to display the Poll
41+ * @return string smarty->fetch() Results
4242 */
4343 function show ($ id )
4444 {
4545 global $ db , $ user , $ smarty ;
4646
47+ /** Validate Parameters */
48+ if (!is_numeric ($ id ) || $ id <= 0 ) {
49+ $ smarty ->assign ('error ' , ['type ' => 'warn ' , 'title ' => t ('invalid-poll_id ' , 'poll ' , [$ id ]), 'dismissable ' => false ]);
50+ return $ smarty ->fetch ('file:layout/elements/block_error.tpl ' );
51+ }
52+ $ id = intval ($ id );
53+ zorgDebugger::log ()->debug ('poll %d ' , [$ id ]);
54+
55+ $ sql = '' ;
4756 $ params = [];
48- $ sql = 'SELECT p.* ,UNIX_TIMESTAMP(p.date) date ,(SELECT count(*) FROM poll_votes WHERE poll=?) total_votes
49- ' .($ user ->is_loggedin () ? ',(SELECT answer FROM poll_votes WHERE poll=? AND user=?) myvote ' : '' ).'
50- FROM polls p WHERE id=? GROUP BY p.id ' ;
51- $ params [] = $ id ;
5257 if ($ user ->is_loggedin ()) {
58+ $ sql = 'SELECT p.*, UNIX_TIMESTAMP(p.date) date, (SELECT count(*) FROM poll_votes WHERE poll=?) total_votes, (SELECT answer FROM poll_votes WHERE poll=? AND user=?) myvote FROM polls p WHERE id=? GROUP BY p.id ' ;
59+ $ params [] = $ id ;
5360 $ params [] = $ id ;
5461 $ params [] = $ user ->id ;
62+ $ params [] = $ id ;
63+ } else {
64+ $ sql = 'SELECT p.*, UNIX_TIMESTAMP(p.date) date, (SELECT count(*) FROM poll_votes WHERE poll=?) total_votes FROM polls p WHERE id=? GROUP BY p.id ' ;
65+ $ params [] = $ id ;
66+ $ params [] = $ id ;
5567 }
56- $ params = $ id ;
5768 $ poll = $ db ->fetch ($ db ->query ($ sql , __FILE__ , __LINE__ , __FUNCTION__ , $ params ));
58- //if (DEVELOPMENT) error_log(sprintf('[DEBUG] <%s:%d> $poll: %s', __FUNCTION__, __LINE__, print_r($poll,true)));
5969
6070 if (!empty ($ poll ) && $ poll !== false )
6171 {
@@ -66,29 +76,28 @@ function show($id)
6676 $ smarty ->assign ('user_has_vote_permission ' , $ user_has_vote_permission );
6777 //if (DEVELOPMENT) error_log(sprintf('[DEBUG] <%s:%d> $user_has_vote_permission: %s', __FUNCTION__, __LINE__, ($user_has_vote_permission?'true':'false')));
6878
69- /** Query Poll answers and return each answer with votes count */
70- $ pollMaxvotes = ($ poll ['total_votes ' ] > 0 ? $ poll ['total_votes ' ] : 0 );
71- $ pollbarMaxwidth = 200 ;
72- $ pollbarSize = 0 ;
73-
74- //$e = $db->query('SELECT count(*) anz FROM poll_votes WHERE poll='.$id.' GROUP BY answer', __FILE__, __LINE__, __FUNCTION__);
75- $ sql = 'SELECT a.*, count(v.user) votes FROM poll_answers a
76- LEFT JOIN poll_votes v ON v.answer=a.id
77- WHERE a.poll=? GROUP BY a.id ORDER BY a.id ' ;
79+ $ sql = 'SELECT a.*, count(v.user) votes FROM poll_answers a LEFT JOIN poll_votes v ON v.answer=a.id WHERE a.poll=? GROUP BY a.id ORDER BY a.id ' ;
7880 $ pollAnswers = $ db ->query ($ sql , __FILE__ , __LINE__ , __FUNCTION__ , [$ id ]);
7981 while ($ pollAnswer = $ db ->fetch ($ pollAnswers ))
8082 {
8183 $ pollAnswersArray [$ pollAnswer ['id ' ]] = $ pollAnswer ;
8284
85+ /** Query Poll answers and return each answer with votes count */
86+ $ pollMaxvotes = ($ poll ['total_votes ' ] > 0 ? $ poll ['total_votes ' ] : 0 );
87+ $ pollbarMaxwidth = 200 ;
88+ $ pollbarSize = 0 ;
89+
8390 /** Poll votes result-bar calculations */
84- if ($ pollAnswer ['votes ' ] == 0 ) $ pollbarSize = 1 ;
85- else $ pollbarSize = round ($ pollAnswer ['votes ' ] / $ pollMaxvotes * $ pollbarMaxwidth );
91+ if (empty ($ pollAnswer ['votes ' ])) {
92+ $ pollbarSize = 1 ;
93+ } else {
94+ $ pollbarSize = round ($ pollAnswer ['votes ' ] / $ pollMaxvotes * $ pollbarMaxwidth );
95+ }
8696 $ pollAnswersArray [$ pollAnswer ['id ' ]]['pollbar_size ' ] = $ pollbarSize ;
8797 $ pollAnswersArray [$ pollAnswer ['id ' ]]['pollbar_space ' ] = $ pollbarMaxwidth - $ pollbarSize ;
8898
8999 if ($ poll ['myvote ' ] == $ pollAnswer ['id ' ]) {
90100 if ($ poll ['myvote ' ] && $ poll ['state ' ]=='open ' && $ user_has_vote_permission ) {
91- //$old_url = base64url_encode("$_SERVER[PHP_SELF]?".url_params());
92101 $ pollAnswersArray [$ pollAnswer ['id ' ]]['unvote_url ' ] = '/actions/poll_unvote.php?poll= ' .$ poll ['id ' ].'&redirect= ' .getURL ();
93102 }
94103 }
@@ -117,12 +126,12 @@ function show($id)
117126 $ smarty ->assign ('voters ' , $ pollVotersArray );
118127 }
119128
120- $ smarty ->display ('file:layout/partials/polls/poll.tpl ' );
129+ return $ smarty ->fetch ('file:layout/partials/polls/poll.tpl ' );
121130
122131 /** Poll not found - $id invalid */
123132 } else {
124133 $ smarty ->assign ('error ' , ['type ' => 'warn ' , 'title ' => t ('invalid-poll_id ' , 'poll ' , [$ id ]), 'dismissable ' => false ]);
125- $ smarty ->display ('file:layout/elements/block_error.tpl ' );
134+ return $ smarty ->fetch ('file:layout/elements/block_error.tpl ' );
126135 }
127136 }
128137
@@ -143,7 +152,7 @@ function user_has_vote_permission($poll_type)
143152 }
144153
145154 /**
146- * Updates the title and options of a poll.
155+ * // TODO Updates the title and options of a poll.
147156 * @link https://zorg.ch/bug/765 [Bug #765] Edit-Link bei bestehenden My Polls fehlt
148157 *
149158 * @version 1.0
@@ -173,4 +182,29 @@ public function update($poll_id, $title, $type, $answers) {
173182
174183 return true ;
175184 }
185+
186+ /**
187+ * Return all Poll IDs
188+ *
189+ * @version 1.0
190+ * @since 1.0 `11.01.2024` `IneX` Method added
191+ *
192+ * @global object $db Globales Class-Object mit allen MySQL-Methoden
193+ * @return array Array with all IDs of all Polls
194+ */
195+ public function getAll ()
196+ {
197+ global $ db ;
198+
199+ $ polls = [];
200+ $ e = $ db ->query ('SELECT id FROM polls ORDER BY date DESC ' , __FILE__ , __LINE__ , 'SELECT id FROM polls ' );
201+ while ($ d = $ db ->fetch ($ e )) {
202+ $ polls [] = $ d ['id ' ];
203+ }
204+
205+ return $ polls ;
206+ }
176207}
208+
209+ /** Instantiate Polls */
210+ $ polls = new Polls ();
0 commit comments