(function () { 'use strict'; var serviceId = 'myFormsAdminService'; angular.module('myformsAdmin').factory(serviceId, ['myFormsDataContext', '$filter', myFormsAdminService]); function myFormsAdminService(myFormsDataContext, $filter) { var service = { getColourPalettes: getColourPalettes, addQuestionImage: addQuestionImage, getQuestionImage: getQuestionImage, handleAnswerFormatsOnSaving: handleAnswerFormatsOnSaving, handleChoiceQuestionsOnLoading: handleChoiceQuestionsOnLoading, submitForm: submitForm, bruteForceSelect: bruteForceSelect, createFormFromTemplate: createFormFromTemplate }; var questionImages = []; return service; //preset colour palettes to choose from when creating a form function getColourPalettes() { return [ { set: "1", category: "Classic", palettes: [ { backgroundColour: '#FFFFFF', fontColour: '#444444', highlightColour: '#444444', name: 'Default' }, { backgroundColour: '#fcfcfc', fontColour: '#050505', highlightColour: '#099CFF', name: 'Blue' }, { backgroundColour: '#FFFFFF', fontColour: '#050505', highlightColour: '#D36F1F', name: 'Orange' }, { backgroundColour: '#fcfcfc', fontColour: '#050505', highlightColour: '#9DB40D', name: 'Green' }, { backgroundColour: '#FFFFFF', fontColour: '#050505', highlightColour: '#C63D0F', name: 'Red' }, { backgroundColour: '#fcfcfc', fontColour: '#050505', highlightColour: '#DF3E82', name: 'Pink' }, { backgroundColour: '#FFFFFF', fontColour: '#050505', highlightColour: '#9B50BA', name: 'Purple' }, { backgroundColour: '#FFFFFF', fontColour: '#666666', highlightColour: '#D76817', name: 'Tangerine' }, { backgroundColour: '#EEEEEE', fontColour: '#464646', highlightColour: '#00A6B6', name: 'Turquoise' }, { backgroundColour: '#FFFFFF', fontColour: '#666666', highlightColour: '#F45750', name: 'Rose' }, { backgroundColour: '#EEEEEE', fontColour: '#464646', highlightColour: '#2F88A7', name: 'Genetic Genre' }, { backgroundColour: '#FFFFFF', fontColour: '#666666', highlightColour: '#33C1C9', name: 'Ice' }, { backgroundColour: '#EEEEEE', fontColour: '#464646', highlightColour: '#49B571', name: 'Jungle' }, { backgroundColour: '#FFFEE9', fontColour: '#464646', highlightColour: '#050504', name: 'CC 1' }, { backgroundColour: '#FAECFA', fontColour: '#464646', highlightColour: '#050503', name: 'CC 2' }, { backgroundColour: '#EAF3FE', fontColour: '#464646', highlightColour: '#050502', name: 'CC 3' }, { backgroundColour: '#EEFAEE', fontColour: '#464646', highlightColour: '#050501', name: 'CC 4' } ] } ]; } function addQuestionImage(imageDate, image) { var alreadyPresent = false; for (var i = 0; i < questionImages.length; i++) { if (questionImages[i].image.lastModified === imageDate) { alreadyPresent = true; questionImages[i].image = image; break; } } if (!alreadyPresent) questionImages.push({ //questionId: questionId, image: image }); } //workaround to make ie perform properly on 'selectList' class elements (it selects very slowly otherwise) function bruteForceSelect() { var selectLists = document.querySelectorAll(".selectList"); for (var x = 0; x < selectLists.length; x++) { selectLists[x].parentNode.insertBefore(selectLists[x], selectLists[x]); } }; //submit the form function submitForm(isCreate, selectedForm) { //fool about with the choice questions so the data is in the correct format for the myforms API handleAnswerFormatsOnSaving(selectedForm); selectedForm.isFormComplete = true; if (selectedForm.formId) { //edit the form return myFormsDataContext.editForm(selectedForm.formId, selectedForm); } else { //create a new form return myFormsDataContext.createForm(selectedForm); } }; function getQuestionImage(imageDate){ for (var i = 0; i < questionImages.length; i++) { if (questionImages[i].image != null) { if (questionImages[i].image.lastModified === imageDate) { return questionImages[i].image; } } } } //to get the choice quetion response data in the form data into the format expected by MyForms we need to do some work function handleAnswerFormatsOnSaving(theForm) { //loop through the questionanswers (a question and answer pair) for (var i = 0; i < theForm.sections[0].questionAnswers.length; i++) { var questionAnswers = theForm.sections[0].questionAnswers[i]; //only for choice questions if (questionAnswers.question.isChoiceQuestion) { //create a choice array for the answer questionAnswers.answer.choices = []; //loop through the choices in the question for (var j = 0; j < questionAnswers.question.choices.length; j++) { //create the choice object var choice = { choice: questionAnswers.question.choices[j].choice, choiceId: questionAnswers.question.choices[j].choiceId, isChosen: false }; //if the choice has already been selected (i.e. we are editing the form with existing answers) set its flag if (questionAnswers.answer && questionAnswers.answer.chosen) { if (questionAnswers.answer.chosen.choiceId === questionAnswers.question.choices[j].choiceId) choice.isChosen = true; } //add the choice to the answer choice array if (choice.choiceId !== -1) questionAnswers.answer.choices.push(choice); } } //do a similar thing for checklist questions if (questionAnswers.question.isCheckListQuestion) { for (j = 0; j < questionAnswers.answer.chosen.length; j++) { questionAnswers.answer.chosen[j].isChosen = true; } questionAnswers.answer.choices = questionAnswers.answer.chosen; for (var j = 0; j < questionAnswers.question.choices.length; j++) { var isChosen = false; for (var k = 0; k < questionAnswers.answer.choices.length; k++) { if (questionAnswers.answer.choices[k].choiceId === questionAnswers.question.choices[j].choiceId) { isChosen = true; break; } } if (!isChosen) { questionAnswers.answer.choices.push( { choice: questionAnswers.question.choices[j].choice, choiceId: questionAnswers.question.choices[j].choiceId, isChosen: false }); } } } else if (questionAnswers.question.isTimeQuestion) { var hours = $filter('date')(questionAnswers.answer.timeAnswer, "HH"); var mins = $filter('date')(questionAnswers.answer.timeAnswer, "mm"); var timeAsDate = new Date(); timeAsDate.setUTCHours(hours); timeAsDate.setUTCMinutes(mins); questionAnswers.answer.timeAnswer = timeAsDate; } if (questionAnswers.question.isDateQuestion) { questionAnswers.answer.dateAnswer = $filter('date')(questionAnswers.answer.dateAnswer, "dd MMMM yyyy"); } } } function handleChoiceQuestionsOnLoading(theForm) { if (theForm.sections[0].questionAnswers) { for (var i = 0; i < theForm.sections[0].questionAnswers.length; i++) { var questionAnswers = theForm.sections[0].questionAnswers[i]; if (questionAnswers.question.isDateQuestion) { questionAnswers.answer.dateAnswer = $filter('date')(questionAnswers.answer.dateAnswer, "dd MMMM yyyy"); } if (questionAnswers.question.isTimeQuestion) { if (questionAnswers.answer.timeAnswer == null || questionAnswers.answer.timeAnswer == undefined) questionAnswers.answer.timeAnswer = new Date(); else { var m = moment(questionAnswers.answer.timeAnswer); var hours = m.hours(); var mins = m.minutes(); var timeAsDate = new Date(); timeAsDate.setHours(hours); timeAsDate.setMinutes(mins); questionAnswers.answer.timeAnswer = timeAsDate; } } if (!questionAnswers.answer.timeAnswer) { questionAnswers.answer.timeAnswer = new Date(); } if (questionAnswers.question.isChoiceQuestion) { if (questionAnswers.answer.choices) { for (var j = 0; j < questionAnswers.answer.choices.length; j++) { if (questionAnswers.answer.choices[j].isChosen) { questionAnswers.answer.chosen = { choice: questionAnswers.answer.choices[j].choice, choiceId: questionAnswers.answer.choices[j].choiceId }; break; } } } } } if (questionAnswers.question.isCheckListQuestion) { questionAnswers.answer.chosen = []; if (questionAnswers.answer.choices) { for (var k = 0; k < questionAnswers.answer.choices.length; k++) { if (questionAnswers.answer.choices[k].isChosen) questionAnswers.answer.chosen.push(questionAnswers.answer.choices[k]); } } } } } function getQuestionAnswers(template) { var questionAnswers = []; //go through all the questions and create a question / answer pair for (var i = 0; i < template.sections[0].questions.length; i++) { questionAnswers.push({ question: template.sections[0].questions[i], answer: {} }); } return questionAnswers; } //create a new form from its template function createFormFromTemplate(template, formId, questionAnswers) { var form = { formType: template.formType, formTemplateId: template.id, name: template.name, title: template.title, description: template.description, iconUri: template.iconUri, backgroundColour: template.backgroundColour, fontColour: template.fontColour, highlightColour:template.highlightColour, sections: [ { name: template.sections[0].name } ], externalId: template.externalId, isFeedbackGivenAfterQuiz: template.isFeedbackGivenAfterQuiz, isFeedbackGivenDuringQuiz: template.isFeedbackGivenDuringQuiz, id: formId, //might be null //activity fields targetEntriesNumber: template.targetEntriesNumber, individualCanCreate: template.individualCanCreate, individualCanDelete: template.individualCanDelete, individualCanEdit: template.individualCanEdit, individualCanView: template.individualCanView }; if(questionAnswers != null){ form.sections[0].questionAnswers = questionAnswers; }else{ form.sections[0].questionAnswers = getQuestionAnswers; } return form; } } })();