Umfragen
checkBoxSingle.class.php
Go to the documentation of this file.
1 <?php
3  public $typ = "checkBoxSingle";
4  public $direct_result = true;
5 
12  public function check_max(){
13  $true_val = $this->get_config("true");
14  $false_val = $this->get_config("false");
15 
16  // get name
17  $name = "mit der ID {$this->ID}";
18  if( (isset($this->dispName) ) AND ( $this->dispName != "") ){
20  }else if ( (isset($this->name) ) AND ( $this->name != "") ){
22  }
23 
24  // get config and check
25  $max = $this->get_config("max");
26  if($max == -1){ // no limit
27  return true;
28  }
29  if( $this->value == $true_val){ // is one of the lucky guys
30  return true;
31  }
32  if( ($this->get_data("count_true") >= $max) ){
33  return "Das Feld <b>$name</b> ist leider nicht mehr verfügbar! Bitte wähle ein Anderes.";
34  }
35  return true;
36  }
37 
38  public function set_default_values($db){
39  $this->set_dispName("Bitte Fragestellung eingeben");
40  }
41 
42  public function get_data($name){
43  if( $name == "count_true" ){
44  if( isset($this->data["count_true"]) ){
45  return $this->data["count_true"];
46  }else{
47  return 0;
48  }
49  }else if( $name == "count_false" ){
50  if( isset($this->data["count_false"]) ){
51  return $this->data["count_false"];
52  }else{
53  return 0;
54  }
55  }else{
56  return parent::get_data($name);
57  }
58  }
59 
60  public function set_data($name,$value){
61  if($name == "count_true"){
62  if( is_numeric($value) ){
63  $this->data["count_true"] = intval($value);
64  }
65  }else if($name == "count_false"){
66  if( is_numeric($value) ){
67  $this->data["count_false"] = intval($value);
68  }
69  }else{
71  }
72  }
73 
74 
75  public function display_option_edit($polltype){
76 
77  }
78  public function handle_option_edit(){
79 
80  }
81 
82  public function display_config_edit(){
83  $true_val = $this->get_config("true");
84  $false_val = $this->get_config("false");
85  if($true_val == ""){$true_val="Ja";}
86  if($false_val == ""){$false_val="Nein";}
87  echo "<table>";
88  echo "<tr>";
89  echo "<td><label for='checkboxsingle_value_true'>Wert bei angewählter Checkbox</label></td>";
90  echo "<td><input type='text' name='checkboxsingle_value_true' id='checkboxsingle_value_true' value='$true_val' /></td>";
91  echo "</tr>";
92  echo "<tr>";
93  echo "<td><label for='checkboxsingle_value_false'>Wert bei <b>ab</b>gewählter Checkbox</label></td>";
94  echo "<td><input type='text' name='checkboxsingle_value_false' id='checkboxsingle_value_false' value='$false_val'/></td>";
95  echo "</tr>";
96  echo "</table>";
97  }
98 
99  public function handle_config_edit(){
100  $all_err_reason = "";
101  if( isset($_POST["checkboxsingle_value_true"]) ){
102  $err = $this->set_config("true",$_POST["checkboxsingle_value_true"]);
103  if( $err !== true ){
105  }
106  }else{
107  $this->set_config("true","Ja");
108  }
109 
110  if( isset($_POST["checkboxsingle_value_false"]) ){
111  $err = $this->set_config("false",$_POST["checkboxsingle_value_false"]);
112  if( $err !== true ){
114  }
115  }else{
116  $this->set_config("false","Nein");
117  }
118 
119  if($all_err_reason == ""){
120  return true;
121  }else{
122  return $all_err_reason;
123  }
124 
125  }
126 
127 
128  public function display_chartType_edit(){
129  $allowed = array("pieChart","barChart","doughnutChart");
130  $allowed_names = array("Kreisdiagramm","Säulendiagramm","Ringdiagramm");
131  $this->display_chartType_edit_generic($allowed,$allowed_names);
132  }
133 
134  public function handle_chartType_edit(){
135  $allowed = array("pieChart","barChart","doughnutChart");
136  return $this->handle_chartType_edit_generic($allowed);
137  }
138 
139 
140  public function display_show_charts_edit(){
141  $cnf = $this->get_config("showCharts");
142  $ckd = "";
143  if($cnf == true ){ $ckd = "checked='checked'"; }
144  echo "<input type='checkbox' name='show_charts' value='true' id='show_charts' $ckd/>
145  <label for='how_charts'>Aswertung der Frage nach dem Ausfüllen der Umfrage anzeigen</label>";
146  }
147 
148  public function handle_show_charts_edit(){
149  if( (isset($_POST["show_charts"])) AND ($_POST["show_charts"]=="true") ){
150  $r = $this->set_config("showCharts",true);
151  if($r !== true){
152  return $r;
153  }else{
154  return true;
155  }
156 
157  }else{
158  $r = $this->set_config("showCharts",false);
159  return $r;
160  }
161  }
162 
163 
164 
165  public function display_max_edit(){
166  $max = $this->get_config("max");
167  echo "<label for='checkboxsingle_max'>Maximum </label>";
168  echo "<input type='text' name='checkboxsingle_max' id='checkboxsingle_max' value='$max' />";
169  }
170 
171  public function handle_max_edit(){
172  $all_err_reason = "";
173  if( isset($_POST["checkboxsingle_max"]) ){
174  $err = $this->set_config("max",$_POST["checkboxsingle_max"]);
175  if( $err !== true ){
177  }
178  }else{
179  $this->set_config("max",-1);
180  }
181 
182  if($all_err_reason == ""){
183  return true;
184  }else{
185  return $all_err_reason;
186  }
187  }
188 
189 
190 
191  public function display_edit(){
192  $true_val = $this->get_config("true");
193  $font_bold= $this->get_config("font-bold");
194 
195  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
196  if($this->is_required){
197  $req = "<span class='required_asterisk'>*</span>";
198  }else{$req = "";}
199 
200  $this->display_edit_begin();
201 
202  echo "<input type='checkbox' name='{$this->name}' id='{$this->name}' value='true' />&nbsp;&nbsp;";
203  echo "<label for='{$this->name}' style='$font_bold' >$req {$this->dispName}</label><br/>";
204  echo "</div>".PHP_EOL;
205  }
206 
207 
208  public function display(){
209  $true_val = $this->get_config("true");
210  $font_family= $this->get_config("font-family");
211  $font_size= $this->get_config("font-size");
212  $font_bold= $this->get_config("font-bold");
213  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
214  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
215  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
216 
217  if( $this->value == $true_val){
218  $checked = "checked='checked'";
219  }else{ $checked = "";}
220 
221  if($this->is_required){
222  $req = "<span class='required_asterisk'>*</span>";
223  }else{$req = "";}
224 
225  $max = $this->get_config("max");
226 
227  // disable when max limit reached
228  if( ($this->get_data("count_true") >= $max) AND ($max != -1) ){
229  $active_style = "disabled_widget_text";
230  // if editing allow deselection
231  if( $this->value !== $true_val ){
232  $active = "disabled='disabled'";
233  }else{
234  $active = "";
235  }
236  }else{$active="";$active_style="";}
237 
238  echo "<div class='widget_container checkBoxSingle_widget' style='$font_family;$font_size'>";
239  echo "<input type='checkbox' name='{$this->name}' id='{$this->name}' value='true' {$checked} $active />&nbsp;&nbsp;";
240  echo "<label for='{$this->name}' class='widget_question $active_style' style='$font_bold' > $req {$this->dispName}</label><br/>";
241  echo "</div>".PHP_EOL;
242  }
243 
244  public function handle_inpt(){
245  $true_val = $this->get_config("true");
246  $false_val = $this->get_config("false");
247  $req = $this->check_required();
248  if($req !== true) {
249  return $req;
250  }else{
251  if( (!isset($this->name)) OR ($this->name == "") ){
252  throw new varException("Widget Name not set!");
253  }
254 
255  if( (isset($_POST[ $this->name ])) AND ($_POST[ $this->name ] == "true") ){
256  $max = $this->check_max();
257  if($max !== true){
258  return $max;
259  }
260  $this->value = $true_val;
261  }else{
262  $this->value = $false_val;
263  }
264  return true;
265  }
266  }
267 
268  public function count($db){
269  // init
270  $this->set_data("count_true",0);
271  $this->set_data("count_false",0);
272 
273  // do sql
274  $sql = "
275  SELECT
276  `{$this->name}` AS 'values',
277  COUNT(`{$this->name}`) AS count
278  FROM `umfragen`.`{$this->pollID}_results`
279  GROUP BY `values`;";
280  $res = $db->get_sql_all_assoc($sql);
281 
282  // write data
283  if( is_array($res) ){
284  foreach( $res as $row ){
285  $val_true = $this->get_config("true");
286  $val_false = $this->get_config("false");
287  if( $row[ "values" ] == $val_true){
288  //$list[ "{$widget->ID}_true" ] = intval($val["count"]);
289  $this->set_data("count_true" , intval($row["count"]) );
290  }else if( $row[ "values" ] == $val_false){
291  $this->set_data("count_false" , intval($row["count"]) );
292  }
293  } // end foreach row
294  }else{
295  return false;
296  }
297  return true;
298  }
299 
300  /*--------------------------------------------------------
301  * ** CHART **
302  * -------------------------------------------------------*/
303  public function chart_make(){
304  $required_classes = array("pieChart","doughnutChart","barChart");
305  foreach($required_classes as $c ){
306  if( !class_exists($c) ){
307  throw new exception("Chart classes not loaded. Please include chart.class.php");
308  }
309  }
310  $type = $this->get_config("chartType");
311  $data = $this->chart_format_data();
312  if( $type == "barChart" ){
313  $this->chart = new barchart($data);
314 
315  }else if( $type == "pieChart" ){
316  $this->chart = new piechart($data);
317 
318  }else if( $type == "doughnutChart" ){
319  $this->chart = new doughnutchart($data);
320  }else{
321  $this->chart = new piechart($data);
322  }
323  }
324 
329  private function chart_format_data_barChart( ){
330  $data = array( "labels" => array() , "datasets" => array( array() ) );
331  $data["labels"][0] = $this->get_config("true");
332  $data["labels"][1] = $this->get_config("false");
333 
334  $data["datasets"][0][0] = $this->get_data("count_true");
335  $data["datasets"][0][1] = $this->get_data("count_false");
336  return $data;
337  }
338 
343  private function chart_format_data_pieChart( ){
344  $data = array();
345  $data[0] = $this->get_data("count_true");
346  $data[1] = $this->get_data("count_false");
347  return $data;
348  }
349 
355  return $this->chart_format_data_pieChart();
356  }
357 
358 
359  public function chart_format_data(){
360  $type = $this->get_config("chartType");
361 
362  $data = array();
363  if( $type == "barChart" ){
365  }else if( $type == "pieChart" ){
367  }else if( $type == "doughnutChart" ){
369  }else{
370  $data = $this->chart_format_data_pieChart();
371  }
372  return $data;
373  }
374 
378  private function chart_display_legend_barChart(){
379  }
380 
384  private function chart_display_legend_pieChart(){
385  if( isset($this->chart) ){
386  $data["labels"][0] = $this->get_config("true");
387  $data["labels"][1] = $this->get_config("false");
388 
389  echo "<table>";
390  echo "<tr>";
391  $color = $this->chart->colors->getRGBA(0,"1");
392  echo "<td style='background-color:$color;width:1em;height:1em'> &nbsp; </td>";
393  $cnf = $this->get_config("true");
394  echo "<td>{$cnf} <span style='color:#810000'>({$this->get_data("count_true")})</span></td>";
395  echo "</tr>";
396  echo "<tr>";
397  $color = $this->chart->colors->getRGBA(1,"1");
398  echo "<td style='background-color:$color;width:1em;height:1em'> &nbsp; </td>";
399  $cnf = $this->get_config("false");
400  echo "<td>{$cnf} <span style='color:#810000'>({$this->get_data("count_false")})</span></td>";
401  echo "</tr>";
402  echo "</table>";
403  }
404  }
405 
411  }
412 
413 
414  public function chart_display_legend(){
415  if( isset($this->chart) ){
416  if( $this->chart instanceof barChart ){
418 
419  }else if( $this->chart instanceof pieChart ){
421 
422  }else if( $this->chart instanceof doughnutChart ){
424  }
425 
426  } // end if chart set
427  }
428 
429 
430  public function chart_display_chart(){
431  if( !isset($this->chart) ){
432  $this->chart_make();
433  }
434  if( isset($this->chart) ){
435  $this->chart->display();
436  }
437  }
438 
439  public function chart_display(){
440  if( !isset($this->chart) ){
441  $this->chart_make();
442  }
443  if( isset($this->chart) ){
444  echo "<h3>{$this->dispName}</h3>";
445  echo "<table><tr>";
446  echo "<td>";
447  $this->chart->display();
448  echo "</td>";
449  echo "<td style='padding-left:30px'>";
450  $this->chart_display_legend();
451  echo "</td>";
452  echo "</tr></table>";
453 
454  }
455  }
456 
457 }
458 // ! no ending newLine
459 ?>