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