Skip to content

Commit

Permalink
Karma integrated with planet openSUSE.
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jun 16, 2012
1 parent 17adfc1 commit 63f8dc5
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 64 deletions.
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

The Karma plugin fetches user activity from Bugzilla and Twitter(Uptil Now), calculates karma score and assigns badges. Users can add the karma widget to their dashboard to view their karma score, it is pre-calculated for every user.

The karma widget displays the user badge, points, number of tweets(to promote OpenSUSE) by the user and number of bugfixes on Bugzilla. Karma fetches last 50 tweets of a user and every tweet that contains OpenSUSE hashtag is rewarded with 2 karma points, and every bug fix is rewarded according to the severity of the bug, a Blocker gets 10 karma points, a Critical gets 8, Major 7 and so on so forth.
The karma widget displays the user badge, points, number of tweets(to promote OpenSUSE) by the user and number of bugfixes on Bugzilla. Karma fetches last tweets of a user and every tweet that is meant to promote openSUSE is rewarded with 2 karma points, and every bug fix is rewarded according to the severity of the bug, a Blocker gets 10 karma points, a Critical gets 8, Major 7 and so on so forth.

The basic backbone of the plugin is a cron script that runs daily to calculate karma score for every user on Connect.

*Admin Settings*

The plugin should be first enabled. The running the cron tab would do the needful, karma details of all users would be fetched and stored in the database.
The plugin should be first enabled. Then running the cron tab would do the needful, karma details of all users would be fetched and stored in the database.

