public function test_data_storage() { Status_msg::success('Success message.'); Status_msg::error('Error message.'); Status_msg::warning('Warning message.', TRUE); // Execution is fast enough that the time won't vary. $time = time(); $msg = Status_msg::get(); $expected = array(array('level' => 'success', 'msg' => 'Success message.', 'sticky' => FALSE, 'time' => $time), array('level' => 'error', 'msg' => 'Error message.', 'sticky' => TRUE, 'time' => $time), array('level' => 'warning', 'msg' => 'Warning message.', 'sticky' => TRUE, 'time' => $time)); $this->assertEquals($msg, $expected); // After getting the first get the messages should be cleared. $msg = Status_msg::get(); $expected = NULL; $this->assertEquals($msg, $expected); }
/** * Summary page to add respondents to a given survey. * @param $sid * * Route - /survey/:sid/respondents/(file|input) */ public function survey_respondents_add($sid, $type) { $survey = $this->survey_model->get($sid); if (!$survey) { show_404(); } else { if (!has_permission('manage respondents any survey')) { show_403(); } } if (!$survey->status_allows('import respondents any survey')) { show_403(); } // Form validation based on import type. switch ($type) { case 'file': // Config data for the file upload. $file_upload_config = array('upload_path' => '/tmp/', 'allowed_types' => 'csv', 'file_name' => 'respondents_' . md5(microtime(true))); // Load needed libraries. $this->load->library('upload', $file_upload_config); $this->form_validation->set_rules('survey_respondents_file', 'Respondents File', 'callback__cb_survey_respondents_add_file_handle'); break; case 'direct': $this->form_validation->set_rules('survey_respondents_text', 'Respondents Text', 'trim|required|xss_clean'); break; } $this->form_validation->set_error_delimiters('<small class="error">', '</small>'); // If the import has invalid respondents they are added to the textarea // to allow the user to correct them. $invalid_respondents = array(); if (isset($_SESSION['respondents_numbers']['invalid'])) { $invalid_respondents = $_SESSION['respondents_numbers']['invalid']; // Unset so they don't bother anymore. unset($_SESSION['respondents_numbers']); } // If no data submitted show the form. if ($this->form_validation->run() == FALSE) { $this->load->view('base/html_start'); $this->load->view('components/navigation', array('active_menu' => 'surveys')); $this->load->view('surveys/survey_respondents_add', array('survey' => $survey, 'import_type' => $type, 'invalid_respondents' => $invalid_respondents)); $this->load->view('base/html_end'); } else { // Processing based on import type. switch ($type) { case 'file': // Read file. $file = $this->input->post('survey_respondents_file'); if (isset($file['full_path'])) { // Load CSVReader library. $this->load->helper('csvreader'); $csv = new CSVReader(); $csv->separator = ','; $rows = $csv->parse_file($file['full_path']); } break; case 'direct': $rows = explode("\n", $this->input->post('survey_respondents_text')); break; } if (!isset($rows)) { Status_msg::error('An error occurred when processing the numbers. Try again.'); redirect($survey->get_url_respondents()); } // Load all call_tasks from DB to check for duplicates. $db_call_tasks = $this->call_task_model->get_all($sid); try { $respondents_numbers = $this->_survey_respondents_import_check($rows, $db_call_tasks); } catch (Exception $e) { Status_msg::error($e->getMessage()); redirect($survey->get_url_respondents()); } // Create a call_task for each respondent number. $success_saves = array(); foreach ($respondents_numbers['valid'] as $number) { // Prepare survey data to construct a new survey_entity $call_task_data = array(); $call_task_data['survey_sid'] = (int) $sid; $call_task_data['number'] = (string) $number; // Construct survey. $new_call_task = Call_task_entity::build($call_task_data); // Save call task. if ($this->call_task_model->save($new_call_task)) { $success_saves[] = $number; } else { // If an error occurred flag as invalid to be imported again. // This should not happen, but you never know. $respondents_numbers['invalid'][] = $number; } } $respondents_numbers['valid'] = $success_saves; // Setting messages. $total_valid = count($respondents_numbers['valid']); $total_invalid = count($respondents_numbers['invalid']); $total_dups = count($respondents_numbers['dups']); $total_numbers = $total_valid + $total_invalid + $total_dups; if ($total_valid) { Status_msg::success("{$total_valid} of {$total_numbers} respondents were successfully imported.", TRUE); } else { Status_msg::warning("No respondents were imported."); } if ($total_invalid) { Status_msg::error("{$total_invalid} of {$total_numbers} respondents were not in a valid format."); } if ($total_dups) { Status_msg::notice("{$total_dups} of {$total_numbers} respondents have duplicate phone numbers."); } // Store invalid in any. if ($total_invalid) { $_SESSION['respondents_numbers']['invalid'] = $respondents_numbers['invalid']; redirect($survey->get_url_respondents_add('direct')); } redirect($survey->get_url_respondents()); } }