Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_select.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question/form_question.inc';
19 require_once SQ_FUDGE_PATH.'/csv/csv.inc';
20 
21 
34 {
35 
36 
44  function Form_Question_Type_Select($assetid=0,$data=Array())
45  {
46  $this->Form_Question($assetid,$data);
47 
48  }//end constructor
49 
50 
59  function getAllowedRules()
60  {
61  return Array('select_limit', 'selection');
62 
63  }//end getAllowedRules()
64 
65 
73  function _getAllowedLinks()
74  {
75  return Array();
76 
77  }//end _getAllowedLinks()
78 
79 
86  function getHtmlField()
87  {
88  $name = 'q'.$this->id;
89 
90  // prepare a valid value for the field id
91  $extras = 'id="'.str_replace(':', '_', $name).'" '.$this->attr('extra');
92 
93  if ($this->attr('tabindex')) {
94  $extras .= ' tabindex="'.$this->attr('tabindex').'"';
95  }
96 
97  ob_start();
98  combo_box($name, $this->getOptions(), $this->attr('multiple'), $this->getValue(), $this->attr('height'), $extras);
99  $html = ob_get_contents();
100  ob_end_clean();
101 
102  return $html;
103 
104  }//end getHtmlField()
105 
106 
113  function getOptions()
114  {
115  $options = $this->attr('options');
116 
117  if (count($options) > 0) {
118  $decoded_options = Array();
119  // remove empty options
120  foreach (array_keys($options) as $k) {
121  if ($options[$k] != '') {
122  $decoded_options[html_entity_decode($k, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET)] = html_entity_decode($options[$k], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
123  }
124  }
125  $options = $decoded_options;
126 
127  // if allow empty is true, add the '-- Leave Empty --' as the first option
128  if ($this->attr('allow_empty')) {
129  if(array_key_exists(html_entity_decode($this->attr('empty_key'), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET), $options)) {
130  trigger_localised_error('CORE0302', E_USER_NOTICE);
131  } else {
132  $empty = Array(html_entity_decode($this->attr('empty_key'), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET) => html_entity_decode($this->attr('empty_text'), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
133  $options = $empty + $options;
134  }
135  }
136  }
137 
138  return $options;
139 
140  }//end getOptions()
141 
142 
151  function getSummary($answer=NULL)
152  {
153  if (is_null($answer)) $answer = $this->value;
154 
155  if (is_array($answer)) {
156  $ans_str = Array();
157  foreach ($answer as $a) {
158  // if the option is '-- Leave Empty --', we should return empty string
159  $answer_by_offset = $this->getAnswerByOffset($a);
160  if ($answer_by_offset != $this->attr('empty_text')) {
161  $ans_str[] = $this->getAnswerByOffset($a);
162  } else {
163  $ans_str[] = '';
164  }
165  }
166  return implode(', ', $ans_str);
167  }
168 
169  // if the option is '-- Leave Empty --', we should return empty string
170  $answer_by_offset = $this->getAnswerByOffset($answer);
171  if ($answer_by_offset != $this->attr('empty_text')) {
172  return $this->getAnswerByOffset($answer);
173  } else {
174  return '';
175  }
176 
177  }//end getSummary()
178 
179 
188  function getXML($answer)
189  {
190  ob_start();
191 
192  echo '<select_q id="'.addslashes($this->id).'" name="'.htmlentities($this->attr('name'), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'">';
193  if (!is_array($answer)) $answer = Array($answer);
194 
195  foreach ($answer as $a) {
196  echo '<option value="'.$a.'">'.htmlentities($this->getAnswerByOffset($a), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'</option>';
197  }
198  echo '</select_q>';
199 
200  $contents = ob_get_contents();
201  ob_end_clean();
202 
203  return $contents;
204 
205  }//end getXML()
206 
207 
216  function isSelected($option)
217  {
218  $options = $this->getValue();
219  if (!is_array($options)) {
220  $options = Array($options);
221  }
222 
223  return in_array($option, $options);
224 
225  }//end isSelected()
226 
227 
236  function isEmptyOption($option)
237  {
238  $option_name = $this->getAnswerByOffset($option);
239  return $option_name == $this->attr('empty_text');
240 
241  }//end isEmptyOption()
242 
243 
255  function getOptionName($option_code)
256  {
257  return $this->getAnswerByOffset($option_code);
258 
259  }//end getOptionName()
260 
261 
273  public function importOptionsFromCSV($filename)
274  {
275  $csv = new CSV($filename);
276  $csv->import();
277 
278  $values = $csv->values;
279  $options = Array();
280  $defaults = Array();
281 
282  if (!empty($values)) {
283  // We will assume that the number of columns in the first row
284  // is representative of the entire list
285  $num_columns = count($values[0]);
286  $index = 0;
287 
288  foreach ($values as $row) {
289  if ($num_columns === 1) {
290  // If we haven't provided a first column, then key and value
291  // are the same
292  $value = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
293  $options[$index] = $value;
294  $index++;
295  } else {
296  $key = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
297  $value = htmlentities($row[1], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
298  $options[$key] = $value;
299 
300  if ($num_columns === 3) {
301  if ($row[2] == 1) {
302  $defaults[] = $key;
303  }
304  }
305  }
306  }
307 
308  $this->setAttrValue('options', $options);
309  if ($num_columns === 3) {
310  if (!$this->attr('multiple')) {
311  if (count($defaults) > 1) {
312  trigger_localised_error('CORE0286', E_USER_NOTICE);
313  }
314  if (count($defaults) > 0) {
315  $defaults = Array($defaults[0]);
316  } else {
317  $defaults = Array();
318  }
319  }
320 
321  $this->setAttrValue('default', $defaults);
322  } else {
323  // What happens if it gets overwritten?
324  $current_default = $this->attr('default');
325  if (!is_array($current_default)) {
326  $current_default = Array($current_default);
327  }
328  foreach ($current_default as $default_key) {
329  // Remove any default options that are not choices in this
330  // particular situation
331  if (!isset($options[$default_key])) {
332  unset($current_default[$default_key]);
333  }
334  }
335  $this->setAttrValue('default', $current_default);
336  }
337  $this->saveAttributes();
338  }
339 
340  }//end importOptionsFromCSV()
341 
342 
343 }//end class
344 ?>