BeforeRate; Getting RateIt "BeforeRated" to work with PhoneGap

Jul 21, 2014 at 11:53 AM
Hi,

I love this Plugin and have been using it with great success for my research project to ask our App users how their hayfever symptoms are (out of 5) and posting the data along with the users GPS co-ordinates to our server for analysis.

Now we would like to ask our users if they took any hayfever medication on the day and record their response and the rating as well as the GPS data, so that we can more accurately analyse the data sets. I was planing on using the RateIt "BeforeRated" event and the phonegap command "navigator.notification.confirm".

Unfortunately, I just can't seem to get these two commands to work together. I am sure I missing something but can't work out what!

Pollen season is fast approaching so I would really REALLY appreciate any help with this!

Thanks in advance!
Ed.
<script src="js/jquery.rateit.js" type="text/javascript"></script>         
<script type ="text/javascript">

function onConfirm(buttonIndex) {
    window.drugs = buttonIndex;
}

//we bind only to the rateit controls within the products div
     $('#products .rateit').on('beforerated', function (e) {

if (value > 0) {
navigator.notification.confirm('Did you take any hayfever medication today?', onConfirm, 'Medication', ['Yes','No']) 
}
     
         var ri = $(this);
         var value = ri.rateit('value');
         var productID = ri.data('productid');
         var latitude = window.latitude;
         var longitude = window.longitude;
         var accuracy = window.accuracy;
         var pos_time = window.pos_time;
         var device_platform = window.device.platform;
     var device_uuid = window.device.uuid;        
     var drugs = window.drugs;         
         ri.rateit('readonly', true);

$.ajax({
 url: 'http://www.website.com/app_sync/survey.php', //your server side script
 data: { id: productID, value: value, latitude: latitude, longitude: longitude, accuracy: accuracy, pos_time: pos_time, device_platform: device_platform, device_uuid: device_uuid, drugs: drugs}, 
  type: 'POST',
  success: function (data) {
      navigator.notification.alert(data, null, 'Thanks!', 'OK')
  },
  error: function (jxhr, msg, err) {
      navigator.notification.alert(msg, null, 'Something\'s gone wrong!', 'OK')
  }
});
});
        </script>
Coordinator
Jul 21, 2014 at 12:44 PM
If I understand correctly:

Once a user presses a rating, you ask him/her a question, once the question has been answered you want to send both the rating and the answer to your server.

In this case you code should look something like this:

$('#products .rateit').on('beforerated', function (e, rating) {

     var ri = $(this);
     var productID = ri.data('productid');
     var pos_time = window.pos_time;
     //....

     //this function is called by Phonegap once the answer to the question has been received.
     var onConfirmCallback = function(buttonIndex) {
         var value = rating;
         var tookDrugs = buttonIndex == 1;

         $.ajax({
            //....
         });         

      }

     if (value > 0) {
        navigator.notification.confirm('Did you take any hayfever medication today?', onConfirmCallback , 'Medication', ['Yes','No']) ; //non-blocking
        ri.rateit('readonly', true);
     }     
}


Jul 21, 2014 at 1:35 PM
Edited Jul 21, 2014 at 1:47 PM
Dear Gidon,

Thank you so much for taking the time to help me. You are absolutely correct. Once a user presses a rating, we want to ask the user a question, once the question has been answered we want to send both the rating and the answer to our server.

I have tried the suggestion you have made but unfortunately it does not seem to work as expected. The rating is not recorded and the "navigator.notification.confirm" function is not called. Have I missed something?
         <script type ="text/javascript">
$('#products .rateit').on('beforerated', function (e, rating) {

     var ri = $(this);
     var productID = ri.data('productid');
     var latitude = window.latitude;
     var longitude = window.longitude;
     var accuracy = window.accuracy;
     var pos_time = window.pos_time;
     var device_platform = window.device.platform;
     var device_uuid = window.device.uuid;        

     //this function is called by Phonegap once the answer to the question has been received.
     var onConfirmCallback = function(buttonIndex) {
     var value = rating;
     var tookDrugs = buttonIndex == 1;

 $.ajax({
   url: 'http://www.website.com/app_sync/survey.php', //your server side script
   data: { id: productID, value: value, latitude: latitude, longitude: longitude, accuracy: accuracy, pos_time: pos_time, device_platform: device_platform, device_uuid: device_uuid}, //our data
   type: 'POST',
   success: function (data) {
     navigator.notification.alert(data, null, 'Thanks!', 'OK')
                            },
   error: function (jxhr, msg, err) {
     navigator.notification.alert(msg, null, 'Something\'s gone wrong!', 'OK')
                                    }
                    });
      }

     if (value > 0) {
        navigator.notification.confirm('Did you take any hayfever medication today?', onConfirmCallback , 'Medication', ['Yes','No']) ; //non-blocking
        ri.rateit('readonly', true);
     }     
});
        </script>
Coordinator
Jul 23, 2014 at 5:07 PM
If the confirm function is not called then the ratingg for sure won't be recorded; since the rating is recorded bY the callback of the confirm function.
So you have to find out why the confirm function is not called.
Try to put a basic alert before and see if that is being called.
Is the rateit being put in readonly mode? If not then it seems like the whole script is not being called.
Aug 9, 2014 at 11:40 AM
Dear Gidon,

I am still struggling with this! I really really need some help.

I have tried to add some console logs and the basic alert exactly as you have suggested.

The script seems to stop at
"var onConfirmCallback = function(buttonIndex) {"


Please Gidon! Please Help!

Ed.
Coordinator
Aug 12, 2014 at 3:40 PM
Does it display the confirmation dialog, or even that doesn't work?
Aug 12, 2014 at 10:45 PM
No nothing happens at all. With the original version posted first the dialog was displayed but only after the vote was sent. With this version the icon lights up but nothing else happens.
Coordinator
Aug 14, 2014 at 10:25 AM
It's kind of impossible to debug it from here. I suggest to ask a friend to look at your code.