/wp-content/plugins/h5p/public/class-h5p-event.php

https://github.com/livinglab/openlab · PHP · 85 lines · 47 code · 11 blank · 27 comment · 2 complexity · 9f1fb6cd7b63ab4b147299d7451aa7da MD5 · raw file

  1. <?php
  2. /**
  3. * Makes it easy to track events throughout the H5P system.
  4. *
  5. * @package H5P
  6. * @copyright 2016 Joubel AS
  7. * @license MIT
  8. */
  9. class H5P_Event extends H5PEventBase {
  10. private $user;
  11. /**
  12. * Adds event type, h5p library and timestamp to event before saving it.
  13. *
  14. * @param string $type
  15. * Name of event to log
  16. * @param string $library
  17. * Name of H5P library affacted
  18. */
  19. function __construct($type, $sub_type = NULL, $content_id = NULL, $content_title = NULL, $library_name = NULL, $library_version = NULL) {
  20. // Track the user who initiated the event as well
  21. $current_user = wp_get_current_user();
  22. $this->user = $current_user->ID;
  23. parent::__construct($type, $sub_type, $content_id, $content_title, $library_name, $library_version);
  24. }
  25. /**
  26. * Store the event.
  27. */
  28. protected function save() {
  29. global $wpdb;
  30. // Get data in array format without NULL values
  31. $data = $this->getDataArray();
  32. $format = $this->getFormatArray();
  33. // Add user
  34. $data['user_id'] = $this->user;
  35. $format[] = '%d';
  36. // Insert into DB
  37. $wpdb->insert("{$wpdb->prefix}h5p_events", $data, $format);
  38. $this->id = $wpdb->insert_id;
  39. return $this->id;
  40. }
  41. /**
  42. * Count number of events.
  43. */
  44. protected function saveStats() {
  45. global $wpdb;
  46. $type = $this->type . ' ' . $this->sub_type;
  47. $current_num = $wpdb->get_var($wpdb->prepare(
  48. "SELECT num
  49. FROM {$wpdb->prefix}h5p_counters
  50. WHERE type = '%s'
  51. AND library_name = '%s'
  52. AND library_version = '%s'
  53. ", $type, $this->library_name, $this->library_version));
  54. if ($current_num === NULL) {
  55. // Insert
  56. $wpdb->insert("{$wpdb->prefix}h5p_counters", array(
  57. 'type' => $type,
  58. 'library_name' => $this->library_name,
  59. 'library_version' => $this->library_version,
  60. 'num' => 1
  61. ), array('%s','%s','%s','%d'));
  62. }
  63. else {
  64. // Update num+1
  65. $wpdb->query($wpdb->prepare(
  66. "UPDATE {$wpdb->prefix}h5p_counters
  67. SET num = num + 1
  68. WHERE type = '%s'
  69. AND library_name = '%s'
  70. AND library_version = '%s'
  71. ", $type, $this->library_name, $this->library_version));
  72. }
  73. }
  74. }