/**
  * Saves a survey to the database.
  * If the survey is not saved yet, its id will be added to the 
  * survey_entity.
  * @param Survey_entity (by reference)
  * 
  * @return boolean
  *   Whether or not the save was successful.
  */
 public function save(Survey_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->sid = increment_counter(self::COUNTER_COLLECTION);
         $entity->created = clone $date;
         // Add properties to prepared_data.
         $prepared_data['sid'] = $entity->sid;
         $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('sid', $entity->sid)->update(self::COLLECTION, array('upsert' => TRUE));
         return $result !== FALSE ? TRUE : FALSE;
     }
 }
 public function test_api_survey_with_status_restrictions()
 {
     // Here we are testing all the API but only for status restrictions.
     // Every other test case should be tested elsewhere.
     // Cleanup
     self::$CI->mongo_db->dropCollection('aw_datacollection_test', 'surveys');
     self::$CI->mongo_db->dropCollection('aw_datacollection_test', 'call_tasks');
     $this->_reset_status_restrictions();
     // Shorter statuses.
     $draft = Survey_entity::STATUS_DRAFT;
     $open = Survey_entity::STATUS_OPEN;
     $closed = Survey_entity::STATUS_CLOSED;
     $canceled = Survey_entity::STATUS_CANCELED;
     // Login user
     $this->_change_user(9903);
     /////////////////////////////////////////////////////////////////
     // Set actions to be allowed only in Draft status.
     $mock_config = self::$status_resctriction_config;
     $mock_config['enketo collect data'] = array(Survey_entity::STATUS_DRAFT);
     $mock_config['enketo testrun'] = array(Survey_entity::STATUS_DRAFT);
     $this->_set_status_restrictions($mock_config);
     // Logged user is 9903
     // User is agent.
     // Create survey.
     // Status open.
     // Valid xml file.
     // User is assigned to survey.
     $survey = Survey_entity::build(array('sid' => 1, 'status' => Survey_entity::STATUS_OPEN, 'files' => array('xml' => 'valid_survey.xml'), 'agents' => array(9903)));
     self::$CI->survey_model->save($survey);
     // Create call task
     self::$CI->mongo_db->insert('call_tasks', array('ctid' => 1001, 'number' => "1100500000000", 'created' => Mongo_db::date(), 'updated' => Mongo_db::date(), 'assigned' => Mongo_db::date(), 'author' => 1, 'assignee_uid' => 9903, 'survey_sid' => 1, 'activity' => array()));
     self::$CI->api_survey_xslt_transform(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']);
     $this->assertArrayHasKey('xml_form', $result);
     self::$CI->api_survey_request_respondents(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']);
     // User assigned to call task.
     // Call task is assigned to survey.
     // User is assigned to survey.
     // Survey is the one data is being submitted for.
     $_POST = array('csrf_aw_datacollection' => self::$CI->security->get_csrf_hash(), 'respondent' => array('ctid' => 1001, 'form_data' => '<valid><tag/></valid>'));
     self::$CI->api_survey_enketo_form_submit(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']);
     /////////////////////////////////////////////////////////////////
     // Test again with correct status restrictions.
     $mock_config = self::$status_resctriction_config;
     $mock_config['enketo collect data'] = array(Survey_entity::STATUS_OPEN);
     $mock_config['enketo testrun'] = array(Survey_entity::STATUS_OPEN);
     $this->_set_status_restrictions($mock_config);
     self::$CI->api_survey_xslt_transform(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']);
     $this->assertArrayHasKey('xml_form', $result);
     self::$CI->api_survey_request_respondents(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']);
     // User assigned to call task.
     // Call task is assigned to survey.
     // User is assigned to survey.
     // Survey is the one data is being submitted for.
     $_POST = array('csrf_aw_datacollection' => self::$CI->security->get_csrf_hash(), 'respondent' => array('ctid' => 1001, 'form_data' => '<valid><tag/></valid>'));
     self::$CI->api_survey_enketo_form_submit(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']);
     /////////////////////////////////////////////////////////////////
     /////////////////////////////////////////////////////////////////
     // To test the manage agents api we need an admin.
     $this->_change_user(9901);
     // Logged user 9901.
     // User is administrator.
     // Create survey.
     // Status open.
     // Valid xml file.
     $survey = Survey_entity::build(array('sid' => 2, 'status' => Survey_entity::STATUS_OPEN, 'files' => array('xml' => 'valid_survey.xml'), 'agents' => array()));
     self::$CI->survey_model->save($survey);
     // Create new agent.
     // Absolute minimum properties for the test.
     $user_agent = User_entity::build(array('uid' => 8801, 'status' => User_entity::STATUS_ACTIVE, 'roles' => array(ROLE_CC_AGENT)));
     self::$CI->user_model->save($user_agent);
     // Set conditions.
     $mock_config = self::$status_resctriction_config;
     $mock_config['manage agents'] = array(Survey_entity::STATUS_DRAFT);
     $this->_set_status_restrictions($mock_config);
     // User is an agent.
     // Action assign
     $_POST = array('uid' => 8801, 'action' => 'assign', 'csrf_aw_datacollection' => self::$CI->security->get_csrf_hash());
     self::$CI->api_survey_manage_agents(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']);
     /////////////////////////////////////////////////////////////////
     // Set conditions.
     $mock_config = self::$status_resctriction_config;
     $mock_config['manage agents'] = array(Survey_entity::STATUS_OPEN);
     $this->_set_status_restrictions($mock_config);
     // User is an agent.
     // Action assign
     $_POST = array('uid' => 8801, 'action' => 'assign', 'csrf_aw_datacollection' => self::$CI->security->get_csrf_hash());
     self::$CI->api_survey_manage_agents(1);
     $result = json_decode(self::$CI->output->get_output(), TRUE);
     $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']);
 }
 /**
  * Creates Survey_entity injecting dependencies.
  * Input params must be the same as in the __construct
  *
  * @access public
  * @static
  *
  * @param array
  *   Survey data to construct the survey.
  *
  * @return Survey_entity
  */
 public static function build($survey_data)
 {
     $survey = new Survey_entity($survey_data);
     $CI = get_instance();
     $CI->config->load('status_restrictions');
     // Inject dependencies.
     $survey->set_status_restrictions_array($CI->config->item('status_restrictions'))->set_file_location($CI->config->item('aw_survey_files_location'));
     return $survey;
 }
 public function test_status_restrictions()
 {
     // Local vars
     $draft = Survey_entity::STATUS_DRAFT;
     $open = Survey_entity::STATUS_OPEN;
     $closed = Survey_entity::STATUS_CLOSED;
     $canceled = Survey_entity::STATUS_CANCELED;
     $mock_restrictions = array('restriction 1' => array($draft, $open), 'restriction 2' => array($draft, $open, $closed), 'restriction 3' => array($draft, $canceled), 'restriction 4' => array($open));
     $mock_draft = array('status' => $draft);
     $survey_draft = new Survey_entity($mock_draft);
     $survey_draft->set_status_restrictions_array($mock_restrictions);
     $this->assertTrue($survey_draft->status_allows('restriction 1'));
     $this->assertTrue($survey_draft->status_allows('restriction 2'));
     $this->assertTrue($survey_draft->status_allows('restriction 3'));
     $this->assertFalse($survey_draft->status_allows('restriction 4'));
     $mock_open = array('status' => $open);
     $survey_open = new Survey_entity($mock_open);
     $survey_open->set_status_restrictions_array($mock_restrictions);
     $this->assertTrue($survey_open->status_allows('restriction 1'));
     $this->assertTrue($survey_open->status_allows('restriction 2'));
     $this->assertFalse($survey_open->status_allows('restriction 3'));
     $this->assertTrue($survey_open->status_allows('restriction 4'));
     $mock_canceled = array('status' => $canceled);
     $survey_canceled = new Survey_entity($mock_canceled);
     $survey_canceled->set_status_restrictions_array($mock_restrictions);
     $this->assertFalse($survey_canceled->status_allows('restriction 1'));
     $this->assertFalse($survey_canceled->status_allows('restriction 2'));
     $this->assertTrue($survey_canceled->status_allows('restriction 3'));
     $this->assertFalse($survey_canceled->status_allows('restriction 4'));
 }
<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
/**
 * File survey2.fix.php
 * Fixtures for survey2
 * 
 * Since the id comes from autoincrement, the loading order is important. 
 */
// Copy files for survey : Handlebars vs something else
copy('resources/fixtures_data/survey_2_xls.xls', 'files/surveys/survey_2_xls.xls');
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())));
Beispiel #6
0
 /**
  * Checks if the submitted status is valid
  * Form validation callback.
  */
 public function _cb_survey_status_valid($status)
 {
     if (!Survey_entity::is_valid_status($status)) {
         $this->form_validation->set_message('_cb_survey_status_valid', 'The %s is not valid.');
         return FALSE;
     }
     return TRUE;
 }
                    <a href="#" class="bttn bttn-default-light bttn-small bttn-dropdown <?php 
        echo $survey->get_status_html_class();
        ?>
 <?php 
        echo empty($available_statuses) ? 'disabled' : '';
        ?>
" data-dropdown="action-bttn"><?php 
        echo $survey->get_status_label();
        ?>
</a>
                    <ul class="action-dropdown for-bttn-small">
                      <?php 
        foreach ($available_statuses as $status_code) {
            ?>
                      <li><?php 
            echo anchor_csrf($survey->get_url_change_status($status_code), Survey_entity::status_label($status_code), array('class' => Survey_entity::status_html_class($status_code), 'data-confirm-action' => "Status changes are irreversible. Are you sure you want to change the status to <em>" . Survey_entity::status_label($status_code) . "</em>?", 'data-confirm-title' => "Change survey status"));
            ?>
</li>
                      <?php 
        }
        ?>
                    </ul>
                  </li>
                </ul>
              </div>
            </article>
            <?php 
    }
    ?>
            
            <?php