Add survey charts. Add cidReq to survey URL. - refs BT#9068

1.10.x
Imanol Losada 11 years ago
parent 321ef5d3a3
commit 9bb3e1377a
  1. 9255
      main/inc/lib/javascript/d3/d3.v3.4.8.js
  2. 9
      main/survey/reporting.php
  3. 191
      main/survey/survey.lib.php

File diff suppressed because it is too large Load Diff

@ -17,6 +17,7 @@ $language_file = 'survey';
require_once '../inc/global.inc.php';
require_once 'survey.lib.php';
$this_section = SECTION_COURSES;
$cidReq = Security::remove_XSS(api_get_cidreq());
$survey_id = intval($_GET['survey_id']);
$survey_data = survey_manager::get_survey($survey_id);
@ -138,10 +139,10 @@ echo '</div>';
// Content
if (!isset($_GET['action']) || isset($_GET['action']) && $_GET['action'] == 'overview') {
$myweb_survey_id = $survey_id;
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=questionreport&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_question.gif',get_lang('DetailedReportByQuestion')).' '.get_lang('DetailedReportByQuestion').'</a></div><div class="sectioncomment">'.get_lang('DetailedReportByQuestionDetail').' </div>';
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_user.gif',get_lang('DetailedReportByUser')).' '.get_lang('DetailedReportByUser').'</a></div><div class="sectioncomment">'.get_lang('DetailedReportByUserDetail').'.</div>';
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=comparativereport&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_comparative.gif',get_lang('ComparativeReport')).' '.get_lang('ComparativeReport').'</a></div><div class="sectioncomment">'.get_lang('ComparativeReportDetail').'.</div>';
echo '<div class="sectiontitle"><a href="reporting.php?action=completereport&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_complete.gif',get_lang('CompleteReport')).' '.get_lang('CompleteReport').'</a></div><div class="sectioncomment">'.get_lang('CompleteReportDetail').'</div>';
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=questionreport&amp;'.$cidReq.'&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_question.gif',get_lang('DetailedReportByQuestion')).' '.get_lang('DetailedReportByQuestion').'</a></div><div class="sectioncomment">'.get_lang('DetailedReportByQuestionDetail').' </div>';
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=userreport&amp;'.$cidReq.'&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_user.gif',get_lang('DetailedReportByUser')).' '.get_lang('DetailedReportByUser').'</a></div><div class="sectioncomment">'.get_lang('DetailedReportByUserDetail').'.</div>';
echo '<div class="sectiontitle"><a href="'.api_get_path(WEB_CODE_PATH).'survey/reporting.php?action=comparativereport&amp;'.$cidReq.'&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_comparative.gif',get_lang('ComparativeReport')).' '.get_lang('ComparativeReport').'</a></div><div class="sectioncomment">'.get_lang('ComparativeReportDetail').'.</div>';
echo '<div class="sectiontitle"><a href="reporting.php?action=completereport&amp;'.$cidReq.'&amp;survey_id='.$myweb_survey_id.'">'.Display::return_icon('survey_reporting_complete.gif',get_lang('CompleteReport')).' '.get_lang('CompleteReport').'</a></div><div class="sectioncomment">'.get_lang('CompleteReportDetail').'</div>';
}
// Footer

