Skip to content

Commit

Permalink
Combine multi-service user insights int a single stream at insights.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Jul 30, 2012
1 parent 092f407 commit e56d119
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 38 deletions.
76 changes: 76 additions & 0 deletions tests/TestOfInsightMySQLDAO.php
Expand Up @@ -207,4 +207,80 @@ public function testDeleteInsightsBySlug() {
$result = $dao->deleteInsightsBySlug('avg_replies_per_week_another_slug', 1);
$this->assertFalse($result);
}

public function testGetPublicInsights() {
$builders = array();
//insert a public instance
$builders[] = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>10,
'network_username'=>'jack', 'network'=>'twitter', 'network_viewer_id'=>10,
'crawler_last_run'=>'1988-01-20 12:00:00', 'is_active'=>1, 'is_public'=>0));
//insert a private instance
$builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>12,
'network_username'=>'jill', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>1, 'is_public'=>1));

//insert 2 insights for a private instance and 3 for a public instance
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-03', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));

//assert that page of insights is only 3 long for public instane
$dao = new InsightMySQLDAO();
$results = $dao->getPublicInsights($page_count=10, $page_number=1);
$this->assertEqual(sizeof($results), 3);
foreach ($results as $result) {
$this->assertTrue(isset($result->instance));
}
}

public function testGetAllInsights() {
$builders = array();
//insert a public instance
$builders[] = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>10,
'network_username'=>'jack', 'network'=>'twitter', 'network_viewer_id'=>10,
'crawler_last_run'=>'1988-01-20 12:00:00', 'is_active'=>1, 'is_public'=>0));
//insert a private instance
$builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>12,
'network_username'=>'jill', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>1, 'is_public'=>1));

//insert 2 insights for a private instance and 3 for a public instance
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-03', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));

//assert that page of insights includes from both private and public
$dao = new InsightMySQLDAO();
$results = $dao->getAllInstanceInsights($page_count=10, $page_number=1);
$this->assertEqual(sizeof($results), 7);
foreach ($results as $result) {
$this->assertTrue(isset($result->instance));
}

/** TODO: Test where insight text = '' or instance is not active */
}
}
59 changes: 59 additions & 0 deletions webapp/_lib/controller/class.InsightStreamController.php
@@ -0,0 +1,59 @@
<?php
/**
*
* ThinkUp/webapp/_lib/controller/class.InsightStreamController.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Insights stream controller
*
* Displays a list of insights for authenticated service users.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
class InsightStreamController extends ThinkUpController {
public function control() {
$this->setViewTemplate('insights.tpl');
$this->addToView('enable_bootstrap', true);

if ($this->shouldRefreshCache() ) {
$insight_dao = DAOFactory::getDAO('InsightDAO');
if (Session::isLoggedIn()) {
if ($this->isAdmin()) {
///show all insights for all service users
$insights = $insight_dao->getAllInstanceInsights($page_count=10, $page_number=1);
$this->addToView('insights', $insights);
$page = (isset($_GET['page']) && is_numeric($_GET['page']))?$_GET['page']:1;
$this->addToView('next_page', $page+1);
$this->addToView('last_page', $page-1);
} else {
//show only service users owner owns
}
} else {
//show just public service users in stream
$insights = $insight_dao->getPublicInsights($page_count=10, $page_number=1);
$this->addToView('insights', $insights);
}
}
return $this->generateView();
}
}
6 changes: 5 additions & 1 deletion webapp/_lib/model/class.Insight.php
Expand Up @@ -81,7 +81,11 @@ class Insight {
const EMPHASIS_LOW = 0;
public function __construct($row = false) {
if ($row) {
$this->id = $row['id'];
if (isset($row["insight_key"])) {
$this->id = $row["insight_key"];
} else {
$this->id = $row['id'];
}
$this->instance_id = $row['instance_id'];
$this->slug = $row['slug'];
$this->prefix = $row['prefix'];
Expand Down
48 changes: 48 additions & 0 deletions webapp/_lib/model/class.InsightMySQLDAO.php
Expand Up @@ -150,4 +150,52 @@ public function deleteInsightsBySlug($slug, $instance_id) {
$result = $this->getUpdateCount($ps);
return ($result > 0);
}

public function getPublicInsights($page_count=10, $page_number=1) {
return self::getInsightsForInstances($page_count=10, $page_number=1, $public_only = true);
}

public function getAllInstanceInsights($page_count=10, $page_number=1) {
return self::getInsightsForInstances($page_count=10, $page_number=1, $public_only = false);
}

private function getInsightsForInstances($page_count=10, $page_number=1, $public_only = true) {
$start_on_record = ($page_number - 1) * $page_count;
$q = "SELECT i.*, i.id as insight_key, su.* FROM #prefix#insights i ";
$q .= "INNER JOIN #prefix#instances su ON i.instance_id = su.id WHERE su.is_active = 1 ";
if ($public_only) {
$q .= "AND su.is_public = 1 ";
}
$q .= " AND i.text != '' ORDER BY date DESC, emphasis DESC, i.id DESC LIMIT :start_on_record, :limit;";
$vars = array(
":start_on_record"=>(int)$start_on_record,
":limit"=>(int)$page_count
);
if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
$ps = $this->execute($q, $vars);
$rows = $this->getDataRowsAsArrays($ps, "Insight");
$insights = array();
foreach ($rows as $row) {
$insight = new Insight($row);
$insight->instance = new Instance($row);
$insights[] = $insight;
}
foreach ($insights as $insight) {
$insight->related_data = unserialize($insight->related_data);
if ($insight->related_data instanceof Post) {
$insight->related_data_type = "post";
} elseif (is_array($insight->related_data)) {
if ($insight->related_data[0] instanceof User) {
$insight->related_data_type = "users";
} elseif ($insight->related_data[0] instanceof Post) {
$insight->related_data_type = "posts";
} elseif (isset($insight->related_data['history'])) {
$insight->related_data_type = "count_history";
}
}
//assume insight came at same time of day as now for relative day notation
$insight->date = $insight->date. " ".date('H').":".date('i');
}
return $insights;
}
}
14 changes: 14 additions & 0 deletions webapp/_lib/model/interface.InsightDAO.php
Expand Up @@ -92,4 +92,18 @@ public function getInsights($instance_id, $page_count=10, $page_number=1);
*/
public function updateInsight($slug, $instance_id, $date, $prefix, $text, $emphasis=Insight::EMPHASIS_LOW,
$related_data=null);
/**
* Get a page of insights for all public users.
* @param int $page_count Number of insight baselines to return
* @param int $page_number Page number
* @return array Insights
*/
public function getPublicInsights($page_count=10, $page_number=1);
/**
* Get a page of insights for all users, public and private.
* @param int $page_count
* @param int $page_number
* @return array Insights
*/
public function getAllInstanceInsights($page_count=10, $page_number=1);
}
4 changes: 2 additions & 2 deletions webapp/_lib/view/_footer.tpl
Expand Up @@ -3,15 +3,15 @@
{/if}


