Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_decision_tree_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page_edit_fns.inc';
19 
20 
30 {
31 
32 
39  function __construct()
40  {
41  parent::__construct();
42  $this->static_screens['details']['force_unlock'] = FALSE;
43 
44  }//end constructor
45 
46 
59  function paintFirstQuestion(&$asset, &$o, $prefix)
60  {
61  $write_access = $asset->writeAccess('links');
62  $first_question = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, 'decision_tree_question', FALSE, 'first_question');
63 
64  if ($write_access) {
65  $assetid = (!empty($first_question['minorid'])) ? $first_question['minorid'] : 0;
66  asset_finder($prefix.'_first_question', $assetid, $type_codes = Array ('decision_tree_question' => 'D'));
67  if (!empty($assetid)) echo '<p>'.get_asset_tag_line($first_question['minorid'], 'details').'</p>';
68  } else {
69  if (!empty($first_question['minorid'])) {
70  echo get_asset_tag_line($first_question['minorid'], 'details');
71  } else {
72  echo "<i>".translate('first_question_not_selected')."</i>";
73  }
74  }
75 
76  return TRUE;
77 
78  }//end paintFirstQuestion()
79 
80 
91  function processFirstQuestion(&$asset, &$o, $prefix)
92  {
93  if (!$asset->writeAccess('links')) return FALSE;
94 
95  $asset_finder = array_get_index($_POST, $prefix.'_first_question');
96  $new_first_id = array_get_index($asset_finder, 'assetid');
97  $existing_first = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, 'decision_tree_question', FALSE, 'first_question');
98 
99  //make sure the selected question is a child of this asset.
100  if (!empty($new_first_id)){
101  $child_questions = $GLOBALS['SQ_SYSTEM']->am->getChildren($asset->id, 'decision_tree_question', FALSE);
102  if (!in_array($new_first_id, array_keys($child_questions))){
103  trigger_error('The first question must be a child of this page', E_USER_NOTICE);
104  return FALSE;
105  }
106  }
107 
108  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
109  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
110 
111  $ok = TRUE;
112  //delete removed/changed selection
113  if (!empty($existing_first) && (empty($new_first_id) || ($new_first_id != $existing_first['minorid']))){
114  if (!$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($existing_first['linkid'])) {
115  $ok = FALSE;
116  }
117  }
118 
119  //create new selection
120  if (!empty($new_first_id) && (empty($existing_first) || ($new_first_id != $existing_first['minorid']))){
121  $first_question = $GLOBALS['SQ_SYSTEM']->am->getAsset($new_first_id);
122  if (!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $first_question, SQ_LINK_NOTICE, 'first_question')){
123  $ok = FALSE;
124  }
125  }
126 
127  if ($ok){
128  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
129  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
130  } else {
131  trigger_error('Failed to Process First Question Selection', E_USER_WARNING);
132  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
133  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
134  }
135 
136  return $ok;
137 
138  }//end processFirstQuestion()
139 
140 
153  function paintAddQuestions(&$asset, &$o, $prefix)
154  {
155  if (!$asset->writeAccess('links')) return FALSE;
156 
157  $am = $GLOBALS['SQ_SYSTEM']->am;
158  $question_types = $am->getTypeDescendants('decision_tree_question');
159  $questions = Array('' => '-- Choose Type --');
160 
161  foreach ($question_types as $question) {
162  $q = str_replace('decision_tree_question_type', '', $question);
163  $q = str_replace('_', ' ', $q);
164  $q = trim(ucwords($q));
165  $questions[$question] = $q;
166  }
167 
168  combo_box($prefix.'question_type', $questions, FALSE, '');
169  echo '&nbsp;';
170 
171  text_box($prefix.'question_name', '', '25');
172 
173  return TRUE;
174 
175  }//end paintAddQuestions()
176 
177 
188  function processAddQuestions(&$asset, &$o, $prefix)
189  {
190  if (!$asset->writeAccess('links')) return FALSE;
191 
192  $type = (isset($_POST[$prefix.'question_type'])) ? $_POST[$prefix.'question_type'] : '';
193  $name = (isset($_POST[$prefix.'question_name'])) ? $_POST[$prefix.'question_name'] : '';
194 
195  if ($type && $name) {
196  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
197  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
198 
199  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type);
200  $type = ucwords($type);
201  $new_asset = new $type;
202  $new_asset->setAttrValue('name', $name);
203  // Link the asset to the parent asset
204  $link = Array(
205  'asset' => $asset,
206  'link_type' => SQ_LINK_TYPE_2,
207  'value' => '',
208  'sort_order' => NULL,
209  'is_dependant' => TRUE,
210  'is_exclusive' => FALSE,
211  );
212 
213  if(!$new_asset->create($link)) {
214  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
215  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
216  return FALSE;
217  }
218 
219  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
220  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
221  }//end if
222 
223  return TRUE;
224 
225  }//end processAddQuestions()
226 
227 
239  function paintResultBodycopies(&$asset, &$o, $prefix)
240  {
241 
242  $admin_access = $asset->writeAccess('attributes');
243  $bodycopy_points = $asset->attr('bodycopy_points');
244  $bodycopies = $asset->getResultBodycopies();
245 
246  ?>
247  <table class="sq-backend-table">
248  <tr>
249  <td class="sq-backend-table-header">
250  <?php echo translate('result_bodycopy'); ?>
251  </td>
252  <td class="sq-backend-table-header">
253  <?php echo translate('points_range_min_max_incl'); ?>
254  </td>
255  </tr>
256  <?php
257 
258  foreach ($bodycopies as $b_link) {
259  if ($b_link['value'] == 'default_result_bodycopy') continue;
260  $assetid = $b_link['minorid'];
261  ?>
262  <tr>
263  <td class="sq-backend-table-cell">
264  <?php echo get_asset_tag_line($assetid, 'details'); ?>
265  </td>
266  <td class="sq-backend-table-cell">
267  <?php
268  $min = (!empty($bodycopy_points[$assetid]['min'])) ? $bodycopy_points[$assetid]['min'] : 0;
269  $max = (!empty($bodycopy_points[$assetid]['max'])) ? $bodycopy_points[$assetid]['max'] : 0;
270  if ($admin_access) {
271  text_box($prefix.'_bodycopy_points_min_'.$assetid, $min);
272  echo "&nbsp;";
273  text_box($prefix.'_bodycopy_points_max_'.$assetid, $max);
274  } else {
275  echo $min.' to '.$max;
276  }
277  ?>
278  </td>
279  </tr>
280  <?php
281  }//end foreach data
282  ?>
283  </table>
284  <?php
285  return TRUE;
286 
287  }//end paintResultBodycopies()
288 
289 
300  function processResultBodycopies(&$asset, &$o, $prefix)
301  {
302 
303  if (!$asset->writeAccess('attributes')) return FALSE;
304 
305  $bodycopy_points = Array();
306  $bodycopies = $asset->getResultBodycopies();
307 
308  foreach ($bodycopies as $b_link) {
309  if ($b_link['value'] == 'default_result_bodycopy') continue;
310  $assetid = $b_link['minorid'];
311 
312  $max = (isset($_POST[$prefix.'_bodycopy_points_max_'.$assetid])) ? $_POST[$prefix.'_bodycopy_points_max_'.$assetid] : 0;
313  $min = (isset($_POST[$prefix.'_bodycopy_points_min_'.$assetid])) ? $_POST[$prefix.'_bodycopy_points_min_'.$assetid] : 0;
314 
315  if (!is_numeric($max) || !is_numeric($min)) {
316  trigger_localised_error('CMS0115', E_USER_WARNING);
317  return FALSE;
318  }
319 
320  if ($min > $max){
321  trigger_localised_error('CMS0116', E_USER_WARNING);
322  return FALSE;
323  }
324 
325  $bodycopy_points[$assetid]['max'] = $max;
326  $bodycopy_points[$assetid]['min'] = $min;
327  }
328 
329  $asset->setAttrValue('bodycopy_points', $bodycopy_points);
330  $asset->saveAttributes();
331 
332  return TRUE;
333 
334  }//end processResultBodycopies()
335 
336 
347  function paintAddResultBodycopies(&$asset, &$o, $prefix)
348  {
349  if (!$asset->writeAccess('links')) return FALSE;
350 
351  $num = Array();
352  for ($i = 0; $i <= 10; $i++) {
353  $num[$i] = $i;
354  }
355 
356  combo_box($prefix.'num_bodycopies', $num, FALSE, '');
357 
358  return TRUE;
359 
360  }//end paintAddResultBodycopies()
361 
362 
373  function processAddResultBodycopies(&$asset, &$o, $prefix)
374  {
375  if (!$asset->writeAccess('links')) return FALSE;
376 
377  $to_create = (isset($_POST[$prefix.'num_bodycopies'])) ? $_POST[$prefix.'num_bodycopies'] : 0;
378  $bodycopies = $asset->getResultBodycopies();
379  $no_bodycopies = count($bodycopies) - 1;
380  for ($i = 0; $i < $to_create; $i++){
381  $no_bodycopies++;
382  if (!$asset->createResultBodycopy('Result Bodycopy '.$no_bodycopies, '')){
383  trigger_localised_error('CMS0117', E_USER_WARNING);
384  return FALSE;
385  }
386  }
387 
388  return TRUE;
389  }//end processAddResultBodycopies()
390 
391 
392 
393 }//end class
394 
395 ?>