/**
  * Saves a Call Task to the database.
  * If the call task is not saved yet, its id will be added to the 
  * call_task_entity.
  * @param Call_task_entity (by reference)
  * 
  * @return boolean
  *   Whether or not the save was successful.
  */
 public function save(Call_task_entity &$entity)
 {
     // To ensure date consistency.
     $date = Mongo_db::date();
     // Set update date:
     $entity->updated = $date;
     if ($entity->author === NULL) {
         $entity->author = current_user()->uid;
     }
     $prepared_data = array();
     foreach ($entity as $field_name => $field_value) {
         $prepared_data[$field_name] = $field_value;
     }
     if ($entity->is_new()) {
         // Add new properties.
         $entity->ctid = increment_counter(self::COUNTER_COLLECTION);
         $entity->created = clone $date;
         // Add properties to prepared_data.
         $prepared_data['ctid'] = $entity->ctid;
         $prepared_data['created'] = $entity->created;
         $result = $this->mongo_db->insert(self::COLLECTION, $prepared_data);
         return $result !== FALSE ? TRUE : FALSE;
     } else {
         $result = $this->mongo_db->set($prepared_data)->where('ctid', $entity->ctid)->update(self::COLLECTION);
         return $result !== FALSE ? TRUE : FALSE;
     }
 }
 /**
  * Direct input testing.
  */
 public function test__survey_respondents_import_check__direct()
 {
     /**
      * 2 valid
      */
     $rows = array();
     $rows[] = '1234';
     $rows[] = '12345';
     // From DB
     $db_call_tasks = array();
     $respondents = self::$CI->_survey_respondents_import_check($rows, $db_call_tasks);
     $expect = array('valid' => array('1234', '12345'), 'dups' => array(), 'invalid' => array());
     $this->assertEquals($expect, $respondents);
     /* ========================================================= */
     /**
      * 3 invalid
      */
     $rows = array();
     $rows[] = '1889798 548';
     $rows[] = '5484.0';
     $rows[] = '8754984.';
     // From DB
     $db_call_tasks = array();
     $respondents = self::$CI->_survey_respondents_import_check($rows, $db_call_tasks);
     $expect = array('valid' => array(), 'dups' => array(), 'invalid' => array('1889798 548', '5484.0', '8754984.'));
     $this->assertEquals($expect, $respondents);
     /* ========================================================= */
     /**
      * 3 valid
      * 1 dups
      * 5 invalid
      */
     $rows = array();
     $rows[] = '1234';
     $rows[] = '12345';
     $rows[] = '123456';
     $rows[] = '12345';
     $rows[] = '12e4';
     $rows[] = '123409875,2134234';
     $rows[] = '';
     $rows[] = '1889798 548';
     $rows[] = '5484.0';
     $rows[] = '8754984.';
     // From DB
     $db_call_tasks = array();
     $respondents = self::$CI->_survey_respondents_import_check($rows, $db_call_tasks);
     $expect = array('valid' => array('1234', '12345', '123456'), 'dups' => array('12345'), 'invalid' => array('12e4', '123409875,2134234', '1889798 548', '5484.0', '8754984.'));
     $this->assertEquals($expect, $respondents);
     /* ========================================================= */
     /**
      * 2 valid
      * 1 dups with file
      * 1 dup with db
      */
     $rows = array();
     $rows[] = '1234';
     $rows[] = '12345';
     $rows[] = '12345';
     $rows[] = '999';
     // From DB - Array or call_task_entities
     // Fake data.
     $db_call_tasks = array();
     $db_call_tasks[] = Call_task_entity::build(array('number' => '999'));
     $db_call_tasks[] = Call_task_entity::build(array('number' => '998'));
     $db_call_tasks[] = Call_task_entity::build(array('number' => '997'));
     $respondents = self::$CI->_survey_respondents_import_check($rows, $db_call_tasks);
     $expect = array('valid' => array('1234', '12345'), 'dups' => array('12345', '999'), 'invalid' => array());
     $this->assertEquals($expect, $respondents);
     /* ========================================================= */
 }
 public function test_call_task_is_reserved()
 {
     $data = array('number' => '00000000000000', 'assignee_uid' => NULL, 'activity' => array());
     $call_task = new Call_task_entity($data);
     $this->assertFalse($call_task->is_reserved());
     $data = array('number' => '00000000000000', 'assignee_uid' => 1, 'activity' => array(array('code' => Call_task_status::CANT_COMPLETE, 'message' => 'A message.')));
     $call_task = new Call_task_entity($data);
     $this->assertFalse($call_task->is_reserved());
     $data = array('number' => '00000000000000', 'assignee_uid' => 1, 'activity' => array());
     $call_task = new Call_task_entity($data);
     $this->assertTrue($call_task->is_reserved());
 }
