<commit_before><?php
/*
    Tatoeba Project, free collaborative creation of multilingual corpuses project
    Copyright (C) 2009  <NAME> <<EMAIL>>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class Contribution extends AppModel {

	var $name = 'Contribution';
	var $actsAs = array("Containable");
	var $belongsTo = array('Sentence', 'User');
    
    /*
    ** get number of contributions made by a given user
    */
    function numberOfContributionsBy($userId){
        return $this->find(
            'count',
            array(
                'conditions' => array( 'Contribution.user_id' => $userId)
                )
            );

    }

    /*
    ** get the Xth best contributors
    */
    function getTopContributors($limit){
        $result = $this->find(
            'all',
            array(
                'order' => 'total DESC',
                'fields' => array(
                    'COUNT(Contribution.id) AS total',
                    'User.username',
                    'User.group_id'
                ),
                'group' => 'Contribution.user_id',
                'conditions' => array (
                    'Contribution.user_id !=' => null,
                    'Contribution.type' => 'sentence',
                    'User.group_id <' => 5
                ),
                'limit' => $limit ,
                'contains' => array (
                    'User' => array (
                        'fields' => array( 'User.username', 'User.group_id'),
                    ),
                
                ) 
            )
        );
        return $result; 

    }
        
    /*TODO*/
    function getContributionsRelatedToSentence($sentenceId){
        $result = $this->find(
            'all',
            array(
                'fields' => array(
                    'Contribution.text',
                    'Contribution.translation_id',
                    'Contribution.action',
                    'Contribution.id',
                    'Contribution.datetime',
                    'User.username',
                    'User.id'
                ),
                'conditions' => array (
                    'Contribution.sentence_id' => $sentenceId     
                ),
                'contains' => array (
                    'User'=> array(
                        'fields' => array('User.username','User.id')
                    ),
                )
            )
        );
        return $result ;
    }
	
	/**
	 * Get last contributions.
     * in a specific language is specified
     * und => any languages
	 */
	function getLastContributions($limit,$lang = 'und'){
        if( strlen ($lang) != 3 OR ! is_numeric($limit) ){
            return array();
        }
        $conditions = array('Contribution.type' => 'sentence');

        if ($lang != 'und'){
            $conditions['Sentence.lang'] = $lang ;
        }

        $query ="
            SELECT 
                `Contribution`.`text`,
                `Contribution`.`translation_id`,
                `Contribution`.`sentence_id`,
                `Contribution`.`action`,
                `Contribution`.`id`,
                `Contribution`.`datetime`,
                `User`.`username`,
                `User`.`id`,
                `Sentence`.`lang`
            FROM `contributions` AS `Contribution` 
                INNER JOIN `sentences` AS `Sentence`
                    ON (`Contribution`.`sentence_id` = `Sentence`.`id`
        ";
        if ($lang != 'und'){ 
            $query .= "AND `Sentence`.`lang` = '$lang'";
        }
        $query.="
                )
                INNER JOIN `users` AS `User`
                    ON (`Contribution`.`user_id` = `User`.`id`)
            WHERE
                `Contribution`.`type` = 'sentence' 
            ";
        if ($lang != 'und'){
            $query .=  "AND `Sentence`.`lang` = '$lang'";
        } 
        $query .= "
            ORDER BY `Contribution`.`datetime` DESC 
            LIMIT $limit; 
        "; 
        

        return  $this->query($query);
        /*
        return $this->find(
            'all', 
			array(
                'fields' => array(
                    'Contribution.text',
                    'Contribution.translation_id',
                    'Contribution.sentence_id',
                    'Contribution.action',
                    'Contribution.id',
                    'Contribution.datetime',
                    'User.username',
                    'User.id',
                    'Sentence.lang'
                ),
				'conditions' => $conditions ,
                'contains'   => array(
                    'User' => array(
                        'fields' => array('User.username','User.id') 
                    ),
                    'Sentence' => array(
                        'fields' => array('Sentence.lang')
                    )
                ),
				'limit' => $limit,
                'order' => 'Contribution.datetime DESC'
			)
		);
        */
	}
	
	/**
	 * Returns number of contributions for each member,
	 * ordered from the highest contributor to the lowest.
	 */	
	function getUsersStatistics(){
		$this->unbindModel(
			array(
				'belongsTo' => array('Sentence')
			)
		);
		$this->recursive = 0;
		$query = array(
			'fields' => array(
				'Contribution.user_id', 'User.id', 'User.username'
				, 'User.since', 'User.group_id', 'COUNT(*) as total'
			),
			'conditions' => array(
				'Contribution.user_id !=' => null
				, 'Contribution.type' => 'sentence'
			),
			'group' => array('Contribution.user_id'),
			'order' => 'total DESC'
		);
		return $this->find('all', $query);
	}	
	
	
	/**
	 * Returns number of contributions for each day.
	 * We only count the number of new sentences, not the
	 * number of modifications.
	 */
	function getActivityTimelineStatistics(){
		$this->Contribution->recursive = 0;
		return $this->find('all', array(
			'fields' => array(
				'Contribution.datetime'
				, 'COUNT(*) as total'
				, 'date_format(datetime,\'%b %D %Y\') as day'
			),
			'conditions' => array(
				'Contribution.datetime > \'2008-01-01 00:00:00\''
				, 'Contribution.translation_id' => null
				, 'Contribution.action' => 'insert'
			),
			'group' => array('day'),
			'order' => 'Contribution.datetime DESC'
		));
	}
}
?>
<commit_msg>re #88 improve contributions request<commit_after><?php
/*
    Tatoeba Project, free collaborative creation of multilingual corpuses project
    Copyright (C) 2009  <NAME> <<EMAIL>>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class Contribution extends AppModel {

	var $name = 'Contribution';
	var $actsAs = array("Containable");
	var $belongsTo = array('Sentence', 'User');
    
    /*
    ** get number of contributions made by a given user
    */
    function numberOfContributionsBy($userId){
        return $this->find(
            'count',
            array(
                'conditions' => array( 'Contribution.user_id' => $userId)
                )
            );

    }

    /*
    ** get the Xth best contributors
    */
    function getTopContributors($limit){
        $result = $this->find(
            'all',
            array(
                'order' => 'total DESC',
                'fields' => array(
                    'COUNT(Contribution.id) AS total',
                    'User.username',
                    'User.group_id'
                ),
                'group' => 'Contribution.user_id',
                'conditions' => array (
                    'Contribution.user_id !=' => null,
                    'Contribution.type' => 'sentence',
                    'User.group_id <' => 5
                ),
                'limit' => $limit ,
                'contains' => array (
                    'User' => array (
                        'fields' => array( 'User.username', 'User.group_id'),
                    ),
                
                ) 
            )
        );
        return $result; 

    }
        
    /*TODO*/
    function getContributionsRelatedToSentence($sentenceId){
        $result = $this->find(
            'all',
            array(
                'fields' => array(
                    'Contribution.text',
                    'Contribution.translation_id',
                    'Contribution.action',
                    'Contribution.id',
                    'Contribution.datetime',
                    'User.username',
                    'User.id'
                ),
                'conditions' => array (
                    'Contribution.sentence_id' => $sentenceId     
                ),
                'contains' => array (
                    'User'=> array(
                        'fields' => array('User.username','User.id')
                    ),
                )
            )
        );
        return $result ;
    }
	
	/**
	 * Get last contributions.
     * in a specific language is specified
     * und => any languages
	 */
	function getLastContributions($limit,$lang = 'und'){
        if( strlen ($lang) != 3 OR ! is_numeric($limit) ){
            return array();
        }
        $conditions = array('Contribution.type' => 'sentence');

        if ($lang != 'und'){
            $conditions['Sentence.lang'] = $lang ;
        }

        $query ="
            SELECT 
                `Contribution`.`text`,
                `Contribution`.`translation_id`,
                `Contribution`.`sentence_id`,
                `Contribution`.`action`,
                `Contribution`.`id`,
                `Contribution`.`datetime`,
                `User`.`username`,
                `User`.`id`,
                `Sentence`.`lang`
            FROM `contributions` AS `Contribution` 
                INNER JOIN `sentences` AS `Sentence`
                    ON (`Contribution`.`sentence_id` = `Sentence`.`id`
        ";
        if ($lang != 'und'){ 
            $query .= "AND `Sentence`.`lang` = '$lang'";
        }
        $query.="
                )
                INNER JOIN `users` AS `User`
                    ON (`Contribution`.`user_id` = `User`.`id`)
            WHERE ";
        if ($lang != 'und'){
            $query .=  "`Sentence`.`lang` = '$lang' AND";
        } 

        $query.="
                `Contribution`.`type` = 'sentence'
            ORDER BY `Contribution`.`datetime` DESC 
            LIMIT $limit; 
        "; 
        

        return  $this->query($query);
        /*
        return $this->find(
            'all', 
			array(
                'fields' => array(
                    'Contribution.text',
                    'Contribution.translation_id',
                    'Contribution.sentence_id',
                    'Contribution.action',
                    'Contribution.id',
                    'Contribution.datetime',
                    'User.username',
                    'User.id',
                    'Sentence.lang'
                ),
				'conditions' => $conditions ,
                'contains'   => array(
                    'User' => array(
                        'fields' => array('User.username','User.id') 
                    ),
                    'Sentence' => array(
                        'fields' => array('Sentence.lang')
                    )
                ),
				'limit' => $limit,
                'order' => 'Contribution.datetime DESC'
			)
		);
        */
	}
	
	/**
	 * Returns number of contributions for each member,
	 * ordered from the highest contributor to the lowest.
	 */	
	function getUsersStatistics(){
		$this->unbindModel(
			array(
				'belongsTo' => array('Sentence')
			)
		);
		$this->recursive = 0;
		$query = array(
			'fields' => array(
				'Contribution.user_id', 'User.id', 'User.username'
				, 'User.since', 'User.group_id', 'COUNT(*) as total'
			),
			'conditions' => array(
				'Contribution.user_id !=' => null
				, 'Contribution.type' => 'sentence'
			),
			'group' => array('Contribution.user_id'),
			'order' => 'total DESC'
		);
		return $this->find('all', $query);
	}	
	
	
	/**
	 * Returns number of contributions for each day.
	 * We only count the number of new sentences, not the
	 * number of modifications.
	 */
	function getActivityTimelineStatistics(){
		$this->Contribution->recursive = 0;
		return $this->find('all', array(
			'fields' => array(
				'Contribution.datetime'
				, 'COUNT(*) as total'
				, 'date_format(datetime,\'%b %D %Y\') as day'
			),
			'conditions' => array(
				'Contribution.datetime > \'2008-01-01 00:00:00\''
				, 'Contribution.translation_id' => null
				, 'Contribution.action' => 'insert'
			),
			'group' => array('day'),
			'order' => 'Contribution.datetime DESC'
		));
	}
}
?>

{'fraction_non_alphanumeric': 0.1319339248358941, 'fraction_numerical': 0.0034624540142826226, 'mean_word_length': 1.4559787422497785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 123, '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': '5870857', 'n_tokens_mistral': 4161, 'n_tokens_neox': 3612, 'n_words': 890}