app/soc/content/js/duplicate-slots-090825.js
changeset 2801 0ee67cc9bd20
parent 2800 cd9eed2b787e
child 2804 92397646e5dc
equal deleted inserted replaced
2800:cd9eed2b787e 2801:0ee67cc9bd20
       
     1 (function () {
       
     2   var duplicateSlots = window.duplicateSlots = function () {
       
     3   };
       
     4   // this variable will contain all the org details, and filled
       
     5   // incrementally
       
     6   var orgs_details = {};
       
     7   // this variable will contain all student/proposal data details,
       
     8   // filled incrementally
       
     9   var assigned_proposals = [];
       
    10 
       
    11   // public function to output actual HTML out of the data (cached or not)
       
    12   duplicateSlots.showDuplicatesHtml =
       
    13     function (orgs_details, student, student_key, proposals) {
       
    14       /*jslint undef:false */
       
    15       if (html_string === '') {
       
    16         jQuery("#div_duplicate_slots").html('');
       
    17         html_string = '<ul>';
       
    18       }
       
    19       html_string += [
       
    20         '<li>',
       
    21         '  Student: ',
       
    22         '    <strong>',
       
    23         '      <a href="/student/show/', student_key, '">', student.name,
       
    24         '</a>',
       
    25         '    </strong> ',
       
    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>");
       
    90     }
       
    91     // at the end, send the JSON for caching purposes
       
    92     jQuery.ajax({
       
    93       url: location.href,
       
    94       type: 'POST',
       
    95       processData: true,
       
    96       data: {result: JSON.stringify(to_json)},
       
    97       contentType: 'application/json',
       
    98       dataType: 'json'
       
    99     });
       
   100   }
       
   101 
       
   102   // private function to load a JSON and pushing the data to the
       
   103   // private global variables
       
   104   function loadSingleJSONData(data) {
       
   105     if (data) {
       
   106       // pushing org details
       
   107       jQuery.each(data.data.orgs, function (org_key, organization) {
       
   108         orgs_details[org_key] = organization;
       
   109       });
       
   110       // pushing proposals
       
   111       jQuery(data.data.proposals).each(
       
   112         function (intIndex, proposal) {
       
   113           // if this student_key is not yet present
       
   114           if (assigned_proposals[proposal.student_key] === undefined) {
       
   115             // create the object and insert general info
       
   116             assigned_proposals[proposal.student_key] = {};
       
   117             assigned_proposals[proposal.student_key].name =
       
   118               proposal.student_name;
       
   119             assigned_proposals[proposal.student_key].contact =
       
   120               proposal.student_contact;
       
   121             assigned_proposals[proposal.student_key].proposals = [];
       
   122           }
       
   123           // anyway, push the accepted proposals
       
   124           assigned_proposals[proposal.student_key].proposals.push(
       
   125             {
       
   126               "org_key" : proposal.org_key,
       
   127               "proposal_key" : proposal.key_name,
       
   128               "proposal_title": proposal.proposal_title
       
   129             }
       
   130           );
       
   131         }
       
   132       );
       
   133     }
       
   134   }
       
   135 
       
   136   function showDuplicates(url_to_query, OFFSET_LENGTH, NUMBER_OF_ORGS) {
       
   137     var current_offset = 0;
       
   138     orgs_details = {};
       
   139     assigned_proposals = [];
       
   140 
       
   141     // Here Ajax call is handled
       
   142     setTimeout(
       
   143       function () {
       
   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;
       
   208     }
       
   209 
       
   210     var successful_calls = 0;
       
   211 
       
   212     jQuery("#id_button_duplicate_slots").fadeOut("slow",
       
   213       function () {
       
   214         jQuery("#duplicates_progress_bar").progressBar(0);
       
   215         jQuery("#description_done").html("");
       
   216         // For every ajax success, bind this function to update user feedback
       
   217         jQuery(this).bind("ajaxSuccess", function () {
       
   218           successful_calls++;
       
   219           var percentage = Math.floor(100 * (successful_calls) / (ITERATIONS));
       
   220           jQuery("#duplicates_progress_bar").progressBar(percentage);
       
   221           jQuery("#description_progressbar").html([
       
   222             " Processed orgs chunk ", successful_calls, "/", ITERATIONS
       
   223           ].join(""));
       
   224           // If this is the last call, feedback the user and
       
   225           // print the duplicates data
       
   226           if (successful_calls === ITERATIONS) {
       
   227             jQuery("#applications_progress_bar").fadeOut("slow",
       
   228               function () {
       
   229                 jQuery("#duplicates_progress_bar").progressBar(0);
       
   230                 jQuery("#id_button_duplicate_slots").fadeIn("slow");
       
   231               }
       
   232             );
       
   233             jQuery("#description_progressbar").html("");
       
   234             jQuery("#description_done").html("<strong> Done!</strong>");
       
   235             jQuery("#duplicates_progress_bar").fadeOut("slow",
       
   236               function () {
       
   237                 jQuery("#id_button_duplicate_slots").val("Recalculate").fadeIn(
       
   238                   "slow",
       
   239                   function () {
       
   240                     // Call printing to HTML function with correct scope
       
   241                     printDuplicatesAndSendJSON.call(this_object);
       
   242                   }
       
   243                 );
       
   244               }
       
   245             );
       
   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         );
       
   259       }
       
   260     );
       
   261   };
       
   262 }());