app/soc/content/js/duplicate-slots-090331.js
changeset 2042 0b8136fe4d43
parent 2034 59de158c936d
child 2044 3aa6123be2a7
equal deleted inserted replaced
2041:9a9307b22312 2042:0b8136fe4d43
       
     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     // Remember this object for Javascript scoping
       
    15     html_string = '';
       
    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     orgs_details = {};
       
    64     assigned_proposals = new Array();
       
    65 
       
    66     // Here Ajax call is handled
       
    67     setTimeout(function() {
       
    68       $.ajax({
       
    69         cache:false,
       
    70         mode: "sync",
       
    71         type: "POST",
       
    72         timeout: 1000000,
       
    73         dataType: "json",
       
    74         url: "/program/assigned_proposals/"+url_to_query+"?limit="+OFFSET_LENGTH+"&offset="+current_offset+"&_="+(new Date().getTime()),
       
    75         success: function (data, textStatus) {
       
    76           if (data) {
       
    77             // Load JSON data
       
    78             loadSingleJSONData(data);
       
    79           }
       
    80         },
       
    81         error: function(XMLHttpRequest, textStatus, errorThrown) {
       
    82           // if there is an error return the button and leave a try again message
       
    83           if (XMLHttpRequest!=undefined) {
       
    84             $("#id_button_duplicate_slots").fadeIn("slow", function() {
       
    85 	      $("#description_done").html("<strong class='error'> Error encountered, try again</strong>");
       
    86             });
       
    87           }
       
    88        }
       
    89       });
       
    90       current_offset+=OFFSET_LENGTH;
       
    91       if (current_offset<NUMBER_OF_ORGS) {
       
    92         setTimeout(arguments.callee,1);
       
    93       }
       
    94     },1);
       
    95     // This prevent page reloading after each ajax call
       
    96     return false;
       
    97   }
       
    98 
       
    99   // private function to load a JSON and pushing the data to the
       
   100   // private global variables
       
   101   function loadSingleJSONData(data) {
       
   102     if (data) {
       
   103       // pushing org details
       
   104       for (var org_key in data.data.orgs) {
       
   105         orgs_details[org_key] = data.data.orgs[org_key];
       
   106       }
       
   107       // pushing proposals
       
   108       $(data.data.proposals).each(
       
   109         function(intIndex, proposal) {
       
   110           // if this student_key is not yet present
       
   111           if (assigned_proposals[proposal.student_key]==undefined) {
       
   112             // create the object and insert general info
       
   113             assigned_proposals[proposal.student_key] = {};
       
   114             assigned_proposals[proposal.student_key].name = proposal.student_name;
       
   115             assigned_proposals[proposal.student_key].contact = proposal.student_contact;
       
   116             assigned_proposals[proposal.student_key].proposals = new Array();
       
   117           }
       
   118           // anyway, push the accepted proposals
       
   119           assigned_proposals[proposal.student_key].proposals.push(
       
   120             {
       
   121               "org_key" : proposal.org_key,
       
   122               "proposal_key" : proposal.key_name,
       
   123               "proposal_title": proposal.proposal_title
       
   124             }
       
   125           );
       
   126         }
       
   127       );
       
   128     }
       
   129   }
       
   130 
       
   131   // private function to generate the JSON to send for caching and calling
       
   132   // the actual function that will print the data
       
   133   function printDuplicatesAndSendJSON() {
       
   134     // JSON skeleton that need to be sent to the server
       
   135     var to_json = {
       
   136       "data": {
       
   137         "orgs" : orgs_details,
       
   138         "students": {}
       
   139       }
       
   140     }
       
   141     // for every student...
       
   142     for (var student_key in assigned_proposals) {
       
   143       var accepted_proposals = assigned_proposals[student_key].proposals.length;
       
   144       // if accepted proposal are less than 2, then ignore and continue the iteration
       
   145       if (accepted_proposals<2) continue;
       
   146       var student = assigned_proposals[student_key];
       
   147       // push this student to the caching JSON
       
   148       to_json.data.students[student_key] = student;
       
   149       var proposals = student.proposals;
       
   150       // call the function that prints the output html
       
   151       this.showDuplicatesHtml(orgs_details,student,student_key,proposals);
       
   152     }
       
   153     if (html_string=="") {
       
   154       $("#div_duplicate_slots").html("<strong>No duplicate slots found</strong>");
       
   155     }
       
   156     // at the end, send the JSON for caching purposes
       
   157     $.ajax({
       
   158       url: location.href,
       
   159       type: 'POST',
       
   160       processData: true,
       
   161       data: {result: JSON.stringify(to_json)},
       
   162       contentType: 'application/json',
       
   163       dataType: 'json',
       
   164     });
       
   165   }
       
   166 
       
   167   // public function to output actual HTML out of the data (cached or not)
       
   168   this.showDuplicatesHtml = function(orgs_details,student,student_key,proposals) {
       
   169     if (html_string == '') {
       
   170       $("#div_duplicate_slots").html('');
       
   171       html_string='<ul>';
       
   172     }
       
   173     html_string+= '<li>Student: <strong><a href="/student/show/'+student_key+'">'+student.name+'</a></strong> (<a href="mailto:'+student.contact+'">'+student.contact+'</a>)';
       
   174     html_string+='<ul>';
       
   175     $(proposals).each(
       
   176       function (intIndex, proposal) {
       
   177         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>';
       
   178         html_string+='<ul><li>Proposal: <a href="/student_proposal/show/'+proposal.proposal_key+'">'+proposal.proposal_title+'</a></li></ul>';
       
   179       }
       
   180     );
       
   181     html_string+='</ul></li>';
       
   182     html_string+='</ul>';
       
   183     $("#div_duplicate_slots").html(html_string);
       
   184   }
       
   185 }