Changeset 13683 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-events/includes/stats/stats-calculator.php
- Timestamp:
- 05/09/2024 08:33:51 AM (2 years ago)
- 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 4 4 5 5 use Exception; 6 use WP_Post;7 use WP_User;8 use GP;9 6 use GP_Locale; 10 7 use GP_Locales; 11 use DateTimeImmutable;12 use DateTimeZone;13 8 14 9 class Stats_Row { 15 10 public int $created; 16 11 public int $reviewed; 12 public int $waiting; 17 13 public int $users; 18 14 public ?GP_Locale $language = null; 19 15 20 public function __construct( $created, $reviewed, $ users, ?GP_Locale $language = null ) {16 public function __construct( $created, $reviewed, $waiting, $users, ?GP_Locale $language = null ) { 21 17 $this->created = $created; 22 18 $this->reviewed = $reviewed; 19 $this->waiting = $waiting; 23 20 $this->users = $users; 24 21 $this->language = $language; … … 104 101 sum(action = 'create') as created, 105 102 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 108 107 where event_id = %d 109 108 group by locale with rollup … … 131 130 } 132 131 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 133 137 $stats_row = new Stats_Row( 134 138 $row->created, 135 139 $row->total - $row->created, 140 $row->waiting, 136 141 $row->users, 137 142 $lang … … 146 151 147 152 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.InterpolatedNotPrepared157 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery158 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching159 // phpcs thinks we're doing a schema change but we aren't.160 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange161 $rows = $wpdb->get_results(162 $wpdb->prepare(163 "164 select user_id, group_concat( distinct locale ) as locales165 from {$gp_table_prefix}event_actions166 where event_id = %d167 group by user_id168 ",169 array(170 $event_id,171 )172 )173 );174 // phpcs:enable175 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.InterpolatedNotPrepared200 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery201 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching202 $all_attendees_ids = $wpdb->get_col(203 $wpdb->prepare(204 "205 select distinct user_id206 from {$gp_table_prefix}event_attendees207 where event_id = %d208 ",209 array(210 $event_id,211 )212 ),213 );214 215 $contributing_ids = $wpdb->get_col(216 $wpdb->prepare(217 "218 select distinct user_id219 from {$gp_table_prefix}event_actions220 where event_id = %d221 ",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.InterpolatedNotPrepared245 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery246 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching247 // phpcs thinks we're doing a schema change but we aren't.248 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange249 $rows = $wpdb->get_results(250 $wpdb->prepare(251 "252 select253 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 users258 from {$gp_table_prefix}event_actions e, {$gp_table_prefix}originals o259 where e.event_id = %d and e.original_id = o.id260 group by o.project_id261 ",262 array(263 $event_id,264 )265 )266 );267 // phpcs:enable268 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;285 153 } 286 154 … … 301 169 return ! empty( $stats->rows() ); 302 170 } 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.InterpolatedNotPrepared317 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery318 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching319 // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange320 $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 < %s324 ",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 }337 171 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)