Skip to content

Commit

Permalink
new relative sharing tool (%age) inspired by Pingdom.com + repair twi…
Browse files Browse the repository at this point in the history
…tter share button #61
  • Loading branch information
MathieuNls committed Jan 7, 2016
1 parent b110b43 commit ccfd52c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 deletions.
6 changes: 3 additions & 3 deletions application/controllers/Measures.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ function accuracyMeasure() {
$watchMeasure = $this->measure->addAccuracyMesure(
$this->input->post('measureId'), $referenceTime, $userTime);

//We store the computed accuracy
$result['accuracy'] = $watchMeasure->accuracy;

// If the computed accuracy makes sense, we return success
if (is_numeric($watchMeasure->accuracy)) {
$result['success'] = true;
//We store the computed accuracy & percentile
$result['accuracy'] = $watchMeasure->accuracy;
$result['percentile'] = $watchMeasure->percentile;
} else {
$result['success'] = false;
}
Expand Down
28 changes: 28 additions & 0 deletions application/models/Measure.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ public function computeAccuracy($watchMeasure) {
return $watchMeasure;
}

/**
* Computes the percentile for an accuracy, i.e, the percentage
* of watches that are less accurate than $accuracy.
*
* The percentile excludes bugged measure (+/- 300 spd)
*
* @param float $accuracy A complete measure with computed
* @return int the percentile for this accuracy.
*/
function computePercentileAccuracy($accuracy){

//This have to be kept in line with the computeAccuracy method
$precisionFormulae = 'ABS(((measure.accuracyUserTime
- measure.measureUserTime)*86400)/
(measure.accuracyReferenceTime - measure.measureReferenceTime)
-86400)';

$moreAccurateCount = $this->count_by($precisionFormulae . ' <',
abs($accuracy));

$valideMeasuresCount = $this->count_by($precisionFormulae . ' <', 300);

return round(100 - (($moreAccurateCount / $valideMeasuresCount) * 100));
}

