PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/llamas.php

http://github.com/ginatrapani/ThinkUp
PHP | 117 lines | 71 code | 8 blank | 38 comment | 8 complexity | fa20b50e70040092dd719d46730c326f MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: Lorenzo Llamas
  4. Description: Did you talk about llamas?
  5. When: February 27, 2015
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/llamas.php
  10. *
  11. * Copyright (c) 2015 Anil Dash
  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 Chris Moyer, Anil Dash
  29. *
  30. * @author Chris Moyer chris@inarow.net, Anil Dash anil@thinkup.com
  31. * @license http://www.gnu.org/licenses/gpl.html
  32. * @copyright 2014-2016 Chris Moyer, Anil Dash
  33. */
  34. class LlamasInsight extends InsightPluginParent implements InsightPlugin {
  35. /**
  36. * Slug for this insight
  37. **/
  38. var $slug = 'llamas';
  39. /**
  40. * Date to run this insight
  41. */
  42. var $run_date = '2015-02-27';
  43. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  44. if (Utils::isTest() || date("Y-m-d") == $this->run_date) {
  45. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  46. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  47. $hero_image = array(
  48. 'url' => 'https://www.thinkup.com/assets/images/insights/2015-02/llama.jpg',
  49. 'alt_text' => 'Llama',
  50. 'credit' => 'Photo: Eric Kilby',
  51. 'img_link' => 'https://www.flickr.com/photos/ekilby/8564867495/'
  52. );
  53. $should_generate_insight = self::shouldGenerateWeeklyInsight(
  54. $this->slug,
  55. $instance,
  56. $insight_date = $this->run_date,
  57. $regenerate_existing_insight=false,
  58. $day_of_week=5,
  59. count($last_week_of_posts)
  60. );
  61. if ($should_generate_insight) {
  62. $this->logger->logInfo("Should generate", __METHOD__.','.__LINE__);
  63. $post_dao = DAOFactory::getDAO('PostDAO');
  64. $topics = array(
  65. 'llama' => array("llama","llamas"),
  66. );
  67. $matches = array();
  68. foreach ($last_week_of_posts as $post) {
  69. foreach ($topics as $key => $strings) {
  70. foreach ($strings as $string) {
  71. if (preg_match_all('/\b'.$string.'\b/i', $post->post_text) > 0) {
  72. $matches[$key] = array('term'=>$string, 'post'=>$post);
  73. unset($topics[$key]);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. if (count($matches) == 0) {
  80. $headline = $this->username.' managed to avoid llamageddon!';
  81. $insight_text = "It seems like half the internet was " .
  82. "<a href='http://www.theverge.com/2015/2/26/8116693/live-the-internet-is-going-bananas-".
  83. "for-this-llama-chase'>talking about runaway llamas</a> " .
  84. 'yesterday. Kudos to ' . $this->username . ' for showing a llama restraint.';
  85. } else {
  86. $headline = $this->username." showed a whole llama love";
  87. $insight_text = "Two runaway llamas <a href='http://www.theverge.com/2015/2/26/8116693/live-".
  88. "the-internet-is-going-bananas-for-this-llama-chase'>took over Twitter yesterday</a>" .
  89. ", and like a llama people, " . $this->username . " couldn't resist.";
  90. }
  91. $insight = new Insight();
  92. $insight->instance_id = $instance->id;
  93. $insight->slug = $this->slug;
  94. $insight->date = $this->run_date;
  95. $insight->headline = $headline;
  96. $insight->text = $insight_text;
  97. $insight->filename = basename(__FILE__, ".php");
  98. $insight->emphasis = Insight::EMPHASIS_HIGH;
  99. $insight->setHeroImage($hero_image);
  100. $this->insight_dao->insertInsight($insight);
  101. }
  102. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  103. }
  104. }
  105. }
  106. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  107. $insights_plugin_registrar->registerInsightPlugin('LlamasInsight');