PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/eoyexclamationcount.php

http://github.com/ginatrapani/ThinkUp
PHP | 224 lines | 167 code | 11 blank | 46 comment | 5 complexity | 8b2077d82b0c1e366d61c4675a9f52c9 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Exclamation count (End of Year)
  4. Description: How often you used exclamation points this year!!!!!
  5. When: December 5
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/eoyexclamationcount.php
  10. *
  11. * Copyright (c) 2012-2016 Gina Trapani
  12. *
  13. * LICENSE:
  14. *
  15. * This file is part of ThinkUp (http://thinkup.com).
  16. *
  17. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  18. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  19. * later version.
  20. *
  21. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  22. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  23. * details.
  24. *
  25. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  26. * <http://www.gnu.org/licenses/>.
  27. *
  28. * Copyright (c) 2014-2016 Adam Pash
  29. *
  30. * @author Adam Pash adam.pash@gmail.com
  31. * @license http://www.gnu.org/licenses/gpl.html
  32. * @copyright 2014-2016 Adam Pash
  33. */
  34. class EOYExclamationCountInsight extends InsightPluginParent implements InsightPlugin {
  35. /**
  36. * Slug for this insight
  37. **/
  38. var $slug = 'eoy_exclamation_count';
  39. /**
  40. * Date to run this insight
  41. **/
  42. var $run_date = '12-07';
  43. //staging
  44. //var $run_date = '11-05';
  45. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  46. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  47. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  48. $year = date('Y');
  49. $regenerate = false;
  50. //testing
  51. //$regenerate = true;
  52. $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight(
  53. $this->slug,
  54. $instance,
  55. $insight_date = "$year-$this->run_date",
  56. $regenerate,
  57. $day_of_year = $this->run_date,
  58. $count_related_posts=null,
  59. array('instagram') //exclude instagram
  60. );
  61. if ($should_generate_insight) {
  62. $this->logger->logInfo("Should generate", __METHOD__.','.__LINE__);
  63. $insight = new Insight();
  64. $insight->instance_id = $instance->id;
  65. $insight->slug = $this->slug;
  66. $insight->date = "$year-$this->run_date";
  67. $count = 0;
  68. $post_dao = DAOFactory::getDAO('PostDAO');
  69. $network = $instance->network;
  70. /**
  71. * Track occurences of exclamations per month
  72. */
  73. $point_chart = array();
  74. $last_year_of_posts = $post_dao->getThisYearOfPostsIterator(
  75. $author_id = $instance->network_user_id,
  76. $network = $network
  77. );
  78. $total_posts = 0;
  79. $months = array(
  80. 'Jan',
  81. 'Feb',
  82. 'Mar',
  83. 'Apr',
  84. 'May',
  85. 'Jun',
  86. 'Jul',
  87. 'Aug',
  88. 'Sep',
  89. 'Oct',
  90. 'Nov',
  91. 'Dec'
  92. );
  93. foreach ($months as $month) {
  94. $point_chart[$month] = 0;
  95. }
  96. foreach ($last_year_of_posts as $post) {
  97. if ($this->hasExclamationPoint($post->post_text)) {
  98. $date = new DateTime($post->pub_date);
  99. $month = $date->format('M');
  100. $point_chart[$month]++;
  101. $count++;
  102. }
  103. $total_posts++;
  104. }
  105. $percent = round($count / $total_posts * 100);
  106. $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance);
  107. $qualified_year = "";
  108. if ( date('Y', strtotime($earliest_pub_date)) == date('Y') ) {
  109. if ( date('n', strtotime($earliest_pub_date)) > 1 ) { //not January
  110. //Earliest post was this year; figure out what month we have data since this year
  111. $since = date('F', strtotime($earliest_pub_date));
  112. $qualified_year = " (at least since ".$since.")";
  113. $since_int = date('n', strtotime($earliest_pub_date));
  114. $since_int--;
  115. $point_chart = array_slice($point_chart, $since_int );
  116. }
  117. }
  118. $copy = array(
  119. 'twitter' => array(
  120. 'normal' => array(
  121. 'headline' => "%username's !!!'s of Twitter, %year",
  122. 'body' => "OMG! In %year, %username used exclamation points " .
  123. "in <strong>%total tweets</strong>. That's %percent% " .
  124. "of %username's tweets this year%qualified_year!"
  125. ),
  126. 'none' => array(
  127. 'headline' => "%username was not impressed with %year",
  128. 'body' => "In %year, %username didn't use one exclamation " .
  129. "point on Twitter%qualified_year. Must be holding out for something " .
  130. "really exciting!"
  131. )
  132. ),
  133. 'facebook' => array(
  134. 'normal' => array(
  135. 'headline' => "%username's emphatic %year on Facebook!",
  136. 'body' => "Enthusiasm is contagious, and in %year, %username " .
  137. "spread the excitement in a total of <strong>%total %posts" .
  138. "</strong> containing exclamation points. " .
  139. "That's %percent% of %username's Facebook posts " .
  140. "this year%qualified_year!"
  141. ),
  142. 'none' => array(
  143. 'headline' => "%username was not impressed with %year",
  144. 'body' => "In %year, %username didn't use one exclamation " .
  145. "point on Facebook%qualified_year. Must be holding out for something " .
  146. "really exciting!"
  147. )
  148. )
  149. );
  150. if ($count > 0) {
  151. $type = 'normal';
  152. $rows = array();
  153. $do_include_chart = false;
  154. foreach ($point_chart as $label => $number) {
  155. $rows[] = array('c'=>array(array('v'=>$label), array('v' => $number)));
  156. if ($number >= 4) { //Y-axis always renders 4 points
  157. $do_include_chart = true;
  158. }
  159. }
  160. if ($do_include_chart && sizeof($rows) > 2) {
  161. $insight->setLineChart(array(
  162. 'cols' => array(
  163. array('label' => 'Month', 'type' => 'string'),
  164. array('label' => 'Total', 'type' => 'number'),
  165. ),
  166. 'rows' => $rows
  167. ));
  168. }
  169. } else {
  170. $type = 'none';
  171. }
  172. $headline = $this->getVariableCopy(
  173. array(
  174. $copy[$network][$type]['headline']
  175. ),
  176. array(
  177. 'year' => $year
  178. )
  179. );
  180. $insight_text = $this->getVariableCopy(
  181. array(
  182. $copy[$network][$type]['body']
  183. ),
  184. array(
  185. 'year' => $year,
  186. 'total' => number_format($count),
  187. 'percent' => $percent,
  188. 'qualified_year' => $qualified_year
  189. )
  190. );
  191. $insight->headline = $headline;
  192. $insight->text = $insight_text;
  193. $insight->filename = basename(__FILE__, ".php");
  194. $insight->emphasis = Insight::EMPHASIS_HIGH;
  195. $insight->header_image = $user->avatar;
  196. $this->insight_dao->insertInsight($insight);
  197. }
  198. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  199. }
  200. public function hasExclamationPoint($post_text) {
  201. $does_match = preg_match_all('/!+/', $post_text);
  202. return $does_match;
  203. }
  204. }
  205. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  206. $insights_plugin_registrar->registerInsightPlugin('EOYExclamationCountInsight');