Skip to content

Commit

Permalink
New Feature Generate EM-Syntax-Highlighted Logic File for Surveys
Browse files Browse the repository at this point in the history
Dev Now able to generate language-specific logic files from admin menu - e.g. showing entire survey and logic in the target language

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev_tms@11660 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
TMSWhite committed Dec 14, 2011
1 parent 5c3bffd commit 1fb6a30
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 49 deletions.
31 changes: 25 additions & 6 deletions admin/html.php
Expand Up @@ -213,7 +213,10 @@
$icontext2=$clang->gTview("Execute This Survey");
}
$baselang = GetBaseLanguageFromSurveyID($surveyid);
if (count(GetAdditionalLanguagesFromSurveyID($surveyid)) == 0)
$tmp_survlangs = GetAdditionalLanguagesFromSurveyID($surveyid);
$tmp_survlangs[] = $baselang;
rsort($tmp_survlangs);
if (count($tmp_survlangs) == 1)
{
$surveysummary .= "<li><a href='#' accesskey='d' onclick=\"window.open('"
. $publicurl."/index.php?sid={$surveyid}&amp;newtest=Y&amp;lang={$baselang}', '_blank')\" title=\"{$icontext2}\" >"
Expand All @@ -227,9 +230,6 @@
. "</a><ul>\n";
$surveysummary .= "<li><a accesskey='d' target='_blank' href='{$publicurl}/index.php?sid=$surveyid&amp;newtest=Y'>"
. "<img src='{$imageurl}/do_30.png' /> $icontext </a><ul>";
$tmp_survlangs = GetAdditionalLanguagesFromSurveyID($surveyid);
$tmp_survlangs[] = $baselang;
rsort($tmp_survlangs);
// Test Survey Language Selection Popup
foreach ($tmp_survlangs as $tmp_lang)
{
Expand Down Expand Up @@ -312,8 +312,27 @@
// QUALITY ASSURANCE BUTTON - SHOW LOGIC FILE
if(bHasSurveyPermission($surveyid,'surveyactivation','read'))
{
$surveysummary .= "<li><a target='_blank' href='{$scriptname}?action=showlogicfile&amp;sid={$surveyid}' >"
. "<img src='{$imageurl}/quality_assurance.png' name='ShowLogicFile' /> ".$clang->gT("Survey Logic File")."</a></li>\n";
$icontext = $clang->gT("Survey Logic File");
}

if (count($tmp_survlangs) == 1)
{
$surveysummary .= "<li><a href='#' onclick=\"window.open('{$scriptname}?action=showlogicfile&amp;sid={$surveyid}', '_blank')\" title=\"{$icontext}\" >"
. "<img src='{$imageurl}/quality_assurance.png' alt='$icontext' />$icontext"
. "</a></li>\n";

} else {
$surveysummary .= "<li><a href='#' "
. "title='{$icontext}'>"
. "<img src='{$imageurl}/quality_assurance.png' alt='{$icontext}' />$icontext"
. "</a><ul>\n";
// Test Survey Language Selection Popup
foreach ($tmp_survlangs as $tmp_lang)
{
$surveysummary .= "<li><a target='_blank' href='{$scriptname}?action=showlogicfile&amp;sid={$surveyid}&amp;lang={$tmp_lang}'>"
. "<img src='{$imageurl}/quality_assurance.png' /> ".getLanguageNameFromCode($tmp_lang,false)."</a></li>";
}
$surveysummary .= "</ul></li>";
}

$surveysummary .='</ul></li>'; // End if survey properties
Expand Down
89 changes: 62 additions & 27 deletions classes/eval/LimeExpressionManager.php
Expand Up @@ -55,7 +55,6 @@ class LimeExpressionManager {
private $gseq2info; // array of group sequence number to static info

private $maxGroupSeq; // the maximum groupSeq reached - this is needed for Index
private $slang='en';
private $q2subqInfo; // mapping of questions to information about their subquestions.
private $qattr; // array of attributes for each question
private $syntaxErrors=array();
Expand Down Expand Up @@ -137,21 +136,30 @@ public static function SetSurveyId($sid=NULL)
}

/**
* A legacy upgrader. Relevance was initially a Question Attribute during development. However, as of 2.0 alpha, it was already in the questions table.
* @return <type>
* Sets the survey language. If the language has changed, then EM cache must be invalidated and refreshed
* @param string $lang
*/
public static function UpgradeRelevanceAttributeToQuestion()
public static function SetSurveyLanguage($lang=NULL)
{
$query = "SELECT qid, value from ".db_table_name('question_attributes')." where attribute='relevance'";
$qresult = db_execute_assoc($query);
$queries = array();
foreach ($qresult->GetRows() as $row)
{
$query = "UPDATE ".db_table_name('questions')." SET relevance='".$row['value']."' WHERE qid=".$row['qid'];
db_execute_assoc($query);
$queries[] = $query;
if (is_null($lang)) {
$lang = 'en'; // should really be the survey base language
}
return $queries;
$lang = sanitize_languagecode($lang);
if (!isset($_SESSION['LEMlang'])) {
$_SESSION['LEMlang'] = $lang;
}
if ($_SESSION['LEMlang'] != $lang) {
// then changing languages, so clear cache
$_SESSION['LEMdirtyFlag'] = true;
$_SESSION['LEMforceRefresh'] = true;

if (isset($_SESSION['LEMsid'])) {
SetSurveyLanguage($_SESSION['LEMsid'], $lang);
$lang = $_SESSION['s_lang'];
}
}

$_SESSION['LEMlang'] = $lang;
}

/**
Expand Down Expand Up @@ -1042,10 +1050,8 @@ public function setVariableAndTokenMappingsForExpressionManager($surveyid,$force
return false; // means that those variables have been cached and no changes needed
}
$now = microtime(true);
// $LEM->slang = (isset($_SESSION['s_lang']) ? $_SESSION['s_lang'] : 'en');
// log_message('debug','**Language=' . $LEM->slang);

$fieldmap=createFieldMap($surveyid,$style='full',$forceRefresh);
$fieldmap=createFieldMap($surveyid,$style='full',$forceRefresh,false,$_SESSION['LEMlang']);
$this->sid= $surveyid;

$this->runtimeTimings[] = array(__METHOD__ . '.createFieldMap',(microtime(true) - $now));
Expand Down Expand Up @@ -1092,20 +1098,21 @@ public function setVariableAndTokenMappingsForExpressionManager($surveyid,$force
'D' => $this->gT("Decrease"),
);

$this->gseq2info = $this->getGroupInfoForEM($surveyid,$this->slang);
$this->gseq2info = $this->getGroupInfoForEM($surveyid,$_SESSION['LEMlang']);
for ($i=0;$i<count($this->gseq2info);++$i)
{
$gseq = $this->gseq2info[$i];
$this->groupId2groupSeq[$gseq['gid']] = $i;
}

$qattr = $this->getQuestionAttributesForEM($surveyid); // what happens if $surveyid is null?
$qattr = $this->getQuestionAttributesForEM($surveyid,NULL,$_SESSION['LEMlang']);

$this->qattr = $qattr;

$this->runtimeTimings[] = array(__METHOD__ . ' - question_attributes_model->getQuestionAttributesForEM',(microtime(true) - $now));
$now = microtime(true);

$this->qans = $this->getAnswerSetsForEM($surveyid,NULL); // ,$this->slang); // TODO - will this work for multi-lingual?
$this->qans = $this->getAnswerSetsForEM($surveyid,NULL,$_SESSION['LEMlang']);

$this->runtimeTimings[] = array(__METHOD__ . ' - answers_model->getAnswerSetsForEM',(microtime(true) - $now));
$now = microtime(true);
Expand Down Expand Up @@ -2045,7 +2052,6 @@ static function StartProcessingPage($allOnOnePage=false,$rooturl=NULL)
$LEM->allOnOnePage=$allOnOnePage;
$LEM->pageTailoringLog='';
$LEM->surveyLogicFile='';
$LEM->slang = (isset($_SESSION['s_lang']) ? $_SESSION['s_lang'] : 'en');
$LEM->processedRelevance=false;
if (!is_null($rooturl)) {
$LEM->surveyOptions['rooturl'] = $rooturl;
Expand Down Expand Up @@ -4727,23 +4733,26 @@ private static function getConditionsForEM($surveyid=NULL, $qid=NULL)
return $data;
}

private function getQuestionAttributesForEM($surveyid=NULL,$qid=NULL)
private function getQuestionAttributesForEM($surveyid=NULL,$qid=NULL, $lang=NULL)
{
if (!is_null($qid)) {
$where = " a.qid = ".$qid." and ";
$where = " a.qid = ".$qid;
}
else if (!is_null($surveyid)) {
$where = " a.qid=b.qid and b.sid=".$surveyid." and ";
$where = " a.qid=b.qid and b.sid=".$surveyid;
}
else {
$where = " and ";
$where = " 1";
}
if (!is_null($lang)) {
$lang = " and a.language='".$lang."' and b.language='".$lang."'";
}

// TODO - does this need to be filtered by language?
$query = "select distinct a.qid, a.attribute, a.value"
." from ".db_table_name('question_attributes')." as a, ".db_table_name('questions')." as b"
." where " . $where . '1'
// ." a.attribute in ('hidden', 'array_filter', 'array_filter_exclude', 'code_filter', 'equals_num_value', 'exclude_all_others', 'exclude_all_others_auto', 'max_answers', 'max_num_value', 'max_num_value_n', 'max_num_value_sgqa', 'min_answers', 'min_num_value', 'min_num_value_n', 'min_num_value_sgqa', 'multiflexible_max', 'multiflexible_min', 'num_value_equals_sgqa', 'other_replace_text', 'show_totals')"
." where " . $where
.$lang
." order by a.qid, a.attribute";

$data = db_execute_assoc($query);
Expand All @@ -4753,6 +4762,20 @@ private function getQuestionAttributesForEM($surveyid=NULL,$qid=NULL)
$qattr[$row['qid']][$row['attribute']] = $row['value'];
}

if (!is_null($lang))
{
// Then get non-language specific first, and overwrite with language-specific
$qattr2 = $qattr;
$qattr = $this->getQuestionAttributesForEM($surveyid,$qid);
foreach ($qattr2 as $q => $qatrs) {
if (is_array($qattrs)) {
foreach ($qattrs as $attr=>$value) {
$qattr[$q][$attr] = $value;
}
}
}
}

return $qattr;
}

Expand Down Expand Up @@ -4942,6 +4965,11 @@ static public function ShowSurveyLogicFile($sid, $language=NULL, $gid=NULL, $qid
// End Message
global $rooturl;

if (!is_null($language)) {
// must be called before getting LEM singleton
self::SetSurveyLanguage($language);
}

$LEM =& LimeExpressionManager::singleton();

$allErrors = array();
Expand Down Expand Up @@ -4978,9 +5006,16 @@ static public function ShowSurveyLogicFile($sid, $language=NULL, $gid=NULL, $qid
$qtypes=getqtypelist('','array');

templatereplace('{SITENAME}'); // to ensure that lime replacement fields loaded

if (is_null($LEM->currentQset) || count($LEM->currentQset) == 0) {
return array(
'errors'=>1,
'html'=>$LEM->gT('No groups or questions found for language ') . $_SESSION['LEMlang'],
);
}

$out = "<table border='1'>"
. "<tr><th>#</th><th>Name [ID]</th><th>Relevance [Validation] (Default)</th><th>Text [Help] (Tip)</th></tr>\n";
. "<tr><th>#</th><th>".$LEM->gT('Name [ID]')."</th><th>".$LEM->gT('Relevance [Validation] (Default)')."</th><th>".$LEM->gT('Text [Help] (Tip)')."</th></tr>\n";

$_gseq=-1;
foreach ($LEM->currentQset as $q) {
Expand Down
16 changes: 14 additions & 2 deletions classes/eval/test/survey_logic_file.php
Expand Up @@ -6,6 +6,14 @@
</head>
<body>
<?php
if (count($_GET) > 0) {
foreach ($_GET as $key=>$val) {
if ($key == 'sid') {
$val = $val . '|N'; // hack to pretend this is not an assessment
}
$_POST[$key] = $val;
}
}
if ($_POST['LEMcalledFromAdmin']=='Y') {
$rootpath = $rootdir;
}
Expand Down Expand Up @@ -77,7 +85,7 @@
((isset($_POST['LEM_PRETTY_PRINT_ALL_SYNTAX']) && $_POST['LEM_PRETTY_PRINT_ALL_SYNTAX'] == 'Y') ? LEM_PRETTY_PRINT_ALL_SYNTAX : 0)
);

$language = (isset($_POST['language']) ? sanitize_languagecode($_POST['language']) : NULL);
$language = (isset($_POST['lang']) ? sanitize_languagecode($_POST['lang']) : NULL);
$gid = (isset($_POST['gid']) ? sanitize_int($_POST['gid']) : NULL);
$qid = (isset($_POST['qid']) ? sanitize_int($_POST['qid']) : NULL);

Expand Down Expand Up @@ -115,9 +123,13 @@
</style>
</head>
<body>
<H3>Logic File for Survey #$surveyid</H3>
EOD;


LimeExpressionManager::SetSurveyLanguage($language);

echo '<H3>' . $clang->gT('Logic File for Survey # ') . $surveyid . "</H3>\n";

$result = LimeExpressionManager::ShowSurveyLogicFile($surveyid, $language, $gid, $qid,$LEMdebugLevel,$assessments);
print $result['html'];

Expand Down
30 changes: 16 additions & 14 deletions common_functions.php
Expand Up @@ -2244,12 +2244,24 @@ function createFieldMap($surveyid, $style='full', $force_refresh=false, $questio

global $dbprefix, $connect, $globalfieldmap, $clang, $aDuplicateQIDs;
$surveyid=sanitize_int($surveyid);

//Get list of questions
if (is_null($sQuestionLanguage))
{
$sQuestionLanguage = GetBaseLanguageFromSurveyID($surveyid);
}
$sQuestionLanguage = sanitize_languagecode($sQuestionLanguage);
if ($clang->langcode != $sQuestionLanguage) {
SetSurveyLanguage($surveyid, $sQuestionLanguage);
}
$s_lang = $clang->langcode;

//checks to see if fieldmap has already been built for this page.
if (isset($globalfieldmap[$surveyid][$style][$clang->langcode]) && $force_refresh==false) {
return $globalfieldmap[$surveyid][$style][$clang->langcode];
if (isset($globalfieldmap[$surveyid][$style][$s_lang]) && $force_refresh==false) {
return $globalfieldmap[$surveyid][$style][$s_lang];
}
if (isset($_SESSION['fieldmap-' . $surveyid . $clang->langcode]) && !$force_refresh) {
return $_SESSION['fieldmap-' . $surveyid . $clang->langcode];
if (isset($_SESSION['fieldmap-' . $surveyid . $s_lang]) && !$force_refresh) {
return $_SESSION['fieldmap-' . $surveyid . $s_lang];
}

$fieldmap["id"]=array("fieldname"=>"id", 'sid'=>$surveyid, 'type'=>"id", "gid"=>"", "qid"=>"", "aid"=>"");
Expand Down Expand Up @@ -2356,16 +2368,6 @@ function createFieldMap($surveyid, $style='full', $force_refresh=false, $questio
}
}

//Get list of questions
if (is_null($sQuestionLanguage))
{
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
}
else
{
$s_lang = $sQuestionLanguage;
}

// Collect all default values once so don't need separate query for each question with defaults
// First collect language specific defaults
$defaultsQuery = "SELECT a.qid, a.sqid, a.scale_id, a.specialtype, a.defaultvalue"
Expand Down
4 changes: 4 additions & 0 deletions group.php
Expand Up @@ -58,6 +58,10 @@
setcookie("limesurvey_timers", "0");
$show_empty_group = false;

if (isset($_REQUEST['lang'])) {
LimeExpressionManager::SetSurveyLanguage($_REQUEST['lang']);
}

if ($previewgrp)
{
$_SESSION['prevstep'] = 1;
Expand Down

0 comments on commit 1fb6a30

Please sign in to comment.