{if $smarty.get.v eq "insights"}
{if $enable_bootstrap}

</div>

<footer class="" style="clear : left;">
<div class="container footer">
<p class="pull-right"><a href="#">Back to top</a></p>
<p><a href="http://thinkup.com/">ThinkUp</a> 2.0&#945; | <a href="http://thinkupapp.com/docs/">Documentation</a> | <a href="https://groups.google.com/forum/?fromgroups#!forum/thinkupapp">Mailing List</a> | <a href="http://twitter.com/thinkup">@thinkup</a></p>
<p>&copy; ThinkUp LLC 2012. It's nice to be nice.</p>
<p>&copy; ThinkUp LLC 2012. It is nice to be nice.</p>
</div>
</footer>

Expand Down
2 changes: 1 addition & 1 deletion webapp/_lib/view/_header.tpl
Expand Up @@ -10,7 +10,7 @@
<link rel="apple-touch-icon-precomposed" href="{$site_root_path}assets/ico/apple-touch-icon-57-precomposed.png">


{if $smarty.get.v eq "insights"}
{if $enable_bootstrap}

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
Expand Down
5 changes: 0 additions & 5 deletions webapp/_lib/view/dashboard.tpl
@@ -1,8 +1,4 @@
{include file="_header.tpl"}
{if $smarty.get.v eq "insights"}
{include file="insights.tpl"}
{else}

{include file="_statusbar.tpl"}

<div class="container_24">
Expand Down Expand Up @@ -172,7 +168,6 @@
</div> <!-- /.clearfix -->
</div> <!-- /.container_24 -->

{/if}


{include file="_footer.tpl"}
9 changes: 6 additions & 3 deletions webapp/_lib/view/insights.tpl
@@ -1,3 +1,5 @@
{include file="_header.tpl"}

<div class="navbar">
<div class="navbar-inner">
<div class="container">
Expand Down Expand Up @@ -68,7 +70,7 @@
<div class="span9">
<div class="alert {if $i->emphasis eq '1'}alert-info{elseif $i->emphasis eq '2'}alert-success{elseif $i->emphasis eq '3'}alert-error{/if} {$i->emphasis} insight-item">
<p>

