/wp-content/plugins/openlab-usage-reporting/includes/report.php

https://github.com/livinglab/openlab · PHP · 201 lines · 135 code · 38 blank · 28 comment · 2 complexity · 4861355be819eeccd546b55dae6f3cc7 MD5 · raw file

  1. <?php
  2. /**
  3. * Registered report callbacks.
  4. *
  5. * @return array
  6. */
  7. function olur_report_callbacks() {
  8. $callbacks = array(
  9. // Users.
  10. 'User' => array(
  11. 'Students' => array( 'label' => 'Students', 'type' => 'student' ),
  12. 'Faculty' => array( 'label' => 'Faculty', 'type' => 'faculty' ),
  13. 'Staff' => array( 'label' => 'Staff', 'type' => 'staff' ),
  14. 'Alumni' => array( 'label' => 'Alumni', 'type' => 'alumni' ),
  15. 'Other' => array( 'label' => 'Other', 'type' => 'other' ),
  16. 'Total' => array( 'label' => 'Total', 'type' => 'total' ),
  17. ),
  18. // Groups.
  19. 'Group' => array(
  20. array( 'label' => 'Courses (Public)', 'type' => 'course', 'status' => 'public' ),
  21. array( 'label' => 'Courses (Private)', 'type' => 'course', 'status' => 'private' ),
  22. array( 'label' => 'Courses (Hidden)', 'type' => 'course', 'status' => 'hidden' ),
  23. array( 'label' => 'Courses (Total)', 'type' => 'course', 'status' => 'any' ),
  24. '',
  25. array( 'label' => 'Clubs (Public)', 'type' => 'club', 'status' => 'public' ),
  26. array( 'label' => 'Clubs (Private)', 'type' => 'club', 'status' => 'private' ),
  27. array( 'label' => 'Clubs (Hidden)', 'type' => 'club', 'status' => 'hidden' ),
  28. array( 'label' => 'Clubs (Total)', 'type' => 'club', 'status' => 'any' ),
  29. '',
  30. array( 'label' => 'Projects (Public)', 'type' => 'project', 'status' => 'public' ),
  31. array( 'label' => 'Projects (Private)', 'type' => 'project', 'status' => 'private' ),
  32. array( 'label' => 'Projects (Hidden)', 'type' => 'project', 'status' => 'hidden' ),
  33. array( 'label' => 'Projects (Total)', 'type' => 'project', 'status' => 'any' ),
  34. ),
  35. // Portfolios.
  36. 'Portfolio' => array(
  37. array( 'label' => 'Student ePortfolios (Public)', 'type' => 'student', 'status' => 'public' ),
  38. array( 'label' => 'Student ePortfolios (Private)', 'type' => 'student', 'status' => 'private' ),
  39. array( 'label' => 'Student ePortfolios (Hidden)', 'type' => 'student', 'status' => 'hidden' ),
  40. array( 'label' => 'Student ePortfolios (Total)', 'type' => 'student', 'status' => 'any' ),
  41. '',
  42. array( 'label' => 'Faculty Portfolios (Public)', 'type' => 'faculty', 'status' => 'public' ),
  43. array( 'label' => 'Faculty Portfolios (Private)', 'type' => 'faculty', 'status' => 'private' ),
  44. array( 'label' => 'Faculty Portfolios (Hidden)', 'type' => 'faculty', 'status' => 'hidden' ),
  45. array( 'label' => 'Faculty Portfolios (Total)', 'type' => 'faculty', 'status' => 'any' ),
  46. '',
  47. array( 'label' => 'Staff Portfolios (Public)', 'type' => 'staff', 'status' => 'public' ),
  48. array( 'label' => 'Staff Portfolios (Private)', 'type' => 'staff', 'status' => 'private' ),
  49. array( 'label' => 'Staff Portfolios (Hidden)', 'type' => 'staff', 'status' => 'hidden' ),
  50. array( 'label' => 'Staff Portfolios (Total)', 'type' => 'staff', 'status' => 'any' ),
  51. ),
  52. // Activity.
  53. 'Activity' => array(
  54. array( 'PROFILES', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users' ),
  55. array( 'label' => 'New Avatar', 'component' => 'profile', 'type' => 'new_avatar' ),
  56. array( 'label' => 'Profile Update', 'component' => 'xprofile', 'type' => 'updated_profile' ),
  57. // @todo These are probably not accurate because of 'site_public'.
  58. '',
  59. array( 'SITES', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users', 'Groups', 'Courses', 'Clubs', 'Projects', 'ePortfolios', 'Portfolios' ),
  60. array( 'label' => 'New Site', 'component' => 'groups', 'type' => 'new_blog' ),
  61. array( 'label' => 'New Site Posts', 'component' => 'groups', 'type' => 'new_blog_post' ),
  62. array( 'label' => 'New Site Comments', 'component' => 'groups', 'type' => 'new_blog_comment' ),
  63. '',
  64. array( 'GROUP FILES', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users', 'Groups', 'Courses', 'Clubs', 'Projects', 'ePortfolios', 'Portfolios' ),
  65. array( 'label' => 'Group File Created', 'component' => 'groups', 'type' => 'added_group_document' ),
  66. array( 'label' => 'Group File Edited', 'component' => 'groups', 'type' => 'edited_group_document' ),
  67. array( 'label' => 'Group File Deleted', 'component' => 'groups', 'type' => 'deleted_group_document' ),
  68. '',
  69. array( 'DISCUSSION FORUMS (since 2014)', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users', 'Groups', 'Courses', 'Clubs', 'Projects', 'ePortfolios', 'Portfolios' ),
  70. array( 'label' => 'New Topics', 'component' => 'groups', 'type' => 'bbp_topic_create' ),
  71. array( 'label' => 'Replies', 'component' => 'groups', 'type' => 'bbp_reply_create' ),
  72. '',
  73. array( 'DOCS', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users', 'Groups', 'Courses', 'Clubs', 'Projects', 'ePortfolios', 'Portfolios' ),
  74. array( 'label' => 'New Doc', 'component' => 'groups', 'type' => 'bp_doc_created' ),
  75. array( 'label' => 'Edit Doc', 'component' => 'groups', 'type' => 'bp_doc_edited' ),
  76. array( 'label' => 'New Doc Comment', 'component' => 'groups', 'type' => 'bp_doc_comment' ),
  77. '',
  78. array( 'GROUP JOINS', 'Total Instances', 'Total Unique Users', 'Students', 'Faculty', 'Staff', 'Alumni', 'Other Users', 'Groups', 'Courses', 'Clubs', 'Projects', 'ePortfolios', 'Portfolios' ),
  79. array( 'label' => 'Joined Group', 'component' => 'groups', 'type' => 'joined_group' ),
  80. ),
  81. // Friendships.
  82. 'Friend' => array(
  83. array( 'FRIENDS (Confirmed/Pending)', 'Student', 'Faculty', 'Staff', 'Alumni', 'Other', 'Total' ),
  84. array( 'label' => 'Student', 'type' => 'student' ),
  85. array( 'label' => 'Faculty', 'type' => 'faculty' ),
  86. array( 'label' => 'Staff', 'type' => 'staff' ),
  87. array( 'label' => 'Alumni', 'type' => 'alumni' ),
  88. array( 'label' => 'Other', 'type' => 'other' ),
  89. array( 'label' => 'Total', 'type' => 'total' ),
  90. ),
  91. );
  92. return $callbacks;
  93. }
  94. /**
  95. * Generate a report and serve as a CSV.
  96. *
  97. * @param string $start MySQL-formatted start date.
  98. * @param string $end MySQL-formatted end date.
  99. */
  100. function olur_generate_report( $start, $end ) {
  101. $data = olur_generate_report_data( $start, $end );
  102. $start_formatted = date( 'Y-m-d', strtotime( $start ) );
  103. $end_formatted = date( 'Y-m-d', strtotime( $end ) );
  104. $filename = sprintf( 'openlab-usage-%s-through-%s.csv', $start_formatted, $end_formatted );
  105. $title_row = array(
  106. sprintf( 'OpenLab usage for the dates %s through %s', $start_formatted, $end_formatted ),
  107. );
  108. $header_row = array(
  109. 0 => '',
  110. 1 => 'Start #',
  111. 2 => '# Created',
  112. 4 => 'End #',
  113. 5 => 'Actively Active',
  114. 6 => 'Passively Active',
  115. );
  116. $fh = @fopen( 'php://output', 'w' );
  117. //fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
  118. header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
  119. header( 'Content-Description: File Transfer' );
  120. header( 'Content-type: text/csv' );
  121. header( "Content-Disposition: attachment; filename={$filename}" );
  122. header( 'Expires: 0' );
  123. header( 'Pragma: public' );
  124. fputcsv( $fh, $title_row );
  125. fputcsv( $fh, $header_row );
  126. foreach ( $data as $data_row ) {
  127. fputcsv( $fh, $data_row );
  128. }
  129. fclose( $fh );
  130. die();
  131. }
  132. /**
  133. * Generate report data.
  134. *
  135. * @param string $start MySQL-formatted start date.
  136. * @param string $end MySQL-formatted end date.
  137. * @return array
  138. */
  139. function olur_generate_report_data( $start, $end ) {
  140. $data = array();
  141. $callbacks = olur_report_callbacks();
  142. foreach ( $callbacks as $class_name => $queries ) {
  143. $class_name = '\OLUR\\' . $class_name;
  144. $counter = new $class_name;
  145. $counter->set_start( $start );
  146. $counter->set_end( $end );
  147. foreach ( $queries as $query ) {
  148. // If the query doesn't have a label, it's a literal.
  149. // Used for blank rows and other labels.
  150. if ( ! isset( $query['label'] ) ) {
  151. $data[] = (array) $query;
  152. } else {
  153. $counter->set_label( $query['label'] );
  154. unset( $query['label'] );
  155. $counter->query( $query );
  156. $data[] = $counter->format_results_for_csv();
  157. }
  158. }
  159. // Insert an empty row after each section.
  160. $data[] = array();
  161. }
  162. return $data;
  163. }