I checked the code again and I still think that something isn't right.
I try to explain better.
This two ajax calls are involved:
// Instant search statistics
jQuery.ajax('index.php?option=com_ats&view=attempts&format=json', {
dataType : 'json',
cache : false,
data : {
task : 'save',
ats_attempt_id : ats_instant_statistics,
title : searchQuery,
ats_category_id : jQuery('#ticket_catid').val(),
modified_on : ''
},
success : function(response){
ats_instant_statistics = response.ats_attempt_id;
jQuery('input[name="ats_attempt_id"]').val(ats_instant_statistics);
}
});
jQuery('#ats-instantreply-grid').on('click', 'a', function(){
var click_type = jQuery(this).data('source');
jQuery.ajax('index.php?option=com_ats&view=attempts&format=json', {
dataType : 'json',
cache : false,
data : {
task : 'save',
ats_attempt_id : ats_instant_statistics,
update_clicks : click_type
}
});
})
The first piece of code create or update a row on the table "#__ats_attempts" and return the id and store it in the global variable "ats_instant_statistics" and in the "ats_attempt_id" form input.
Only the first call create a new row, the next calls update the same row updating "title" and "ats_category_id".
The second piece of code register an event that increment the column "ticket_clicks" or "docimport_clicks" when the user press the "View" button on a related result.
----- Now check the case 1: -----
An user press the "New ticket" button.
The user write something into the topic for example "search1" in the category "1".
The first piece of code create the row and set the column "ats_category_id" with 1 and "title" with "search1" and return the id.
The second piece of code register the event but the inner function is not called. (***)
If the user press a "View" button (suppose on a related doc) the inner function of the second piece of code is called and update the column "docimport_clicks" changing is value from 0 to 1.
If the user press a "View" button (suppose on a ticket) the inner function of second piece of code is called and update the column "ticket_clicks" changing is value from 0 to 1.
If the user create the ticket the "onAfterSave" method on "AtsControllerNewtickets" update the row on "#__ats_attempts" setting the column "ats_ticket_id" with the id of the new created ticket.
Everything is fine.
--------------------------------------
----- Now check the case 2: -----
An user press the "New ticket" button.
The user write something into the topic for example "search1" in the category "1".
The first piece of code create the row and set the column "ats_category_id" with 1 and "title" with "search1" and return the id;
The second piece of code register the event but the inner function is not called. (***)
If the user press a "View" button (suppose on a related doc) the inner function of the second piece of code is called and update the column "docimport_clicks" changing is value from 0 to 1.
Now the user change the topic to "search2" in the category "1".
The first piece of code update the row and set the column "ats_category_id" with 1 and "title" with "search2" and return the same id;
The second piece of code register the event but the inner function is not called. (***)
If the user press a "View" button (suppose on a ticket) the inner function of the second piece of code is called (twice) and update the column "ticket_clicks" changing is value from 0 to 1 (first call) and from 1 to 2 (second call).
I think is not correct because the user clicked just one time.
If the user create the ticket the "onAfterSave" method on "AtsControllerNewtickets" update the row on "#__ats_attempts" setting the column "ats_ticket_id" with the id of the new created ticket.
If you check the statistic you see the wrong number of access to the tickets (2 instead 1).
--------------------------------------
With the change that I made to the javascript the step marked with (***) is execute just one time on "akeeba.jQuery(document).ready()" event.
The variable "ats_instant_statistics" is global and when the inner function of the second piece of code is called contains the correct id value (at least on my browser).
With "the inner function of the second piece of code" I mean this:
var click_type = jQuery(this).data('source');
jQuery.ajax('index.php?option=com_ats&view=attempts&format=json', {
dataType : 'json',
cache : false,
data : {
task : 'save',
ats_attempt_id : ats_instant_statistics,
update_clicks : click_type
}
});