<?php
class ConferenceModerator extends PhonalisaRecord {

    public  $id;
    public  $userId;
    public  $conferenceId;
    public  $moderatorCall;
    public  $announce_file;
    
    public static function myAttributes() {
    
        return array(
                'id'=>'id',
                'userId'=>'userId',
                'conferenceId'=>'conferenceId',
                'moderatorCall'=>'moderatorCall',
                'announce_file'=>'announce_file',
        );
    }
    
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Calendar 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 strtolower('ConferenceModerator');
    }
    
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
               array('id,userId,moderatorCall,announce_file, moderatorCall','safe'), // FIXME 
        );
    }
    
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }
    
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => __('ID'),
            'userId'=>__('CONFERENCE_MODERATOR_MODEL_USER_ID'),
            'conferenceId'=>__('CONFERENCE_MODERATOR_MODEL_CONFERENCE_ID'),
            'moderatorCall'=>__('CONFERENCE_MODERATOR_MODEL_MODERATOR_CALL'),
            'announce_file'=>__('CONFERENCE_MODERATOR_MODEL_ANNOUNCE_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($conferenceId,$order='id')
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        
        $models = $this->findAllByAttributes(array('conferenceId'=>$conferenceId));
        if (empty($models)) {
            $models[] = $this;
        } 
        return new CArrayDataProvider($models, array('pagination'=>false));

    }

    public function getAvailableModerators($conferenceId) {
        
        $models = $this->findAllByAttributes(array('conferenceId'=>$conferenceId));
        $currentUserIds = array();
        $availebleUsers = array();
        foreach ($models as $model) {
            $currentUser = User::model()->findByPk($model->userId);
            if ($currentUser != NULL) {
                $currentUserIds[] = $currentUser->id;
            }
        }
        $sortHelper =array();
        $users = User::model()->findAll();
        foreach ($users as $user) {
            if (!in_array($user->id,$currentUserIds)) {
                   $sortHelper [$user->extension] = $user;
            }
        }
        ksort($sortHelper);
        foreach ($sortHelper as $extension => $user) {
                $userName = getFormatedUsername($user);
                $availebleUsers[$user->id] = $userName;            
        }

        return $availebleUsers;
    }
}