Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
decision_tree_question_type_select_edit_fns.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_decision_tree/decision_tree_question/decision_tree_question_edit_fns.inc';
19 
20 
31 {
32 
33 
38  function __construct()
39  {
40  parent::__construct();
41 
42  }//end constructor
43 
54  function paintOptions(&$asset, &$o, $prefix)
55  {
56  // Check for write access
57  if ($asset->writeAccess('attributes')) {
58  $read_only = FALSE;
59  } else {
60  $read_only = TRUE;
61  }
62 
63  $prefix = str_replace(':','_',$prefix); // handle shadow assets
64  $value = $asset->attr('options');
65 
66  if (!$read_only) {
67  ?>
68  <div id="option-list-<?php echo $prefix; ?>">
69  <?php
70  $order = 0;
71  if (count($value) > 0) {
72  foreach ($value as $k => $v) {
73  $k = html_entity_decode($k, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
74  $v = html_entity_decode($v, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
75  $printup = ($order < 1) ? FALSE : TRUE;
76  $this->_printOptionField($k, $v, $order, $prefix, 20, $printup);
77  $order++;
78  }
79  }
80  for ($i = 0; $i < 2; $i++) {
81  $k = '';
82  $v = '';
83  $printdown = ($i < 1) ? TRUE : FALSE;
84  $this->_printOptionField($k, $v, $order, $prefix, 20, TRUE, $printdown);
85  $order++;
86  }
87  ?>
88  </div>
89  <script type="text/javascript">
90  <?php
91  // Yeesh... this is hackish but this essentially (attempts to)
92  // correct the lack of delete-button class in front-end
93  // asset builder creation
94  if (!SQ_IN_BACKEND && !SQ_IN_LIMBO) {
95  ?>
96  buttons = document.getElementsByTagName('BUTTON');
97  for (i = 0; i < buttons.length; i++) {
98  if (buttons[i].className == 'delete-button') {
99  with (buttons[i].style) {
100  backgroundImage = 'url(<?php echo sq_web_path('lib') ?>/web/images/icons/delete.png)';
101  backgroundPosition = '50% 50%';
102  backgroundRepeat = 'no-repeat';
103  width = '16px';
104  height = '16px';
105  backgroundColor = 'transparent';
106  border = '0px';
107  cursor = 'pointer';
108  }
109  }
110  }
111  <?php
112  }
113  ?>
114  // Some variables
115  var optionItemPrefix = '<?php echo $prefix; ?>';
116 
117  // Functions for option list attribute
118  var doExpandList = new Function('doExpandOptionList(this)');
119  var doDeleteRow = new Function('doDeleteOptionListRow(this); return false;');
120  var doMoveDn = new Function('sListMoveDown(this); return false;');
121  var doMoveUp = new Function('sListMoveUp(this); return false;');
122 
123  function sListMoveDown(obj) {
124  var currentOrder = 0;
125 
126  var list = document.getElementById('option-list-<?php echo $prefix; ?>');
127  var inputs = list.getElementsByTagName('INPUT');
128  for (var i=0 ; i < inputs.length; i++) {
129  if (obj.id == inputs[i].id) {
130  currentOrder = i;
131  break;
132  }
133  }
134 
135  if (currentOrder == inputs.length) return;
136 
137  // If the input is disabled, don't respond
138  if (inputs[currentOrder].disabled) return;
139 
140  // Do the move
141  if (currentOrder > 2) {
142  sListMoveMe(currentOrder, currentOrder-2);
143  }
144  }
145 
146  function sListMoveUp(obj) {
147  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
148  var currentOrder = 0;
149 
150  var inputs = optionList.getElementsByTagName('INPUT');
151  for (var i=0 ; i < inputs.length; i++) {
152  if (obj.id == inputs[i].id) {
153  currentOrder = i;
154  break;
155  }
156  }
157 
158  if (currentOrder == inputs.length) return;
159 
160  // If the input is disabled, don't respond
161  if (inputs[currentOrder].disabled) return;
162 
163  // Do the move
164  if (currentOrder > 0) {
165  sListMoveMe(currentOrder-2, currentOrder);
166  }
167  }
168 
169  function doExpandOptionList(input)
170  {
171  // abort if we are not the last input in the lit
172  var nextInput = input.nextSibling;
173  while (nextInput !== null) {
174  if (nextInput.tagName == 'INPUT') {
175  return;
176  }
177  nextInput = nextInput.nextSibling;
178  }
179 
180  // abort if we and the second-last input are both empty
181  var lastInput = input.previousSibling;
182  var last2Input = lastInput.previousSibling;
183 
184  while (lastInput !== null) {
185  if (lastInput.tagName == 'INPUT') {
186  last2Input = lastInput.previousSibling;
187  while (last2Input !== null) {
188  if (last2Input.tagName == 'INPUT') {
189  if (last2Input.value == '') {
190  return;
191  }
192  break;
193  }
194  last2Input = last2Input.previousSibling;
195  }
196  break;
197  }
198  lastInput = lastInput.previousSibling;
199  }
200 
201  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
202  var inputs = optionList.getElementsByTagName('INPUT');
203 
204  // add move down button to the previous input
205  var moveDownButton = lastInput.previousSibling;
206  while (moveDownButton != null) {
207  moveDownButton = moveDownButton.previousSibling;
208  if (moveDownButton.tagName == 'A' && moveDownButton.name=="movedown") {
209  break;
210  }
211  }
212  var newMoveDownButton = moveDownButton.cloneNode(true);
213  newMoveDownButton.id = optionItemPrefix+'_options['+(inputs.length/2)+']';
214  newMoveDownButton.onclick = doMoveDn;
215 
216 
217  //If safari, we will remove the script for printing move up/down icon, it's causing document.write to overwrite the page in safari
218  var buttonScript = newMoveDownButton.getElementsByTagName("script")[0];
219  var browserAgent = navigator.userAgent.toLowerCase();
220  if ((browserAgent.indexOf("safari") != -1) && buttonScript != null) {
221  newMoveDownButton.removeChild(buttonScript);
222  }
223 
224 
225 
226  var brElements = lastInput.parentNode.getElementsByTagName('BR');
227  input.parentNode.removeChild(brElements[brElements.length-1]);
228  input.parentNode.appendChild(document.createTextNode(' '));
229  input.parentNode.appendChild(newMoveDownButton);
230  input.parentNode.appendChild(document.createElement('BR'));
231 
232  // add the extra field
233  var newInput = input.cloneNode(true);
234  var newKeyInput = lastInput.cloneNode(true);
235  var newId = (inputs.length/2);
236  var newKeyId = (inputs.length/2);
237  newKeyInput.onfocus = doExpandList;
238  newKeyInput.value = '';
239  newKeyInput.id = optionItemPrefix+'_options_keys['+newId+']';
240  input.parentNode.appendChild(newKeyInput);
241  input.parentNode.appendChild(document.createTextNode(' '));
242  newInput.onfocus = doExpandList;
243  newInput.value = '';
244  newInput.id = optionItemPrefix+'_options['+newId+']';
245  input.parentNode.appendChild(newInput);
246  input.parentNode.appendChild(document.createTextNode(' '));
247  var delButton = input.nextSibling;
248  while (delButton.tagName != 'BUTTON') {
249  delButton = delButton.nextSibling;
250  }
251  delButton = delButton.cloneNode(true);
252  delButton.onclick = doDeleteRow;
253  input.parentNode.appendChild(delButton);
254  input.parentNode.appendChild(document.createTextNode(' '));
255 
256  // add the move up button to the new input
257  var moveUpButton = input.nextSibling;
258  while (moveUpButton != null) {
259  if (moveUpButton.tagName == 'A' && moveUpButton.name == 'moveup') {
260  break;
261  }
262  moveUpButton = moveUpButton.nextSibling;
263  }
264  moveUpButton = moveUpButton.cloneNode(true);
265  moveUpButton.id = optionItemPrefix+'_options['+((inputs.length/2)-1)+']';
266  moveUpButton.onclick = doMoveUp;
267 
268  //If safari, we will remove the script for printing move up/down icon, it's causing document.write to overwrite the page in safari
269  var buttonScript = moveUpButton.getElementsByTagName("script")[0];
270  var browserAgent = navigator.userAgent.toLowerCase();
271  if ((browserAgent.indexOf("safari") != -1) && buttonScript != null) {
272  moveUpButton.removeChild(buttonScript);
273  }
274 
275  input.parentNode.appendChild(moveUpButton);
276  input.parentNode.appendChild(document.createTextNode(' '));
277  input.parentNode.appendChild(document.createElement('BR'));
278 
279  }
280 
281  function doDeleteOptionListRow(button)
282  {
283  var input = button.previousSibling;
284  while (input.tagName != 'INPUT') {
285  input = input.previousSibling;
286  }
287 
288  var input2 = input.previousSibling;
289  while (input2.tagName != 'INPUT') {
290  input2 = input2.previousSibling;
291  }
292 
293  if (input.value == '') return;
294 
295  // If the input is disabled, don't respond
296  if (input.disabled) return;
297 
298  // Don't let the option list get down to a single element. Clear the field
299  // instead, but leave it as two elements
300  var inputs = input.parentNode.getElementsByTagName('INPUT');
301  if (inputs.length <= 2) {
302  input.value = '';
303  return;
304  }
305 
306  var moveUpBut = button.nextSibling;
307  while (moveUpBut != null) {
308  if (moveUpBut.tagName == 'A' && moveUpBut.name == 'moveup') {
309  break;
310  }
311  moveUpBut = moveUpBut.nextSibling;
312  }
313  button.parentNode.removeChild(moveUpBut);
314 
315  var moveDownBut = button.nextSibling;
316  while (moveDownBut != null) {
317  if (moveDownBut.tagName == 'A' && moveDownBut.name == 'movedown') {
318  break;
319  }
320  moveDownBut = moveDownBut.nextSibling;
321  }
322  button.parentNode.removeChild(moveDownBut);
323 
324 
325  var brTag = button.nextSibling;
326  while (brTag.tagName != 'BR') {
327  brTag = brTag.nextSibling;
328  }
329  button.parentNode.removeChild(input2);
330  button.parentNode.removeChild(input);
331  button.parentNode.removeChild(brTag);
332  button.parentNode.removeChild(button);
333  }
334 
335  // attach the event handlers
336  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
337  var inputs = optionList.getElementsByTagName('INPUT');
338  for (var j=0; j < inputs.length; j++) {
339  inputs[j].onfocus = doExpandList;
340  }
341  var buttons = optionList.getElementsByTagName('BUTTON');
342  for (var j=0; j < buttons.length; j++) {
343  buttons[j].onclick = doDeleteRow;
344  }
345 
346  lastOrder = <?php echo $order; ?>;
347 
348  function sListMoveMe(first, second) {
349  var l = document.getElementById('option-list-<?php echo $prefix; ?>');
350  var f = l.getElementsByTagName('INPUT');
351  var tempkey = f[first-1].value;
352 
353  f[first-1].value = f[second-1].value;
354  f[second-1].value = tempkey;
355 
356  var temp = f[first].value;
357  f[first].value = f[second].value;
358  f[second].value = temp;
359  }
360 
361  </script>
362  <?php
363  } else {
364  if (count($value) > 0) {
365  echo '<ul><li>'.html_entity_decode(implode('</li><li>', $value), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'</li></ul>';
366  }
367  }
368 
369  }//end paintOptions()
370 
371 
382  function processOptions(&$asset, &$o, $prefix)
383  {
384  $prefix = str_replace(':','_',$prefix); // handle shadow assets
385 
386  // Save the values
387  if (!isset($_REQUEST[$prefix.'_options'])) return;
388  $value = $_REQUEST[$prefix.'_options'];
389  foreach ($value as $i => $v) {
390  $value[$i] = htmlentities($v, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
391  if ($v == '') unset($value[$i]);
392  }
393 
394  // Save the key fields
395  if (!isset($_REQUEST[$prefix.'_options_keys'])) return;
396  $key_value = $_REQUEST[$prefix.'_options_keys'];
397  foreach ($key_value as $j => $k) {
398  $key_value[$j] = htmlentities($k, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
399  if (($k == '') && (isset($value[$j]) && !($value[$j] == ''))) {
400  $key_value[$j] = $value[$j];
401  } else if (($k == '') && (!isset($value[$j]) || ($value[$j] == ''))) {
402  unset($key_value[$j]);
403  }
404  }
405 
406  $key_value_count = count($key_value);
407  $value_count = count($value);
408  if ($key_value_count != $value_count) return;
409  if ($key_value_count > 0) {
410  $values = array_combine($key_value, $value);
411  } else {
412  $values = Array();
413  }
414 
415  $processed_value = $values;
416  if ($asset->writeAccess('attributes')) {
417  $asset->setAttrValue('options', $processed_value);
418  $asset->saveAttributes();
419  }
420 
421  }//end processOptions()
422 
423 
433  function _printMoveDownButton($order, $prefix)
434  {
435  // We don't need to check order here because the check are in place for the buttons
436  $orderdn = $order + 2;
437  ?>
438  <a href="#" style="cursor:pointer;" onclick="sListMoveMe(<?php echo $order; ?>, <?php echo ($orderdn); ?>);return false;" tabindex="99999" name="movedown" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]"><script type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/down_arrow.png' ?>", "16", "16", "Move Down");</script></a>
439  <?php
440 
441  }//end _printMoveDownButton()
442 
443 
453  function _printMoveUpButton($order, $prefix)
454  {
455  // We don't need to check order here because the check are in place for the buttons
456  $orderup = $order - 2;
457  ?>
458  <a href="#" style="cursor:pointer;" onclick="sListMoveMe(<?php echo $order; ?>, <?php echo ($orderup); ?>);return false;" tabindex="99999" name="moveup" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]"><script type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/up_arrow.png' ?>", "16", "16", "Move Up");</script></a>
459  <?php
460 
461  }//end _printMoveUpButton()
462 
463 
478  function _printOptionField($key, $value, $order, $prefix, $size='10', $showup=TRUE, $showdown=TRUE)
479  {
480  // The order is key1=0, value1=1, key2=2, value2=3 etc. This is just the way they are ordered
481  $current_button = ($order * 2) + 1;
482 
483  // Cut the key field down to half the value field
484  $key_size = $size / 2;
485 
486  // Show the field
487  ?>
488  <input type="text" name="<?php echo $prefix; ?>_options_keys[]" value="<?php echo $key; ?>" id="<?php echo $prefix; ?>_options_keys[<?php echo $order; ?>]" size="<?=$key_size; ?>" />
489  <input type="text" name="<?php echo $prefix; ?>_options[]" value="<?php echo $value; ?>" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]" size="<?=$size; ?>" />
490  <button type="button" tabindex="99999" class="delete-button">&nbsp;</button>
491  <?php
492  if ($showup) {
493  $this->_printMoveUpButton($current_button, $prefix);
494  }
495  if ($showdown) {
496  $this->_printMoveDownButton($current_button, $prefix);
497  }
498  ?><br />
499  <?php
500 
501  }//end _printOptionField()
502 
503 
515  function paintDestinations(&$asset, &$o, $prefix)
516  {
517  $write_access = $asset->writeAccess('attributes');
518  $options = $asset->getOptions();
519  $q_dest = $asset->attr('question_destinations');
520 
521  ?>
522  <table class="sq-backend-table">
523  <tr>
524  <td class="sq-backend-table-header">
525  <?php echo translate('answer_key'); ?>
526  </td>
527  <td class="sq-backend-table-header">
528  <?php echo translate('next_question'); ?>
529  </td>
530  <td class="sq-backend-table-header">
531  <?php echo translate('result_bodycopy'); ?>
532  </td>
533  <td class="sq-backend-table-header">
534  <?php echo translate('points_value'); ?>
535  </td>
536  </tr>
537  <?php
538  foreach ($options as $key => $value){
539  if ($key === $asset->attr('empty_key')) continue;
540  ?>
541  <tr>
542  <td class="sq-backend-table-cell">
543  <?php
544  echo $key;
545  ?>
546  </td>
547  <td class="sq-backend-table-cell">
548  <?php
549  $nxt_q = (!empty($q_dest[$key]['next_question'])) ? $q_dest[$key]['next_question'] : '';
550  if ($write_access){
551  $type_codes = Array (
552  'decision_tree_question_type_select' => 'I',
553  'decision_tree_question_type_numeric' => 'I',
554  );
555  asset_finder($prefix.'_next_question_'.$key, $nxt_q, $type_codes);
556  } else {
557  if ($nxt_q) echo get_asset_tag_line($nxt_q, 'details');
558  }
559  ?>
560  </td>
561  <td class="sq-backend-table-cell">
562  <?php
563  $res_body = (!empty($q_dest[$key]['result_bodycopy'])) ? $q_dest[$key]['result_bodycopy'] : '';
564  if ($write_access){
565  $type_codes = Array (
566  'bodycopy' => 'I',
567  );
568  asset_finder($prefix.'_bodycopy_'.$key, $res_body, $type_codes);
569  } else {
570  if ($res_body) echo get_asset_tag_line($res_body, 'details');
571  }
572  ?>
573  </td>
574  <td class="sq-backend-table-cell">
575  <?php
576  $points = (!empty($q_dest[$key]['points_value'])) ? $q_dest[$key]['points_value'] : 0;
577  if ($write_access){
578  text_box($prefix.'_points_value_'.$key, $points);
579  } else {
580  echo $points;
581  }
582  ?>
583  </td>
584  </tr>
585  <?php }
586  ?>
587  </table>
588  <?php
589  return TRUE;
590  }//end paintDestinations()
591 
592 
604  function processDestinations(&$asset, &$o, $prefix)
605  {
606  if (!$asset->writeAccess('attributes')) return FALSE;
607  $options = $asset->getOptions();
608  $q_dest = Array();
609 
610  foreach ($options as $key => $value){
611  if ($key === $asset->attr('empty_key')) continue;
612  $nxt_q = (isset($_POST[$prefix.'_next_question_'.$key])) ? $_POST[$prefix.'_next_question_'.$key]['assetid'] : '';
613  if (!empty($nxt_q)){
614  //check if its a child else create a link under it
615  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($nxt_q, 'decision_tree_question', FALSE, NULL, NULL, TRUE, 1, 1);
616  if (!array_key_exists($asset->id, $parents)){
617  $minor = $GLOBALS['SQ_SYSTEM']->am->getAsset($nxt_q);
618  if(!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $minor, SQ_LINK_TYPE_2)) $nxt_q = '';
619  }
620  }
621  $q_dest[$key]['next_question'] = $nxt_q;
622 
623  $res_body = (isset($_POST[$prefix.'_bodycopy_'.$key])) ? $_POST[$prefix.'_bodycopy_'.$key]['assetid'] : '';
624  $q_dest[$key]['result_bodycopy'] = $res_body;
625 
626  $points = (!empty($_POST[$prefix.'_points_value_'.$key])) ? $_POST[$prefix.'_points_value_'.$key] : 0;
627 
628  if (!is_numeric($points)) {
629  trigger_localised_error('CMS0114', E_USER_NOTICE);
630  $points = 0;
631  }
632  $q_dest[$key]['points_value'] = $points;
633 
634  }
635 
636  $asset->setAttrValue('question_destinations', $q_dest);
637  $asset->saveAttributes();
638 
639  return TRUE;
640  }//end processDestinations()
641 
642 
643 
644 }//end class
645 ?>