Making WordPress.org


Ignore:
Timestamp:
05/09/2024 08:33:51 AM (2 years ago)
Author:
amieiro
Message:

Translate: Sync "Translation Events" from GitHub

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-events/includes/stats/stats-calculator.php

    r13565 r13683  
    44
    55use Exception;
    6 use WP_Post;
    7 use WP_User;
    8 use GP;
    96use GP_Locale;
    107use GP_Locales;
    11 use DateTimeImmutable;
    12 use DateTimeZone;
    138
    149class Stats_Row {
    1510        public int $created;
    1611        public int $reviewed;
     12        public int $waiting;
    1713        public int $users;
    1814        public ?GP_Locale $language = null;
    1915
    20         public function __construct( $created, $reviewed, $users, ?GP_Locale $language = null ) {
     16        public function __construct( $created, $reviewed, $waiting, $users, ?GP_Locale $language = null ) {
    2117                $this->created  = $created;
    2218                $this->reviewed = $reviewed;
     19                $this->waiting  = $waiting;
    2320                $this->users    = $users;
    2421                $this->language = $language;
     
    104101                                        sum(action = 'create') as created,
    105102                                        count(*) as total,
    106                                         count(distinct user_id) as users
    107                                 from {$gp_table_prefix}event_actions
     103                                        sum(t.status = 'waiting') as waiting,
     104                                        count(distinct ea.user_id) as users
     105                                from {$gp_table_prefix}event_actions ea
     106                                left join {$gp_table_prefix}translations t ON ea.original_id = t.original_id and ea.user_id = t.user_id
    108107                                where event_id = %d
    109108                                group by locale with rollup
     
    131130                        }
    132131
     132                        if ( is_null( $row->waiting ) ) {
     133                                // The corresponding translations are missing. Could be a unit test or data corruption.
     134                                $row->waiting = 0;
     135                        }
     136
    133137                        $stats_row = new Stats_Row(
    134138                                $row->created,
    135139                                $row->total - $row->created,
     140                                $row->waiting,
    136141                                $row->users,
    137142                                $lang
     
    146151
    147152                return $stats;
    148         }
    149 
    150         /**
    151          * Get contributors for an event.
    152          */
    153         public function get_contributors( int $event_id ): array {
    154                 global $wpdb, $gp_table_prefix;
    155 
    156                 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    157                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
    158                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    159                 // phpcs thinks we're doing a schema change but we aren't.
    160                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
    161                 $rows = $wpdb->get_results(
    162                         $wpdb->prepare(
    163                                 "
    164                                 select user_id, group_concat( distinct locale ) as locales
    165                                 from {$gp_table_prefix}event_actions
    166                                 where event_id = %d
    167                                 group by user_id
    168                         ",
    169                                 array(
    170                                         $event_id,
    171                                 )
    172                         )
    173                 );
    174                 // phpcs:enable
    175 
    176                 $users = array();
    177                 foreach ( $rows as $row ) {
    178                         $user          = new WP_User( $row->user_id );
    179                         $user->locales = explode( ',', $row->locales );
    180                         $users[]       = $user;
    181                 }
    182 
    183                 uasort(
    184                         $users,
    185                         function ( $a, $b ) {
    186                                 return strcasecmp( $a->display_name, $b->display_name );
    187                         }
    188                 );
    189 
    190                 return $users;
    191         }
    192 
    193         /**
    194          * Get attendees without contributions for an event.
    195          */
    196         public function get_attendees_not_contributing( int $event_id ): array {
    197                 global $wpdb, $gp_table_prefix;
    198 
    199                 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    200                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
    201                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    202                 $all_attendees_ids = $wpdb->get_col(
    203                         $wpdb->prepare(
    204                                 "
    205                                 select distinct user_id
    206                                 from {$gp_table_prefix}event_attendees
    207                                 where event_id = %d
    208                         ",
    209                                 array(
    210                                         $event_id,
    211                                 )
    212                         ),
    213                 );
    214 
    215                 $contributing_ids = $wpdb->get_col(
    216                         $wpdb->prepare(
    217                                 "
    218                                 select distinct user_id
    219                                 from {$gp_table_prefix}event_actions
    220                                 where event_id = %d
    221                         ",
    222                                 array(
    223                                         $event_id,
    224                                 )
    225                         )
    226                 );
    227 
    228                 $attendees_not_contributing_ids = array_diff( $all_attendees_ids, $contributing_ids );
    229 
    230                 $attendees_not_contributing = array();
    231                 foreach ( $attendees_not_contributing_ids as $user_id ) {
    232                         $attendees_not_contributing[] = new WP_User( $user_id );
    233                 }
    234 
    235                 return $attendees_not_contributing;
    236         }
    237 
    238         /**
    239          * Get projects for an event.
    240          */
    241         public function get_projects( int $event_id ): array {
    242                 global $wpdb, $gp_table_prefix;
    243 
    244                 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    245                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
    246                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    247                 // phpcs thinks we're doing a schema change but we aren't.
    248                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
    249                 $rows = $wpdb->get_results(
    250                         $wpdb->prepare(
    251                                 "
    252                                 select
    253                                         o.project_id as project,
    254                                         group_concat( distinct e.locale ) as locales,
    255                                         sum(action = 'create') as created,
    256                                         count(*) as total,
    257                                         count(distinct user_id) as users
    258                                 from {$gp_table_prefix}event_actions e, {$gp_table_prefix}originals o
    259                                 where e.event_id = %d and e.original_id = o.id
    260                                 group by o.project_id
    261                         ",
    262                                 array(
    263                                         $event_id,
    264                                 )
    265                         )
    266                 );
    267                 // phpcs:enable
    268 
    269                 $projects = array();
    270                 foreach ( $rows as $row ) {
    271                         $row->project      = GP::$project->get( $row->project );
    272                         $project_name      = $row->project->name;
    273                         $parent_project_id = $row->project->parent_project_id;
    274                         while ( $parent_project_id ) {
    275                                 $parent_project    = GP::$project->get( $parent_project_id );
    276                                 $parent_project_id = $parent_project->parent_project_id;
    277                                 $project_name      = substr( htmlspecialchars_decode( $parent_project->name ), 0, 35 ) . ' - ' . $project_name;
    278                         }
    279                         $projects[ $project_name ] = $row;
    280                 }
    281 
    282                 ksort( $projects );
    283 
    284                 return $projects;
    285153        }
    286154
     
    301169                return ! empty( $stats->rows() );
    302170        }
    303 
    304         /**
    305          * Check if a user is a new translation contributor. A new contributor is a user who has made 10 or fewer translations before event start time.
    306          *
    307          * @param Event_Start_Date $event_start The event start date.
    308          * @param int              $user_id      The user ID.
    309          *
    310          * @return bool True if the user is a new translation contributor, false otherwise.
    311          */
    312         public function is_new_translation_contributor( $event_start, $user_id ) {
    313                 global $wpdb, $gp_table_prefix;
    314                 $new_contributor_max_translation_count = 10;
    315                 $event_start_date_time                 = $event_start->__toString();
    316                 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    317                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
    318                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    319                 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
    320                 $user_translations_count = $wpdb->get_var(
    321                         $wpdb->prepare(
    322                                 "
    323                         select count(*) from {$gp_table_prefix}translations where user_id = %d and date_added < %s
    324                 ",
    325                                 array(
    326                                         $user_id,
    327                                         $event_start_date_time,
    328                                 )
    329                         )
    330                 );
    331 
    332                 if ( get_userdata( $user_id ) && ! $user_translations_count ) {
    333                         return true;
    334                 }
    335                 return $user_translations_count <= $new_contributor_max_translation_count;
    336         }
    337171}
Note: See TracChangeset for help on using the changeset viewer.