/**
* Add a base measure (1/2) to $watchId given $referenceTime and $userTime
*
Expand Down Expand Up @@ -155,6 +180,9 @@ function addAccuracyMesure($measureId, $referenceTime, $userTime) {
$this->notify(NEW_ACCURACY,
array('measure' => $watchMeasure));

$watchMeasure->percentile =
$this->computePercentileAccuracy($watchMeasure->accuracy);

return $watchMeasure;
}

Expand Down
7 changes: 7 additions & 0 deletions application/tests/models/Measure_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public function test_getMeasuresCountByWatchBrand() {
$this->assertEquals(6, $count);
}

public function test_computePercentileAccuracy(){

$this->assertEquals(67, $this->obj->computePercentileAccuracy(1.5));
$this->assertEquals(0, $this->obj->computePercentileAccuracy(7));

}

}

?>
29 changes: 15 additions & 14 deletions application/views/measure/get-accuracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col-md-6 col-md-offset-3">
<form class="form-horizontal" method="post" name="newAccuracy">


<div id="mainExplanation" class="form-group">
<center>
At the end of the countdown, please enter below the exact time as it is on your watch. <br>Let's check the accuracy of your watch!
Expand Down Expand Up @@ -43,43 +43,44 @@
</div>
</div>

<div class="form-group sync-success" style="display:none; text-align: center">
<div class="form-group sync-success" style="display:none; text-align: center">

<div class="col-sm-12">

<canvas class="col-sm-4" id="canvas_animated_watch" width="250" height="250"></canvas>
<div class="col-sm-8">

<div class="col-sm-8">
<h1>Congratulations!</h1> <br/> <p class="accuracy-subtitle"> The accuracy of your <strong><span class="watch-brand"><?php echo $selectedWatch->brand;?></span></strong> is </p>
</div>
<div class="col-sm-8">
<strong><span class="watch-accuracy"></span> seconds a day!</strong>
<strong><span class="watch-accuracy"></span> seconds a day!</strong>
</div>
</div>

<div class="share-plugin col-md-offset-1 col-sm-10">
<center><p><br /><br />We are happy to have you around, help us spread the love for mechanical watches and share your accuracy on:</p></center>
<center><p><br /><br />You're watch is <b>more accurate than <span class="watch-percentile"></span>%</b> of all tested watches.</p></center>

<div class="col-sm-12">
<div class="share-button" id="twitter" data-url="<?php echo base_url() . 'result' ;?>" data-text="My #<?php echo $selectedWatch->brand;?> runs at {WatchAccuracy} spd on @toolwatchapp"></div>
<div class="share-button" id="facebook" data-url="<?php echo base_url() . 'result' ;?>" data-text="My <?php echo $selectedWatch->brand;?> runs at {WatchAccuracy} spd on https://www.facebook.com/Toolwatch"></div>
<div class="share-button" id="googleplus" data-url="<?php echo base_url() . 'result' ;?>" data-text="My #<?php echo $selectedWatch->brand;?> runs at {WatchAccuracy} spd on https://plus.google.com/104724190750629608501/"></div>
<div class="share-button" id="twitter" data-url="<?php echo base_url() . 'result' ;?>" data-text="My watch is more accurate than {WatchPercentile}% of all tested watches on @toolwatchapp"></div>
<div class="share-button" id="facebook" data-url="<?php echo base_url() . 'result' ;?>" data-text="My watch is more accurate than {WatchPercentile}% of all tested watches on https://www.facebook.com/Toolwatch"></div>
<div class="share-button" id="googleplus" data-url="<?php echo base_url() . 'result' ;?>" data-text="My watch is more accurate than {WatchPercentile}% of all tested watches on https://plus.google.com/104724190750629608501/"></div>
</div>


<br/>

<center><p><br /><br />We <i style="color:#4d77a7" class="fa fa-heart"></i> <a href="https://instagram.com/toolwatchapp/">Instagram</a>, tag us with your wristshots and share your results using #ToolwatchApp !</p></center>

<!-- www.intagme.com -->
<iframe src="http://www.intagme.com/in/?u=dG9vbHdhdGNoYXBwfGlufDEwMHw0fDJ8fHllc3w1fHVuZGVmaW5lZHx5ZXM=" allowTransparency="true" frameborder="0" scrolling="no" style="margin-left:5px; border:none; overflow:hidden; width:auto; height: 230px" ></iframe>

</div>

</div>

</div>


</div>

<div class="form-group">
Expand All @@ -97,4 +98,4 @@
</div>
</div>
<img style="display:none" id="watch" src="<?php echo img_url('flatwatch-blank.png');?>">
</div>
</div>
3 changes: 2 additions & 1 deletion assets/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,11 @@ $(document).ready(function()
}

$('.watch-accuracy').html(result.accuracy);
$('.watch-percentile').html(result.percentile);


$('.share-button').each(function(index){
$(this).attr("data-text", $(this).attr("data-text").replace("{WatchAccuracy}", result.accuracy));
$(this).attr("data-text", $(this).attr("data-text").replace("{WatchPercentile}", result.percentile));
});

initShareButton();
Expand Down
56 changes: 28 additions & 28 deletions assets/js/jquery.sharrre.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
facebook: "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27{url}%27&callback=?",
//old method facebook: "http://graph.facebook.com/?id={url}&callback=?",
//facebook : "http://api.ak.facebook.com/restserver.php?v=1.0&method=links.getStats&urls={url}&format=json"
twitter: "http://cdn.api.twitter.com/1/urls/count.json?url={url}&callback=?",

twitter: "http://opensharecount.com/count.json?url={url}",
digg: "http://services.digg.com/2.0/story.getInfo?links={url}&type=javascript&callback=?",
delicious: 'http://feeds.delicious.com/v2/json/urlinfo/data?url={url}&callback=?',
//stumbleupon: "http://www.stumbleupon.com/services/1.01/badge.getinfo?url={url}&format=jsonp&callback=?",
Expand Down Expand Up @@ -208,7 +208,7 @@
'<div style="'+cssCount+'background-color:#fff;margin-bottom:5px;overflow:hidden;text-align:center;border:1px solid #ccc;border-radius:3px;">'+count+'</div>'+
'<div style="'+cssShare+'display:block;padding:0;text-align:center;text-decoration:none;width:50px;background-color:#7EACEE;border:1px solid #40679C;border-radius:3px;color:#fff;">'+
'<img src="http://www.delicious.com/static/img/delicious.small.gif" height="10" width="10" alt="Delicious" /> Add</div></div></div>');

$(self.element).find('.delicious').on('click', function(){
self.openPopup('delicious');
});
Expand All @@ -221,7 +221,7 @@
loading = 1;
(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//platform.stumbleupon.com/1/widgets.js';
li.src = '//platform.stumbleupon.com/1/widgets.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
s = window.setTimeout(function(){
Expand All @@ -243,7 +243,7 @@
loading = 1;
(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//platform.linkedin.com/in.js';
li.src = '//platform.linkedin.com/in.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
}
Expand All @@ -257,7 +257,7 @@

(function() {
var li = document.createElement('script');li.type = 'text/javascript';li.async = true;
li.src = '//assets.pinterest.com/js/pinit.js';
li.src = '//assets.pinterest.com/js/pinit.js';
var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(li, s);
})();
}
Expand Down Expand Up @@ -349,16 +349,16 @@
================================================== */
function Plugin( element, options ) {
this.element = element;

this.options = $.extend( true, {}, defaults, options);
this.options.share = options.share; //simple solution to allow order of buttons

this._defaults = defaults;
this._name = pluginName;

this.init();
};

