canvas-lms/public/javascripts/sis_import.js

204 lines
9.0 KiB
JavaScript
Raw Normal View History

/*
* Copyright (C) 2011 - present Instructure, Inc.
2011-02-01 09:57:29 +08:00
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2011-02-01 09:57:29 +08:00
*/
import I18n from 'i18n!sis_import'
import $ from 'jquery'
import htmlEscape from './str/htmlEscape'
import './jquery.ajaxJSON'
import './jquery.instructure_forms' /* formSubmit, formErrors */
import './jquery.instructure_misc_plugins' /* showIf, disableIf */
import 'jqueryui/progressbar'
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
2011-02-01 09:57:29 +08:00
$(document).ready(function(event) {
var state = 'nothing';
$("#batch_mode").change(function(event) {
$('#batch_mode_term_id_label').showIf($(this).attr('checked'));
$('#batch_mode_term_id').showIf($(this).attr('checked'));
}).change();
var $override_sis_stickiness = $("#override_sis_stickiness");
var $add_sis_stickiness = $("#add_sis_stickiness");
var $clear_sis_stickiness = $("#clear_sis_stickiness");
var $add_sis_stickiness_container = $("#add_sis_stickiness_container");
var $clear_sis_stickiness_container = $("#clear_sis_stickiness_container");
function updateSisCheckboxes(event) {
$add_sis_stickiness_container.showIf($override_sis_stickiness.attr('checked'));
$clear_sis_stickiness_container.showIf($override_sis_stickiness.attr('checked'));
$add_sis_stickiness.disableIf($clear_sis_stickiness.attr('checked'));
$clear_sis_stickiness.disableIf($add_sis_stickiness.attr('checked'));
}
$override_sis_stickiness.change(updateSisCheckboxes);
$add_sis_stickiness.change(updateSisCheckboxes);
$clear_sis_stickiness.change(updateSisCheckboxes);
updateSisCheckboxes(null);
2011-02-01 09:57:29 +08:00
function createMessageHtml(batch){
var output = "";
if(batch.processing_errors && batch.processing_errors.length > 0){
output += "<li>" + htmlEscape(I18n.t('headers.import_errors', "Errors that prevent importing")) + "\n<ul>";
2011-02-01 09:57:29 +08:00
for(var i in batch.processing_errors) {
var message = batch.processing_errors[i];
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
output += "<li>" + htmlEscape(message[0]) + " - " + htmlEscape(message[1]) + "</li>";
2011-02-01 09:57:29 +08:00
}
output += "</ul>\n</li>";
}
if(batch.processing_warnings && batch.processing_warnings.length > 0){
output += "<li>" + htmlEscape(I18n.t('headers.import_warnings', "Warnings")) + "\n<ul>";
2011-02-01 09:57:29 +08:00
for(var i in batch.processing_warnings) {
var message = batch.processing_warnings[i];
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
output += "<li>" + htmlEscape(message[0]) + " - " + htmlEscape(message[1]) + "</li>";
2011-02-01 09:57:29 +08:00
}
output += "</ul>\n</li>";
}
output += "</ul>";
return output;
}
function createCountsHtml(batch){
if(!(batch.data && batch.data.counts)){
return '';
}
var output = "<ul><li>" + htmlEscape(I18n.t('headers.imported_items', "Imported Items")) + "<ul>";
output += "<li>" + htmlEscape(I18n.t('import_counts.accounts', "Accounts: %{account_count}", {account_count: batch.data.counts.accounts})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.terms', "Terms: %{term_count}", {term_count: batch.data.counts.terms})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.courses', "Courses: %{course_count}", {course_count: batch.data.counts.courses})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.sections', "Sections: %{section_count}", {section_count: batch.data.counts.sections})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.users', "Users: %{user_count}", {user_count: batch.data.counts.users})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.enrollments', "Enrollments: %{enrollment_count}", {enrollment_count: batch.data.counts.enrollments})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.crosslists', "Crosslists: %{crosslist_count}", {crosslist_count: batch.data.counts.xlists})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.groups', "Groups: %{group_count}", {group_count: batch.data.counts.groups})) + "</li>";
output += "<li>" + htmlEscape(I18n.t('import_counts.group_enrollments', "Group Enrollments: %{group_enrollments_count}", {group_enrollments_count: batch.data.counts.group_memberships})) + "</li>";
2011-02-01 09:57:29 +08:00
output += "</ul></li></ul>";
return output
}
function startPoll() {
$("#sis_importer").html(htmlEscape(I18n.t('status.processing', "Processing")) + " <div style='font-size: 0.6em;'>" + htmlEscape(I18n.t('notices.processing_takes_awhile', "this may take a bit...")) + "</div>")
2011-02-01 09:57:29 +08:00
.attr('disabled', true);
$(".instruction").hide();
$(".progress_bar_holder").slideDown();
$(".copy_progress").progressbar();
state = "nothing";
var fakeTickCount = 0;
var tick = function() {
if(state == "nothing") {
fakeTickCount++;
var progress = ($(".copy_progress").progressbar('option', 'value') || 0) + 0.25;
if(fakeTickCount < 10) {
$(".copy_progress").progressbar('option', 'value', progress);
}
setTimeout(tick, 2000);
} else {
state = "nothing";
fakeTickCount = 0;
setTimeout(tick, 10000);
}
};
var checkup = function() {
var lastProgress = null;
var waitTime = 1500;
$.ajaxJSON(location.href, 'GET', {}, function(data) {
state = "updating";
var sis_batch = data;
2011-02-01 09:57:29 +08:00
var progress = 0;
if(sis_batch) {
progress = Math.max($(".copy_progress").progressbar('option', 'value') || 0, sis_batch.progress);
$(".copy_progress").progressbar('option', 'value', progress);
$("#import_log").empty();
}
if(!sis_batch || sis_batch.workflow_state == 'imported') {
$("#sis_importer").hide();
$(".copy_progress").progressbar('option', 'value', 100);
$(".progress_message").html($.raw(htmlEscape(I18n.t('messages.import_complete_success', "The import is complete and all records were successfully imported.")) + createCountsHtml(sis_batch)));
2011-02-01 09:57:29 +08:00
} else if(sis_batch.workflow_state == 'failed') {
var code = "sis_batch_" + sis_batch.id;
2011-02-01 09:57:29 +08:00
$(".progress_bar_holder").hide();
$("#sis_importer").hide();
var message = I18n.t('errors.import_failed_code', "There was an error importing your SIS data. No records were imported. Please notify your system administrator and give them the following code: \"%{code}\"", {code: code});
$(".sis_messages .sis_error_message").text(message);
2011-02-01 09:57:29 +08:00
$(".sis_messages").show();
} else if(sis_batch.workflow_state == 'failed_with_messages') {
$(".progress_bar_holder").hide();
$("#sis_importer").hide();
var message = htmlEscape(I18n.t('errors.import_failed_messages', "No SIS records were imported. The import failed with these messages:"));
2011-02-01 09:57:29 +08:00
message += createMessageHtml(sis_batch);
$(".sis_messages .sis_error_message").html($.raw(message));
2011-02-01 09:57:29 +08:00
$(".sis_messages").show();
} else if(sis_batch.workflow_state == 'imported_with_messages') {
$(".progress_bar_holder").hide();
$("#sis_importer").hide();
var message = htmlEscape(I18n.t('messages.import_complete_warnings', "The SIS data was imported but with these messages:"));
2011-02-01 09:57:29 +08:00
message += createMessageHtml(sis_batch);
message += createCountsHtml(sis_batch);
$(".sis_messages").show().html($.raw(message));
2011-02-01 09:57:29 +08:00
} else {
if(progress == lastProgress) {
waitTime = Math.max(waitTime + 500, 30000);
} else {
waitTime = 1500;
}
lastProgress = progress;
setTimeout(checkup, 1500);
}
}, function() {
setTimeout(checkup, 3000);
});
};
setTimeout(checkup, 2000);
setTimeout(tick, 1000)
}
$("#sis_importer").formSubmit({
fileUpload: true,
success: function(data) {
if(data && data.id) {
2011-02-01 09:57:29 +08:00
startPoll();
} else {
//show error message
$(".sis_messages .sis_error_message").text(data.error_message);
2011-02-01 09:57:29 +08:00
$(".sis_messages").show();
if(data.batch_in_progress){
startPoll();
}
}
},
error: function(data) {
$(this).find(".submit_button").attr('disabled', false).text(I18n.t('buttons.process_data', "Process Data"));
2011-02-01 09:57:29 +08:00
$(this).formErrors(data);
}
});
function check_if_importing() {
state = "checking";
$.ajaxJSON(location.href, 'GET', {}, function(data) {
state = "nothing";
var sis_batch = data;
2011-02-01 09:57:29 +08:00
var progress = 0;
if(sis_batch && (sis_batch.workflow_state == "importing" || sis_batch.workflow_state == "created")) {
state = "nothing";
startPoll();
}
});
}
check_if_importing();
});