copy('resources/fixtures_data/survey_2_xml.xml', 'files/surveys/survey_2_xml.xml');
// Survey 2.
$survey = new Survey_entity(array('title' => 'Handlebars', 'client' => 'Digital Dreams Inc', 'status' => Survey_entity::STATUS_OPEN, 'goal' => 20, 'introduction' => 'Hi, my name is ______ and I am calling on behalf of Digital Dreams. Handlebars is interested in learning more about you and your relation with it. You are being contacted because of your participation in the workshop held near your house. We would like to ask you some questions about your coffee consumption while using handlebars. Your participation is very important, because your responses will help us improve our framework and thus make it more usable. This survey will only take about 5 minutes of your time.', 'description' => 'This survey will help us understand how handlebars is used by developers and how it can be improved.', 'files' => array('xls' => "survey_2_xls.xls", 'xml' => "survey_2_xml.xml", 'last_conversion' => array('date' => NULL, 'warnings' => NULL))));
// Assign agent, user 3
$survey->assign_agent(3);
$this->survey_model->save($survey);
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// Call task.
$call_task = new Call_task_entity(array('number' => "1000000000001", 'assigned' => Mongo_db::date(), 'author' => 1, 'assignee_uid' => 3, 'survey_sid' => 2));
$call_task->add_status(new Call_task_status(array('code' => Call_task_status::NO_REPLY, 'message' => NULL, 'author' => 3, 'created' => Mongo_db::date())));
$call_task->add_status(new Call_task_status(array('code' => Call_task_status::NO_REPLY, 'message' => NULL, 'author' => 3, 'created' => Mongo_db::date())));
$this->call_task_model->save($call_task);
///////////////////////////////////////////////////////
// Call task.
$call_task = new Call_task_entity(array('number' => "1000000000002", 'assigned' => Mongo_db::date(), 'author' => 1, 'assignee_uid' => 3, 'survey_sid' => 2));
$call_task->add_status(new Call_task_status(array('code' => Call_task_status::CANT_COMPLETE, 'message' => 'Not to be done right now. Maybe later.', 'author' => 3, 'created' => Mongo_db::date())));
$this->call_task_model->save($call_task);
///////////////////////////////////////////////////////
// Call task.
$call_task = new Call_task_entity(array('number' => "1000000000003", 'assigned' => Mongo_db::date(), 'author' => 1, 'assignee_uid' => 3, 'survey_sid' => 2));
$call_task->add_status(new Call_task_status(array('code' => Call_task_status::INVALID_NUMBER, 'message' => NULL, 'author' => 3, 'created' => Mongo_db::date())));
$this->call_task_model->save($call_task);
///////////////////////////////////////////////////////
// Add some respondents to be used for data collection.
for ($r = 0; $r < 100; $r++) {
    // Call task.
    $call_task = new Call_task_entity(array('number' => (string) (2000000000000 + $r), 'assigned' => Mongo_db::date(), 'author' => 1, 'survey_sid' => 2));
    $this->call_task_model->save($call_task);
}
Exemple #5
0
 /**
  * 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());
     }
 }