
We have installed the Balanced CMS Customer Survey Extension on one of our Magento shops.
In the Magento administrator panel it display results in two formats. General format, which shows the results in the format of HTML forms with the form elements filled with answers submitted by the customer. The other format is graphical, which shows the results in the form of pie charts and graphs.
We decided to implement a new feature so that the administrator can easily export the results to a .csv file. Below are the steps we took to do this:
The first step was to add an item to the Action dropdown against each survey item on the View Surveys grid layout. This is achieved in the file below inside the function _prepareColumns().
app/code/local/Balanced/CustomerSurvey/Block/Grid.php
Where the module is adding a column for the ‘action’, we need to add an item with a label of for example ‘Export Results to .csv’ just below the item labeled ‘Results’.
$this->addColumn('action', array( 'header' => Mage::helper('CustomerSurvey')->__('Action'), 'width' => '100', 'type' => 'action', 'getter' => 'getId', 'actions' => array( array( 'caption' => Mage::helper('CustomerSurvey')->__('Edit'), 'url' => array('base'=> '*/*/edit'), 'field' => 'id' ), array( 'caption' => Mage::helper('CustomerSurvey')->__('Results'), 'url' => array('base'=> '*/*/results'), 'field' => 'id' ), array( 'caption' => Mage::helper('CustomerSurvey')->__('Export Results to .csv'), 'url' => array('base'=> '*/*/csvexport'), 'field' => 'id' ) ), 'filter' => false, 'sortable' => false, 'index' => 'stores', 'is_system' => true, ));
In the code above notice that the action which will be called to export results is ‘csvexport’. In the next step lets create an action in the controller.
In the view controller located at the path below add a new function csvExportAction() which is called when admin selects the export csv option in the dropdown we mentioned in the previous step.
app/code/local/Balanced/CustomerSurvey/controllers/ViewController.php
public function csvExportAction() { $surveyid = $this->getRequest()->getParam('id'); $file = Mage::getModel('customersurvey/results')->exportSurvey($surveyid); $this->_prepareDownloadResponse($file, file_get_contents(Mage::getBaseDir('export').'/'.$file)); }
We need to write some code which basically fetches the survey results and generates a .csv file. We put them in Results model (Balanced_CustomerSurvey_Model_Results) located at the path below.
app/code/local/Balanced/CustomerSurvey/Model/Results.php
Add below functions to Balanced_CustomerSurvey_Model_Results class.
/** * Get questions for survey * @return (Array) */ public function getMyQuestions($customersurveyId) { if($customersurveyId) { $questions = Mage::getModel('customersurvey/questions')->getCollection()->addFieldToFilter('customersurvey_id', $customersurveyId); $questions = $questions->addAttributeToSort('sort_order', 'ASC'); } else { $questions = array(); } return $questions; } /** * Get responses given to survey * @return (Array) */ public function getMyResponses($customersurveyId) { if($customersurveyId) { //get the list of responses $groupedResponses = Mage::getModel('customersurvey/results')->getCollection(); $groupedResponses->getSelect()->where("main_table.customersurvey_id = " . $customersurveyId)->group('main_table.input_time'); //now for each response in the list, get the group of answers $responsesArray = array(); foreach($groupedResponses as $groupedResponse) { $responses = Mage::getModel('customersurvey/results')->getCollection(); $responses->getSelect()->where("main_table.customersurvey_id = " . $customersurveyId . " and main_table.input_time = '" . $groupedResponse->input_time . "'"); $innerResponses = array(); $innerResponses['input_time'] = $groupedResponse->input_time; $innerResponses['answers'] = array(); foreach($responses as $response) { $innerResponses['answers'][$response->question_id] = $response->answer; } $responsesArray[] = $innerResponses; } } else { $responsesArray = array(); } return $responsesArray; } /** * Writing head row to csv */ public function exportSurvey($customersurveyId) { $this->surveyId = $customersurveyId; $fileName = 'survey_export_'.$customersurveyId.'.csv'; $fp = fopen(Mage::getBaseDir('export').'/'.$fileName, 'w'); // write first row for questions $this->writeHeadRow($fp); $myQuestions = $this->getMyQuestions($customersurveyId); $myResponses = $this->getMyResponses($customersurveyId); // write rows for answers in respected columns foreach($myResponses as $response) { $answers = array(); array_push($answers,date("F j, Y g:i a", strtotime($response['input_time']))); foreach($myQuestions as $question) { if(key_exists($question->question_id,$response['answers'])) { $answer = $response['answers'][$question->question_id]; switch($question->answer_type) { case "bubbles1": case "bubbles2": case "field": case "area": array_push($answers,$answer); break; case "star": array_push($answers,$answer); break; case "yesno": array_push($answers,$answer); break; case "customradio": case "checkboxes": if($question->possible_answers) { $possibleAnswers = explode('|||', $question->possible_answers); } else { array_push($answers,$answer); break; } $counterA = 0; $possibleAnswerArray = array(); foreach($possibleAnswers as $possibleAnswer) { if(!(strpos($answer, " " . $counterA . ",") === false)) { array_push($possibleAnswerArray,$possibleAnswer); } $counterA++; } if(count($possibleAnswerArray)>0) { array_push($answers,implode(', ',$possibleAnswerArray)); } break; default: array_push($answers,""); break; } } else { array_push($answers,""); } } if(count($answers) > 0) { fputcsv($fp, $answers, self::DELIMITER, self::ENCLOSURE); } } fclose($fp); return $fileName; } /** * Writing head row to csv */ protected function writeHeadRow($fp) { fputcsv($fp, $this->getHeadRowValues(), self::DELIMITER, self::ENCLOSURE); } /** * Fetching questions to generate first row for csv. * @return (Array) */ protected function getHeadRowValues() { $myQuestions = $this->getMyQuestions($this->surveyId); $questions = array(); // Adding a column to capture date and time when survey was submitted. array_push($questions,"Date"); foreach($myQuestions as $question) { array_push($questions,$question->question); } return $questions; }
The administrator now just needs to select the action ‘Export Results to .csv’ under the Actions dropdown on the ‘View Surveys’ page. Upon selecting an action it will open the dialog box asking to save a .csv file.
Customisation to this existing extension was simple to implement. Currently we are exporting survey questions and all answers along with a time stamp but more could be added. Leave a comment if you have any questions or queries, we would love to hear from you.