Skip to content

Commit

Permalink
Dev: Factor out getJavascriptForMatching()
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Oct 7, 2016
1 parent fa27644 commit 88fe2ae
Showing 1 changed file with 56 additions and 52 deletions.
108 changes: 56 additions & 52 deletions application/controllers/admin/conditionsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function index($subaction, $iSurveyID=null, $gid=null, $qid=null)
$this->getController()->redirect(array('admin'));
}

// This will redirect after logic is reset
if ($p_subaction == "resetsurveylogic") {
$this->resetSurveyLogic($iSurveyID);
}
Expand Down Expand Up @@ -210,6 +211,7 @@ public function index($subaction, $iSurveyID=null, $gid=null, $qid=null)
'qid' => $qid
);

// Subaction = form submission
$this->applySubaction($p_subaction, $args);

$cquestions = array();
Expand All @@ -232,9 +234,9 @@ public function index($subaction, $iSurveyID=null, $gid=null, $qid=null)
// , and find out which number in that order this question is
// Then, using the same array which is now properly sorted by group then question
// Create an array of all the questions that appear AFTER the current one
$qrows = $this->getQRows($qid);
$questionlist = $this->getQuestionList($qid, $qrows);
$postquestionlist = $this->getPostQuestionList($qid, $qrows);
$questionRows = $this->getQuestionRows($qid);
$questionlist = $this->getQuestionList($qid, $questionRows);
$postquestionlist = $this->getPostQuestionList($qid, $questionRows);

$theserows = $this->getTheseRows($questionlist);
$postrows = $this->getPostRows($postquestionlist);
Expand Down Expand Up @@ -264,55 +266,8 @@ public function index($subaction, $iSurveyID=null, $gid=null, $qid=null)
$questionNavOptions = $this->getQuestionNavOptions($theserows, $postrows, $args);

//Now display the information and forms
//BEGIN: PREPARE JAVASCRIPT TO SHOW MATCHING ANSWERS TO SELECTED QUESTION
$javascriptpre = CHtml::openTag('script', array('type' => 'text/javascript'))
. "<!--\n"
. "\tvar Fieldnames = new Array();\n"
. "\tvar Codes = new Array();\n"
. "\tvar Answers = new Array();\n"
. "\tvar QFieldnames = new Array();\n"
. "\tvar Qcqids = new Array();\n"
. "\tvar Qtypes = new Array();\n";

$jn = 0;
if ( isset($canswers) )
{
foreach($canswers as $can)
{
$an = ls_json_encode(flattenText($can[2]));
$javascriptpre .= "Fieldnames[{$jn}]='{$can[0]}';\n"
. "Codes[{$jn}]='{$can[1]}';\n"
. "Answers[{$jn}]={$an};\n";
$jn++;
}
}

$jn = 0;
if ( isset($cquestions) )
{
foreach ($cquestions as $cqn)
{
$javascriptpre .= "QFieldnames[$jn]='$cqn[3]';\n"
."Qcqids[$jn]='$cqn[1]';\n"
."Qtypes[$jn]='$cqn[2]';\n";
$jn++;
}
}

// record a JS variable to let jQuery know if survey is Anonymous
if ($surveyIsAnonymized)
{
$javascriptpre .= "isAnonymousSurvey = true;";
}
else
{
$javascriptpre .= "isAnonymousSurvey = false;";
}

$javascriptpre .= "//-->\n"
.CHtml::closeTag('script');

//END: PREPARE JAVASCRIPT TO SHOW MATCHING ANSWERS TO SELECTED QUESTION
$javascriptpre = $this->getJavascriptForMatching($canswers, $cquestions, $surveyIsAnonymized);

$aViewUrls = array();

Expand Down Expand Up @@ -1352,7 +1307,7 @@ protected function getSurveyIsAnonymized()
* @param int $qid
* @return array
*/
protected function getQRows($qid)
protected function getQuestionRows($qid)
{
$qresult = Question::model()->with(array(
'groups' => array(
Expand Down Expand Up @@ -2105,4 +2060,53 @@ protected function createNavigatorUrl($gid, $qid)
)
);
}

/**
* Javascript to match question with answer
* @param array $canswers
* @param array $cquestions
* @param boolean $surveyIsAnonymized
* @return string js
*/
protected function getJavascriptForMatching(array $canswers, array $cquestions, $surveyIsAnonymized)
{
$javascriptpre = CHtml::openTag('script', array('type' => 'text/javascript'))
. "<!--\n"
. "\tvar Fieldnames = new Array();\n"
. "\tvar Codes = new Array();\n"
. "\tvar Answers = new Array();\n"
. "\tvar QFieldnames = new Array();\n"
. "\tvar Qcqids = new Array();\n"
. "\tvar Qtypes = new Array();\n";

$jn = 0;
foreach($canswers as $can) {
$an = ls_json_encode(flattenText($can[2]));
$javascriptpre .= "Fieldnames[{$jn}]='{$can[0]}';\n"
. "Codes[{$jn}]='{$can[1]}';\n"
. "Answers[{$jn}]={$an};\n";
$jn++;
}

$jn = 0;
foreach ($cquestions as $cqn) {
$javascriptpre .= "QFieldnames[$jn]='$cqn[3]';\n"
."Qcqids[$jn]='$cqn[1]';\n"
."Qtypes[$jn]='$cqn[2]';\n";
$jn++;
}

// record a JS variable to let jQuery know if survey is Anonymous
if ($surveyIsAnonymized) {
$javascriptpre .= "isAnonymousSurvey = true;";
}
else {
$javascriptpre .= "isAnonymousSurvey = false;";
}

$javascriptpre .= "//-->\n"
.CHtml::closeTag('script');

return $javascriptpre;
}
}

0 comments on commit 88fe2ae

Please sign in to comment.