PageRenderTime 85ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/assignment-desk/php/user.php

https://github.com/digitalstrategyworks/Reese-WordPress
PHP | 326 lines | 220 code | 33 blank | 73 comment | 34 complexity | 40df2d95054e845fc3201863962bb58b MD5 | raw file
  1. <?php
  2. if ( !class_exists( 'ad_user' ) ) {
  3. /**
  4. * Class for managing all Assignment Desk user-related views
  5. */
  6. class ad_user
  7. {
  8. function __construct() {
  9. }
  10. function init() {
  11. global $assignment_desk;
  12. add_action('edit_user_profile', array(&$this, 'profile_options'));
  13. add_action('show_user_profile', array(&$this, 'profile_options'));
  14. add_action('show_user_profile', array(&$this, 'profile_statistics'));
  15. add_action('profile_update', array(&$this, 'save_profile_options'));
  16. add_filter('manage_users_columns', array(&$this, 'manage_user_columns'));
  17. add_filter('manage_users_custom_column', array(&$this, 'handle_ad_user_total_words_column'), 10, 3);
  18. add_filter('manage_users_custom_column', array(&$this, 'handle_ad_user_average_words_column'), 10, 3);
  19. add_filter('manage_users_custom_column', array(&$this, 'handle_ad_user_pitches_count_column'), 10, 3);
  20. if ( isset($assignment_desk->public_facing_options['public_facing_volunteering_enabled']) && $assignment_desk->public_facing_options['public_facing_volunteering_enabled'] == 'on' ) {
  21. add_filter('manage_users_custom_column', array(&$this, 'handle_ad_user_volunteer_count_column'), 10, 3);
  22. }
  23. add_filter('manage_users_custom_column', array(&$this, 'handle_ad_user_type_column'), 10, 3);
  24. }
  25. /**
  26. * Add custom columns to the manage_users view.
  27. * @return array The columns
  28. */
  29. function manage_user_columns($user_columns) {
  30. global $assignment_desk;
  31. $custom_fields_to_add = array(
  32. _('_ad_user_type') => __('User Type'),
  33. _('_ad_user_total_words') => __('Total Words'),
  34. _('_ad_user_average_words') => __('Average Words'),
  35. _('_ad_user_pitch_count') => __('Pitches'),
  36. );
  37. if ( $assignment_desk->public_facing_options['public_facing_volunteering_enabled'] == 'on' ) {
  38. $custom_fields_to_add[_('_ad_user_volunteer_count')] = __('Volunteered');
  39. }
  40. foreach ($custom_fields_to_add as $field => $title) {
  41. $user_columns[$field] = $title;
  42. }
  43. return $user_columns;
  44. }
  45. /**
  46. * Filter for displaying the user type custom column
  47. * @return string The content of the cell
  48. */
  49. function handle_ad_user_type_column( $default, $column_name, $user_id ) {
  50. global $assignment_desk;
  51. if ( $column_name == __( '_ad_user_type' ) ) {
  52. $user_type_term_name = __('None assigned');
  53. $user_type = (int)get_usermeta($user_id, $assignment_desk->option_prefix.'user_type', true);
  54. $term = get_term($user_type, $assignment_desk->custom_taxonomies->user_type_label);
  55. if($term->name){
  56. $user_type_term_name = $term->name;
  57. }
  58. return $user_type_term_name;
  59. }
  60. return $default;
  61. }
  62. /**
  63. * Filter for displaying the average words custom column
  64. * @param string $default The content of the cell
  65. * @param string $column_name The name of the column
  66. * @param int $user_id The ID of the user
  67. * @return string the content of the cell
  68. */
  69. function handle_ad_user_average_words_column( $default, $column_name, $user_id ) {
  70. if ( $column_name == __( '_ad_user_average_words' ) ) {
  71. return $this->average_words($user_id);
  72. }
  73. return $default;
  74. }
  75. /**
  76. * Returns the average words per post for this user
  77. * @return int The average words per post for the user
  78. */
  79. function average_words( $user_id ) {
  80. $num_posts = get_usernumposts($user_id);
  81. if($num_posts){
  82. return $this->total_words($user_id) / $num_posts;
  83. }
  84. return 0;
  85. }
  86. /**
  87. * Filter for displaying the total words custom column
  88. * @param string $default The content of the cell
  89. * @param string $column_name The name of the column
  90. * @param int $user_id The ID of the user
  91. * @return string The content of the cell
  92. */
  93. function handle_ad_user_total_words_column( $default, $column_name, $user_id ) {
  94. if ( $column_name == __( '_ad_user_total_words' ) ) {
  95. return $this->total_words($user_id);
  96. }
  97. return $default;
  98. }
  99. /**
  100. * Returns the sum of the number of words in any published post where
  101. * user_id is a coauthor and an accepted participant in the writer role.
  102. * @param int $user_id The ID of the user
  103. * @return int The total words for all of the user's posts.
  104. */
  105. function total_words($user_id){
  106. global $assignment_desk, $wpdb;
  107. $total_words = 0;
  108. $user = get_userdata($user_id);
  109. // Get post ID's where this user is a participant
  110. $participant_posts = $wpdb->get_results("SELECT $wpdb->posts.ID
  111. FROM $wpdb->posts
  112. LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
  113. WHERE $wpdb->posts.post_status = 'publish'
  114. AND $wpdb->posts.post_type = 'post'
  115. AND $wpdb->postmeta.meta_key = '_ad_participant_$user->ID'", ARRAY_N);
  116. $post_ids = array();
  117. if ( $participant_posts ) {
  118. foreach($participant_posts as $p){
  119. $post_ids[] = $p[0];
  120. }
  121. }
  122. // @todo - Make this configurable.
  123. $writer_role = get_term_by('name', _('Writer'), $assignment_desk->custom_taxonomies->user_role_label);
  124. // Of all the posts where this user is a participant, which have writers associated with them?
  125. $participant_records = array();
  126. if ( $post_ids ) {
  127. $participant_records = $wpdb->get_results("SELECT * FROM $wpdb->postmeta
  128. WHERE post_id IN (" . implode(', ', $post_ids) . ")
  129. AND meta_key = '_ad_participant_role_{$writer_role->term_id}'");
  130. }
  131. if(!$writer_role){
  132. return 0;
  133. }
  134. // Accumulate post ids where this user is in the Writer role and they've accepted the assignment.
  135. $posts_to_consider = array();
  136. foreach ( $participant_records as $record ) {
  137. $roles = maybe_unserialize($record->meta_value);
  138. if ( 'accepted' == $roles[$user->user_login] ) {
  139. $posts_to_consider[]= $record->post_id;
  140. }
  141. }
  142. $author_posts = $wpdb->get_results("SELECT ID FROM $wpdb->posts
  143. WHERE post_status = 'publish'
  144. AND post_type = 'post'
  145. AND post_author = $user->ID");
  146. foreach( $author_posts as $post ) {
  147. $posts_to_consider[]= $post->ID;
  148. }
  149. error_log('posts ' . $posts_to_consider);
  150. // Add up all of the word counts we stashed in the postmeta during save_post
  151. foreach( $posts_to_consider as $post_id ) {
  152. $words = (int)get_post_meta($post_id, '_ad_word_count', true);
  153. if ( !$words ) {
  154. $words = str_word_count($wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID=$post_id"));
  155. }
  156. $total_words += $words;
  157. }
  158. return $total_words;
  159. }
  160. /**
  161. * Filter for displaying the volunteer count custom column
  162. * @param string $default The content of the cell
  163. * @param string $column_name The name of the column
  164. * @param int $user_id The ID of the user
  165. * @return string The content of the cell
  166. */
  167. function handle_ad_user_volunteer_count_column( $default, $column_name, $user_id ) {
  168. global $assignment_desk, $wpdb;
  169. if ( $column_name == __( '_ad_user_volunteer_count' ) ) {
  170. return $this->volunteer_count($user_id);
  171. }
  172. return $default;
  173. }
  174. /**
  175. * Get the number of times a user has volunteered for a post.
  176. * @param int $user_id The ID of the user
  177. * @return int The number of times a user has volunteered for a post.
  178. */
  179. function volunteer_count( $user_id ) {
  180. $count = 0;
  181. $volunteered_for = get_usermeta($user_id, '_ad_volunteer');
  182. if($volunteered_for){
  183. $count = count($volunteered_for);
  184. }
  185. return $count;
  186. }
  187. /**
  188. * Filter for displaying the pitch count custom column
  189. * @param string $default The content of the cell
  190. * @param string $column_name The name of the column
  191. * @param int $user_id The ID of the user
  192. * @return string The content of the cell
  193. */
  194. function handle_ad_user_pitches_count_column( $default, $column_name, $user_id ) {
  195. global $assignment_desk, $wpdb;
  196. if ( $column_name == __( '_ad_user_pitch_count' ) ) {
  197. return $this->pitch_count($user_id);
  198. }
  199. return $default;
  200. }
  201. /**
  202. * Get the number of posts the user has pitched.
  203. * @param int $user_id The ID of the user
  204. * @return int The number of posts the user has pitched
  205. */
  206. function pitch_count( $user_id ) {
  207. global $wpdb;
  208. $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key='_ad_pitched_by' AND meta_value='$user_id'");
  209. if(!$count){
  210. $count = 0;
  211. }
  212. return $count;
  213. }
  214. /**
  215. * Add custom fields to the user profile form.
  216. */
  217. function profile_options() {
  218. global $profileuser, $assignment_desk;
  219. $user_id = (int)$profileuser->ID;
  220. $user_type = (int)get_usermeta($user_id, $assignment_desk->option_prefix.'user_type', true);
  221. $current_user_type_term = get_term($user_type, $assignment_desk->custom_taxonomies->user_type_label);
  222. ?>
  223. <a name="assignment_desk"></a>
  224. <h3>Assignment Desk Settings</h3>
  225. <table class="form-table">
  226. <tr>
  227. <th>User Type</th>
  228. <?php
  229. if ( current_user_can($assignment_desk->define_editor_permissions) ){
  230. // Need to have 'get'=>'all' so that it will retrieve a custom taxonomy
  231. $user_type_taxonomy = get_terms($assignment_desk->custom_taxonomies->user_type_label, array('get'=>'all'));
  232. ?>
  233. <td><select id="assignment_desk-user_type" name="assignment_desk-user_type">
  234. <option value="0">- None assigned -</option>
  235. <?php foreach ($user_type_taxonomy as $user_type_term) : ?>
  236. <option value="<?php echo $user_type_term->term_id; ?>"<?php if ($user_type_term->term_id == $user_type) { echo ' selected="selected"'; } ?>><?php echo $user_type_term->name; ?></option>
  237. <?php endforeach; ?>
  238. </select>
  239. <p class="setting-description">Indicate whether the user is a
  240. <?php foreach ($user_type_taxonomy as $key => $user_type_term) : ?>
  241. <?php
  242. // @todo Need an "or" if there are only two terms
  243. if ($key >= 2) { break; }
  244. echo $user_type_term->name . ', ';
  245. if ($key == 1) { echo 'etc.'; }
  246. ?>
  247. <?php endforeach; ?>
  248. </p>
  249. </td>
  250. <?php
  251. }
  252. else {
  253. echo "<td>{$current_user_type_term->name}</td>";
  254. }
  255. ?>
  256. </tr>
  257. </table>
  258. <?php
  259. }
  260. function profile_statistics() {
  261. global $profileuser, $assignment_desk;
  262. ?>
  263. <h3>Statistics</h3>
  264. <table class="form-table">
  265. <tr>
  266. <th>Average Words</th><td><?php echo $this->average_words($profileuser->ID); ?></td>
  267. </tr>
  268. <tr>
  269. <th>Total Words</th><td><?php echo $this->total_words($profileuser->ID); ?></td>
  270. </tr>
  271. <tr>
  272. <th>Pitches</th><td><?php echo $this->pitch_count($profileuser->ID); ?></td>
  273. </tr>
  274. <tr>
  275. <th>Volunteered</th><td><?php echo $this->volunteer_count($profileuser->ID); ?></td>
  276. </tr>
  277. </table>
  278. <?php
  279. }
  280. /**
  281. * Update the custom data in the user profile form.
  282. * @param int $user_id The ID of the user
  283. */
  284. function save_profile_options($user_id) {
  285. global $assignment_desk;
  286. if ( current_user_can($assignment_desk->define_editor_permissions) ) {
  287. $user_type = (int)$_POST['assignment_desk-user_type'];
  288. update_usermeta($user_id, $assignment_desk->option_prefix.'user_type', $user_type);
  289. }
  290. }
  291. }
  292. }
  293. ?>