@ -2964,6 +2964,20 @@ class SurveyUtil
$data[$row['option_id']] = $row;
}
$chartData = array();
foreach ($options as $option) {
$optionText = strip_tags($option['option_text']);
$optionText = html_entity_decode($optionText);
$votes = $data[$option['question_option_id']]['total'];
if (empty($votes)) {
$votes = '0';
}
array_push($chartData, array('option' => $optionText, 'votes' => $votes));
}
self::drawChart($chartData);
$htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
// displaying the table: headers
echo '<table class="data_table">';
@ -3066,6 +3080,28 @@ class SurveyUtil
$number_of_answers += $row['total'];
$data[$row['option_id']][$row['value']] = $row;
}
$chartData = array();
foreach ($options as $option) {
$optionText = strip_tags($option['option_text']);
$optionText = html_entity_decode($optionText);
for ($i = 1; $i <= $question['max_value']; $i++) {
$votes = $data[$option['question_option_id']][$i]['total'];
if (empty($votes)) {
$votes = '0';
}
array_push(
$chartData,
array(
'serie' => $optionText,
'option' => $i,
'votes' => $votes
)
);
}
}
self::drawChart($chartData, true);
// Displaying the table: headers
echo '<table class="data_table">';
echo ' <tr>';
@ -3874,86 +3910,131 @@ class SurveyUtil
$answers_y = SurveyUtil::get_answers_of_question_by_user($_GET['survey_id'], $_GET['yaxis']);
// Displaying the table
echo '<table border="1" class="data_table">';
$tableHtml = '<table border="1" class="data_table">';
$xOptions = array();
// The header
echo ' <tr>';
$tableHtml .= ' <tr>';
for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
if ($ii == 0) {
echo ' <th>&nbsp;</th>';
$tableHtml .= ' <th>&nbsp;</th>';
} else {
if ($question_x['type'] == 'score') {
for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
echo ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
$tableHtml .= ' <th>'.$question_x['answers'][($ii-1)].'<br />'.$x.'</th>';
}
$x = '';
} else {
echo ' <th>'.$question_x['answers'][($ii-1)].'</th>';
$tableHtml .= ' <th>'.$question_x['answers'][($ii-1)].'</th>';
}
$optionText = strip_tags($question_x['answers'][$ii-1]);
$optionText = html_entity_decode($optionText);
array_push($xOptions, trim($optionText));
}
}
echo ' </tr>';
$tableHtml .= ' </tr>';
$chartData = array();
// The main part
for ($ij = 0; $ij < count($question_y['answers']); $ij++) {
$currentYQuestion = strip_tags($question_y['answers'][$ij]);
$currentYQuestion = html_entity_decode($currentYQuestion);
// The Y axis is a scoring question type so we have more rows than the options (actually options * maximum score)
if ($question_y['type'] == 'score') {
for ($y = 1; $y <= $question_y['maximum_score']; $y++) {
echo ' <tr>';
$tableHtml .= ' <tr>';
for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
if ($question_x['type'] == 'score') {
for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
if ($ii == 0) {
echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
$tableHtml .= ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
break;
} else {
echo ' <td align="center">';
echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
echo '</td>';
$tableHtml .= ' <td align="center">';
$votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, $y);
$tableHtml .= $votes;
array_push(
$chartData,
array(
'serie' => array($currentYQuestion, $xOptions[$ii-1]),
'option' => $x,
'votes' => $votes
)
);
$tableHtml .= '</td>';
}
}
} else {
if ($ii == 0) {
echo ' <th>'.$question_y['answers'][($ij)].' '.$y.'</th>';
$tableHtml .= ' <th>'.$question_y['answers'][$ij].' '.$y.'</th>';
} else {
echo ' <td align="center">';
echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
echo '</td>';
$tableHtml .= ' <td align="center">';
$votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], 0, $y);
$tableHtml .= $votes;
array_push(
$chartData,
array(
'serie' => array($currentYQuestion, $xOptions[$ii-1]),
'option' => $y,
'votes' => $votes
)
);
$tableHtml .= '</td>';
}
}
}
echo ' </tr>';
$tableHtml .= ' </tr>';
}
}
// The Y axis is NOT a score question type so the number of rows = the number of options
else {
echo ' <tr>';
$tableHtml .= ' <tr>';
for ($ii = 0; $ii <= count($question_x['answers']); $ii++) {
if ($question_x['type'] == 'score') {
for ($x = 1; $x <= $question_x['maximum_score']; $x++) {
if ($ii == 0) {
echo ' <th>'.$question_y['answers'][($ij)].'</th>';
$tableHtml .= ' <th>'.$question_y['answers'][$ij].'</th>';
break;
} else {
echo ' <td align="center">';
echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
echo '</td>';
$tableHtml .= ' <td align="center">';
$votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)], $x, 0);
$tableHtml .= $votes;
array_push(
$chartData,
array(
'serie' => array($currentYQuestion, $xOptions[$ii-1]),
'option' => $x,
'votes' => $votes
)
);
$tableHtml .= '</td>';
}
}
} else {
if ($ii == 0) {
echo ' <th>'.$question_y['answers'][($ij)].'</th>';
$tableHtml .= ' <th>'.$question_y['answers'][($ij)].'</th>';
} else {
echo ' <td align="center">';
echo SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
echo '</td>';
$tableHtml .= ' <td align="center">';
$votes = SurveyUtil::comparative_check($answers_x, $answers_y, $question_x['answersid'][($ii-1)], $question_y['answersid'][($ij)]);
$tableHtml .= $votes;
array_push(
$chartData,
array(
'serie' => $xOptions[$ii-1],
'option' => $currentYQuestion,
'votes' => $votes
)
);
$tableHtml .= '</td>';
}
}
}
echo ' </tr>';
$tableHtml .= ' </tr>';
}
}
echo '</table>';
$tableHtml .= '</table>';
self::drawChart($chartData, true);
echo $tableHtml;
}
}
@ -4019,7 +4100,7 @@ class SurveyUtil
// Check if the user has given $option_x as answer
if (in_array($check_x, $answers)) {
// Check if the user has given $option_y as an answer
if (in_array($check_y, $answers_y[$user])) {
if (!is_null($answers_y[$user]) && in_array($check_y, $answers_y[$user])) {
$counter++;
}
}
@ -5115,4 +5196,58 @@ class SurveyUtil
return false;
}
}
/**
* Display survey question chart
* @param array Chart data
* @param boolean Tells if the chart has a serie. False by default
* @return void (direct output)
*/
static function drawChart($chartData, $hasSerie = false)
{
$htmlChart = '<div id="chartContainer" class="span12">'.
api_get_js("d3/d3.v3.4.8.js").'
<script src="http://dimplejs.org/dist/dimple.v2.1.0.min.js"></script>
<script type="text/javascript">
var svg = dimple.newSvg("#chartContainer", "100%", 400);
var data = [';
$order = array();
foreach ($chartData as $chartDataElement) {
$htmlChart .= '{"';
if (!$hasSerie) {
$htmlChart .= get_lang("Option") . '":"' . $chartDataElement['option'] . '", "';
array_push($order, $chartDataElement['option']);
} else if (!is_array($chartDataElement['serie'])) {
$htmlChart .= get_lang("Option") . '":"' . $chartDataElement['serie'] . '", "' .
get_lang("Score") . '":"' . $chartDataElement['option'] . '", "';
} else {
$htmlChart .= get_lang("Serie") . '":"' . $chartDataElement['serie'][0] . '", "' .
get_lang("Option") . '":"' . $chartDataElement['serie'][1] . '", "' .
get_lang("Score") . '":"' . $chartDataElement['option'] . '", "';
}
$htmlChart .= get_lang("Votes") . '":"' . $chartDataElement['votes'] .
'"},';
}
rtrim($htmlChart, ",");
$htmlChart .= '];
var myChart = new dimple.chart(svg, data);
myChart.addMeasureAxis("y", "'.get_lang("Votes").'");';
if (!$hasSerie) {
$htmlChart .= 'myChart . addCategoryAxis("x", "'.get_lang("Option").'");';
$htmlChart .= 'myChart.addSeries("'.get_lang("Option").'", dimple.plot.bar);';
} else if (!is_array($chartDataElement['serie'])) {
$htmlChart .= 'myChart.addCategoryAxis("x", ["'.get_lang("Option").'","'.get_lang("Score").'"]);';
$htmlChart .= 'myChart.addSeries("'.get_lang("Option").'", dimple.plot.bar);';
} else {
$htmlChart .= 'myChart.addCategoryAxis("x", ["'.get_lang("Option").'","'.get_lang("Score").'"]);';
$htmlChart .= 'myChart.addSeries("'.get_lang("Serie").'", dimple.plot.bar);';
}
$htmlChart .= 'myChart.draw();
</script>
</div>';
echo $htmlChart;
}
}

Loading…
Cancel
Save