Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. function results_export($survey_id = null)
  2. {
  3. if(empty($survey_id))
  4. {
  5. redirect(site_url().'admin/survey');
  6. }
  7.  
  8. $results = $this->Survey_Model->getResults($survey_id);
  9. if($results === FALSE)
  10. {
  11. echo "ERROR";
  12. }
  13.  
  14. //Build the results into an array more suited for a spreadsheet.
  15. $new_results = array();
  16. $result = $results->first_row('array');
  17.  
  18. //Wanted array values
  19. $wanted_result = array('name', 'email', 'date', 'phone', 'city', 'state');
  20.  
  21. //Columns present in answers uses key=question_ID[.option_ID] val=question form
  22. $answer_columns = array();
  23.  
  24. // Iterate through survey results
  25. while($result != $results->last_row('array'))
  26. {
  27. $id = $result['result_ID'];
  28.  
  29. //Add desired values to new array
  30. foreach($wanted_result as $key)
  31. {
  32. $new_results[$id][$key] = $result[$key];
  33. }
  34. $new_results[$id]['date'] = date('Y-m-d H:i', $new_results[$id]['date']);
  35.  
  36. //Add all folowing results in this instance to sub array answers
  37. //Just iterates through answers to this result
  38. while($result['result_ID'] == $id)
  39. {
  40.  
  41. //Give the answers a similar array structure to columens.
  42. //i.e. key=question_ID[.option_ID]
  43. $ans_id = !empty($result['option_ID']) ?
  44. $result['question_ID'].'.'.$result['option_ID'] : $result['question_ID'];
  45.  
  46. //Add to column list
  47. $answer_columns[$ans_id] = $result['question'];
  48. //Set answers to
  49. $new_results[$id][$ans_id] = $result['answer'];
  50.  
  51. //If at last row, die
  52. if($result == $results->last_row('array'))
  53. {
  54. break;
  55. }
  56. $result = $results->next_row('array');
  57. }
  58.  
  59. }
  60. //Clean up
  61. $results->free_result();
  62. $results = $new_results;
  63. unset($new_results);
  64.  
  65. $questions = $this->Survey_Model->getQuestions($survey_id);
  66. if($questions === FALSE)
  67. {
  68. echo "ERROR";
  69. }
  70.  
  71. //builds column_ids array key=question_id(.option_id) value=question
  72. $column_ids = array();
  73. foreach($questions as $question)
  74. {
  75. if($question['type'] == 'grid')
  76. {
  77. foreach($question['options'] as $option)
  78. {
  79. $key = $question['question_ID'].'.'.$option['option_ID'];
  80. $column_ids[$key] = $question['question'].'['.$option['option'].']';
  81. }
  82. }
  83. else
  84. {
  85. $key = $question['question_ID'];
  86. $column_ids[$key] = $question['question'];
  87. }
  88. }
  89. //Cleanup
  90. unset($questions);
  91.  
  92. //Now we need to combine the columns from the answers and the survey together with the other columns.
  93. foreach ($answer_columns as $key=>$col)
  94. {
  95. if(!isset($column_ids[$key]))
  96. {
  97. $column_ids[$key] = $col;
  98. }
  99. }
  100. //More cleanup
  101. unset($answer_columns);
  102.  
  103. //Almost done, just need to start printing everything.
  104. //Lets make the column headers
  105. $columns = array('name'=>'Name', 'email'=>'Email', 'date'=>'Date', 'phone'=>'Phone #', 'city'=>'City', 'state'=>'State');
  106.  
  107. $out = create_csv_line($columns, ',', ',').create_csv_line($column_ids);
  108. foreach($results as $result)
  109. {
  110. $out_arr = array();
  111. //Lets print results in correct order.
  112. foreach($columns as $key=>$col)
  113. {
  114. $out_arr[$key] = $result[$key];
  115. }
  116. //answers now.
  117. foreach($column_ids as $key=>$col)
  118. {
  119. $out_arr[$key] = isset($result[$key]) ? $result[$key]: " ";
  120. }
  121. //Create line
  122. $out .= create_csv_line($out_arr);
  123. }
  124.  
  125. $survey = $this->Survey_Model->getSurvey($survey_id)->row_array();
  126. $file = date('Y_m_d')."_".$survey['title']."_old_survey.csv";
  127. $file = preg_replace('/[^\w\d. -]/si', '', $file);
  128. header("Content-type: text/csv");
  129. header("Content-Disposition: attachment; filename=\"".$file."\"");
  130. header("Pragma: no-cache");
  131. header("Expires: 0");
  132. //And finally, echo out csv
  133. echo $out;
  134. }
Add Comment
Please, Sign In to add comment