From 1ce437d7a412825d438feb622dd082a1a0c4a9a7 Mon Sep 17 00:00:00 2001 From: Shubham Sachdeva Date: Mon, 27 Jun 2011 15:36:17 +0000 Subject: [PATCH] Question groups list now added to surveymenu. Made helpers for Import and Copy survey(feature available after import controller is implemented). Fixed few errors in common_helper and groups_model! git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_ci@10354 b72ed6b6-b9f8-46b5-92b4-906544132732 --- application/controllers/admin/survey.php | 163 ++++++ application/core/SurveyCommonController.php | 36 +- .../helpers/admin/export_data_helper.php | 535 ++++++++++++++++++ .../admin/export_structure_xml_helper.php | 217 +++++++ application/helpers/common_helper.php | 33 +- application/models/groups_model.php | 2 +- application/models/questions_model.php | 19 + application/models/surveys_model.php | 2 + .../views/admin/Survey/copySurvey_view.php | 64 +++ .../admin/Survey/exportSurveyError_view.php | 13 + application/views/admin/Survey/surveybar.php | 86 ++- 11 files changed, 1154 insertions(+), 16 deletions(-) create mode 100644 application/helpers/admin/export_data_helper.php create mode 100644 application/helpers/admin/export_structure_xml_helper.php create mode 100644 application/views/admin/Survey/copySurvey_view.php create mode 100644 application/views/admin/Survey/exportSurveyError_view.php diff --git a/application/controllers/admin/survey.php b/application/controllers/admin/survey.php index 1db7045084c..a56c96585c8 100644 --- a/application/controllers/admin/survey.php +++ b/application/controllers/admin/survey.php @@ -38,6 +38,169 @@ function view($surveyid) } } + function copy() + { + + if ( $this->input->post('copysurveytranslinksfields') == "on" || $this->input->post('translinksfields') == "on") + { + $sTransLinks = true; + } + + //$importsurvey = "
".$clang->gT("Copy survey")."
\n"; + $copyfunction= true; + //$importsurvey .= "
\n"; + $importerror=false; // Put a var for continue + $surveyid = sanitize_int($this->input->post('copysurveylist')); + + $exclude = array(); + if (get_magic_quotes_gpc()) {$sNewSurveyName = stripslashes($this->input->post('copysurveyname'));} + else{ + $sNewSurveyName=$this->input->post('copysurveyname'); + } + + /*require_once("../classes/inputfilter/class.inputfilter_clean.php"); + $myFilter = new InputFilter('','',1,1,1); + if ($filterxsshtml) + { + $sNewSurveyName = $myFilter->process($sNewSurveyName); + } else { + $sNewSurveyName = html_entity_decode($sNewSurveyName, ENT_QUOTES, "UTF-8"); + } */ + if ($this->input->post('copysurveyexcludequotas') == "on") + { + $exclude['quotas'] = true; + } + if ($this->input->post('copysurveyexcludeanswers') == "on") + { + $exclude['answers'] = true; + } + if ($this->input->post('copysurveyresetconditions') == "on") + { + $exclude['conditions'] = true; + } + echo "hi"; + //include("export_structure_xml.php"); + $this->load->helper('admin/export_structure_xml'); + + if (!isset($surveyid)) + { + $surveyid=returnglobal('sid'); + } + + $clang = $this->limesurvey_lang; + if (!$surveyid) + { + + self::_getAdminHeader(); + self::_showadminmenu(); + $data['clang'] = $clang; + $this->load->view('admin/Survey/exportSurveyError_view',$data); + + exit; + } + + if (!isset($copyfunction)) + { + $fn = "limesurvey_survey_$surveyid.lss"; + header("Content-Type: text/xml"); + header("Content-Disposition: attachment; filename=$fn"); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header("Pragma: public"); // HTTP/1.0 + echo getXMLData('',$surveyid); + exit; + } + + $copysurveydata = getXMLData($exclude,$surveyid); + // $this->load->helper('import'); + //require_once('import_functions.php'); + if (!$importerror) + { + $aImportResults=XMLImportSurvey('',$copysurveydata,$sNewSurveyName); + } + else + { + $importerror=true; + } + + /** + if (isset($aImportResults['error']) && $aImportResults['error']!=false) + { + $importsurvey .= "
".$clang->gT("Error")."

\n"; + $importsurvey .= $aImportResults['error']."

\n"; + $importsurvey .= "\n"; + $importerror = true; + } + + if (!$importerror) + { + $importsurvey .= "
\n
".$clang->gT("Success")."


\n"; + + $importsurvey .= "".$clang->gT("Survey import summary")."
\n"; + + + $importsurvey .= "
\n"; + + if (count($aImportResults['importwarnings'])>0) + { + $importsurvey .= "
".$clang->gT("Warnings").":

\n"; + } + + $importsurvey .= "".$clang->gT("Copy of survey is completed.")."
\n" + . "".$clang->gT("Go to survey")."
\n"; + + + + + } + // end of traitment an close message box + $importsurvey .= "

\n"; */ + + $data['clang'] = $clang; + $data['aImportResults'] = $aImportResults; + $data['importerror'] = $importerror; + self::_getAdminHeader(); + self::_showadminmenu(); + self::_surveybar($surveyid); + //self::_surveysummary($surveyid); + $this->load->view('admin/Survey/copySurvey_view',$data); + self::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual")); + + } + function index($action) { global $surveyid; diff --git a/application/core/SurveyCommonController.php b/application/core/SurveyCommonController.php index c5eb9cc39d8..b4e7bf3c2ff 100644 --- a/application/core/SurveyCommonController.php +++ b/application/core/SurveyCommonController.php @@ -40,7 +40,7 @@ function _surveybar($surveyid) { //$this->load->helper('surveytranslator'); $clang = $this->limesurvey_lang; - + echo $this->config->item('gid'); $baselang = GetBaseLanguageFromSurveyID($surveyid); $condition = array('sid' => $surveyid, 'language' => $baselang); $this->load->model('surveys_model'); @@ -144,7 +144,33 @@ function _surveybar($surveyid) $data['responsesread'] = bHasSurveyPermission($surveyid,'responses','read'); // TOKEN MANAGEMENT BUTTON $data['tokenmanagement'] = bHasSurveyPermission($surveyid,'surveysettings','update') || bHasSurveyPermission($surveyid,'tokens','read'); - + + $data['gid'] = $gid = $this->config->item('gid'); + + if (bHasSurveyPermission($surveyid,'surveycontent','read')) + { + $data['permission']= true; + } + else + { + $data['gid'] = $gid =null; + $qid=null; + } + + if (getgrouplistlang($gid, $baselang)) + { + $data['groups']= getgrouplistlang($gid, $baselang); + } + else + { + $data['groups']= ""; + } + + $data['GidPrev'] = $GidPrev = getGidPrevious($surveyid, $gid); + + $data['GidNext'] = $GidNext = getGidNext($surveyid, $gid); + $data['activated'] = $activated; + $this->load->view("admin/survey/surveybar",$data); @@ -252,8 +278,12 @@ function _surveybar($surveyid) $surveysummary .= "\n"; } - + $surveysummary .= "\n" + . "\n" + . "\n"; */ + + } /** diff --git a/application/helpers/admin/export_data_helper.php b/application/helpers/admin/export_data_helper.php new file mode 100644 index 00000000000..3be00cf271f --- /dev/null +++ b/application/helpers/admin/export_data_helper.php @@ -0,0 +1,535 @@ +load->helper('common'); +/** + * Strips html tags and replaces new lines + * + * @param $string + * @return $string + */ +function strip_tags_full($string) { + $string=html_entity_decode($string, ENT_QUOTES, "UTF-8"); + //combining these into one mb_ereg_replace call ought to speed things up + //$string = str_replace(array("\r\n","\r","\n",'-oth-'), '', $string); + //The backslashes must be escaped twice, once for php, and again for the regexp + //$string = str_replace("'|\\\\'", "'", $string); + return FlattenText($string); +} + +/** + * Returns true if passed $value is numeric + * + * @param $value + * @return bool + */ +function my_is_numeric($value) { + if (empty($value)) return true; + $eng_or_world = preg_match + ('/^[+-]?'. // start marker and sign prefix + '(((([0-9]+)|([0-9]{1,4}(,[0-9]{3,4})+)))?(\\.[0-9])?([0-9]*)|'. // american + '((([0-9]+)|([0-9]{1,4}(\\.[0-9]{3,4})+)))?(,[0-9])?([0-9]*))'. // world + '(e[0-9]+)?'. // exponent + '$/', // end marker + $value) == 1; + return ($eng_or_world); +} + +function spss_export_data ($na = null) { + global $length_data; + + // Build array that has to be returned + $fields = spss_fieldmap(); + + //Now get the query string with all fields to export + $query = spss_getquery(); + + $result=db_execute_num($query) or safe_die("Couldn't get results
$query"); //Checked + $num_fields = $result->num_rows(); + + //This shouldn't occur, but just to be safe: + if (count($fields)<>$num_fields) safe_die("Database inconsistency error"); + + //while (!$result->EOF) { + while ($num_fields > 0) { + //$row = $result->GetRowAssoc(true); //Get assoc array, use uppercase + $row = $result->result_array(); + reset($fields); //Jump to the first element in the field array + $i = 1; + foreach ($fields as $field) + { + //$fieldno = strtoupper($field['sql_name']); + $fieldno = strtoupper($field['sql_name']); + if ($field['SPSStype']=='DATETIME23.2'){ + #convert mysql datestamp (yyyy-mm-dd hh:mm:ss) to SPSS datetime (dd-mmm-yyyy hh:mm:ss) format + if (isset($row[$fieldno])) + { + list( $year, $month, $day, $hour, $minute, $second ) = preg_split( '([^0-9])', $row[$fieldno] ); + if ($year != '' && (int)$year >= 1970) + { + echo "'".date('d-m-Y H:i:s', mktime( $hour, $minute, $second, $month, $day, $year ) )."'"; + } else + { + echo ($na); + } + } else + { + echo ($na); + } + } else if ($field['LStype'] == 'Y') + { + if ($row[$fieldno] == 'Y') // Yes/No Question Type + { + echo( "'1'"); + } else if ($row[$fieldno] == 'N'){ + echo( "'2'"); + } else { + echo($na); + } + } else if ($field['LStype'] == 'G') //Gender + { + if ($row[$fieldno] == 'F') + { + echo( "'1'"); + } else if ($row[$fieldno] == 'M'){ + echo( "'2'"); + } else { + echo($na); + } + } else if ($field['LStype'] == 'C') //Yes/No/Uncertain + { + if ($row[$fieldno] == 'Y') + { + echo( "'1'"); + } else if ($row[$fieldno] == 'N'){ + echo( "'2'"); + } else if ($row[$fieldno] == 'U'){ + echo( "'3'"); + } else { + echo($na); + } + } else if ($field['LStype'] == 'E') //Increase / Same / Decrease + { + if ($row[$fieldno] == 'I') + { + echo( "'1'"); + } else if ($row[$fieldno] == 'S'){ + echo( "'2'"); + } else if ($row[$fieldno] == 'D'){ + echo( "'3'"); + } else { + echo($na); + } + } elseif (($field['LStype'] == 'P' || $field['LStype'] == 'M') && (substr($field['code'],-7) != 'comment' && substr($field['code'],-5) != 'other')) + { + if ($row[$fieldno] == 'Y') + { + echo("'1'"); + } else + { + echo("'0'"); + } + } elseif (!$field['hide']) { + $strTmp=mb_substr(strip_tags_full($row[$fieldno]), 0, $length_data); + if (trim($strTmp) != ''){ + $strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp)); + /* + * Temp quick fix for replacing decimal dots with comma's + if (my_is_numeric($strTemp)) { + $strTemp = str_replace('.',',',$strTemp); + } + */ + echo "'$strTemp'"; + } + else + { + echo $na; + } + } + if ($i<$num_fields && !$field['hide']) echo ','; + $i++; + } + echo "\n"; + //$result->MoveNext(); + } +} + +/** + * Check it the gives field has a labelset and return it as an array if true + * + * @param $field array field from spss_fieldmap + * @return array or false + */ +function spss_getvalues ($field = array(), $qidattributes = null ) { + global $surveyid, $language, $length_vallabel; + $CI =& get_instance(); + $clang = $CI->limesurvey_lang; + if (!isset($field['LStype']) || empty($field['LStype'])) return false; + $answers=array(); + if (strpos("!LORFWZWH1",$field['LStype']) !== false) { + if (substr($field['code'],-5) == 'other' || substr($field['code'],-7) == 'comment') { + //We have a comment field, so free text + } else { + /**$query = "SELECT {$dbprefix}answers.code, {$dbprefix}answers.answer, + {$dbprefix}questions.type FROM {$dbprefix}answers, {$dbprefix}questions WHERE"; + + if (isset($field['scale_id'])) $query .= " {$dbprefix}answers.scale_id = " . (int) $field['scale_id'] . " AND"; + + $query .= " {$dbprefix}answers.qid = '".$field["qid"]."' and {$dbprefix}questions.language='".$language."' and {$dbprefix}answers.language='".$language."' + and {$dbprefix}questions.qid='".$field['qid']."' ORDER BY sortorder ASC"; */ + $CI->load->model('questions_model'); + + $result=$CI->questions_model->valueLabel($field,$language); //db_execute_assoc($query) or safe_die("Couldn't lookup value labels
$query
".$connect->ErrorMsg()); //Checked + $num_results = $result->num_rows(); + if ($num_results > 0) + { + $displayvaluelabel = 0; + # Build array that has to be returned + for ($i=0; $i < $num_results; $i++) + { + $row = $result->result_array(); + $answers[] = array('code'=>$row['code'], 'value'=>mb_substr(strip_tags_full($row["answer"]),0,$length_vallabel)); + } + } + } + } elseif ($field['LStype'] == ':') { + $displayvaluelabel = 0; + //Get the labels that could apply! + if (is_null($qidattributes)) $qidattributes=getQuestionAttributes($field["qid"], $field['LStype']); + if (trim($qidattributes['multiflexible_max'])!='') { + $maxvalue=$qidattributes['multiflexible_max']; + } else { + $maxvalue=10; + } + if (trim($qidattributes['multiflexible_min'])!='') + { + $minvalue=$qidattributes['multiflexible_min']; + } else { + $minvalue=1; + } + if (trim($qidattributes['multiflexible_step'])!='') + { + $stepvalue=$qidattributes['multiflexible_step']; + } else { + $stepvalue=1; + } + if ($qidattributes['multiflexible_checkbox']!=0) { + $minvalue=0; + $maxvalue=1; + $stepvalue=1; + } + for ($i=$minvalue; $i<=$maxvalue; $i+=$stepvalue) + { + $answers[] = array('code'=>$i, 'value'=>$i); + } + } elseif ($field['LStype'] == 'M' && substr($field['code'],-5) != 'other' && $field['size'] > 0) + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Yes')); + $answers[] = array('code'=>0, 'value'=>$clang->gT('Not Selected')); + } elseif ($field['LStype'] == "P" && substr($field['code'],-5) != 'other' && substr($field['code'],-7) != 'comment') + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Yes')); + $answers[] = array('code'=>0, 'value'=>$clang->gT('Not Selected')); + } elseif ($field['LStype'] == "G" && $field['size'] > 0) + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Female')); + $answers[] = array('code'=>2, 'value'=>$clang->gT('Male')); + } elseif ($field['LStype'] == "Y" && $field['size'] > 0) + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Yes')); + $answers[] = array('code'=>2, 'value'=>$clang->gT('No')); + } elseif ($field['LStype'] == "C" && $field['size'] > 0) + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Yes')); + $answers[] = array('code'=>2, 'value'=>$clang->gT('No')); + $answers[] = array('code'=>3, 'value'=>$clang->gT('Uncertain')); + } elseif ($field['LStype'] == "E" && $field['size'] > 0) + { + $answers[] = array('code'=>1, 'value'=>$clang->gT('Increase')); + $answers[] = array('code'=>2, 'value'=>$clang->gT('Same')); + $answers[] = array('code'=>3, 'value'=>$clang->gT('Decrease')); + } + if (count($answers)>0) { + //check the max width of the answers + $size = 0; + $spsstype = $field['SPSStype']; + foreach ($answers as $answer) { + $len = mb_strlen($answer['code']); + if ($len>$size) $size = $len; + if ($spsstype=='F' && (my_is_numeric($answer['code'])===false || $size>16)) $spsstype='A'; + } + $answers['SPSStype'] = $spsstype; + $answers['size'] = $size; + return $answers; + } else { + return false; + } +} + +/** + * Creates a fieldmap with all information necessary to output the fields + * + * @param $prefix string prefix for the variable ID + * @return array + */ +function spss_fieldmap($prefix = 'V') { + global $surveyid, $typeMap;//, $connect, $clang; + global $surveyprivate, $tokensexist, $language; + $CI =& get_instance(); + $fieldmap = createFieldMap($surveyid, 'full'); //Create a FULL fieldmap + + #See if tokens are being used + $tokensexist = tableExists('tokens_'.$surveyid); + + #Lookup the names of the attributes + $condition = array('sid' => $surveyid); + $field = array('sid','anonymized', 'language'); + //$query="SELECT sid, anonymized, language FROM {$dbprefix}surveys WHERE sid=$surveyid"; + $CI->load->model('surveys_model'); + $result=$CI->surveys_model->getSomeRecords($field,$condition);//($query) or safe_die("Couldn't count fields
$query
".$connect->ErrorMsg()); //Checked) + $num_results = $result->num_rows(); + $num_fields = $num_results; + # Build array that has to be returned + for ($i=0; $i < $num_results; $i++) { + $row = $result->row_array(); + $surveyprivate=$row['anonymized']; + $language=$row['language']; + } + + $fieldno=0; + + $fields=array(); + if (isset($tokensexist) && $tokensexist == true && $surveyprivate == 'N') { + $tokenattributes=GetTokenFieldsAndNames($surveyid,false); + foreach ($tokenattributes as $attributefield=>$attributedescription) + { + //Drop the token field, since it is in the survey too + if($attributefield!='token') { + $fieldno++; + $fields[] = array('id'=>"$prefix$fieldno",'name'=>mb_substr($attributefield, 0, 8), + 'qid'=>0,'code'=>'','SPSStype'=>'A','LStype'=>'Undef', + 'VariableLabel'=>$attributedescription,'sql_name'=>$attributefield,'size'=>'100', + 'title'=>$attributefield,'hide'=>0, 'scale'=>''); + } + } + } + + $tempArray = array(); + $fieldnames = $CI->db->list_fields();//($connect->MetaColumnNames("{$dbprefix}survey_$surveyid", true)); + $num_results = count($fieldnames); + $num_fields = $num_results; + $diff = 0; + $noQID = Array('id', 'token', 'datestamp', 'submitdate', 'startdate', 'startlanguage', 'ipaddr', 'refurl', 'lastpage'); + # Build array that has to be returned + for ($i=0; $i < $num_results; $i++) { + #Conditions for SPSS fields: + # - Length may not be longer than 8 characters + # - Name may not begin with a digit + $fieldname = $fieldnames[$i]; + $fieldtype = ''; + $ftype=''; + $val_size = 1; + $hide = 0; + $export_scale = ''; + $code=''; + $scale_id = null; + $aQuestionAttribs=array(); + + #Determine field type + if ($fieldname=='submitdate' || $fieldname=='startdate' || $fieldname == 'datestamp') { + $fieldtype = 'DATETIME23.2'; + } elseif ($fieldname=='startlanguage') { + $fieldtype = 'A'; + $val_size = 19; + } elseif ($fieldname=='token') { + $fieldtype = 'A'; + $val_size = 16; + } elseif ($fieldname=='id') { + $fieldtype = 'F'; + $val_size = 7; //Arbitrarilty restrict to 9,999,999 (7 digits) responses/survey + } elseif ($fieldname == 'ipaddr') { + $fieldtype = 'A'; + $val_size = 15; + } elseif ($fieldname == 'refurl') { + $fieldtype = 'A'; + $val_size = 255; + } elseif ($fieldname == 'lastpage') { + $hide = 1; + } + + #Get qid (question id) + if (in_array($fieldname, $noQID) || substr($fieldname,0,10)=='attribute_'){ + $qid = 0; + $varlabel = $fieldname; + $ftitle = $fieldname; + } else{ + //GET FIELD DATA + if (!isset($fieldmap[$fieldname])) { + //Field in database but no longer in survey... how is this possible? + //@TODO: think of a fix. + $fielddata = array(); + $qid=0; + $varlabel = $fieldname; + $ftitle = $fieldname; + $fieldtype = "F"; + $val_size = 1; + } else { + $fielddata=$fieldmap[$fieldname]; + $qid=$fielddata['qid']; + $ftype=$fielddata['type']; + $fsid=$fielddata['sid']; + $fgid=$fielddata['gid']; + $code=mb_substr($fielddata['fieldname'],strlen($fsid."X".$fgid."X".$qid)); + $varlabel=$fielddata['question']; + if (isset($fielddata['scale'])) $varlabel = "[{$fielddata['scale']}] ". $varlabel; + if (isset($fielddata['subquestion'])) $varlabel = "[{$fielddata['subquestion']}] ". $varlabel; + if (isset($fielddata['subquestion2'])) $varlabel = "[{$fielddata['subquestion2']}] ". $varlabel; + if (isset($fielddata['subquestion1'])) $varlabel = "[{$fielddata['subquestion1']}] ". $varlabel; + $ftitle=$fielddata['title']; + if (!is_null($code) && $code<>"" ) $ftitle .= "_$code"; + if (isset($typeMap[$ftype]['size'])) $val_size = $typeMap[$ftype]['size']; + if (isset($fielddata['scale_id'])) $scale_id = $fielddata['scale_id']; + if($fieldtype == '') $fieldtype = $typeMap[$ftype]['SPSStype']; + if (isset($typeMap[$ftype]['hide'])) { + $hide = $typeMap[$ftype]['hide']; + $diff++; + } + //Get default scale for this type + if (isset($typeMap[$ftype]['Scale'])) $export_scale = $typeMap[$ftype]['Scale']; + //But allow override + $aQuestionAttribs = getQuestionAttributes($qid,$ftype); + if (isset($aQuestionAttribs['scale_export'])) $export_scale = $aQuestionAttribs['scale_export']; + } + + } + $fieldno++; + $fid = $fieldno - $diff; + $lsLong = isset($typeMap[$ftype]["name"])?$typeMap[$ftype]["name"]:$ftype; + $tempArray = array('id'=>"$prefix$fid",'name'=>mb_substr($fieldname, 0, 8), + 'qid'=>$qid,'code'=>$code,'SPSStype'=>$fieldtype,'LStype'=>$ftype,"LSlong"=>$lsLong, + 'ValueLabels'=>'','VariableLabel'=>$varlabel,"sql_name"=>$fieldname,"size"=>$val_size, + 'title'=>$ftitle,'hide'=>$hide,'scale'=>$export_scale, 'scale_id'=>$scale_id); + //Now check if we have to retrieve value labels + $answers = spss_getvalues($tempArray, $aQuestionAttribs); + if (is_array($answers)) { + //Ok we have answers + if (isset($answers['size'])) { + $tempArray['size'] = $answers['size']; + unset($answers['size']); + } + if (isset($answers['SPSStype'])) { + $tempArray['SPSStype'] = $answers['SPSStype']; + unset($answers['SPSStype']); + } + $tempArray['answers'] = $answers; + } + $fields[] = $tempArray; + } + return $fields; +} + +/** + * Creates a query string with all fields for the export + * + * @return string + */ +function spss_getquery() { + global $surveyprivate, $surveyid, $tokensexist; + $CI =& get_instance(); + #See if tokens are being used + if (isset($tokensexist) && $tokensexist == true && $surveyprivate == 'N') { + $query="SELECT "; + $tokenattributes=GetTokenFieldsAndNames($surveyid,false); + foreach ($tokenattributes as $attributefield=>$attributedescription) { + //Drop the token field, since it is in the survey too + if($attributefield!='token') { + $query .= $CI->db->dbprefix."tokens_$surveyid.$attributefield, "; + } + } + $query .= $CI->db->dbprefix."survey_$surveyid.* + FROM ".$CI->db->dbprefix."survey_$surveyid + LEFT JOIN ".$CI->db->dbprefix."tokens_$surveyid ON ".$CI->db->dbprefix."survey_$surveyid.token = ".$CI->db->dbprefix."tokens_$surveyid.token"; + } else { + $query = "SELECT * + FROM ".$CI->db->dbprefix."survey_$surveyid"; + } + switch (incompleteAnsFilterstate()) { + case 'inc': + //Inclomplete answers only + $query .= ' WHERE submitdate is null '; + break; + case 'filter': + //Inclomplete answers only + $query .= ' WHERE submitdate is not null '; + break; + } + return $query; +} + +/** +* BuildXMLFromQuery() creates a datadump of a table in XML using XMLWriter +* +* @param mixed $xmlwriter The existing XMLWriter object +* @param mixed $Query The table query to build from +* @param mixed $tagname If the XML tag of the resulting question should be named differently than the table name set it here +* @param array $excludes array of columnames not to include in export +*/ +function BuildXMLFromQuery($xmlwriter, $Query, $tagname='', $excludes = array()) +{ + //global $dbprefix, $connect; + $CI =& get_instance(); + $CI->load->helper('database'); + $QueryResult = db_execute_assoc($Query) or safe_die ("ERROR: $QueryResult"); //safe + + preg_match('/FROM (\w+)( |,)/', $Query, $MatchResults); + if ($tagname!='') + { + $TableName=$tagname; + } + else + { + $TableName = $MatchResults[1];; + $TableName = substr($TableName, strlen($CI->db->dbprefix), strlen($TableName)); + } + if ($QueryResult->num_rows()>0) + { + $exclude = array_flip($excludes); //Flip key/value in array for faster checks + $xmlwriter->startElement($TableName); + $xmlwriter->startElement('fields'); + $Columninfo = $QueryResult->list_fields(); + foreach ($Columninfo as $fieldname=>$value) + { + if (!isset($exclude[$fieldname])) $xmlwriter->writeElement('fieldname',$fieldname); + } + $xmlwriter->endElement(); // close columns + $xmlwriter->startElement('rows'); + while ($Row = $QueryResult->result_array()) + { + $xmlwriter->startElement('row'); + foreach ($Row as $Key=>$Value) + { + if (!isset($exclude[$Key])) { + $xmlwriter->startElement($Key); + // Remove invalid XML characters + $xmlwriter->writeCData(preg_replace('/[^\x9\xA\xD\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u','',$Value)); + $xmlwriter->endElement(); + } + } + $xmlwriter->endElement(); // close row + } + $xmlwriter->endElement(); // close rows + $xmlwriter->endElement(); // close tablename + } +} diff --git a/application/helpers/admin/export_structure_xml_helper.php b/application/helpers/admin/export_structure_xml_helper.php new file mode 100644 index 00000000000..b11686dfaec --- /dev/null +++ b/application/helpers/admin/export_structure_xml_helper.php @@ -0,0 +1,217 @@ +load->helper('admin/export_data'); +/** +if (!isset($surveyid)) +{ + $surveyid=returnglobal('sid'); +} + +$clang = $CI->limesurvey_lang; +if (!$surveyid) +{ + + * echo $htmlheader + ."
\n" + ."\n" + ."\t\n" + ."\t\n" + ."
" + .$clang->gT("Export Survey")."
\n" + ."
" + .$clang->gT("Error")."
\n" + .$clang->gT("No SID has been provided. Cannot dump survey")."
\n" + ."
\n" + ."\t
\n" + ."\n"; + + exit; +} */ + +function getXMLStructure($xmlwriter, $exclude=array(),$surveyid) +{ + //global $surveyid; + + $sdump = ""; + $CI =& get_instance(); + if ((!isset($exclude) && $exclude['answers'] !== true) || empty($exclude)) + { + //Answers table + + $aquery = "SELECT ".$CI->db->dbprefix."answers.* + FROM ".$CI->db->dbprefix."answers, ".$CI->db->dbprefix."questions + WHERE ".$CI->db->dbprefix."answers.language=".$CI->db->dbprefix."questions.language + AND ".$CI->db->dbprefix."answers.qid=".$CI->db->dbprefix."questions.qid + AND ".$CI->db->dbprefix."questions.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$aquery); + } + + // Assessments + $query = "SELECT ".$CI->db->dbprefix."assessments.* + FROM ".$CI->db->dbprefix."assessments + WHERE ".$CI->db->dbprefix."assessments.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$query); + + if ((!isset($exclude) && $exclude['conditions'] !== true) || empty($exclude)) + { + //Conditions table + $cquery = "SELECT DISTINCT ".$CI->db->dbprefix."conditions.* + FROM ".$CI->db->dbprefix."conditions, ".$CI->db->dbprefix."questions + WHERE ".$CI->db->dbprefix."conditions.qid=".$CI->db->dbprefix."questions.qid + AND ".$CI->db->dbprefix."questions.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$cquery); + } + + //Default values + $query = "SELECT ".$CI->db->dbprefix."defaultvalues.* + FROM ".$CI->db->dbprefix."defaultvalues JOIN ".$CI->db->dbprefix."questions ON ".$CI->db->dbprefix."questions.qid = ".$CI->db->dbprefix."defaultvalues.qid AND ".$CI->db->dbprefix."questions.sid=$surveyid AND ".$CI->db->dbprefix."questions.language=".$CI->db->dbprefix."defaultvalues.language "; + + BuildXMLFromQuery($xmlwriter,$query); + + // Groups + $gquery = "SELECT * + FROM ".$CI->db->dbprefix."groups + WHERE sid=$surveyid + ORDER BY gid"; + BuildXMLFromQuery($xmlwriter,$gquery); + + //Questions + $qquery = "SELECT * + FROM ".$CI->db->dbprefix."questions + WHERE sid=$surveyid and parent_qid=0 + ORDER BY qid"; + BuildXMLFromQuery($xmlwriter,$qquery); + + //Subquestions + $qquery = "SELECT * + FROM ".$CI->db->dbprefix."questions + WHERE sid=$surveyid and parent_qid>0 + ORDER BY qid"; + BuildXMLFromQuery($xmlwriter,$qquery,'subquestions'); + + //Question attributes + $sBaseLanguage=GetBaseLanguageFromSurveyID($surveyid); + if ($CI->db->dbdriver == 'odbc_mssql' || $CI->db->dbdriver == 'odbtp' || $CI->db->dbdriver == 'mssql_n' || $CI->db->dbdriver =='mssqlnative') + { + $query="SELECT qa.qid, qa.attribute, cast(qa.value as varchar(4000)) as value + FROM ".$CI->db->dbprefix."question_attributes qa JOIN ".$CI->db->dbprefix."questions q ON q.qid = qa.qid AND q.sid={$surveyid} + where q.language='{$sBaseLanguage}' group by qa.qid, qa.attribute, cast(qa.value as varchar(4000))"; + } + else { + $query="SELECT qa.qid, qa.attribute, qa.value + FROM ".$CI->db->dbprefix."question_attributes qa JOIN ".$CI->db->dbprefix."questions q ON q.qid = qa.qid AND q.sid={$surveyid} + where q.language='{$sBaseLanguage}' group by qa.qid, qa.attribute, qa.value"; + } + + BuildXMLFromQuery($xmlwriter,$query,'question_attributes'); + + if ((!isset($exclude) && $exclude['quotas'] !== true) || empty($exclude)) + { + //Quota + $query = "SELECT ".$CI->db->dbprefix."quota.* + FROM ".$CI->db->dbprefix."quota + WHERE ".$CI->db->dbprefix."quota.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$query); + + //1Quota members + $query = "SELECT ".$CI->db->dbprefix."quota_members.* + FROM ".$CI->db->dbprefix."quota_members + WHERE ".$CI->db->dbprefix."quota_members.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$query); + + //Quota languagesettings + $query = "SELECT ".$CI->db->dbprefix."quota_languagesettings.* + FROM ".$CI->db->dbprefix."quota_languagesettings, ".$CI->db->dbprefix."quota + WHERE ".$CI->db->dbprefix."quota.id = ".$CI->db->dbprefix."quota_languagesettings.quotals_quota_id + AND ".$CI->db->dbprefix."quota.sid=$surveyid"; + BuildXMLFromQuery($xmlwriter,$query); + } + + // Surveys + $squery = "SELECT * + FROM ".$CI->db->dbprefix."surveys + WHERE sid=$surveyid"; + //Exclude some fields from the export + BuildXMLFromQuery($xmlwriter,$squery,'',array('owner_id','active','datecreated')); + + // Survey language settings + $slsquery = "SELECT * + FROM ".$CI->db->dbprefix."surveys_languagesettings + WHERE surveyls_survey_id=$surveyid"; + BuildXMLFromQuery($xmlwriter,$slsquery); + +} + +function getXMLData($exclude = array(),$surveyid) +{ + global $dbversionnumber; + $xml = getXMLWriter(); + $xml->openMemory(); + $xml->setIndent(true); + $xml->startDocument('1.0', 'UTF-8'); + $xml->startElement('document'); + $xml->writeElement('LimeSurveyDocType','Survey'); + $xml->writeElement('DBVersion',$dbversionnumber); + $xml->startElement('languages'); + $surveylanguages=GetAdditionalLanguagesFromSurveyID($surveyid); + $surveylanguages[]=GetBaseLanguageFromSurveyID($surveyid); + foreach ($surveylanguages as $surveylanguage) + { + $xml->writeElement('language',$surveylanguage); + } + $xml->endElement(); + getXMLStructure($xml,$exclude,$surveyid); + $xml->endElement(); // close columns + $xml->endDocument(); + return $xml->outputMemory(true); +} +/** +if (!isset($copyfunction)) +{ + $fn = "limesurvey_survey_$surveyid.lss"; + header("Content-Type: text/xml"); + header("Content-Disposition: attachment; filename=$fn"); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header("Pragma: public"); // HTTP/1.0 + echo getXMLData(); + exit; +} */ +?> diff --git a/application/helpers/common_helper.php b/application/helpers/common_helper.php index eddd79bb9a1..204b3139353 100644 --- a/application/helpers/common_helper.php +++ b/application/helpers/common_helper.php @@ -636,25 +636,30 @@ function getQuestions($surveyid,$gid,$selectedqid) */ function getGidPrevious($surveyid, $gid) { - global $CI, $clang; - + //global $CI, $clang; + $CI =& get_instance(); + $clang = $CI->limesurvey_lang; + if (!$surveyid) {$surveyid=returnglobal('sid');} $s_lang = GetBaseLanguageFromSurveyID($surveyid); $CI->load->model('groups_model'); //$gquery = "SELECT gid FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='{$s_lang}' ORDER BY group_order"; $qresult = $CI->groups_model->getGroupID($surveyid,$s_lang); //checked $qrows = $qresult->result_array(); - + $i = 0; $iPrev = -1; - if ($qrows->num_rows() > 0) + + if ($qresult->num_rows() > 0) { + foreach ($qrows as $qrow) { if ($gid == $qrow['gid']) {$iPrev = $i - 1;} $i += 1; } } + if ($iPrev >= 0) {$GidPrev = $qrows[$iPrev]['gid'];} else {$GidPrev = "";} return $GidPrev; @@ -671,7 +676,9 @@ function getGidPrevious($surveyid, $gid) */ function getQidPrevious($surveyid, $gid, $qid) { - global $CI, $clang; + //global $CI, $clang; + $CI =& get_instance(); + $clang = $CI->limesurvey_lang; $s_lang = GetBaseLanguageFromSurveyID($surveyid); $CI->load->model('questions'); //$qquery = 'SELECT * FROM '.db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND language='{$s_lang}' and parent_qid=0 order by question_order"; @@ -680,7 +687,7 @@ function getQidPrevious($surveyid, $gid, $qid) $i = 0; $iPrev = -1; - if ($qrows->num_rows() > 0) + if ($qresult->num_rows() > 0) { foreach ($qrows as $qrow) { @@ -703,8 +710,9 @@ function getQidPrevious($surveyid, $gid, $qid) */ function getGidNext($surveyid, $gid) { - global $CI, $clang; - + //global $CI, $clang; + $CI =& get_instance(); + $clang = $CI->limesurvey_lang; if (!$surveyid) {$surveyid=returnglobal('sid');} $s_lang = GetBaseLanguageFromSurveyID($surveyid); $CI->load->model('groups_model'); @@ -715,7 +723,7 @@ function getGidNext($surveyid, $gid) $GidNext=""; $i = 0; $iNext = 1; - if ($qrows->num_rows() > 0) + if ($qresult->num_rows() > 0) { foreach ($qrows as $qrow) { @@ -1334,11 +1342,14 @@ function getgroupname($gid) */ function getgrouplistlang($gid, $language) { - global $surveyid, $CI, $clang; - $CI->load->config('lsconfig'); + global $surveyid; + $CI =& get_instance(); + $clang = $CI->limesurvey_lang; + $CI->load->model('groups_model'); $groupselecter=""; if (!$surveyid) {$surveyid=returnglobal('sid');} + //$gidquery = "SELECT gid, group_name FROM ".$CI->db->prefix('groups')." WHERE sid=$surveyid AND language='".$language."' ORDER BY group_order"; $gidresult = $CI->groups_model->getGroupAndID($surveyid,$language) or safe_die("Couldn't get group list in common_helper.php
"); //Checked) foreach ($gidresult->result_array() as $gv) diff --git a/application/models/groups_model.php b/application/models/groups_model.php index b52e20b3a92..587ecd658cf 100644 --- a/application/models/groups_model.php +++ b/application/models/groups_model.php @@ -35,7 +35,7 @@ function getGroupID($sid,$language) $this->db->select('gid'); $this->db->where('sid',$sid); $this->db->where('language',$language); - $this->db->where('parent_qid',0); + //$this->db->where('parent_qid',0); $this->db->order_by("group_order","asc"); $data = $this->db->get('groups'); diff --git a/application/models/questions_model.php b/application/models/questions_model.php index 7252ea0d0b9..0df79865ee6 100644 --- a/application/models/questions_model.php +++ b/application/models/questions_model.php @@ -113,5 +113,24 @@ function update($data, $condition=FALSE) $this->db->update('questions', $data); } + + function valueLabel($field,$language) + { + if (isset($field['scale_id'])) + { + $query = "SELECT answers.code, answers.answer, questions.type FROM answers, questions WHERE"; + $query .= " answers.scale_id = ? AND"; + $query .= " answers.qid = ? AND questions.language = ? AND answers.language = ? AND questions.qid = ? ORDER BY sortorder ASC"; + return $this->db->query($query, array((int) $field['scale_id'], $field["qid"], $language, $language, $field['qid'])); + } else + { + $query = "SELECT answers.code, answers.answer, questions.type FROM answers, questions WHERE"; + + $query .= " answers.qid = ? AND questions.language = ? AND answers.language = ? AND questions.qid = ? ORDER BY sortorder ASC"; + return $this->db->query($query, array($field["qid"], $language, $language, $field['qid'])); + } + + + } } \ No newline at end of file diff --git a/application/models/surveys_model.php b/application/models/surveys_model.php index c818b19c897..8b6c5dd8bb4 100644 --- a/application/models/surveys_model.php +++ b/application/models/surveys_model.php @@ -43,4 +43,6 @@ function insertNewSurvey($data) $this->db->insert('surveys', $data); } + + } \ No newline at end of file diff --git a/application/views/admin/Survey/copySurvey_view.php b/application/views/admin/Survey/copySurvey_view.php new file mode 100644 index 00000000000..9aa913e7f7d --- /dev/null +++ b/application/views/admin/Survey/copySurvey_view.php @@ -0,0 +1,64 @@ +
gT("Copy survey"); ?>
+
+ + +
gT("Error"); ?>

+

+ + +
gT("Success"); ?>


+ + gT("Survey import summary"); ?>
+ + +
+ + 0) + { ?> +
gT("Warnings"); ?>:
+
+ + + gT("Copy of survey is completed."); ?> + '> + gT("Go to survey"); ?> + +
\ No newline at end of file diff --git a/application/views/admin/Survey/exportSurveyError_view.php b/application/views/admin/Survey/exportSurveyError_view.php new file mode 100644 index 00000000000..f9a65375d60 --- /dev/null +++ b/application/views/admin/Survey/exportSurveyError_view.php @@ -0,0 +1,13 @@ + +
+ + + +
+ gT("Export Survey"); ?>
+
+ gT("Error"); ?>
+ gT("No SID has been provided. Cannot dump survey"); ?>
+
+
+ \ No newline at end of file diff --git a/application/views/admin/Survey/surveybar.php b/application/views/admin/Survey/surveybar.php index a7cb5cb61b8..d6c3b66e083 100644 --- a/application/views/admin/Survey/surveybar.php +++ b/application/views/admin/Survey/surveybar.php @@ -183,5 +183,89 @@ <?php echo $clang->gT("Token management");?> - + + + \ No newline at end of file