<commit_before><?php

/**
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class Survey_model extends CI_Model {

    private $db_subdomain;
    private $subdomain;

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('subdomain', 'url', 'string', 'http'));
        //$this->load->model('Account_model');
        $this->subdomain = get_subdomain();
        $this->ONLINE_SUBDOMAIN_SUFFIX = '-0';
        $this->db_subdomain = ( $this->_has_subdomain_suffix() ) ? substr($this->subdomain, 0, strlen($this->subdomain)-strlen($this->ONLINE_SUBDOMAIN_SUFFIX)) : $this->subdomain;
        log_message('debug', 'Survey model initalized');
    }
    
    // returns true if a requested survey form is live / published, used for manifest
    public function is_live_survey($survey_subdomain = NULL)
    {
        //since management of surveys is done at the OpenRosa-compliant server, best we could do here is see if the form_id is present
        //in the current /formList
        return TRUE;      
    }
    
    // returns true if a requested survey or template exists in db
    public function is_launched_survey()
    {     
        return ($this->subdomain) ? ( ($this->_get_item('subdomain', FALSE)) ? TRUE : FALSE ) : NULL;
    }
    
    // returns true if a requested survey or template exists and is active
    public function is_active_survey()
    {     
        return ($this->subdomain) ? (($this->_is_active()) ? TRUE : FALSE ) : NULL;
    }

    public function get_form_props()
    {
        return ($this->subdomain) ? $this->_get_items(array('server_url', 'form_id', 'hash', 'media_hash', 'xsl_version'), TRUE) : NULL;
    }

    public function get_server_url()
    {
        return ($this->subdomain) ? $this->_get_item('server_url') : NULL;
    }

    public function get_form_id()
    {        
        return ($this->subdomain) ? $this->_get_item('form_id') : NULL;
    }

    public function get_data_url()
    {
        return ($this->subdomain) ? $this->_get_item('data_url') : NULL;
    }

    public function get_form_submission_url()
    {
        return ($this->subdomain) ? $this->_get_item('submission_url') : NULL;
    }

    public function get_webform_url_if_launched($server_url, $form_id, $options = array('type' => NULL))
    {
        //No need to check quota for simple get, I think
        //If deciding to do this, be aware that the GET/POST /surveys/list will also stop working and is required
        //and is required by enketo_account_manager

        $subdomain = $this->_get_subdomain($server_url, $form_id, TRUE);

        if (!$subdomain) {
            return array(
                'error'     => 'subdomain',
                'message'   => 'could not find form in database'
            );
        }

        $result = $this->_get_webform_urls($subdomain, $server_url, $form_id, $options);

        if (!$result) {
            return array(
                'error'     => 'unknown',
                'message'   => 'unknow error occurred while creating form urls'
            );
        }

        return $result;
    }

    public function get_webform_list($server_url = NULL)
    {
        $surveys = $this->_get_records($server_url);
        foreach ($surveys as $i => $survey) {
            $surveys[$i]['url'] = $this->_get_full_survey_url($survey['subdomain']);
            unset($surveys[$i]['subdomain']);
        }
        return $surveys;
    }

    /**
     * Creates subdomain if form does not yet exist in database
     */
    public function get_webform_url($server_url, $form_id, $quota, $submission_url = NULL, $options = array('type' => NULL))
    {
        $quota_used = $this->_get_record_number($server_url);
        $quota_exceeded_response = array(
            'error'   => 'full',
            'message' => 'the quota for this account has been used up, consider upgrading'
        );

        $existing_active_subdomain = $this->_get_subdomain($server_url, $form_id, TRUE);

        if ( $quota_used == $quota ){
            if (empty($existing_active_subdomain)) {
                return $quota_exceeded_response;
            }
            $subdomain = $existing_active_subdomain;
        } else if ($quota_used > $quota) {
            return $quota_exceeded_response;
        } else {
             //duplicates check in _launch;
            $subdomain = (!empty($existing_active_subdomain)) ? $existing_active_subdomain : $this->_launch($server_url, $form_id, $submission_url);

            if (!$subdomain) {
                return array(<commit_msg>always launch<commit_after><?php

/**
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class Survey_model extends CI_Model {

    private $db_subdomain;
    private $subdomain;

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('subdomain', 'url', 'string', 'http'));
        //$this->load->model('Account_model');
        $this->subdomain = get_subdomain();
        $this->ONLINE_SUBDOMAIN_SUFFIX = '-0';
        $this->db_subdomain = ( $this->_has_subdomain_suffix() ) ? substr($this->subdomain, 0, strlen($this->subdomain)-strlen($this->ONLINE_SUBDOMAIN_SUFFIX)) : $this->subdomain;
        log_message('debug', 'Survey model initalized');
    }
    
    // returns true if a requested survey form is live / published, used for manifest
    public function is_live_survey($survey_subdomain = NULL)
    {
        //since management of surveys is done at the OpenRosa-compliant server, best we could do here is see if the form_id is present
        //in the current /formList
        return TRUE;      
    }
    
    // returns true if a requested survey or template exists in db
    public function is_launched_survey()
    {     
        return ($this->subdomain) ? ( ($this->_get_item('subdomain', FALSE)) ? TRUE : FALSE ) : NULL;
    }
    
    // returns true if a requested survey or template exists and is active
    public function is_active_survey()
    {     
        return ($this->subdomain) ? (($this->_is_active()) ? TRUE : FALSE ) : NULL;
    }

    public function get_form_props()
    {
        return ($this->subdomain) ? $this->_get_items(array('server_url', 'form_id', 'hash', 'media_hash', 'xsl_version'), TRUE) : NULL;
    }

    public function get_server_url()
    {
        return ($this->subdomain) ? $this->_get_item('server_url') : NULL;
    }

    public function get_form_id()
    {        
        return ($this->subdomain) ? $this->_get_item('form_id') : NULL;
    }

    public function get_data_url()
    {
        return ($this->subdomain) ? $this->_get_item('data_url') : NULL;
    }

    public function get_form_submission_url()
    {
        return ($this->subdomain) ? $this->_get_item('submission_url') : NULL;
    }

    public function get_webform_url_if_launched($server_url, $form_id, $options = array('type' => NULL))
    {
        //No need to check quota for simple get, I think
        //If deciding to do this, be aware that the GET/POST /surveys/list will also stop working and is required
        //and is required by enketo_account_manager

        $subdomain = $this->_get_subdomain($server_url, $form_id, TRUE);

        if (!$subdomain) {
            return array(
                'error'     => 'subdomain',
                'message'   => 'could not find form in database'
            );
        }

        $result = $this->_get_webform_urls($subdomain, $server_url, $form_id, $options);

        if (!$result) {
            return array(
                'error'     => 'unknown',
                'message'   => 'unknow error occurred while creating form urls'
            );
        }

        return $result;
    }

    public function get_webform_list($server_url = NULL)
    {
        $surveys = $this->_get_records($server_url);
        foreach ($surveys as $i => $survey) {
            $surveys[$i]['url'] = $this->_get_full_survey_url($survey['subdomain']);
            unset($surveys[$i]['subdomain']);
        }
        return $surveys;
    }

    /**
     * Creates subdomain if form does not yet exist in database
     */
    public function get_webform_url($server_url, $form_id, $quota, $submission_url = NULL, $options = array('type' => NULL))
    {
        $quota_used = $this->_get_record_number($server_url);
        $quota_exceeded_response = array(
            'error'   => 'full',
            'message' => 'the quota for this account has been used up, consider upgrading'
        );

        $existing_active_subdomain = $this->_get_subdomain($server_url, $form_id, TRUE);

        if ( $quota_used == $quota ){
            if (empty($existing_active_subdomain)) {
                return $quota_exceeded_response;
            }
            $subdomain = $existing_active_subdomain;
        } else if ($quota_used > $quota) {
            return $quota_exceeded_response;
        } else {
             //duplicates check in _launch;
            $subdomain = $this->_launch($server_url, $form_id, $submission_url);

            if (!$subdomain) {
                return array(
{'fraction_non_alphanumeric': 0.12132860240488863, 'fraction_numerical': 0.001971220185294697, 'mean_word_length': 2.5086445366528354, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209570', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2945, 'n_words': 808}