Umfragen
checkBoxList.class.php
Go to the documentation of this file.
1 <?php
2 if(!class_exists("checkBox") ){
3  die("CheckBox class is not loaded ... please load it before checkBoxList");
4 }
6  public $typ = "checkBoxList";
7  public $direct_result = true;
8  public $long_result = true;
9  public $child_widgets_whitelist = array();
10  public $child_options_whitelist = array("checkBox");
11 
12  public $option_list = array();
13 
14 
15  function __construct(){
16  }
17 
18  public function get_config($name){
19  if(!is_array($this->config)){
20  $this->unserialize_config();
21  }
22 
23  return parent::get_config($name);
24  }
25 
26 
27  public function set_config($name,$val){
28  if( (!isset($this->config)) OR ($this->config == "") ){
29  $this->config = array();
30  }
31  if( (isset($this->config)) AND (is_string($this->config)) AND ($this->config != "") ){
32  $this->unserialize_config();
33  }
34 
35  return parent::set_config($name,$val);
36  }
37 
38 
39  function check_required(){
40  if($this->is_required == false){
41  return true;
42  }
43 
44  $name = "mit der ID {$this->ID}";
45  if( (isset($this->dispName) ) AND ( $this->dispName != "") ){
47  }else if ( (isset($this->name) ) AND ( $this->name != "") ){
49  }
50 
51  $ok = false;
52  $keys = array_keys($_POST);
53  foreach($this->option_list as $opt){
54  if( in_array($opt->ID,$keys) ){
55  if( $_POST[$opt->ID] != ""){
56  $ok = true;
57  break;
58  }
59  }
60  }
61 
62  if( $ok === false ){
63  return "Das Feld <b>$name</b> ist ein Plichtfeld. Bitte dieses angeben.";
64  }else{
65  return true;
66  }
67 
68  }
69 
70  public function set_default_values($db){
71  $this->set_dispName("Bitte Fragestellung eingeben");
72  if( (isset($this->pollID)) AND (isset($this->ID)) AND (!empty($this->pollID)) AND (!empty($this->ID)) AND ($this->pollID != -1) AND ($this->ID != -1) ){
73  $this->widget_list = new object_list($this->pollID,$this->ID);
74  $this->option_list = new object_list($this->pollID,$this->ID);
75  $opt = new checkBox();
76  $opt->set_default_values($db);
77  $this->insert_option(-1,$opt);
78  $db->insert_widget($opt,POLLTYPE_SIMPLE);
79 
80  }
81  }
82 
83 
84  function display_edit(){
85  if( (sizeof($this->option_list) == 0) OR !(isset($this->name)) ){
86  return false;
87  }else{
88  if($this->is_required){
89  $req = "<span class='required_asterisk'>*</span>";
90  }else{$req = "";}
91  $font_bold= $this->get_config("font-bold");
92  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
93 
94  $columns = $this->get_config("columns");
95  $option_list = array();
96 
97  if( $columns == "columns" ){
98  $option_list = $this->rotate_option_list();
99  }else{
101  }
102 
103  $this->display_edit_begin();
104 
105  echo "<fieldset ><legend style='$font_bold' >{$this->dispName} $req</legend>".PHP_EOL;
106  $values = explode(",",$this->value);
107  echo "<table><tr>";
108  foreach($option_list as $option){
109  if( (isset($this->value)) AND ($this->value == $option->value)){
110  $sel = true;
111  }else{$sel=false;}
112  // radioButtons and dummyRadioButtons have display method
113  $option->display($sel,$columns);
114  }
115  echo "</tr></table>";
116  echo "</fieldset>";
117  echo "</div>".PHP_EOL;
118  }
119  }
120 
121 
122  function display(){
123  if( (sizeof($this->option_list) == 0) OR !(isset($this->name)) ){
124  return false;
125  }else{
126  if($this->is_required){
127  $req = "<span class='required_asterisk'>*</span>";
128  }else{$req = "";}
129  $font_family= $this->get_config("font-family");
130  $font_size= $this->get_config("font-size");
131  $font_bold= $this->get_config("font-bold");
132  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
133  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
134  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
135 
136  $columns = $this->get_config("columns");
137  $option_list = array();
138 
139  if( $columns == "columns" ){
140  $option_list = $this->rotate_option_list();
141  }else{
143  }
144 
145  echo "<div class='widget_container checkBoxList_widget' style='$font_family;$font_size'>";
146  echo "<fieldset ><legend style='$font_bold' class='widget_question' >{$this->dispName} $req</legend>".PHP_EOL;
147  $values = explode(",",$this->value);
148  echo "<table><tr>";
149 
150  foreach($option_list as $option){
151 
152  if( in_array($option->value,$values) ){
153  $sel = true;
154  }else{$sel=false;}
155  // radioButtons and dummyRadioButtons have display method
156  $option->display($sel,$columns);
157  }
158 
159  echo "</tr></table>";
160  echo "</fieldset>";
161  echo "</div>".PHP_EOL;
162  }
163  }
164 
165 
166  function handle_inpt(){
167  $req = $this->check_required();
168  if($req !== true){
169  return $req;
170  }else{
171  if( (!isset($this->name)) OR ($this->name == "") ){
172  throw new varException("Widget Name not set!");
173  }
174  if( (!isset($this->option_list)) OR ($this->option_list == "") ){
175  throw new varException("Widget list of options not set!");
176  }
177 
178  $values_old = explode(",",$this->value); // old values should be loaded
179  $values = array();
180  // walk through option list
181  foreach($this->option_list as $option){
182  $max = $option->check_max();
183  // if the option is seleced
184  if ( (isset($_POST[ $option->ID ])) AND ($_POST[ $option->ID ] == $option->value) ){
185  // if option not available AND option not already selected before
186  if( ($max !== true) AND (!in_array($_POST[ $option->ID ],$values_old)) ){
187  return $max;
188  }
189  $values[] = $option->value;
190  }
191 
192  }
193 
194  if ( sizeof($values) != 0){
195  $this->value = implode(",",$values);
196  }
197  return true;
198  }
199  }
200 
201  function count($db){
202  // init with 0
203  foreach( $this->option_list as $child ){
204  $child->set_data("count",0);
205  }
206 
207  // do sql
208  $sql = "
209  SELECT
210  `{$this->name}` AS 'values',
211  COUNT(`{$this->name}`) AS count
212  FROM `umfragen`.`{$this->pollID}_results`
213  GROUP BY `values`;";
214  $res = $db->get_sql_all_assoc($sql);
215 
216  // write data
217  if( is_array($res) ){
218  // go through each result combination
219  foreach( $res as $row ){
220  $vals = explode(",",$row[ "values" ]);
221  // go through each value
222  foreach($vals as $val){
223  // get the child widget belonging to the value.. if there are 2 same values there can be confusion
224  foreach( $this->option_list as $child){
225  if( $child->value == $val){
226  $child_set = $child;
227  break;
228  }
229  }
230  // if widget found add counter for actual result combination
231  if( isset($child_set) ){
232  $actual = $child_set->get_data("count"); // return 0 when no data set
233  $child_set->set_data("count", $actual+intval($row["count"]) );
234  unset($child_set); // free child for next iteration, avoid counting on the same object
235  } // end if child found
236  } // end foreach value
237  }// end foreach row
238  }else{
239  return false;
240  } //end result is array
241 
242  return true;
243  }
244 
245  /*--------------------------------------------------------
246  * ** CHART **
247  * -------------------------------------------------------*/
248  public function chart_make(){
249  $required_classes = array("pieChart","doughnutChart","radarChart","barChart");
250  foreach($required_classes as $c ){
251  if( !class_exists($c) ){
252  throw new exception("Chart classes not loaded. Please include chart.class.php");
253  }
254  }
255  $type = $this->get_config("chartType");
256  $data = $this->chart_format_data();
257  if( $type == "barChart" ){
258  $this->chart = new barchart($data);
259 
260  }else if( $type == "pieChart" ){
261  $this->chart = new piechart($data);
262 
263  }else if( $type == "doughnutChart" ){
264  $this->chart = new doughnutchart($data);
265 
266  }else if( $type == "radarChart" ){
267  $this->chart = new radarchart($data);
268  }else{
269  $this->chart = new piechart($data);
270  }
271  }
272 
277  private function chart_format_data_barChart( ){
278  $data = array( "labels" => array() , "datasets" => array( array() ) );
279  for( $i=0 ; $i<sizeof($this->option_list) ; ++$i ){
280  $data["labels"][] = "Antwort".($i+1);
281  $data["datasets"][0][] = $this->option_list[$i]->get_data("count");
282  }
283  return $data;
284  }
285 
290  private function chart_format_data_pieChart( ){
291  $data = array();
292  foreach( $this->option_list as $option ){
293  $data[] = $option->get_data("count");
294  }
295  return $data;
296  }
297 
303  return $this->chart_format_data_pieChart();
304  }
305 
310  public function chart_format_data_radarChart(){
311  return $this->chart_format_data_barChart();
312  }
313 
314 
315  public function chart_format_data(){
316  $type = $this->get_config("chartType");
317 
318  $data = array();
319  if( $type == "barChart" ){
321  }else if( $type == "pieChart" ){
323  }else if( $type == "doughnutChart" ){
325  }else if( $type == "radarChart" ){
327  }else{
328  $data = $this->chart_format_data_pieChart();
329  }
330 
331  return $data;
332  }
333 
334 
335 
339  private function chart_display_legend_barChart(){
340  echo "<table style='border-collapse:collapse'>";
341  $c=0;
342  echo "<tr><th colspan='2'>Legende (v.l.n.r.)</th></tr>";
343  foreach( $this->option_list as $option ){
344  echo "<tr style='border-bottom:1px solid #D0D0D0'>";
345  $n = $c+1;
346  echo "<td style='border-right:1px solid #949494;padding-right:1em'>Antwort$n</td>";
347  echo "<td style='padding-left:1em'>{$option->dispName} <span style='color:#810000'>({$option->get_data("count")})</span></td>";
348  echo "</tr>";
349  ++$c;
350  }
351  echo "</table>";
352  }
353 
359  }
360 
364  private function chart_display_legend_pieChart(){
365  if( isset($this->chart) ){
366  echo "<table style='border-collapse:collapse'>";
367  $c=0;
368  echo "<tr><th colspan='2'>Legende</th></tr>";
369  foreach( $this->option_list as $option ){
370  echo "<tr style='border-bottom:1px solid #D0D0D0;'>";
371  $color = $this->chart->colors->getRGBA($c,"1");
372  $n = $c+1;
373  echo "<td style='background-color:$color;width:1em;height:1em;border: 2px solid white'> &nbsp; </td>";
374  echo "<td>{$option->dispName} <span style='color:#810000'>({$option->get_data("count")})</span></td>";
375  echo "</tr>";
376  ++$c;
377  }
378  echo "</table>";
379  }
380  }
381 
387  }
388 
389 
390  public function chart_display_legend(){
391  if( isset($this->chart) ){
392  if( $this->chart instanceof barChart ){
394 
395  }else if( $this->chart instanceof radarChart ){
397 
398  }else if( $this->chart instanceof pieChart ){
400 
401  }else if( $this->chart instanceof doughnutChart ){
403  }
404 
405  } // end if chart set
406  }
407 
408 
409  public function chart_display_chart(){
410  if( !isset($this->chart) ){
411  $this->chart_make();
412  }
413  if( isset($this->chart) ){
414  $this->chart->display();
415  }
416  }
417 
418  public function chart_display(){
419  if( !isset($this->chart) ){
420  $this->chart_make();
421  }
422  if( isset($this->chart) ){
423  echo "<h3>{$this->dispName}</h3>";
424  echo "<table><tr>";
425  echo "<td>";
426  $this->chart->display();
427  echo "</td>";
428  echo "<td style='padding-left:30px'>";
429  $this->chart_display_legend();
430  echo "</td>";
431  echo "</tr></table>";
432 
433  }
434  }
435 
436 
437 }
438 // ! no ending newLine
439 ?>