Skip to content

Commit

Permalink
New feature #09500: Add ANSWER, QUESTIONHELP and other to beforeQuest…
Browse files Browse the repository at this point in the history
…ionRender plugin event

Dev: Move whole QUestion system to specific function in SurveyRuntimeHelper::getQuestionReplacement
Dev: Actually use array from qanda
Dev: static for quick call by templatereplace : set to non static ?
  • Loading branch information
Shnoulle committed Mar 5, 2015
1 parent 8667e61 commit 1d9b183
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 256 deletions.
56 changes: 19 additions & 37 deletions application/controllers/admin/templates.php
Expand Up @@ -1012,47 +1012,29 @@ protected function _initialise($templatename, $screenname, $editfile, $showsumma
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/startgroup.pstpl", $aData));
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/groupdescription.pstpl", $aData));

$question = array(
'all' => gT("How many roads must a man walk down?"),// Still in use ?
'text' => gT("How many roads must a man walk down?"),
'code' => '1a',
'help' => 'helpful text',
'mandatory' => gT("*"),
'man_class' => ' mandatory',
'man_message' => '',
'valid_message' => '',
'file_valid_message' => '',
'essentials' => 'id="question1"',
'class' => 'list-radio',
'input_error_class' => '',
'number' => '1',
'type' => 'L'
$aReplacements = array(
'QUESTION_TEXT' => gT("How many roads must a man walk down?"),
'QUESTION_CODE' => '1a',
'QUESTIONHELP' => 'helpful text',
'QUESTION_MANDATORY' => gT("*"),
'QUESTION_MAN_CLASS' => ' mandatory',
'QUESTION_ESSENTIALS' => 'id="question1"',
'QUESTION_CLASS' => 'list-radio',
'QUESTION_NUMBER' => '1',
);
$aData['question'] = $question;

$answer = $this->getController()->render('/admin/templates/templateeditor_question_answer_view', array(), true);
$aData['answer'] = $answer;
$aReplacements['ANSWER'] = $this->getController()->render('/admin/templates/templateeditor_question_answer_view', array(), true);
$aData['aReplacements'] = $aReplacements;
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/question.pstpl", $aData));

$answer = $this->getController()->render('/admin/templates/templateeditor_question_answer_view', array('alt' => true), true);
$aData['answer'] = $answer;
$question = array(
'all' => gT("Please explain something in detail:"),// Still in use ?
'text' => gT('Please explain something in detail:'),
'code' => '2a',
'help' => '',
'mandatory' => '',
'man_message' => '',
'valid_message' => '',
'file_valid_message' => '',
'essentials' => 'id="question2"',
'class' => 'text-long',
'man_class' => 'mandatory',
'input_error_class' => '',
'number' => '2',
'type' => 'T'
$aReplacements = array(
'QUESTION_TEXT' => gT('Please explain something in detail:'),
'QUESTION_CODE' => '2a',
'QUESTION_ESSENTIALS' => 'id="question2"',
'QUESTION_CLASS' => 'text-long',
'QUESTION_NUMBER' => '2',
);
$aData['question'] = $question;
$aReplacements['ANSWER'] = $this->getController()->render('/admin/templates/templateeditor_question_answer_view', array('alt' => true), true);
$aData['aReplacements'] = $aReplacements;
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/question.pstpl", $aData));
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/endgroup.pstpl", $aData));
$myoutput = array_merge($myoutput, doreplacement(getTemplatePath($templatename) . "/navigator.pstpl", $aData));
Expand Down
225 changes: 191 additions & 34 deletions application/helpers/SurveyRuntimeHelper.php
Expand Up @@ -1055,67 +1055,42 @@ function run($surveyid,$args) {
$lastgroup = $lastgrouparray[0] . "X" . $lastgrouparray[1]; // id of the last group, derived from question id
$lastanswer = $qa[7];

$q_class = getQuestionClass($qinfo['info']['type']);

$man_class = '';
if ($qinfo['info']['mandatory'] == 'Y')
{
$man_class .= ' mandatory';
}

if ($qinfo['anyUnanswered'] && $_SESSION[$LEMsessid]['maxstep'] != $_SESSION[$LEMsessid]['step'])
{
$man_class .= ' missing';
}

$n_q_display = '';
if ($qinfo['hidden'] && $qinfo['info']['type'] != '*')
{
continue; // skip this one
}

if ((!$qinfo['relevant']) || ($qinfo['hidden'] && $qinfo['info']['type'] == '*'))
{
$n_q_display = ' style="display: none;"';
}

$aReplacement=array();
$question = $qa[0];
//===================================================================
// The following four variables offer the templating system the
// capacity to fully control the HTML output for questions making the
// above echo redundant if desired.
$question['essentials'] = 'id="question' . $qa[4] . '"' . $n_q_display;
$question['class'] = $q_class;
$question['man_class'] = $man_class;
$question['code'] = $qa[5];
$question['sgq'] = $qa[7];
$question['aid'] = !empty($qinfo['info']['aid']) ? $qinfo['info']['aid'] : 0;
$question['sqid'] = !empty($qinfo['info']['sqid']) ? $qinfo['info']['sqid'] : 0;
$question['type']=$qinfo['info']['type'];
//===================================================================
$answer = $qa[1];
$help = $qinfo['info']['help']; // $qa[2];

$redata = compact(array_keys(get_defined_vars()));

$question_template = file_get_contents($sTemplatePath.'question.pstpl');
// Fix old template : can we remove it ? Old template are surely already broken by another issue
if (preg_match('/\{QUESTION_ESSENTIALS\}/', $question_template) === false || preg_match('/\{QUESTION_CLASS\}/', $question_template) === false)
{
// if {QUESTION_ESSENTIALS} is present in the template but not {QUESTION_CLASS} remove it because you don't want id="" and display="" duplicated.
$question_template = str_replace('{QUESTION_ESSENTIALS}', '', $question_template);
$question_template = str_replace('{QUESTION_CLASS}', '', $question_template);
echo '
<!-- NEW QUESTION -->
<div id="question' . $qa[4] . '" class="' . $q_class . $man_class . '"' . $n_q_display . '>';
echo templatereplace($question_template, array(), $redata, false, false, $qa[4]);
echo '</div>';
}
else
{
// TMSW - eventually refactor so that only substitutes the QUESTION_** fields - doesn't need full power of template replace
// TMSW - also, want to return a string, and call templatereplace once on that result string once all done.
echo templatereplace($question_template, array(), $redata, false, false, $qa[4]);
$question_template ="<div {QUESTION_ESSENTIALS} class='{QUESTION_CLASS} {QUESTION_MAN_CLASS} {QUESTION_INPUT_ERROR_CLASS}'"
. $question_template
. "</div>";
}
$redata = compact(array_keys(get_defined_vars()));
$aQuestionReplacement=$this->getQuestionReplacement($qa);
echo templatereplace($question_template, $aQuestionReplacement, $redata, false, false, $qa[4]);

}
if ($surveyMode == 'group') {
echo "<input type='hidden' name='lastgroup' value='$lastgroup' id='lastgroup' />\n"; // for counting the time spent on each group
Expand Down Expand Up @@ -1208,4 +1183,186 @@ public function setJavascriptVar($iSurveyId)
}
// Maybe remove one from index and allow empty $surveyid here.
}

/**
* Construction of replacement array, actually doing it with redata
*
* @param $aQuestionQanda : array from qanda helper
* @return aray of replacement for question.psptl
**/
public static function getQuestionReplacement($aQuestionQanda)
{

// Get the default replacement and set empty value by default
$aReplacement=array(
"QID"=>"",
//"GID"=>"", // Attention : set in replacement helper too (by gid).
"SGQ"=>"",
"AID"=>"",
"QUESTION_CODE"=>"",
"QUESTION_NUMBER"=>"",
"QUESTION"=>"",
"QUESTION_TEXT"=>"",
"QUESTIONHELP"=>"", // User help
"QUESTIONHELPPLAINTEXT"=>"",
"QUESTION_CLASS"=>"",
"QUESTION_MAN_CLASS"=>"",
"QUESTION_INPUT_ERROR_CLASS"=>"",
"ANSWER"=>"",
"QUESTION_HELP"=>"", // Core help
"QUESTION_VALID_MESSAGE"=>"",
"QUESTION_FILE_VALID_MESSAGE"=>"",
"QUESTION_MAN_MESSAGE"=>"",
"QUESTION_MANDATORY"=>"",
"QUESTION_ESSENTIALS"=>"",
);
if(!is_array($aQuestionQanda) || empty($aQuestionQanda[0]))
{
return $aReplacement;
}
$iQid=$aQuestionQanda[4];
$lemQuestionInfo = LimeExpressionManager::GetQuestionStatus($iQid);
$iSurveyId=Yii::app()->getConfig('surveyID');// Or : by SGQA of question ? by Question::model($iQid)->sid;
$oSurveyId=Survey::model()->findByPk($iSurveyId);
$sType=$lemQuestionInfo['info']['type'];

// Core value : not replaced
$aReplacement['QID']=$iQid;
$aReplacement['GID']=$aQuestionQanda[6];// Not sure for aleatory
$aReplacement['SGQ']=$aQuestionQanda[7];
$aReplacement['AID']=isset($aQuestionQanda[0]['aid']) ? $aQuestionQanda[0]['aid'] : "" ;
$aReplacement['QUESTION_CODE']=$aReplacement['QUESTION_NUMBER']="";
$sCode=$aQuestionQanda[5];
$iNumber=$aQuestionQanda[0]['number'];
switch (Yii::app()->getConfig('showqnumcode'))
{
case 'both':
$aReplacement['QUESTION_CODE']=$sCode;
$aReplacement['QUESTION_NUMBER']=$iNumber;
break;
case 'number':
$aReplacement['QUESTION_NUMBER']=$iNumber;
break;
case 'number':
$aReplacement['QUESTION_CODE']=$sCode;
break;
case 'choose':
default:
switch($oSurveyId->showqnumcode)
{
case 'B': // Both
$aReplacement['QUESTION_CODE']=$sCode;
$aReplacement['QUESTION_NUMBER']=$iNumber;
break;
case 'N':
$aReplacement['QUESTION_NUMBER']=$iNumber;
break;
case 'C':
$aReplacement['QUESTION_CODE']=$sCode;
break;
case 'X':
default:
break;
}
break;
}
$aReplacement['QUESTION']=$aQuestionQanda[0]['all'] ; // Deprecated : only used in old template (very old)
// Core value : user text
$aReplacement['QUESTION_TEXT'] = $aQuestionQanda[0]['text'];
if ($sType == '*')
{
$aReplacement['QUESTION_TEXT'] = '<div class="em_equation">' .$aReplacement['QUESTION_TEXT']. '</div>';
}
$aReplacement['QUESTIONHELP']=$lemQuestionInfo['info']['help'];// User help
// To be moved in a extra plugin : QUESTIONHELP img adding
$sTemplateDir=Template::model()->getTemplatePath($oSurveyId->template);
$sTemplateUrl=Template::model()->getTemplateURL($oSurveyId->template);
if(flattenText($aReplacement['QUESTIONHELP'], true,true) != '')
{
if (file_exists($sTemplateDir . '/help.gif'))
{
$helpicon = $sTemplateUrl . '/help.gif';
}
elseif (file_exists($sTemplateDir . '/help.png'))
{
$helpicon = $sTemplateUrl . '/help.png';
}
else
{
$helpicon=Yii::app()->getConfig('imageurl')."/help.gif";
}
$aReplacement['QUESTIONHELP']="<img src='{$helpicon}' alt='Help' align='left' />".$aReplacement['QUESTIONHELP'];
}
// Core value :the classes
$aReplacement['QUESTION_CLASS'] = Question::getQuestionClass($sType);
$aMandatoryClass = array();
if ($lemQuestionInfo['info']['mandatory'] == 'Y')// $aQuestionQanda[0]['mandatory']=="*"
{
$aMandatoryClass[]= 'mandatory';
}
if ($lemQuestionInfo['anyUnanswered'] && $_SESSION['survey_' . $iSurveyId]['maxstep'] != $_SESSION['survey_' . $iSurveyId]['step'])// This is working ?
{
$aMandatoryClass[]= 'missing';
}
$aReplacement['QUESTION_MAN_CLASS']=!empty($aMandatoryClass) ? " ".implode(" ",$aMandatoryClass) : "";
$aReplacement['QUESTION_INPUT_ERROR_CLASS']=$aQuestionQanda[0]['input_error_class'];
// Core value : LS text : EM and not
$aReplacement['ANSWER']=$aQuestionQanda[1];
$aReplacement['QUESTION_HELP']=$aQuestionQanda[0]['help'];// Core help only, not EM
$aReplacement['QUESTION_VALID_MESSAGE']=$aQuestionQanda[0]['valid_message'];// $lemQuestionInfo['validTip']
$aReplacement['QUESTION_FILE_VALID_MESSAGE']=$aQuestionQanda[0]['file_valid_message'];// $lemQuestionInfo['??']
$aReplacement['QUESTION_MAN_MESSAGE']=$aQuestionQanda[0]['man_message'];
$aReplacement['QUESTION_MANDATORY']=$aQuestionQanda[0]['mandatory'];
// For QUESTION_ESSENTIALS
$aHtmlOptions=array();
if ((!$lemQuestionInfo['relevant']) || ($lemQuestionInfo['hidden']))// && $lemQuestionInfo['info']['type'] == '*'))
{
$aHtmlOptions['style'] = 'display: none;';
}

// Launch the event
$event = new PluginEvent('beforeQuestionRender');
// Some helper
$event->set('surveyId', $iSurveyId);
$event->set('type', $sType);
$event->set('code', $sCode);
$event->set('qid', $iQid);
// User text
$event->set('text', $aReplacement['QUESTION_TEXT']);
$event->set('questionhelp', $aReplacement['QUESTIONHELP']);
// The classes
$event->set('class', $aReplacement['QUESTION_CLASS']);
$event->set('man_class', $aReplacement['QUESTION_MAN_CLASS']);
$event->set('input_error_class', $aReplacement['QUESTION_INPUT_ERROR_CLASS']);
// LS core text
$event->set('answers', $aReplacement['ANSWER']);
$event->set('help', $aReplacement['QUESTION_HELP']);
$event->set('man_message', $aReplacement['QUESTION_MAN_MESSAGE']);
$event->set('valid_message', $aReplacement['QUESTION_VALID_MESSAGE']);
$event->set('file_valid_message', $aReplacement['QUESTION_FILE_VALID_MESSAGE']);
// htmlOptions for container
$event->set('aHtmlOptions', $aHtmlOptions);

App()->getPluginManager()->dispatchEvent($event);
// User text
$aReplacement['QUESTION_TEXT'] = $event->get('text');
$aReplacement['QUESTIONHELP'] = $event->get('questionhelp');
$aReplacement['QUESTIONHELPPLAINTEXT']=strip_tags(addslashes($aReplacement['QUESTIONHELP']));
// The classes
$aReplacement['QUESTION_CLASS'] = $event->get('class');
$aReplacement['QUESTION_MAN_CLASS'] = $event->get('man_class');
$aReplacement['QUESTION_INPUT_ERROR_CLASS'] = $event->get('input_error_class');
// LS core text
$aReplacement['ANSWER'] = $event->get('answers');
$aReplacement['QUESTION_HELP'] = $event->get('help');
$aReplacement['QUESTION_MAN_MESSAGE'] = $event->get('man_message');
$aReplacement['QUESTION_VALID_MESSAGE'] = $event->get('valid_message');
$aReplacement['QUESTION_FILE_VALID_MESSAGE'] = $event->get('file_valid_message');
$aReplacement['QUESTION_MANDATORY'] = $event->get('mandatory',$aReplacement['QUESTION_MANDATORY']);
// Always add id for QUESTION_ESSENTIALS
$aHtmlOptions['id']="question{$iQid}";
$aReplacement['QUESTION_ESSENTIALS']=CHtml::renderAttributes($aHtmlOptions);

return $aReplacement;
}
}
10 changes: 2 additions & 8 deletions application/helpers/admin/template_helper.php
Expand Up @@ -14,14 +14,8 @@


function doreplacement($file,$data) { //Produce sample page from template file
/**
$output=array();
foreach(file($file) as $op) {
$output[]=templatereplace($op);
}
return $output;
*/
return (array)templatereplace(file_get_contents($file),array(),$data);
$aReplacements=isset($data['aReplacements']) ? $data['aReplacements'] : array();
return (array)templatereplace(file_get_contents($file),$aReplacements,$data);
}


Expand Down

0 comments on commit 1d9b183

Please sign in to comment.