Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function results_export($survey_id = null)
- {
- if(empty($survey_id))
- {
- redirect(site_url().'admin/survey');
- }
- $results = $this->Survey_Model->getResults($survey_id);
- if($results === FALSE)
- {
- echo "ERROR";
- }
- //Build the results into an array more suited for a spreadsheet.
- $new_results = array();
- $result = $results->first_row('array');
- //Wanted array values
- $wanted_result = array('name', 'email', 'date', 'phone', 'city', 'state');
- //Columns present in answers uses key=question_ID[.option_ID] val=question form
- $answer_columns = array();
- // Iterate through survey results
- while($result != $results->last_row('array'))
- {
- $id = $result['result_ID'];
- //Add desired values to new array
- foreach($wanted_result as $key)
- {
- $new_results[$id][$key] = $result[$key];
- }
- $new_results[$id]['date'] = date('Y-m-d H:i', $new_results[$id]['date']);
- //Add all folowing results in this instance to sub array answers
- //Just iterates through answers to this result
- while($result['result_ID'] == $id)
- {
- //Give the answers a similar array structure to columens.
- //i.e. key=question_ID[.option_ID]
- $ans_id = !empty($result['option_ID']) ?
- $result['question_ID'].'.'.$result['option_ID'] : $result['question_ID'];
- //Add to column list
- $answer_columns[$ans_id] = $result['question'];
- //Set answers to
- $new_results[$id][$ans_id] = $result['answer'];
- //If at last row, die
- if($result == $results->last_row('array'))
- {
- break;
- }
- $result = $results->next_row('array');
- }
- }
- //Clean up
- $results->free_result();
- $results = $new_results;
- unset($new_results);
- $questions = $this->Survey_Model->getQuestions($survey_id);
- if($questions === FALSE)
- {
- echo "ERROR";
- }
- //builds column_ids array key=question_id(.option_id) value=question
- $column_ids = array();
- foreach($questions as $question)
- {
- if($question['type'] == 'grid')
- {
- foreach($question['options'] as $option)
- {
- $key = $question['question_ID'].'.'.$option['option_ID'];
- $column_ids[$key] = $question['question'].'['.$option['option'].']';
- }
- }
- else
- {
- $key = $question['question_ID'];
- $column_ids[$key] = $question['question'];
- }
- }
- //Cleanup
- unset($questions);
- //Now we need to combine the columns from the answers and the survey together with the other columns.
- foreach ($answer_columns as $key=>$col)
- {
- if(!isset($column_ids[$key]))
- {
- $column_ids[$key] = $col;
- }
- }
- //More cleanup
- unset($answer_columns);
- //Almost done, just need to start printing everything.
- //Lets make the column headers
- $columns = array('name'=>'Name', 'email'=>'Email', 'date'=>'Date', 'phone'=>'Phone #', 'city'=>'City', 'state'=>'State');
- $out = create_csv_line($columns, ',', ',').create_csv_line($column_ids);
- foreach($results as $result)
- {
- $out_arr = array();
- //Lets print results in correct order.
- foreach($columns as $key=>$col)
- {
- $out_arr[$key] = $result[$key];
- }
- //answers now.
- foreach($column_ids as $key=>$col)
- {
- $out_arr[$key] = isset($result[$key]) ? $result[$key]: " ";
- }
- //Create line
- $out .= create_csv_line($out_arr);
- }
- $survey = $this->Survey_Model->getSurvey($survey_id)->row_array();
- $file = date('Y_m_d')."_".$survey['title']."_old_survey.csv";
- $file = preg_replace('/[^\w\d. -]/si', '', $file);
- header("Content-type: text/csv");
- header("Content-Disposition: attachment; filename=\"".$file."\"");
- header("Pragma: no-cache");
- header("Expires: 0");
- //And finally, echo out csv
- echo $out;
- }
Add Comment
Please, Sign In to add comment