<commit_before><?php
/**
 * OpenEyes
 *
 * (C) OpenEyes Foundation, 2016
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2016, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

namespace OEModule\OphCoCvi\models;

/**
 * This is the model class for table "et_ophcocvi_eventinfo".
 *
 * The followings are the available columns in table:
 * @property string $id
 * @property integer $event_id
 * @property integer $is_draft
 * @property integer $generated_document_id
 * @property string last_modified_date
 *
 * The followings are the available model relations:
 *
 * @property \ElementType $element_type
 * @property \EventType $eventType
 * @property \Event $event
 * @property \User $user
 * @property \User $usermodified
 * @property \ProtectedFile $generated_document
 * @property Element_OphCoCvi_ClinicalInfo $clinical_element
 * @property Element_OphCoCvi_ClericalInfo $clerical_element
 * @property Element_OphCoCvi_ConsentSignature $consent_element
 * @property Element_OphCoCvi_Demographics $demographics_element
 */

class Element_OphCoCvi_EventInfo extends \BaseEventTypeElement
{
    /**
     * Returns the static model of the specified AR class.
     * @return the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'et_ophcocvi_eventinfo';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('event_id, is_draft, generated_document_id, ', 'safe'),
            //array('is_draft', 'required'),
            array('id, event_id, is_draft, generated_document_id, ', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'element_type' => array(
                self::HAS_ONE,
                'ElementType',
                'id',
                'on' => "element_type.class_name='" . get_class($this) . "'"
            ),
            'eventType' => array(self::BELONGS_TO, 'EventType', 'event_type_id'),
            'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'user' => array(self::BELONGS_TO, 'User', 'created_user_id'),
            'usermodified' => array(self::BELONGS_TO, 'User', 'last_modified_user_id'),
            'generated_document' => array(self::BELONGS_TO, 'ProtectedFile', 'generated_document_id'),
            // define duplicate event relations for elements to be eager loaded in the same call
            'clinical_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'clinical_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ClinicalInfo',
                array('id' => 'event_id'),
                'through' => 'clinical_event'
            ),
            'clerical_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'clerical_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ClericalInfo',
                array('id' => 'event_id'),
                'through' => 'clerical_event'
            ),
            'consent_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'consent_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ConsentSignature',
                array('id' => 'event_id'),
                'through' => 'consent_event'
            ),
            'demographics_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'demographics_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_Demographics',
                array('id' => 'event_id'),
                'through' => 'demographics_event'
            )
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'event_id' => 'Event',
            'is_draft' => 'Is draft',
            'generated_document_id' => 'Generated file',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('event_id', $this->event_id, true);
        $criteria->compare('is_draft', $this->is_draft);
        $criteria->compare('generated_document_id', $this->generated_document_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    public function patientCviCount($patient_id)
    {
        $criteria = new \CDbCriteria;
        $criteria->select = 't.id,t.event_id';
        $criteria->join = 'join event on event.id = t.event_id ';
        $criteria->join .= 'join episode on event.episode_id = episode.id';
        $criteria->condition = 'episode.patient_id = :patient_id';
        $criteria->params = array(':patient_id' => $patient_id);
        $cvis = Element_OphCoCvi_EventInfo::model()->findAll($criteria);
        return $cvis;
    }

    /**
     * @TODO: Should probably be storing a fixed date for issue rather than relying on modified date
     *
     * @return null|string
     */
    public function getIssueDateForDisplay()
    {
        if (!$this->is_draft) {
            return $this->last_modified_date;
        }
        return null;
    }

    /**
     * @return string
     */
    public function getIssueStatusForDisplay()
    {
        if ($this->is_draft) {
            return $this->event->info ? $this->event->info : 'Draft';
        }
        return 'Issued';
    }

    /**
     * Returns an associative array of the data values for printing
     */
    public function getStructuredDataForPrint()
    {
        $result = array();

        return $result;
    }

    public $useContainerView = false;
}
<commit_msg>+ bugfix - make default scope to only retrieve eventinfo elements for CVI when the related event has not been deleted.<commit_after><?php
/**
 * OpenEyes
 *
 * (C) OpenEyes Foundation, 2016
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2016, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

namespace OEModule\OphCoCvi\models;

/**
 * This is the model class for table "et_ophcocvi_eventinfo".
 *
 * The followings are the available columns in table:
 * @property string $id
 * @property integer $event_id
 * @property integer $is_draft
 * @property integer $generated_document_id
 * @property string last_modified_date
 *
 * The followings are the available model relations:
 *
 * @property \ElementType $element_type
 * @property \EventType $eventType
 * @property \Event $event
 * @property \User $user
 * @property \User $usermodified
 * @property \ProtectedFile $generated_document
 * @property Element_OphCoCvi_ClinicalInfo $clinical_element
 * @property Element_OphCoCvi_ClericalInfo $clerical_element
 * @property Element_OphCoCvi_ConsentSignature $consent_element
 * @property Element_OphCoCvi_Demographics $demographics_element
 */