*User settings*
Add the karma widget to your dashboard or profile to view your score and badge.
Binary file added default_icons/Blog-O-Manic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added default_icons/Bug Buster.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed default_icons/Bug Squasher.gif
Binary file not shown.
Binary file removed default_icons/Bugzilla Viking.gif
Binary file not shown.
Binary file removed default_icons/Enthusiast.gif
Binary file not shown.
Binary file added default_icons/Enthusiast.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed default_icons/Master Ninja.gif
Binary file not shown.
Binary file added default_icons/Notable Endeavor.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed default_icons/Novice.gif
Binary file not shown.
Binary file added default_icons/Novice.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added default_icons/SUSE Herald.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added default_icons/SUSE Samurai.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed default_icons/SUSE Spartan.gif
Binary file not shown.
Binary file added default_icons/Twitteratti.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 116 additions & 55 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function karma_cron($hook, $entity_type, $returnvalue, $params) {

//for each user assign karma score(this is done daily).
foreach ($entities as $entity) {
//guid of each user
$guid = $entity->guid;
//email of each user
$email = $entity->email;
//twitter screen name of each user
Expand All @@ -31,42 +33,49 @@ function karma_cron($hook, $entity_type, $returnvalue, $params) {
$num_of_bugs_fixed = $bugzilla[1];

//twitter score
$twitter = twitter_score($twitter_screen_name);
$twitter = twitter_score($twitter_screen_name,$guid);
$twitter_score = $twitter[0];
$num_of_tweets = $twitter[1];

//total score
$total_score = array();
$total_score[0] = $twitter_score;
$total_score[1] = $bugzilla_score;
//planet opensuse score
$planet_opensuse = planet_opensuse_score($entity->blog,$guid);
$planet_opensuse_score = $planet_opensuse[0];
$num_of_posts = $planet_opensuse[1];


//create an instance of ElggObject class to store karma score for each user.
$karma = new ElggObject();
$guid = $entity->guid;

//check if karma object exists for user, if it does then update it.
$entities = get_entities('object','karma',$guid);
if(isset($entities[0])) {
$karma = $entities[0];
$old_activity = $karma->activity;
$old_marketing_score = $karma->marketing_score;
$karma->developer_score = $bugzilla_score;
$karma->marketing_score = array($old_marketing_score[0] + $twitter_score, $old_marketing_score[1] + $planet_opensuse_score);
$karma->activity = array($num_of_tweets + $old_activity[0],$num_of_bugs_fixed,$num_of_posts + $old_activity[2]);
}
//when karma details do not exist
//when karma details do not exist before
else {
$karma->title = $entity->name;
$karma->description = "Karma Score";
$karma->subtype="karma";
$karma->marketing_score = array($twitter_score,$planet_opensuse_score);
$karma->developer_score = $bugzilla_score;
$karma->activity = array($num_of_tweets,$num_of_bugs_fixed,$num_of_posts);
$karma->access_id = ACCESS_PUBLIC;
$karma->owner_guid = $guid;
}
$karma->score = $total_score;
$karma->activity = array($num_of_tweets,$num_of_bugs_fixed);
$karma->last_updated = time();
$karma->save();
}//end of foreach user

//after calculating score for all users, assign badges to each.
$karma_entities = get_entities('object','karma');
foreach ($karma_entities as $karma_entity) {
//send each user's score to assign badge.
$badge = assign_badge($karma_entity->score);
$badge = assign_badge($karma_entity->developer_score,$karma_entity->marketing_score);
$karma_entity->badge = $badge;
$karma_entity->save();
}
Expand Down Expand Up @@ -126,80 +135,132 @@ function bugzilla_score($email) {
}

//calculates score based on user-tweets on twitter.
function twitter_score($twitter_screen_name) {
function twitter_score($twitter_screen_name,$guid) {
$score = 0;
$num_of_tweets = 0;
//call to twitter, returns user's last 50 tweets.
$json = file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=$twitter_screen_name&count=50");
$json = file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=$twitter_screen_name&count=5");
$file = json_decode($json,'true');
foreach($file as $key=>$value) {
foreach ( $value['entities'] as $entity=>$entity_array )
{
foreach ($file as $key=>$value) {
foreach ($value['entities'] as $entity=>$entity_array) {
//look for opensuse hashtag
if (strcmp($entity,"hashtags") == 0 )
{
foreach ($entity_array as $index=>$hashtag)
{
if(stripos($hashtag['text'],"openSUSE") !== false)
{
$score += 2;
$num_of_tweets ++;
if (strcmp($entity,"hashtags") == 0 ) {
foreach ($entity_array as $index=>$hashtag) {
if (stripos($hashtag['text'],"openSUSE") !== false) {
//checks the time of publishing, should be after the last update.
$check = check_date($value['created_at'],$guid);
if ($check == true) {
$score += 2;
$num_of_tweets ++;
}
}
}
}
}
}
$twitter_score = array();
$twitter_score[0] = $score;
$twitter_score[1] = $num_of_tweets;
$twitter_score = array($score,$num_of_tweets);

return $twitter_score;
}

//finding maximum bugzilla score.
//planet opensuse score
function planet_opensuse_score($blog_url,$guid) {
$score = 0;
$num_of_posts = 0;
$rss = new SimpleXMLElement('http://planet.opensuse.org/en/rss20.xml', null, true);
$item = $rss->xpath('channel/item');
foreach($rss->xpath('channel/item') as $item) {
if(stripos($item->link,$blog_url) !== False) {
//checks the time of publishing, should be after the last update.
$check = check_date($item->pubDate,$guid);
if ($check == true) {
$score += 5;
$num_of_posts ++;
}
}
}
$planet_opensuse = array($score,$num_of_posts);

return $planet_opensuse;
}

//finding maximum bugzilla and marketing score.
function find_max_score() {
//form an array of bugzilla scores and call max function.
$score = array();
$bugzilla_scores = array();
$marketing_scores = array();
$entities = get_entities('object','karma');
if(is_null($entities[0])) {
return 0;
}
else {
foreach ($entities as $entity) {
$score_array = $entity->score;
$score[] = $score_array[1]; //bugzilla score.
$developer_scores[] = $entity->developer_score;//bugzilla score.
$marketing_array = $entity->marketing_score;//marketing score, array of twitter and planet openSUSE scores.
$marketing_scores[] = $marketing_array[0] + $marketing_array[1];
}
$max_score = max($score);
$max_developer = max($developer_scores);
$max_marketing = max($marketing_scores);

$max_score = array($max_developer,$max_marketing);

return $max_score;
}
}

function assign_badge($total_score) {
$max_score = find_max_score();//max_score is maximum bugzilla score of all users.
/* For now the title SUSE Spartan is assigned for securing maximum bug fixing score
* and making the most tweets, Bug squasher is assigned for securing maximum bug fixing score
* Bugzilla Viking is assigned for securing greater than 85% of maximum
* score, Master Ninja for securing greater than 70% but less than 85% and Enthusiast for
* tweeting but not making any bug fixes and Novice for the rest.*/
//checks if time of publishing is greater than last update.
function check_date($date,$guid) {
$timestamp = strtotime($date);
$entities = get_entities('object','karma',$guid);
if(isset($entities[0])) {
$karma = $entities[0];
$last_updated = $karma->last_updated;
if ($timestamp > $last_updated)
return true;
else
return false;
}
else {
return true;
}
}

//assigns badge given marketing and developer score
function assign_badge($developer,$marketing) {

$bugzilla_score = $total_score[1];
$twitter_score = $total_score[0];
$bugzilla_score = $developer;
$twitter_score = $marketing[0];
$planet_opensuse_score = $marketing[1];

if ($bugzilla_score == 0 && $twitter_score == 0)
$badge = "Novice";
else if ($twitter_score > 0 && $bugzilla_score == 0)
$badge = "Enthusiast";
else if ($bugzilla_score >= $max_score && $twitter_score >= 30)
$badge = "SUSE Spartan";
else if ($bugzilla_score >= $max_score)
$badge = "Bug Squasher";
else if ($bugzilla_score >= 0.85*$max_score && $bugzilla_score < $max_score)
$badge = "Bugzilla Viking";
else if ($bugzilla_score >=0.5*$max_score && $bugzilla_score < 0.7*$max_score)
$badge = "Master Ninja";
else if ($bugzilla_score < 0.5*$max_score && $bugzilla_score > 0 )
$badge = "Novice";
$max_score = find_max_score();
$max_developer = $max_score[0];
$max_marketing = $max_score[1];

$marketing_score = $twitter_score + $planet_opensuse_score;
if($bugzilla_score == 0 && $planet_opensuse_score == 0 && $twitter_score == 0 )
$badge = "Novice";

else if ($bugzilla_score >= $marketing_score) {
if ($bugzilla_score >= 0.75*$max_developer && $bugzilla_score <= $max_developer)
$badge = "SUSE Samurai";
else if ($bugzilla_score >=0.5*$max_developer && $bugzilla_score < 0.7*$max_developer)
$badge = "Bug Buster";
else if ($bugzilla_score < 0.5*$max_developer && $bugzilla_score > 0 )
$badge = "Notable Endeavor";
}

else if ($marketing_score > $bugzilla_score) {
if ($marketing_score >= 0.75*$max_marketing && $marketing_score <= $max_marketing)
$badge = "SUSE Herald";
else if ($marketing_score >= 0.5*$max_marketing && $marketing_score < 0.75*$max_marketing)
$badge = "Enthusiast";
else {
if ($planet_opensuse_score > $twitter_score)
$badge = "Blog-o-Manic";
else
$badge = "Twitteratti";
}

}
return $badge;
}

Expand Down
17 changes: 10 additions & 7 deletions views/default/widgets/karma/view.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<style type="text/css">
img.emblem {
position: relative;
left: 30px;
left: 20px;
height: 181px;
width: 150px;
width: 200px;
}
div.score {
color: #3873B6;
Expand All @@ -21,21 +21,22 @@
/*isset($entities[0]) should always return true, since the plugin is enabled and the cron
* is being triggered hourly for every user, even if he uses the widget or not */

if(isset($entities[0]))
if (isset($entities[0]))
{
//karma details exists, print them.
$entity = $entities[0];
$badge = $entity->badge;
$activity = $entity->activity;
$score = $entity->score;
$total_score = $score[1] + $score[0];
$developer_score = $entity->developer_score;
$marketing_score = $entity->marketing_score;
$total_score = $marketing_score[0] + $marketing_score[1] + $developer_score;
$title = $badge." (".$total_score." Points)";
}
$img_class = 'class = emblem';
?>
<!-- displapy the user's badge -->
<div class = "emblem">
&nbsp;&nbsp;&nbsp;<img <?php echo $img_class; ?> src="<?php echo elgg_format_url($vars['url']."mod/karma/default_icons/".$badge.".gif");?>" border="0"/>
&nbsp;&nbsp;&nbsp;<img <?php echo $img_class; ?> src="<?php echo elgg_format_url($vars['url']."mod/karma/default_icons/".$badge.".jpg");?>" border="0"/>
</div>
<div class = "score">
<?php
Expand All @@ -44,7 +45,9 @@
echo "Tweets : ".$activity[0];
echo '<br>';
echo "Bug Fixes : ".$activity[1];

echo '<br>';
echo "Planet OpenSUSE posts : ".$activity[2];
?>
</div>


0 comments on commit 63f8dc5

Please sign in to comment.