PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/maythefourth.php

http://github.com/ginatrapani/ThinkUp
PHP | 123 lines | 68 code | 11 blank | 44 comment | 7 complexity | c3dfc6607e04a21252290fab4095c411 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: May The Fourth Be With You
  4. Description: Did you talk about Star Wars?
  5. When: May 4, 2015
  6. */
  7. /**
  8. *
  9. * ThinkUp/webapp/plugins/insightsgenerator/insights/maythefourth.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 StarWarsInsight extends InsightPluginParent implements InsightPlugin {
  35. /**
  36. * Slug for this insight
  37. **/
  38. var $slug = 'starwars';
  39. /**
  40. * Date to run this insight
  41. */
  42. var $run_date = '2015-05-04';
  43. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  44. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  45. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  46. if (Utils::isTest() || date("Y-m-d") == $this->run_date) {
  47. $this->logger->logInfo("Should generate insight", __METHOD__.','.__LINE__);
  48. $hero_image = array(
  49. 'url' => 'https://www.thinkup.com/assets/images/insights/2015-05/starwars.jpg',
  50. 'alt_text' => 'RockTrooper',
  51. 'credit' => 'Photo: JD Hancock',
  52. 'img_link' => 'https://www.flickr.com/photos/jdhancock/4932301604'
  53. );
  54. $post_dao = DAOFactory::getDAO('PostDAO');
  55. $last_year_of_posts = $post_dao->getPostsByUserInRange(
  56. $author_id = $instance->network_user_id,
  57. $network = $instance->network,
  58. $from=date('Y-m-d', strtotime('-1 year')),
  59. $until = date('Y-m-d'),
  60. $order_by='pub_date', $direction='DESC', $iterator=true, $is_public=false
  61. );
  62. $topics = array(
  63. 'force' => array("star wars","force awakens", "bb-8", "darth vader", "bb8", "StarWars",
  64. "StarWarsDay"),
  65. );
  66. $matches = array();
  67. $matched_posts = array();
  68. $matched = false;
  69. //print_r($last_year_of_posts);
  70. foreach ($last_year_of_posts as $post) {
  71. foreach ($topics as $key => $strings) {
  72. foreach ($strings as $string) {
  73. if (preg_match_all('/\b'.strtolower($string).'\b/i', strtolower($post->post_text),
  74. $matches)) {
  75. $matched = true;
  76. }
  77. //DEBUG
  78. // else {
  79. // $this->logger->logInfo("Didn't find ".$string." in ".$post->post_text,
  80. // __METHOD__.','.__LINE__);
  81. // }
  82. }
  83. }
  84. if ($matched) {
  85. $this->logger->logInfo("Matched post ".$post->post_text, __METHOD__.','.__LINE__);
  86. $matched_posts[] = $post;
  87. }
  88. $matched = false;
  89. }
  90. if (count($matched_posts) > 0) {
  91. $headline = "The Force is strong with " . $this->username ." on #StarWarsDay";
  92. $insight_text = $this->username
  93. . " was ready for Star Wars Day. May the fourth be with you... always.";
  94. $insight = new Insight();
  95. $insight->instance_id = $instance->id;
  96. $insight->slug = $this->slug;
  97. $insight->date = $this->run_date;
  98. $insight->headline = $headline;
  99. $insight->text = $insight_text;
  100. $insight->filename = basename(__FILE__, ".php");
  101. $insight->emphasis = Insight::EMPHASIS_HIGH;
  102. $insight->setHeroImage($hero_image);
  103. $matched_posts_sliced = array_slice($matched_posts, 0, 20);
  104. $insight->setPosts($matched_posts_sliced);
  105. $this->insight_dao->insertInsight($insight);
  106. }
  107. }
  108. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  109. }
  110. }
  111. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  112. $insights_plugin_registrar->registerInsightPlugin('StarWarsInsight');