mandrill_simplenews_report.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @file
  4. * Mandrill simplenews user activity report.
  5. * Provides user (email address) centric reports. Includes total number of
  6. * newsletters sent, last interaction date, list of newsletters sent with
  7. * their subject, open and click count based on report pulled from
  8. * Mandrill web services.
  9. */
  10. /**
  11. * Implements hook_menu().
  12. */
  13. function mandrill_simplenews_report_menu() {
  14. $items = array();
  15. $items['admin/reports/mandrill/simplenews'] = array(
  16. 'title' => 'Simplenews',
  17. 'page callback' => array('mandrill_simplenews_report_newsletter'),
  18. 'access arguments' => array('view mandrill reports'),
  19. 'type' => MENU_LOCAL_TASK,
  20. );
  21. $items['admin/reports/mandrill/simplenews/%taxonomy_term'] = array(
  22. 'title' => 'Newsletter subscription',
  23. 'page callback' => array('mandrill_simplenews_report_newsletter_subscription'),
  24. 'page arguments' => array(4),
  25. 'access arguments' => array('view mandrill reports'),
  26. 'type' => MENU_CALLBACK,
  27. );
  28. $items['admin/reports/mandrill/simplenews/%taxonomy_term/%/%'] = array(
  29. 'title' => 'Mandrill User Activity Report',
  30. 'page callback' => array('mandrill_simplenews_report_user_activity'),
  31. 'page arguments' => array(4, 5, 6),
  32. 'access arguments' => array('view mandrill reports'),
  33. 'type' => MENU_CALLBACK,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * Page callback to show the simplenews user subscription details in mandrill reports.
  39. */
  40. function mandrill_simplenews_report_newsletter() {
  41. drupal_set_title(t('Newsletter Categories'));
  42. $items = array();
  43. foreach (simplenews_categories_load_multiple() as $list) {
  44. $item = l($list->name, 'admin/reports/mandrill/simplenews/' . $list->tid);
  45. $item .= !empty($list->description) ? '<div class="description">' . $list->description . '</div>' : '';
  46. $items[] = $item;
  47. }
  48. $variables = array(
  49. 'title' => t('Click newsletter categories below to see their reports'),
  50. 'items' => $items,
  51. 'attributes' => array(
  52. 'class' => array('admin-list'),
  53. ),
  54. );
  55. return theme('item_list', $variables);
  56. }
  57. /**
  58. * Page callback to display subscribed user list for newsletter category.
  59. */
  60. function mandrill_simplenews_report_newsletter_subscription($term) {
  61. $tid = $term->tid;
  62. $title = t('Newsletter !title', array('!title' => $term->name));
  63. drupal_set_title($title);
  64. $breadcrumb = array(
  65. l(t('Home'), '<front>'),
  66. l(t('Administration'), 'admin'),
  67. l(t('Reports'), 'admin/reports'),
  68. l(t('Mandrill'), 'admin/reports/mandrill'),
  69. l(t('Newsletter'), 'admin/reports/mandrill/simplenews'),
  70. );
  71. drupal_set_breadcrumb($breadcrumb);
  72. $rows = array();
  73. $header = array(
  74. 'mail' => array('data' => t('Email'), 'field' => 'sn.mail', 'sort' => 'asc'),
  75. 'username' => array('data' => t('Username'), 'field' => 'u.name'),
  76. 'status' => array('data' => t('Status'), 'field' => 'sn.activated'),
  77. 'emails' => array('data' => t('Total no. of emails sent')),
  78. 'last_interaction' => array('data' => t('Last interaction'), 'field' => 'ms.last_interaction'),
  79. 'report' => array('data' => t('Report')),
  80. );
  81. $query = db_select('simplenews_subscriber', 'sn')->extend('PagerDefault')->extend('TableSort');
  82. $query->leftJoin('users', 'u', 'sn.uid = u.uid');
  83. $query->innerJoin('simplenews_subscription', 'su', 'sn.snid = su.snid');
  84. $query->leftJoin('mandrill_simplenews_report_subscription_extra', 'ms', 'su.snid = ms.snid');
  85. $query->condition('su.status', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
  86. $query->condition('su.tid', $tid);
  87. $query->condition('ms.tid', $tid);
  88. $query->addField('u', 'name', 'name');
  89. $query->addField('ms', 'last_interaction', 'last_interaction');
  90. $subscription = $query
  91. ->fields('sn', array('snid', 'activated', 'mail', 'uid', 'language', 'created'))
  92. ->limit(30)
  93. ->orderByHeader($header)
  94. ->execute();
  95. foreach ($subscription as $subscriber) {
  96. $sent_mail_count = db_query("SELECT COUNT(id) FROM {mandrill_simplenews_report_newsletter_sent}
  97. WHERE tid = :tid AND sent_to_mail = :sent_to_mail",
  98. array(':tid' => $tid, ':sent_to_mail' => $subscriber->mail))->fetchField();
  99. $last_interaction = $subscriber->last_interaction;
  100. $rows[] = array(
  101. 'mail' => $subscriber->mail,
  102. 'username' => $subscriber->name,
  103. 'status' => theme('simplenews_status', array('source' => 'activated', 'status' => $subscriber->activated)),
  104. 'emails' => $sent_mail_count,
  105. 'last_interaction' => ((string) (int) $last_interaction === $last_interaction) ? format_date($last_interaction) : format_date($subscriber->timestamp),
  106. 'report' => ($sent_mail_count > 0) ? l(t('See User Activity Report'), 'admin/reports/mandrill/simplenews/' . $tid . '/' . $subscriber->mail . '/' . $subscriber->snid ) : '',
  107. );
  108. }
  109. // @todo: add pager
  110. $empty = t('No subscribers available');
  111. return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $empty));
  112. }
  113. /**
  114. * Page callback to display mandrill user activity reports.
  115. */
  116. function mandrill_simplenews_report_user_activity($term, $mail, $snid) {
  117. $tid = $term->tid;
  118. $output = '';
  119. $newsletter_name = $term->name;
  120. $breadcrumb = array(
  121. l(t('Home'), '<front>'),
  122. l(t('Administration'), 'admin'),
  123. l(t('Reports'), 'admin/reports'),
  124. l(t('Mandrill'), 'admin/reports/mandrill'),
  125. l(t('Newsletter'), 'admin/reports/mandrill/simplenews'),
  126. l($term->name, 'admin/reports/mandrill/simplenews/' . $tid),
  127. );
  128. drupal_set_breadcrumb($breadcrumb);
  129. drupal_set_title(t('User !mail in !newsletter', array('!mail' => $mail, '!newsletter' => $newsletter_name)));
  130. // get the number of emails sent to the subscriber.
  131. $sent_mail_count = db_query("SELECT COUNT(id) FROM {mandrill_simplenews_report_newsletter_sent}
  132. WHERE tid = :tid AND sent_to_mail = :sent_to_mail",
  133. array(':tid' => $tid, ':sent_to_mail' => $mail))->fetchField();
  134. // get the user's subscription date and time for the newsletter term.
  135. $created = db_query("SELECT timestamp FROM {simplenews_subscription} WHERE tid = :tid AND snid = :snid", array(':tid' => $tid, ':snid' => $snid))->fetchField();
  136. // append the member since and mail sent details along with more details.
  137. $caption = '<p>' . t('Member Since : !created', array('!created' => format_date($created))) . '</p>';
  138. $caption .= '<p>' . t('Total number of emails sent : !mail_count', array('!mail_count' => $sent_mail_count)) . '</p>';
  139. $header = array(
  140. array('data' => t('Newsletter Subject'), 'field' => 'sn.subject'),
  141. array('data' => t('Sent time'), 'field' => 'sn.sent_timestamp', 'sort' => 'asc'),
  142. array('data' => t('Opens'), 'field' => 'sn.open_count'),
  143. array('data' => t('Clicks'), 'field' => 'sn.click_count'),
  144. );
  145. // get the user activity details from the table.
  146. $query = db_select('mandrill_simplenews_report_newsletter_sent', 'sn')
  147. ->extend('PagerDefault')
  148. ->extend('TableSort');
  149. $query->fields('sn', array('subject', 'sent_timestamp', 'open_count', 'click_count'));
  150. $query->condition('sent_to_mail', $mail);
  151. $query->condition('tid', $tid);
  152. $result = $query
  153. ->limit(50)
  154. ->orderByHeader($header)
  155. ->execute();
  156. $rows = array();
  157. foreach ($result as $row) {
  158. $rows[]['data'] = array(
  159. 'newsletter_title' => check_plain($row->subject),
  160. 'newsletter_sent_time' => format_date($row->sent_timestamp, 'medium'),
  161. 'open_count' => $row->open_count,
  162. 'click_count' => $row->click_count,
  163. );
  164. }
  165. return array(
  166. 'pager_table' => array(
  167. '#theme' => 'table',
  168. '#header' => $header,
  169. '#rows' => $rows,
  170. '#caption' => $caption,
  171. '#empty' => t('No results founds.'),
  172. ),
  173. 'pager_pager' => array('#theme' => 'pager')
  174. );
  175. }
  176. /**
  177. * Implements hook_cronapi()
  178. */
  179. function mandrill_simplenews_report_cronapi($op, $job = NULL) {
  180. $items['mandrill_simplenews_email_report'] = array(
  181. 'description' => t('To make mandrill api calls to get sent out emails report.'),
  182. 'rule' => '0 */6 * * *',
  183. 'weight' => 11,
  184. 'callback' => 'mandrill_simplenews_mandrill_email_report_cron',
  185. );
  186. $items['mandrill_simplenews_last_interaction'] = array(
  187. 'description' => t('To update the user last interaction time on newsletters'),
  188. 'rule' => '0 */24 * * *',
  189. 'weight' => 13,
  190. 'callback' => 'mandrill_simplenews_update_last_interaction_cron',
  191. );
  192. return $items;
  193. }
  194. /**
  195. * Cron Function callback to get the email report.
  196. */
  197. function mandrill_simplenews_mandrill_email_report_cron() {
  198. $api_key = variable_get('mandrill_api_key', '');
  199. $api_timeout = variable_get('mandrill_api_timeout', 120);
  200. $maildrop_mandrill = new MandrillSimplenewsReport($api_key, $api_timeout);
  201. $maildrop_mandrill->activity_report_import();
  202. }
  203. /**
  204. * Cron Function callback to get the last interaction of user on newsletters.
  205. */
  206. function mandrill_simplenews_update_last_interaction_cron() {
  207. // query the mail and tid from the simplenews_subscription table.
  208. $results = db_query("SELECT sn.snid, sn.mail, su.tid, su.timestamp
  209. FROM simplenews_subscription su
  210. INNER JOIN simplenews_subscriber sn ON su.snid = sn.snid
  211. WHERE status = :status", array(':status' => 1));
  212. foreach ($results as $result) {
  213. // get the last time when the user opened the newsletter mail.
  214. $last_interaction = db_query_range("SELECT sent_timestamp
  215. FROM {mandrill_simplenews_report_newsletter_sent}
  216. WHERE tid = :tid AND open_count > :open_count AND sent_to_mail = :sent_to_mail
  217. ORDER BY sent_timestamp DESC", 0, 1,
  218. array(':tid' => $result->tid, ':open_count' => 0, ':sent_to_mail' => $result->mail))->fetchField();
  219. $time = ((string) (int) $last_interaction === $last_interaction) ? $last_interaction : $result->timestamp;
  220. // if time is not empty, update in md_simplenews_subscription_extra table.
  221. if ($time) {
  222. $update_subscription = db_merge('mandrill_simplenews_report_subscription_extra')
  223. ->key(array(
  224. 'tid' => $result->tid,
  225. 'snid' => $result->snid,
  226. ))
  227. ->fields(array(
  228. 'snid' => $result->snid,
  229. 'tid' => $result->tid,
  230. 'last_interaction' => $time,
  231. ))
  232. ->execute();
  233. }
  234. }
  235. watchdog('mandrill_simplenews_last_interaction', 'last interaction time updated successfully');
  236. }