2017-04-28 12:24:56 +08:00
/ *
* 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 .
*
2017-04-28 12:24:56 +08:00
* 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
* /
2017-03-23 00:15:02 +08:00
import I18n from 'i18n!sis_import'
import $ from 'jquery'
2017-05-24 23:19:24 +08:00
import htmlEscape from './str/htmlEscape'
import './jquery.ajaxJSON'
import './jquery.instructure_forms' /* formSubmit, formErrors */
import './jquery.instructure_misc_plugins' /* showIf, disableIf */
2017-03-23 00:15:02 +08:00
import 'jqueryui/progressbar'
2011-11-11 00:31:45 +08:00
2011-02-01 09:57:29 +08:00
$ ( document ) . ready ( function ( event ) {
var state = 'nothing' ;
2017-05-10 00:25:19 +08:00
2011-05-23 23:52:11 +08:00
$ ( "#batch_mode" ) . change ( function ( event ) {
2017-05-10 00:25:19 +08:00
$ ( '#batch_mode_term_id_label' ) . showIf ( $ ( this ) . attr ( 'checked' ) ) ;
$ ( '#batch_mode_term_id' ) . showIf ( $ ( this ) . attr ( 'checked' ) ) ;
2011-05-23 23:52:11 +08:00
} ) . change ( ) ;
2017-05-10 00:25:19 +08:00
2011-10-13 07:28:30 +08:00
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 ) {
2014-12-19 17:35:02 +08:00
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 ] ;
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 ) {
2014-12-19 17:35:02 +08:00
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 ] ;
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 '' ;
}
2017-03-24 07:03:32 +08:00
var output = "<ul><li>" + htmlEscape ( I18n . t ( 'headers.imported_items' , "Imported Items" ) ) + "<ul>" ;
2014-12-19 17:35:02 +08:00
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 ( ) {
2014-12-19 17:35:02 +08:00
$ ( "#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" ;
2012-06-14 06:26:33 +08:00
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 ) ;
2014-12-19 17:35:02 +08:00
$ ( ".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' ) {
2017-03-24 07:03:32 +08:00
var code = "sis_batch_" + sis _batch . id ;
2011-02-01 09:57:29 +08:00
$ ( ".progress_bar_holder" ) . hide ( ) ;
$ ( "#sis_importer" ) . hide ( ) ;
2011-06-28 23:48:56 +08:00
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 } ) ;
2014-12-19 17:35:02 +08:00
$ ( ".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 ( ) ;
2014-12-19 17:35:02 +08:00
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 ) ;
2014-12-19 17:35:02 +08:00
$ ( ".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 ( ) ;
2014-12-19 17:35:02 +08:00
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 ) ;
2014-12-19 17:35:02 +08:00
$ ( ".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 ) {
2012-06-14 06:26:33 +08:00
if ( data && data . id ) {
2011-02-01 09:57:29 +08:00
startPoll ( ) ;
} else {
//show error message
2012-07-25 01:33:50 +08:00
$ ( ".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 ) {
2011-06-28 23:48:56 +08:00
$ ( 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" ;
2012-06-14 06:26:33 +08:00
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 ( ) ;
} ) ;