Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
decision_tree_question_type_numeric_edit_fns.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/cms/page_templates/page_decision_tree/decision_tree_question/decision_tree_question_edit_fns.inc';
18 
29 {
30 
31 
36  function __construct()
37  {
38  parent::__construct();
39 
40  }//end constructor
41 
42 
54  function paintDestinations(&$asset, &$o, $prefix)
55  {
56  $write_access = $asset->writeAccess('attributes');
57  $rules = $asset->attr('rules');
58  $q_dest = $asset->attr('question_destinations');
59 
60  ?>
61  <table class="sq-backend-table">
62  <tr>
63  <td class="sq-backend-table-header">
64  <?php echo translate('answer_rule'); ?>
65  </td>
66  <td class="sq-backend-table-header">
67  <?php echo translate('next_question'); ?>
68  </td>
69  <td class="sq-backend-table-header">
70  <?php echo translate('result_bodycopy'); ?>
71  </td>
72  <td class="sq-backend-table-header">
73  <?php echo translate('points_value'); ?>
74  </td>
75  </tr>
76  <?php
77  foreach ($rules as $rule => $details){
78  ?>
79  <tr>
80  <td class="sq-backend-table-cell">
81  <?php
82  $asset->printDescription($details);
83  ?>
84  </td>
85  <td class="sq-backend-table-cell">
86  <?php
87  $nxt_q = (!empty($q_dest[$rule]['next_question'])) ? $q_dest[$rule]['next_question'] : '';
88  if ($write_access){
89  $type_codes = Array (
90  'decision_tree_question_type_select' => 'I',
91  'decision_tree_question_type_numeric' => 'I',
92  );
93  asset_finder($prefix.'_next_question_'.$rule, $nxt_q, $type_codes);
94  } else {
95  if ($nxt_q) echo get_asset_tag_line($nxt_q, 'details');
96  }
97  ?>
98  </td>
99  <td class="sq-backend-table-cell">
100  <?php
101  $res_body = (!empty($q_dest[$rule]['result_bodycopy'])) ? $q_dest[$rule]['result_bodycopy'] : '';
102  if ($write_access){
103  $type_codes = Array (
104  'bodycopy' => 'I',
105  );
106  asset_finder($prefix.'_bodycopy_'.$rule, $res_body, $type_codes);
107  } else {
108  if ($res_body) echo get_asset_tag_line($res_body, 'details');
109  }
110  ?>
111  </td>
112  <td class="sq-backend-table-cell">
113  <?php
114  $points = (!empty($q_dest[$rule]['points_value'])) ? $q_dest[$rule]['points_value'] : 0;
115  if ($write_access){
116  text_box($prefix.'_points_value_'.$rule, $points);
117  } else {
118  echo $points;
119  }
120  ?>
121  </td>
122  </tr>
123  <?php }
124  ?>
125  </table>
126  <?php
127  return TRUE;
128  }//end paintDestinations()
129 
130 
142  function processDestinations(&$asset, &$o, $prefix)
143  {
144  if (!$asset->writeAccess('attributes')) return FALSE;
145  $rules = $asset->attr('rules');
146  $q_dest = Array();
147 
148  foreach ($rules as $rule => $details){
149 
150  $nxt_q = (isset($_POST[$prefix.'_next_question_'.$rule])) ? $_POST[$prefix.'_next_question_'.$rule]['assetid'] : '';
151  if (!empty($nxt_q)){
152  //check if its a child else create a link under it
153  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($nxt_q, 'decision_tree_question', FALSE, NULL, NULL, TRUE, 1, 1);
154  if (!array_key_exists($asset->id, $parents)){
155  $minor = $GLOBALS['SQ_SYSTEM']->am->getAsset($nxt_q);
156  if(!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $minor, SQ_LINK_TYPE_2)) $nxt_q = '';
157  }
158  }
159  $q_dest[$rule]['next_question'] = $nxt_q;
160 
161  $res_body = (isset($_POST[$prefix.'_bodycopy_'.$rule])) ? $_POST[$prefix.'_bodycopy_'.$rule]['assetid'] : '';
162  $q_dest[$rule]['result_bodycopy'] = $res_body;
163 
164  $points = (isset($_POST[$prefix.'_points_value_'.$rule])) ? $_POST[$prefix.'_points_value_'.$rule] : 0;
165  if (!is_numeric($points)) {
166  trigger_localised_error('CMS0114', E_USER_NOTICE);
167  $points = 0;
168  }
169  $q_dest[$rule]['points_value'] = $points;
170 
171  }
172 
173  $asset->setAttrValue('question_destinations', $q_dest);
174  $asset->saveAttributes();
175 
176  return TRUE;
177  }//end processDestinations()
178 
189  function paintOptions(&$asset, &$o, $prefix)
190  {
191 
192  $write_access = $asset->writeAccess('attributes');
193 
194  $rules = $asset->attr('rules');
195  $contents = $asset->getOperators();
196 
197  if ($write_access) {
198 
199  ?><table class="sq-backend-table" style="width:auto">
200  <tr>
201  <th><?php echo translate('range_type'); ?></th>
202  <th><?php echo translate('range_values'); ?></th>
203  <th><?php echo translate('delete'); ?></th>
204  </tr><?php
205  foreach ($rules as $key => $item) {
206  ?><tr><?php
207  ?><td><?php
208 
209  combo_box($prefix.'[RULES][default]['.$key.'][logic]', $contents, FALSE, $item['logic']);
210 
211  ?></td><?php
212  ?><td><?php
213 
214  text_box($prefix.'[RULES][default]['.$key.'][value_1]', $item['value_1'], 10);
215  echo '&nbsp';
216  text_box($prefix.'[RULES][default]['.$key.'][value_2]', $item['value_2'], 10);
217 
218  ?></td><?php
219  ?><td><?php check_box($prefix.'[RULES][default]['.$key.'][delete]', 1, FALSE, 'document.getElementById("'.$prefix.'[RULES][default]['.$key.'][logic]").disabled = document.getElementById("'.$prefix.'[RULES][default]['.$key.'][value_1]").disabled = document.getElementById("'.$prefix.'[RULES][default]['.$key.'][value_2]").disabled = this.checked') ?></td><?php
220  ?></tr><?php
221  }
222  ?>
223  <tr>
224  <td colspan="3"><a href="#" id="<?php echo $prefix?>[RULES][add]" onclick="addAnother(this.parentNode.parentNode); return false;">Add another...</a></td>
225  </tr>
226  </table>
227  <script type="text/javascript">
228  nextIndex = <?php echo (int)count($rules); ?>;
229  function addAnother(node) {
230 
231  var options = '<?php foreach ($contents as $value => $desc) {
232  echo '<option value="'.$value.'">'.$desc.'</option>';
233  }
234  ?>';
235  var prefix = '<?php echo $prefix; ?>';
236  var parent = node.parentNode;
237 
238  // create a new row
239  tr = document.createElement('tr');
240 
241  // build the cells
242  td1 = document.createElement('td');
243  td1.innerHTML = '<select name="'+prefix+'[RULES][default][' + nextIndex + '][logic]" class="sq-form-field">'+options+'</select>';
244  td2 = document.createElement('td');
245  var box1 = '<input type="text" name="'+prefix+'[RULES][default][' + nextIndex + '][value_1]" value="" size="10" />&nbsp;';
246  td2.innerHTML = box1+'<input type="text" name="'+prefix+'[RULES][default][' + nextIndex + '][value_2]" value="" size="10" />';
247  td3 = document.createElement('td');
248  td3.innerHTML = '<em>new</em>';
249 
250  // Now put them all together
251  tr.appendChild(td1);
252  tr.appendChild(td2);
253  tr.appendChild(td3);
254  parent.insertBefore(tr, node);
255 
256  // Go to next index
257  nextIndex++;
258  }
259  </script>
260  <?php
261 
262  } else {
263 
264  ?><table class="sq-backend-table" style="width:auto">
265  <tr>
266  <th>Answer Rules</th>
267  </tr><?php
268  foreach ($rules as $key => $item) {
269  ?><tr><?php
270  ?><td><?php $asset->printDescription($item); ?></td><?php
271  ?></tr><?php
272  }
273  ?></table><?php
274  }
275 
276  return TRUE;
277 
278  }//end paintOptions()
279 
290  function processOptions(&$asset, &$o, $prefix)
291  {
292 
293  if (!$asset->writeAccess('attributes')) return FALSE;
294 
295  if (isset($_POST[$prefix]['RULES'])) {
296  // Get what we have been given
297  $rules = $_POST[$prefix]['RULES']['default'];
298 
299  foreach ($rules as $key => $value) {
300  // Get rid of indexes we don't need, as a cleansing measure
301  $allowed_sub_keys = Array('logic', 'value_1', 'value_2', 'delete');
302  foreach ($value as $sub_key) {
303  if (!in_array($sub_key, $allowed_sub_keys)) {
304  unset($rules[$key][$sub_key]);
305  }
306  }
307 
308  // If we are set to delete a row, then delete the lot of it
309  if (array_get_index($value, 'delete', FALSE)) {
310  unset($rules[$key]);
311  }
312  }
313 
314  $processed_rules = Array();
315  foreach ($rules as &$value) {
316  $value['value_1'] = trim($value['value_1']);
317  $value['value_2'] = trim($value['value_2']);
318  if ($value['value_1'] == '' && $value['value_2'] == '') continue;
319  $new_key = strtolower($value['logic'].'_'.$value['value_1'].'_'.$value['value_2']);
320  $processed_rules[$new_key] = $value;
321  }
322  unset($value);
323 
324  $asset->setAttrValue('rules', $processed_rules);
325  $asset->saveAttributes();
326  }
327 
328  return TRUE;
329 
330  }//end processOptions()
331 
332 
333 }//end class
334 ?>