PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/lovewins.php

http://github.com/ginatrapani/ThinkUp
PHP | 123 lines | 76 code | 11 blank | 36 comment | 9 complexity | 01b743ecc911479d52a77c9c3318ade3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Love Wins
  4. Description: Did you celebrate the SCOTUS decision on marriage equality? #LoveWins
  5. When: June 26-28, 2015
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/lovewins.php
  10. *
  11. * Copyright (c) 2015 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) 2015 Gina Trapani
  29. *
  30. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  31. * @license http://www.gnu.org/licenses/gpl.html
  32. * @copyright 2015 Gina Trapani
  33. */
  34. class LoveWinsInsight extends InsightPluginParent implements InsightPlugin {
  35. /**
  36. * Slug for this insight
  37. **/
  38. var $slug = 'lovewins';
  39. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  40. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  41. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  42. $has_never_run = !$this->insight_dao->doesInsightExist($this->slug, $instance->id);
  43. if (Utils::isTest() || ($has_never_run
  44. && (date("Y-m-d") == '2015-06-27' || date("Y-m-d") == '2015-06-28') )
  45. ) {
  46. $this->logger->logInfo("Should generate insight", __METHOD__.','.__LINE__);
  47. $hero_image = array(
  48. 'url' => 'https://www.thinkup.com/assets/images/insights/2015-06/white-house-rainbow.jpg',
  49. 'alt_text' => '1600 Pennsylvania Avenue',
  50. 'credit' => 'Photo: Jason Goldman',
  51. 'img_link' => 'https://twitter.com/Goldman44/status/614599247959322624'
  52. );
  53. $topics = array(
  54. 'lovewins' => array("LoveWins","marriage equality", "scotus", "gay marriage", "pride"),
  55. );
  56. $matches = array();
  57. $matched_posts = array();
  58. $matched = false;
  59. foreach ($last_week_of_posts as $post) {
  60. foreach ($topics as $key => $strings) {
  61. foreach ($strings as $string) {
  62. if (preg_match_all('/\b'.strtolower($string).'\b/i', strtolower($post->post_text),
  63. $matches)) {
  64. $matched = true;
  65. $this->logger->logInfo("FOUND ".$string." in ".$post->post_text,
  66. __METHOD__.','.__LINE__);
  67. }
  68. //DEBUG
  69. else {
  70. $this->logger->logInfo("Didn't find ".$string." in ".$post->post_text,
  71. __METHOD__.','.__LINE__);
  72. }
  73. }
  74. }
  75. if ($matched) {
  76. $this->logger->logInfo("Matched post ".$post->post_text, __METHOD__.','.__LINE__);
  77. $matched_posts[] = $post;
  78. }
  79. $matched = false;
  80. }
  81. if (count($matched_posts) > 0) {
  82. if ($instance->network == 'facebook') {
  83. $headline = $this->username ." had enough pride for all 50 states";
  84. $insight_text = $this->username
  85. .' joined the <a href="https://facebook.com/celebratepride">marriage equality celebration</a> '
  86. .'this week!';
  87. } else {
  88. $headline = $this->username." joined the #LoveWins celebration";
  89. $insight_text = $this->username
  90. .' was all about <a href="https://twitter.com/hashtag/LoveWins">marriage equality</a> '
  91. .'this week.';
  92. }
  93. $insight = new Insight();
  94. $insight->instance_id = $instance->id;
  95. $insight->slug = $this->slug;
  96. $insight->date = date("Y-m-d");
  97. $insight->headline = $headline;
  98. $insight->text = $insight_text;
  99. $insight->filename = basename(__FILE__, ".php");
  100. $insight->emphasis = Insight::EMPHASIS_HIGH;
  101. $insight->setHeroImage($hero_image);
  102. $matched_posts_sliced = array_slice($matched_posts, 0, 5);
  103. $insight->setPosts($matched_posts_sliced);
  104. $this->insight_dao->insertInsight($insight);
  105. }
  106. }
  107. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  108. }
  109. }
  110. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  111. $insights_plugin_registrar->registerInsightPlugin('LoveWinsInsight');