(function () { "use strict"; angular .module('shared') .controller('unitAssignmentModal', unitAssignmentModal); unitAssignmentModal.$inject = ['$scope', 'close', 'unitAssignmentInfo', 'AuthGo', 'studentList', 'unitAssignment', 'messageHandler', 'RKConstants', 'windowService', 'SiteHelper', 'MessageBox']; function unitAssignmentModal($scope, close, unitAssignmentInfo, AuthGo, studentList, unitAssignment, messageHandler, RKConstants, windowService, SiteHelper, MessageBox) { var ctrl = this; var maxResourceLimit = 100; ctrl.cancel = ctrl.close = function() { studentList.clearSelected(); close(); }; ctrl.unitAssignmentInfo = unitAssignmentInfo; ctrl.kidsRosterUrl = AuthGo.getUrl('kaz', '/main/students'); ctrl.constructedResponseEnabled = false; ctrl.selectedEnglishMaterialTypes = {}; ctrl.selectedSpanishMaterialTypes = {}; ctrl.selectedVocabularyGameTypes = {}; ctrl.vocabularyGameTypes = unitAssignmentInfo.vocabulary_game_types !== undefined && unitAssignmentInfo.vocabulary_game_types !== null && unitAssignmentInfo.vocabulary_game_types.length > 0 ? unitAssignmentInfo.vocabulary_game_types : null; //console.log(ctrl.vocabularyGameTypes); ctrl.selectedMaterials = {}; ctrl.englishLanguageId = RKConstants.ENGLISH_LANGUAGE_ID; ctrl.spanishLanguageId = RKConstants.SPANISH_LANGUAGE_ID; getMaterialTypesForLanguage(); initializeSelections(); ctrl.switchAnyStudents = switchAnyStudents; ctrl.isValid = isValid; ctrl.submit = submit; function getMaterialTypesForLanguage() { ctrl.englishMaterialTypes = _.filter(ctrl.unitAssignmentInfo.unit_material_types, function(materialType) { return materialType.languages.indexOf(ctrl.englishLanguageId) != -1; }); ctrl.spanishMaterialTypes = _.filter(ctrl.unitAssignmentInfo.unit_material_types, function(materialType) { return materialType.languages.indexOf(ctrl.spanishLanguageId) != -1; }); } function switchAnyStudents() { if (studentList.pending) { return "pending"; } return studentList.get().length ? "some" : "none"; } function isValid() { return studentList.getSelected().length && hasAnySelectedMaterialTypes() && isValidISLParts(); } function submit() { MessageBox.show({ message: 'Change student assignment? Changing the student assignment will erase all progress on the current assignment.', responses: [ {id: 'nope', subtle: true, label: 'No'}, {id: 'confirm', confirm: true,label: 'Yes'} ] }).then(function (response) { if (response.id === 'confirm') { if(isValid()) { updateScienceAssignment(); } else { console.assert(isValid(), "Invalid submit attempt"); } } else if (response.id === 'nope') { ctrl.close(); } }); } function initializeSelections() { _.each(ctrl.englishMaterialTypes, function (englishMaterialType) { ctrl.selectedEnglishMaterialTypes[englishMaterialType.material_type_id] = true; }); _.each(ctrl.unitAssignmentInfo.unit_interactive_lessons.lessonParts, function (interactiveLessonPart) { ctrl.selectedMaterials[interactiveLessonPart.material.id] = true; }); } function hasAnySelectedMaterialTypes() { var hasEnglishMaterialTypeSelected = _.find(ctrl.selectedEnglishMaterialTypes, function(materialType) {return materialType;}); var hasSpanishMaterialTypeSelected = _.find(ctrl.selectedSpanishMaterialTypes, function(materialType) {return materialType;}); return hasEnglishMaterialTypeSelected || hasSpanishMaterialTypeSelected; } function isValidISLParts() { return !ctrl.selectedEnglishMaterialTypes[93] || getSelectedMaterials().length > 0; } function getSelectedMaterials() { if(!ctrl.selectedEnglishMaterialTypes[93]) { return []; } var selected = []; _.each(ctrl.selectedMaterials, function(enabled, materialId) { if(enabled) selected.push(materialId); }); return selected; } function updateScienceAssignment() { var limitLookup = null; var studentIDsAsDelimitedString = ""; for (var i = 0; i < studentList.getSelected().length; i++) { var studentId = studentList.getSelected()[i].student_id; studentIDsAsDelimitedString = studentIDsAsDelimitedString + studentId + ","; } if (studentIDsAsDelimitedString.length < 1) { studentIDsAsDelimitedString = 0; } else { studentIDsAsDelimitedString = studentIDsAsDelimitedString.slice(0, -1); } unitAssignment.getStudentResourceLimit(studentIDsAsDelimitedString).then(function (response) { limitLookup = JSON.parse(response.data); }); unitAssignment.updateMultipleScienceIndividualAssignment(_.pluck(studentList.getSelected(), 'student_id'), compileDataForRequest()) .then(function() { messageHandler.publishSuccess('Assignment created. Track progress on the ', 'assignments page.', assignmentRedirect); ctrl.close(); }, function(error) { var errorMessage = error.data.message; if (errorMessage.includes('limit')) { errorMessage = "The following students have exceeded their resource limit: "; for (var i = 0; i < studentList.getSelected().length; ++i) { var currentStudentId = studentList.getSelected()[i].student_id; if (limitLookup[currentStudentId] >= maxResourceLimit) { var student = studentList.getSelected()[i]; errorMessage = errorMessage + student.student_name + ", "; } } errorMessage = errorMessage.slice(0, -2); MessageBox.show({ message: errorMessage, responses: [ { id: 'ok', confirm: true, label: 'OK' } ] }).then(function (response) { ctrl.messageBoxOpen = false; },function (reason) { alert(errorMessage); }); } else { messageHandler.publishError('There was an error adding the assignment. Try again later.'); } }); } function assignmentRedirect() { windowService.redirect(AuthGo.getUrl(SiteHelper.KAZ_SITE_ABBREVIATION, '/main/Assign/action/readingPractice#!/tabs/science')); } function compileDataForRequest() { return { is_enabled: true, constructedResponseEnabled: ctrl.constructedResponseEnabled, scienceIndividualAssignment: ctrl.unitAssignmentInfo.unit_id, scienceIndividualAssignmentConfig: JSON.stringify(getScienceIndividualAssignmentConfig()), scienceIndividualAssignmentConfigMaterials: JSON.stringify(getSelectedMaterials()), vocabGameTypeIds: JSON.stringify(getSelectedVocabGameTypeIds()) }; } function getSelectedVocabGameTypeIds() { var selectedVocabIds = []; _.each(Object.keys(ctrl.selectedVocabularyGameTypes),function(key) { if (ctrl.selectedVocabularyGameTypes[key]) { selectedVocabIds.push(key); } }); return selectedVocabIds; } function getScienceIndividualAssignmentConfig() { return _.reduce(ctrl.unitAssignmentInfo.unit_material_types, function(memo, materialType) { var materialTypeId = materialType.material_type_id; var assignmentMaterialTypeConfig = { materialTypeID: materialTypeId, isChecked: ctrl.selectedEnglishMaterialTypes[materialTypeId] || ctrl.selectedSpanishMaterialTypes[materialTypeId], isEnglishChecked: !!ctrl.selectedEnglishMaterialTypes[materialTypeId], isSpanishChecked: !!ctrl.selectedSpanishMaterialTypes[materialTypeId] }; if(assignmentMaterialTypeConfig.isEnglishChecked || assignmentMaterialTypeConfig.isSpanishChecked) memo.push(assignmentMaterialTypeConfig); return memo; }, []); } } })();