18 require_once SQ_CORE_PACKAGE_PATH.
'/page/page.inc';
44 $this->_ser_attrs = TRUE;
45 parent::__construct($assetid);
62 if (!parent::_createAdditional($link))
return FALSE;
65 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
66 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'folder');
69 'type_formats' =>
'folder',
70 'result_bodycopies' =>
'folder',
73 foreach ($sub_assets as $name => $type) {
75 $copy_link = Array(
'asset' => &$this,
'value' => $name ,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
77 $asset->setAttrValue(
'name', ucwords(str_replace(
'_',
' ', $name)));
78 if (!$asset->create($copy_link))
return FALSE;
80 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
85 $copy_link = Array(
'asset' => &$this,
'value' =>
'page_contents' ,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
86 $asset->setAttrValue(
'name',
'Page Contents');
87 $args = Array(
'content' => $this->_getDefaultBodycopyContent(
'page_contents'));
88 if (!$asset->create($copy_link, $args))
return FALSE;
90 if (!$this->_createTypeFormat(
'decision_tree_question_type_select'))
return FALSE;
91 if (!$this->_createTypeFormat(
'decision_tree_question_type_numeric'))
return FALSE;
107 $page_links = parent::_getAllowedLinks();
108 $page_links[SQ_LINK_TYPE_2][
'decision_tree_question'] = Array(
'card' =>
'M',
'exclusive' => FALSE);
109 $page_links[SQ_LINK_NOTICE][
'decision_tree_question'] = Array(
'card' => 1,
'exclusive' => FALSE);
110 $page_links[SQ_LINK_TYPE_2][
'bodycopy'] = Array(
'card' =>
'1',
'exclusive' => TRUE);
129 $bodycopy_link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'page_contents',
'major', TRUE);
130 if (!$GLOBALS[
'SQ_PURGING_TRASH'] && (!empty($bodycopy_link) && $bodycopy_link[
'linkid'] == $linkid)) {
131 return translate(
'error_bodycopy_link_cannot_delete');
134 return parent::isDeletableLink($linkid);
153 if (isset($_GET[
'q_id']) && isset($_GET[
'a_id']) && isset($_GET[
'pts'])){
155 $GLOBALS[
'SQ_SYSTEM']->paintLogin(translate(
'login'), translate(
'cannot_access_asset', $asset->name));
158 $this->
printNextStep($_GET[
'q_id'], $_GET[
'a_id'], $_GET[
'pts']);
162 parent::printFrontendAsset($asset, $design);
178 $replacements = Array();
180 $page_contents = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'page_contents',
'major', TRUE);
181 if (empty($page_contents))
return;
183 $bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($page_contents[
'minorid']);
186 if ($bodycopy && !$bodycopy->isEmpty()) {
187 $keyword_list = $bodycopy->getKeywords();
189 if (!empty($keyword_list)) {
190 foreach ($keyword_list as $keyword) {
194 if (is_null($replacement)) {
201 if (!is_null($replacement)) {
203 $replacements[$keyword] = $replacement;
208 $bodycopy->setKeywordReplacements($replacements);
209 $bodycopy->printBody();
227 $question = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($question_id);
228 if (is_null($question))
return;
230 if ($question->type() ==
'decision_tree_question_type_select' && $answer === $question->attr(
'empty_key'))
return;
232 $destinations = $question->attr(
'question_destinations');
233 if (empty($destinations))
return;
235 if ($question->type() ==
'decision_tree_question_type_numeric'){
236 $rules = $question->attr(
'rules');
237 foreach ($rules as $rule_index => $rule_details){
238 $valid = $question->evaluate($answer, $rule_details);
240 $answer = $rule_index;
244 if (empty($valid))
return;
247 $answer_dest = $destinations[$answer];
248 $answer_pts = (!empty($answer_dest[
'points_value'])) ? $answer_dest[
'points_value'] : 0;
250 $accumulated_pts = $total_points + $answer_pts;
252 if (!empty($answer_dest[
'next_question'])) {
253 $new_question = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($answer_dest[
'next_question']);
254 $bodycopy = $this->
getBodycopy($new_question->type(),
'type_formats');
255 }
else if (!empty($answer_dest[
'result_bodycopy'])) {
256 $bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($answer_dest[
'result_bodycopy']);
258 $bodycopy_points = $this->
attr(
'bodycopy_points');
259 foreach ($bodycopy_points as $assetid => $points){
260 if ($accumulated_pts >= $points[
'min'] && $accumulated_pts <= $points[
'max']){
261 $bodycopy = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
266 if (!isset($bodycopy)){
268 $bodycopy = $this->
getBodycopy(
'default_result_bodycopy',
'result_bodycopies');
272 $replacements = Array();
274 if ($bodycopy && !$bodycopy->isEmpty()) {
275 $keyword_list = $bodycopy->getKeywords();
277 if (!empty($keyword_list)) {
278 foreach ($keyword_list as $keyword) {
281 if ($keyword ==
'accumulated_points'){
282 $replacements[$keyword] = $accumulated_pts;
286 if (isset($new_question)){
289 if (is_null($replacement)) {
296 if (!is_null($replacement)) {
298 $replacements[$keyword] = $replacement;
303 $bodycopy->setKeywordReplacements($replacements);
304 if (isset($new_question)) {
305 echo
'<input id="q'.$new_question->id.
'_points" name="q'.$new_question->id.
'_points" type="hidden" value="'.$answer_pts.
'" />';
306 echo
'<div id="q'.$new_question->id.
'_wrapper" class="sq_question_wrapper">';
308 echo
'<div class="sq_result_bodycopy">';
311 $bodycopy->printBody();
329 $current_url = $this->
getURL();
330 $default_val = ($question->type() ==
'decision_tree_question_type_select') ? $question->attr(
'empty_key') :
'';
332 $regex_code =
'/(^$)|(^[-+]?\d*\.?\d+$)/';
336 <script
type=
"text/javascript">
338 var accum_points=
new Array();
341 function get_next_step(answer, question)
344 if (document.getElementById(
"q"+question).type ==
'text'){
345 var result = decision_tree_validate_numeric(answer);
346 if (result == null) {
347 alert (
'You must enter a numeric answer');
353 var q_element = document.getElementById(
"q"+question+
"_wrapper");
354 var req_div = q_element.parentNode;
355 curr_id = req_div.id;
356 curr_id = curr_id.substring(1);
357 curr_id = parseInt(curr_id);
361 xmlHttp=
new XMLHttpRequest();
367 xmlHttp=
new ActiveXObject(
"Microsoft.XMLHTTP");
371 alert (
"Your browser does not support XMLHTTP!");
376 var points = calculate_points(question);
377 var url =
"<?php echo $current_url ?>";
379 xmlHttp.onreadystatechange = question_state_changed;
380 url=url+
'?a_id='+answer+
'&q_id='+question+
'&pts='+points;
381 xmlHttp.open(
"GET", url,
true);
385 function question_state_changed()
387 if (xmlHttp.readyState==4 && xmlHttp.status==200)
392 var nextdiv = document.getElementById(
"q"+q_no);
393 var response = xmlHttp.responseText;
394 nextdiv.innerHTML=response;
398 var divIdName =
"q"+q_no;
399 var newdiv = document.createElement(
'div');
400 newdiv.setAttribute(
"id",divIdName);
401 nextdiv.appendChild(newdiv);
405 function calculate_points(question)
410 var points_element = document.getElementById(
"q"+question+
"_points");
412 if (points_element != null) point_val = points_element.value;
413 accum_points[q_no] = parseFloat(point_val);
416 for (var i = 1; i <= q_no; i++) {
417 points = points + accum_points[i];
423 function reset_questions()
425 get_next_step(
"<?php echo $default_val; ?>", <?php echo $question->id; ?>);
426 var first_q = document.getElementById(
"<?php echo "q
".$question->id; ?>");
427 if (first_q.type ==
"radio") {
428 var radio_boxes = document.getElementsByName(first_q.name);
429 for (var e = 0; e < radio_boxes.length; e++) {
430 if (radio_boxes[e].type ==
"radio") {
431 radio_boxes[e].checked =
false;
434 }
else if (first_q.type ==
"select-one") {
435 first_q.selectedIndex =
"<?php echo $default_val; ?>";
436 }
else if (first_q.type ==
"text") {
441 function decision_tree_validate_numeric(answer)
443 answer.replace(/,/,
"");
444 return answer.match(<?php echo $regex_code ?>);
450 $contents = ob_get_contents();
466 private function _createTypeFormat($new_type)
469 $folder = $this->
getFolder(
'type_formats');
470 $types = $GLOBALS[
'SQ_SYSTEM']->am->getTypeDescendants(
'decision_tree_question');
473 if ($new_type !=
'' && !in_array($new_type, $types)) {
474 trigger_localised_error(
'CMS0045', E_USER_WARNING, $new_type);
479 $current_link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE, $new_type);
480 if (empty($current_link)) {
482 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
483 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
484 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
488 'value' => $new_type,
489 'link_type' => SQ_LINK_TYPE_2,
495 $bodycopy->setAttrValue(
'name', $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($new_type,
'name').
' Format');
496 $args = Array(
'content' => $this->_getDefaultBodycopyContent($new_type));
497 if (!$bodycopy->create($copy_link, $args)) {
498 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
499 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
502 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
503 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
505 trigger_localised_error(
'CMS0046', E_USER_WARNING, $new_type);
525 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'folder', TRUE, $type);
526 if (empty($link))
return $null;
528 $folder = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'minorid'], $link[
'minor_type_code']);
529 if (is_null($folder))
return $null;
544 private function _getDefaultBodycopyContent($bodycopy_code)
546 switch ($bodycopy_code) {
547 case 'page_contents':
548 return '%decision_tree_contents% <br /> %reset_button%';
549 case 'decision_tree_question_type_select':
550 return '%question_title% <br /> %question_field%';
551 case 'decision_tree_question_type_numeric':
552 return '%question_title% <br /> %question_field% %question_button%';
573 if (!$name || !$type)
return $null;
574 $am = $GLOBALS[
'SQ_SYSTEM']->am;
577 if (is_null($folder))
return $null;
579 $bodycopy_link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE, $name);
580 if (empty($bodycopy_link))
return $null;
582 $bodycopy = $am->getAsset($bodycopy_link[
'minorid'], $bodycopy_link[
'minor_type_code']);
596 public function getResultBodycopies()
599 $folder = $this->
getFolder(
'result_bodycopies');
600 $bodycopies = (!empty($folder)) ? $GLOBALS[
'SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2,
'bodycopy', TRUE) : Array();
618 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
619 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
621 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'bodycopy');
623 $parent = $this->
getFolder(
'result_bodycopies');
624 if (is_null($parent))
return FALSE;
626 $copy_link = Array(
'asset' => $parent,
'value' => $link_value ,
'link_type' => SQ_LINK_TYPE_2,
'is_dependant' => 1,
'is_exclusive' => 1);
627 $asset->setAttrValue(
'name', $name);
628 if (!$asset->create($copy_link)) {
629 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
630 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
634 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
635 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
655 if (empty($question_asset))
return $replacement;
656 $is_question_keyword = preg_match(
'/^question\_(.*)$/', $keyword, $question_matches);
658 if ($is_question_keyword) {
660 list(, $sub_keyword) = $question_matches;
661 switch ($sub_keyword) {
663 $replacement = $question_asset->getHtmlField();
667 $replacement = $question_asset->attr(
'name');
671 $replacement = $question_asset->attr(
'note');
675 if ($question_asset->type() !=
'decision_tree_question_type_numeric')
break;
676 $replacement = $question_asset->getHtmlButton();
698 $replacements = Array();
700 if ($keyword ==
'reset_button') {
702 reset_button(
'qReset',
'Reset',
'reset_questions()');
703 $replacement = ob_get_contents();
708 if ($keyword !=
'decision_tree_contents')
return $replacement;
710 $first_question = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_NOTICE,
'decision_tree_question', FALSE,
'first_question');
711 if (empty($first_question)) {
712 trigger_localised_error(
'CMS0118', E_USER_NOTICE, $this->
id);
716 $question = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($first_question[
'minorid']);
718 $bodycopy = $this->
getBodycopy($question->type(),
'type_formats');
720 if ($bodycopy && !$bodycopy->isEmpty()) {
721 $keyword_list = $bodycopy->getKeywords();
723 if (!empty($keyword_list)) {
724 foreach ($keyword_list as $keyword) {
726 if ($keyword ==
'accumulated_points'){
727 $replacements[$keyword] = 0;
733 if (is_null($replacement)) {
740 if (!is_null($replacement)) {
742 $replacements[$keyword] = $replacement;
747 $bodycopy->setKeywordReplacements($replacements);
749 echo
'<div id="questions">';
750 echo
'<div id="q1">';
751 echo
'<div id="q'.$first_question[
'minorid'].
'_wrapper" class="sq_question_wrapper">';
752 $bodycopy->printBody();
754 echo
'<div id="q2">';
760 $replacement = ob_get_contents();
785 if (!isset($vars[
'keywords']))
return;
789 $parents = $GLOBALS[
'SQ_SYSTEM']->am->getParents($broadcaster->id,
'bodycopy', TRUE);
790 $page_contents = $GLOBALS[
'SQ_SYSTEM']->am->getLink($this->
id, SQ_LINK_TYPE_2,
'bodycopy', TRUE,
'page_contents',
'major', TRUE);
791 if (empty($page_contents))
return FALSE;
794 if (array_key_exists($page_contents[
'minorid'], $parents)) {
795 $keywords = @$this->_tmp[
'keywords'][
'page_contents'];
796 if (is_null($keywords) || !is_array($keywords)) {
797 $keywords = $this->_getPageContentsBodycopyKeywords();
798 $this->_tmp[
'keywords'][
'page_contents'] = $keywords;
803 $folder = $this->
getFolder(
'type_formats');
804 if (is_null($folder))
return FALSE;
808 $bodycopy_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($folder->id, SQ_LINK_TYPE_2,
'bodycopy');
810 $bodycopy_code = NULL;
811 foreach ($bodycopy_links as $link_info) {
812 if (isset($parents[$link_info[
'minorid']])) {
813 $bodycopy_code = $link_info[
'value'];
818 if (is_null($bodycopy_code))
return;
820 $keywords = @$this->_tmp[
'keywords'][$bodycopy_code];
822 if (is_null($keywords) || !is_array($keywords)) {
826 $keyword_provider_method = str_replace(
'decision_tree_',
'', $bodycopy_code);
827 $keyword_provider_method = str_replace(
'_',
' ', $keyword_provider_method);
828 $keyword_provider_method =
'_get'.str_replace(
' ',
'', ucwords($keyword_provider_method)).
'BodycopyKeywords';
830 if (method_exists($this, $keyword_provider_method)) {
831 $keywords = $this->$keyword_provider_method();
832 $this->_tmp[
'keywords'][$bodycopy_code] = $keywords;
837 $vars[
'keywords'] = array_merge($vars[
'keywords'], $keywords);
848 private function _getQuestionTypeSelectBodycopyKeywords()
851 $keywords[
'question_title'] =
'Question Title';
852 $keywords[
'question_field'] =
'Question Input Field';
853 $keywords[
'question_note'] =
'Question Note';
854 $keywords[
'accumulated_points'] =
'Accumulated Points';
867 private function _getQuestionTypeNumericBodycopyKeywords()
870 $keywords[
'question_title'] =
'Question Title';
871 $keywords[
'question_field'] =
'Question Input Field';
872 $keywords[
'question_note'] =
'Question Note';
873 $keywords[
'question_button'] =
'Question Button';
874 $keywords[
'accumulated_points'] =
'Accumulated Points';
887 private function _getPageContentsBodycopyKeywords()
890 $keywords[
'decision_tree_contents'] =
'Decision Tree Contents';
891 $keywords[
'reset_button'] =
'Reset Button';