app/soc/content/js/duplicate-slots-090324.js
changeset 2004 4d9e41c947fd
child 2005 c55051b20017
equal deleted inserted replaced
2003:ed24a0bd19d9 2004:4d9e41c947fd
       
     1 //http://www.digital-web.com/articles/scope_in_javascript/
       
     2 var duplicateSlots = new function() {
       
     3   // this variable will contain all the org details, and filled
       
     4   // incrementally
       
     5   var orgs_details = {};
       
     6   // this variable will contain all student/proposal data details,
       
     7   // filled incrementally
       
     8   var assigned_proposals = new Array();
       
     9 
       
    10   // public function to begin iterating load of JSONs and then call printing
       
    11   // of duplicates
       
    12 
       
    13   this.showDuplicatesInit = function() {
       
    14 
       
    15     // Remember this object for Javascript scoping
       
    16     var this_object = this;
       
    17     var NUMBER_OF_ORGS = number_of_orgs;
       
    18     var OFFSET_LENGTH = offset_length;
       
    19     // Variables to handle progress bar updating
       
    20     var ITERATIONS = (number_of_orgs % offset_length)==0 ? Math.floor(number_of_orgs/offset_length) : Math.floor(number_of_orgs/offset_length)+1;
       
    21     var successful_calls = 0;
       
    22 
       
    23     $("#id_button_duplicate_slots").fadeOut("slow",
       
    24       function() {
       
    25         $("#duplicates_progress_bar").progressBar(0);
       
    26         $("#description_done").html("");
       
    27         // For every ajax success, bind this function to update user feedback
       
    28         $(this).bind("ajaxSuccess", function() {
       
    29           successful_calls++;
       
    30           var percentage = Math.floor(100 * (successful_calls) / (ITERATIONS));
       
    31           $("#duplicates_progress_bar").progressBar(percentage);
       
    32           $("#description_progressbar").html(" Processed orgs chunk " + (successful_calls) + "/" + ITERATIONS);
       
    33           // If this is the last call, feedback the user and print the duplicates data
       
    34           if (successful_calls==ITERATIONS) {
       
    35             $("#applications_progress_bar").fadeOut("slow",
       
    36               function() {
       
    37                 $("#duplicates_progress_bar").progressBar(0);
       
    38                 $("#id_button_duplicate_slots").fadeIn("slow");
       
    39               }
       
    40             );
       
    41             $("#description_progressbar").html("");
       
    42             $("#description_done").html("<strong> Done!</strong>");
       
    43             $("#duplicates_progress_bar").fadeOut("slow",
       
    44               function() {
       
    45                 $("#id_button_duplicate_slots").val("Recalculate").fadeIn("slow",
       
    46                   function() {
       
    47                     // Call printing to HTML function with correct scope
       
    48                     printDuplicatesAndSendJSON.call(this_object);
       
    49                   }
       
    50                 );
       
    51               }
       
    52             );
       
    53           }
       
    54         });
       
    55         // Call the showDuplicates function for the first time with correct scope
       
    56         $("#duplicates_progress_bar").fadeIn("slow", showDuplicates.apply(this_object,[url_to_query,OFFSET_LENGTH,NUMBER_OF_ORGS]));
       
    57       }
       
    58     );
       
    59   }
       
    60 
       
    61   function showDuplicates(url_to_query,OFFSET_LENGTH,NUMBER_OF_ORGS) {
       
    62     var current_offset = 0;
       
    63 
       
    64     // Here Ajax call is handled
       
    65     setTimeout(function() {
       
    66       $.ajax({
       
    67         cache:false,
       
    68         mode: "sync",
       
    69         type: "POST",
       
    70         timeout: 1000000,
       
    71         dataType: "json",
       
    72         url: "/program/assigned_proposals/"+url_to_query+"?limit="+OFFSET_LENGTH+"&offset="+current_offset+"&_="+(new Date().getTime()),
       
    73         success: function (data, textStatus) {
       
    74           if (data) {
       
    75             // Load JSON data
       
    76             loadSingleJSONData(data);
       
    77           }
       
    78         },
       
    79         error: function(XMLHttpRequest, textStatus, errorThrown) {
       
    80           // if there is an error return the button and leave a try again message
       
    81           if (XMLHttpRequest!=undefined) {
       
    82             $("#id_button_duplicate_slots").fadeIn("slow", function() {
       
    83 	      $("#description_done").html("<strong class='error'> Error encountered, try again</strong>");
       
    84             });
       
    85           }
       
    86        }
       
    87       });
       
    88       current_offset+=OFFSET_LENGTH;
       
    89       if (current_offset<NUMBER_OF_ORGS) {
       
    90         setTimeout(arguments.callee,1);
       
    91       }
       
    92     },1);
       
    93     // This prevent page reloading after each ajax call
       
    94     return false;
       
    95   }
       
    96 
       
    97   // private function to load a JSON and pushing the data to the
       
    98   // private global variables
       
    99   function loadSingleJSONData(data) {
       
   100     if (data) {
       
   101       // pushing org details
       
   102       for (var org_key in data.data.orgs) {
       
   103         orgs_details[org_key] = data.data.orgs[org_key];
       
   104       }
       
   105       // pushing proposals
       
   106       $(data.data.proposals).each(
       
   107         function(intIndex, proposal) {
       
   108           // if this student_key is not yet present
       
   109           if (assigned_proposals[proposal.student_key]==undefined) {
       
   110             // create the object and insert general info
       
   111             assigned_proposals[proposal.student_key] = {};
       
   112             assigned_proposals[proposal.student_key].name = proposal.student_name;
       
   113             assigned_proposals[proposal.student_key].contact = proposal.student_contact;
       
   114             assigned_proposals[proposal.student_key].proposals = new Array();
       
   115           }
       
   116           // anyway, push the accepted proposals
       
   117           assigned_proposals[proposal.student_key].proposals.push(
       
   118             {
       
   119               "org_key" : proposal.org_key,
       
   120               "proposal_key" : proposal.key_name,
       
   121               "proposal_title": proposal.proposal_title
       
   122             }
       
   123           );
       
   124         }
       
   125       );
       
   126     }
       
   127   }
       
   128 
       
   129   // private function to generate the JSON to send for caching and calling
       
   130   // the actual function that will print the data
       
   131   function printDuplicatesAndSendJSON() {
       
   132     // JSON skeleton that need to be sent to the server
       
   133     var to_json = {
       
   134       "data": {
       
   135         "orgs" : orgs_details,
       
   136         "students": {}
       
   137       }
       
   138     }
       
   139     // for every student...
       
   140     for (var student_key in assigned_proposals) {
       
   141       var accepted_proposals = assigned_proposals[student_key].proposals.length;
       
   142       // if accepted proposal are less than 2, then ignore and continue the iteration
       
   143       if (accepted_proposals<2) continue;
       
   144       var student = assigned_proposals[student_key];
       
   145       // push this student to the caching JSON
       
   146       to_json.data.students[student_key] = student;
       
   147       var proposals = student.proposals;
       
   148       // call the function that prints the output html
       
   149       this.showDuplicatesHtml(orgs_details,student,student_key,proposals);
       
   150     }
       
   151     if (html_string=="") {
       
   152       $("#div_duplicate_slots").html("<strong>No duplicate slots found</strong>");
       
   153     }
       
   154     // at the end, send the JSON for caching purposes
       
   155     $.ajax({
       
   156       url: location.href,
       
   157       type: 'POST',
       
   158       processData: true,
       
   159       data: {result: JSON.stringify(to_json)},
       
   160       contentType: 'application/json',
       
   161       dataType: 'json',
       
   162     });
       
   163   }
       
   164 
       
   165   // public function to output actual HTML out of the data (cached or not)
       
   166   this.showDuplicatesHtml = function(orgs_details,student,student_key,proposals) {
       
   167     if (html_string==='') {
       
   168       html_string='<ul>';
       
   169     }
       
   170     html_string+= '<li>Student: <strong><a href="/student/show/'+student_key+'">'+student.name+'</a></strong> (<a href="mailto:'+student.contact+'">'+student.contact+'</a>)';
       
   171     html_string+='<ul>';
       
   172     $(proposals).each(
       
   173       function (intIndex, proposal) {
       
   174         html_string+='<li>Organization: <a href="/org/show/'+proposal.org_key+'">'+orgs_details[proposal.org_key].name+'</a>, admin: '+orgs_details[proposal.org_key].admin_name+' (<a href="mailto:'+orgs_details[proposal.org_key].admin_email+'">'+orgs_details[proposal.org_key].admin_email+'</a>)</li>';
       
   175         html_string+='<ul><li>Proposal: <a href="/student_proposal/show/'+proposal.proposal_key+'">'+proposal.proposal_title+'</a></li></ul>';
       
   176       }
       
   177     );
       
   178     html_string+='</ul></li>';
       
   179     html_string+='</ul>';
       
   180     $("#div_duplicate_slots").html(html_string);
       
   181   }
       
   182 }