Umfragen
radioButtonList.class.php
Go to the documentation of this file.
1 <?php
2 if(!class_exists("radioButton") ){
3  die("radioButton class is not loaded ... please load it before radioButtonList");
4 }
5 
7  public $typ = "radioButtonList";
8  public $direct_result = true;
9  public $child_widgets_whitelist = array();
10  public $child_options_whitelist = array("radioButton");
11 
12  public $option_list = array();
13 
14  private $chart = null;
15 
16  function __construct(){
17  }
18 
19  public function set_default_values($db){
20  $this->set_dispName("Bitte Fragestellung eingeben");
21 
22  if( (isset($this->pollID)) AND (isset($this->ID)) AND (!empty($this->pollID)) AND (!empty($this->ID)) AND ($this->pollID != -1) AND ($this->ID != -1) ){
23  $this->widget_list = new object_list($this->pollID,$this->ID);
24  $this->option_list = new object_list($this->pollID,$this->ID);
25  $opt = new radioButton();
26  $opt->set_default_values($db);
27  $this->insert_option(-1,$opt);
28  $db->insert_widget($opt,POLLTYPE_SIMPLE);
29 
30  }
31  }
32 
33 
34  public function get_config($name){
35  if(!is_array($this->config)){
36  $this->unserialize_config();
37  }
38 
39  return parent::get_config($name);
40  }
41 
42 
43 
44  public function set_config($name,$val){
45  if( (!isset($this->config)) OR ($this->config == "") ){
46  $this->config = array();
47  }
48  if( (isset($this->config)) AND (is_string($this->config)) AND ($this->config != "") ){
49  $this->unserialize_config();
50  }
51 
52  return parent::set_config($name,$val);
53  }
54 
55 
56  // using check_required from widgetBaseClass not container class
57  function check_required(){
58  if($this->is_required == false){
59  return true;
60  }
61 
62  $name = "mit der ID {$this->ID}";
63  if( (isset($this->dispName) ) AND ( $this->dispName != "") ){
65  }else if ( (isset($this->name) ) AND ( $this->name != "") ){
67  }
68 
69  if( !isset($_POST[ $this->name ]) ){
70  return "Das Feld <b>$name</b> ist ein Plichtfeld. Bitte dieses angeben.";
71  }else{
72  if( $_POST[ $this->name ] =="" ){
73  return "Das Feld <b>$name</b> ist ein Plichtfeld. Bitte dieses angeben.";
74  }else{
75  return true;
76  }
77  }
78  }
79 
80 
82  $cnf = $this->get_config("dropdown");
83  $ckd = "";
84  if($cnf == true ){ $ckd = "checked='checked'"; }
85  echo "<input type='checkbox' name='display_as_drop_down' value='true' id='display_as_drop_down' $ckd/>
86  <label for='display_as_drop_down'>Als Dropdown-liste anzeigen</label>";
87  }
88 
90  if( (isset($_POST["display_as_drop_down"])) AND ($_POST["display_as_drop_down"]=="true") ){
91  $r = $this->set_config("dropdown","true");
92  if($r !== true){
93  return $r;
94  }else{
95  return true;
96  }
97 
98  }else{
99  $r = $this->set_config("dropdown","false");
100  return $r;
101  }
102  }
103 
104 
105 
106  function display_edit(){
107  if( (sizeof($this->option_list) == 0) OR !(isset($this->name)) ){
108  return false;
109  }else{
110  $dropdown = $this->get_config("dropdown");
111  $font_bold= $this->get_config("font-bold");
112  if($this->is_required){
113  $req = "<span class='required_asterisk'>*</span>";
114  }else{$req = "";}
115  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
116 
117 
118  if($dropdown == true ){
119  $this->display_edit_begin();
120  echo "<label for='{$this->name}' style='$font_bold'>{$this->dispName} $req</label><br/>".PHP_EOL;
121  echo "<span class='widget_indent'></span> <select id='{$this->name}' name='{$this->name}' >";
122  echo "<option value=''></option>";
123  foreach($this->option_list as $opt){
124  $opt->display2();
125  }
126  echo "</select>";
127  echo "</div>".PHP_EOL;
128  }else{
129  $this->display_edit_begin();
130 
131  $columns = $this->get_config("columns");
132  $option_list = array();
133 
134  if( $columns == "columns" ){
135  $option_list = $this->rotate_option_list();
136  }else{
138  }
139 
140  echo "<fieldset ><legend style='$font_bold' >{$this->dispName} $req</legend>".PHP_EOL;
141  echo "<table><tr>";
142  foreach($option_list as $option){
143  // radioButtons and dummyRadioButtons have display method
144  $option->display(false,$columns,$this->name);
145  }
146  echo "</tr></table>";
147  echo "</fieldset>";
148  echo "</div>".PHP_EOL;
149  } // end if dropdown
150  }
151  }
152 
153  function display(){
154  if( (sizeof($this->option_list) == 0) OR !(isset($this->name)) ){
155  return false;
156  }else{
157  $dropdown = $this->get_config("dropdown");
158  if($this->is_required){
159  $req = "<span class='required_asterisk'>*</span>";
160  }else{$req = "";}
161  $font_family= $this->get_config("font-family");
162  $font_size= $this->get_config("font-size");
163  $font_bold= $this->get_config("font-bold");
164 
165  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
166  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
167  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
168 
169  // DROPDOWN
170  if($dropdown == true ){
171  echo "<div class='widget_container dropDownList_widget' style='$font_family;$font_size'>";
172  echo "<legend for='{$this->name}' style='$font_bold' ><span class='widget_question'>{$this->dispName} $req</span></legend>".PHP_EOL;
173  echo "<span class='widget_indent'></span> <select id='{$this->name}' name='{$this->name}' >";
174  echo "<option value=''></option>";
175  foreach($this->option_list as $opt){
176  if( (isset($this->value)) AND ($this->value == $opt->value) ){
177  $sel = true;
178  }else{$sel = false;}
179 
180  $opt->display2($sel);
181  }
182  echo "</select>";
183  echo "</div>".PHP_EOL;
184  // RADIO BUTTON
185  }else{
186  $columns = $this->get_config("columns");
187  $option_list = array();
188 
189  if( $columns == "columns" ){
190  $option_list = $this->rotate_option_list();
191  }else{
193  }
194 
195  echo "<div class='widget_container radioButtonList_widget' style='$font_family;$font_size'>";
196  echo "<fieldset ><legend class='widget_question' style='$font_bold' >{$this->dispName} $req</legend>".PHP_EOL;
197  echo "<table><tr>";
198 
199  foreach($option_list as $option){
200  if( (isset($this->value)) AND ($this->value == $option->value)){
201  $sel = true;
202  }else{$sel=false;}
203  // radioButtons and dummyRadioButtons have display method
204  $option->display($sel,$columns,$this->name);
205  }
206  echo "</tr></table>";
207  echo "</fieldset>";
208  echo "</div>".PHP_EOL;
209  } // end if dropdown
210  }
211  }
212 
213  function handle_inpt(){
214  $req = $this->check_required();
215  // if there is a value do not trigger required
216  if( $this->value != "" ){
217  $req = true;
218  }
219 
220  if($req !== true){
221  return $req;
222  }else{
223  if( (!isset($this->name)) OR ($this->name == "") ){
224  throw new varException("Widget Name not set!");
225  }
226  if( (!isset($this->option_list)) OR ($this->option_list == "") ){
227  throw new varException("Widget list of options not set!");
228  }
229 
230  $value = -1;
231  for ($i=0 ; $i<sizeof($this->option_list) ; ++$i){
232  if ( isset($_POST[$this->name]) ){
233  // if option selected AND NOT changed do nothing
234  if( ($this->value != "") AND ($this->value == $_POST[$this->name]) AND ($this->option_list[$i]->value == $_POST[$this->name]) ){
235  return true;
236  // option selected
237  }else if ($this->option_list[$i]->value == $_POST[$this->name]) {
238  $max = $this->option_list[$i]->check_max();
239  if( $max !== true){
240  return $max;
241  }
242  $value = $i;
243  }
244  } // end if isset post
245  } // end for
246  // set
247  if ($value != -1){
248  $this->value = $this->option_list[$value]->value;
249  }
250  return true;
251 
252  }
253  }
254 
255  function count($db){
256  // init with 0
257  foreach( $this->option_list as $child ){
258  $child->set_data("count",0);
259  }
260 
261  // do sql
263  $sql = "
264  SELECT `{$pollID}_results`.`$name`,
265  `$pollID`.`parent`,
266  `$pollID`.`ID`,
267  count(`ID`) as count
268  FROM `{$pollID}_results`
269  JOIN `$pollID` ON
270  `$pollID`.`value`=`{$pollID}_results`.`$name`
271  AND `$pollID`.`parent`='$ID'
272  GROUP BY `$pollID`.`ID`;";
273  $res = $db->get_sql_all_assoc($sql);
274 
275  // write data
276  if( is_array($res) ){
277  foreach( $res as $row ){
278  $child = $this->get_option_by_id( intval($row["ID"]) );
279  if( isset($child) ){
280  $child->set_data("count",intval($row["count"]));
281  }
282  }
283  }else{
284  return false;
285  }
286  return true;
287  }
288 
289 
290  /*--------------------------------------------------------
291  * ** CHART **
292  * -------------------------------------------------------*/
293  public function chart_make(){
294  $required_classes = array("pieChart","doughnutChart","radarChart","barChart");
295  foreach($required_classes as $c ){
296  if( !class_exists($c) ){
297  throw new exception("Chart classes not loaded. Please include chart.class.php");
298  }
299  }
300  $type = $this->get_config("chartType");
301  $data = $this->chart_format_data();
302  if( $type == "barChart" ){
303  $this->chart = new barchart($data);
304 
305  }else if( $type == "pieChart" ){
306  $this->chart = new piechart($data);
307 
308  }else if( $type == "doughnutChart" ){
309  $this->chart = new doughnutchart($data);
310 
311  }else if( $type == "radarChart" ){
312  $this->chart = new radarchart($data);
313  }else{
314  $this->chart = new piechart($data);
315  }
316  }
317 
322  private function chart_format_data_barChart( ){
323  $data = array( "labels" => array() , "datasets" => array( array() ) );
324  for( $i=0 ; $i<sizeof($this->option_list) ; ++$i ){
325  $data["labels"][] = "Antwort".($i+1);
326  $data["datasets"][0][] = $this->option_list[$i]->get_data("count");
327  }
328  return $data;
329  }
330 
335  private function chart_format_data_pieChart( ){
336  $data = array();
337  foreach( $this->option_list as $option ){
338  $data[] = $option->get_data("count");
339  }
340  return $data;
341  }
342 
348  return $this->chart_format_data_pieChart();
349  }
350 
355  public function chart_format_data_radarChart(){
356  return $this->chart_format_data_barChart();
357  }
358 
359 
360  public function chart_format_data(){
361  $type = $this->get_config("chartType");
362 
363  $data = array();
364  if( $type == "barChart" ){
366  }else if( $type == "pieChart" ){
368  }else if( $type == "doughnutChart" ){
370  }else if( $type == "radarChart" ){
372  }else{
373  $data = $this->chart_format_data_pieChart();
374  }
375 
376  return $data;
377  }
378 
379 
380 
384  private function chart_display_legend_barChart(){
385  echo "<table style='border-collapse:collapse'>";
386  $c=0;
387  echo "<tr><th colspan='2'>Legende (v.l.n.r.)</th></tr>";
388  foreach( $this->option_list as $option ){
389  echo "<tr style='border-bottom:1px solid #D0D0D0'>";
390  $n = $c+1;
391  echo "<td style='border-right:1px solid #949494;padding-right:1em'>Antwort$n</td>";
392  echo "<td style='padding-left:1em'>{$option->dispName} <span style='color:#810000'>({$option->get_data("count")})</span></td>";
393  echo "</tr>";
394  ++$c;
395  }
396  echo "</table>";
397  }
398 
404  }
405 
409  private function chart_display_legend_pieChart(){
410  if( isset($this->chart) ){
411  echo "<table style='border-collapse:collapse'>";
412  $c=0;
413  echo "<tr><th colspan='2'>Legende</th></tr>";
414  foreach( $this->option_list as $option ){
415  echo "<tr style='border-bottom:1px solid #D0D0D0;'>";
416  $color = $this->chart->colors->getRGBA($c,"1");
417  $n = $c+1;
418  echo "<td style='background-color:$color;width:1em;height:1em;border: 2px solid white'> &nbsp; </td>";
419  echo "<td>{$option->dispName} <span style='color:#810000'>({$option->get_data("count")})</span></td>";
420  echo "</tr>";
421  ++$c;
422  }
423  echo "</table>";
424  }
425  }
426 
432  }
433 
434 
435  public function chart_display_legend(){
436  if( isset($this->chart) ){
437  if( $this->chart instanceof barChart ){
439 
440  }else if( $this->chart instanceof radarChart ){
442 
443  }else if( $this->chart instanceof pieChart ){
445 
446  }else if( $this->chart instanceof doughnutChart ){
448  }
449 
450  } // end if chart set
451  }
452 
453 
454  public function chart_display_chart(){
455  if( !isset($this->chart) ){
456  $this->chart_make();
457  }
458  if( isset($this->chart) ){
459  $this->chart->display();
460  }
461  }
462 
463  public function chart_display(){
464  if( !isset($this->chart) ){
465  $this->chart_make();
466  }
467  if( isset($this->chart) ){
468  echo "<h3>{$this->dispName}</h3>";
469  echo "<table><tr>";
470  echo "<td>";
471  $this->chart->display();
472  echo "</td>";
473  echo "<td style='padding-left:30px'>";
474  $this->chart_display_legend();
475  echo "</td>";
476  echo "</tr></table>";
477 
478  }
479  }
480 
481 }
482 
483 // ! no ending newLine
484 ?>