Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_tickbox_table.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question/form_question.inc';
19 require_once SQ_FUDGE_PATH.'/general/text.inc';
20 
21 
34 {
35 
36 
44  function Form_Question_Type_Tickbox_Table($assetid=0,$data=Array())
45  {
46  $this->Form_Question($assetid,$data);
47 
48  }//end constructor
49 
50 
57  function getHtmlField()
58  {
59  return $this->_getOutput('active');
60 
61  }//end getHtmlField()
62 
63 
70  function getSummary()
71  {
72  return $this->_getOutput('plaintext');
73 
74  }//end getSummary()
75 
76 
85  function _getOutput($type='plaintext')
86  {
87  $prefix = $this->getSafePrefix();
88 
89  $question_contents = $this->attr('question_contents');
90  $datapath = sq_web_path('data').'/asset_types/form_question_type_tickbox_table/files';
91 
92  $form = $this->getMySection();
93  $submission = $this->getValue();
94 
95  $to_output = '';
96 
97  switch ($type) {
98  // plaintext part for e-mail sending and submission confirmation
99  case 'plaintext':
100  if (empty($question_contents )) return '';
101  $header = array_shift($question_contents);
102  $title = array_shift($header);
103  $output = Array();
104 
105  if (!empty($question_contents)) {
106  foreach ($question_contents as $question_id=>$question) {
107  $question_title = array_shift($question);
108  $this_question = Array();
109  foreach ($question as $option_id=>$option) {
110  if ($submission && isset($submission[$question_id][$option_id])) {
111  $this_question[] = $header[$option_id].' ('.$option.')';
112  }
113  }
114  if (!empty($this_question)) {
115  $output[] = $question_title.': '.implode(', ', $this_question);
116  }
117  }
118  }
119  return implode("; \n", $output);
120  break;
121 
122  // editor part for dynamic JavaScript page
123  case 'editor':
124  $header = array_shift($question_contents);
125  $to_output = '<style type="text/css">@import url("'.$datapath.'/style.css");</style>'.
126  '<script type="text/javascript" src="'.$datapath.'/InputTable.js">//blank</script>'.
127  '<table border="0" cellspacing="0" cellpadding="0" id="'.$prefix.'" class="tbl"></table>'.
128  '<script type="text/javascript">'.
129  "var Quests = Array(\n";
130  $output = Array();
131  if (!empty($header)) {
132  array_walk($header, 'escape_double_quotes');
133  $output[] = ' Array("'.implode('", "', $header).'")';
134  foreach ($question_contents as $question_id=>$question) {
135  array_walk($question, 'escape_double_quotes');
136  $output[] = ' Array("'.implode('", "', $question).'")';
137  }
138  $to_output .= implode(",\n", $output);
139  }
140  $to_output .= ");\n";
141  $to_output .= ' var tb = new InputTable("'.$prefix.'", "tb");'.
142  ' tb.datapath = "'.$datapath.'";'.
143  ' tb.setup(Quests);'.
144  '</script>';
145  return $to_output;
146  break;
147 
148  // active and passive part for front-end and unlocked back-end view
149  default:
150  $to_output = '<table border="0" cellpadding="0" cellspacing="0" class="sq-backend-table" id="'.$prefix.'">'.
151  ' <tr>'.
152  ' <td class="sq-backend-table-header">';
153  if (empty($question_contents) || empty($question_contents[0])) {
154  $to_output .= '<div align="center">'.translate('core_form_tickbox_table_not_properly_defined');
155  if ($type == 'passive') {
156  $to_output .= ' '.translate('core_form_tickbox_table_incorrectly_displayed');
157  }
158  $to_output .= '</div>';
159  } else {
160  $extras = $this->attr('extra');
161 
162  if ($this->attr('tabindex')) {
163  $extras .= ' tabindex="'.$this->attr('tabindex').'"';
164  }
165 
166  $header = array_shift($question_contents);
167  $to_output .= array_shift($header).'</td>';
168  foreach ($header as $key=>$title) {
169  $to_output .= '<td class="sq-backend-table-header">'.$title.'</td>';
170  }
171  $to_output .= "</tr>\n";
172  $j = 0;
173  foreach ($question_contents as $question_id=>$question) {
174  $j++;
175  $to_output .= '<tr';
176  if ($j % 2 == 0) $to_output .= ' class="even"';
177  $to_output .= '>';
178 
179  $to_output .= '<td class="sq-backend-table-header">'.array_shift($question).'</td>';
180  foreach ($question as $option_id=>$option) {
181  $to_output .= '<td class="sq-backend-table-cell">';
182  if (empty($option)) {
183  $to_output .= '&mdash;';
184  } else if ($type != 'passive') {
185  ob_start();
186  check_box($prefix.'['.$question_id.']['.$option_id.']', '1', $submission && isset($submission[$question_id][$option_id]), NULL, $extras);
187  $to_output .= ob_get_contents();
188  ob_end_clean();
189  ob_start();
190  label($option, $prefix.'['.$question_id.']['.$option_id.']');
191  $label = ob_get_contents();
192  ob_end_clean();
193  $to_output .= ' <span onclick="this.previousSibling.previousSibling.checked = !this.previousSibling.previousSibling.checked">'.$label.'</span>';
194  } else {
195  $to_output .= $option;
196  }
197  $to_output .= '</td>';
198  }
199  $to_output .= '</tr>';
200  }
201  }//end else
202  $to_output .= '</table>';
203 
204  }//end switch
205  return $to_output;
206 
207  }//end _getOutput()
208 
209 
225  function hasValidValue($answer=NULL, $mute_errors=FALSE)
226  {
227  if (is_null($answer)) $answer = $this->getValue();
228 
229  if ($this->attr('is_required')) {
230  $req_rule = Array('rule_code' => 'select_limit', 'operator' => '>=', 'value' => '1');
231  array_push($this->vars['rules']['value'], $req_rule);
232  }
233 
234  $ok = parent::hasValidValue($answer, $mute_errors);
235 
236  if ($this->attr('is_required')) {
237  array_pop($this->vars['rules']['value']);
238  }
239 
240  return $ok;
241 
242  }//end hasValidValue()
243 
244 
251  function getXMLstring()
252  {
253  $prefix = $this->getSafePrefix();
254 
255  $question_contents = $this->attr('question_contents');
256  $datapath = sq_web_path('data').'/asset_types/form_question_type_tickbox_table/files';
257  $submission = array_get_index($this->_tmp,'submission', FALSE);
258  $to_output = '<question>';
259  $to_output .= '<prefix><![CDATA['.$prefix.']]></prefix>';
260  $to_output .= '<datapath><![CDATA['.$datapath.']]></datapath>';
261 
262  if (empty($question_contents )) {
263  $to_output .= '</question>';
264  } else {
265  $header = array_shift($question_contents);
266  $title = array_shift($header);
267  $to_output .= '<title><![CDATA['.$title.']]></title>';
268  $to_output .= '<optiontitles>';
269  foreach ($header as $key=>$item) {
270  $to_output .= '<title id="'.$key.'"><![CDATA['.$item.']]></title>';
271  }
272  $to_output .= '</optiontitles>';
273 
274  foreach ($question_contents as $question_id=>$question) {
275  $question_title = array_shift($question);
276  $to_output .= '<subquestion id="'.$question_id.'">';
277  $to_output .= '<title><![CDATA['.$question_title.']]></title>';
278 
279  $to_output .= '<options>';
280  foreach ($question as $option_id=>$option) {
281  $checked = 'checked="0"';
282  if ($submission && isset($submission[$question_id][$option_id])) {
283  $checked = 'checked="1"';
284  }
285  $to_output .= '<option id="'.$option_id.'" '.$checked.'><![CDATA['.$option.']]></option>';
286  }
287  $to_output .= '</options>';
288  $to_output .= '</subquestion>';
289  }
290  $to_output .= '</question>';
291  }
292 
293  return $to_output;
294 
295  }//end getXMLstring()
296 
297 
304  function getSafePrefix()
305  {
306  return $prefix = 'q'.str_replace(':', '_', $this->getPrefix());
307 
308  }//end getSafePrefix()
309 
310 
317  function isSelection()
318  {
319  return TRUE;
320 
321  }//end isSelection()
322 
323 
332  function isSelected($option_code)
333  {
334  $value = $this->getValue();
335  if (empty($value)) return FALSE;
336 
337  // decipher the option id
338  list($line,$option) = explode('_', $option_code);
339 
340  // check if the option was submitted
341  return !empty($value[$line][$option]);
342 
343  }//end isSelected()
344 
345 
357  function getOptionName($option_code)
358  {
359  $question_contents = $this->attr('question_contents');
360 
361  // decipher the option id
362  list($line_id,$option_id) = explode('_', $option_code);
363 
364  $header = array_shift($question_contents);
365  // first column of first row is of no importance
366  array_shift($header);
367 
368  $line = $question_contents[$line_id];
369  $title = array_shift($line);
370 
371  $option = $header[$option_id];
372 
373  $name = $title.': '.$option.' - ('.$line[$option_id].')';
374 
375  return $name;
376 
377  }//end getOptionName()
378 
379 
386  function getOptions()
387  {
388  $options = Array();
389  $lines = $this->attr('question_contents');
390  $header = array_shift($lines);
391  // remove the first option - it does not carry any value
392  array_shift($header);
393 
394  foreach ($lines as $id => $line) {
395  $title = array_shift($line);
396  foreach ($header as $o_id => $o_name) {
397  $option_id = $id.'_'.$o_id;
398  $options[$option_id] = $title.': '.$o_name.' - ('.$line[$o_id].')';
399  }
400  }
401 
402  return $options;
403 
404  }//end getOptions()
405 
406 
413  function populate()
414  {
415  $prefix = $this->getSafePrefix();
416  $this->setValue(array_get_index($_POST, $prefix, Array()));
417  return TRUE;
418 
419  }//end populate()
420 
421 
422 }//end class
423 ?>