You have not selected a survey!";
exit;
}
// Set language for questions and labels to base language of this survey
$language = GetBaseLanguageFromSurveyID($surveyid);
//Delete any stats files from the temp directory that aren't from today.
deleteNotPattern($tempdir, "STATS_*.png","STATS_".date("d")."*.png");
$statisticsoutput .= "\t\n";
$statisticsoutput .= "
\n"
."\t
".$clang->gT("Quick Statistics")."
\n";
//Get the menubar
$statisticsoutput .= browsemenubar();
$statisticsoutput .= "
\n"
."
\n"
."
"
.""
."".$clang->gT("Filter Settings").""
."
\n"
."
\n"
."\n";
$fieldmap = createFieldMap($surveyid, "full",true);
$selectlist = "";
foreach ($fieldmap as $field)
{
$selectlist .= "\n";
//create a select list of all the possible answers to this question
switch($field['type'])
{
case "Q":
case "S":
case "T":
case "U":
case "N":
case "K":
//text type - don't do anything
break;
case "G":
$thisselect="\n";
$answerselects[]=$thisselect;
$asnames[]=$field['fieldname'];
break;
case "Y":
$thisselect="\n";
$answerselects[]=$thisselect;
$asnames[]=$field['fieldname'];
break;
case "M":
//multiple choice - yes or nothing
$thisselect="\n";
$answerselects[]=$thisselect;
$asnames[]=$field['fieldname'];
break;
case "L":
//list - show possible answers
$query = "SELECT * FROM ".db_table_name("answers")." WHERE qid={$field['qid']} AND language='{$language}'";
$result = db_execute_assoc($query);
$thisselect="\n";
$answerselects[]=$thisselect;
$asnames[]=$field['fieldname'];
break;
} // switch
}
// DISPLAY RESULTS
if (isset($_POST['display']) && $_POST['display'])
{
// Create progress bar
$prb = new ProgressBar();
$prb->pedding = 2; // Bar Pedding
$prb->brd_color = "#404040 #dfdfdf #dfdfdf #404040"; // Bar Border Color
$prb->setFrame(); // set ProgressBar Frame
$prb->frame['left'] = 50; // Frame position from left
$prb->frame['top'] = 80; // Frame position from top
$prb->addLabel('text','txt1',$clang->gT("Please wait ...")); // add Text as Label 'txt1' and value 'Please wait'
$prb->addLabel('percent','pct1'); // add Percent as Label 'pct1'
$prb->addButton('btn1',$clang->gT('Go Back'),'?action=statistics&sid='.$surveyid); // add Button as Label 'btn1' and action '?restart=1'
$process_status = 35;
$prb->show(); // show the ProgressBar
$statisticsoutput .= "\n";
// 1: Get list of questions with answers chosen
$prb->setLabelValue('txt1',$clang->gT('Getting Questions and Answers ...'));
$prb->moveStep(5);
for (reset($_POST); $key=key($_POST); next($_POST)) { $postvars[]=$key;} // creates array of post variable names
foreach ($postvars as $pv)
{
if (in_array($pv, $allfields)) //Only do this if there is actually a value for the $pv
{
$firstletter=substr($pv,0,1);
if ($pv != "sid" && $pv != "display" && $firstletter != "M" && $firstletter != "T" &&
$firstletter != "Q" && $firstletter != "D" && $firstletter != "N" && $firstletter != "K" &&
$pv != "summary" && substr($pv, 0, 2) != "id" && substr($pv, 0, 9) != "datestamp") //pull out just the fieldnames
{
$thisquestion = db_quote_id($pv)." IN (";
foreach ($_POST[$pv] as $condition)
{
$thisquestion .= "'$condition', ";
}
$thisquestion = substr($thisquestion, 0, -2)
. ")";
$selects[]=$thisquestion;
}
elseif (substr($pv, 0, 1) == "M")
{
list($lsid, $lgid, $lqid) = explode("X", $pv);
$aquery="SELECT code FROM ".db_table_name("answers")." WHERE qid=$lqid AND language='{$language}' ORDER BY sortorder, answer";
$aresult=db_execute_num($aquery) or safe_die ("Couldn't get answers $aquery ".$connect->ErrorMsg());
while ($arow=$aresult->FetchRow()) // go through every possible answer
{
if (in_array($arow[0], $_POST[$pv])) // only add condition if answer has been chosen
{
$mselects[]=db_quote_id(substr($pv, 1, strlen($pv)).$arow[0])." = 'Y'";
}
}
if ($mselects)
{
$thismulti=implode(" OR ", $mselects);
$selects[]="($thismulti)";
}
}
elseif (substr($pv, 0, 1) == "N" || substr($pv, 0, 1) == "K")
{
if (substr($pv, strlen($pv)-1, 1) == "G" && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 1, -1))." > ".sanitize_int($_POST[$pv]);
}
if (substr($pv, strlen($pv)-1, 1) == "L" && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 1, -1))." < ".sanitize_int($_POST[$pv]);
}
}
elseif (substr($pv, 0, 2) == "id")
{
if (substr($pv, strlen($pv)-1, 1) == "G" && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 0, -1))." > '".$_POST[$pv]."'";
}
if (substr($pv, strlen($pv)-1, 1) == "L" && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 0, -1))." < '".$_POST[$pv]."'";
}
if (substr($pv, strlen($pv)-1, 1) == "=" && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 0, -1))." = '".$_POST[$pv]."'";
}
}
elseif ((substr($pv, 0, 1) == "T" || substr($pv, 0, 1) == "Q" ) && $_POST[$pv] != "")
{
$selects[]=db_quote_id(substr($pv, 1, strlen($pv)))." like '%".$_POST[$pv]."%'";
}
elseif (substr($pv, 0, 1) == "D" && $_POST[$pv] != "")
{
if (substr($pv, -1, 1) == "=")
{
$selects[]=db_quote_id(substr($pv, 1, strlen($pv)-2))." = '".$_POST[$pv]."'";
}
else
{
if (substr($pv, -1, 1) == "<")
{
$selects[]= db_quote_id(substr($pv, 1, strlen($pv)-2)) . " > '".$_POST[$pv]."'";
}
if (substr($pv, -1, 1) == ">")
{
$selects[]= db_quote_id(substr($pv, 1, strlen($pv)-2)) . " < '".$_POST[$pv]."'";
}
}
}
elseif (substr($pv, 0, 9) == "datestamp")
{
if (substr($pv, -1, 1) == "=" && !empty($_POST[$pv]))
{
$selects[] = db_quote_id('datestamp')." = '".$_POST[$pv]."'";
}
else
{
if (substr($pv, -1, 1) == "<" && !empty($_POST[$pv]))
{
$selects[]= db_quote_id('datestamp')." > '".$_POST[$pv]."'";
}
if (substr($pv, -1, 1) == ">" && !empty($_POST[$pv]))
{
$selects[]= db_quote_id('datestamp')." < '".$_POST[$pv]."'";
}
}
}
} else {
$statisticsoutput .= "";
}
}
// 2: Do SQL query
$prb->setLabelValue('txt1',$clang->gT('Getting Result Count ...'));
$prb->moveStep(35);
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid");
if (incompleteAnsFilterstate() === true) {$query .= " WHERE submitdate is not null";}
$result = db_execute_num($query) or safe_die ("Couldn't get total $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$total=$row[0];}
if (isset($selects) && $selects)
{
if (incompleteAnsFilterstate() === true) {$query .= " AND ";}
else {$query .= " WHERE ";}
$query .= implode(" AND ", $selects);
}
elseif (!empty($_POST['sql']) && !isset($_POST['id=']))
{
$newsql=substr($_POST['sql'], strpos($_POST['sql'], "WHERE")+5, strlen($_POST['sql']));
//$query = $_POST['sql'];
if (incompleteAnsFilterstate() === true) {$query .= " AND ".$newsql;}
else {$query .= " WHERE ".$newsql;}
}
$result=db_execute_num($query) or safe_die("Couldn't get results $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$results=$row[0];}
// 3: Present results including option to view those rows
$statisticsoutput .= " \n
\n"
."\t
"
.$clang->gT("Results")."
\n"
."\t
"
.""
."".$clang->gT("No of records in this query").": $results \n\t\t"
.$clang->gT("Total records in survey").": $total \n";
if ($total)
{
$percent=sprintf("%01.2f", ($results/$total)*100);
$statisticsoutput .= $clang->gT("Percentage of total")
.": $percent% ";
}
$statisticsoutput .= "\n\t\t
\n";
if (isset ($selects) && $selects) {$sql=implode(" AND ", $selects);}
elseif (!empty($newsql)) {$sql = $newsql;}
if (!isset($sql) || !$sql) {$sql="NULL";}
if ($results > 0)
{
$statisticsoutput .= "\t
"
."\t\t
\n"
."\t\t
\n\t
\n";
}
$statisticsoutput .= "
\n";
}
$process_status = 40;
//Show Summary results
if (isset($summary) && $summary)
{
$prb->setLabelValue('txt1',$clang->gT('Generating Summaries ...'));
$prb->moveStep($process_status);
if ($usejpgraph == 1 && isset($jpgraphdir)) //JPGRAPH CODING SUBMITTED BY Pieterjan Heyse
{
//Delete any old temp image files
deletePattern($tempdir, "STATS_".date("d")."X".$currentuser."X".$surveyid."X"."*.png");
}
$runthrough=$summary;
//START Chop up fieldname and find matching questions
$lq = "SELECT DISTINCT qid FROM ".db_table_name("questions")." WHERE sid=$surveyid"; //GET LIST OF LEGIT QIDs FOR TESTING LATER
$lr = db_execute_assoc($lq);
while ($lw = $lr->FetchRow())
{
$legitqids[] = $lw['qid']; //this creates an array of question id's'
}
//Finished collecting legitqids
foreach ($runthrough as $rt)
{
if ($process_status < 100) $process_status++;
$prb->moveStep($process_status);
// 1. Get answers for question ##############################################################
if (substr($rt, 0, 1) == "M" || substr($rt, 0, 1) == "J") //MULTIPLE OPTION, THEREFORE MULTIPLE FIELDS.
{
list($qsid, $qgid, $qqid) = explode("X", substr($rt, 1, strlen($rt)), 3);
$nquery = "SELECT title, type, question, lid, other FROM ".db_table_name("questions")." WHERE language='{$language}' and qid='$qqid'";
$nresult = db_execute_num($nquery) or safe_die ("Couldn't get question $nquery ".$connect->ErrorMsg());
while ($nrow=$nresult->FetchRow())
{
$qtitle=$nrow[0];
$qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]);
$qlid=$nrow[3];
$qother=$nrow[4];
}
//1. Get list of answers
$query="SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qqid' AND language='{$language}' ORDER BY sortorder, answer";
$result=db_execute_num($query) or safe_die("Couldn't get list of answers for multitype $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow())
{
$mfield=substr($rt, 1, strlen($rt))."$row[0]";
$alist[]=array("$row[0]", "$row[1]", $mfield);
}
if ($qother == "Y")
{
$mfield=substr($rt, 1, strlen($rt))."other";
$alist[]=array($clang->gT("Other"), $clang->gT("Other"), $mfield);
}
}
elseif (substr($rt, 0, 1) == "T" || substr($rt, 0, 1) == "S") //Short and long text
{
list($qsid, $qgid, $qqid) = explode("X", substr($rt, 1, strlen($rt)), 3);
$nquery = "SELECT title, type, question, other FROM ".db_table_name("questions")." WHERE qid='$qqid' AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die("Couldn't get text question $nquery ".$connect->ErrorMsg());
while ($nrow=$nresult->FetchRow())
{
$qtitle=strip_tags($nrow[0]); $qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]);
}
$mfield=substr($rt, 1, strlen($rt));
$alist[]=array("Answers", $clang->gT("Answer"), $mfield);
$alist[]=array("NoAnswer", $clang->gT("No answer"), $mfield);
}
elseif (substr($rt, 0, 1) == "Q") //Multiple short text
{
list($qsid, $qgid, $qqid) = explode("X", substr($rt, 1, strlen($rt)), 3);
$tmpqid=substr($qqid, 0, strlen($qqid)-1);
while (!in_array ($tmpqid,$legitqids)) $tmpqid=substr($tmpqid, 0, strlen($tmpqid)-1);
$qidlength=strlen($tmpqid);
$qaid=substr($qqid, $qidlength, strlen($qqid)-$qidlength);
$nquery = "SELECT title, type, question, other FROM ".db_table_name("questions")." WHERE qid='".substr($qqid, 0, $qidlength)."' AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die("Couldn't get text question $nquery ".$connect->ErrorMsg());
$count = substr($qqid, strlen($qqid)-1);
while ($nrow=$nresult->FetchRow())
{
$qtitle=strip_tags($nrow[0]).'-'.$count; $qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]);
}
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qqid' AND code='$qaid' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details (Array 5p Q) $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$atext=$qrow[1];
}
$qtitle .= " [$atext]";
$mfield=substr($rt, 1, strlen($rt));
$alist[]=array("Answers", $clang->gT("Answer"), $mfield);
$alist[]=array("NoAnswer", $clang->gT("No answer"), $mfield);
}
elseif (substr($rt, 0, 1) == "R") //RANKING OPTION THEREFORE CONFUSING
{
$lengthofnumeral=substr($rt, strpos($rt, "-")+1, 1);
list($qsid, $qgid, $qqid) = explode("X", substr($rt, 1, strpos($rt, "-")-($lengthofnumeral+1)), 3);
$nquery = "SELECT title, type, question FROM ".db_table_name("questions")." WHERE qid='$qqid' AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die ("Couldn't get question $nquery ".$connect->ErrorMsg());
while ($nrow=$nresult->FetchRow())
{
$qtitle=strip_tags($nrow[0]). " [".substr($rt, strpos($rt, "-")-($lengthofnumeral), $lengthofnumeral)."]";
$qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]). "[".$clang->gT("Ranking")." ".substr($rt, strpos($rt, "-")-($lengthofnumeral), $lengthofnumeral)."]";
}
$query="SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qqid' AND language='{$language}' ORDER BY sortorder, answer";
$result=db_execute_num($query) or safe_die("Couldn't get list of answers for multitype $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow())
{
$mfield=substr($rt, 1, strpos($rt, "-")-1);
$alist[]=array("$row[0]", "$row[1]", $mfield);
}
}
elseif (substr($rt, 0, 1) == "N" || substr($rt, 0, 1) == "K") //NUMERICAL TYPE
{
if (substr($rt, -1) == "G" || substr($rt, -1) == "L" || substr($rt, -1) == "=")
{
//DO NOTHING
}
else
{
list($qsid, $qgid, $qqid) = explode("X", $rt, 3);
if(substr($rt, 0, 1) == "K")
{ // This is a multiple numerical question so we need to strip of the answer id to find the question title
$tmpqid=substr($qqid, 0, strlen($qqid)-1);
while (!in_array ($tmpqid,$legitqids)) $tmpqid=substr($tmpqid, 0, strlen($tmpqid)-1);
$qidlength=strlen($tmpqid);
$qaid=substr($qqid, $qidlength, strlen($qqid)-$qidlength);
$nquery = "SELECT title, type, question, qid, lid
FROM ".db_table_name("questions")."
WHERE qid='".substr($qqid, 0, $qidlength)."'
AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die("Couldn't get text question $nquery ".$connect->ErrorMsg());
} else {
$nquery = "SELECT title, type, question, qid, lid FROM ".db_table_name("questions")." WHERE qid='$qqid' AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die ("Couldn't get question $nquery ".$connect->ErrorMsg());
}
while ($nrow=$nresult->FetchRow())
{
$qtitle=strip_tags($nrow[0]);
$qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]);
$qiqid=$nrow[3];
$qlid=$nrow[4];
}
//Get answer texts for multiple numerical
if(substr($rt, 0, 1) == "K")
{
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qaid' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details (Array 5p Q) $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$atext=$qrow[1];
}
$qtitle .= " [$atext]";
}
$statisticsoutput .= " \n
\n"
."\t
".$clang->gT("Field Summary for")." $qtitle:"
."
\n"
."\t
$qquestion
\n"
."\t
\n\t\t
"
.$clang->gT("Calculation")."
\n"
."\t\t
"
.$clang->gT("Result")."
\n"
."\t
\n";
$fieldname=substr($rt, 1, strlen($rt));
if ($connect->databaseType == 'odbc_mssql')
{ $query = "SELECT STDEVP(".db_quote_id($fieldname)."*1) as stdev"; }
else
{ $query = "SELECT STDDEV(".db_quote_id($fieldname).") as stdev"; }
$query .= ", SUM(".db_quote_id($fieldname)."*1) as sum";
$query .= ", AVG(".db_quote_id($fieldname)."*1) as average";
$query .= ", MIN(".db_quote_id($fieldname)."*1) as minimum";
$query .= ", MAX(".db_quote_id($fieldname)."*1) as maximum";
if ($connect->databaseType == 'odbc_mssql')
{ $query .= " FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($fieldname)." IS NOT NULL AND (".db_quote_id($fieldname)." NOT LIKE ' ')"; }
else
{ $query .= " FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($fieldname)." IS NOT NULL AND (".db_quote_id($fieldname)." != ' ')"; }
if (incompleteAnsFilterstate() === true) {$query .= " AND submitdate is not null";}
if ($sql != "NULL") {$query .= " AND $sql";}
$result=db_execute_assoc($query) or safe_die("Couldn't do maths testing $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow())
{
$showem[]=array($clang->gT("Sum"), $row['sum']);
$showem[]=array($clang->gT("Standard Deviation"), $row['stdev']);
$showem[]=array($clang->gT("Average"), $row['average']);
$showem[]=array($clang->gT("Minimum"), $row['minimum']);
$maximum=$row['maximum']; //we're going to put this after the quartiles for neatness
$minimum=$row['minimum'];
}
//CALCULATE QUARTILES
$query ="SELECT ".db_quote_id($fieldname)." FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($fieldname)." IS NOT null AND ".db_quote_id($fieldname)." != ' '";
if (incompleteAnsFilterstate() === true) {$query .= " AND submitdate is not null";}
if ($sql != "NULL") {$query .= " AND $sql";}
$result=$connect->Execute($query) or safe_die("Disaster during median calculation $query ".$connect->ErrorMsg());
$querystarter="SELECT ".db_quote_id($fieldname)." FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($fieldname)." IS NOT null AND ".db_quote_id($fieldname)." != ' '";
if (incompleteAnsFilterstate() === true) {$querystarter .= " AND submitdate is not null";}
if ($sql != "NULL") {$querystarter .= " AND $sql";}
$medcount=$result->RecordCount();
array_unshift($showem, array($clang->gT("Count"), $medcount));
if ($medcount>1) // Calculating only makes sense with more than one result
{
//1ST QUARTILE (Q1)
$q1=(1/4)*($medcount+1);
$q1b=(int)((1/4)*($medcount+1));
$q1c=$q1b-1;
$q1diff=$q1-$q1b;
$total=0;
if ($q1c<1) {$q1c=1;$lastnumber=0;} // fix if there are too few values to evaluate.
if ($q1 != $q1b)
{
//ODD NUMBER
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1 ";
$result=db_select_limit_assoc($query, $q1c, 2) or safe_die("1st Quartile query failed ".$connect->ErrorMsg());
while ($row=$result->FetchRow())
{
if ($total == 0) {$total=$total-$row[$fieldname];}
else {$total=$total+$row[$fieldname];}
$lastnumber=$row[$fieldname];
}
$q1total=$lastnumber-(1-($total*$q1diff));
if ($q1total < $minimum) {$q1total=$minimum;}
$showem[]=array($clang->gT("1st Quartile (Q1)"), $q1total);
}
else
{
//EVEN NUMBER
//TODO: See note above for 'ODD'
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1 ";
$result=db_select_limit_assoc($query,1, $q1c) or safe_die ("1st Quartile query failed ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$showem[]=array("1st Quartile (Q1)", $row[$fieldname]);}
}
$total=0;
//MEDIAN (Q2)
$median=(1/2)*($medcount+1);
$medianb=(int)((1/2)*($medcount+1));
$medianc=$medianb-1;
$mediandiff=$median-$medianb;
if ($median != (int)((($medcount+1)/2)-1))
{
//remainder
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1 ";
$result=db_select_limit_assoc($query,2, $medianc) or safe_die("What a complete mess with the remainder $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$total=$total+$row[$fieldname];}
$showem[]=array($clang->gT("2nd Quartile (Median)"), $total/2);
}
else
{
//EVEN NUMBER
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1 ";
$result=db_select_limit_assoc($query,1, $medianc) or safe_die("What a complete mess $query ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$showem[]=array("Median Value", $row[$fieldname]);}
}
$total=0;
//3RD QUARTILE (Q3)
$q3=(3/4)*($medcount+1);
$q3b=(int)((3/4)*($medcount+1));
$q3c=$q3b-1;
$q3diff=$q3-$q3b;
if ($q3 != $q3b)
{
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1 ";
$result = db_select_limit_assoc($query,2,$q3c) or safe_die("3rd Quartile query failed ".$connect->ErrorMsg());
$lastnumber='';
while ($row=$result->FetchRow())
{
if ($total == 0) {$total=$total-$row[$fieldname];}
else {$total=$total+$row[$fieldname];}
if (!$lastnumber) {$lastnumber=$row[$fieldname];}
}
$q3total=$lastnumber+($total*$q3diff);
if ($q3total < $maximum) {$q1total=$maximum;}
$showem[]=array($clang->gT("3rd Quartile (Q3)"), $q3total);
}
else
{
$query = $querystarter . " ORDER BY ".db_quote_id($fieldname)."*1";
$result = db_select_limit_assoc($query,1, $q3c) or safe_die("3rd Quartile even query failed ".$connect->ErrorMsg());
while ($row=$result->FetchRow()) {$showem[]=array("3rd Quartile (Q3)", $row[$fieldname]);}
}
$total=0;
$showem[]=array($clang->gT("Maximum"), $maximum);
foreach ($showem as $shw)
{
$statisticsoutput .= "\t
\n"
."\t\t
$shw[0]
\n"
."\t\t
$shw[1]
\n"
."\t
\n";
}
$statisticsoutput .= "\t
\n"
."\t\t
\n"
."\t\t\t".$clang->gT("Null values are ignored in calculations")." \n"
."\t\t\t".$clang->gT("Q1 and Q3 calculated using")." ".$clang->gT("minitab method").""
."\n"
."\t\t
".$clang->gT("Not enough values for calculation")."
\n"
."\t
\n\n";
unset($showem);
}
}
}
elseif (substr($rt, 0, 2) == "id" || substr($rt, 0, 9) == "datestamp" || (substr($rt, 0, 1) == "D"))
{
}
else // NICE SIMPLE SINGLE OPTION ANSWERS
{
$fieldmap=createFieldMap($surveyid);
$fielddata=arraySearchByKey($rt, $fieldmap, "fieldname", 1);
$qsid=$fielddata['sid'];
$qgid=$fielddata['gid'];
$qqid=$fielddata['qid'];
$qanswer=$fielddata['aid'];
$qtype=$fielddata['type'];
$qastring=$fielddata['question'];
$rqid=$qqid;
$nquery = "SELECT title, type, question, qid, lid, lid1, other FROM ".db_table_name("questions")." WHERE qid='{$rqid}' AND language='{$language}'";
$nresult = db_execute_num($nquery) or safe_die ("Couldn't get question $nquery ".$connect->ErrorMsg());
while ($nrow=$nresult->FetchRow())
{
$qtitle=strip_tags($nrow[0]);
$qtype=$nrow[1];
$qquestion=strip_tags($nrow[2]);
$qiqid=$nrow[3];
$qlid=$nrow[4];
$qlid1=$nrow[5];
$qother=$nrow[6];
}
$alist[]=array("", $clang->gT("No answer"));
switch($qtype)
{
case "A": //Array of 5 point choices
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qanswer' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details (Array 5p Q) $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
for ($i=1; $i<=5; $i++)
{
$alist[]=array("$i", "$i");
}
$atext=$qrow[1];
}
$qquestion .= " \n[".$atext."]";
$qtitle .= "($qanswer)";
break;
case "B": //Array of 10 point choices
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qanswer' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details (Array 10p Q) $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
for ($i=1; $i<=10; $i++)
{
$alist[]=array("$i", "$i");
}
$atext=$qrow[1];
}
$qquestion .= " \n[".$atext."]";
$qtitle .= "($qanswer)";
break;
case "C": //Array of Yes/No/$clang->gT("Uncertain")
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qanswer' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$alist[]=array("Y", $clang->gT("Yes"));
$alist[]=array("N", $clang->gT("No"));
$alist[]=array("U", $clang->gT("Uncertain"));
$atext=$qrow[1];
}
$qquestion .= " \n[".$atext."]";
$qtitle .= "($qanswer)";
break;
case "E": //Array of Yes/No/$clang->gT("Uncertain")
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qanswer' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$alist[]=array("I", $clang->gT("Increase"));
$alist[]=array("S", $clang->gT("Same"));
$alist[]=array("D", $clang->gT("Decrease"));
$atext=$qrow[1];
}
$qquestion .= " \n[".$atext."]";
$qtitle .= "($qanswer)";
break;
case "F": //Array of Flexible
case "H": //Array of Flexible by Column
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qiqid' AND code='$qanswer' AND language='{$language}' ORDER BY sortorder, answer";
$qresult=db_execute_num($qquery) or safe_die ("Couldn't get answer details $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$fquery = "SELECT * FROM ".db_table_name("labels")." WHERE lid='{$qlid}' AND language='{$language}'ORDER BY sortorder, code";
$fresult = db_execute_assoc($fquery);
while ($frow=$fresult->FetchRow())
{
$alist[]=array($frow['code'], strip_tags($frow['title']));
}
$atext=$qrow[1];
}
$qquestion .= " \n[".$atext."]";
$qtitle .= "($qanswer)";
break;
case "G": //Gender
$alist[]=array("F", $clang->gT("Female"));
$alist[]=array("M", $clang->gT("Male"));
break;
case "Y": //Yes\No
$alist[]=array("Y", $clang->gT("Yes"));
$alist[]=array("N", $clang->gT("No"));
break;
case "I": //Language
// Using previously defined $survlangs array of language codes
foreach ($survlangs as $availlang)
{$alist[]=array($availlang, getLanguageNameFromCode($availlang,false));}
break;
case "5": //5 Point
for ($i=1; $i<=5; $i++)
{
$alist[]=array("$i", "$i");
}
break;
case "W":
case "Z":
$fquery = "SELECT * FROM ".db_table_name("labels")." WHERE lid='{$qlid}' AND language='{$language}' ORDER BY sortorder, code";
$fresult = db_execute_assoc($fquery);
while ($frow=$fresult->FetchRow())
{
$alist[]=array($frow['code'], strip_tags($frow['title']));
}
if ($qother == "Y")
{
$alist[]=array($clang->gT("Other"),$clang->gT("Other"),$fielddata['fieldname'].'other');
}
break;
case "1":
$qidattributes=getQuestionAttributes($qqid);
if (substr($rt,-1,1) == 0)
{
// Label 1
$fquery = "SELECT * FROM ".db_table_name("labels")." WHERE lid='{$qlid}' AND language='{$language}' ORDER BY sortorder, code";
if ($dsheaderA=arraySearchByKey("dualscale_headerA", $qidattributes, "attribute", 1))
{
$labelheader= "[".$dsheaderA['value']."]";
}
else
{
$labelheader ='';
}
$labelno = "Label 1";
}
else
{
// Label 2
$fquery = "SELECT * FROM ".db_table_name("labels")." WHERE lid='{$qlid1}' AND language='{$language}' ORDER BY sortorder, code";
if ($dsheaderB=arraySearchByKey("dualscale_headerB", $qidattributes, "attribute", 1))
{
$labelheader= "[" . $dsheaderB['value'] . "]";
}
else
{
$labelheader ='';
}
$labelno = "Label 2";
}
$fresult = db_execute_assoc($fquery);
while ($frow=$fresult->FetchRow())
{
$alist[]=array($frow['code'], strip_tags($frow['title']));
}
$qtitle = $qtitle." [".$qanswer."][".$labelno."]";
$qquestion = $qastring .$labelheader;
break;
default:
$qquery = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$qqid' AND language='{$language}' ORDER BY sortorder, answer";
$qresult = db_execute_num($qquery) or safe_die ("Couldn't get answers list $qquery ".$connect->ErrorMsg());
while ($qrow=$qresult->FetchRow())
{
$alist[]=array("$qrow[0]", "$qrow[1]");
}
if (($qtype == "L" || $qtype == "!") && $qother == "Y")
{
$alist[]=array($clang->gT("Other"),$clang->gT("Other"),$fielddata['fieldname'].'other');
}
}
}
//foreach ($alist as $al) {$statisticsoutput .= "$al[0] - $al[1] ";} //debugging line
//foreach ($fvalues as $fv) {$statisticsoutput .= "$fv | ";} //debugging line
//2. Collect and Display results #######################################################################
if (isset($alist) && $alist) //Make sure there really is an answerlist, and if so:
{
$statisticsoutput .= "
\n"
."\t
"
.$clang->gT("Field Summary for")." $qtitle:"
."
\n"
."\t
"
."$qquestion
\n"
."\t
\n\t\t
"
."".$clang->gT("Answer")."
\n"
."\t\t
"
."".$clang->gT("Count")."
\n"
."\t\t
"
."".$clang->gT("Percentage")."
\n"
."\t
\n";
$TotalCompleted = 0; // this will count the asnwers considered completed
foreach ($alist as $al)
{
if (isset($al[2]) && $al[2]) //picks out alist that come from the multiple list above
{
if ($al[1] == $clang->gT("Other"))
{
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid")." WHERE ";
$query .= ($connect->databaseType == "mysql")? db_quote_id($al[2])." != ''" : "NOT (".db_quote_id($al[2])." LIKE '')";
}
elseif ($qtype == "U" || $qtype == "T" || $qtype == "S" || $qtype == "Q")
{
if($al[0]=="Answers")
{
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid")." WHERE ";
$query .= ($connect->databaseType == "mysql")? db_quote_id($al[2])." != ''" : "NOT (".db_quote_id($al[2])." LIKE '')";
}
elseif($al[0]=="NoAnswer")
{
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid")." WHERE (".db_quote_id($al[2])." IS NULL OR ";
$query .= ($connect->databaseType == "mysql")? db_quote_id($al[2])." = '')" : " (".db_quote_id($al[2])." LIKE ''))";
}
}
else
{
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($al[2])." =";
if (substr($rt, 0, 1) == "R")
{
$query .= " '$al[0]'";
}
else
{
$query .= " 'Y'";
}
}
}
else
{
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid")." WHERE ".db_quote_id($rt)." = '$al[0]'";
}
if (incompleteAnsFilterstate() === true) {$query .= " AND submitdate is not null";}
if ($sql != "NULL") {$query .= " AND $sql";}
$result=db_execute_num($query) or safe_die ("Couldn't do count of values $query ".$connect->ErrorMsg());
// $statisticsoutput .= "\n\n\n";
while ($row=$result->FetchRow()) // this just extracts the data, after we present
{
$TotalCompleted += $row[0];
if ($al[0] == "")
{$fname=$clang->gT("No answer");}
elseif ($al[0] == $clang->gT("Other") || $al[0] == "Answers")
{$fname="$al[1] ";}
elseif ($qtype == "S" || $qtype == "U" || $qtype == "T" || $qtype == "Q")
{
if ($al[0] == "Answers")
{
$fname= "$al[1] ";
}
elseif ($al[0] == "NoAnswer")
{
$fname= "$al[1]";
}
}
else
{$fname="$al[1] ($al[0])";}
if ($results > 0)
{
$gdata[] = ($row[0]/$results)*100;
} else
{
$gdata[] = "N/A";
}
$grawdata[]=$row[0];
$label[]=$fname;
$justcode[]=$al[0];
$lbl[] = wordwrap(strip_tags($fname), 20, "\n");
}
}
if ((incompleteAnsFilterstate() === false) and ($qtype != "M") and ($qtype != "P"))
{
if (isset($_POST["noncompleted"]) and ($_POST["noncompleted"] == "on"))
{
$i=0;
while (isset($gdata[$i]))
{
if ($gdata[$i] != "N/A") { $gdata[$i] = ($grawdata[$i]/$TotalCompleted)*100; }
$i++;
}
}
else
{
$TotalIncomplete = $results - $TotalCompleted;
$fname=$clang->gT("Non completed");
if ($results > 0)
{
$gdata[] = ($TotalIncomplete/$results)*100;
} else
{
$gdata[] = "N/A";
}
$grawdata[]=$TotalIncomplete;
$label[]= $fname;
$justcode[]=$fname;
$lbl[] = wordwrap(strip_tags($fname), 20, "\n");
}
}
$i=0;
while (isset($gdata[$i]))
{
$statisticsoutput .= "\t
";
//First, lets delete any earlier graphs from the tmp directory
//$gdata and $lbl are arrays built at the end of the last section
//that contain the values, and labels for the data we are about
//to send to jpgraph.
if ($qtype == "M" || $qtype == "P") { //Bar Graph
$graph = new Graph(640,320,'png');
$graph->SetScale("textint");
$graph->img->SetMargin(50,50,50,50);
$graph->xaxis->SetTickLabels($justcode);
$graph->xaxis->SetFont(constant($jpgraphfont), FS_NORMAL, 8);
$graph->xaxis->SetColor("black");
// $graph->xaxis->title->Set($clang->gT("Code"));
$graph->xaxis->title->SetFont(constant($jpgraphfont), FS_BOLD, 9);
$graph->xaxis->title->SetColor("black");
$graph->yaxis->SetFont(constant($jpgraphfont), FS_NORMAL, 8);
$graph->yaxis->SetColor("black");
$graph->yaxis->title->Set($clang->gT("Count")." / $results");
$graph->yaxis->title->SetFont(constant($jpgraphfont), FS_BOLD, 9);
$graph->yaxis->title->SetColor("black");
//$graph->Set90AndMargin();
} else { //Pie Charts
$i = 0;
foreach ($gdata as $data)
{
if ($data != 0){$i++;}
}
$totallines=$i;
if ($totallines>15) {
$gheight=320+(6.7*($totallines-15));
$fontsize=7;
$legendtop=0.01;
$setcentrey=0.5/(($gheight/320));
} else {
$gheight=320;
$fontsize=8;
$legendtop=0.07;
$setcentrey=0.5;
}
$graph = new PieGraph(640,$gheight,'png');
$graph->legend->SetFont(constant($jpgraphfont), FS_NORMAL, $fontsize);
$graph->legend->SetPos(0.015, $legendtop, 'right', 'top');
$graph->legend->SetFillColor("white");
global $jpgraph_antialiasing;
if ($jpgraph_antialiasing == 1) $graph->SetAntiAliasing();
}
$graph->title->SetColor("#EEEEEE");
$graph->SetMarginColor("#FFFFFF");
// Set A title for the plot
//$graph->title->Set($qquestion);
$graph->title->SetFont(constant($jpgraphfont),FS_BOLD,13);
// Create pie plot
if ($qtype == "M" || $qtype == "P") { //Bar Graph
$p1 = new BarPlot($grawdata);
$p1->SetWidth(0.8);
$p1->SetValuePos("center");
$p1->SetFillColor("#4f81bd");
if (!in_array(0, $grawdata)) { //don't show shadows if any of the values are 0 - jpgraph bug
$p1->SetShadow();
}
$p1->value->Show();
$p1->value->SetFont(constant($jpgraphfont),FS_BOLD,8);
$p1->value->SetColor("#FFFFFF");
} else { //Pie Chart
// this block is to remove the items with value == 0
$i = 0;
while (isset ($gdata[$i]))
{
if ($gdata[$i] == 0)
{
array_splice ($gdata, $i, 1);
array_splice ($lbl, $i, 1);
}
else
{$i++;}
}
$p1 = new PiePlot3d($gdata);
// $statisticsoutput .= "
";print_r($lbl);$statisticsoutput .= "
";
// $statisticsoutput .= "
";print_r($gdata);$statisticsoutput .= "
";
$p1->SetTheme("earth");
$p1->SetLegends($lbl);
$p1->SetSize(200);
$p1->SetCenter(0.375,$setcentrey);
$p1->value->SetColor("#000000");
$p1->value->SetFont(constant($jpgraphfont),FS_NORMAL,12);
// Set how many pixels each slice should explode
//$p1->Explode(array(0,15,15,25,15));
}
if (!isset($ci)) {$ci=0;}
$ci++;
$graph->Add($p1);
$gfilename="STATS_".date("d")."X".$currentuser."X".$surveyid."X".$ci.date("His").".png";
$graph->Stroke($tempdir."/".$gfilename);
$statisticsoutput .= "
";
////// PIE ALL DONE
}
$statisticsoutput .= "
";
}
unset($gdata);
unset($grawdata);
unset($label);
unset($lbl);
unset($justcode);
unset ($alist);
}
$statisticsoutput .= "  \n";
}
if (isset($prb))
{
$prb->setLabelValue('txt1',$clang->gT('Completed'));
$prb->moveStep(100);
$prb->hide();
}
function deletePattern($dir, $pattern = "")
{
$deleted = false;
$pattern = str_replace(array("\*","\?"), array(".*","."), preg_quote($pattern));
if (substr($dir,-1) != "/") $dir.= "/";
if (is_dir($dir))
{
$d = opendir($dir);
while ($file = readdir($d))
{
if (is_file($dir.$file) && ereg("^".$pattern."$", $file))
{
if (unlink($dir.$file))
{
$deleted[] = $file;
}
}
}
closedir($d);
return $deleted;
}
else return 0;
}
function deleteNotPattern($dir, $matchpattern, $pattern = "")
{
$deleted = false;
$pattern = str_replace(array("\*","\?"), array(".*","."), preg_quote($pattern));
$matchpattern = str_replace(array("\*","\?"), array(".*","."), preg_quote($matchpattern));
if (substr($dir,-1) != "/") $dir.= "/";
if (is_dir($dir))
{
$d = opendir($dir);
while ($file = readdir($d))
{
if (is_file($dir.$file) && ereg("^".$matchpattern."$", $file) && !ereg("^".$pattern."$", $file))
{
if (unlink($dir.$file))
{
$deleted[] = $file;
}
}
}
closedir($d);
return $deleted;
}
else return 0;
}
function showSpeaker($hinttext)
{
global $imagefiles, $clang;
$reshtml= "gT("Question","js").": $hinttext')\" />";
return $reshtml;
}
function countLines($array)
{
//$totalelements=count($array);
$totalnewlines=0;
foreach ($array as $ar)
{
$totalnewlines=$totalnewlines+substr_count($ar, "\n")+1;
}
$totallines=$totalnewlines+count($array);
return $totallines;
}
?>