Umfragen
public.php
Go to the documentation of this file.
1 <?php
2 /*
3  * public.php
4  *
5  * Copyright 2013 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 function display_poll_form($show_poll,$preview=false){
25  if($show_poll->anonymous == true){
26  print_info("Diese Umfrage wird anonym ausgewertet! <br/> Es werden also keine Benutzerdaten gespeichert, die auf die Herkunft (username, voller Name) hinweisen könnten.");
27  }
28  echo '<form method="POST" action="">';
29  $show_poll->display();
30  echo "<p style='margin-top:2em;'>Die Felder, die mit <span class='required_asterisk'>*</span> markiert sind, sind Plichtfelder.</p>";
31  if($preview==false){
32  echo '<br/><input type="submit" value="Senden" name="poll_submit"/>';
33  }
34  echo "</form>";
35 }
36 
37 
38 
39 // SESSION
40 require_once("inc/user.class.php");
41 if( !(isset($_SESSION)) ){
42  session_start();
43 }
44 
45 session_regenerate_id();
46 
47 
48 require_once("config.php");
49 require_once("inc/tools.php");
50 
51 require_once("inc/db.class.php");
52 require_once("inc/config.class.php");
53 require_once("inc/poll.class.php");
54 require_once("inc/html.class.php");
55 require_once("inc/messages.class.php");
56 require_once("inc/chart.class.php");
57 $db= new db( DB_USER, DB_PASS, DB_DATABASE, DB_HOST );
59 $config->load();
60 
61 $now = time();
62 
63 // LOAD POLLS
64 $ps = $db->get_all_polls_array();
65 $polls = array();
66 foreach($ps as $pps){
67  // check if public poll
68  if( $pps["anonymous"] == "2"){
69  $p = new poll();
70  $p->load_from_id($db,$pps["ID"]);
71  // check timeout and Status
72  if( ($p->status == STATUS_NORMAL) AND
73  ($p->get_timeout_timestamp() >= $now) ){
74  $polls[$p->ID] = $p;
75  }
76  unset($p);
77  }
78 }
79 
80 // default page --> list public polls
81 $page = "index";
82 
83 
84 if( (isset($_GET["h"])) AND (strlen($_GET["h"]) == 32) ){
85  // if poll is public
86  foreach( $polls as &$p ){
87  if( $p->hashID === $_GET["h"] ){
88  $poll = $p;
89  $page = "show poll";
90  }
91  } // end foreach poll
92 } // end if option "h"
93 
94 
95 // submit form
96 if( (isset($_POST["poll_submit"])) AND (isset($poll)) ){
97  $poll->load_from_id($db,$poll->ID); // get old results
98  $page = "handle poll";
99 }
100 
101 // load counters for widgets
102 if( ($page == "show poll") OR ($page == "handle poll") ){
103  $poll->load_widget_result_count($db);
104 }
105 
106 
107 // BEGIN HTML
108 HTML::doctype();
109 // include polls theme
110 if( ($page == "show poll") OR ($page == "handle poll") ){
111  if( $poll->theme !== "" ){
112  HTML::head('<link href="'.APP_ROOT.'/CSS/Themes/'.$poll->theme.'" rel="stylesheet" type="text/css">' );
113  }else{
114  HTML::head();
115  }
116 }else{
117  HTML::head();
118 }
119 HTML::menu();
120 
121 
122 if ($page == "index"){
123 
124 
125 // SHOW POLL FORM
126 }else if ($page == "show poll"){
127  // fancy title
128  echo "<p id='poll_title'> <span id='umfrage'>Umfrage</span> &laquo;<i>{$poll->name}</i>&raquo;</p>";
129  // display ... timeout and status check done by loading complete list above
130  display_poll_form($poll);
131 
132 // HANDLE POLL INPUT
133 }else if($page == "handle poll"){
134 
135  $errors = $poll->handle_inpt(); // returns array of errors
136 
137  // if there is an input error reshow the form
138  if(sizeof($errors) != 0){
139  foreach($errors as $e){
140  print_err($e);
141  }
142  // fancy title
143  echo "<p id='poll_title'> <span id='umfrage'>Umfrage</span> &laquo;<i>{$poll->name}</i>&raquo;</p>";
144  display_poll_form($poll);
145 
146  }else{
147  // insert in DB if there is no error
148 
149  $ret = $db->insert_poll_result($poll);
150 
151  $email_stack = array("pollID" => $poll->ID , "type" => $ret["action"] , "fullName" => "Unbekannt", "uname" => "" , "timestamp" => "NOW()" );
152  if ( $ret["value"] !== false ){
153  $notif_events = $poll->get_config("email_notif");
154  if( in_array($ret["action"],$notif_events) ){
155  $db->email_stack_add($email_stack);
156  }
157  // failure
158  }else{
159  $messages->add_message( new errorMessage("Fehler in der Datenbank! Deine Daten wurden <b>nicht</b> gespeichert") );
160  }
161 
162  echo "<a href='public.php'>Zurück</a>";
163 
164  echo "<p>Vielen Dank, dass du an dieser Umfrage Teilgenommen hast.</p>";
165  // charts
166  $aktuelle_statistik = false;
167  foreach( $poll->widget_list as $widget ){
168  if( $widget->get_config("showCharts") ){
169  $aktuelle_statistik = true;
170  break;
171  }
172  }
173  if( $aktuelle_statistik ){
174  echo "<h2>Aktuelle Statistik</h2>";
175  echo '<script src="JS/Chart.js"></script>';
176  $poll->load_widget_result_count($db);
177  foreach( $poll->widget_list as $widget ){
178  $c = $widget->get_config("showCharts");
179  if( $c === true ){
180  if( $widget instanceof chartable_widget ){
181  $widget->chart_display();
182  }
183  }
184  }
185  } // end if show aktuelle Statistik
186  // SHOW RESULTS !!!
187 
188 
189  } // end if errors
190 }
191 
192 
193 HTML::foot();
194 
195 // send notification emails in a new background process
196 exec("cd backend && php email-bg.php");
197 
198 ?>