Umfragen
page.php
Go to the documentation of this file.
1 <?php
2 /*
3  * page.php
4  *
5  * Copyright 2012 Johannes <jojo@jojo-42>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  *
22  *
23  */
24 
25 function display_poll_form($show_poll,$preview=false){
26  if($show_poll->anonymous == true){
27  print_info("Diese Umfrage wird anonym ausgewertet! <br/> Es werden also keine Benutzerdaten gespeichert, die auf die Herkunft (username, voller Name) hinweisen könnten.");
28  }
29  echo '<form method="POST" action="">';
30  $show_poll->display();
31  echo "<p style='margin-top:2em;'>Die Felder, die mit <span class='required_asterisk'>*</span> markiert sind, sind Plichtfelder.</p>";
32  if($preview==false){
33  echo '<br/><input type="submit" value="Senden" name="poll_submit"/>';
34  }
35  echo "</form>";
36 }
37 
38 
39 function display_poll_link($poll,$polls_new,$polls_visited,$imgname){
40  $timeout = $poll->get_timeout_string();
41  $new = "<td style='width:1em;'></td>";
42  $anon = "";
43  if( (in_array($poll->ID,$polls_new)) AND (!in_array($poll->ID,$polls_visited)) ){
44  $new = "<td style='max-width:1em'> <img src='".APP_ROOT."/icons/new.png' style='position:relative;right:2px;width:1.5em;' alt=''/> </td>";
45  }
46  if( $poll->anonymous ){
47  $anon = "<span style='color:#C80D32;font-weight:bold;font-size:70%;line-height:70%;margin-left:.5em;vertical-align:middle'>(anonym)</span>";
48  }
49 
50  echo "<li style='margin:0px;padding:0px;margin-right:1.5em;margin-bottom:.5em;border-top:1px solid #C6C6C6;padding-top:5px'>";
51  echo "<table>";
52  echo "<tr>";
53  echo $new;
54  echo "<td> <a href=\"page.php?pID={$poll->ID}\" title='ID{$poll->ID}'> <img src='".APP_ROOT."/icons/pollicon/$imgname' style='height:4em;' alt=''/> </a> </td>";
55  echo "<td style='vertical-align:top'>";
56  echo "<a href=\"page.php?pID={$poll->ID}\" title='ID{$poll->ID}' style=';color:#759A24;text-decoration:none;font:1.5em/0.9 OFL Sorts Mill Goudy,Georgia,serif;letter-spacing:-1px' > {$poll->name}</a>";
57  echo $anon;
58  echo "<br/> <span style='font-size:80%;position:relative;top:-2px;color:#505050'>von {$poll->owner_fn}</span>";
59  echo "<br/> <span style='font-size:80%;position:relative;top:-2px;color:#959595'>bis zum $timeout</span>";
60  echo "</td>";
61  echo "</tr>";
62  echo "</table>";
63  echo "</li>";
64 }
65 
66 function get_color_array($pollist,$colors){
67  $max =sizeof($pollist);
68  $colar = array();
69  if( $max >= $colors ){
70  for( $a=1 ; $a <= $colors ; ++$a ){
71  $colar[$a] = (int) ($max / $colors);
72  if( $a <= ($max%$colors) ){
73  ++$colar[$a];
74  }
75  }
76  }else{
77  for($i=1;$i<=$colors;++$i){
78  $colar[$i] = 0;
79  }
80  for( $i=1 ; $i <= $max ; ++$i ){
81  $index = (int) $colors/$max;
82  $index = $index * $i;
83  if($index > $colors){
84  $index = $colors;
85  }
86  $colar[ $index ] = 1;
87  }
88  }
89  return $colar;
90 }
91 // SESSION
92 require_once("config.php");
93 require_once("inc/user.class.php");
94 
95 require_once("inc/tools.php");
96 make_session();
97 session_regenerate_id();
98 
99 require_once("inc/check_login.php");
100 check_login(); // needs user object in session
101 
102 require_once("inc/db.class.php");
103 require_once("inc/config.class.php");
104 
105 require_once("inc/poll.class.php");
106 require_once("inc/html.class.php");
107 require_once("inc/messages.class.php");
108 require_once("inc/chart.class.php");
109 
111 $db= new db( DB_USER, DB_PASS, DB_DATABASE, DB_HOST );
113 $config->load();
114 
115 $now = time();
116 
117 // LOAD POLLS
118 $ps = $db->get_all_polls_array();
119 $polls = array(); // pollID => pollObj
120 $polls_ID = array(); // 0basedIndex of pollIDs
121 foreach($ps as $p){
122  $pp = "";
123  // FILTER ALLOWED
124  if ($p["groups"] == "*"){
125  // wildcard allowes everyone
126  $pp = $p["ID"];
127  }else{
128  // get allowed groups for this poll
129  $poll_groups = explode(",",$p["groups"]);
130  // go through each group ID and check if one of the given groups is matching
131  $projects = array();
132  if( isset($_SESSION["projects"]) ){
133  foreach( $_SESSION["projects"] as $pr ){
134  $projects[] = $pr[1]; // get groupID
135  }
136  }
137  foreach($poll_groups as $poll_group){
138  //if( ( $poll_group == $_SESSION["group"] ) OR ( in_array($poll_group,$projects) ) ){
139  if( ( $poll_group == $_SESSION["user"]->group ) OR ( in_array($poll_group,$projects) ) ){
140  $pp = $p["ID"];
141  }
142  }
143  }
144  // owners have the expicit right to use their polls
145  //if( intval($_SESSION["userID"]) === intval($p["owner"]) ){
146  if( intval($_SESSION["user"]->ID) === intval($p["owner"]) ){
147  $pp = $p["ID"];
148  }
149 
150  // LOAD POLL
151  if( $pp != "" ){
152  $p = new poll();
153  $p->load_from_id($db,$pp);
154  $polls[ $p->ID ] = $p;
155  $polls_ID[] = intval($pp);
156  }
157 }
158 
159 
160 $page = "index"; // default
161 if( (isset($_GET["pID"])) AND (is_numeric($_GET["pID"])) ){
162  $pID = intval($_GET["pID"]);
163  if( in_array($pID,$polls_ID) ){
164  $ppoll = &$polls[$pID];
165  //$ppoll->load_values_from_id($db,$_SESSION["user"]) ;
166  $ppoll->load_values_from_id($db,$_SESSION["user"]->name) ;
167  $page = "show poll";
168  }
169 }else if( (isset($_GET["preview"])) AND (is_numeric($_GET["preview"])) ){
170  $pID = intval($_GET["preview"]);
171  $p = new poll();
172  $p->load_from_id($db,$pID);
173  if(($p->owner == $_SESSION["user"]->ID) OR (in_array($_SESSION["user"]->ID,explode(",",SUPER_ADMIN)))){
174  $page= "preview";
175  $ppoll = $p;
176  }
177 }
178 if( isset($_POST["poll_submit"]) ){
179  $ppoll->load_from_id($db,$pID); // load the poll
180  $ppoll->load_values_from_id($db,$_SESSION["user"]->name); // load data for user important !!
181  $page = "handle poll";
182 }
183 
184 if( ($page == "show poll") OR ($page == "preview") OR ($page == "handle poll") ){
185  $ppoll->load_widget_result_count($db);
186 }
187 
188 // BEGIN HTML
189 HTML::doctype();
190 // include polls theme
191 if( ($page == "show poll") OR ($page == "handle poll") OR ($page == "preview") ){
192  if( $ppoll->theme !== "" ){
193  HTML::head('<link href="'.APP_ROOT.'/CSS/Themes/'.$ppoll->theme.'" rel="stylesheet" type="text/css">' );
194  }else{
195  HTML::head();
196  }
197 }else{
198  HTML::head();
199 }
200 
201 // vars:
202 // $polls -> array of all polls that are allowed
203 // $page -> "handle poll" "index" "show poll" "preview"... action to do
204 // $ppoll -> pointer to the poll we use in case of "show poll" "handle_poll" and "preview"
205  HTML::menu();
206 
207 if ($page == "index"){
208 
209 
210 
211  if(sizeof($polls) > 0){
212 
213 // SORT POLLS
214  $my_polls = array();
215  $other_polls = array();
216  $polls_timeouts = array();
217  $polls_sorted = array();
218  $polls_copy = $polls;
219  $polls_new = array();
220  // get the visited polls
221  $polls_visited = $db->get_visited_polls_for_user($_SESSION["user"]->name);
222  // get timeout timestamps into array
223  foreach( $polls as $poll ){
224  $polls_timeouts[ $poll->ID ] = $poll->get_timeout_timestamp();
225  }
226  // sort ids by timestamp
227  asort($polls_timeouts);
228  // put new polls on top
229  $keys = array_keys($polls_timeouts);
230  foreach( $keys as $id){
231  if( ($polls[$id]->get_active_since_timestammp() >= ($now-1728000)) ){
232  if( isset($polls_copy[$id]) ){
233  $polls_sorted[] = $polls_copy[ $id ];
234  unset($polls_copy[$id]);
235  $polls_new[] = $polls[$id]->ID;
236  }
237  }
238  }
239  // append the rest acording to sort
240  foreach( $keys as $id){
241  if( isset($polls_copy[$id]) ){
242  $polls_sorted[] = $polls_copy[ $id ];
243  }
244  }
245  // filter for user timeout status and owner
246  foreach($polls_sorted as $poll){
247  if( ($poll->owner == $_SESSION["user"]->ID) ){
248  if( ($poll->status == STATUS_NORMAL) AND
249  ($poll->get_timeout_timestamp() >= $now) ){
250  $my_polls[] = $poll;
251  }
252  }else{
253  if( ($poll->status == STATUS_NORMAL) AND
254  ($poll->get_timeout_timestamp() >= $now) ){
255  $other_polls[] = $poll;
256  }
257  }
258  }
259 
260  unset($polls_copy);
261 
262  echo "<table style='width:100%'><tr>";
263 
264  if( sizeof($other_polls)>0 ){
265  $colors = get_color_array($other_polls,11);
266  echo "<td style='vertical-align:top;width:50%'>";
267  echo "<h2>Umfragen</h2>";
268  echo "<ul style='list-style:none;padding:0px'>";
269  $i=0;
270  $color = 1;
271  foreach($other_polls as $poll){
272  if( $poll->noDisplay === false ){
273  --$colors[$color];
274  while( (isset($colors[$color]) ) AND ($colors[$color] <= 0) ){
275  ++$color;
276  }
277  if( $color >= 11){
278  $color = 11;
279  }
280  $imgname = "pollicon-color{$color}.png";
281  display_poll_link($poll,$polls_new,$polls_visited,$imgname);
282  ++$i;
283  }
284  }
285  echo "</ul>";
286  echo "</td>";
287  }
288 
289 
290 // MY POLLS
291 
292  if( sizeof($my_polls) >0 ){
293 
294  echo "<td style='vertical-align:top;width:50%'>";
295  echo "<h2>Eigene Umfragen</h2>";
296  echo "<ul style='list-style:none;padding:0px'>";
297  $colors = get_color_array($my_polls,11);
298  $i=0;
299  $color=1;
300  foreach($my_polls as $poll){
301  if( $poll->noDisplay === false ){
302  --$colors[$color];
303  while( (isset($colors[$color]) ) AND ($colors[$color] <= 0) ){
304  ++$color;
305  }
306  if( $color >= 11){
307  $color = 11;
308  }
309  $imgname = "pollicon-color{$color}.png";
310 
311  display_poll_link($poll,$polls_new,$polls_visited,$imgname);
312  ++$i;
313  }
314  }
315  echo "</ul>";
316  }else{
317  $umfragen_anderer = "Umfragen";
318  }
319  echo "</td>";
320 
321  echo "</tr></table>";
322 
323 
324  }
325 
326  echo "<p style='margin-top:2em'><a href='backend/#umfrage_hinzuf%C3%BCgen' style='font-size:1.2em' >&rarr; Eine Umfrage erstellen</a></p>";
327 
328 
329 
330 // SHOW POLL FORM
331 }else if ($page == "show poll"){
332  if( ($ppoll->status == STATUS_NORMAL) AND ($ppoll->get_timeout_timestamp() >= $now) ){
333  // fancy title
334  if ($ppoll->owner == $_SESSION["user"]->ID ){
335  echo "<p id='poll_title'> <span id='umfrage'>Umfrage</span> &laquo;<i>{$ppoll->name}</i>&raquo; von {$ppoll->owner_fn} <span style='float:right'><a href='backend/edit.php?pollID={$ppoll->ID}' style='font-size:60%;color:#5F5F5F' >bearbeiten</a></span></p>";
336  }else{
337  echo "<p id='poll_title'><span id='umfrage'>Umfrage</span> &laquo;<i>{$ppoll->name}</i>&raquo; von {$ppoll->owner_fn} </p>";
338  }
339 
340  display_poll_form($ppoll);
341  }
342 // HANDLE POLL INPUT
343 }else if($page == "handle poll"){
344  if( ($ppoll->status == STATUS_NORMAL) ){
345  $errors = $ppoll->handle_inpt(); // returns array of errors
346  // if there is an input error reshow the form
347  if(sizeof($errors) != 0){
348  foreach($errors as $e){
349  $messages->add_message( new errorMessage($e) );
350  }
351  // fancy title
352  if ($ppoll->owner == $_SESSION["user"]->ID ){
353  echo "<p id='poll_title'> <span id='umfrage'>Umfrage</span> &laquo;<i>{$ppoll->name}</i>&raquo; von {$ppoll->owner_fn} <span style='float:right'><a href='backend/edit.php?pollID={$ppoll->ID}' style='font-size:60%;color:#5F5F5F' >bearbeiten</a></span></p>";
354  }else{
355  echo "<p id='poll_title'><span id='umfrage'>Umfrage</span> &laquo;<i>{$ppoll->name}</i>&raquo; von {$ppoll->owner_fn} </p>";
356  }
357  $messages->display_messages();
358  $messages->del_all_messages();
359  display_poll_form($ppoll);
360  }else{
361  // insert in DB if there is no error
362  if ( (isset($_SESSION["user"]->name)) AND ($_SESSION["user"]->is_auth === true) ){
363 
364  $email_stack = array("pollID" => $ppoll->ID , "type" => "insert" , "fullName" => $_SESSION["user"]->fullName, "uname" => $_SESSION["user"]->name , "timestamp" => "NOW()" );
365  $ret = $db->insert_poll_result($ppoll);
366  $email_stack["type"] = $ret["action"];
367  if ( $ret["value"] !== false ){
368  $notif_events = $ppoll->get_config("email_notif");
369  if( in_array($ret["action"],$notif_events) ){
370  $db->email_stack_add($email_stack);
371  }
372  }else{
373  $messages->add_message( new errorMessage("Fehler in der Datenbank! Deine Daten wurden <b>nicht</b> gespeichert") );
374  }
375 
376  $db->update_visited_polls($_SESSION["user"]->name,$ppoll->ID);
377 
378  $messages->display_messages();
379  $messages->del_all_messages();
380  echo "<a href='page.php'>Zurück</a>";
381 
382  echo "<p>Vielen Dank, dass du an dieser Umfrage teilgenommen hast.</p>";
383  // non-anonym -> get infos from DB
384  if( $ppoll->anonymous === false ){
385  $res = $db->get_poll_result_for_user($ppoll->ID,$_SESSION["user"]->name);
386  if( $res != false ){
387  echo "<p>Du hast folgende Werte übertragen:</p>";
388  echo "<table class='table_3'> <tr> <th>Frage</th> <th>Wert</th> </tr> ";
389  $res_db_colnames = array_keys($res);
390  $c = 0;
391  foreach( $res_db_colnames as $col){
392  if( (is_numeric($col)) AND ($ppoll->type == POLLTYPE_SIMPLE) ){
393  $w = $ppoll->get_widget_by_id( intval($col) );
394  if( isset($w) ){
395  $question = $w->dispName;
396  }else{
397  $question = $col;
398  }
399  }else{
400  $question = $col;
401  }
402 
403  $r = $c%2;
404  echo "<tr class='line$r'>";
405  echo "<td>$question</td>";
406  echo "<td>{$res[$col]}</td>";
407  echo "</tr>";
408  ++$c;
409 
410  }
411  echo "</table>";
412  } // end if there is a result
413  // anonyme polls -> get infos from poll-object
414  }else{
415  echo "<p>Du hast folgende Werte übertragen:</p>";
416  echo "<table class='table_3'> <tr> <th>Frage</th> <th>Wert</th> </tr> ";
417  $c = 0;
418  foreach( $ppoll->widget_list as $widget ){
419  if( $widget instanceof container ){
420  foreach( $widget->widget_list as $question ){
421  $r = $c%2;
422  echo "<tr class='line$r'>";
423  echo "<td>{$question->dispName}</td>";
424  $value = htmlspecialchars($question->value,ENT_QUOTES,"UTF-8",true);
425  echo "<td>{$value}</td>";
426  echo "</tr>";
427  ++$c;
428  }
429  }else if( ($widget instanceof input_widget) ){
430  $r = $c%2;
431  echo "<tr class='line$r'>";
432  echo "<td>{$widget->dispName}</td>";
433  $value = htmlspecialchars($widget->value,ENT_QUOTES,"UTF-8",true);
434  echo "<td>{$value}</td>";
435  echo "</tr>";
436  ++$c;
437  }
438 
439  }
440  }
441  echo "</table>";
442 
443  // timeout display
444  if( $ppoll->anonymous === false ){
445  $tmout = $ppoll->get_timeout_string();
446  echo "<p>Du kannst deine Angaben noch bis zum <b>$tmout</b> ändern.</p>";
447  }
448 
449  // charts
450  $aktuelle_statistik = false;
451  foreach( $ppoll->widget_list as $widget ){
452  if( $widget->get_config("showCharts") ){
453  $aktuelle_statistik = true;
454  break;
455  }
456  }
457  if( $aktuelle_statistik ){
458  echo "<h2>Aktuelle Statistik</h2>";
459  echo '<script src="JS/Chart.js"></script>';
460  $ppoll->load_widget_result_count($db);
461  foreach( $ppoll->widget_list as $widget ){
462  $c = $widget->get_config("showCharts");
463  if( $c === true ){
464  if( $widget instanceof chartable_widget ){
465  $widget->chart_display();
466  }
467  }
468  }
469  } // end if show aktuelle statistik
470  } // end if authentificated
471  } // end if no errors
472  } // end if poll status normal
473 }else if ($page == "preview"){
474  echo "<p id='poll_title'><span id='umfrage'>Umfrage</span> &laquo;<i>{$ppoll->name}</i>&raquo; von {$ppoll->owner_fn}</p>";
475  display_poll_form($ppoll,true); // poll, preview
476 
477 }
478 
479 
480 HTML::foot();
481 
482 // send notification emails in a new background process
483 exec("cd backend && php email-bg.php");
484 ?>
485