/* Initialization method
================================================== */
Plugin.prototype.init = function () {
Expand All @@ -368,7 +368,7 @@
urlJson.stumbleupon = this.options.urlCurl + '?url={url}&type=stumbleupon'; // PHP script for Stumbleupon...
}
$(this.element).addClass(this.options.className); //add class

//HTML5 Custom data
if(typeof $(this.element).data('title') !== 'undefined'){
this.options.title = $(this.element).attr('data-title');
Expand All @@ -379,14 +379,14 @@
if(typeof $(this.element).data('text') !== 'undefined'){
this.options.text = $(this.element).data('text');
}

//how many social website have been selected
$.each(this.options.share, function(name, val) {
if(val === true){
self.options.shareTotal ++;
}
});

if(self.options.enableCounter === true){ //if for some reason you don't need counter
//get count of social share that have been selected
$.each(this.options.share, function(name, val) {
Expand All @@ -405,7 +405,7 @@
else{ // if you want to use official button like example 3 or 5
this.loadButtons();
}

//add hover event
$(this.element).hover(function(){
//load social button if enable and 1 time
Expand All @@ -416,14 +416,14 @@
}, function(){
self.options.hide(self, self.options);
});

//click event
$(this.element).click(function(){
self.options.click(self, self.options);
return false;
});
};

/* loadButtons methode
================================================== */
Plugin.prototype.loadButtons = function () {
Expand All @@ -438,7 +438,7 @@
}
});
};

/* getSocialJson methode
================================================== */
Plugin.prototype.getSocialJson = function (name) {
Expand Down Expand Up @@ -471,7 +471,7 @@
self.rendererPerso();
//console.log(json); //debug
})
.error(function() {
.error(function() {
self.options.count[name] = 0;
self.rendererPerso();
});
Expand All @@ -482,7 +482,7 @@
self.rendererPerso();
}
};

/* launch render methode
================================================== */
Plugin.prototype.rendererPerso = function () {
Expand All @@ -493,7 +493,7 @@
this.options.render(this, this.options);
}
};

/* render methode
================================================== */
Plugin.prototype.renderer = function () {
Expand All @@ -502,31 +502,31 @@
if(this.options.shorterTotal === true){ //format number like 1.2k or 5M
total = this.shorterTotal(total);
}

if(template !== ''){ //if there is a template
template = template.replace('{total}', total);
$(this.element).html(template);
}
else{ //template by defaults
$(this.element).html(
'<div class="box"><a class="count" href="#">' + total + '</a>' +
'<div class="box"><a class="count" href="#">' + total + '</a>' +
(this.options.title !== '' ? '<a class="share" href="#">' + this.options.title + '</a>' : '') +
'</div>'
);
}
};

/* format total numbers like 1.2k or 5M
================================================== */
Plugin.prototype.shorterTotal = function (num) {
if (num >= 1e6){
num = (num / 1e6).toFixed(2) + "M"
} else if (num >= 1e3){
} else if (num >= 1e3){
num = (num / 1e3).toFixed(1) + "k"
}
return num;
};

/* Methode for open popup
================================================== */
Plugin.prototype.openPopup = function (site) {
Expand All @@ -545,14 +545,14 @@
ga('send', 'event', '_trackSocial', tracking[site].site);
}
};

/* Methode for add +1 to a counter
================================================== */
Plugin.prototype.simulateClick = function () {
var html = $(this.element).html();
$(this.element).html(html.replace(this.options.total, this.options.total+1));
};

/* Methode for add +1 to a counter
================================================== */
Plugin.prototype.update = function (url, text) {
Expand Down Expand Up @@ -583,4 +583,4 @@
});
}
};
})(jQuery, window, document);
})(jQuery, window, document);

0 comments on commit ccfd52c

Please sign in to comment.