public function test_flat_xform_survey_en_pt()
 {
     $expected_xform_flat = array('/survey_en_pt/user_name' => array('machine_label' => "user_name", 'type' => "string", 'label' => array('Portugues' => "Qual é o teu nome?", 'English' => "What's your name?")), '/survey_en_pt/a_integer' => array('machine_label' => "a_integer", 'type' => "int", 'label' => array('Portugues' => "Escolhe um número inteiro:", 'English' => "Pick an integer:")), '/survey_en_pt/a_decimal' => array('machine_label' => "a_decimal", 'type' => "decimal", 'label' => array('Portugues' => "Escolhe um número decimal:", 'English' => "Pick a decimal:")), '/survey_en_pt/calculate' => array('machine_label' => "calculate", 'type' => "string", 'label' => "calculate", 'system' => "1"), '/survey_en_pt/required_text' => array('machine_label' => "required_text", 'type' => "string", 'label' => array('Portugues' => "Questão obrigatória:", 'English' => "Required question")), '/survey_en_pt/skip_example' => array('machine_label' => "skip_example", 'type' => "select1", 'label' => array('Portugues' => "Saltar a próxima questão?", 'English' => "Skip the next question?"), 'items' => array('yes' => array('Portugues' => "Sim", 'English' => "Yes"), 'no' => array('Portugues' => "Não", 'English' => "No"))), '/survey_en_pt/skipable_question' => array('machine_label' => "skipable_question", 'type' => "string", 'label' => array('Portugues' => "Então escreve alguma coisa.", 'English' => "Then enter something.")), '/survey_en_pt/repeat_test/repeating_question' => array('machine_label' => "repeating_question", 'type' => "string", 'label' => array('Portugues' => "Esta é uma questão repetível.", 'English' => "This is a repeating question.")), '/survey_en_pt/group_test/select_multiple' => array('machine_label' => "select_multiple", 'type' => "select", 'label' => array('Portugues' => "Escolha múltipla", 'English' => "Select multiple"), 'items' => array('yes' => array('Portugues' => "Sim", 'English' => "Yes"), 'no' => array('Portugues' => "Não", 'English' => "No"))), '/survey_en_pt/group_test/group_within_note/select_multiple_within' => array('machine_label' => "select_multiple_within", 'type' => "select", 'label' => array('Portugues' => "Escolha múltipla dentro de um grupo.", 'English' => "Select multiple within a group."), 'items' => array('banana' => array('Portugues' => "Banana", 'English' => "Banana"), 'pear' => array('Portugues' => "Pera", 'English' => "Pear"), 'kiwi' => array('Portugues' => "Kiwi", 'English' => "Kiwi"), 'apple' => array('Portugues' => "Maçã", 'English' => "Apple"), 'passion_fruit' => array('Portugues' => "Maracujá", 'English' => "Passion Fruit"), 'tomato' => array('Portugues' => "Tomate", 'English' => "Tomato"))));
     $survey_no_lang = new OR_xform_results(or_xform_results_helper_test::FILE_PATH . 'survey_en_pt/survey_en_pt.xml');
     $this->assertEquals($expected_xform_flat, $survey_no_lang->get_flatten());
 }
Пример #2
0
 /**
  * Export the collected data to csv file.
  * @param $sid
  *   Survey sid
  * @param $type
  *  The type of export, if human readable of machine readable.
  *
  * Route - /survey/:sid/data_export/(csv_human|csv_machine)
  */
 public function survey_export_csv($sid, $type)
 {
     if (!has_permission('export csv data any survey')) {
         show_403();
     }
     $survey = $this->survey_model->get($sid);
     if (!$survey) {
         show_404();
     }
     if (!$survey->status_allows('export csv data any survey')) {
         show_403();
     }
     // Load stuff.
     $this->load->model('survey_result_model');
     $this->load->helper('or_xform_results');
     try {
         $flattener = new OR_xform_results($survey->get_xml_full_path());
     } catch (Exception $e) {
         // The xform file does not exist or is not readable.
         show_404();
     }
     // Load results.
     $results = $this->survey_result_model->get_all($sid);
     // Type of export.
     switch ($type) {
         case 'csv_human':
             $label_key = "label";
             $value_key = "value";
             $filename = sprintf('survey_results_%d_normalized.csv', $survey->sid);
             break;
         case 'csv_machine':
             $label_key = "machine_label";
             $value_key = "machine_value";
             $filename = sprintf('survey_results_%d_raw.csv', $survey->sid);
             break;
     }
     // Compose header of csv file.
     // The header is being created from the flat xfrom so if all the result
     // files fail, we'll have an empty csv with an header.
     $flat = $flattener->get_flatten();
     $header = array();
     foreach ($flat as $key => $value) {
         // The language only matters when we're exporting a non system
         // question in a normalised format.
         if ($type == 'csv_human' && !isset($value['system']) && $flattener->is_translated()) {
             $header[] = $value['label'][$flattener->get_preferred_language()];
         } else {
             // Machine labels are never translated and human label behave the
             // same way if there's no translation.
             $header[] = $value[$label_key];
         }
     }
     // Headers.
     header("Cache-Control: public");
     header("Cache-Control: no-cache, must-revalidate");
     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=" . $filename);
     header("Content-Type: application/octet-stream; ");
     header("Content-Transfer-Encoding: binary");
     // Open stream.
     $output = fopen('php://output', 'w');
     // Put headers.
     fputcsv($output, $header);
     // Compose data.
     foreach ($results as $survey_result_entity) {
         try {
             $parsed_file = $flattener->parse_result_file($survey_result_entity->get_xml_full_path());
         } catch (Exception $e) {
             // The file does not exist or is not readable. Skip.
             continue;
         }
         $fields = array();
         foreach ($parsed_file as $data) {
             $fields[] = is_array($data[$value_key]) ? implode(' ', $data[$value_key]) : $data[$value_key];
         }
         fputcsv($output, $fields);
     }
     //Close stream.
     fclose($output);
 }