class Element_OphCoCvi_EventInfo extends \BaseEventTypeElement
{
    private $defaultScopeDisabled = false;
    /**
     * Returns the static model of the specified AR class.
     * @return the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'et_ophcocvi_eventinfo';
    }

    /**
     * Sets default scope for events such that we never pull back any rows that have deleted set to 1.
     *
     * @return array of mandatory conditions
     */
    public function defaultScope()
    {
        if ($this->defaultScopeDisabled) {
            return array();
        }

        return array(
            'with' => array(
                'event' => array(
                    'condition' => 'event.deleted = false'
                )
            )
        );
    }

    /**
     * Turn off the default scope
     *
     * @return $this
     */
    public function disableDefaultScope()
    {
        $this->defaultScopeDisabled = true;

        return $this;
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('event_id, is_draft, generated_document_id, ', 'safe'),
            //array('is_draft', 'required'),
            array('id, event_id, is_draft, generated_document_id, ', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
            'element_type' => array(
                self::HAS_ONE,
                'ElementType',
                'id',
                'on' => "element_type.class_name='" . get_class($this) . "'"
            ),
            'eventType' => array(self::BELONGS_TO, 'EventType', 'event_type_id'),
            'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'user' => array(self::BELONGS_TO, 'User', 'created_user_id'),
            'usermodified' => array(self::BELONGS_TO, 'User', 'last_modified_user_id'),
            'generated_document' => array(self::BELONGS_TO, 'ProtectedFile', 'generated_document_id'),
            // define duplicate event relations for elements to be eager loaded in the same call
            'clinical_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'clinical_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ClinicalInfo',
                array('id' => 'event_id'),
                'through' => 'clinical_event'
            ),
            'clerical_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'clerical_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ClericalInfo',
                array('id' => 'event_id'),
                'through' => 'clerical_event'
            ),
            'consent_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'consent_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_ConsentSignature',
                array('id' => 'event_id'),
                'through' => 'consent_event'
            ),
            'demographics_event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'demographics_element' => array(
                self::HAS_ONE,
                'OEModule\OphCoCvi\models\Element_OphCoCvi_Demographics',
                array('id' => 'event_id'),
                'through' => 'demographics_event'
            )
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'event_id' => 'Event',
            'is_draft' => 'Is draft',
            'generated_document_id' => 'Generated file',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('event_id', $this->event_id, true);
        $criteria->compare('is_draft', $this->is_draft);
        $criteria->compare('generated_document_id', $this->generated_document_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    public function patientCviCount($patient_id)
    {
        $criteria = new \CDbCriteria;
        $criteria->select = 't.id,t.event_id';
        $criteria->join = 'join event on event.id = t.event_id ';
        $criteria->join .= 'join episode on event.episode_id = episode.id';
        $criteria->condition = 'episode.patient_id = :patient_id';
        $criteria->params = array(':patient_id' => $patient_id);
        $cvis = Element_OphCoCvi_EventInfo::model()->findAll($criteria);
        return $cvis;
    }

    /**
     * @TODO: Should probably be storing a fixed date for issue rather than relying on modified date
     *
     * @return null|string
     */
    public function getIssueDateForDisplay()
    {
        if (!$this->is_draft) {
            return $this->last_modified_date;
        }
        return null;
    }

    /**
     * @return string
     */
    public function getIssueStatusForDisplay()
    {
        if ($this->is_draft) {
            return $this->event->info ? $this->event->info : 'Draft';
        }
        return 'Issued';
    }

    /**
     * Returns an associative array of the data values for printing
     */
    public function getStructuredDataForPrint()
    {
        $result = array();

        return $result;
    }

    public $useContainerView = false;
}

{'fraction_non_alphanumeric': 0.1164301816735181, 'fraction_numerical': 0.0017902134995358705, 'mean_word_length': 2.4101288718064664, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '5526928', 'n_tokens_mistral': 4459, 'n_tokens_neox': 4321, 'n_words': 1124}