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