{$i->instance->network_username} {$i->instance->network|capitalize}<br>
<!-- begin related_data_type attachment data -->
{if $i->related_data_type eq 'users'}
{include file="_insights.users.tpl"}
Expand All @@ -94,15 +96,16 @@
<ul class="pager">
{if $next_page}
<li class="previous">
<a href="{$site_root_path}?{if $smarty.get.v}v={$smarty.get.v}&{/if}{if $smarty.get.u}u={$smarty.get.u}&{/if}{if $smarty.get.n}n={$smarty.get.n|urlencode}&{/if}page={$next_page}" id="next_page" class="pull-left btn btn-small"><i class="icon-arrow-left"></i> Older</a>
<a href="{$site_root_path}insights.php?{if $smarty.get.v}v={$smarty.get.v}&{/if}{if $smarty.get.u}u={$smarty.get.u}&{/if}{if $smarty.get.n}n={$smarty.get.n|urlencode}&{/if}page={$next_page}" id="next_page" class="pull-left btn btn-small"><i class="icon-arrow-left"></i> Older</a>
</li>
{/if}
{if $last_page}
<li class="next">
<a href="{$site_root_path}?{if $smarty.get.v}v={$smarty.get.v}&{/if}{if $smarty.get.u}u={$smarty.get.u}&{/if}{if $smarty.get.n}n={$smarty.get.n|urlencode}&{/if}page={$last_page}" id="last_page" class="pull-right btn btn-small">Newer <i class="icon-arrow-right"></i></a>
<a href="{$site_root_path}insights.php?{if $smarty.get.v}v={$smarty.get.v}&{/if}{if $smarty.get.u}u={$smarty.get.u}&{/if}{if $smarty.get.n}n={$smarty.get.n|urlencode}&{/if}page={$last_page}" id="last_page" class="pull-right btn btn-small">Newer <i class="icon-arrow-right"></i></a>
</li>
{/if}
</ul>

</div>
</div>
{include file="_footer.tpl"}
31 changes: 31 additions & 0 deletions webapp/insights.php
@@ -0,0 +1,31 @@
<?php
/**
*
* ThinkUp/webapp/insights.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
*/
require_once 'init.php';

$controller = new InsightStreamController();
echo $controller->go();
7 changes: 0 additions & 7 deletions webapp/plugins/facebook/model/class.FacebookPlugin.php
Expand Up @@ -219,13 +219,6 @@ public function getDashboardMenuItems($instance) {
$messagestab->addDataset($messagestabds);
$menus["posts-toyou"] = $messagestab;

$insights_menu_item = new MenuItem("Insights", "Insights", 'insights.tpl');
$insights_menu_ds_1 = new Dataset("insights", 'InsightDAO', "getInsights",
array($instance->id, 50, "#page_number#") );
$insights_menu_ds_1->addHelp('userguide/listings/facebook/dashboard_insights');
$insights_menu_item->addDataset($insights_menu_ds_1);
$menus['insights'] = $insights_menu_item;

return $menus;
}

Expand Down
2 changes: 1 addition & 1 deletion webapp/plugins/facebook/tests/TestOfFacebookPlugin.php
Expand Up @@ -64,7 +64,7 @@ public function testMenuItemRegistration() {
$menus = $webapp_plugin_registrar->getDashboardMenu($instance);
$posts_menu = $menus["posts-all"];

$this->assertEqual(sizeof($menus), 8);
$this->assertEqual(sizeof($menus), 7);
$post_tab = $menus['posts-all'];
$this->assertEqual($post_tab->name, "All posts");
$this->assertEqual($post_tab->description, "All your status updates");
Expand Down
7 changes: 0 additions & 7 deletions webapp/plugins/googleplus/model/class.GooglePlusPlugin.php
Expand Up @@ -170,13 +170,6 @@ public function getDashboardMenuItems($instance) {
$qtab->addDataset($qtabds);
$menus["posts-questions"] = $qtab;

$insights_menu_item = new MenuItem("Insights", "Insights", 'insights.tpl');
$insights_menu_ds_1 = new Dataset("insights", 'InsightDAO', "getInsights",
array($instance->id, 50, "#page_number#") );
$insights_menu_ds_1->addHelp('userguide/listings/googleplus/dashboard_insights');
$insights_menu_item->addDataset($insights_menu_ds_1);
$menus['insights'] = $insights_menu_item;

return $menus;
}
}
2 changes: 1 addition & 1 deletion webapp/plugins/googleplus/tests/TestOfGooglePlusPlugin.php
Expand Up @@ -66,7 +66,7 @@ public function testMenuItemRegistration() {
$menus = $webapp_plugin_registrar->getDashboardMenu($instance);
$posts_menu = $menus["posts-all"];

$this->assertEqual(sizeof($menus), 6);
$this->assertEqual(sizeof($menus), 5);

$post_tab = $menus['posts-all'];
$this->assertEqual($post_tab->name, "All posts");
Expand Down

0 comments on commit e56d119

Please sign in to comment.