Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_tickbox_list.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question/form_question.inc';
19 
20 
33 {
34 
35 
43  function Form_Question_Type_Tickbox_List($assetid=0,$data=Array())
44  {
45  $this->Form_Question($assetid,$data);
46 
47  }//end constructor
48 
49 
57  function _getAllowedLinks()
58  {
59  return $links;
60 
61  }//end _getAllowedLinks()
62 
63 
70  function getHtmlField()
71  {
72  $extras = $this->attr('extra');
73 
74  if ($this->attr('tabindex')) {
75  $extras .= ' tabindex="'.$this->attr('tabindex').'"';
76  }
77  return $this->listBox($this->getOptions(), TRUE, $extras);
78 
79  }//end getHtmlField()
80 
81 
92  function listBox($options, $multiple=FALSE, $extras='')
93  {
94  $type = ($multiple) ? 'checkbox' : 'radio';
95  if (!stristr($extras, 'class=')) {
96  $extras .= ' class="sq-form-field"';
97  }
98  $name = 'q'.$this->id;
99  $safe_name = str_replace(':', '_', $name);
100 
101  $html = '<ul>';
102  $defaults = $this->getValue();
103  if (!is_array($defaults)) $defaults = Array($defaults);
104  foreach ($options as $key => $value) {
105  $html .= '<li>';
106 
107  $html .= '<input type="'.$type.'" ';
108  $html .= 'name="'.$name.(($multiple) ? '[]' : '').'" ';
109  $html .= 'id="'.$safe_name.'_'.$key.'" ';
110  $html .= 'value="'.htmlspecialchars($key).'"';
111  if (in_array($key, $defaults)) {
112  $html .= ' checked="checked"';
113  }
114  $html .= ($extras == '') ? '/>' : ' '.$extras.' />';
115 
116  $html .= '&nbsp;<label for="'.$safe_name.'_'.$key.'">'.$value.'</label>';
117 
118  $html .= '</li>';
119  }
120 
121  $html .= '</ul>';
122 
123  return $html;
124 
125  }//end listBox()
126 
127 
134  function getOptions()
135  {
136  $options = @unserialize($this->attr('options'));
137  if (!is_array($options) && empty($options)) {
138  $options = Array();
139  }//end if
140  // remove empty options
141  foreach (array_keys($options) as $k) {
142  if ($options[$k] == '') unset($options[$k]);
143  }
144 
145  return $options;
146 
147  }//end getOptions()
148 
149 
165  function hasValidValue($answer=NULL, $mute_errors=FALSE)
166  {
167  if (is_null($answer)) $answer = $this->getValue();
168 
169  if (!is_array($answer)) {
170  $answer = Array($answer);
171  }
172  $value = $this->getAnswerByOffset($answer);
173 
174  if ($this->attr('is_required')) {
175  $req_rule = Array('rule_code' => 'select_limit', 'operator' => '>=', 'value' => '1');
176  array_push($this->vars['rules']['value'], $req_rule);
177  }
178 
179  $ok = parent::hasValidValue($value, $mute_errors);
180 
181  if ($this->attr('is_required')) {
182  array_pop($this->vars['rules']['value']);
183  }
184 
185  return $ok;
186 
187  }//end hasValidValue()
188 
189 
200  function generateJSCode()
201  {
202  if ($this->attr('is_required')) {
203  $req_rule = Array('rule_code' => 'select_limit', 'operator' => '>=', 'value' => '1');
204  array_push($this->vars['rules']['value'], $req_rule);
205  }
206 
207  $code = parent::generateJSCode();
208 
209  if ($this->attr('is_required')) {
210  array_pop($this->vars['rules']['value']);
211  }
212 
213  return $code;
214 
215  }//end generateJSCode()
216 
217 
226  function getAllowedRules()
227  {
228  return Array('select_limit', 'selection');
229 
230  }//end getAllowedRules()
231 
232 
241  function getSummary($answer=NULL)
242  {
243  if (is_null($answer)) $answer = $this->getValue();
244 
245  if (is_array($answer)) {
246  $ans_str = Array();
247  foreach ($answer as $a) {
248  $ans_str[] = $this->getAnswerByOffset($a);
249  }
250  return implode(', ', $ans_str);
251  }
252  return $this->getAnswerByOffset($answer);
253 
254  }//end getSummary()
255 
256 
265  function isSelected($value)
266  {
267  $values = $this->getValue();
268  if (!is_null($values)) {
269  if (is_array($values)) {
270  return in_array($value, $values);
271  } else {
272  return ($value == $values && !empty($values));
273  }
274  } else {
275  $defaults = $this->attr('default');
276  return (in_array($value, $defaults));
277  }
278 
279  }//end isSelected()
280 
281 
293  function getOptionName($option_code)
294  {
295  return $this->getAnswerByOffset($option_code);
296 
297  }//end getOptionName()
298 
299 
308  function getXML($answer)
309  {
310  ob_start();
311 
312  echo '<select_q id="'.addslashes($this->id).'" name="'.htmlspecialchars($this->attr('name')).'">';
313  if (!is_array($answer)) $answer = Array($answer);
314 
315  foreach ($answer as $a) {
316  echo '<option value="'.$a.'">'.htmlspecialchars($this->getAnswerByOffset($a)).'</option>';
317  }
318  echo '</select_q>';
319 
320  $contents = ob_get_contents();
321  ob_end_clean();
322 
323  return $contents;
324 
325  }//end getXML()
326 
327 
334  function getHtmlLabel()
335  {
336  return $this->attr('name');
337 
338  }//end getHtmlLabel()
339 
340 
341 
342 
349  function populate()
350  {
351  $prefix = 'q'.$this->id;
352  $this->setValue(array_get_index($_POST, $prefix, Array()));
353  return TRUE;
354 
355  }//end populate()
356 
357 }//end class
358 ?>