* @version 1.0 * @since 03 Nov 2013 13:29:40 */ require_once dirname(__FILE__) . '/../_config.inc.php'; /******************************************************************** * CONFIG ********************************************************************/ /******************************************************************** * MAIN ********************************************************************/ switch (getFormData('op')) { case 'json' : echo getResponsesJSON(getFormData('question_id')); $app->stop(); die; case 'timeline' : echo getTimelineJSON(getFormData('survey_id')); $app->stop(); die; case 'csv' : header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=survey_' . getFormData('survey_id') . '_' . date('Y-m-d') . '.csv'); header('Content-Transfer-Encoding: binary'); echo "participant_id,question_id,response,response_parsed,added_datetime,question_id,survey_id,rank,question_text,added_datetime,added_by_user_id,modified_datetime,modified_by_user_id\n"; echo getResponsesCSV(getFormData('survey_id')); $app->stop(); die; } /******************************************************************** * OUTPUT ********************************************************************/ /******************************************************************** * FUNCTIONS ********************************************************************/ /* * Generates the JSON required for the Google Charts API. Docs: * https://developers.google.com/chart/interactive/docs/php_example * https://google-developers.appspot.com/chart/interactive/docs/gallery/piechart#donut * * @access public * @param int $question_id * @return string JSON for Google Charts ajax call. * @author Quinn Comendant * @version 1.0 * @since 03 Nov 2013 13:31:12 */ function getResponsesJSON($question_id) { $db =& DB::getInstance(); $q = Question::get(array('question_id' => $question_id)); $q_parsed = Question::parseOptions($q['question_text']); $qid = $db->query(" SELECT response_parsed, COUNT(response) AS response_count FROM response_tbl WHERE response_tbl.question_id = '" . $db->escapeString($question_id) . "' GROUP BY question_id, response_parsed ORDER BY response_tbl.added_datetime ASC "); $gviz_data = array( 'cols' => array( array('id' => 'labels', 'label' => 'Response', 'type' => 'string'), array('id' => 'values', 'label' => 'Count', 'type' => 'number'), ), 'rows' => array() ); while ($row = mysql_fetch_assoc($qid)) { $gviz_data['rows'][] = array('c' => array(array('v' => $row['response_parsed']), array('v' => (int)$row['response_count']))); } return json_encode($gviz_data); } /* * Generates the JSON required for the Google Charts API. Docs: * https://developers.google.com/chart/interactive/docs/php_example * https://google-developers.appspot.com/chart/interactive/docs/gallery/piechart#donut * * @access public * @param int $question_id * @return string JSON for Google Charts ajax call. * @author Quinn Comendant * @version 1.0 * @since 03 Nov 2013 13:31:12 */ function getTimelineJSON($survey_id) { $db =& DB::getInstance(); $where_clause = ''; if (is_numeric($survey_id)) { $where_clause = "AND event_id = '" . mysql_real_escape_string($survey_id) . "'"; } $qid = $db->query(" SELECT YEAR(response_tbl.added_datetime) AS udate_year, MONTH(response_tbl.added_datetime) AS udate_month, DAY(response_tbl.added_datetime) AS udate_day, COUNT(response_tbl.added_datetime) AS udate_count FROM response_tbl LEFT JOIN question_tbl ON (response_tbl.question_id = question_tbl.question_id) WHERE survey_id = '" . $db->escapeString($survey_id) . "' GROUP BY DATE(response_tbl.added_datetime) ORDER BY response_tbl.added_datetime ASC "); $gviz_data = array( 'cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date'), array('id' => 'responses', 'label' => 'Responses per day', 'type' => 'number')), 'rows' => array() ); while ($row = mysql_fetch_assoc($qid)) { $gviz_data['rows'][] = array('c' => array(array('v' => sprintf('Date(%s,%s,%s)', $row['udate_year'], $row['udate_month'] - 1, $row['udate_day'])), array('v' => $row['udate_count']))); } return json_encode($gviz_data); } /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 03 Nov 2013 14:59:50 */ function getResponsesCSV($survey_id) { $db =& DB::getInstance(); $qid = $db->query(" SELECT * FROM response_tbl LEFT JOIN question_tbl ON (response_tbl.question_id = question_tbl.question_id) WHERE question_tbl.survey_id = '" . $db->escapeString($survey_id) . "' ORDER BY response_tbl.added_datetime ASC "); $results = array(); while ($row = mysql_fetch_assoc($qid)) { $results[] = '"' . join('","', $row) . '"'; } return join("\n", $results); } ?>