diff --git a/.gitignore b/.gitignore index 3cef16d..30eeabc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ composer.lock -config.php \ No newline at end of file +config.php +.idea diff --git a/README.md b/README.md index f5c5527..910b549 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ to allow more people to contribute and extend it. So here it is, feel free to add any improvement or bells and whistles you would like to it. -Thanks to @johncongdon for "making it work" +Thanks to @johncongdon for "making it work" and @piontkowski for putting it on the web. ## Installation @@ -21,6 +21,8 @@ Rename ```config-sample.php``` to ```config.php``` Add your Meetup API key to ```config.php``` +Add members who you _don't_ want to be part of the drawing to the excluded_member_ids array in ```config.php``` + ### Command Line Modify the script ```pickme-cli.php``` and add your Event Id. diff --git a/config-sample.php b/config-sample.php index d3ea654..7132d10 100644 --- a/config-sample.php +++ b/config-sample.php @@ -3,4 +3,5 @@ $config = array( 'meetup_api' => '', + 'excluded_member_ids' => [], ); \ No newline at end of file diff --git a/pickme-cli.php b/pickme-cli.php index e6e3cfd..684ba66 100755 --- a/pickme-cli.php +++ b/pickme-cli.php @@ -22,7 +22,7 @@ passthru('clear'); $rsvps = array(); -foreach ($meetup_rsvps as $rsvp){ +foreach ($meetup_rsvps as $rsvp) { $rsvps[] = $rsvp; } @@ -44,4 +44,4 @@ print "\r" . str_repeat(' ', $screen_width) . "\r"; print "The winner is" . str_repeat(' ', 80) . "\n"; -print $rsvps[0]['member']['name']."\n"; +print $rsvps[0]['member']['name'] . "\n"; diff --git a/public_html/ajax.php b/public_html/ajax.php index c4349c9..42c551c 100644 --- a/public_html/ajax.php +++ b/public_html/ajax.php @@ -3,10 +3,14 @@ use \DMS\Service\Meetup\MeetupKeyAuthClient; $client = MeetupKeyAuthClient::factory(array('key' => $config['meetup_api'])); -$meetup_rsvps = $client->getRSVPs(array('event_id' => $_POST['event_id'] )); +$meetup_rsvps = $client->getRSVPs(array('event_id' => $_POST['event_id'])); $rsvps = array(); -foreach ($meetup_rsvps as $rsvp){ +foreach ($meetup_rsvps as $rsvp) { + if (in_array($rsvp['member']['member_id'], $config['excluded_member_ids'])) { + continue; + } + $rsvps[] = $rsvp; } diff --git a/public_html/index.php b/public_html/index.php index 4fdd210..57b2e53 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,65 +1,68 @@ - - - - Pickme: Meetup random event winner picker - - - - - - - - - - - - - + + + + Pickme: Meetup random event winner picker + + + + + + + + + + + + + - +--> -
-
-
-
+
+
+
+
-
+
- +--> -
+
- Enter details below. -

- -

- + Enter details below. +

+ +

