reports.activity-audit.inc

File

modules/stats/reports.activity-audit.inc
View source
  1. <?php
  2. function stats_report_activity_audit_form() {
  3. $form = array();
  4. // If the Batch module isn't enabled, stop right here!!!
  5. if (!function_exists("batch_set")) {
  6. $form = array();
  7. $form["markup"] = array(
  8. "value" => "<p><b>" . t("To use this report, the Batch module must be enabled. Please ask your administrator
  9. to enable the Batch module.") . "</b></p>",
  10. );
  11. return $form;
  12. }
  13. $form['mark_top'] = array(
  14. 'type' => 'markup',
  15. 'value' => '<p>This report lets you export user activities over a date range to a CSV file.</p>',
  16. );
  17. $form += stats_get_date_range_form_elements(TRUE, '', date('Y-m-d'));
  18. $form['submit_btn'] = array(
  19. 'type' => 'submit',
  20. 'value' => 'Submit',
  21. 'spinner' => TRUE,
  22. 'inline' => TRUE,
  23. );
  24. $form['mark_under_filter'] = array(
  25. 'type' => 'markup',
  26. 'value' => "<div><strong>Note:</strong> Depending on the date range selected, this report may take several minutes to run.</div>
  27. <hr>",
  28. );
  29. // TODO: if we have submitted, then display submission information
  30. // check: $_SESSION["stats_report_activity_audit_batch_results"]
  31. //$html .= "<p>" . l(t("Download CSV"), "stats/download-csv-from-batch-file/$last_batch_id", "filename=" . urlencode("lee_cocur_progress_report__" . format_date(convert_time($last_timestamp))), array('class' => 'button')) . "</p>";
  32. if (isset($_SESSION["stats_report_activity_audit_batch_results"])) {
  33. $html = "";
  34. $last_batch_id = $_SESSION["stats_report_activity_audit_batch_results"]["last_batch_id"];
  35. $last_timestamp = $_SESSION["stats_report_activity_audit_batch_results"]["last_timestamp"];
  36. $html .= "<p>Last run: " . format_date(convert_time($last_timestamp)) . ".</p>
  37. <p>" . l(t("Download CSV"), "stats/download-csv-from-batch-file/$last_batch_id", "filename=" . urlencode("activity_audit__" . format_date(convert_time($last_timestamp))), array('class' => 'button')) . "</p>";
  38. $form['mark_results'] = array(
  39. 'type' => 'markup',
  40. 'value' => $html,
  41. );
  42. }
  43. return $form;
  44. } // form
  45. function stats_report_activity_audit_form_validate($form, $form_state) {
  46. $start = $form_state['values']['start_date'];
  47. $end = $form_state['values']['end_date'];
  48. if ($end == '') $end = date('Y-m-d');
  49. if ($start == '') $start = date('Y-m-d');
  50. if ($start > $end) {
  51. form_error('start_date', t("You have selected a Start date which occurs after the End date. Please review and try again."));
  52. return;
  53. }
  54. } // ... validate
  55. function stats_report_activity_audit_form_submit($form, $form_state) {
  56. unset($_SESSION["stats_report_activity_audit_batch_results"]);
  57. $start = $form_state['values']['start_date'];
  58. $end = $form_state['values']['end_date'];
  59. if ($end == '') $end = date('Y-m-d');
  60. if ($start == '') $start = date('Y-m-d');
  61. $start .= " 00:00:00";
  62. $end .= " 23:23:59";
  63. // Okay, set up the batch....
  64. $batch = array(
  65. "operation" => array("stats_report_activity_audit_perform_batch_operation", array($start, $end)),
  66. "title" => t("Activity Audit - CSV Export - Time Range: ") . $start . ' - ' . $end,
  67. "file" => menu_get_module_path("stats") . "/reports.activity-audit.inc",
  68. "progress_message" => "Processing record @current of @total",
  69. "display_percent" => TRUE,
  70. );
  71. watchdog('stats', "report_activity_audit range:$start to $end", array());
  72. // Set the batch...
  73. batch_set($batch);
  74. } // ... submit
  75. function stats_report_activity_audit_perform_batch_operation(&$batch, $start, $end) {
  76. $csv = '';
  77. $types_exclude = array(
  78. 'access_denied', 'page_not_found', 'ban_ip', 'cron', 'debug', 'http_request', 'lassie', 'nightly_import_routine',
  79. 'night_import_routine', 'php_error', 'system', 'engagements_debug',
  80. 'php_warning', 'saml', 'update_status', 'save_adv_draft', 'save_adv_draft_whatif',
  81. );
  82. //////////////////////////// FIRST TIME ///////////////////////////////
  83. // If this is our first time through, let's init our values.
  84. if (!isset($batch["results"]["total"])) {
  85. // Our first time through. Let's start up.
  86. $batch["results"]["current"] = 0;
  87. $batch["results"]["finished"] = FALSE;
  88. // CSV headers
  89. $csv = '"DB Log ID","User","CWID","User Type","Action","Msg/Details","Timestamp"' . "\n";
  90. $types_not_in = "AND `type` NOT IN ('" . join("','", $types_exclude) . "')";
  91. // We need the "total" number of students in the selected major.
  92. $res = db_query("SELECT COUNT(wid) as count FROM watchdog
  93. WHERE `timestamp` >= :start
  94. AND `timestamp` <= :end
  95. $types_not_in
  96. AND severity = :wdnotice
  97. ORDER BY wid
  98. ", array(':start' => strtotime($start), ':end' => strtotime($end), ':wdnotice' => WATCHDOG_NOTICE));
  99. $cur = db_fetch_array($res);
  100. $batch["results"]["total"] = intval($cur["count"]);
  101. } // if first time through
  102. // Okay, we can now begin the actual batch process.
  103. $current = $batch["results"]["current"];
  104. $total = $batch["results"]["total"];
  105. $limit = mt_rand(150, 250); // how many records to examine for THIS time through the function.
  106. $c = 0; // count of records.
  107. $db = get_global_database_handler();
  108. $types_not_in = "AND `type` NOT IN ('" . join("','", $types_exclude) . "')";
  109. // We need the "total" number of students in the selected major.
  110. $res = db_query("SELECT * FROM watchdog
  111. WHERE `timestamp` >= :start
  112. AND `timestamp` <= :end
  113. $types_not_in
  114. AND severity = :wdnotice
  115. ORDER BY wid
  116. LIMIT $current, $limit
  117. ", array(':start' => strtotime($start), ':end' => strtotime($end), ':wdnotice' => WATCHDOG_NOTICE));
  118. while ($cur = db_fetch_object($res)) {
  119. if ($c >= $limit) {
  120. break;
  121. }
  122. // Get data and add to $csv
  123. $user_id = intval($cur->user_id);
  124. $user_name = $cur->user_name;
  125. $cwid = $cur->cwid;
  126. $type = $cur->type;
  127. $vars = array();
  128. if ($cur->variables) {
  129. $vars = @unserialize($cur->variables);
  130. }
  131. if (!$vars) $vars = array();
  132. $message = strip_tags($cur->message);
  133. if (count($vars) > 0) {
  134. $message = strip_tags(t($message, $vars));
  135. }
  136. if (strlen($message) > 200) {
  137. $message = trim(substr($message, 0, 200)) . "...";
  138. }
  139. // Escape any quotes for CSV
  140. $message = str_replace('"', '""', $message);
  141. if ($user_id === 0 && $type == 'content') {
  142. // This is likely a text message coming in, or an anonymous user simply viewing content. We can skip it.
  143. $c++;
  144. continue;
  145. }
  146. $is_student = intval($cur->is_student);
  147. $is_faculty = intval($cur->is_faculty);
  148. $user_type = "";
  149. if ($is_student === 1) {
  150. $user_type = "student";
  151. }
  152. if ($is_faculty === 1) {
  153. $user_type = "faculty";
  154. }
  155. $ts = intval($cur->timestamp);
  156. $timestamp = date("Y-m-d H:i:s", $ts);
  157. // Add to our CSV variable
  158. //$csv .= '"WID","User","CWID","User Type","Action","Msg/Details","Timestamp"' . "\n";
  159. $csv .= "\"$cur->wid\",\"$user_name\",\"$cwid\",\"$user_type\",\"$type\",\"$message\",\"$timestamp\"" . "\n";
  160. $c++;
  161. } // while $cur
  162. // Write our work to a temporary file in the /files/private dir for this batch.
  163. batch_append_to_batch_file($batch, $csv);
  164. // So that we don't use up all server resources, let's pause every so often.
  165. if (mt_rand(1, 30) == 3) {
  166. sleep(1);
  167. }
  168. // Update our $batch results variables
  169. $batch["results"]["current"] = $current + $c;
  170. if ($batch["results"]["current"] >= $total) {
  171. // We are finished!
  172. $batch["results"]["finished"] = TRUE;
  173. $_SESSION["stats_report_activity_audit_batch_results"]["results"] = $batch['results'];
  174. $_SESSION["stats_report_activity_audit_batch_results"]["last_batch_id"] = $batch["batch_id"];
  175. $_SESSION["stats_report_activity_audit_batch_results"]["last_timestamp"] = time();
  176. fp_add_message(t("Activity audit CSV created successfully. Use the button below to download."));
  177. }
  178. } // ... batch_operation
  179. //

Functions