+ -
+
-
+
-
-
-
-
+
+
+
+
-
+
- - - + + + diff --git a/public_html/main.js b/public_html/main.js index 4221a56..0f314d3 100644 --- a/public_html/main.js +++ b/public_html/main.js @@ -3,15 +3,18 @@ * */ +/** + * @callback parseAttendeesCallback + * @param {Object[]} attendees + */ /** * Query the Meetup API and get a list of attendees, * given an API key. * - * @param int eventid The Meetup Event ID to be used for gathering a list. - * @param string meetupapi The Meetup API for the account to be used. - * @param function callback A function to execute after successful ajax request. + * @param {int} eventid The Meetup Event ID to be used for gathering a list. + * @param {parseAttendeesCallback} callback A function to execute after successful ajax request. */ -function get_attendees( eventid, callback ){ +function get_attendees(eventid, callback) { $.ajax({ type: 'POST', @@ -20,12 +23,12 @@ function get_attendees( eventid, callback ){ event_id: eventid }, success: callback, - dataType:'json' + dataType: 'json' }) - .fail( function( xhr, status, error ){ - $("#winner-container").html("There was an error with the ajax request. Check your internet connection and the console for more information."); - console.log("Status: " + status + " Error Thrown: " + error); - }); + .fail(function (xhr, status, error) { + $("#winner-container").html("There was an error with the ajax request. Check your internet connection and the console for more information."); + console.log("Status: " + status + " Error Thrown: " + error); + }); } @@ -34,175 +37,168 @@ function get_attendees( eventid, callback ){ * * Also, do some basic error checking to see if we got a response or error. * - * @param mixed data The response from the Meetup API. + * @param {Object[]} attendees The response from the Meetup API. + * @param {Object} attendees[].member The individual object that contains details about the member. + * @param {string} attendees[].member_photo.photo_link A URL to the member's photo. * @return null */ -var attendee = []; -function parse_attendees(attendees){ - +function parse_attendees(attendees) { + // Check to see if we have results or an error. - - if(attendees){ - - if( !isEmpty( attendees ) ) { - - for( i = 0; i < attendees.length; i++ ){ - - if( attendees[i].response == "yes" ){ + + if (attendees) { + var rsvps = []; + if (!$.isEmptyObject(attendees)) { + + for (var i = 0; i < attendees.length; i++) { + + if (attendees[i].response === "yes") { var obj = {}; obj.name = attendees[i].member.name; attendees[i].member_photo ? obj.photo = attendees[i].member_photo.photo_link : obj.photo = ""; - attendee.push(obj); + rsvps.push(obj); } - + } - - select_winner( attendee ); - + + select_winner(rsvps); + } - - else{ - + + else { + $("#winner-container").html("No RSVP's were found. Most likely, you didn't put in the right event ID. Reload the page and try again."); - - return; - + } - + } - + else { - + $("#winner-container").html( - data.status + "

"+ - data.problem + "

"+ "Most likely, you didn't put in the right API key. Reload the page and try again." ); - - return; - } } /** - * Checks to see if a given object is empty. + * Pick a winner and do something special for them. * - * @param object obj The object in question. - * @return bool Returns true if the object is empty, false if not. + * @param {Object[]} rsvps The RSVP's in the proper format. + * @return null */ -function isEmpty( obj ){ +function select_winner(rsvps) { - for(var i in obj){ - return false; - } - - return true; + $('#winner-banner').text('Choosing winner in...'); -} -/** - * Pick a winner and do something special for them. - * - * @param mixed rsvps The RSVP's in the proper format. - * @return null - */ -function select_winner( rsvps ){ - - $('#winner-banner').text( 'Choosing winner in...' ); - - - var start_time = new Date().getTime(), - seconds_to_run = 5000, // in milliseconds - seconds = 0, - interval = setInterval( function(){ - - seconds = new Date().getTime() - start_time; - - $('#countdown-timer').text( Math.round( Math.abs( ( seconds - seconds_to_run) / 1000 ) ) ); - - if( seconds > seconds_to_run ){ - - // If the code inside here runs, then display a picture of the chosen winner! - clearInterval(interval); - $('#winner-photo').html('

 '); + var start_time, seconds_to_run, seconds, interval; + start_time = new Date().getTime(); + seconds_to_run = 3000; + seconds = 0; + interval = setInterval(function () { + + seconds = new Date().getTime() - start_time; + + //noinspection JSJQueryEfficiency + $('#countdown-timer').text(Math.ceil(Math.abs((seconds - seconds_to_run) / 1000))); + + if (seconds > seconds_to_run) { + + // If the code inside here runs, then display a picture of the chosen winner! + clearInterval(interval); + $('#winner-photo').html('

  '); + $('#countdown-timer').empty(); + $('#winner-banner').text('WINNER!!!').css({'font-size': '3em', 'color': 'blue', 'font-weight': 'bold'}); + + $('#again').click(function () { + + $('#winner-banner').empty().css({'font-size': '', 'color': '', 'font-weight': ''}); $('#countdown-timer').empty(); - $('#winner-banner').text('WINNER!!!').css( {'font-size':'3em','color':'blue','font-weight':'bold'} ); - - $('#again').click(function(e){ - - $('#winner-banner').empty().css( {'font-size':'','color':'','font-weight':''} ); - $('#countdown-timer').empty(); - $('#winner-name').empty(); - $('#winner-photo').empty(); - - select_winner(attendee); - }); - - // Shoot off some fireworks. - var r = 4 + parseInt(Math.random()*16); - for(var i = r; i--;){ - setTimeout( - 'createFirework(8,14,2,null,null,null,null,null,Math.random()>0.5,true)', - (i+1)*(1+parseInt(Math.random()*1000))); - } - - return; // End the program. - + $('#winner-name').empty(); + $('#winner-photo').empty(); + + rsvps.shift(); + select_winner(rsvps); + }); + + $('#reset').click(function () { + + $('#winner-banner').empty().css({'font-size': '', 'color': '', 'font-weight': ''}); + $('#countdown-timer').empty(); + $('#winner-name').empty(); + $('#winner-photo').empty(); + + run(); + }); + + // Shoot off some fireworks. + var r = 4 + parseInt(Math.random() * 16); + for (var i = r; i--;) { + setTimeout( + 'createFirework(8,14,2,null,null,null,null,null,Math.random()>0.5,true)', + (i + 1) * (1 + parseInt(Math.random() * 1000))); } - - shuffle( rsvps ); - - $('#winner-name').text( rsvps[0].name ); - }, 50 ); + return; // End the program. + + } + + shuffle(rsvps); + + $('#winner-name').text(rsvps[0].name); + + }, 50); } /** * Randomize a given array * - * @param array array The array to be randomized. - * @return array The randomized array. + * @param {Array} array The array to be randomized. + * @return {Array} The randomized array. */ -function shuffle( array ) { +function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While elements remain... - while ( 0 !== currentIndex ) { + while (0 !== currentIndex) { // Pick one of the elements... - randomIndex = Math.floor( Math.random() * currentIndex ); + randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; - + } return array; } +/** + * Run the program to pick a winner for the Meetup. + */ +function run() +{ + var event = $('#meetingid').val(); + + if (event) { + $('#form-container').slideUp(); + get_attendees(event, parse_attendees); + } +} + /** * Finally, bind an event handler to the submit button click event. */ -$(document).ready(function(){ - - $('#submit').click(function(e){ - var event = $( '#meetingid' ).val(); - - if( event ){ - $('#form-container').slideUp(); - get_attendees( event, parse_attendees ); - } - - else{ - return; - } +$(document).ready(function () { + $('#submit').click(function () { + run(); }); - });